Skip to content

Commit 3d17abe

Browse files
Resolve whitespace issue
1 parent dd61898 commit 3d17abe

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

linear_algebra/matrix_trace.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515
def trace(matrix: NDArray[float64]) -> float:
1616
"""
1717
Calculate the trace of a square matrix.
18+
1819
The trace is the sum of the diagonal elements of a square matrix.
20+
1921
Parameters:
2022
matrix (NDArray[float64]): A square matrix
23+
2124
Returns:
2225
float: The trace of the matrix
26+
2327
Raises:
2428
ValueError: If the matrix is not square
29+
2530
Examples:
2631
>>> import numpy as np
2732
>>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float)
@@ -47,8 +52,10 @@ def trace(matrix: NDArray[float64]) -> float:
4752
def trace_properties_demo(matrix: NDArray[float64]) -> dict:
4853
"""
4954
Demonstrate various properties of the trace operation.
55+
5056
Parameters:
5157
matrix (NDArray[float64]): A square matrix
58+
5259
Returns:
5360
dict: Dictionary containing trace properties and calculations
5461
"""
@@ -78,13 +85,14 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict:
7885
"scalar_factor": scalar,
7986
"identity_trace": tr_identity,
8087
"trace_equals_transpose": abs(tr - tr_transpose) < 1e-10,
81-
"scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10,
88+
"scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10
8289
}
8390

8491

8592
def test_trace() -> None:
8693
"""
8794
Test function for matrix trace calculation.
95+
8896
>>> test_trace() # self running tests
8997
"""
9098
# Test 1: 2x2 matrix
@@ -93,28 +101,28 @@ def test_trace() -> None:
93101
assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed"
94102

95103
# Test 2: 3x3 matrix
96-
matrix_3x3 = np.array(
97-
[[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float
98-
)
104+
matrix_3x3 = np.array([[2.0, -1.0, 3.0],
105+
[4.0, 5.0, -2.0],
106+
[1.0, 0.0, 7.0]], dtype=float)
99107
tr_3x3 = trace(matrix_3x3)
100108
assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed"
101109

102110
# Test 3: Identity matrix
103111
identity_4x4 = np.eye(4, dtype=float)
104112
tr_identity = trace(identity_4x4)
105-
assert abs(tr_identity - 4.0) < 1e-10, (
106-
"Identity matrix trace should equal dimension"
107-
)
113+
assert (
114+
abs(tr_identity - 4.0) < 1e-10
115+
), "Identity matrix trace should equal dimension"
108116

109117
# Test 4: Zero matrix
110118
zero_matrix = np.zeros((3, 3), dtype=float)
111119
tr_zero = trace(zero_matrix)
112120
assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace"
113121

114122
# Test 5: Trace properties
115-
test_matrix = np.array(
116-
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float
117-
)
123+
test_matrix = np.array([[1.0, 2.0, 3.0],
124+
[4.0, 5.0, 6.0],
125+
[7.0, 8.0, 9.0]], dtype=float)
118126
properties = trace_properties_demo(test_matrix)
119127
assert properties["trace_equals_transpose"], "Trace should equal transpose trace"
120128
assert properties["scalar_property_check"], "Scalar multiplication property failed"
@@ -123,9 +131,9 @@ def test_trace() -> None:
123131
diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0])
124132
tr_diagonal = trace(diagonal_matrix)
125133
expected = 1.0 + 2.0 + 3.0 + 4.0
126-
assert abs(tr_diagonal - expected) < 1e-10, (
127-
"Diagonal matrix trace should equal sum of diagonal elements"
128-
)
134+
assert (
135+
abs(tr_diagonal - expected) < 1e-10
136+
), "Diagonal matrix trace should equal sum of diagonal elements"
129137

130138

131139
if __name__ == "__main__":

0 commit comments

Comments
 (0)