|
15 | 15 | def trace(matrix: NDArray[float64]) -> float: |
16 | 16 | """ |
17 | 17 | Calculate the trace of a square matrix. |
18 | | - |
| 18 | +
|
19 | 19 | The trace is the sum of the diagonal elements of a square matrix. |
20 | 20 |
|
21 | 21 | Parameters: |
@@ -43,95 +43,99 @@ def trace(matrix: NDArray[float64]) -> float: |
43 | 43 | """ |
44 | 44 | if matrix.shape[0] != matrix.shape[1]: |
45 | 45 | raise ValueError("Matrix must be square") |
46 | | - |
| 46 | + |
47 | 47 | return float(np.sum(np.diag(matrix))) |
48 | 48 |
|
49 | 49 |
|
50 | 50 | def trace_properties_demo(matrix: NDArray[float64]) -> dict: |
51 | 51 | """ |
52 | 52 | Demonstrate various properties of the trace operation. |
53 | | - |
| 53 | +
|
54 | 54 | Parameters: |
55 | 55 | matrix (NDArray[float64]): A square matrix |
56 | | - |
| 56 | +
|
57 | 57 | Returns: |
58 | 58 | dict: Dictionary containing trace properties and calculations |
59 | 59 | """ |
60 | 60 | if matrix.shape[0] != matrix.shape[1]: |
61 | 61 | raise ValueError("Matrix must be square") |
62 | | - |
| 62 | + |
63 | 63 | n = matrix.shape[0] |
64 | | - |
| 64 | + |
65 | 65 | # Calculate trace |
66 | 66 | tr = trace(matrix) |
67 | | - |
| 67 | + |
68 | 68 | # Calculate transpose trace (should be equal to original) |
69 | 69 | tr_transpose = trace(matrix.T) |
70 | | - |
| 70 | + |
71 | 71 | # Calculate trace of scalar multiple |
72 | 72 | scalar = 2.0 |
73 | 73 | tr_scalar = trace(scalar * matrix) |
74 | | - |
| 74 | + |
75 | 75 | # Create identity matrix for comparison |
76 | 76 | identity = np.eye(n, dtype=float64) |
77 | 77 | tr_identity = trace(identity) |
78 | | - |
| 78 | + |
79 | 79 | return { |
80 | 80 | "original_trace": tr, |
81 | 81 | "transpose_trace": tr_transpose, |
82 | 82 | "scalar_multiple_trace": tr_scalar, |
83 | 83 | "scalar_factor": scalar, |
84 | 84 | "identity_trace": tr_identity, |
85 | 85 | "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, |
86 | | - "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 |
| 86 | + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10, |
87 | 87 | } |
88 | 88 |
|
89 | 89 |
|
90 | 90 | def test_trace() -> None: |
91 | 91 | """ |
92 | 92 | Test function for matrix trace calculation. |
93 | | - |
| 93 | +
|
94 | 94 | >>> test_trace() # self running tests |
95 | 95 | """ |
96 | 96 | # Test 1: 2x2 matrix |
97 | 97 | matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) |
98 | 98 | tr_2x2 = trace(matrix_2x2) |
99 | 99 | assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" |
100 | | - |
| 100 | + |
101 | 101 | # Test 2: 3x3 matrix |
102 | | - matrix_3x3 = np.array([[2.0, -1.0, 3.0], |
103 | | - [4.0, 5.0, -2.0], |
104 | | - [1.0, 0.0, 7.0]], dtype=float) |
| 102 | + matrix_3x3 = np.array( |
| 103 | + [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float |
| 104 | + ) |
105 | 105 | tr_3x3 = trace(matrix_3x3) |
106 | 106 | assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" |
107 | | - |
| 107 | + |
108 | 108 | # Test 3: Identity matrix |
109 | 109 | identity_4x4 = np.eye(4, dtype=float) |
110 | 110 | tr_identity = trace(identity_4x4) |
111 | | - assert abs(tr_identity - 4.0) < 1e-10, "Identity matrix trace should equal dimension" |
112 | | - |
| 111 | + assert abs(tr_identity - 4.0) < 1e-10, ( |
| 112 | + "Identity matrix trace should equal dimension" |
| 113 | + ) |
| 114 | + |
113 | 115 | # Test 4: Zero matrix |
114 | 116 | zero_matrix = np.zeros((3, 3), dtype=float) |
115 | 117 | tr_zero = trace(zero_matrix) |
116 | 118 | assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" |
117 | | - |
| 119 | + |
118 | 120 | # Test 5: Trace properties |
119 | | - test_matrix = np.array([[1.0, 2.0, 3.0], |
120 | | - [4.0, 5.0, 6.0], |
121 | | - [7.0, 8.0, 9.0]], dtype=float) |
| 121 | + test_matrix = np.array( |
| 122 | + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float |
| 123 | + ) |
122 | 124 | properties = trace_properties_demo(test_matrix) |
123 | 125 | assert properties["trace_equals_transpose"], "Trace should equal transpose trace" |
124 | 126 | assert properties["scalar_property_check"], "Scalar multiplication property failed" |
125 | | - |
| 127 | + |
126 | 128 | # Test 6: Diagonal matrix |
127 | 129 | diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) |
128 | 130 | tr_diagonal = trace(diagonal_matrix) |
129 | 131 | expected = 1.0 + 2.0 + 3.0 + 4.0 |
130 | | - assert abs(tr_diagonal - expected) < 1e-10, "Diagonal matrix trace should equal sum of diagonal elements" |
| 132 | + assert abs(tr_diagonal - expected) < 1e-10, ( |
| 133 | + "Diagonal matrix trace should equal sum of diagonal elements" |
| 134 | + ) |
131 | 135 |
|
132 | 136 |
|
133 | 137 | if __name__ == "__main__": |
134 | 138 | import doctest |
135 | | - |
| 139 | + |
136 | 140 | doctest.testmod() |
137 | 141 | test_trace() |
0 commit comments