From abc002577c1e5d8a1213f525191452510f3d1aaa Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Thu, 9 Oct 2025 15:29:00 +0530 Subject: [PATCH 01/16] added script demonstrates the implementation of the Sigmoid function. The sigmoid function is a logistic function, which describes growth as being initially exponential, but then slowing down and barely growing at all when a limit is reached. It's commonly used as an activation function in neural networks. --- .../activation_functions/sigmoid.py | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 neural_network/activation_functions/sigmoid.py diff --git a/neural_network/activation_functions/sigmoid.py b/neural_network/activation_functions/sigmoid.py new file mode 100644 index 000000000000..ace29ce4fb8c --- /dev/null +++ b/neural_network/activation_functions/sigmoid.py @@ -0,0 +1,40 @@ +""" +This script demonstrates the implementation of the Sigmoid function. + +The sigmoid function is a logistic function, which describes growth as being initially +exponential, but then slowing down and barely growing at all when a limit is reached. +It's commonly used as an activation function in neural networks. + +For more detailed information, you can refer to the following link: +https://en.wikipedia.org/wiki/Sigmoid_function +""" + +import numpy as np + + +def sigmoid(vector: np.ndarray) -> np.ndarray: + """ + Implements the sigmoid activation function. + + Parameters: + vector (np.ndarray): A vector that consists of numeric values + + Returns: + np.ndarray: Input vector after applying sigmoid activation function + + Formula: f(x) = 1 / (1 + e^(-x)) + + Examples: + >>> sigmoid(np.array([-1.0, 0.0, 1.0, 2.0])) + array([0.26894142, 0.5 , 0.73105858, 0.88079708]) + + >>> sigmoid(np.array([-5.0, -2.5, 2.5, 5.0])) + array([0.00669285, 0.07585818, 0.92414182, 0.99330715]) + """ + return 1 / (1 + np.exp(-vector)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From bb4c622f737d39ef5d2e78dde97909f252fd4997 Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Thu, 9 Oct 2025 17:22:58 +0530 Subject: [PATCH 02/16] Added Tanh Activation Function --- neural_network/activation_functions/tanh.py | 40 +++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 neural_network/activation_functions/tanh.py diff --git a/neural_network/activation_functions/tanh.py b/neural_network/activation_functions/tanh.py new file mode 100644 index 000000000000..4e14f80c131d --- /dev/null +++ b/neural_network/activation_functions/tanh.py @@ -0,0 +1,40 @@ +""" +This script demonstrates the implementation of the Hyperbolic Tangent (Tanh) function. + +The tanh function is a hyperbolic function that maps any real-valued input to a value +between -1 and 1. It's commonly used as an activation function in neural networks +and is a scaled version of the sigmoid function. + +For more detailed information, you can refer to the following link: +https://en.wikipedia.org/wiki/Hyperbolic_functions#Hyperbolic_tangent +""" + +import numpy as np + + +def tanh(vector: np.ndarray) -> np.ndarray: + """ + Implements the hyperbolic tangent (tanh) activation function. + + Parameters: + vector (np.ndarray): A vector that consists of numeric values + + Returns: + np.ndarray: Input vector after applying tanh activation function + + Formula: f(x) = (e^x - e^(-x)) / (e^x + e^(-x)) = (e^(2x) - 1) / (e^(2x) + 1) + + Examples: + >>> tanh(np.array([-1.0, 0.0, 1.0, 2.0])) + array([-0.76159416, 0. , 0.76159416, 0.96402758]) + + >>> tanh(np.array([-5.0, -2.5, 2.5, 5.0])) + array([-0.9999092, -0.9866143, 0.9866143, 0.9999092]) + """ + return np.tanh(vector) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From b3715537eca531e7afe1f3b4bd030c506d88f218 Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:19:38 +0530 Subject: [PATCH 03/16] Matrix determinant calculation using various methods --- linear_algebra/determinant.py | 197 ++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 linear_algebra/determinant.py diff --git a/linear_algebra/determinant.py b/linear_algebra/determinant.py new file mode 100644 index 000000000000..5051f207f51d --- /dev/null +++ b/linear_algebra/determinant.py @@ -0,0 +1,197 @@ +""" +Matrix determinant calculation using various methods. + +The determinant is a scalar value that characterizes a square matrix. +It provides important information about the matrix, such as whether it's invertible. + +Reference: https://en.wikipedia.org/wiki/Determinant +""" + +import numpy as np +from numpy import float64 +from numpy.typing import NDArray + + +def determinant_recursive(matrix: NDArray[float64]) -> float: + """ + Calculate the determinant of a square matrix using recursive cofactor expansion. + + This method is suitable for small matrices but becomes inefficient for large matrices. + + Parameters: + matrix (NDArray[float64]): A square matrix + + Returns: + float: The determinant of the matrix + + Raises: + ValueError: If the matrix is not square + + Examples: + >>> import numpy as np + >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) + >>> determinant_recursive(matrix) + -2.0 + + >>> matrix = np.array([[5.0]], dtype=float) + >>> determinant_recursive(matrix) + 5.0 + """ + if matrix.shape[0] != matrix.shape[1]: + raise ValueError("Matrix must be square") + + n = matrix.shape[0] + + # Base cases + if n == 1: + return float(matrix[0, 0]) + + if n == 2: + return float(matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]) + + # Recursive case: cofactor expansion along the first row + det = 0.0 + for col in range(n): + # Create submatrix by removing row 0 and column col + submatrix = np.delete(np.delete(matrix, 0, axis=0), col, axis=1) + + # Calculate cofactor + cofactor = ((-1) ** col) * matrix[0, col] * determinant_recursive(submatrix) + det += cofactor + + return det + + +def determinant_lu(matrix: NDArray[float64]) -> float: + """ + Calculate the determinant using LU decomposition. + + This method is more efficient for larger matrices than recursive expansion. + + Parameters: + matrix (NDArray[float64]): A square matrix + + Returns: + float: The determinant of the matrix + + Raises: + ValueError: If the matrix is not square + """ + if matrix.shape[0] != matrix.shape[1]: + raise ValueError("Matrix must be square") + + n = matrix.shape[0] + + # Create a copy to avoid modifying the original matrix + A = matrix.astype(float64, copy=True) + + # Keep track of row swaps for sign adjustment + swap_count = 0 + + # Forward elimination to get upper triangular matrix + for i in range(n): + # Find pivot + max_row = i + for k in range(i + 1, n): + if abs(A[k, i]) > abs(A[max_row, i]): + max_row = k + + # Swap rows if needed + if max_row != i: + A[[i, max_row]] = A[[max_row, i]] + swap_count += 1 + + # Check for singular matrix + if abs(A[i, i]) < 1e-14: + return 0.0 + + # Eliminate below pivot + for k in range(i + 1, n): + factor = A[k, i] / A[i, i] + for j in range(i, n): + A[k, j] -= factor * A[i, j] + + # Calculate determinant as product of diagonal elements + det = 1.0 + for i in range(n): + det *= A[i, i] + + # Adjust sign based on number of row swaps + if swap_count % 2 == 1: + det = -det + + return det + + +def determinant(matrix: NDArray[float64]) -> float: + """ + Calculate the determinant of a square matrix using the most appropriate method. + + Uses recursive expansion for small matrices (≤3x3) and LU decomposition for larger ones. + + Parameters: + matrix (NDArray[float64]): A square matrix + + Returns: + float: The determinant of the matrix + + Examples: + >>> import numpy as np + >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) + >>> determinant(matrix) + -2.0 + """ + if matrix.shape[0] != matrix.shape[1]: + raise ValueError("Matrix must be square") + + n = matrix.shape[0] + + # Use recursive method for small matrices, LU decomposition for larger ones + if n <= 3: + return determinant_recursive(matrix) + else: + return determinant_lu(matrix) + + +def test_determinant() -> None: + """ + Test function for matrix determinant calculation. + + >>> test_determinant() # self running tests + """ + # Test 1: 2x2 matrix + matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) + det_2x2 = determinant(matrix_2x2) + assert abs(det_2x2 - (-2.0)) < 1e-10, "2x2 determinant calculation failed" + + # Test 2: 3x3 matrix + matrix_3x3 = np.array([[2.0, -3.0, 1.0], + [2.0, 0.0, -1.0], + [1.0, 4.0, 5.0]], dtype=float) + det_3x3 = determinant(matrix_3x3) + assert abs(det_3x3 - 49.0) < 1e-10, "3x3 determinant calculation failed" + + # Test 3: Singular matrix + singular_matrix = np.array([[1.0, 2.0], [2.0, 4.0]], dtype=float) + det_singular = determinant(singular_matrix) + assert abs(det_singular) < 1e-10, "Singular matrix should have zero determinant" + + # Test 4: Identity matrix + identity_3x3 = np.eye(3, dtype=float) + det_identity = determinant(identity_3x3) + assert abs(det_identity - 1.0) < 1e-10, "Identity matrix should have determinant 1" + + # Test 5: Compare recursive and LU methods + test_matrix = np.array([[1.0, 2.0, 3.0], + [0.0, 1.0, 4.0], + [5.0, 6.0, 0.0]], dtype=float) + det_recursive = determinant_recursive(test_matrix) + det_lu = determinant_lu(test_matrix) + assert abs(det_recursive - det_lu) < 1e-10, "Recursive and LU methods should give same result" + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + test_determinant() From 249e64ecd6ad03e7aaa016b82321258c0b9601b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 12:51:15 +0000 Subject: [PATCH 04/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/determinant.py | 70 ++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/linear_algebra/determinant.py b/linear_algebra/determinant.py index 5051f207f51d..c9d30a71dff4 100644 --- a/linear_algebra/determinant.py +++ b/linear_algebra/determinant.py @@ -15,7 +15,7 @@ def determinant_recursive(matrix: NDArray[float64]) -> float: """ Calculate the determinant of a square matrix using recursive cofactor expansion. - + This method is suitable for small matrices but becomes inefficient for large matrices. Parameters: @@ -39,33 +39,33 @@ def determinant_recursive(matrix: NDArray[float64]) -> float: """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + n = matrix.shape[0] - + # Base cases if n == 1: return float(matrix[0, 0]) - + if n == 2: return float(matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]) - + # Recursive case: cofactor expansion along the first row det = 0.0 for col in range(n): # Create submatrix by removing row 0 and column col submatrix = np.delete(np.delete(matrix, 0, axis=0), col, axis=1) - + # Calculate cofactor cofactor = ((-1) ** col) * matrix[0, col] * determinant_recursive(submatrix) det += cofactor - + return det def determinant_lu(matrix: NDArray[float64]) -> float: """ Calculate the determinant using LU decomposition. - + This method is more efficient for larger matrices than recursive expansion. Parameters: @@ -79,15 +79,15 @@ def determinant_lu(matrix: NDArray[float64]) -> float: """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + n = matrix.shape[0] - + # Create a copy to avoid modifying the original matrix A = matrix.astype(float64, copy=True) - + # Keep track of row swaps for sign adjustment swap_count = 0 - + # Forward elimination to get upper triangular matrix for i in range(n): # Find pivot @@ -95,38 +95,38 @@ def determinant_lu(matrix: NDArray[float64]) -> float: for k in range(i + 1, n): if abs(A[k, i]) > abs(A[max_row, i]): max_row = k - + # Swap rows if needed if max_row != i: A[[i, max_row]] = A[[max_row, i]] swap_count += 1 - + # Check for singular matrix if abs(A[i, i]) < 1e-14: return 0.0 - + # Eliminate below pivot for k in range(i + 1, n): factor = A[k, i] / A[i, i] for j in range(i, n): A[k, j] -= factor * A[i, j] - + # Calculate determinant as product of diagonal elements det = 1.0 for i in range(n): det *= A[i, i] - + # Adjust sign based on number of row swaps if swap_count % 2 == 1: det = -det - + return det def determinant(matrix: NDArray[float64]) -> float: """ Calculate the determinant of a square matrix using the most appropriate method. - + Uses recursive expansion for small matrices (≤3x3) and LU decomposition for larger ones. Parameters: @@ -143,9 +143,9 @@ def determinant(matrix: NDArray[float64]) -> float: """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + n = matrix.shape[0] - + # Use recursive method for small matrices, LU decomposition for larger ones if n <= 3: return determinant_recursive(matrix) @@ -156,42 +156,44 @@ def determinant(matrix: NDArray[float64]) -> float: def test_determinant() -> None: """ Test function for matrix determinant calculation. - + >>> test_determinant() # self running tests """ # Test 1: 2x2 matrix matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) det_2x2 = determinant(matrix_2x2) assert abs(det_2x2 - (-2.0)) < 1e-10, "2x2 determinant calculation failed" - + # Test 2: 3x3 matrix - matrix_3x3 = np.array([[2.0, -3.0, 1.0], - [2.0, 0.0, -1.0], - [1.0, 4.0, 5.0]], dtype=float) + matrix_3x3 = np.array( + [[2.0, -3.0, 1.0], [2.0, 0.0, -1.0], [1.0, 4.0, 5.0]], dtype=float + ) det_3x3 = determinant(matrix_3x3) assert abs(det_3x3 - 49.0) < 1e-10, "3x3 determinant calculation failed" - + # Test 3: Singular matrix singular_matrix = np.array([[1.0, 2.0], [2.0, 4.0]], dtype=float) det_singular = determinant(singular_matrix) assert abs(det_singular) < 1e-10, "Singular matrix should have zero determinant" - + # Test 4: Identity matrix identity_3x3 = np.eye(3, dtype=float) det_identity = determinant(identity_3x3) assert abs(det_identity - 1.0) < 1e-10, "Identity matrix should have determinant 1" - + # Test 5: Compare recursive and LU methods - test_matrix = np.array([[1.0, 2.0, 3.0], - [0.0, 1.0, 4.0], - [5.0, 6.0, 0.0]], dtype=float) + test_matrix = np.array( + [[1.0, 2.0, 3.0], [0.0, 1.0, 4.0], [5.0, 6.0, 0.0]], dtype=float + ) det_recursive = determinant_recursive(test_matrix) det_lu = determinant_lu(test_matrix) - assert abs(det_recursive - det_lu) < 1e-10, "Recursive and LU methods should give same result" + assert abs(det_recursive - det_lu) < 1e-10, ( + "Recursive and LU methods should give same result" + ) if __name__ == "__main__": import doctest - + doctest.testmod() test_determinant() From 6205411418276c81acf1b4282e65734aa16d69c0 Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:22:29 +0530 Subject: [PATCH 05/16] Matrix trace calculation --- linear_algebra/matrix_trace.py | 137 +++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 linear_algebra/matrix_trace.py diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py new file mode 100644 index 000000000000..4a8e290579f8 --- /dev/null +++ b/linear_algebra/matrix_trace.py @@ -0,0 +1,137 @@ +""" +Matrix trace calculation. + +The trace of a square matrix is the sum of the elements on the main diagonal. +It's an important linear algebra operation with many applications. + +Reference: https://en.wikipedia.org/wiki/Trace_(linear_algebra) +""" + +import numpy as np +from numpy import float64 +from numpy.typing import NDArray + + +def trace(matrix: NDArray[float64]) -> float: + """ + Calculate the trace of a square matrix. + + The trace is the sum of the diagonal elements of a square matrix. + + Parameters: + matrix (NDArray[float64]): A square matrix + + Returns: + float: The trace of the matrix + + Raises: + ValueError: If the matrix is not square + + Examples: + >>> import numpy as np + >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) + >>> trace(matrix) + 5.0 + + >>> matrix = np.array([[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float) + >>> trace(matrix) + 14.0 + + >>> matrix = np.array([[5.0]], dtype=float) + >>> trace(matrix) + 5.0 + """ + if matrix.shape[0] != matrix.shape[1]: + raise ValueError("Matrix must be square") + + return float(np.sum(np.diag(matrix))) + + +def trace_properties_demo(matrix: NDArray[float64]) -> dict: + """ + Demonstrate various properties of the trace operation. + + Parameters: + matrix (NDArray[float64]): A square matrix + + Returns: + dict: Dictionary containing trace properties and calculations + """ + if matrix.shape[0] != matrix.shape[1]: + raise ValueError("Matrix must be square") + + n = matrix.shape[0] + + # Calculate trace + tr = trace(matrix) + + # Calculate transpose trace (should be equal to original) + tr_transpose = trace(matrix.T) + + # Calculate trace of scalar multiple + scalar = 2.0 + tr_scalar = trace(scalar * matrix) + + # Create identity matrix for comparison + identity = np.eye(n, dtype=float64) + tr_identity = trace(identity) + + return { + "original_trace": tr, + "transpose_trace": tr_transpose, + "scalar_multiple_trace": tr_scalar, + "scalar_factor": scalar, + "identity_trace": tr_identity, + "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 + } + + +def test_trace() -> None: + """ + Test function for matrix trace calculation. + + >>> test_trace() # self running tests + """ + # Test 1: 2x2 matrix + matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) + tr_2x2 = trace(matrix_2x2) + assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" + + # Test 2: 3x3 matrix + matrix_3x3 = np.array([[2.0, -1.0, 3.0], + [4.0, 5.0, -2.0], + [1.0, 0.0, 7.0]], dtype=float) + tr_3x3 = trace(matrix_3x3) + assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" + + # Test 3: Identity matrix + identity_4x4 = np.eye(4, dtype=float) + tr_identity = trace(identity_4x4) + assert abs(tr_identity - 4.0) < 1e-10, "Identity matrix trace should equal dimension" + + # Test 4: Zero matrix + zero_matrix = np.zeros((3, 3), dtype=float) + tr_zero = trace(zero_matrix) + assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" + + # Test 5: Trace properties + test_matrix = np.array([[1.0, 2.0, 3.0], + [4.0, 5.0, 6.0], + [7.0, 8.0, 9.0]], dtype=float) + properties = trace_properties_demo(test_matrix) + assert properties["trace_equals_transpose"], "Trace should equal transpose trace" + assert properties["scalar_property_check"], "Scalar multiplication property failed" + + # Test 6: Diagonal matrix + diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) + tr_diagonal = trace(diagonal_matrix) + expected = 1.0 + 2.0 + 3.0 + 4.0 + assert abs(tr_diagonal - expected) < 1e-10, "Diagonal matrix trace should equal sum of diagonal elements" + + +if __name__ == "__main__": + import doctest + + doctest.testmod() + test_trace() From 2f6508a69c0e2f1ecfb0c38e754be552d8339ca4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 12:54:30 +0000 Subject: [PATCH 06/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/matrix_trace.py | 56 ++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index 4a8e290579f8..5a1b0c5b7ea6 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -15,7 +15,7 @@ def trace(matrix: NDArray[float64]) -> float: """ Calculate the trace of a square matrix. - + The trace is the sum of the diagonal elements of a square matrix. Parameters: @@ -43,39 +43,39 @@ def trace(matrix: NDArray[float64]) -> float: """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + return float(np.sum(np.diag(matrix))) def trace_properties_demo(matrix: NDArray[float64]) -> dict: """ Demonstrate various properties of the trace operation. - + Parameters: matrix (NDArray[float64]): A square matrix - + Returns: dict: Dictionary containing trace properties and calculations """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + n = matrix.shape[0] - + # Calculate trace tr = trace(matrix) - + # Calculate transpose trace (should be equal to original) tr_transpose = trace(matrix.T) - + # Calculate trace of scalar multiple scalar = 2.0 tr_scalar = trace(scalar * matrix) - + # Create identity matrix for comparison identity = np.eye(n, dtype=float64) tr_identity = trace(identity) - + return { "original_trace": tr, "transpose_trace": tr_transpose, @@ -83,55 +83,59 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: "scalar_factor": scalar, "identity_trace": tr_identity, "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, - "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10, } def test_trace() -> None: """ Test function for matrix trace calculation. - + >>> test_trace() # self running tests """ # Test 1: 2x2 matrix matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) tr_2x2 = trace(matrix_2x2) assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" - + # Test 2: 3x3 matrix - matrix_3x3 = np.array([[2.0, -1.0, 3.0], - [4.0, 5.0, -2.0], - [1.0, 0.0, 7.0]], dtype=float) + matrix_3x3 = np.array( + [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float + ) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" - + # Test 3: Identity matrix identity_4x4 = np.eye(4, dtype=float) tr_identity = trace(identity_4x4) - assert abs(tr_identity - 4.0) < 1e-10, "Identity matrix trace should equal dimension" - + assert abs(tr_identity - 4.0) < 1e-10, ( + "Identity matrix trace should equal dimension" + ) + # Test 4: Zero matrix zero_matrix = np.zeros((3, 3), dtype=float) tr_zero = trace(zero_matrix) assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" - + # Test 5: Trace properties - test_matrix = np.array([[1.0, 2.0, 3.0], - [4.0, 5.0, 6.0], - [7.0, 8.0, 9.0]], dtype=float) + test_matrix = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float + ) properties = trace_properties_demo(test_matrix) assert properties["trace_equals_transpose"], "Trace should equal transpose trace" assert properties["scalar_property_check"], "Scalar multiplication property failed" - + # Test 6: Diagonal matrix diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) tr_diagonal = trace(diagonal_matrix) expected = 1.0 + 2.0 + 3.0 + 4.0 - assert abs(tr_diagonal - expected) < 1e-10, "Diagonal matrix trace should equal sum of diagonal elements" + assert abs(tr_diagonal - expected) < 1e-10, ( + "Diagonal matrix trace should equal sum of diagonal elements" + ) if __name__ == "__main__": import doctest - + doctest.testmod() test_trace() From 1bfbd426bd6bde609b08ecb0c342f697cbb62770 Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:26:11 +0530 Subject: [PATCH 07/16] refactor --- linear_algebra/matrix_trace.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index 5a1b0c5b7ea6..ed4f76e8c8d8 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -15,28 +15,21 @@ def trace(matrix: NDArray[float64]) -> float: """ Calculate the trace of a square matrix. - The trace is the sum of the diagonal elements of a square matrix. - Parameters: matrix (NDArray[float64]): A square matrix - Returns: float: The trace of the matrix - Raises: ValueError: If the matrix is not square - Examples: >>> import numpy as np >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) >>> trace(matrix) 5.0 - >>> matrix = np.array([[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float) >>> trace(matrix) 14.0 - >>> matrix = np.array([[5.0]], dtype=float) >>> trace(matrix) 5.0 @@ -50,10 +43,8 @@ def trace(matrix: NDArray[float64]) -> float: def trace_properties_demo(matrix: NDArray[float64]) -> dict: """ Demonstrate various properties of the trace operation. - Parameters: matrix (NDArray[float64]): A square matrix - Returns: dict: Dictionary containing trace properties and calculations """ From 4b8b24d3e545ab3c5cf0f3bcba3cfcbe3be32688 Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:28:28 +0530 Subject: [PATCH 08/16] refactor --- linear_algebra/determinant.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/linear_algebra/determinant.py b/linear_algebra/determinant.py index c9d30a71dff4..144d3316c35f 100644 --- a/linear_algebra/determinant.py +++ b/linear_algebra/determinant.py @@ -14,19 +14,16 @@ def determinant_recursive(matrix: NDArray[float64]) -> float: """ - Calculate the determinant of a square matrix using recursive cofactor expansion. - - This method is suitable for small matrices but becomes inefficient for large matrices. - + Calculate the determinant of a square matrix + using recursive cofactor expansion. + This method is suitable for + small matrices but becomes inefficient for large matrices. Parameters: matrix (NDArray[float64]): A square matrix - Returns: float: The determinant of the matrix - Raises: ValueError: If the matrix is not square - Examples: >>> import numpy as np >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) @@ -65,15 +62,12 @@ def determinant_recursive(matrix: NDArray[float64]) -> float: def determinant_lu(matrix: NDArray[float64]) -> float: """ Calculate the determinant using LU decomposition. - - This method is more efficient for larger matrices than recursive expansion. - + This method is more efficient for larger matrices + than recursive expansion. Parameters: matrix (NDArray[float64]): A square matrix - Returns: float: The determinant of the matrix - Raises: ValueError: If the matrix is not square """ @@ -125,16 +119,14 @@ def determinant_lu(matrix: NDArray[float64]) -> float: def determinant(matrix: NDArray[float64]) -> float: """ - Calculate the determinant of a square matrix using the most appropriate method. - - Uses recursive expansion for small matrices (≤3x3) and LU decomposition for larger ones. - + Calculate the determinant of a square matrix using + the most appropriate method. + Uses recursive expansion for small matrices (≤3x3) + and LU decomposition for larger ones. Parameters: matrix (NDArray[float64]): A square matrix - Returns: float: The determinant of the matrix - Examples: >>> import numpy as np >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) From 3e54f33281443701fea8d2d8484e5387f42193f4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 12:58:48 +0000 Subject: [PATCH 09/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/determinant.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/linear_algebra/determinant.py b/linear_algebra/determinant.py index 144d3316c35f..5a7e75e461ea 100644 --- a/linear_algebra/determinant.py +++ b/linear_algebra/determinant.py @@ -14,9 +14,9 @@ def determinant_recursive(matrix: NDArray[float64]) -> float: """ - Calculate the determinant of a square matrix + Calculate the determinant of a square matrix using recursive cofactor expansion. - This method is suitable for + This method is suitable for small matrices but becomes inefficient for large matrices. Parameters: matrix (NDArray[float64]): A square matrix @@ -62,7 +62,7 @@ def determinant_recursive(matrix: NDArray[float64]) -> float: def determinant_lu(matrix: NDArray[float64]) -> float: """ Calculate the determinant using LU decomposition. - This method is more efficient for larger matrices + This method is more efficient for larger matrices than recursive expansion. Parameters: matrix (NDArray[float64]): A square matrix @@ -119,9 +119,9 @@ def determinant_lu(matrix: NDArray[float64]) -> float: def determinant(matrix: NDArray[float64]) -> float: """ - Calculate the determinant of a square matrix using + Calculate the determinant of a square matrix using the most appropriate method. - Uses recursive expansion for small matrices (≤3x3) + Uses recursive expansion for small matrices (≤3x3) and LU decomposition for larger ones. Parameters: matrix (NDArray[float64]): A square matrix From e4ab15561a46ce069fb80eec572da3342166855e Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:32:15 +0530 Subject: [PATCH 10/16] convert uppercase variable to lowercase --- linear_algebra/determinant.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/linear_algebra/determinant.py b/linear_algebra/determinant.py index 5a7e75e461ea..b40a9005d7f5 100644 --- a/linear_algebra/determinant.py +++ b/linear_algebra/determinant.py @@ -77,7 +77,7 @@ def determinant_lu(matrix: NDArray[float64]) -> float: n = matrix.shape[0] # Create a copy to avoid modifying the original matrix - A = matrix.astype(float64, copy=True) + copy = matrix.astype(float64, copy=True) # Keep track of row swaps for sign adjustment swap_count = 0 @@ -87,28 +87,28 @@ def determinant_lu(matrix: NDArray[float64]) -> float: # Find pivot max_row = i for k in range(i + 1, n): - if abs(A[k, i]) > abs(A[max_row, i]): + if abs(copy[k, i]) > abs(copy[max_row, i]): max_row = k # Swap rows if needed if max_row != i: - A[[i, max_row]] = A[[max_row, i]] + copy[[i, max_row]] = copy[[max_row, i]] swap_count += 1 # Check for singular matrix - if abs(A[i, i]) < 1e-14: + if abs(copy[i, i]) < 1e-14: return 0.0 # Eliminate below pivot for k in range(i + 1, n): - factor = A[k, i] / A[i, i] + factor = copy[k, i] / copy[i, i] for j in range(i, n): - A[k, j] -= factor * A[i, j] + copy[k, j] -= factor * copy[i, j] # Calculate determinant as product of diagonal elements det = 1.0 for i in range(n): - det *= A[i, i] + det *= copy[i, i] # Adjust sign based on number of row swaps if swap_count % 2 == 1: From 97fb398e21dec902e655761d9ddcf71daec85f0c Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:34:32 +0530 Subject: [PATCH 11/16] Format 3x3 matrix for better readability --- linear_algebra/matrix_trace.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index ed4f76e8c8d8..b50a828987eb 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -91,7 +91,10 @@ def test_trace() -> None: # Test 2: 3x3 matrix matrix_3x3 = np.array( - [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float + [[2.0, -1.0, 3.0], + [4.0, 5.0, -2.0], + [1.0, 0.0, 7.0]], + dtype=float ) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" From 4f1214e11e69fb4739146a9f9947c74546248600 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:04:55 +0000 Subject: [PATCH 12/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/matrix_trace.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index b50a828987eb..ed4f76e8c8d8 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -91,10 +91,7 @@ def test_trace() -> None: # Test 2: 3x3 matrix matrix_3x3 = np.array( - [[2.0, -1.0, 3.0], - [4.0, 5.0, -2.0], - [1.0, 0.0, 7.0]], - dtype=float + [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float ) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" From 1dd58fe3a6f03d3f36ff6eb54a395d0805212871 Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:42:55 +0530 Subject: [PATCH 13/16] line length error resolve --- linear_algebra/matrix_trace.py | 65 ++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index ed4f76e8c8d8..5bbaa9e16615 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -14,7 +14,7 @@ def trace(matrix: NDArray[float64]) -> float: """ - Calculate the trace of a square matrix. + Calculate the trace of a square matrix. The trace is the sum of the diagonal elements of a square matrix. Parameters: matrix (NDArray[float64]): A square matrix @@ -27,16 +27,20 @@ def trace(matrix: NDArray[float64]) -> float: >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) >>> trace(matrix) 5.0 - >>> matrix = np.array([[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float) + + >>> matrix = np.array( + ... [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float + ... ) >>> trace(matrix) 14.0 + >>> matrix = np.array([[5.0]], dtype=float) >>> trace(matrix) 5.0 """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + return float(np.sum(np.diag(matrix))) @@ -44,29 +48,29 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: """ Demonstrate various properties of the trace operation. Parameters: - matrix (NDArray[float64]): A square matrix + matrix (NDArray[float64]): A square matrix Returns: dict: Dictionary containing trace properties and calculations """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + n = matrix.shape[0] - + # Calculate trace tr = trace(matrix) - + # Calculate transpose trace (should be equal to original) tr_transpose = trace(matrix.T) - + # Calculate trace of scalar multiple scalar = 2.0 tr_scalar = trace(scalar * matrix) - + # Create identity matrix for comparison identity = np.eye(n, dtype=float64) tr_identity = trace(identity) - + return { "original_trace": tr, "transpose_trace": tr_transpose, @@ -74,59 +78,58 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: "scalar_factor": scalar, "identity_trace": tr_identity, "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, - "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10, + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 } def test_trace() -> None: """ - Test function for matrix trace calculation. - + Test function for matrix trace calculation. >>> test_trace() # self running tests """ # Test 1: 2x2 matrix matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) tr_2x2 = trace(matrix_2x2) assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" - + # Test 2: 3x3 matrix - matrix_3x3 = np.array( - [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float - ) + matrix_3x3 = np.array([[2.0, -1.0, 3.0], + [4.0, 5.0, -2.0], + [1.0, 0.0, 7.0]], dtype=float) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" - + # Test 3: Identity matrix identity_4x4 = np.eye(4, dtype=float) tr_identity = trace(identity_4x4) - assert abs(tr_identity - 4.0) < 1e-10, ( - "Identity matrix trace should equal dimension" - ) - + assert ( + abs(tr_identity - 4.0) < 1e-10 + ), "Identity matrix trace should equal dimension" + # Test 4: Zero matrix zero_matrix = np.zeros((3, 3), dtype=float) tr_zero = trace(zero_matrix) assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" - + # Test 5: Trace properties - test_matrix = np.array( - [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float - ) + test_matrix = np.array([[1.0, 2.0, 3.0], + [4.0, 5.0, 6.0], + [7.0, 8.0, 9.0]], dtype=float) properties = trace_properties_demo(test_matrix) assert properties["trace_equals_transpose"], "Trace should equal transpose trace" assert properties["scalar_property_check"], "Scalar multiplication property failed" - + # Test 6: Diagonal matrix diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) tr_diagonal = trace(diagonal_matrix) expected = 1.0 + 2.0 + 3.0 + 4.0 - assert abs(tr_diagonal - expected) < 1e-10, ( - "Diagonal matrix trace should equal sum of diagonal elements" - ) + assert ( + abs(tr_diagonal - expected) < 1e-10 + ), "Diagonal matrix trace should equal sum of diagonal elements" if __name__ == "__main__": import doctest - + doctest.testmod() test_trace() From dd6189863b73ef40cbbd5b9f56317d8051af296f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:13:54 +0000 Subject: [PATCH 14/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/matrix_trace.py | 58 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index 5bbaa9e16615..5c56b6c160ea 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -14,7 +14,7 @@ def trace(matrix: NDArray[float64]) -> float: """ - Calculate the trace of a square matrix. + Calculate the trace of a square matrix. The trace is the sum of the diagonal elements of a square matrix. Parameters: matrix (NDArray[float64]): A square matrix @@ -40,7 +40,7 @@ def trace(matrix: NDArray[float64]) -> float: """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + return float(np.sum(np.diag(matrix))) @@ -48,29 +48,29 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: """ Demonstrate various properties of the trace operation. Parameters: - matrix (NDArray[float64]): A square matrix + matrix (NDArray[float64]): A square matrix Returns: dict: Dictionary containing trace properties and calculations """ if matrix.shape[0] != matrix.shape[1]: raise ValueError("Matrix must be square") - + n = matrix.shape[0] - + # Calculate trace tr = trace(matrix) - + # Calculate transpose trace (should be equal to original) tr_transpose = trace(matrix.T) - + # Calculate trace of scalar multiple scalar = 2.0 tr_scalar = trace(scalar * matrix) - + # Create identity matrix for comparison identity = np.eye(n, dtype=float64) tr_identity = trace(identity) - + return { "original_trace": tr, "transpose_trace": tr_transpose, @@ -78,58 +78,58 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: "scalar_factor": scalar, "identity_trace": tr_identity, "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, - "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10, } def test_trace() -> None: """ - Test function for matrix trace calculation. + Test function for matrix trace calculation. >>> test_trace() # self running tests """ # Test 1: 2x2 matrix matrix_2x2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) tr_2x2 = trace(matrix_2x2) assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" - + # Test 2: 3x3 matrix - matrix_3x3 = np.array([[2.0, -1.0, 3.0], - [4.0, 5.0, -2.0], - [1.0, 0.0, 7.0]], dtype=float) + matrix_3x3 = np.array( + [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float + ) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" - + # Test 3: Identity matrix identity_4x4 = np.eye(4, dtype=float) tr_identity = trace(identity_4x4) - assert ( - abs(tr_identity - 4.0) < 1e-10 - ), "Identity matrix trace should equal dimension" - + assert abs(tr_identity - 4.0) < 1e-10, ( + "Identity matrix trace should equal dimension" + ) + # Test 4: Zero matrix zero_matrix = np.zeros((3, 3), dtype=float) tr_zero = trace(zero_matrix) assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" - + # Test 5: Trace properties - test_matrix = np.array([[1.0, 2.0, 3.0], - [4.0, 5.0, 6.0], - [7.0, 8.0, 9.0]], dtype=float) + test_matrix = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float + ) properties = trace_properties_demo(test_matrix) assert properties["trace_equals_transpose"], "Trace should equal transpose trace" assert properties["scalar_property_check"], "Scalar multiplication property failed" - + # Test 6: Diagonal matrix diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) tr_diagonal = trace(diagonal_matrix) expected = 1.0 + 2.0 + 3.0 + 4.0 - assert ( - abs(tr_diagonal - expected) < 1e-10 - ), "Diagonal matrix trace should equal sum of diagonal elements" + assert abs(tr_diagonal - expected) < 1e-10, ( + "Diagonal matrix trace should equal sum of diagonal elements" + ) if __name__ == "__main__": import doctest - + doctest.testmod() test_trace() From 3d17abe8a993ae84860f51d1a6e6467fe18992cf Mon Sep 17 00:00:00 2001 From: Mathasuriya-infy Date: Fri, 24 Oct 2025 18:50:19 +0530 Subject: [PATCH 15/16] Resolve whitespace issue --- linear_algebra/matrix_trace.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index 5c56b6c160ea..af4d16d2feda 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -15,13 +15,18 @@ def trace(matrix: NDArray[float64]) -> float: """ Calculate the trace of a square matrix. + The trace is the sum of the diagonal elements of a square matrix. + Parameters: matrix (NDArray[float64]): A square matrix + Returns: float: The trace of the matrix + Raises: ValueError: If the matrix is not square + Examples: >>> import numpy as np >>> matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=float) @@ -47,8 +52,10 @@ def trace(matrix: NDArray[float64]) -> float: def trace_properties_demo(matrix: NDArray[float64]) -> dict: """ Demonstrate various properties of the trace operation. + Parameters: matrix (NDArray[float64]): A square matrix + Returns: dict: Dictionary containing trace properties and calculations """ @@ -78,13 +85,14 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: "scalar_factor": scalar, "identity_trace": tr_identity, "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, - "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10, + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 } def test_trace() -> None: """ Test function for matrix trace calculation. + >>> test_trace() # self running tests """ # Test 1: 2x2 matrix @@ -93,18 +101,18 @@ def test_trace() -> None: assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" # Test 2: 3x3 matrix - matrix_3x3 = np.array( - [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float - ) + matrix_3x3 = np.array([[2.0, -1.0, 3.0], + [4.0, 5.0, -2.0], + [1.0, 0.0, 7.0]], dtype=float) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" # Test 3: Identity matrix identity_4x4 = np.eye(4, dtype=float) tr_identity = trace(identity_4x4) - assert abs(tr_identity - 4.0) < 1e-10, ( - "Identity matrix trace should equal dimension" - ) + assert ( + abs(tr_identity - 4.0) < 1e-10 + ), "Identity matrix trace should equal dimension" # Test 4: Zero matrix zero_matrix = np.zeros((3, 3), dtype=float) @@ -112,9 +120,9 @@ def test_trace() -> None: assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" # Test 5: Trace properties - test_matrix = np.array( - [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float - ) + test_matrix = np.array([[1.0, 2.0, 3.0], + [4.0, 5.0, 6.0], + [7.0, 8.0, 9.0]], dtype=float) properties = trace_properties_demo(test_matrix) assert properties["trace_equals_transpose"], "Trace should equal transpose trace" assert properties["scalar_property_check"], "Scalar multiplication property failed" @@ -123,9 +131,9 @@ def test_trace() -> None: diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) tr_diagonal = trace(diagonal_matrix) expected = 1.0 + 2.0 + 3.0 + 4.0 - assert abs(tr_diagonal - expected) < 1e-10, ( - "Diagonal matrix trace should equal sum of diagonal elements" - ) + assert ( + abs(tr_diagonal - expected) < 1e-10 + ), "Diagonal matrix trace should equal sum of diagonal elements" if __name__ == "__main__": From eb3866a14619caed6c030880e72359e1123b608d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:20:42 +0000 Subject: [PATCH 16/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- linear_algebra/matrix_trace.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/linear_algebra/matrix_trace.py b/linear_algebra/matrix_trace.py index af4d16d2feda..075117025680 100644 --- a/linear_algebra/matrix_trace.py +++ b/linear_algebra/matrix_trace.py @@ -85,7 +85,7 @@ def trace_properties_demo(matrix: NDArray[float64]) -> dict: "scalar_factor": scalar, "identity_trace": tr_identity, "trace_equals_transpose": abs(tr - tr_transpose) < 1e-10, - "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10 + "scalar_property_check": abs(tr_scalar - scalar * tr) < 1e-10, } @@ -101,18 +101,18 @@ def test_trace() -> None: assert abs(tr_2x2 - 5.0) < 1e-10, "2x2 trace calculation failed" # Test 2: 3x3 matrix - matrix_3x3 = np.array([[2.0, -1.0, 3.0], - [4.0, 5.0, -2.0], - [1.0, 0.0, 7.0]], dtype=float) + matrix_3x3 = np.array( + [[2.0, -1.0, 3.0], [4.0, 5.0, -2.0], [1.0, 0.0, 7.0]], dtype=float + ) tr_3x3 = trace(matrix_3x3) assert abs(tr_3x3 - 14.0) < 1e-10, "3x3 trace calculation failed" # Test 3: Identity matrix identity_4x4 = np.eye(4, dtype=float) tr_identity = trace(identity_4x4) - assert ( - abs(tr_identity - 4.0) < 1e-10 - ), "Identity matrix trace should equal dimension" + assert abs(tr_identity - 4.0) < 1e-10, ( + "Identity matrix trace should equal dimension" + ) # Test 4: Zero matrix zero_matrix = np.zeros((3, 3), dtype=float) @@ -120,9 +120,9 @@ def test_trace() -> None: assert abs(tr_zero) < 1e-10, "Zero matrix should have zero trace" # Test 5: Trace properties - test_matrix = np.array([[1.0, 2.0, 3.0], - [4.0, 5.0, 6.0], - [7.0, 8.0, 9.0]], dtype=float) + test_matrix = np.array( + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], dtype=float + ) properties = trace_properties_demo(test_matrix) assert properties["trace_equals_transpose"], "Trace should equal transpose trace" assert properties["scalar_property_check"], "Scalar multiplication property failed" @@ -131,9 +131,9 @@ def test_trace() -> None: diagonal_matrix = np.diag([1.0, 2.0, 3.0, 4.0]) tr_diagonal = trace(diagonal_matrix) expected = 1.0 + 2.0 + 3.0 + 4.0 - assert ( - abs(tr_diagonal - expected) < 1e-10 - ), "Diagonal matrix trace should equal sum of diagonal elements" + assert abs(tr_diagonal - expected) < 1e-10, ( + "Diagonal matrix trace should equal sum of diagonal elements" + ) if __name__ == "__main__":