From 349c78684ad46c6d14d97bd9f34c1d10bce22cbd Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 07:47:20 +0900 Subject: [PATCH 01/31] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base_matrix_cholesky_decomposition.hpp | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/base_matrix/base_matrix_cholesky_decomposition.hpp b/base_matrix/base_matrix_cholesky_decomposition.hpp index 0494a65..d7becf3 100644 --- a/base_matrix/base_matrix_cholesky_decomposition.hpp +++ b/base_matrix/base_matrix_cholesky_decomposition.hpp @@ -1,3 +1,17 @@ +/** + * @file base_matrix_cholesky_decomposition.hpp + * @brief Provides Cholesky decomposition implementations for dense, diagonal, + * and sparse matrices. + * + * This header defines a set of template functions within the Base::Matrix + * namespace for performing Cholesky decomposition on different matrix types, + * including dense matrices, diagonal matrices, and compiled sparse matrices. + * The decomposition is implemented for fixed-size matrices and supports error + * handling for non-positive-definite matrices. + * + * All functions include error handling for non-positive-definite matrices and + * return a fallback matrix if decomposition fails. + */ #ifndef __BASE_MATRIX_CHOLESKY_DECOMPOSITION_HPP__ #define __BASE_MATRIX_CHOLESKY_DECOMPOSITION_HPP__ @@ -14,6 +28,28 @@ namespace Base { namespace Matrix { +/** + * @brief Computes the Cholesky decomposition of a symmetric, positive-definite + * matrix. + * + * This function performs the Cholesky decomposition on the input matrix U, + * producing an upper-triangular matrix Y such that U = Y^T * Y. If the + * decomposition fails due to non-positive-definite input or near-zero division, + * the function sets the zero_div_flag to true and returns the fallback matrix + * Y_b. + * + * @tparam T Numeric type of the matrix elements (e.g., float, double). + * @tparam M The dimension of the square matrix. + * @param U The input symmetric, positive-definite matrix to decompose (size M x + * M). + * @param Y_b The fallback matrix to return if decomposition fails. + * @param division_min Minimum value to avoid division by zero or very small + * numbers. + * @param[out] zero_div_flag Set to true if a zero or negative value is + * encountered during decomposition. + * @return Matrix The upper-triangular matrix resulting from the + * Cholesky decomposition, or Y_b on failure. + */ template inline Matrix cholesky_decomposition(const Matrix &U, const Matrix &Y_b, @@ -72,6 +108,28 @@ cholesky_decomposition(const Matrix &U, const Matrix &Y_b, return Y; } +/** + * @brief Computes the Cholesky decomposition for a diagonal matrix. + * + * This function calculates the Cholesky decomposition of a diagonal matrix `U`. + * The result is a diagonal matrix `Y` such that `Y * Y^T = U`, where each + * diagonal element of `Y` is the square root of the corresponding element in + * `U`. + * + * If any diagonal element of `U` is negative, the function sets the + * `zero_div_flag` to true, assigns the fallback matrix `Y_b` to `Y`, and + * terminates the computation. + * + * @tparam T The numeric type of the matrix elements. + * @tparam M The size of the diagonal matrix. + * @param U The input diagonal matrix to decompose. + * @param Y_b The fallback diagonal matrix to use if a negative value is + * encountered. + * @param zero_div_flag Reference to a boolean flag that is set to true if a + * negative diagonal element is found in `U`. + * @return The resulting diagonal matrix from the Cholesky decomposition, or + * `Y_b` if a negative value is encountered. + */ template inline DiagMatrix cholesky_decomposition_diag(const DiagMatrix &U, const DiagMatrix &Y_b, @@ -91,6 +149,32 @@ inline DiagMatrix cholesky_decomposition_diag(const DiagMatrix &U, return Y; } +/** + * @brief Computes the Cholesky decomposition of a sparse symmetric + * positive-definite matrix. + * + * This function takes a sparse matrix in compiled format and computes its + * Cholesky decomposition, returning the upper-triangular matrix Y such that U = + * Y^T * Y. The function handles numerical stability by checking for + * non-positive pivots and sets a flag if a zero or negative division is + * detected. + * + * @tparam T The numeric type of the matrix elements (e.g., float, + * double). + * @tparam M The dimension of the square matrix. + * @tparam RowIndices_U The type used for row indices in the sparse matrix + * representation. + * @tparam RowPointers_U The type used for row pointers in the sparse matrix + * representation. + * @param U The input sparse matrix in compiled format. + * @param Y_b The fallback matrix to return if decomposition fails. + * @param division_min The minimum value used to avoid division by zero in + * reciprocal square root. + * @param zero_div_flag Reference to a boolean flag that is set to true if a + * zero or negative pivot is encountered. + * @return Matrix The upper-triangular matrix resulting from the + * Cholesky decomposition, or Y_b if failed. + */ template inline Matrix cholesky_decomposition_sparse( From 50fb9c22f7bcf12c0e52622c9bdada5c05add1ab Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 07:52:51 +0900 Subject: [PATCH 02/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base_matrix_compiled_sparse_operation.hpp | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/base_matrix/base_matrix_compiled_sparse_operation.hpp b/base_matrix/base_matrix_compiled_sparse_operation.hpp index 8766b7c..c00be3b 100644 --- a/base_matrix/base_matrix_compiled_sparse_operation.hpp +++ b/base_matrix/base_matrix_compiled_sparse_operation.hpp @@ -1,3 +1,31 @@ +/******************************************************************************** +@file base_matrix_compiled_sparse_operation.hpp +@brief +This file provides a comprehensive set of template-based operations for sparse +matrix arithmetic in C++. It supports various combinations of sparse, dense, and +diagonal matrices and vectors, including addition, subtraction, multiplication, +and transpose operations. The implementation is highly generic and leverages +template metaprogramming for compile-time recursion and efficient code +generation. Both recursive template and for-loop based implementations are +provided, controlled by macros. + +@details +The file defines operations for: +- Sparse matrix negation, addition, subtraction, and scalar multiplication. +- Sparse matrix multiplication with dense matrices, diagonal matrices, and +vectors. +- Dense matrix and diagonal matrix arithmetic with sparse matrices. +- Sparse matrix multiplication with other sparse matrices, including transposed +cases. +- Setting diagonal matrix values into sparse matrices. +- Utility structures for compile-time calculation of result matrix types. + +@note +tparam M is the number of columns in the matrix. +tparam N is the number of rows in the matrix. +Somehow programming custom is vice versa, +but in this project, we use the mathematical custom. +********************************************************************************/ #ifndef __BASE_MATRIX_COMPILED_SPARSE_OPERATION_HPP__ #define __BASE_MATRIX_COMPILED_SPARSE_OPERATION_HPP__ @@ -24,6 +52,24 @@ template struct Loop { + /** + * @brief + * Recursively computes the negation of all values in a sparse matrix row + * segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam RowIndices Type representing the row indices of nonzero elements. + * @tparam RowPointers Type representing the row pointers for sparse storage. + * @tparam J Current row index (compile-time). + * @tparam K Unused parameter for compatibility. + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix. + * @param[out] Y Output sparse matrix, where each value is set to the negation + * of the corresponding value in A. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -36,6 +82,26 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive negation loop in sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam RowIndices Type representing the row indices of nonzero elements. + * @tparam RowPointers Type representing the row pointers for sparse storage. + * @tparam J Current row index (compile-time). + * @tparam K Unused parameter for compatibility. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix. + * @param[out] Y Output sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { From ec9765cc8a2921c4d1669d6f243c757b0846dc76 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 10:26:00 +0900 Subject: [PATCH 03/31] =?UTF-8?q?=E5=A4=96=E9=83=A8=E3=83=AA=E3=83=9D?= =?UTF-8?q?=E3=82=B8=E3=83=88=E3=83=AA=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- external_libraries/base_utility_cpp | 2 +- external_libraries/python_math_to_cpp | 2 +- test_vs/MCAP_tester | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/external_libraries/base_utility_cpp b/external_libraries/base_utility_cpp index c4d8663..3508c9f 160000 --- a/external_libraries/base_utility_cpp +++ b/external_libraries/base_utility_cpp @@ -1 +1 @@ -Subproject commit c4d8663116cbfcb197a986c14cc323702d3ca3b4 +Subproject commit 3508c9feed073712bfba88978f35f95e5433550b diff --git a/external_libraries/python_math_to_cpp b/external_libraries/python_math_to_cpp index 903f03f..9d878f0 160000 --- a/external_libraries/python_math_to_cpp +++ b/external_libraries/python_math_to_cpp @@ -1 +1 @@ -Subproject commit 903f03f899896000524adf385545124cf387380f +Subproject commit 9d878f0b9e0980cca9901ea148609d10b5fbf7e5 diff --git a/test_vs/MCAP_tester b/test_vs/MCAP_tester index 145a2e3..1fbd7b9 160000 --- a/test_vs/MCAP_tester +++ b/test_vs/MCAP_tester @@ -1 +1 @@ -Subproject commit 145a2e38f72dca331bb9cb99fc2aff51c41a17ec +Subproject commit 1fbd7b9c6ad6c803128a843831a0c3db8746e8b8 From 6cd409fc67ea905fa0525e702b6e96f78db49198 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 19:55:32 +0900 Subject: [PATCH 04/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base_matrix_compiled_sparse_operation.hpp | 3322 ++++++++++++++++- 1 file changed, 3320 insertions(+), 2 deletions(-) diff --git a/base_matrix/base_matrix_compiled_sparse_operation.hpp b/base_matrix/base_matrix_compiled_sparse_operation.hpp index c00be3b..dfc49e5 100644 --- a/base_matrix/base_matrix_compiled_sparse_operation.hpp +++ b/base_matrix/base_matrix_compiled_sparse_operation.hpp @@ -115,6 +115,23 @@ struct Loop { template struct Row { + /** + * @brief + * Computes the negation of all values in a specific row of a sparse matrix. + * + * This function recursively processes each row of the sparse matrix, applying + * the negation operation to all non-zero elements in that row. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam RowIndices Type representing the row indices of nonzero elements. + * @tparam RowPointers Type representing the row pointers for sparse storage. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix. + * @param[out] Y Output sparse matrix, where each value is set to the negation + * of the corresponding value in A. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -128,6 +145,22 @@ struct Row { template struct Row { + /** + * @brief + * Computes the negation of all values in the first row of a sparse matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam RowIndices Type representing the row indices of nonzero elements. + * @tparam RowPointers Type representing the row pointers for sparse storage. + * @param[in] A Input sparse matrix. + * @param[out] Y Output sparse matrix, where each value is set to the negation + * of the corresponding value in A. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -136,6 +169,22 @@ struct Row { } }; +/** + * @brief + * Computes the negation of all values in a sparse matrix. + * + * This function serves as the entry point for negating all values in a sparse + * matrix. It initializes the recursive computation starting from the last row. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam RowIndices Type representing the row indices of nonzero elements. + * @tparam RowPointers Type representing the row pointers for sparse storage. + * @param[in] A Input sparse matrix. + * @param[out] Y Output sparse matrix, where each value is set to the negation + * of the corresponding value in A. + */ template inline void @@ -146,6 +195,21 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixMinus +/** + * @brief + * Negates all values in a sparse matrix. + * + * This function creates a new sparse matrix where each value is the negation of + * the corresponding value in the input sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam RowIndices Type representing the row indices of nonzero elements. + * @tparam RowPointers Type representing the row pointers for sparse storage. + * @param[in] A Input sparse matrix to be negated. + * @return A new sparse matrix with negated values. + */ template inline CompiledSparseMatrix @@ -178,6 +242,28 @@ template struct Loop { + /** + * @brief + * Recursively computes the product of a sparse matrix row segment with a + * dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] sum Accumulator for the product result. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, T &sum) { @@ -192,6 +278,31 @@ template struct Loop { + /** + * @brief + * End condition for the recursive product computation in sparse matrix + * operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] sum Accumulator for the product result. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, T &sum) { @@ -207,6 +318,28 @@ template struct Core { + /** + * @brief + * Computes the product of a sparse matrix row with a dense matrix. + * + * This function serves as the core computation for multiplying a specific row + * of a sparse matrix with a dense matrix, accumulating the result in a sum. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] Y Output dense matrix where the computed sum is stored. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -224,6 +357,30 @@ template struct List { + /** + * @brief + * Computes the product of a sparse matrix with a dense matrix for multiple + * rows. + * + * This function recursively processes each row of the sparse matrix, calling + * the core computation for each row and accumulating results in the output + * dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -237,6 +394,27 @@ struct List { template struct List { + /** + * @brief + * Computes the product of the first row of a sparse matrix with a dense + * matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -249,6 +427,29 @@ struct List { template struct Column { + /** + * @brief + * Computes the product of a sparse matrix with a dense matrix for multiple + * columns. + * + * This function recursively processes each column of the dense matrix, + * calling the list computation for each column and accumulating results in + * the output dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -262,6 +463,27 @@ struct Column { template struct Column { + /** + * @brief + * Computes the product of the first column of a sparse matrix with a dense + * matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first column of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense + * matrix. + * @tparam K Number of rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -270,6 +492,24 @@ struct Column { } }; +/** + * @brief + * Computes the product of a sparse matrix with a dense matrix. + * + * This function serves as the entry point for multiplying a sparse matrix with + * a dense matrix, producing a resulting dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage. + * @tparam K Number of rows in the dense matrix. + * @param[in] A Input sparse matrix. + * @param[in] B Input dense matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ template inline void @@ -281,6 +521,24 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixMultiplyDenseMatrix +/** + * @brief + * Multiplies a sparse matrix with a dense matrix. + * + * This function creates a new dense matrix that is the product of a sparse + * matrix and a dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage. + * @tparam K Number of rows in the dense matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to be multiplied. + * @return A new dense matrix that is the product of A and B. + */ template inline Matrix @@ -319,6 +577,28 @@ template struct Loop { + /** + * @brief + * Recursively computes the product of a dense matrix row with a sparse matrix + * column segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -337,6 +617,31 @@ template struct Loop { + /** + * @brief + * End condition for the recursive product computation in dense matrix and + * sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -353,6 +658,29 @@ template struct Core { + /** + * @brief + * Computes the product of a dense matrix row with a sparse matrix column. + * + * This function serves as the core computation for multiplying a specific row + * of a dense matrix with a sparse matrix, accumulating results in the output + * dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -367,6 +695,30 @@ template struct List { + /** + * @brief + * Computes the product of a dense matrix with a sparse matrix for multiple + * rows. + * + * This function recursively processes each row of the dense matrix, calling + * the core computation for each row and accumulating results in the output + * dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -381,6 +733,27 @@ struct List { template struct List { + /** + * @brief + * Computes the product of the first row of a dense matrix with a sparse + * matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first row of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -393,6 +766,29 @@ struct List { template struct Column { + /** + * @brief + * Computes the product of a dense matrix with a sparse matrix for multiple + * columns. + * + * This function recursively processes each column of the sparse matrix, + * calling the list computation for each column and accumulating results in + * the output dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @tparam J Current column index (compile-time). + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -407,6 +803,27 @@ struct Column { template struct Column { + /** + * @brief + * Computes the product of the first column of a dense matrix with a sparse + * matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first column of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse + * storage. + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -416,6 +833,25 @@ struct Column { } }; +/** + * @brief + * Computes the product of a dense matrix with a sparse matrix. + * + * This function serves as the entry point for multiplying a dense matrix with + * a sparse matrix, producing a resulting dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage. + * @tparam K Number of rows in the sparse matrix. + * @param[in] A Input dense matrix. + * @param[in] B Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ template inline void @@ -427,6 +863,25 @@ compute(const Matrix &A, } // namespace DenseMatrixMultiplySparseMatrix +/** + * @brief + * Multiplies a dense matrix with a sparse matrix. + * + * This function creates a new dense matrix that is the product of a dense + * matrix and a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam N Number of rows in the dense matrix and columns in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage. + * @tparam K Number of rows in the sparse matrix. + * @param[in] A Input dense matrix to be multiplied. + * @param[in] B Input sparse matrix to be multiplied. + * @return A new dense matrix that is the product of A and B. + */ template inline Matrix @@ -463,6 +918,25 @@ template struct Loop { + /** + * @brief + * Recursively computes the addition of a sparse matrix row with a dense + * matrix row segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -478,6 +952,28 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive addition computation in sparse matrix and + * dense matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @param[in] A Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -491,6 +987,25 @@ struct Loop { template struct Row { + /** + * @brief + * Computes the addition of a sparse matrix row with a dense matrix row. + * + * This function serves as the core computation for adding a specific row of a + * sparse matrix to a dense matrix, accumulating results in the output dense + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -505,6 +1020,24 @@ struct Row { template struct Row { + /** + * @brief + * Computes the addition of the first row of a sparse matrix with a dense + * matrix row. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse + * storage. + * @param[in] A Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -514,6 +1047,22 @@ struct Row { } }; +/** + * @brief + * Computes the addition of a sparse matrix with a dense matrix. + * + * This function serves as the entry point for adding a sparse matrix to a + * dense matrix, producing a resulting dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage. + * @param[in] A Input sparse matrix. + * @param[out] Y Output dense matrix where results are accumulated. + */ template inline void @@ -524,6 +1073,23 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixAddDenseMatrix +/** + * @brief + * Adds a sparse matrix to a dense matrix. + * + * This function creates a new dense matrix that is the result of adding a + * sparse matrix to a dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage. + * @param[in] A Input sparse matrix to be added. + * @param[in] B Input dense matrix to be added. + * @return A new dense matrix that is the sum of A and B. + */ template inline Matrix @@ -551,6 +1117,24 @@ operator+(const CompiledSparseMatrix &A, } /* Dense Matrix add Sparse Matrix */ + +/** + * @brief + * Adds a dense matrix to a sparse matrix. + * + * This function creates a new dense matrix that is the result of adding a + * dense matrix to a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage. + * @param[in] B Input dense matrix to be added. + * @param[in] A Input sparse matrix to be added. + * @return A new dense matrix that is the sum of B and A. + */ template inline Matrix @@ -585,6 +1169,29 @@ template struct Loop { + /** + * @brief + * Recursively computes the addition of a sparse matrix row with another + * sparse matrix row segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be added. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -604,6 +1211,32 @@ template struct Loop { + /** + * @brief + * End condition for the recursive addition computation in sparse matrix and + * sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @param[in] A Input sparse matrix to be added. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -618,6 +1251,30 @@ template struct Row { + /** + * @brief + * Computes the addition of a sparse matrix row with another sparse matrix + * row. + * + * This function serves as the core computation for adding a specific row of a + * sparse matrix to another sparse matrix, accumulating results in the output + * sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be added. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -634,6 +1291,28 @@ template struct Row { + /** + * @brief + * Computes the addition of the first row of a sparse matrix with another + * sparse matrix row. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @param[in] A Input sparse matrix to be added. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -643,6 +1322,27 @@ struct Row inline void @@ -661,6 +1361,25 @@ namespace SetDiagMatrixToValuesSparseMatrix { template struct Core { + /** + * @brief + * Recursively sets the diagonal values of a sparse matrix from a diagonal + * matrix. + * + * This function processes each diagonal element of the sparse matrix, setting + * its value based on the corresponding element in the diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows and columns in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam I Current index (compile-time). + * @param[in,out] Y Output sparse matrix where diagonal values are set. + * @param[in] B Input diagonal matrix providing values for the diagonal. + */ static void apply(CompiledSparseMatrix &Y, const DiagMatrix &B) { @@ -673,6 +1392,24 @@ struct Core { template struct Core { + /** + * @brief + * Base case for the recursive setting of diagonal values in a sparse matrix. + * + * This specialization is instantiated when I is 0, meaning it processes the + * first diagonal element. It does not perform any action, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows and columns in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in,out] Y Output sparse matrix where diagonal values are set. + * @param[in] B Input diagonal matrix providing values for the diagonal. + */ static void apply(CompiledSparseMatrix &Y, const DiagMatrix &B) { @@ -681,6 +1418,23 @@ struct Core { } }; +/** + * @brief + * Sets the diagonal values of a sparse matrix from a diagonal matrix. + * + * This function serves as the entry point for setting the diagonal values of a + * sparse matrix using the values from a diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows and columns in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in,out] Y Output sparse matrix where diagonal values are set. + * @param[in] B Input diagonal matrix providing values for the diagonal. + */ template inline void @@ -712,6 +1466,25 @@ struct DiagAddSubSparse { } // namespace CompiledSparseOperation +/** + * @brief + * Adds a sparse matrix to a diagonal matrix. + * + * This function creates a new sparse matrix that is the result of adding a + * sparse matrix to a diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix and rows in the diagonal + * matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be added. + * @param[in] B Input diagonal matrix to be added. + * @return A new sparse matrix that is the sum of A and B. + */ template inline auto @@ -765,6 +1538,26 @@ operator+(const CompiledSparseMatrix &A, } /* Diag Matrix add Sparse Matrix */ + +/** + * @brief + * Adds a diagonal matrix to a sparse matrix. + * + * This function creates a new sparse matrix that is the result of adding a + * diagonal matrix to a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix and rows in the diagonal + * matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] B Input diagonal matrix to be added. + * @param[in] A Input sparse matrix to be added. + * @return A new sparse matrix that is the sum of B and A. + */ template inline auto @@ -841,6 +1634,29 @@ struct SparseAddSubSparse { } // namespace CompiledSparseOperation /* Sparse Matrix add Sparse Matrix */ + +/** + * @brief + * Adds two sparse matrices together. + * + * This function creates a new sparse matrix that is the result of adding two + * sparse matrices together. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrices. + * @tparam N Number of rows in the sparse matrices. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @param[in] A Input first sparse matrix to be added. + * @param[in] B Input second sparse matrix to be added. + * @return A new sparse matrix that is the sum of A and B. + */ template inline auto @@ -895,6 +1711,24 @@ operator+(const CompiledSparseMatrix &A, } /* Sparse Matrix subtract Dense Matrix */ + +/** + * @brief + * Subtracts a dense matrix from a sparse matrix. + * + * This function creates a new dense matrix that is the result of subtracting a + * dense matrix from a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage. + * @param[in] A Input sparse matrix to be subtracted from. + * @param[in] B Input dense matrix to be subtracted. + * @return A new dense matrix that is the result of A - B. + */ template inline Matrix @@ -929,6 +1763,25 @@ template struct Loop { + /** + * @brief + * Recursively computes the subtraction of a sparse matrix row from a dense + * matrix row segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -944,6 +1797,28 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive subtraction computation in dense matrix and + * sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -957,6 +1832,25 @@ struct Loop { template struct Row { + /** + * @brief + * Computes the subtraction of a sparse matrix row from a dense matrix row. + * + * This function serves as the core computation for subtracting a specific row + * of a sparse matrix from a dense matrix, accumulating results in the output + * dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -971,6 +1865,24 @@ struct Row { template struct Row { + /** + * @brief + * Computes the subtraction of the first row of a sparse matrix from a dense + * matrix row. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, Matrix &Y) { @@ -980,6 +1892,24 @@ struct Row { } }; +/** + * @brief + * Computes the subtraction of a sparse matrix from a dense matrix. + * + * This function serves as the entry point for subtracting a sparse matrix from + * a dense matrix, producing a resulting dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam N Number of rows in the sparse matrix and rows in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output dense matrix where results are accumulated. + */ template inline void @@ -990,6 +1920,25 @@ compute(const CompiledSparseMatrix &A, } // namespace DenseMatrixSubSparseMatrix +/** + * @brief + * Subtracts a sparse matrix from a dense matrix. + * + * This function creates a new dense matrix that is the result of subtracting a + * sparse matrix from a dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam N Number of rows in the dense matrix and rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] B Input dense matrix to be subtracted from. + * @param[in] A Input sparse matrix to be subtracted. + * @return A new dense matrix that is the result of B - A. + */ template inline Matrix @@ -1017,6 +1966,26 @@ operator-(const Matrix &B, } /* Sparse Matrix subtract Diag Matrix */ + +/** + * @brief + * Subtracts a diagonal matrix from a sparse matrix. + * + * This function creates a new sparse matrix that is the result of subtracting a + * diagonal matrix from a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix and rows in the diagonal + * matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be subtracted from. + * @param[in] B Input diagonal matrix to be subtracted. + * @return A new sparse matrix that is the result of A - B. + */ template inline auto @@ -1077,6 +2046,30 @@ template struct Loop { + /** + * @brief + * Recursively computes the subtraction of a sparse matrix row from a diagonal + * matrix row segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the diagonal matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the diagonal matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1096,6 +2089,33 @@ template struct Loop { + /** + * @brief + * End condition for the recursive subtraction computation in sparse matrix + * and diagonal matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the diagonal matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the diagonal matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1110,6 +2130,30 @@ template struct Row { + /** + * @brief + * Computes the subtraction of a sparse matrix row from a diagonal matrix row. + * + * This function serves as the core computation for subtracting a specific row + * of a sparse matrix from a diagonal matrix, accumulating results in the + * output sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the diagonal matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the diagonal matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1126,6 +2170,29 @@ template struct Row { + /** + * @brief + * Computes the subtraction of the first row of a sparse matrix from a + * diagonal matrix row. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix and rows in the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the diagonal matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the diagonal matrix. + * @param[in] A Input sparse matrix to be subtracted. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1135,6 +2202,25 @@ struct Row inline void @@ -1147,6 +2233,26 @@ compute(const CompiledSparseMatrix &A, } // namespace DiagMatrixSubSparseMatrix +/** + * @brief + * Subtracts a sparse matrix from a diagonal matrix. + * + * This function creates a new sparse matrix that is the result of subtracting a + * sparse matrix from a diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the diagonal matrix and rows in the sparse + * matrix. + * @tparam N Number of rows in the diagonal matrix and rows in the sparse + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] B Input diagonal matrix to be subtracted from. + * @param[in] A Input sparse matrix to be subtracted. + * @return A new sparse matrix that is the result of B - A. + */ template inline auto @@ -1208,6 +2314,29 @@ template struct Loop { + /** + * @brief + * Recursively computes the subtraction of a sparse matrix row from another + * sparse matrix row segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be subtracted from. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1227,6 +2356,32 @@ template struct Loop { + /** + * @brief + * End condition for the recursive subtraction computation in sparse matrix + * operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam K Current column index (compile-time). + * @param[in] A Input sparse matrix to be subtracted from. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1241,6 +2396,30 @@ template struct Row { + /** + * @brief + * Computes the subtraction of a sparse matrix row from another sparse matrix + * row. + * + * This function serves as the core computation for subtracting a specific row + * of a sparse matrix from another sparse matrix, accumulating results in the + * output sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be subtracted from. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1257,6 +2436,28 @@ template struct Row { + /** + * @brief + * Computes the subtraction of the first row of a sparse matrix from another + * sparse matrix row. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @param[in] A Input sparse matrix to be subtracted from. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, CompiledSparseMatrix &Y) { @@ -1266,6 +2467,27 @@ struct Row inline void @@ -1278,6 +2500,28 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixSubSparseMatrix +/** + * @brief + * Subtracts one sparse matrix from another sparse matrix. + * + * This function creates a new sparse matrix that is the result of subtracting + * one sparse matrix from another. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrices. + * @tparam N Number of rows in the sparse matrices. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @param[in] A Input sparse matrix to be subtracted from. + * @param[in] B Input sparse matrix to be subtracted. + * @return A new sparse matrix that is the result of A - B. + */ template inline auto @@ -1339,6 +2583,23 @@ namespace SparseMatrixMultiplyScalar { template struct Loop { + /** + * @brief + * Recursively computes the scalar multiplication of a sparse matrix element. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam I Current index (compile-time). + * @tparam End End index for the loop. + * @param[in] A Input sparse matrix to be multiplied by scalar. + * @param[in] scalar Scalar value to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const T &scalar, @@ -1354,6 +2615,27 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive scalar multiplication computation in sparse + * matrix operations. + * + * This specialization of the Loop struct is instantiated when the current + * index equals the end index, indicating that all elements have been + * processed. The function does nothing, serving as the base case to terminate + * recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied by scalar. + * @param[in] scalar Scalar value to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const T &scalar, @@ -1365,6 +2647,24 @@ struct Loop { } }; +/** + * @brief + * Computes the scalar multiplication of a sparse matrix. + * + * This function serves as the entry point for multiplying a sparse matrix by a + * scalar, producing a resulting sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied by scalar. + * @param[in] scalar Scalar value to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ template inline void @@ -1378,6 +2678,24 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixMultiplyScalar +/** + * @brief + * Multiplies a sparse matrix by a scalar. + * + * This function creates a new sparse matrix that is the result of multiplying a + * sparse matrix by a scalar. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied by scalar. + * @param[in] scalar Scalar value to multiply with. + * @return A new sparse matrix that is the result of A * scalar. + */ template inline CompiledSparseMatrix @@ -1402,6 +2720,25 @@ operator*(const CompiledSparseMatrix &A, } /* Scalar multiply Sparse Matrix */ + +/** + * @brief + * Multiplies a scalar by a sparse matrix. + * + * This function creates a new sparse matrix that is the result of multiplying a + * scalar by a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] scalar Scalar value to multiply with. + * @param[in] A Input sparse matrix to be multiplied by scalar. + * @return A new sparse matrix that is the result of scalar * A. + */ template inline CompiledSparseMatrix @@ -1433,6 +2770,25 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with a + * vector segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @param[out] sum Accumulator for the result. + */ static void compute(const CompiledSparseMatrix &A, const Vector &b, T &sum) { @@ -1447,6 +2803,28 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @param[out] sum Accumulator for the result. + */ static void compute(const CompiledSparseMatrix &A, const Vector &b, T &sum) { @@ -1461,6 +2839,26 @@ struct Loop { template struct Core { + /** + * @brief + * Computes the multiplication of a sparse matrix row with a vector. + * + * This function serves as the core computation for multiplying a specific row + * of a sparse matrix with a vector, accumulating results in the output + * vector. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @param[out] y Output vector where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Vector &b, Vector &y) { @@ -1476,6 +2874,23 @@ struct Core { template struct List { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with a vector + * across multiple rows. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @param[out] y Output vector where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Vector &b, Vector &y) { @@ -1489,6 +2904,25 @@ struct List { template struct List { + /** + * @brief + * Computes the multiplication of the first row of a sparse matrix with a + * vector. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @param[out] y Output vector where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Vector &b, Vector &y) { @@ -1497,6 +2931,24 @@ struct List { } }; +/** + * @brief + * Computes the multiplication of a sparse matrix with a vector. + * + * This function serves as the entry point for multiplying a sparse matrix by a + * vector, producing a resulting vector. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @param[out] y Output vector where results are accumulated. + */ template inline void @@ -1507,6 +2959,24 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixMultiplyVector +/** + * @brief + * Multiplies a sparse matrix by a vector. + * + * This function creates a new vector that is the result of multiplying a sparse + * matrix by a vector. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] b Input vector to multiply with. + * @return A new vector that is the result of A * b. + */ template inline Vector @@ -1543,6 +3013,26 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a column vector with a sparse + * matrix segment. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the column vector and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] a Input column vector to multiply with. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] y Output column vector where results are accumulated. + */ static void compute(const ColVector &a, const CompiledSparseMatrix &B, @@ -1558,6 +3048,29 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in column vector + * and sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the column vector and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current column index (compile-time). + * @param[in] a Input column vector to multiply with. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] y Output column vector where results are accumulated. + */ static void compute(const ColVector &a, const CompiledSparseMatrix &B, @@ -1573,6 +3086,24 @@ struct Loop { template struct List { + /** + * @brief + * Recursively computes the multiplication of a column vector with a sparse + * matrix across multiple segments. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the column vector and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current column index (compile-time). + * @param[in] a Input column vector to multiply with. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] y Output column vector where results are accumulated. + */ static void compute(const ColVector &a, const CompiledSparseMatrix &B, @@ -1588,6 +3119,26 @@ struct List { template struct List { + /** + * @brief + * Computes the multiplication of the first column of a column vector with a + * sparse matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first column of the column vector. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the column vector and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] a Input column vector to multiply with. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] y Output column vector where results are accumulated. + */ static void compute(const ColVector &a, const CompiledSparseMatrix &B, @@ -1598,6 +3149,25 @@ struct List { } }; +/** + * @brief + * Computes the multiplication of a column vector with a sparse matrix. + * + * This function serves as the entry point for multiplying a column vector by a + * sparse matrix, producing a resulting column vector. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the column vector and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] a Input column vector to multiply with. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] y Output column vector where results are accumulated. + */ template inline void @@ -1610,6 +3180,25 @@ compute(const ColVector &a, } // namespace ColVectorMultiplySparseMatrix +/** + * @brief + * Multiplies a column vector by a sparse matrix. + * + * This function creates a new column vector that is the result of multiplying a + * column vector by a sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the column vector and columns in the sparse + * matrix. + * @tparam K Number of rows in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] a Input column vector to multiply with. + * @param[in] B Input sparse matrix to multiply with. + * @return A new column vector that is the result of a * B. + */ template inline ColVector colVector_a_mul_SparseB( @@ -1644,6 +3233,27 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with a dense + * matrix column segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] sum Accumulator for the result. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, T &sum) { @@ -1659,6 +3269,30 @@ template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and dense matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] sum Accumulator for the result. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, T &sum) { @@ -1674,6 +3308,29 @@ template struct Core { + /** + * @brief + * Computes the multiplication of a sparse matrix row with a dense matrix + * column. + * + * This function serves as the core computation for multiplying a specific row + * of a sparse matrix with a column of a dense matrix, accumulating results in + * the output matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -1690,6 +3347,25 @@ template struct List { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with a dense + * matrix across multiple rows. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -1715,6 +3391,24 @@ struct List { template struct Column { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with a dense + * matrix across multiple columns. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam I Current column index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -1728,6 +3422,26 @@ struct Column { template struct Column { + /** + * @brief + * Computes the multiplication of the first column of a sparse matrix with a + * dense matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first column of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] Y Output dense matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -1736,6 +3450,26 @@ struct Column { } }; +/** + * @brief + * Computes the multiplication of a sparse matrix with a dense matrix + * (transposed). + * + * This function serves as the entry point for multiplying a sparse matrix by a + * dense matrix, producing a resulting dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with. + * @param[out] Y Output dense matrix where results are accumulated. + */ template inline void @@ -1747,6 +3481,25 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseMatrixMultiplyDenseTranspose +/** + * @brief + * Multiplies a sparse matrix by the transpose of a dense matrix. + * + * This function creates a new matrix that is the result of multiplying a sparse + * matrix by the transpose of a dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input dense matrix to multiply with (transposed). + * @return A new matrix that is the result of A * B^T. + */ template inline Matrix matrix_multiply_SparseA_mul_BTranspose( @@ -1786,6 +3539,36 @@ template struct InnerLoop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with another + * sparse matrix column segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam L Current column index (compile-time). + * @tparam LEnd End index of the current segment. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1808,6 +3591,38 @@ template { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and sparse matrix operations. + * + * This specialization of the InnerLoop struct is instantiated when the L + * index equals the LEnd index, indicating that all elements in the current + * segment have been processed. The function does nothing, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1825,6 +3640,35 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with another + * sparse matrix column across multiple segments. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @tparam J Current row index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1847,6 +3691,38 @@ template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1864,6 +3740,36 @@ template struct Core { + /** + * @brief + * Computes the multiplication of a sparse matrix row with another sparse + * matrix column. + * + * This function serves as the core computation for multiplying a specific row + * of a sparse matrix with a column of another sparse matrix, accumulating + * results in the output sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1881,6 +3787,33 @@ template struct List { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with another + * sparse matrix across multiple rows. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @tparam J Current row index (compile-time). + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1899,6 +3832,35 @@ template struct List { + /** + * @brief + * Computes the multiplication of the first row of a sparse matrix with + * another sparse matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1914,6 +3876,32 @@ template struct Column { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with another + * sparse matrix across multiple columns. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam RowIndices_Y Type representing the row indices of nonzero elements + * in the output sparse matrix. + * @tparam RowPointers_Y Type representing the row pointers for sparse storage + * in the output sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -1924,6 +3912,26 @@ struct Column { } }; +/** + * @brief + * Computes the multiplication of a sparse matrix with another sparse matrix. + * + * This function serves as the entry point for multiplying two sparse matrices, + * producing a resulting sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ template @@ -1965,6 +3973,28 @@ struct SparseMulSparse { } // namespace CompiledSparseOperation +/** + * @brief + * Multiplies a sparse matrix by another sparse matrix. + * + * This function creates a new sparse matrix that is the result of multiplying + * two sparse matrices. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @return A new sparse matrix that is the result of A * B. + */ template @@ -2030,6 +4060,29 @@ template struct InnerLoop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with another + * sparse matrix column segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @tparam I Current row index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam L Current column index (compile-time). + * @tparam LEnd End index of the current segment. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2053,6 +4106,31 @@ template struct InnerLoop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and sparse matrix operations. + * + * This specialization of the InnerLoop struct is instantiated when the L + * index equals the LEnd index, indicating that all elements in the current + * segment have been processed. The function does nothing, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @tparam I Current row index (compile-time). + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2070,6 +4148,28 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with another + * sparse matrix column across multiple segments. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @tparam I Current row index (compile-time). + * @tparam Start Start index of the current segment. + * @tparam End End index of the current segment. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2090,6 +4190,31 @@ template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @tparam I Current row index (compile-time). + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2106,6 +4231,32 @@ template struct Core { + /** + * @brief + * Computes the multiplication of a sparse matrix row with another sparse + * matrix column. + * + * This function serves as the core computation for multiplying a specific row + * of a sparse matrix with a column of another sparse matrix, accumulating + * results in the output sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2122,6 +4273,26 @@ template struct List { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with another + * sparse matrix across multiple rows. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @tparam I Current row index (compile-time). + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2140,6 +4311,31 @@ template struct List { + /** + * @brief + * Computes the multiplication of the first row of a sparse matrix with + * another sparse matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + * @param[in] A Input sparse matrix to be multiplied. + * @param[in] B Input sparse matrix to multiply with. + * @param[out] Y Output sparse matrix where results are accumulated. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2155,6 +4351,25 @@ template struct Column { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with another + * sparse matrix across multiple columns. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the sparse matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam K Number of columns in the dense matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the dense matrix. + * @tparam RowPointers_B Type representing the row pointers for dense storage + * in the dense matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2165,6 +4380,27 @@ struct Column { } }; +/** + * @brief + * Computes the multiplication of a sparse matrix with another sparse matrix. + * + * This function serves as the entry point for multiplying two sparse matrices, + * producing a resulting sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + */ template @@ -2206,6 +4442,27 @@ struct SparseATransposeMulSparseB { } // namespace CompiledSparseOperation +/** + * @brief + * Multiplies the transpose of a sparse matrix by another sparse matrix. + * + * This function creates a new sparse matrix that is the result of multiplying + * the transpose of a sparse matrix A with another sparse matrix B. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the first sparse matrix (A^T). + * @tparam N Number of columns in the first sparse matrix (A^T). + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix (A^T). + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix (A^T). + * @tparam K Number of rows in the second sparse matrix (B). + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix (B). + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix (B). + * @return A new sparse matrix that is the result of A^T * B. + */ template @@ -2245,12 +4502,33 @@ inline auto matrix_multiply_SparseATranspose_mul_SparseB( /* Sparse Matrix multiply Sparse Matrix Transpose */ namespace SparseMatrixMultiplySparseTranspose { -// Core conditional operation for Sparse Matrix multiply Sparse Matrix Transpose +// Core conditional operation for Sparse Matrix multiply Sparse Matrix +// Transpose template struct CoreConditional { + /** + * @brief + * Computes the multiplication of a sparse matrix row with another sparse + * matrix column segment, with a conditional operation based on the indices. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2268,6 +4546,29 @@ template struct CoreConditional { + /** + * @brief + * Computes the multiplication of a sparse matrix row with another sparse + * matrix column segment when the condition is met. + * + * This specialization is instantiated when the condition is met, meaning that + * the indices match and the multiplication can be performed. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2285,6 +4586,26 @@ template struct InnerLoop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with another + * sparse matrix column segment. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2309,6 +4630,31 @@ template struct InnerLoop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and sparse matrix operations. + * + * This specialization of the InnerLoop struct is instantiated when the O + * index equals the O_End index, indicating that all elements in the current + * segment have been processed. The function does nothing, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2326,6 +4672,26 @@ template struct OuterLoop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with another + * sparse matrix column across multiple segments. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2348,6 +4714,31 @@ template struct OuterLoop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and sparse matrix operations. + * + * This specialization of the OuterLoop struct is instantiated when the L + * index equals the L_End index, indicating that all elements in the current + * segment have been processed. The function does nothing, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2364,6 +4755,30 @@ template struct Core { + /** + * @brief + * Computes the multiplication of a sparse matrix row with another sparse + * matrix column segment. + * + * This function serves as the core computation for multiplying a specific row + * of a sparse matrix with a column of another sparse matrix, accumulating + * results in the output sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2381,6 +4796,26 @@ template struct List { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with another + * sparse matrix across multiple rows. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2399,6 +4834,29 @@ template struct List { + /** + * @brief + * Computes the multiplication of the first row of a sparse matrix with + * another sparse matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2414,6 +4872,26 @@ template struct Column { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with another + * sparse matrix across multiple columns. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2432,6 +4910,29 @@ template struct Column { + /** + * @brief + * Computes the multiplication of the first column of a sparse matrix with + * another sparse matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first column of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of columns in the first sparse matrix. + * @tparam N Number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K Number of rows in the second sparse matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const CompiledSparseMatrix &A, const CompiledSparseMatrix &B, @@ -2442,6 +4943,29 @@ struct Column @@ -2480,6 +5004,27 @@ struct SparseAMulSparseBTranspose { } // namespace CompiledSparseOperation +/** + * @brief + * Multiplies a sparse matrix A with the transpose of another sparse matrix B. + * + * This function creates a new sparse matrix that is the result of multiplying + * the sparse matrix A with the transpose of the sparse matrix B. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the first sparse matrix (A). + * @tparam N Number of columns in the first sparse matrix (A). + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the first sparse matrix (A). + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the first sparse matrix (A). + * @tparam K Number of rows in the second sparse matrix (B^T). + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the second sparse matrix (B^T). + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the second sparse matrix (B^T). + * @return A new sparse matrix that is the result of A * B^T. + */ template @@ -2526,6 +5071,20 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with a + * diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the sparse matrix. + * @tparam N Number of columns in the sparse matrix and size of the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const DiagMatrix &B, @@ -2541,6 +5100,25 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and diagonal matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the sparse matrix. + * @tparam N Number of columns in the sparse matrix and size of the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const DiagMatrix &B, @@ -2556,6 +5134,20 @@ struct Loop { template struct Row { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix row with a + * diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the sparse matrix. + * @tparam N Number of columns in the sparse matrix and size of the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const DiagMatrix &B, @@ -2571,6 +5163,23 @@ struct Row { template struct Row { + /** + * @brief + * Computes the multiplication of the first row of a sparse matrix with a + * diagonal matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the sparse matrix. + * @tparam N Number of columns in the sparse matrix and size of the diagonal + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const DiagMatrix &B, @@ -2581,6 +5190,22 @@ struct Row { } }; +/** + * @brief + * Computes the multiplication of a sparse matrix with a diagonal matrix. + * + * This function serves as the entry point for multiplying a sparse matrix A + * with a diagonal matrix B, producing a resulting sparse matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the sparse matrix A. + * @tparam N Number of columns in the sparse matrix A and size of the diagonal + * matrix B. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix A. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix A. + */ template inline void @@ -2591,6 +5216,23 @@ compute(const CompiledSparseMatrix &A, } } // namespace SparseMatrixMultiplyDiagMatrix +/** + * @brief + * Multiplies a sparse matrix A with a diagonal matrix B. + * + * This function creates a new sparse matrix that is the result of multiplying + * the sparse matrix A with the diagonal matrix B. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the sparse matrix (A). + * @tparam N Number of columns in the sparse matrix (A) and size of the diagonal + * matrix (B). + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix (A). + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix (A). + * @return A new sparse matrix that is the result of A * B. + */ template inline auto @@ -2627,6 +5269,19 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a diagonal matrix with a sparse + * matrix row. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2642,6 +5297,24 @@ struct Loop { template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in diagonal + * matrix and sparse matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2657,6 +5330,19 @@ struct Loop { template struct Row { + /** + * @brief + * Recursively computes the multiplication of a diagonal matrix with a sparse + * matrix row. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2672,6 +5358,22 @@ struct Row { template struct Row { + /** + * @brief + * Computes the multiplication of the first row of a diagonal matrix with a + * sparse matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2682,6 +5384,21 @@ struct Row { } }; +/** + * @brief + * Computes the multiplication of a diagonal matrix with a sparse matrix. + * + * This function serves as the entry point for multiplying a diagonal matrix A + * with a sparse matrix B, producing a resulting sparse matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix A. + * @tparam N Number of columns in the sparse matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ template inline void @@ -2694,6 +5411,22 @@ compute(const DiagMatrix &A, } // namespace DiagMatrixMultiplySparseMatrix +/** + * @brief + * Multiplies a diagonal matrix A with a sparse matrix B. + * + * This function creates a new sparse matrix that is the result of multiplying + * the diagonal matrix A with the sparse matrix B. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix (A). + * @tparam K Number of columns in the sparse matrix (B). + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix (B). + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix (B). + * @return A new sparse matrix that is the result of A * B. + */ template inline auto @@ -2730,6 +5463,23 @@ template struct ConditionalOperation { + /** + * @brief + * Computes the multiplication of a diagonal matrix with a sparse matrix + * transpose. + * + * This function is a specialization for the case when the condition is false, + * meaning it does not perform any operation. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam K Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2747,6 +5497,23 @@ template struct ConditionalOperation { + /** + * @brief + * Computes the multiplication of a diagonal matrix with a sparse matrix + * transpose. + * + * This function is a specialization for the case when the condition is true, + * meaning it performs the multiplication operation. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam K Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2763,6 +5530,20 @@ template struct InnerLoop { + /** + * @brief + * Recursively computes the multiplication of a diagonal matrix with a sparse + * matrix transpose. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam K Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + * @tparam Y_Type Type representing the output matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2780,6 +5561,24 @@ template struct InnerLoop { + /** + * @brief + * End condition for the recursive multiplication computation in diagonal + * matrix and sparse matrix transpose operations. + * + * This specialization of the InnerLoop struct is instantiated when the KStart + * index equals the KEnd index, indicating that all elements in the current + * segment have been processed. The function does nothing, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam K Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2794,6 +5593,22 @@ struct InnerLoop struct Core { + /** + * @brief + * Computes the multiplication of a diagonal matrix with a sparse matrix + * transpose. + * + * This function serves as the core computation for multiplying a diagonal + * matrix A with a sparse matrix B, producing a resulting sparse matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix A. + * @tparam K Number of columns in the sparse matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2809,6 +5624,19 @@ struct Core { template struct List { + /** + * @brief + * Recursively computes the multiplication of a diagonal matrix with a sparse + * matrix transpose. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam K Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2823,6 +5651,22 @@ struct List { template struct List { + /** + * @brief + * Computes the multiplication of the first row of a diagonal matrix with a + * sparse matrix transpose. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first row of the diagonal matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix. + * @tparam K Number of columns in the sparse matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -2832,6 +5676,22 @@ struct List { } }; +/** + * @brief + * Computes the multiplication of a diagonal matrix with a sparse matrix + * transpose. + * + * This function serves as the entry point for multiplying a diagonal matrix A + * with a sparse matrix B, producing a resulting sparse matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix A. + * @tparam K Number of columns in the sparse matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ template inline void @@ -2863,6 +5723,23 @@ struct TransposeDiagAMulSparseB { } // namespace CompiledSparseOperation +/** + * @brief + * Computes the transpose of the multiplication of a diagonal matrix A with a + * sparse matrix B. + * + * This function serves as the entry point for multiplying a diagonal matrix A + * with a sparse matrix B, producing a resulting sparse matrix Y that is the + * transpose of the product. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the diagonal matrix A. + * @tparam K Number of columns in the sparse matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ template inline auto matrix_multiply_Transpose_DiagA_mul_SparseB( @@ -2903,6 +5780,21 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a dense matrix with a sparse + * matrix transpose. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -2919,6 +5811,26 @@ template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in dense matrix + * and sparse matrix transpose operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -2935,6 +5847,24 @@ template struct Core { + /** + * @brief + * Computes the multiplication of a dense matrix with a sparse matrix + * transpose. + * + * This function serves as the core computation for multiplying a dense matrix + * A with a sparse matrix B, producing a resulting dense matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix A. + * @tparam N Number of columns in the sparse matrix B. + * @tparam K Number of columns in the dense matrix A and rows in the sparse + * matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -2952,6 +5882,21 @@ template struct List { + /** + * @brief + * Recursively computes the multiplication of a dense matrix with a sparse + * matrix transpose. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -2966,6 +5911,24 @@ struct List { template struct List { + /** + * @brief + * Computes the multiplication of the first row of a dense matrix with a + * sparse matrix transpose. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first row of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -2979,6 +5942,21 @@ struct List { template struct Column { + /** + * @brief + * Recursively computes the multiplication of a dense matrix with a sparse + * matrix transpose. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -2993,6 +5971,24 @@ struct Column { template struct Column { + /** + * @brief + * Computes the multiplication of the first column of a dense matrix with a + * sparse matrix transpose. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first column of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3002,6 +5998,22 @@ struct Column { } }; +/** + * @brief + * Computes the multiplication of a dense matrix A with the transpose of a + * sparse matrix B, resulting in a dense matrix Y. + * + * This function serves as the entry point for multiplying a dense matrix A with + * the transpose of a sparse matrix B, producing a resulting dense matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix A. + * @tparam N Number of columns in the sparse matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ template inline void @@ -3014,6 +6026,24 @@ compute(const Matrix &A, } // namespace DenseMatrixMultiplySparseTranspose +/** + * @brief + * Multiplies a dense matrix A with the transpose of a sparse matrix B. + * + * This function creates a new dense matrix that is the result of multiplying + * the dense matrix A with the transpose of the sparse matrix B. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix (A). + * @tparam N Number of columns in the sparse matrix (B). + * @tparam K Number of columns in the dense matrix (A) and rows in the sparse + * matrix (B). + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix (B). + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix (B). + * @return A new dense matrix that is the result of A * B^T. + */ template inline Matrix matrix_multiply_A_mul_SparseBTranspose( @@ -3052,6 +6082,21 @@ template struct InnerLoop { + /** + * @brief + * Recursively computes the multiplication of a dense matrix with a sparse + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3071,6 +6116,26 @@ template struct InnerLoop { + /** + * @brief + * End condition for the recursive multiplication computation in dense matrix + * and sparse matrix operations. + * + * This specialization of the InnerLoop struct is instantiated when the Start + * index equals the End index, indicating that all elements in the current + * segment have been processed. The function does nothing, serving as the base + * case to terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3087,6 +6152,21 @@ template struct MiddleLoop { + /** + * @brief + * Recursively computes the multiplication of a dense matrix with a sparse + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3105,6 +6185,24 @@ template struct MiddleLoop { + /** + * @brief + * Computes the multiplication of the first row of a dense matrix with a + * sparse matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first row of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3121,6 +6219,21 @@ template struct OuterLoop { + /** + * @brief + * Recursively computes the multiplication of a dense matrix with a sparse + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3137,6 +6250,24 @@ struct OuterLoop { template struct OuterLoop { + /** + * @brief + * Computes the multiplication of the first column of a dense matrix with a + * sparse matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first column of the dense matrix. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix. + * @tparam N Number of columns in the sparse matrix. + * @tparam K Number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const Matrix &A, const CompiledSparseMatrix &B, @@ -3147,7 +6278,24 @@ struct OuterLoop { } }; -// Main function +/** + * @brief + * Computes the multiplication of a dense matrix A with a sparse matrix B, + * resulting in a dense matrix Y. + * + * This function serves as the entry point for multiplying a dense matrix A with + * a sparse matrix B, producing a resulting dense matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix A. + * @tparam N Number of columns in the sparse matrix B. + * @tparam K Number of columns in the dense matrix A and rows in the sparse + * matrix B. + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix B. + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix B. + */ template inline void @@ -3161,6 +6309,24 @@ compute(const Matrix &A, } // namespace DenseMatrixTransposeMultiplySparse +/** + * @brief + * Multiplies a dense matrix A with the transpose of a sparse matrix B. + * + * This function creates a new dense matrix that is the result of multiplying + * the dense matrix A with the transpose of the sparse matrix B. + * + * @tparam T Value type of the matrix. + * @tparam M Number of rows in the dense matrix (A). + * @tparam N Number of columns in the sparse matrix (B). + * @tparam K Number of columns in the dense matrix (A) and rows in the sparse + * matrix (B). + * @tparam RowIndices_B Type representing the row indices of nonzero elements + * in the sparse matrix (B). + * @tparam RowPointers_B Type representing the row pointers for sparse storage + * in the sparse matrix (B). + * @return A new dense matrix that is the result of A^T * B. + */ template inline Matrix matrix_multiply_ATranspose_mul_SparseB( @@ -3197,6 +6363,21 @@ template struct Loop { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with a dense + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam K Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3214,6 +6395,26 @@ template struct Loop { + /** + * @brief + * End condition for the recursive multiplication computation in sparse matrix + * and dense matrix operations. + * + * This specialization of the Loop struct is instantiated when the Start index + * equals the End index, indicating that all elements in the current segment + * have been processed. The function does nothing, serving as the base case to + * terminate recursion. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam K Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3229,6 +6430,23 @@ template struct Core { + /** + * @brief + * Computes the multiplication of a sparse matrix with a dense matrix. + * + * This function serves as the core computation for multiplying a sparse + * matrix A with a dense matrix B, producing a resulting dense matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix A. + * @tparam M Number of columns in the dense matrix B. + * @tparam K Number of columns in the sparse matrix A and rows in the dense + * matrix B. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix A. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix A. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3243,6 +6461,21 @@ template struct List { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with a dense + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam K Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3256,6 +6489,24 @@ struct List { template struct List { + /** + * @brief + * Computes the multiplication of the first column of a sparse matrix with a + * dense matrix. + * + * This function is a specialization for the base case when J is 0, meaning it + * processes the first column of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam K Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3268,6 +6519,21 @@ struct List { template struct Column { + /** + * @brief + * Recursively computes the multiplication of a sparse matrix with a dense + * matrix. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam K Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3281,6 +6547,24 @@ struct Column { template struct Column { + /** + * @brief + * Computes the multiplication of the first column of a sparse matrix with a + * dense matrix. + * + * This function is a specialization for the base case when I is 0, meaning it + * processes the first column of the sparse matrix. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix. + * @tparam M Number of columns in the dense matrix. + * @tparam K Number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix. + */ static void compute(const CompiledSparseMatrix &A, const Matrix &B, Matrix &Y) { @@ -3289,6 +6573,22 @@ struct Column { } }; +/** + * @brief + * Computes the multiplication of a sparse matrix A with a dense matrix B, + * resulting in a dense matrix Y. + * + * This function serves as the entry point for multiplying a sparse matrix A + * with a dense matrix B, producing a resulting dense matrix Y. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix A. + * @tparam M Number of columns in the dense matrix B. + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix A. + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix A. + */ template inline void @@ -3300,6 +6600,24 @@ compute(const CompiledSparseMatrix &A, } // namespace SparseTransposeMatrixMultiplyDenseMatrix +/** + * @brief + * Multiplies a sparse matrix A with a dense matrix B. + * + * This function creates a new dense matrix that is the result of multiplying + * the sparse matrix A with the dense matrix B. + * + * @tparam T Value type of the matrix. + * @tparam N Number of rows in the sparse matrix (A). + * @tparam M Number of columns in the dense matrix (B). + * @tparam K Number of columns in the sparse matrix (A) and rows in the dense + * matrix (B). + * @tparam RowIndices_A Type representing the row indices of nonzero elements + * in the sparse matrix (A). + * @tparam RowPointers_A Type representing the row pointers for sparse storage + * in the sparse matrix (A). + * @return A new dense matrix that is the result of A * B. + */ template inline Matrix matrix_multiply_SparseAT_mul_B( From 988323a16f267a8e2c0a3b8bd4b856606a19f22a Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 20:13:07 +0900 Subject: [PATCH 05/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_compiled_sparse.hpp | 1292 +++++++++++++++++++ 1 file changed, 1292 insertions(+) diff --git a/base_matrix/base_matrix_compiled_sparse.hpp b/base_matrix/base_matrix_compiled_sparse.hpp index 3dc3bfa..6d25001 100644 --- a/base_matrix/base_matrix_compiled_sparse.hpp +++ b/base_matrix/base_matrix_compiled_sparse.hpp @@ -1,3 +1,43 @@ +/** + * @file base_matrix_compiled_sparse.hpp + * @brief Provides a highly generic, template-based implementation of sparse + * matrix operations for fixed-size matrices. + * + * This file defines the `Base::Matrix` namespace, which contains the + * `CompiledSparseMatrix` class template and a suite of supporting functions and + * meta-programming utilities for manipulating sparse matrices in a highly + * efficient and type-safe manner. The implementation supports both standard + * vector and fixed-size array storage, and provides compile-time and runtime + * algorithms for: + * - Construction and assignment of sparse matrices + * - Conversion between dense and sparse representations + * - Element-wise and block-wise access and modification + * - Transposition, real/complex conversion, and diagonal operations + * - Efficient loop unrolling via template meta-programming for performance + * + * The code is designed for use in high-performance scientific computing, code + * generation, or embedded systems where matrix sparsity patterns are known at + * compile time. + * + * Classes and Main Components: + * + * - CompiledSparseMatrix: + * Represents a sparse matrix with compile-time fixed dimensions and + * sparsity pattern. + * - T: Element type (e.g., double, Complex) + * - M: Number of rows + * - N: Number of columns + * - RowIndices: Type encoding the row indices of nonzero elements + * - RowPointers: Type encoding the start/end of each row's nonzero elements + * Provides constructors, copy/move semantics, element access, and static + * creation utilities. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_COMPILED_SPARSE_HPP__ #define __BASE_MATRIX_COMPILED_SPARSE_HPP__ @@ -19,6 +59,36 @@ namespace Base { namespace Matrix { +/* + * @class CompiledSparseMatrix + * @brief A fixed-size, template-based sparse matrix class for efficient storage + * and operations. + * + * This class represents a sparse matrix with compile-time fixed dimensions and + * a compile-time sparsity pattern, specified by RowIndices and RowPointers + * types. It supports both std::vector and std::array storage for the nonzero + * values, depending on the compile-time macro. + * + * Key Features: + * - Efficient storage of only nonzero elements, with access via operator[]. + * - Copy/move constructors and assignment operators. + * - Static creation utilities for full, dense, and diagonal matrices. + * - Conversion to dense matrix representation. + * - Compile-time and runtime algorithms for element access, assignment, and + * manipulation. + * + * Template Parameters: + * @tparam T Element type (e.g., double, Complex) + * @tparam M Number of columns (mathematical convention) + * @tparam N Number of rows + * @tparam RowIndices Type encoding the row indices of nonzero elements + * @tparam RowPointers Type encoding the start/end of each row's nonzero + * elements + * + * Usage: + * - For high-performance scientific computing, code generation, or embedded + * systems where the sparsity pattern is known at compile time. + */ template class CompiledSparseMatrix { @@ -82,10 +152,44 @@ class CompiledSparseMatrix { } /* Function */ + + /** + * @brief Provides access to the element at the specified index in the matrix + * values. + * + * @param index The position of the element to access. + * @return Reference to the element of type T at the given index. + */ T &operator[](std::size_t index) { return this->values[index]; } + /** + * @brief Provides constant access to the element at the specified index in + * the matrix values. + * + * @param index The position of the element to access. + * @return Constant reference to the element of type T at the given index. + */ const T &operator[](std::size_t index) const { return this->values[index]; } + /** + * @brief Creates a CompiledSparseMatrix where all elements are initialized to + * the given value. + * + * This static inline function constructs and returns a CompiledSparseMatrix + * object with all entries set to the specified value. The size of the + * underlying storage is determined by the number of non-zero elements as + * indicated by RowPointers::list[M]. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of rows in the matrix. + * @tparam N The number of columns in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @param value The value to initialize all elements of the matrix + * with. + * @return CompiledSparseMatrix + * A sparse matrix with all elements set to the specified value. + */ static inline CompiledSparseMatrix full(const T &value) { CompiledSparseMatrix full( @@ -110,6 +214,25 @@ template struct Loop { + /** + * @brief Core loop for computing the output dense matrix from a compiled + * sparse matrix. + * + * This template struct recursively computes the values of the output dense + * matrix by iterating over the non-zero elements of the compiled sparse + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @tparam J Current row index in the output dense matrix. + * @tparam K Current column index in the output dense matrix. + * @tparam Start Starting index for the current row's non-zero + * elements. + * @tparam End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &mat, Matrix &result) { @@ -124,6 +247,21 @@ struct Loop { template struct Loop { + /** + * @brief End of the core loop for computing the output dense matrix. + * + * This template struct represents the termination condition of the core loop + * for computing the output dense matrix from a compiled sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @tparam J Current row index in the output dense matrix. + * @tparam K Current column index in the output dense matrix. + * @tparam End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &mat, Matrix &result) { @@ -138,6 +276,22 @@ struct Loop { template struct Core { + /** + * @brief Core loop for computing the output dense matrix from a compiled + * sparse matrix. + * + * This template struct recursively computes the values of the output dense + * matrix by iterating over the non-zero elements of the compiled sparse + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @tparam J Current row index in the output dense matrix. + * @tparam K Current column index in the output dense matrix. + */ static void compute(const CompiledSparseMatrix &mat, Matrix &result) { @@ -151,6 +305,20 @@ struct Core { template struct Row { + /** + * @brief Row loop for computing the output dense matrix from a compiled + * sparse matrix. + * + * This template struct recursively computes the values of the output dense + * matrix by iterating over the rows of the compiled sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @tparam J Current row index in the output dense matrix. + */ static void compute(const CompiledSparseMatrix &mat, Matrix &result) { @@ -164,6 +332,18 @@ struct Row { template struct Row { + /** + * @brief End of the row loop for computing the output dense matrix. + * + * This template struct represents the termination condition of the row loop + * for computing the output dense matrix from a compiled sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + */ static void compute(const CompiledSparseMatrix &mat, Matrix &result) { @@ -172,6 +352,21 @@ struct Row { } }; +/** + * @brief Computes the output dense matrix from a compiled sparse matrix. + * + * This function computes the output dense matrix by iterating over the + * non-zero elements of the compiled sparse matrix and filling in the + * corresponding entries in the result matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @param mat The compiled sparse matrix to convert to dense format. + * @param result The resulting dense matrix to fill with computed values. + */ template inline void @@ -183,6 +378,22 @@ compute(const CompiledSparseMatrix &mat, } // namespace OutputDenseMatrix +/** + * @brief Converts a compiled sparse matrix to a dense matrix. + * + * This function takes a compiled sparse matrix and converts it to a dense + * matrix by iterating over the non-zero elements and filling in the + * corresponding entries in the result matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices. + * @tparam RowPointers The type representing row pointers. + * @param mat The compiled sparse matrix to convert to dense format. + * @return Matrix A dense matrix containing the values from the sparse + * matrix. + */ template inline Matrix output_dense_matrix( @@ -214,6 +425,23 @@ namespace SubstituteDenseMatrixToSparseMatrix { template struct Column { + /** + * @brief Recursive computation of a column in the sparse matrix. + * + * This template struct recursively computes the values of a specific column + * in the sparse matrix by accessing the corresponding elements in the dense + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam I Current row index in the sparse matrix. + * @tparam J_idx Current column index in the dense matrix. + */ static void compute(const Matrix &A, CompiledSparseMatrix &Y) { @@ -227,6 +455,22 @@ struct Column { template struct Column { + /** + * @brief Termination condition for the column computation. + * + * This template struct represents the termination condition of the column + * computation, where J_idx is 0. It sets the first element of the column in + * the sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam I Current row index in the sparse matrix. + */ static void compute(const Matrix &A, CompiledSparseMatrix &Y) { @@ -239,6 +483,22 @@ struct Column { template struct Row { + /** + * @brief Recursive computation of a row in the sparse matrix. + * + * This template struct recursively computes the values of a specific row in + * the sparse matrix by accessing the corresponding elements in the dense + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam I_idx Current row index in the sparse matrix. + */ static void compute(const Matrix &A, CompiledSparseMatrix &Y) { @@ -252,6 +512,21 @@ struct Row { template struct Row { + /** + * @brief Termination condition for the row computation. + * + * This template struct represents the termination condition of the row + * computation, where I_idx is 0. It computes the last column of the sparse + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + */ static void compute(const Matrix &A, CompiledSparseMatrix &Y) { @@ -259,6 +534,24 @@ struct Row { } }; +/** + * @brief Computes the sparse matrix from a dense matrix. + * + * This function computes the sparse matrix by iterating over the rows and + * columns of the dense matrix and filling in the corresponding entries in the + * sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param A The dense matrix to convert to sparse format. + * @param Y The resulting sparse matrix to fill with computed + * values. + */ template static inline void @@ -269,6 +562,20 @@ compute(const Matrix &A, } // namespace SubstituteDenseMatrixToSparseMatrix +/** + * @brief Creates a compiled sparse matrix from a dense matrix. + * + * This function constructs a compiled sparse matrix from a given dense matrix + * by iterating over its elements and filling in the non-zero values in the + * sparse matrix representation. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The dense matrix to convert to sparse format. + * @return CompiledSparseMatrix + * A sparse matrix representation of the input dense matrix. + */ template inline auto create_compiled_sparse(const Matrix &A) -> CompiledSparseMatrix, @@ -306,6 +613,20 @@ template using DiagMatrixRowPointers = typename TemplatesOperation::ToRowIndices< TemplatesOperation::MatrixRowNumbers<(M + 1)>>::type; +/** + * @brief Creates a compiled sparse matrix from a diagonal matrix. + * + * This function constructs a compiled sparse matrix from a given diagonal + * matrix by copying the diagonal elements into the sparse matrix + * representation. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of rows and columns in the diagonal matrix. + * @param A The diagonal matrix to convert to sparse format. + * @return CompiledSparseMatrix, + * DiagMatrixRowPointers> + * A sparse matrix representation of the input diagonal matrix. + */ template inline auto create_compiled_sparse(const DiagMatrix &A) -> CompiledSparseMatrix, @@ -320,6 +641,23 @@ inline auto create_compiled_sparse(const DiagMatrix &A) } /* Create Compiled Sparse Matrix from SparseAvailable */ + +/** + * @brief Creates a compiled sparse matrix from a sparse available type. + * + * This function constructs a compiled sparse matrix from a given sparse + * available type, which contains the necessary information about the sparsity + * pattern and the number of columns and rows. + * + * @tparam T The type of the matrix elements. + * @tparam SparseAvailable The type representing the sparsity pattern and size. + * @param values An initializer list of values to fill the sparse matrix. + * @return CompiledSparseMatrix, + * RowPointersFromSparseAvailable> + * A sparse matrix representation of the input values. + */ template inline auto create_compiled_sparse(std::initializer_list values) -> CompiledSparseMatrix struct CoreIf { + /** + * @brief Core conditional operation for setting sparse matrix value. + * + * This template struct checks if the current row index matches the specified + * row index and sets the value accordingly. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam K Current index in the row indices list. + * @tparam RowToGet_I The row index to check against. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -361,6 +715,22 @@ struct CoreIf { template struct CoreIf { + /** + * @brief Core conditional operation for setting sparse matrix value when + * RowToSet == RowIndices_A::list[K]. + * + * This template struct sets the value in the sparse matrix if the current + * row index matches the specified row index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam K Current index in the row indices list. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -374,6 +744,25 @@ template struct CoreConditional { + /** + * @brief Core conditional operation for setting sparse matrix value. + * + * This template struct checks if the current row index matches the specified + * row index and sets the value accordingly. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + * @tparam L Difference between RowToSet and RowIndices_A::list[K]. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -388,6 +777,25 @@ template struct CoreConditional { + /** + * @brief Core conditional operation for setting sparse matrix value when + * RowToSet == RowIndices_A::list[K]. + * + * This template struct sets the value in the sparse matrix if the current + * row index matches the specified row index. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -403,6 +811,25 @@ template struct InnerLoop { + /** + * @brief Core inner loop for setting sparse matrix value. + * + * This template struct iterates over the non-zero elements of the sparse + * matrix and sets the value at the specified column and row index. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + * @tparam K_End Ending index for the current row's non-zero elements. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -422,6 +849,25 @@ template struct InnerLoop { + /** + * @brief End of the inner loop for setting sparse matrix value. + * + * This template struct represents the termination condition of the inner + * loop for setting sparse matrix value, where K_End is 0. It does nothing + * as there are no more elements to process. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -437,6 +883,25 @@ template struct OuterConditional { + /** + * @brief Conditional operation for setting sparse matrix value. + * + * This template struct checks if the current column index matches the + * specified column index and performs the necessary operations accordingly. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam C_J Current column index in the output dense matrix. + * @tparam J Current row index in the output dense matrix. + * @tparam J_End Ending index for the current row's non-zero elements. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -452,6 +917,25 @@ template struct OuterConditional { + /** + * @brief Conditional operation for setting sparse matrix value when + * ColumnToSet == J. + * + * This template struct sets the value in the sparse matrix if the current + * column index matches the specified column index. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current row index in the output dense matrix. + * @tparam J_End Ending index for the current row's non-zero elements. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -468,6 +952,25 @@ template struct OuterLoop { + /** + * @brief Core outer loop for setting sparse matrix value. + * + * This template struct iterates over the columns of the sparse matrix and + * performs the necessary operations to set the value at the specified column + * and row index. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam J_End Ending index for the current row's non-zero elements. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -487,6 +990,24 @@ template struct OuterLoop { + /** + * @brief End of the outer loop for setting sparse matrix value. + * + * This template struct represents the termination condition of the outer + * loop for setting sparse matrix value, where J_End is 0. It does nothing as + * there are no more columns to process. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + */ static void compute(CompiledSparseMatrix &A, const T &value) { @@ -496,6 +1017,25 @@ struct OuterLoop @@ -509,6 +1049,25 @@ compute(CompiledSparseMatrix &A, } // namespace SetSparseMatrixValue +/** + * @brief Sets a value in the sparse matrix at a specific column and row index. + * + * This function updates the value at the specified column and row index in the + * sparse matrix. It uses a compile-time loop to find the correct position in + * the sparse matrix and set the value. + * + * @tparam ColumnToSet The column index to set. + * @tparam RowToSet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param A The sparse matrix to update. + * @param value The value to set at the specified column and row index. + */ template @@ -542,6 +1101,25 @@ inline void set_sparse_matrix_value( } /* Set Sparse Matrix each element values */ + +/** + * @brief Sets the value of a specific element in the sparse matrix. + * + * This function updates the value at the specified element index in the sparse + * matrix. It uses a static assertion to ensure that the element index is valid + * and then sets the value at that index. + * + * @tparam ElementToSet The index of the element to set. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param A The sparse matrix to update. + * @param value The value to set at the specified element index. + */ template inline void set_sparse_matrix_element_value( @@ -561,6 +1139,22 @@ namespace GetSparseMatrixValue { template struct CoreIf { + /** + * @brief Core conditional operation for getting sparse matrix value. + * + * This template struct checks if the current row index matches the specified + * row index and retrieves the value accordingly. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam K Current index in the row indices list. + * @tparam RowToGet_I The row index to check against. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -573,6 +1167,22 @@ struct CoreIf { template struct CoreIf { + /** + * @brief Core conditional operation for getting sparse matrix value when + * RowToGet == RowIndices_A::list[K]. + * + * This template struct retrieves the value from the sparse matrix if the + * current row index matches the specified row index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam K Current index in the row indices list. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -586,6 +1196,25 @@ template struct CoreConditional { + /** + * @brief Core conditional operation for getting sparse matrix value. + * + * This template struct checks if the current row index matches the specified + * row index and retrieves the value accordingly. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + * @tparam L Difference between RowToGet and RowIndices_A::list[K]. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -600,6 +1229,25 @@ template struct CoreConditional { + /** + * @brief Core conditional operation for getting sparse matrix value when + * RowToGet == RowIndices_A::list[K]. + * + * This template struct retrieves the value from the sparse matrix if the + * current row index matches the specified row index. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -615,6 +1263,25 @@ template struct InnerLoop { + /** + * @brief Core inner loop for getting sparse matrix value. + * + * This template struct iterates over the non-zero elements of the sparse + * matrix and retrieves the value at the specified column and row index. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + * @tparam K_End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -634,6 +1301,25 @@ template struct InnerLoop { + /** + * @brief End of the inner loop for getting sparse matrix value. + * + * This template struct represents the termination condition of the inner + * loop for getting sparse matrix value, where K_End is 0. It does nothing + * as there are no more elements to process. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -649,6 +1335,25 @@ template struct OuterConditional { + /** + * @brief Conditional operation for getting sparse matrix value. + * + * This template struct checks if the current column index matches the + * specified column index and performs the necessary operations accordingly. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam C_J Current column index in the output dense matrix. + * @tparam J Current row index in the output dense matrix. + * @tparam J_End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -664,6 +1369,25 @@ template struct OuterConditional { + /** + * @brief Conditional operation for getting sparse matrix value when + * ColumnToGet == J. + * + * This template struct retrieves the value from the sparse matrix if the + * current column index matches the specified column index. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current row index in the output dense matrix. + * @tparam J_End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -680,6 +1404,25 @@ template struct OuterLoop { + /** + * @brief Core outer loop for getting sparse matrix value. + * + * This template struct iterates over the columns of the sparse matrix and + * performs the necessary operations to retrieve the value at the specified + * column and row index. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + * @tparam J_End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -699,6 +1442,24 @@ template struct OuterLoop { + /** + * @brief End of the outer loop for getting sparse matrix value. + * + * This template struct represents the termination condition of the outer + * loop for getting sparse matrix value, where J_End is 0. It does nothing as + * there are no more columns to process. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @tparam J Current index in the row indices list. + */ static void compute(const CompiledSparseMatrix &A, T &value) { @@ -708,6 +1469,25 @@ struct OuterLoop @@ -720,6 +1500,27 @@ compute(const CompiledSparseMatrix &A, } // namespace GetSparseMatrixValue +/** + * @brief Retrieves the value at a specific column and row index in the sparse + * matrix. + * + * This function uses compile-time loops to find the value at the specified + * column and row index in the sparse matrix. It returns the value if found, + * otherwise returns zero. + * + * @tparam ColumnToGet The column index to get. + * @tparam RowToGet The row index to check against. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param A The sparse matrix to query. + * @return The value at the specified column and row index, or zero if not + * found. + */ template @@ -756,6 +1557,24 @@ inline T get_sparse_matrix_value( } /* Get Sparse Matrix each element values */ + +/** + * @brief Retrieves the value of a specific element in the sparse matrix. + * + * This function returns the value at the specified element index in the sparse + * matrix. It uses a static assertion to ensure that the element index is valid. + * + * @tparam ElementToGet The index of the element to get. + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_A The type representing row indices of the sparse + * matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param A The sparse matrix to query. + * @return The value at the specified element index. + */ template inline T get_sparse_matrix_element_value( @@ -774,6 +1593,26 @@ template struct OutputTransposeMatrixLoop { + /** + * @brief Core loop for outputting the transpose of a sparse matrix. + * + * This template struct iterates over the non-zero elements of the sparse + * matrix and sets the corresponding values in the result matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam Result_Type The type of the result matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + * @tparam Start Starting index for the current row's non-zero + * elements. + * @tparam End Ending index for the current row's non-zero elements. + */ static void compute(const CompiledSparseMatrix &mat, Result_Type &result) { @@ -791,6 +1630,25 @@ template struct OutputTransposeMatrixLoop { + /** + * @brief End of the core loop for outputting the transpose of a sparse + * matrix. + * + * This template struct represents the termination condition of the core loop + * for outputting the transpose of a sparse matrix, where Start == End. It + * does nothing as there are no more elements to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam Result_Type The type of the result matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + */ static void compute(const CompiledSparseMatrix &mat, Result_Type &result) { @@ -805,6 +1663,24 @@ template struct OutputTransposeMatrixCore { + /** + * @brief Core loop for outputting the transpose of a sparse matrix. + * + * This template struct iterates over the rows of the sparse matrix and + * processes the non-zero elements to set the corresponding values in the + * result matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam Result_Type The type of the result matrix. + * @tparam J Current index in the row indices list. + * @tparam K Current index in the row pointers list. + */ static void compute(const CompiledSparseMatrix &mat, Result_Type &result) { @@ -819,6 +1695,22 @@ struct OutputTransposeMatrixCore { template struct OutputTransposeMatrixRow { + /** + * @brief Row loop for outputting the transpose of a sparse matrix. + * + * This template struct iterates over the rows of the sparse matrix and + * processes each row to set the corresponding values in the result matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam Result_Type The type of the result matrix. + * @tparam J Current index in the row indices list. + */ static void compute(const CompiledSparseMatrix &mat, Result_Type &result) { @@ -835,6 +1727,22 @@ template struct OutputTransposeMatrixRow { + /** + * @brief End of the row loop for outputting the transpose of a sparse matrix. + * + * This template struct represents the termination condition of the row loop + * for outputting the transpose of a sparse matrix, where J is 0. It does + * nothing as there are no more rows to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam Result_Type The type of the result matrix. + */ static void compute(const CompiledSparseMatrix &mat, Result_Type &result) { @@ -844,6 +1752,25 @@ struct OutputTransposeMatrixRow inline void @@ -877,6 +1804,22 @@ struct Transpose { } // namespace CompiledSparseOperation +/** + * @brief Outputs the transpose of a sparse matrix. + * + * This function computes the transpose of the given sparse matrix and returns + * it as a new sparse matrix with transposed dimensions. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the input matrix. + * @tparam N The number of rows in the input matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param mat The sparse matrix to transpose. + * @return A new sparse matrix representing the transpose of the input matrix. + */ template inline auto output_matrix_transpose( @@ -903,6 +1846,22 @@ namespace ConvertRealSparseMatrixToComplex { template struct Loop { + /** + * @brief Core loop for converting a real sparse matrix to a complex sparse + * matrix. + * + * This template struct iterates over the non-zero elements of the real sparse + * matrix and sets the corresponding values in the complex sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam I Current index in the values list. + */ static void compute( const CompiledSparseMatrix &From_matrix, CompiledSparseMatrix, M, N, RowIndices, RowPointers> @@ -918,6 +1877,22 @@ struct Loop { template struct Loop { + /** + * @brief End of the core loop for converting a real sparse matrix to a + * complex sparse matrix. + * + * This template struct represents the termination condition of the core loop + * for converting a real sparse matrix to a complex sparse matrix, where I is + * 0. It does nothing as there are no more elements to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + */ static void compute( const CompiledSparseMatrix &From_matrix, CompiledSparseMatrix, M, N, RowIndices, RowPointers> @@ -928,6 +1903,24 @@ struct Loop { } }; +/** + * @brief Computes the conversion from a real sparse matrix to a complex sparse + * matrix. + * + * This function uses compile-time loops to iterate over the non-zero elements + * of the real sparse matrix and sets the corresponding values in the complex + * sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param From_matrix The real sparse matrix to convert. + * @param To_matrix Reference to store the converted complex sparse matrix. + */ template inline void compute( @@ -941,6 +1934,23 @@ inline void compute( } // namespace ConvertRealSparseMatrixToComplex +/** + * @brief Converts a real sparse matrix to a complex sparse matrix. + * + * This function takes a real sparse matrix and converts it to a complex sparse + * matrix by setting the real part of each element in the complex matrix to the + * corresponding value in the real matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param From_matrix The real sparse matrix to convert. + * @return A new complex sparse matrix with the converted values. + */ template inline CompiledSparseMatrix, M, N, RowIndices, RowPointers> @@ -972,6 +1982,21 @@ namespace GetRealSparseMatrixFromComplex { template struct Loop { + /** + * @brief Core loop for extracting the real part from a complex sparse matrix. + * + * This template struct iterates over the non-zero elements of the complex + * sparse matrix and sets the corresponding values in the real sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam I Current index in the values list. + */ static void compute(const CompiledSparseMatrix, M, N, RowIndices, RowPointers> &From_matrix, @@ -987,6 +2012,22 @@ struct Loop { template struct Loop { + /** + * @brief End of the core loop for extracting the real part from a complex + * sparse matrix. + * + * This template struct represents the termination condition of the core loop + * for extracting the real part from a complex sparse matrix, where I is 0. It + * does nothing as there are no more elements to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + */ static void compute(const CompiledSparseMatrix, M, N, RowIndices, RowPointers> &From_matrix, @@ -998,6 +2039,24 @@ struct Loop { } }; +/** + * @brief Computes the conversion from a complex sparse matrix to a real sparse + * matrix. + * + * This function uses compile-time loops to iterate over the non-zero elements + * of the complex sparse matrix and sets the corresponding values in the real + * sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param From_matrix The complex sparse matrix to convert. + * @param To_matrix Reference to store the converted real sparse matrix. + */ template inline void @@ -1011,6 +2070,22 @@ compute(const CompiledSparseMatrix, M, N, RowIndices, RowPointers> } // namespace GetRealSparseMatrixFromComplex +/** + * @brief Converts a complex sparse matrix to a real sparse matrix. + * + * This function takes a complex sparse matrix and extracts the real part of + * each element, returning a new real sparse matrix with the converted values. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param From_matrix The complex sparse matrix to convert. + * @return A new real sparse matrix with the converted values. + */ template inline CompiledSparseMatrix @@ -1043,6 +2118,23 @@ namespace GetImagSparseMatrixFromComplex { template struct Loop { + /** + * @brief Core loop for extracting the imaginary part from a complex sparse + * matrix. + * + * This template struct iterates over the non-zero elements of the complex + * sparse matrix and sets the corresponding values in the imaginary sparse + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @tparam I Current index in the values list. + */ static void compute(const CompiledSparseMatrix, M, N, RowIndices, RowPointers> &From_matrix, @@ -1058,6 +2150,22 @@ struct Loop { template struct Loop { + /** + * @brief End of the core loop for extracting the imaginary part from a + * complex sparse matrix. + * + * This template struct represents the termination condition of the core loop + * for extracting the imaginary part from a complex sparse matrix, where I is + * 0. It does nothing as there are no more elements to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + */ static void compute(const CompiledSparseMatrix, M, N, RowIndices, RowPointers> &From_matrix, @@ -1069,6 +2177,25 @@ struct Loop { } }; +/** + * @brief Computes the conversion from a complex sparse matrix to an imaginary + * sparse matrix. + * + * This function uses compile-time loops to iterate over the non-zero elements + * of the complex sparse matrix and sets the corresponding values in the + * imaginary sparse matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param From_matrix The complex sparse matrix to convert. + * @param To_matrix Reference to store the converted imaginary sparse + * matrix. + */ template inline void @@ -1082,6 +2209,23 @@ compute(const CompiledSparseMatrix, M, N, RowIndices, RowPointers> } // namespace GetImagSparseMatrixFromComplex +/** + * @brief Converts a complex sparse matrix to an imaginary sparse matrix. + * + * This function takes a complex sparse matrix and extracts the imaginary part + * of each element, returning a new imaginary sparse matrix with the converted + * values. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices The type representing row indices of the sparse + * matrix. + * @tparam RowPointers The type representing row pointers of the sparse + * matrix. + * @param From_matrix The complex sparse matrix to convert. + * @return A new imaginary sparse matrix with the converted values. + */ template inline CompiledSparseMatrix @@ -1115,6 +2259,26 @@ template struct Loop { + /** + * @brief Core loop for diagonal inverse multiplication of a sparse matrix. + * + * This template struct iterates over the non-zero elements of the sparse + * matrix and performs the diagonal inverse multiplication with the diagonal + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse + * matrix B. + * @tparam RowPointers_B The type representing row pointers of the sparse + * matrix B. + * @tparam J Current index in the row indices list of B. + * @tparam K Current index in the row pointers list of B. + * @tparam Start Starting index for the current row's non-zero + * elements. + * @tparam End Ending index for the current row's non-zero elements. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -1133,6 +2297,24 @@ struct Loop { template struct Loop { + /** + * @brief End of the core loop for diagonal inverse multiplication of a sparse + * matrix. + * + * This template struct represents the termination condition of the core loop + * for diagonal inverse multiplication of a sparse matrix, where Start == End. + * It does nothing as there are no more elements to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse + * matrix B. + * @tparam RowPointers_B The type representing row pointers of the sparse + * matrix B. + * @tparam J Current index in the row indices list of B. + * @tparam K Current index in the row pointers list of B. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -1151,6 +2333,23 @@ struct Loop { template struct Core { + /** + * @brief Core loop for diagonal inverse multiplication of a sparse matrix. + * + * This template struct iterates over the non-zero elements of the sparse + * matrix and performs the diagonal inverse multiplication with the diagonal + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse + * matrix B. + * @tparam RowPointers_B The type representing row pointers of the sparse + * matrix B. + * @tparam J Current index in the row indices list of B. + * @tparam K Current index in the row pointers list of B. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -1166,6 +2365,22 @@ struct Core { template struct Row { + /** + * @brief Row loop for diagonal inverse multiplication of a sparse matrix. + * + * This template struct iterates over the rows of the sparse matrix and + * processes each row to perform the diagonal inverse multiplication with the + * diagonal matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse + * matrix B. + * @tparam RowPointers_B The type representing row pointers of the sparse + * matrix B. + * @tparam J Current index in the row indices list of B. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -1183,6 +2398,22 @@ struct Row { template struct Row { + /** + * @brief End of the row loop for diagonal inverse multiplication of a sparse + * matrix. + * + * This template struct represents the termination condition of the row loop + * for diagonal inverse multiplication of a sparse matrix, where J is 0. It + * does nothing as there are no more rows to process. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse + * matrix B. + * @tparam RowPointers_B The type representing row pointers of the sparse + * matrix B. + */ static void compute(const DiagMatrix &A, const CompiledSparseMatrix &B, @@ -1194,6 +2425,25 @@ struct Row { } }; +/** + * @brief Computes the diagonal inverse multiplication of a sparse matrix. + * + * This function uses compile-time loops to iterate over the rows and non-zero + * elements of the sparse matrix, performing the diagonal inverse multiplication + * with the diagonal matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse matrix + * B. + * @tparam RowPointers_B The type representing row pointers of the sparse matrix + * B. + * @param A The diagonal matrix to multiply with. + * @param B The sparse matrix to multiply. + * @param division_min Minimum value to avoid division by zero. + * @param result Reference to store the result of the multiplication. + */ template inline void @@ -1208,6 +2458,26 @@ compute(const DiagMatrix &A, } // namespace DiagonalInverseMultiplySparse +/** + * @brief Performs diagonal inverse multiplication of a sparse matrix. + * + * This function takes a diagonal matrix and a sparse matrix, and performs the + * diagonal inverse multiplication, returning a new sparse matrix with the + * results. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse matrix + * B. + * @tparam RowPointers_B The type representing row pointers of the sparse matrix + * B. + * @param A The diagonal matrix to multiply with. + * @param B The sparse matrix to multiply. + * @param division_min Minimum value to avoid division by zero. + * @return A new sparse matrix resulting from the diagonal inverse + * multiplication. + */ template inline CompiledSparseMatrix @@ -1239,6 +2509,28 @@ diag_inv_multiply_sparse( return result; } +/** + * @brief Performs diagonal inverse multiplication of a sparse matrix with a + * partitioned approach. + * + * This function takes a diagonal matrix and a sparse matrix, and performs the + * diagonal inverse multiplication, returning a new sparse matrix with the + * results. It is optimized for partitioned sparse matrices. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam RowIndices_B The type representing row indices of the sparse matrix + * B. + * @tparam RowPointers_B The type representing row pointers of the sparse matrix + * B. + * @param A The diagonal matrix to multiply with. + * @param B The sparse matrix to multiply. + * @param division_min Minimum value to avoid division by zero. + * @param matrix_size Size of the matrix for partitioning. + * @return A new sparse matrix resulting from the diagonal inverse + * multiplication. + */ template inline CompiledSparseMatrix From 1afa34522539a7329c08c34a24fa83dacf0db014 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 20:17:42 +0900 Subject: [PATCH 06/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_complex.hpp | 333 ++++++++++++++++++++++++++++ 1 file changed, 333 insertions(+) diff --git a/base_matrix/base_matrix_complex.hpp b/base_matrix/base_matrix_complex.hpp index 043f5c2..54efd36 100644 --- a/base_matrix/base_matrix_complex.hpp +++ b/base_matrix/base_matrix_complex.hpp @@ -1,3 +1,24 @@ +/** + * @file base_matrix_complex.hpp + * @brief Defines a templated Complex number class and related operations for + * matrix computations. + * + * This file provides the implementation of a generic Complex class within + * the Base::Matrix namespace, supporting basic arithmetic, assignment, and + * comparison operators for complex numbers. It also includes a set of utility + * functions for complex arithmetic, such as division, absolute value, phase, + * conjugate, square root, and sign. The code is designed to be used in + * mathematical and matrix computation contexts, with additional type traits to + * identify complex types. + * + * Classes: + * - Complex: A template class representing a complex number with real and + * imaginary parts of type T. Provides constructors, copy/move semantics, + * arithmetic operators, and compound assignment operators for complex number + * manipulation. + * - Is_Complex_Type: Type trait to determine if a type is a specialization + * of Complex. + */ #ifndef __BASE_MATRIX_COMPLEX_HPP__ #define __BASE_MATRIX_COMPLEX_HPP__ @@ -12,6 +33,15 @@ namespace Base { namespace Matrix { +/** + * @brief A templated Complex number class. + * + * This class represents a complex number with real and imaginary parts of + * type T. It supports basic arithmetic operations, assignment, and comparison + * operators. + * + * @tparam T The type of the real and imaginary parts of the complex number. + */ template class Complex { public: /* Type */ @@ -48,6 +78,16 @@ template class Complex { } /* Method */ + + /** + * @brief Adds two Complex numbers. + * + * This operator overloads the + operator to perform element-wise addition + * of the real and imaginary parts of two Complex numbers. + * + * @param a_comp The Complex number to add to this instance. + * @return A new Complex number representing the sum of this and a_comp. + */ inline Complex operator+(const Complex &a_comp) const { Complex result; @@ -57,6 +97,16 @@ template class Complex { return result; } + /** + * @brief Subtracts two Complex numbers. + * + * This operator overloads the - operator to perform element-wise subtraction + * of the real and imaginary parts of two Complex numbers. + * + * @param a_comp The Complex number to subtract from this instance. + * @return A new Complex number representing the difference of this and + * a_comp. + */ inline Complex operator-(const Complex &a_comp) const { Complex result; @@ -66,6 +116,14 @@ template class Complex { return result; } + /** + * @brief Negates the Complex number. + * + * This operator overloads the unary - operator to negate both the real and + * imaginary parts of the Complex number. + * + * @return A new Complex number representing the negation of this instance. + */ inline Complex operator-(void) const { Complex result; @@ -75,6 +133,16 @@ template class Complex { return result; } + /** + * @brief Multiplies two Complex numbers. + * + * This operator overloads the * operator to perform multiplication of two + * Complex numbers using the formula (a + bi)(c + di) = (ac - bd) + (ad + + * bc)i. + * + * @param a_comp The Complex number to multiply with this instance. + * @return A new Complex number representing the product of this and a_comp. + */ inline Complex operator*(const Complex &a_comp) const { Complex result; @@ -84,25 +152,80 @@ template class Complex { return result; } + /** + * @brief Divides two Complex numbers. + * + * This operator overloads the / operator to perform division of two Complex + * numbers using the formula (a + bi)/(c + di) = ((ac + bd)/(c^2 + d^2)) + + * ((bc - ad)/(c^2 + d^2))i. + * + * @param a_comp The Complex number to divide this instance by. + * @return A new Complex number representing the quotient of this and a_comp. + */ inline void operator+=(const T &a) { this->real += a; } + /** + * @brief Subtracts a scalar from the Complex number. + * + * This operator overloads the -= operator to perform element-wise + * subtraction of a scalar from the real part of the Complex number. + * + * @param a The scalar to subtract from the real part of this instance. + */ inline void operator-=(const T &a) { this->real -= a; } + /** + * @brief Multiplies the Complex number by a scalar. + * + * This operator overloads the *= operator to perform element-wise + * multiplication of the real and imaginary parts of the Complex number by a + * scalar. + * + * @param a The scalar to multiply both the real and imaginary parts of this + * instance. + */ inline void operator*=(const T &a) { this->real *= a; this->imag *= a; } + /** + * @brief Divides the Complex number by a scalar. + * + * This operator overloads the /= operator to perform element-wise division + * of the real and imaginary parts of the Complex number by a scalar. + * + * @param a The scalar to divide both the real and imaginary parts of this + * instance. + */ inline void operator+=(const Complex &a_comp) { this->real += a_comp.real; this->imag += a_comp.imag; } + /** + * @brief Subtracts a Complex number from this instance. + * + * This operator overloads the -= operator to perform element-wise + * subtraction of the real and imaginary parts of a Complex number from this + * instance. + * + * @param a_comp The Complex number to subtract from this instance. + */ inline void operator-=(const Complex &a_comp) { this->real -= a_comp.real; this->imag -= a_comp.imag; } + /** + * @brief Multiplies this Complex number by another Complex number. + * + * This operator overloads the *= operator to perform multiplication of two + * Complex numbers using the formula (a + bi)(c + di) = (ac - bd) + (ad + + * bc)i. + * + * @param a_comp The Complex number to multiply with this instance. + */ inline void operator*=(const Complex &a_comp) { T real_temp = this->real * a_comp.real - this->imag * a_comp.imag; @@ -118,6 +241,17 @@ template class Complex { }; /* Scalar */ + +/** + * @brief Adds a scalar to a Complex number. + * + * This operator overloads the + operator to add a scalar to the real part of + * a Complex number, leaving the imaginary part unchanged. + * + * @param a_comp The Complex number to add the scalar to. + * @param b The scalar to add to the real part of a_comp. + * @return A new Complex number representing the sum of a_comp and b. + */ template inline Complex operator+(const Complex &a_comp, T b) { Complex result; @@ -128,6 +262,16 @@ inline Complex operator+(const Complex &a_comp, T b) { return result; } +/** + * @brief Adds a Complex number to a scalar. + * + * This operator overloads the + operator to add a Complex number to a scalar, + * effectively adding the scalar to the real part of the Complex number. + * + * @param b The scalar to add to the real part of a_comp. + * @param a_comp The Complex number to which the scalar is added. + * @return A new Complex number representing the sum of b and a_comp. + */ template inline Complex operator+(T b, const Complex &a_comp) { Complex result; @@ -138,6 +282,16 @@ inline Complex operator+(T b, const Complex &a_comp) { return result; } +/** + * @brief Subtracts a scalar from a Complex number. + * + * This operator overloads the - operator to subtract a scalar from the real + * part of a Complex number, leaving the imaginary part unchanged. + * + * @param a_comp The Complex number from which the scalar is subtracted. + * @param b The scalar to subtract from the real part of a_comp. + * @return A new Complex number representing the difference of a_comp and b. + */ template inline Complex operator-(const Complex &a_comp, T b) { Complex result; @@ -148,6 +302,17 @@ inline Complex operator-(const Complex &a_comp, T b) { return result; } +/** + * @brief Subtracts a Complex number from a scalar. + * + * This operator overloads the - operator to subtract a Complex number from a + * scalar, effectively subtracting the real part of the Complex number from the + * scalar and negating the imaginary part. + * + * @param b The scalar from which the Complex number is subtracted. + * @param a_comp The Complex number to subtract from b. + * @return A new Complex number representing the difference of b and a_comp. + */ template inline Complex operator-(T b, const Complex &a_comp) { Complex result; @@ -158,6 +323,16 @@ inline Complex operator-(T b, const Complex &a_comp) { return result; } +/** + * @brief Multiplies a Complex number by a scalar. + * + * This operator overloads the * operator to multiply both the real and + * imaginary parts of a Complex number by a scalar. + * + * @param a_comp The Complex number to multiply by the scalar. + * @param b The scalar to multiply both the real and imaginary parts of a_comp. + * @return A new Complex number representing the product of a_comp and b. + */ template inline Complex operator*(const Complex &a_comp, T b) { Complex result; @@ -168,6 +343,16 @@ inline Complex operator*(const Complex &a_comp, T b) { return result; } +/** + * @brief Multiplies a scalar by a Complex number. + * + * This operator overloads the * operator to multiply both the real and + * imaginary parts of a Complex number by a scalar. + * + * @param b The scalar to multiply both the real and imaginary parts of a_comp. + * @param a_comp The Complex number to multiply by the scalar. + * @return A new Complex number representing the product of b and a_comp. + */ template inline Complex operator*(T b, const Complex &a_comp) { Complex result; @@ -178,6 +363,16 @@ inline Complex operator*(T b, const Complex &a_comp) { return result; } +/** + * @brief Divides a Complex number by a scalar. + * + * This operator overloads the / operator to divide both the real and + * imaginary parts of a Complex number by a scalar. + * + * @param a_comp The Complex number to divide by the scalar. + * @param b The scalar to divide both the real and imaginary parts of a_comp. + * @return A new Complex number representing the quotient of a_comp and b. + */ template inline Complex operator/(const Complex &a_comp, T b) { Complex result; @@ -188,6 +383,17 @@ inline Complex operator/(const Complex &a_comp, T b) { return result; } +/** + * @brief Divides a scalar by a Complex number. + * + * This operator overloads the / operator to divide a scalar by a Complex + * number, effectively dividing the scalar by the magnitude of the Complex + * number and adjusting the real and imaginary parts accordingly. + * + * @param b The scalar to divide by the Complex number. + * @param a_comp The Complex number by which the scalar is divided. + * @return A new Complex number representing the quotient of b and a_comp. + */ template inline bool operator<(const Complex &a_comp, const Complex &b_comp) { bool result = false; @@ -201,6 +407,17 @@ inline bool operator<(const Complex &a_comp, const Complex &b_comp) { return result; } +/** + * @brief Compares two Complex numbers for less than or equal to. + * + * This operator overloads the <= operator to compare the sum of the real and + * imaginary parts of two Complex numbers. + * + * @param a_comp The first Complex number to compare. + * @param b_comp The second Complex number to compare. + * @return True if the sum of a_comp is less than or equal to the sum of + * b_comp, false otherwise. + */ template inline bool operator<=(const Complex &a_comp, const Complex &b_comp) { bool result = false; @@ -214,6 +431,17 @@ inline bool operator<=(const Complex &a_comp, const Complex &b_comp) { return result; } +/** + * @brief Compares two Complex numbers for greater than. + * + * This operator overloads the > operator to compare the sum of the real and + * imaginary parts of two Complex numbers. + * + * @param a_comp The first Complex number to compare. + * @param b_comp The second Complex number to compare. + * @return True if the sum of a_comp is greater than the sum of b_comp, false + * otherwise. + */ template inline bool operator>(const Complex &a_comp, const Complex &b_comp) { bool result = false; @@ -227,6 +455,17 @@ inline bool operator>(const Complex &a_comp, const Complex &b_comp) { return result; } +/** + * @brief Compares two Complex numbers for greater than or equal to. + * + * This operator overloads the >= operator to compare the sum of the real and + * imaginary parts of two Complex numbers. + * + * @param a_comp The first Complex number to compare. + * @param b_comp The second Complex number to compare. + * @return True if the sum of a_comp is greater than or equal to the sum of + * b_comp, false otherwise. + */ template inline bool operator>=(const Complex &a_comp, const Complex &b_comp) { bool result = false; @@ -241,6 +480,19 @@ inline bool operator>=(const Complex &a_comp, const Complex &b_comp) { } /* complex functions */ + +/** + * @brief Divides two Complex numbers. + * + * This function performs division of two Complex numbers using the formula + * (a + bi)/(c + di) = ((ac + bd)/(c^2 + d^2)) + ((bc - ad)/(c^2 + d^2))i, + * avoiding division by zero using a minimum value. + * + * @param a_comp The Complex number to be divided. + * @param b_comp The Complex number to divide by. + * @param division_min The minimum value to avoid division by zero. + * @return A new Complex number representing the quotient of a_comp and b_comp. + */ template inline Complex complex_divide(const Complex &a_comp, const Complex &b_comp, @@ -258,6 +510,18 @@ inline Complex complex_divide(const Complex &a_comp, return result; } +/** + * @brief Divides a scalar by a Complex number. + * + * This function performs division of a scalar by a Complex number using the + * formula (a)/(c + di) = (a*c)/(c^2 + d^2) - (a*d)/(c^2 + d^2)i, avoiding + * division by zero using a minimum value. + * + * @param a The scalar to be divided. + * @param b_comp The Complex number to divide by. + * @param division_min The minimum value to avoid division by zero. + * @return A new Complex number representing the quotient of a and b_comp. + */ template inline Complex complex_divide(T a, const Complex &b_comp, const T &division_min) { @@ -272,6 +536,16 @@ inline Complex complex_divide(T a, const Complex &b_comp, return result; } +/** + * @brief Divides a Complex number by a scalar. + * + * This function performs division of a Complex number by a scalar, effectively + * dividing both the real and imaginary parts by the scalar. + * + * @param a_comp The Complex number to be divided. + * @param b The scalar to divide both the real and imaginary parts of a_comp. + * @return A new Complex number representing the quotient of a_comp and b. + */ template inline T complex_abs(const Complex &a_comp) { T result; @@ -281,6 +555,15 @@ template inline T complex_abs(const Complex &a_comp) { return result; } +/** + * @brief Computes the squared absolute value of a Complex number. + * + * This function calculates the squared absolute value of a Complex number + * using the formula |a + bi|^2 = a^2 + b^2. + * + * @param a_comp The Complex number whose squared absolute value is computed. + * @return The squared absolute value of a_comp. + */ template inline T complex_abs_sq(const Complex &a_comp) { T result; @@ -289,6 +572,16 @@ template inline T complex_abs_sq(const Complex &a_comp) { return result; } +/** + * @brief Computes the phase (angle) of a Complex number. + * + * This function calculates the phase of a Complex number using the + * atan2 function, which computes the angle in radians between the positive + * x-axis and the line to the point (real, imag). + * + * @param a_comp The Complex number whose phase is computed. + * @return The phase of a_comp in radians. + */ template inline T complex_phase(const Complex &a_comp) { T result; @@ -297,6 +590,15 @@ template inline T complex_phase(const Complex &a_comp) { return result; } +/** + * @brief Computes the complex conjugate of a Complex number. + * + * This function returns a new Complex number with the same real part and the + * negated imaginary part of the input Complex number. + * + * @param a_comp The Complex number whose conjugate is computed. + * @return A new Complex number representing the conjugate of a_comp. + */ template inline Complex complex_conjugate(const Complex &a_comp) { Complex result; @@ -307,6 +609,16 @@ inline Complex complex_conjugate(const Complex &a_comp) { return result; } +/** + * @brief Computes the square root of a Complex number. + * + * This function calculates the square root of a Complex number using a + * method that avoids division by zero, returning a new Complex number + * representing the square root. + * + * @param a_comp The Complex number whose square root is computed. + * @return A new Complex number representing the square root of a_comp. + */ template inline Complex complex_sqrt(const Complex &a_comp) { Complex result; @@ -327,6 +639,17 @@ template inline Complex complex_sqrt(const Complex &a_comp) { return result; } +/** + * @brief Computes the square root of a Complex number with a minimum division + * value. + * + * This function calculates the square root of a Complex number, ensuring that + * division by zero is avoided by using a specified minimum value for division. + * + * @param a_comp The Complex number whose square root is computed. + * @param division_min The minimum value to avoid division by zero. + * @return A new Complex number representing the square root of a_comp. + */ template inline Complex complex_sqrt(const Complex &a_comp, const T &division_min) { @@ -349,6 +672,16 @@ inline Complex complex_sqrt(const Complex &a_comp, return result; } +/** + * @brief Computes the sign of a Complex number. + * + * This function calculates the sign of a Complex number by normalizing it to + * have a magnitude of 1, effectively returning a new Complex number with the + * same direction as the input but with a magnitude of 1. + * + * @param a_comp The Complex number whose sign is computed. + * @return A new Complex number representing the sign of a_comp. + */ template inline Complex complex_sign(const Complex &a_comp, const T &division_min) { From 7f3e95f280795f47f5fdae55daaf9d1efbe1653d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 20:34:46 +0900 Subject: [PATCH 07/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_concatenate.hpp | 957 ++++++++++++++++++++++++ 1 file changed, 957 insertions(+) diff --git a/base_matrix/base_matrix_concatenate.hpp b/base_matrix/base_matrix_concatenate.hpp index 701737e..cad5ff0 100644 --- a/base_matrix/base_matrix_concatenate.hpp +++ b/base_matrix/base_matrix_concatenate.hpp @@ -1,3 +1,21 @@ +/** + * @file base_matrix_concatenate.hpp + * @brief Provides template functions and structures for concatenating matrices + * (dense, diagonal, and sparse) both vertically and horizontally. + * + * This header defines a comprehensive set of template-based utilities for + * concatenating matrices of various types (dense, diagonal, and compiled + * sparse) in both vertical and horizontal directions. The code supports + * compile-time matrix size and type deduction, and provides both loop-based and + * template-recursive implementations for efficient operations. It also includes + * helper structures for managing sparse matrix metadata during concatenation. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_CONCATENATE_HPP__ #define __BASE_MATRIX_CONCATENATE_HPP__ @@ -21,6 +39,25 @@ namespace Matrix { template struct VerticalConcatenateLoop { + + /** + * @brief Concatenates two matrices vertically and stores the result in a + * third matrix. + * + * This static function copies the rows of matrix A and matrix B into the + * output matrix Y, stacking A on top of B. The operation is performed + * recursively for each row index. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @tparam N The number of rows in matrices A and B. + * @tparam Row The current row index for recursion. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @param Y The output matrix with M + P rows and N columns, containing the + * concatenated result. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &Y) { Base::Utility::copy(A.data[Row], Y.data[Row]); @@ -32,6 +69,21 @@ struct VerticalConcatenateLoop { // end of recursion template struct VerticalConcatenateLoop { + /** + * @brief Base case for the recursive vertical concatenation loop. + * + * This static function copies the first row of matrix A and matrix B into + * the output matrix Y, completing the vertical concatenation. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @tparam N The number of rows in matrices A and B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @param Y The output matrix with M + P rows and N columns, containing the + * concatenated result. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &Y) { Base::Utility::copy(A.data[0], Y.data[0]); @@ -39,6 +91,23 @@ struct VerticalConcatenateLoop { } }; +/** + * @brief Concatenates two matrices vertically and stores the result in a third + * matrix. + * + * This function takes two matrices A and B, concatenates them vertically, and + * stores the result in matrix Y. The operation is performed using a template + * loop to handle the row-wise copying of elements. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrix A. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @param Y The output matrix with M + P rows and N columns, containing the + * concatenated result. + */ template static inline void COMPILED_SPARSE_VERTICAL_CONCATENATE(const Matrix &A, @@ -47,6 +116,21 @@ COMPILED_SPARSE_VERTICAL_CONCATENATE(const Matrix &A, VerticalConcatenateLoop::compute(A, B, Y); } +/** + * @brief Updates a vertically concatenated matrix with two input matrices. + * + * This function takes two matrices A and B, and updates the output matrix Y + * by concatenating A on top of B. The operation is performed using either a + * loop-based or template-based approach, depending on the compilation flags. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix(Matrix &Y, const Matrix &A, @@ -66,6 +150,23 @@ inline void update_vertically_concatenated_matrix(Matrix &Y, #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates two matrices vertically and returns the result as a new + * matrix. + * + * This function takes two matrices A and B, concatenates them vertically, and + * returns a new matrix Y containing the result. The operation is performed + * using a template loop to handle the row-wise copying of elements. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrix A. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + P rows and N columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically(const Matrix &A, const Matrix &B) @@ -94,6 +195,21 @@ template struct DenseAndDiag { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with a dense matrix and a + * diagonal matrix. + * + * This function takes a dense matrix A and a diagonal matrix B, and updates the + * output matrix Y by concatenating A on top of B. The operation is performed + * using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( typename ConcatenateVertically::DenseAndDiag::Y_Type &Y, @@ -108,6 +224,23 @@ inline void update_vertically_concatenated_matrix( Y.values); } +/** + * @brief Concatenates a dense matrix and a diagonal matrix vertically and + * returns the result as a new matrix. + * + * This function takes a dense matrix A and a diagonal matrix B, concatenates + * them vertically, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + N rows and N columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically(const Matrix &A, const DiagMatrix &B) -> @@ -146,6 +279,22 @@ struct DenseAndSparse { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with a dense matrix and a + * compiled sparse matrix. + * + * This function takes a dense matrix A and a compiled sparse matrix B, and + * updates the output matrix Y by concatenating A on top of B. The operation is + * performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( @@ -164,6 +313,24 @@ inline void update_vertically_concatenated_matrix( ((M * N) + RowPointers_B::list[P])>(B.values, Y.values); } +/** + * @brief Concatenates a dense matrix and a compiled sparse matrix vertically + * and returns the result as a new matrix. + * + * This function takes a dense matrix A and a compiled sparse matrix B, + * concatenates them vertically, and returns a new matrix Y containing the + * result. The operation is performed using compiled sparse operations to + * efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + P rows and N columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically( @@ -202,6 +369,21 @@ template struct DiagAndDense { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with a diagonal matrix and a + * dense matrix. + * + * This function takes a diagonal matrix A and a dense matrix B, and updates the + * output matrix Y by concatenating A on top of B. The operation is performed + * using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( typename ConcatenateVertically::DiagAndDense::Y_Type &Y, @@ -215,6 +397,23 @@ inline void update_vertically_concatenated_matrix( Y.values); } +/** + * @brief Concatenates a diagonal matrix and a dense matrix vertically and + * returns the result as a new matrix. + * + * This function takes a diagonal matrix A and a dense matrix B, concatenates + * them vertically, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + P rows and M columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically(const DiagMatrix &A, const Matrix &B) -> @@ -249,6 +448,19 @@ template struct DiagAndDiag { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with two diagonal matrices. + * + * This function takes two diagonal matrices A and B, and updates the output + * matrix Y by concatenating A on top of B. The operation is performed using + * compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( typename ConcatenateVertically::DiagAndDiag::Y_Type &Y, @@ -261,6 +473,21 @@ inline void update_vertically_concatenated_matrix( Base::Utility::copy(sparse_B.values, Y.values); } +/** + * @brief Concatenates two diagonal matrices vertically and returns the result + * as a new matrix. + * + * This function takes two diagonal matrices A and B, concatenates them + * vertically, and returns a new matrix Y containing the result. The operation + * is performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with 2 * M rows and M columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically(const DiagMatrix &A, const DiagMatrix &B) -> @@ -299,6 +526,21 @@ struct DiagAndSparse { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with a diagonal matrix and a + * compiled sparse matrix. + * + * This function takes a diagonal matrix A and a compiled sparse matrix B, and + * updates the output matrix Y by concatenating A on top of B. The operation is + * performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( @@ -315,6 +557,23 @@ inline void update_vertically_concatenated_matrix( (M + RowPointers_B::list[P])>(B.values, Y.values); } +/** + * @brief Concatenates a diagonal matrix and a compiled sparse matrix + * vertically and returns the result as a new matrix. + * + * This function takes a diagonal matrix A and a compiled sparse matrix B, + * concatenates them vertically, and returns a new matrix Y containing the + * result. The operation is performed using compiled sparse operations to + * efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + P rows and M columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically( @@ -357,6 +616,22 @@ struct SparseAndDense { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with a compiled sparse matrix + * and a dense matrix. + * + * This function takes a compiled sparse matrix A and a dense matrix B, and + * updates the output matrix Y by concatenating A on top of B. The operation is + * performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( @@ -374,6 +649,24 @@ inline void update_vertically_concatenated_matrix( Y.values); } +/** + * @brief Concatenates a compiled sparse matrix and a dense matrix vertically + * and returns the result as a new matrix. + * + * This function takes a compiled sparse matrix A and a dense matrix B, + * concatenates them vertically, and returns a new matrix Y containing the + * result. The operation is performed using compiled sparse operations to + * efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + P rows and N columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically( @@ -416,6 +709,21 @@ struct SparseAndDiag { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with a compiled sparse matrix + * and a diagonal matrix. + * + * This function takes a compiled sparse matrix A and a diagonal matrix B, and + * updates the output matrix Y by concatenating A on top of B. The operation is + * performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template inline void update_vertically_concatenated_matrix( @@ -432,6 +740,23 @@ inline void update_vertically_concatenated_matrix( (RowPointers_A::list[M] + N)>(sparse_B.values, Y.values); } +/** + * @brief Concatenates a compiled sparse matrix and a diagonal matrix vertically + * and returns the result as a new matrix. + * + * This function takes a compiled sparse matrix A and a diagonal matrix B, + * concatenates them vertically, and returns a new matrix Y containing the + * result. The operation is performed using compiled sparse operations to + * efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in both matrices A and B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + N rows and N columns, containing the + * concatenated result. + */ template inline auto concatenate_vertically( @@ -477,6 +802,22 @@ struct SparseAndSparse { } // namespace ConcatenateVertically +/** + * @brief Updates a vertically concatenated matrix with two compiled sparse + * matrices. + * + * This function takes two compiled sparse matrices A and B, and updates the + * output matrix Y by concatenating A on top of B. The operation is performed + * using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + */ template @@ -497,6 +838,23 @@ inline void update_vertically_concatenated_matrix( B.values, Y.values); } +/** + * @brief Concatenates two compiled sparse matrices vertically and returns the + * result as a new matrix. + * + * This function takes two compiled sparse matrices A and B, concatenates them + * vertically, and returns a new matrix Y containing the result. The operation + * is performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in both matrices A and B. + * @tparam P The number of columns in matrix B. + * @param A The first input matrix (top part of the result). + * @param B The second input matrix (bottom part of the result). + * @return A new matrix Y with M + P rows and N columns, containing the + * concatenated result. + */ template @@ -517,9 +875,27 @@ inline auto concatenate_vertically( } /* Functions: Concatenate horizontally */ + template struct CopyRowsFirstLoop { + /** + * @brief Concatenates matrix A into the first N columns of matrix Y. + * + * This static function copies the contents of matrix A into the corresponding + * rows and columns of matrix Y, starting from the first column. The resulting + * matrix Y has additional columns (N + P) to accommodate further + * concatenation. The function uses a utility copy method for the current row + * and recursively processes the remaining rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in matrix A. + * @tparam P The number of additional rows in matrix Y. + * @tparam Row The current row being processed (used for recursion). + * @param A The input matrix to be concatenated. + * @param Y The output matrix with concatenated columns. + */ static void compute(const Matrix &A, Matrix &Y) { Base::Utility::copy(A(Row), Y(Row)); CopyRowsFirstLoop::compute(A, Y); @@ -528,11 +904,39 @@ struct CopyRowsFirstLoop { template struct CopyRowsFirstLoop { + /** + * @brief Base case for the recursive copy of rows from matrix A to matrix Y. + * + * This static function copies the contents of the first row of matrix A into + * the corresponding row of matrix Y. It serves as the base case for the + * recursive function that processes all rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in matrix A. + * @tparam P The number of additional rows in matrix Y. + * @param A The input matrix to be concatenated. + * @param Y The output matrix with concatenated columns. + */ static void compute(const Matrix &A, Matrix &Y) { Base::Utility::copy(A(0), Y(0)); } }; +/** + * @brief Concatenates matrix A into the first N columns of matrix Y. + * + * This function copies the contents of matrix A into the corresponding rows and + * columns of matrix Y, starting from the first column. The resulting matrix Y + * has additional columns (N + P) to accommodate further concatenation. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in matrix A. + * @tparam P The number of additional rows in matrix Y. + * @param A The input matrix to be concatenated. + * @param Y The output matrix with concatenated columns. + */ template static inline void COMPILED_SPARSE_HORIZONTAL_CONCATENATE_1(const Matrix &A, @@ -543,6 +947,23 @@ COMPILED_SPARSE_HORIZONTAL_CONCATENATE_1(const Matrix &A, template struct CopyRowsSecondLoop { + /** + * @brief Concatenates matrix B into the last P columns of matrix Y. + * + * This static function copies the contents of matrix B into the corresponding + * rows and columns of matrix Y, starting from the column index N. The + * resulting matrix Y has additional columns (N + P) to accommodate the + * concatenation. The function uses a utility copy method for the current row + * and recursively processes the remaining rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in matrix A. + * @tparam P The number of additional rows in matrix Y. + * @tparam Row The current row being processed (used for recursion). + * @param B The input matrix to be concatenated. + * @param Y The output matrix with concatenated columns. + */ static void compute(const Matrix &B, Matrix &Y) { Base::Utility::copy(B(Row), Y(N + Row)); CopyRowsSecondLoop::compute(B, Y); @@ -551,11 +972,39 @@ struct CopyRowsSecondLoop { template struct CopyRowsSecondLoop { + /** + * @brief Base case for the recursive copy of rows from matrix B to matrix Y. + * + * This static function copies the contents of the first row of matrix B into + * the corresponding row of matrix Y, starting from column index N. It serves + * as the base case for the recursive function that processes all rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in matrix A. + * @tparam P The number of additional rows in matrix Y. + * @param B The input matrix to be concatenated. + * @param Y The output matrix with concatenated columns. + */ static void compute(const Matrix &B, Matrix &Y) { Base::Utility::copy(B(0), Y(N)); } }; +/** + * @brief Concatenates matrix B into the last P columns of matrix Y. + * + * This function copies the contents of matrix B into the corresponding rows and + * columns of matrix Y, starting from the column index N. The resulting matrix Y + * has additional columns (N + P) to accommodate the concatenation. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in matrix A. + * @tparam P The number of additional rows in matrix Y. + * @param B The input matrix to be concatenated. + * @param Y The output matrix with concatenated columns. + */ template static inline void COMPILED_SPARSE_HORIZONTAL_CONCATENATE_2(const Matrix &B, @@ -563,6 +1012,21 @@ COMPILED_SPARSE_HORIZONTAL_CONCATENATE_2(const Matrix &B, CopyRowsSecondLoop::compute(B, Y); } +/** + * @brief Updates a horizontally concatenated matrix with two matrices. + * + * This function takes two matrices A and B, and updates the output matrix Y by + * concatenating A on the left and B on the right. The operation is performed + * using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @tparam P The number of rows in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix(Matrix &Y, const Matrix &A, @@ -588,6 +1052,23 @@ inline void update_horizontally_concatenated_matrix(Matrix &Y, #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates two matrices horizontally and returns the result as a new + * matrix. + * + * This function takes two matrices A and B, concatenates them horizontally, and + * returns a new matrix Y containing the result. The operation is performed + * using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @tparam P The number of rows in matrix B. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and N + P columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally(const Matrix &A, const Matrix &B) @@ -606,6 +1087,29 @@ template struct ConcatMatrixSetFromDenseColumn { + /** + * @brief Copies a dense matrix column into a horizontally concatenated sparse + * matrix. + * + * This static function copies the value from the dense matrix A at position + * (I, J_idx) into the corresponding position in the sparse matrix Y, taking + * into account the specified offsets for columns and rows. It then + * recursively processes the previous column (J_idx - 1). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the dense matrix A. + * @tparam N The number of rows in the dense matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @tparam I The current row index being processed. + * @tparam J_idx The current column index being processed. + * @param Y The output sparse matrix to be updated with the value from A. + * @param A The input dense matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const Matrix &A) { @@ -626,6 +1130,26 @@ template { + /** + * @brief Copies the first column of a dense matrix into a horizontally + * concatenated sparse matrix. + * + * This static function copies the value from the dense matrix A at position + * (I, 0) into the corresponding position in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the dense matrix A. + * @tparam N The number of rows in the dense matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @param Y The output sparse matrix to be updated with the value from A. + * @param A The input dense matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const Matrix &A) { @@ -640,6 +1164,28 @@ template struct ConcatMatrixSetFromDenseRow { + /** + * @brief Copies a dense matrix row into a horizontally concatenated sparse + * matrix. + * + * This static function copies the values from the dense matrix A at row I_idx + * into the corresponding positions in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. It then recursively + * processes the next row (I_idx - 1). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the dense matrix A. + * @tparam N The number of rows in the dense matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @tparam I_idx The current row index being processed. + * @param Y The output sparse matrix to be updated with values from A. + * @param A The input dense matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const Matrix &A) { @@ -658,6 +1204,26 @@ template struct ConcatMatrixSetFromDenseRow { + /** + * @brief Copies the first row of a dense matrix into a horizontally + * concatenated sparse matrix. + * + * This static function copies the values from the dense matrix A at row 0 + * into the corresponding positions in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the dense matrix A. + * @tparam N The number of rows in the dense matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @param Y The output sparse matrix to be updated with values from A. + * @param A The input dense matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const Matrix &A) { @@ -673,6 +1239,27 @@ template struct ConcatMatrixSetFromDiagRow { + /** + * @brief Copies a diagonal matrix row into a horizontally concatenated sparse + * matrix. + * + * This static function copies the value from the diagonal matrix B at index + * I_idx into the corresponding position in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. It then recursively + * processes the previous row (I_idx - 1). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the diagonal matrix B. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @tparam I_idx The current row index being processed. + * @param Y The output sparse matrix to be updated with the value from B. + * @param B The input diagonal matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const DiagMatrix &B) { @@ -692,6 +1279,25 @@ template struct ConcatMatrixSetFromDiagRow { + /** + * @brief Copies the first row of a diagonal matrix into a horizontally + * concatenated sparse matrix. + * + * This static function copies the value from the diagonal matrix B at index 0 + * into the corresponding position in the sparse matrix Y, taking into account + * the specified offsets for columns and rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the diagonal matrix B. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @param Y The output sparse matrix to be updated with the value from B. + * @param B The input diagonal matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const DiagMatrix &B) { @@ -722,6 +1328,22 @@ template struct DenseAndDiag { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with a dense matrix and a + * diagonal matrix. + * + * This function takes a dense matrix A and a diagonal matrix B, and updates the + * output matrix Y by concatenating A on the left and B on the right. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( typename ConcatenateHorizontally::DenseAndDiag::Y_Type &Y, @@ -765,6 +1387,23 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates a dense matrix and a diagonal matrix horizontally and + * returns the result as a new matrix. + * + * This function takes a dense matrix A and a diagonal matrix B, concatenates + * them horizontally, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and N + M columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally(const Matrix &A, const DiagMatrix &B) -> @@ -785,6 +1424,31 @@ template struct ConcatMatrixSetFromSparseColumn { + /** + * @brief Copies a sparse matrix column into a horizontally concatenated + * sparse matrix. + * + * This static function copies the value from the sparse matrix A at position + * (I, J_idx) into the corresponding position in the sparse matrix Y, taking + * into account the specified offsets for columns and rows. It then + * recursively processes the previous column (J_idx - 1). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix A. + * @tparam N The number of rows in the sparse matrix A. + * @tparam RowIndices_A The row indices for the sparse matrix A. + * @tparam RowPointers_A The row pointers for the sparse matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @tparam I The current row index being processed. + * @tparam J_idx The current column index being processed. + * @param Y The output sparse matrix to be updated with the value from A. + * @param A The input sparse matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const CompiledSparseMatrix &A) { @@ -807,6 +1471,26 @@ template { + /** + * @brief Copies the first column of a sparse matrix into a horizontally + * concatenated sparse matrix. + * + * This static function copies the value from the sparse matrix A at position + * (I, 0) into the corresponding position in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix A. + * @tparam N The number of rows in the sparse matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @param Y The output sparse matrix to be updated with the value from A. + * @param A The input sparse matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const CompiledSparseMatrix &A) { @@ -822,6 +1506,30 @@ template struct ConcatMatrixSetFromSparseRow { + /** + * @brief Copies a sparse matrix row into a horizontally concatenated sparse + * matrix. + * + * This static function copies the values from the sparse matrix A at row + * I_idx into the corresponding positions in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. It then recursively + * processes the next row (I_idx - 1). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix A. + * @tparam N The number of rows in the sparse matrix A. + * @tparam RowIndices_A The row indices for the sparse matrix A. + * @tparam RowPointers_A The row pointers for the sparse matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @tparam I_idx The current row index being processed. + * @param Y The output sparse matrix to be updated with values from A. + * @param A The input sparse matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const CompiledSparseMatrix &A) { @@ -842,6 +1550,26 @@ template { + /** + * @brief Copies the first row of a sparse matrix into a horizontally + * concatenated sparse matrix. + * + * This static function copies the values from the sparse matrix A at row 0 + * into the corresponding positions in the sparse matrix Y, taking into + * account the specified offsets for columns and rows. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix A. + * @tparam N The number of rows in the sparse matrix A. + * @tparam Y_Col The number of columns in the sparse matrix Y. + * @tparam Y_Row The number of rows in the sparse matrix Y. + * @tparam Column_Offset The offset for columns in the sparse matrix Y. + * @tparam Row_Offset The offset for rows in the sparse matrix Y. + * @tparam RowIndices_Y The row indices for the sparse matrix Y. + * @tparam RowPointers_Y The row pointers for the sparse matrix Y. + * @param Y The output sparse matrix to be updated with values from A. + * @param A The input sparse matrix from which values are copied. + */ static void compute(CompiledSparseMatrix &Y, const CompiledSparseMatrix &A) { @@ -877,6 +1605,23 @@ struct DenseAndSparse { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with a dense matrix and a + * sparse matrix. + * + * This function takes a dense matrix A and a sparse matrix B, and updates the + * output matrix Y by concatenating A on the left and B on the right. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( @@ -931,6 +1676,24 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates a dense matrix and a sparse matrix horizontally and + * returns the result as a new matrix. + * + * This function takes a dense matrix A and a sparse matrix B, concatenates + * them horizontally, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and N + L columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally( @@ -969,6 +1732,22 @@ template struct DiagAndDense { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with a diagonal matrix and + * a dense matrix. + * + * This function takes a diagonal matrix A and a dense matrix B, and updates the + * output matrix Y by concatenating A on the left and B on the right. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( typename ConcatenateHorizontally::DiagAndDense::Y_Type &Y, @@ -1011,6 +1790,23 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates a diagonal matrix and a dense matrix horizontally and + * returns the result as a new matrix. + * + * This function takes a diagonal matrix A and a dense matrix B, concatenates + * them horizontally, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix B. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and N + M columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally(const DiagMatrix &A, const Matrix &B) -> @@ -1045,6 +1841,19 @@ template struct DiagAndDiag { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with two diagonal matrices. + * + * This function takes two diagonal matrices A and B, and updates the output + * matrix Y by concatenating A on the left and B on the right. The operation is + * performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( typename ConcatenateHorizontally::DiagAndDiag::Y_Type &Y, @@ -1088,6 +1897,21 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates two diagonal matrices horizontally and returns the result + * as a new matrix. + * + * This function takes two diagonal matrices A and B, concatenates them + * horizontally, and returns a new matrix Y containing the result. The operation + * is performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and 2 * M columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally(const DiagMatrix &A, const DiagMatrix &B) -> @@ -1126,6 +1950,22 @@ struct DiagAndSparse { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with a diagonal matrix and + * a sparse matrix. + * + * This function takes a diagonal matrix A and a sparse matrix B, and updates + * the output matrix Y by concatenating A on the left and B on the right. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( @@ -1181,6 +2021,23 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates a diagonal matrix and a sparse matrix horizontally and + * returns the result as a new matrix. + * + * This function takes a diagonal matrix A and a sparse matrix B, concatenates + * them horizontally, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix B. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and M + N columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally( @@ -1223,6 +2080,23 @@ struct SparseAndDense { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with a sparse matrix and a + * dense matrix. + * + * This function takes a sparse matrix A and a dense matrix B, and updates the + * output matrix Y by concatenating A on the left and B on the right. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix B. + * @tparam L The number of rows in matrix A. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( @@ -1279,6 +2153,24 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates a sparse matrix and a dense matrix horizontally and + * returns the result as a new matrix. + * + * This function takes a sparse matrix A and a dense matrix B, concatenates + * them horizontally, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix B. + * @tparam L The number of rows in matrix A. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and N + L columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally( @@ -1321,6 +2213,22 @@ struct SparseAndDiag { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with a sparse matrix and a + * diagonal matrix. + * + * This function takes a sparse matrix A and a diagonal matrix B, and updates + * the output matrix Y by concatenating A on the left and B on the right. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template inline void update_horizontally_concatenated_matrix( @@ -1380,6 +2288,23 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates a sparse matrix and a diagonal matrix horizontally and + * returns the result as a new matrix. + * + * This function takes a sparse matrix A and a diagonal matrix B, concatenates + * them horizontally, and returns a new matrix Y containing the result. The + * operation is performed using compiled sparse operations to efficiently copy + * the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and M + N columns, containing the + * concatenated result. + */ template inline auto concatenate_horizontally( @@ -1425,6 +2350,21 @@ struct SparseAndSparse { } // namespace ConcatenateHorizontally +/** + * @brief Updates a horizontally concatenated matrix with two sparse matrices. + * + * This function takes two sparse matrices A and B, and updates the output + * matrix Y by concatenating A on the left and B on the right. The operation is + * performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param Y The output matrix to be updated with the concatenated result. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + */ template @@ -1495,6 +2435,23 @@ inline void update_horizontally_concatenated_matrix( #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Concatenates two sparse matrices horizontally and returns the result + * as a new matrix. + * + * This function takes two sparse matrices A and B, concatenates them + * horizontally, and returns a new matrix Y containing the result. The operation + * is performed using compiled sparse operations to efficiently copy the values. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in both matrices A and B. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param A The first input matrix (left part of the result). + * @param B The second input matrix (right part of the result). + * @return A new matrix Y with M rows and N + L columns, containing the + * concatenated result. + */ template From 67ca1ec4d581dc2d6cda2633b3bbe7c936c04008 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 20:48:22 +0900 Subject: [PATCH 08/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_diagonal.hpp | 1025 ++++++++++++++++++++++++++ 1 file changed, 1025 insertions(+) diff --git a/base_matrix/base_matrix_diagonal.hpp b/base_matrix/base_matrix_diagonal.hpp index 3580127..28ddd94 100644 --- a/base_matrix/base_matrix_diagonal.hpp +++ b/base_matrix/base_matrix_diagonal.hpp @@ -1,3 +1,22 @@ +/** + * @file base_matrix_diagonal.hpp + * @brief Diagonal Matrix Operations for the Base::Matrix Namespace + * + * This header defines the DiagMatrix class template and a comprehensive set of + * operations for diagonal matrices within the Base::Matrix namespace. The code + * provides efficient, type-safe, and optionally loop-unrolled implementations + * for diagonal matrix arithmetic, including addition, subtraction, + * multiplication, division, inversion, and conversion between real and complex + * representations. It also supports interactions with dense matrices and + * vectors, as well as utility functions such as trace computation and dense + * matrix extraction. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_DIAGONAL_HPP__ #define __BASE_MATRIX_DIAGONAL_HPP__ @@ -15,6 +34,19 @@ namespace Base { namespace Matrix { +/** + * @brief DiagMatrix is a fixed-size diagonal matrix class template. + * + * This class represents a diagonal matrix of size MxM, storing only the + * diagonal elements. The storage can be either std::vector or std::array depending on the + * __BASE_MATRIX_USE_STD_VECTOR__ macro. + * + * @tparam T Type of the matrix elements. + * @tparam M Size of the matrix (number of rows and columns). + * + * @note Only the diagonal elements are stored and manipulated. + */ template class DiagMatrix { public: #ifdef __BASE_MATRIX_USE_STD_VECTOR__ @@ -80,26 +112,105 @@ template class DiagMatrix { } /* Function */ + + /** + * @brief Creates and returns an identity diagonal matrix. + * + * This static method constructs a diagonal matrix of size M x M, + * where all diagonal elements are set to 1 (of type T), and all + * off-diagonal elements are zero. The resulting matrix is an identity + * matrix in the context of diagonal matrices. + * + * @tparam T The type of the matrix elements. + * @tparam M The size (number of rows and columns) of the square matrix. + * @return DiagMatrix An identity diagonal matrix of type T and size M. + */ static inline DiagMatrix identity() { DiagMatrix identity(std::vector(M, static_cast(1))); return identity; } + /** + * @brief Creates and returns a diagonal matrix filled with a specified value. + * + * This static method constructs a diagonal matrix of size M x M, + * where all diagonal elements are set to the specified value, and all + * off-diagonal elements are zero. + * + * @tparam T The type of the matrix elements. + * @tparam M The size (number of rows and columns) of the square matrix. + * @param value The value to fill the diagonal elements. + * @return DiagMatrix A diagonal matrix of type T and size M filled with + * the specified value. + */ static inline DiagMatrix full(const T &value) { DiagMatrix full(std::vector(M, value)); return full; } + /** + * @brief Creates and returns a diagonal matrix filled with zeros. + * + * This static method constructs a diagonal matrix of size M x M, + * where all diagonal elements are set to zero, and all off-diagonal + * elements are also zero. + * + * @tparam T The type of the matrix elements. + * @tparam M The size (number of rows and columns) of the square matrix. + * @return DiagMatrix A diagonal matrix of type T and size M filled with + * zeros. + */ T &operator[](std::size_t index) { return this->data[index]; } + /** + * @brief Accesses the diagonal element at the specified index. + * + * This method provides read-only access to the diagonal element at the + * specified index. If the index is out of bounds, it returns the last + * element in the diagonal. + * + * @param index The index of the diagonal element to access. + * @return const T& A constant reference to the diagonal element at the + * specified index. + */ const T &operator[](std::size_t index) const { return this->data[index]; } + /** + * @brief Accesses the diagonal element at the specified index. + * + * This method provides read-write access to the diagonal element at the + * specified index. If the index is out of bounds, it returns the last + * element in the diagonal. + * + * @param index The index of the diagonal element to access. + * @return T& A reference to the diagonal element at the specified index. + */ constexpr std::size_t rows() const { return M; } + /** + * @brief Returns the number of columns in the diagonal matrix. + * + * Since this is a square matrix, the number of columns is equal to the + * number of rows. + * + * @return std::size_t The number of columns in the diagonal matrix. + */ constexpr std::size_t cols() const { return M; } + /** + * @brief Returns a vector representing the specified row of the diagonal + * matrix. + * + * For a diagonal matrix, only the diagonal element at the given row index is + * non-zero. If the provided row index is out of bounds (greater than or equal + * to M), it is clamped to the last valid index. + * + * @param row The index of the row to retrieve. + * @return Vector A vector with only the diagonal element at the + * specified row set, all other elements are zero. + */ inline Vector get_row(std::size_t row) const { if (row >= M) { row = M - 1; @@ -111,6 +222,18 @@ template class DiagMatrix { return result; } + /** + * @brief Returns a vector representing the specified column of the diagonal + * matrix. + * + * For a diagonal matrix, only the diagonal element at the given column index + * is non-zero. If the provided column index is out of bounds (greater than or + * equal to M), it is clamped to the last valid index. + * + * @param col The index of the column to retrieve. + * @return Vector A vector with only the diagonal element at the + * specified column set, all other elements are zero. + */ inline DiagMatrix inv(T division_min) const { DiagMatrix result; @@ -135,6 +258,16 @@ namespace DiagMatrixAddDiagMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise addition of two diagonal matrices. + * + * This function recursively computes the sum of two diagonal matrices A and + * B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after addition. + */ static void compute(const DiagMatrix &A, const DiagMatrix B, DiagMatrix &result) { result[M_idx] = A[M_idx] + B[M_idx]; @@ -144,12 +277,33 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise addition of two diagonal matrices. + * + * This function serves as the base case for the recursive addition of two + * diagonal matrices A and B, storing the result in the provided result + * matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after addition. + */ static void compute(const DiagMatrix &A, const DiagMatrix B, DiagMatrix &result) { result[0] = A[0] + B[0]; } }; +/** + * @brief Computes the element-wise addition of two diagonal matrices. + * + * This function initiates the recursive computation of the sum of two diagonal + * matrices A and B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after addition. + */ template inline void compute(const DiagMatrix &A, const DiagMatrix &B, DiagMatrix &result) { @@ -158,6 +312,17 @@ inline void compute(const DiagMatrix &A, const DiagMatrix &B, } // namespace DiagMatrixAddDiagMatrix +/** + * @brief Adds two diagonal matrices element-wise. + * + * This function computes the sum of two diagonal matrices A and B, returning a + * new diagonal matrix that contains the element-wise sums of the corresponding + * diagonal elements. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @return DiagMatrix The resulting diagonal matrix after addition. + */ template inline DiagMatrix operator+(const DiagMatrix &A, const DiagMatrix &B) { @@ -182,6 +347,17 @@ namespace DiagMatrixAddMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise addition of a diagonal matrix and a dense + * matrix. + * + * This function recursively computes the sum of a diagonal matrix A and a + * dense matrix B, storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @param result The resulting dense matrix after addition. + */ static void compute(const DiagMatrix &A, Matrix &result) { result.template set(result.template get() + @@ -192,12 +368,34 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise addition of a diagonal matrix and a dense + * matrix. + * + * This function serves as the base case for the recursive addition of a + * diagonal matrix A and a dense matrix B, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after addition. + */ static void compute(const DiagMatrix &A, Matrix &result) { result.template set<0, 0>(result.template get<0, 0>() + A[0]); } }; +/** + * @brief Computes the element-wise addition of a diagonal matrix and a dense + * matrix. + * + * This function initiates the recursive computation of the sum of a diagonal + * matrix A and a dense matrix B, storing the result in the provided result + * matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after addition. + */ template inline void compute(const DiagMatrix &A, Matrix &result) { Core::compute(A, result); @@ -205,6 +403,17 @@ inline void compute(const DiagMatrix &A, Matrix &result) { } // namespace DiagMatrixAddMatrix +/** + * @brief Adds a diagonal matrix to a dense matrix element-wise. + * + * This function computes the sum of a diagonal matrix A and a dense matrix B, + * returning a new dense matrix that contains the element-wise sums of the + * corresponding diagonal elements. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @return Matrix The resulting dense matrix after addition. + */ template inline Matrix operator+(const DiagMatrix &A, const Matrix &B) { @@ -225,6 +434,17 @@ inline Matrix operator+(const DiagMatrix &A, return result; } +/** + * @brief Adds a dense matrix to a diagonal matrix element-wise. + * + * This function computes the sum of a dense matrix A and a diagonal matrix B, + * returning a new dense matrix that contains the element-wise sums of the + * corresponding diagonal elements. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @return Matrix The resulting dense matrix after addition. + */ template inline Matrix operator+(const Matrix &A, const DiagMatrix &B) { @@ -250,6 +470,15 @@ namespace DiagMatrixMinus { // when I_idx < M template struct Loop { + /** + * @brief Computes the element-wise negation of a diagonal matrix. + * + * This function recursively computes the negation of a diagonal matrix A, + * storing the result in the provided result matrix. + * + * @param A The diagonal matrix to negate. + * @param result The resulting diagonal matrix after negation. + */ static void compute(const DiagMatrix &A, DiagMatrix &result) { result[I_idx] = -A[I_idx]; Loop::compute(A, result); @@ -258,11 +487,29 @@ template struct Loop { // row recursion termination template struct Loop { + /** + * @brief Computes the element-wise negation of a diagonal matrix. + * + * This function serves as the base case for the recursive negation of a + * diagonal matrix A, storing the result in the provided result matrix. + * + * @param A The diagonal matrix to negate. + * @param result The resulting diagonal matrix after negation. + */ static void compute(const DiagMatrix &A, DiagMatrix &result) { result[0] = -A[0]; } }; +/** + * @brief Computes the element-wise negation of a diagonal matrix. + * + * This function initiates the recursive computation of the negation of a + * diagonal matrix A, storing the result in the provided result matrix. + * + * @param A The diagonal matrix to negate. + * @param result The resulting diagonal matrix after negation. + */ template inline void compute(const DiagMatrix &A, DiagMatrix &result) { Loop::compute(A, result); @@ -270,6 +517,16 @@ inline void compute(const DiagMatrix &A, DiagMatrix &result) { } // namespace DiagMatrixMinus +/** + * @brief Negates a diagonal matrix element-wise. + * + * This function computes the negation of a diagonal matrix A, returning a new + * diagonal matrix that contains the negated values of the corresponding + * diagonal elements. + * + * @param A The diagonal matrix to negate. + * @return DiagMatrix The resulting diagonal matrix after negation. + */ template inline DiagMatrix operator-(const DiagMatrix &A) { DiagMatrix result; @@ -294,6 +551,16 @@ namespace DiagMatrixSubDiagMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise subtraction of two diagonal matrices. + * + * This function recursively computes the difference between two diagonal + * matrices A and B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after subtraction. + */ static void compute(const DiagMatrix &A, const DiagMatrix B, DiagMatrix &result) { result[M_idx] = A[M_idx] - B[M_idx]; @@ -303,12 +570,34 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise subtraction of two diagonal matrices. + * + * This function serves as the base case for the recursive subtraction of two + * diagonal matrices A and B, storing the result in the provided result + * matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after subtraction. + */ static void compute(const DiagMatrix &A, const DiagMatrix B, DiagMatrix &result) { result[0] = A[0] - B[0]; } }; +/** + * @brief Computes the element-wise subtraction of two diagonal matrices. + * + * This function initiates the recursive computation of the difference between + * two diagonal matrices A and B, storing the result in the provided result + * matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after subtraction. + */ template inline void compute(const DiagMatrix &A, const DiagMatrix &B, DiagMatrix &result) { @@ -317,6 +606,17 @@ inline void compute(const DiagMatrix &A, const DiagMatrix &B, } // namespace DiagMatrixSubDiagMatrix +/** + * @brief Subtracts two diagonal matrices element-wise. + * + * This function computes the difference between two diagonal matrices A and B, + * returning a new diagonal matrix that contains the element-wise differences + * of the corresponding diagonal elements. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @return DiagMatrix The resulting diagonal matrix after subtraction. + */ template inline DiagMatrix operator-(const DiagMatrix &A, const DiagMatrix &B) { @@ -341,6 +641,17 @@ namespace DiagMatrixSubMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise subtraction of a diagonal matrix from a + * dense matrix. + * + * This function recursively computes the difference between a dense matrix A + * and a diagonal matrix B, storing the result in the provided result matrix. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @param result The resulting dense matrix after subtraction. + */ static void compute(const DiagMatrix &A, Matrix &result) { result.template set(result.template get() + @@ -351,12 +662,34 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise subtraction of a diagonal matrix from a + * dense matrix. + * + * This function serves as the base case for the recursive subtraction of a + * diagonal matrix A from a dense matrix, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after subtraction. + */ static void compute(const DiagMatrix &A, Matrix &result) { result.template set<0, 0>(result.template get<0, 0>() + A[0]); } }; +/** + * @brief Computes the element-wise subtraction of a diagonal matrix from a + * dense matrix. + * + * This function initiates the recursive computation of the difference between + * a dense matrix A and a diagonal matrix B, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after subtraction. + */ template inline void compute(const Matrix &A, Matrix &result) { Core::compute(A, result); @@ -364,6 +697,17 @@ inline void compute(const Matrix &A, Matrix &result) { } // namespace DiagMatrixSubMatrix +/** + * @brief Subtracts a diagonal matrix from a dense matrix element-wise. + * + * This function computes the difference between a diagonal matrix A and a + * dense matrix B, returning a new dense matrix that contains the element-wise + * differences of the corresponding diagonal elements. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @return Matrix The resulting dense matrix after subtraction. + */ template inline Matrix operator-(const DiagMatrix &A, const Matrix &B) { @@ -388,6 +732,16 @@ namespace MatrixSubDiagMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise subtraction of a diagonal matrix from a + * dense matrix. + * + * This function recursively computes the difference between a dense matrix A + * and a diagonal matrix B, storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after subtraction. + */ static void compute(const DiagMatrix &A, Matrix &result) { result.template set(result.template get() - @@ -398,11 +752,33 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise subtraction of a diagonal matrix from a + * dense matrix. + * + * This function serves as the base case for the recursive subtraction of a + * diagonal matrix A from a dense matrix, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after subtraction. + */ static void compute(const DiagMatrix &A, Matrix &result) { result.template set<0, 0>(result.template get<0, 0>() - A[0]); } }; +/** + * @brief Computes the element-wise subtraction of a diagonal matrix from a + * dense matrix. + * + * This function initiates the recursive computation of the difference between + * a dense matrix A and a diagonal matrix B, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param result The resulting dense matrix after subtraction. + */ template inline void compute(const DiagMatrix &A, Matrix &result) { Core::compute(A, result); @@ -410,6 +786,17 @@ inline void compute(const DiagMatrix &A, Matrix &result) { } // namespace MatrixSubDiagMatrix +/** + * @brief Subtracts a dense matrix from a diagonal matrix element-wise. + * + * This function computes the difference between a diagonal matrix A and a + * dense matrix B, returning a new dense matrix that contains the element-wise + * differences of the corresponding diagonal elements. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @return Matrix The resulting dense matrix after subtraction. + */ template inline Matrix operator-(const Matrix &A, const DiagMatrix &B) { @@ -435,6 +822,17 @@ namespace DiagMatrixMultiplyScalar { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise multiplication of a diagonal matrix by a + * scalar. + * + * This function recursively computes the product of a diagonal matrix and a + * scalar, storing the result in the provided result matrix. + * + * @param mat The diagonal matrix to multiply. + * @param scalar The scalar value to multiply with. + * @param result The resulting diagonal matrix after multiplication. + */ static void compute(const DiagMatrix &mat, const T scalar, DiagMatrix &result) { result[M_idx] = mat[M_idx] * scalar; @@ -444,12 +842,36 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise multiplication of a diagonal matrix by a + * scalar. + * + * This function serves as the base case for the recursive multiplication of a + * diagonal matrix and a scalar, storing the result in the provided result + * matrix. + * + * @param mat The diagonal matrix to multiply. + * @param scalar The scalar value to multiply with. + * @param result The resulting diagonal matrix after multiplication. + */ static void compute(const DiagMatrix &mat, const T scalar, DiagMatrix &result) { result[0] = mat[0] * scalar; } }; +/** + * @brief Computes the element-wise multiplication of a diagonal matrix by a + * scalar. + * + * This function initiates the recursive computation of the product of a + * diagonal matrix and a scalar, storing the result in the provided result + * matrix. + * + * @param mat The diagonal matrix to multiply. + * @param scalar The scalar value to multiply with. + * @param result The resulting diagonal matrix after multiplication. + */ template inline void compute(const DiagMatrix &mat, const T &scalar, DiagMatrix &result) { @@ -458,6 +880,17 @@ inline void compute(const DiagMatrix &mat, const T &scalar, } // namespace DiagMatrixMultiplyScalar +/** + * @brief Multiplies a diagonal matrix by a scalar. + * + * This function computes the product of a diagonal matrix A and a scalar value, + * returning a new diagonal matrix that contains the products of the + * corresponding diagonal elements with the scalar. + * + * @param A The diagonal matrix to multiply. + * @param scalar The scalar value to multiply with. + * @return DiagMatrix The resulting diagonal matrix after multiplication. + */ template inline DiagMatrix operator*(const DiagMatrix &A, const T &scalar) { DiagMatrix result; @@ -477,6 +910,17 @@ inline DiagMatrix operator*(const DiagMatrix &A, const T &scalar) { return result; } +/** + * @brief Multiplies a scalar by a diagonal matrix. + * + * This function computes the product of a scalar value and a diagonal matrix A, + * returning a new diagonal matrix that contains the products of the + * corresponding diagonal elements with the scalar. + * + * @param scalar The scalar value to multiply with. + * @param A The diagonal matrix to multiply. + * @return DiagMatrix The resulting diagonal matrix after multiplication. + */ template inline DiagMatrix operator*(const T &scalar, const DiagMatrix &A) { DiagMatrix result; @@ -501,6 +945,16 @@ namespace DiagMatrixMultiplyVector { // M_idx < M template struct Core { + /** + * @brief Computes the multiplication of a diagonal matrix with a vector. + * + * This function recursively computes the product of a diagonal matrix A and a + * vector vec, storing the result in the provided result vector. + * + * @param A The diagonal matrix. + * @param vec The vector to multiply with. + * @param result The resulting vector after multiplication. + */ static void compute(const DiagMatrix &A, Vector vec, Vector &result) { result[M_idx] = A[M_idx] * vec[M_idx]; @@ -510,12 +964,34 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the multiplication of a diagonal matrix with a vector. + * + * This function serves as the base case for the recursive multiplication of a + * diagonal matrix A and a vector vec, storing the result in the provided + * result vector. + * + * @param A The diagonal matrix. + * @param vec The vector to multiply with. + * @param result The resulting vector after multiplication. + */ static void compute(const DiagMatrix &A, Vector vec, Vector &result) { result[0] = A[0] * vec[0]; } }; +/** + * @brief Computes the multiplication of a diagonal matrix with a vector. + * + * This function initiates the recursive computation of the product of a + * diagonal matrix A and a vector vec, storing the result in the provided + * result vector. + * + * @param A The diagonal matrix. + * @param vec The vector to multiply with. + * @param result The resulting vector after multiplication. + */ template inline void compute(const DiagMatrix &A, const Vector &vec, Vector &result) { @@ -524,6 +1000,17 @@ inline void compute(const DiagMatrix &A, const Vector &vec, } // namespace DiagMatrixMultiplyVector +/** + * @brief Multiplies a diagonal matrix by a vector. + * + * This function computes the product of a diagonal matrix A and a vector vec, + * returning a new vector that contains the products of the corresponding + * diagonal elements with the vector elements. + * + * @param A The diagonal matrix to multiply. + * @param vec The vector to multiply with. + * @return Vector The resulting vector after multiplication. + */ template inline Vector operator*(const DiagMatrix &A, const Vector &vec) { @@ -549,6 +1036,16 @@ namespace DiagMatrixMultiplyDiagMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise multiplication of two diagonal matrices. + * + * This function recursively computes the product of two diagonal matrices A + * and B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after multiplication. + */ static void compute(const DiagMatrix &A, DiagMatrix B, DiagMatrix &result) { result[M_idx] = A[M_idx] * B[M_idx]; @@ -558,12 +1055,33 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise multiplication of two diagonal matrices. + * + * This function serves as the base case for the recursive multiplication of + * two diagonal matrices A and B, storing the result in the provided result + * matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after multiplication. + */ static void compute(const DiagMatrix &A, DiagMatrix B, DiagMatrix &result) { result[0] = A[0] * B[0]; } }; +/** + * @brief Computes the element-wise multiplication of two diagonal matrices. + * + * This function initiates the recursive computation of the product of two + * diagonal matrices A and B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after multiplication. + */ template inline void compute(const DiagMatrix &A, const DiagMatrix &B, DiagMatrix &result) { @@ -572,6 +1090,17 @@ inline void compute(const DiagMatrix &A, const DiagMatrix &B, } // namespace DiagMatrixMultiplyDiagMatrix +/** + * @brief Multiplies two diagonal matrices element-wise. + * + * This function computes the product of two diagonal matrices A and B, + * returning a new diagonal matrix that contains the products of the + * corresponding diagonal elements. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @return DiagMatrix The resulting diagonal matrix after multiplication. + */ template inline DiagMatrix operator*(const DiagMatrix &A, const DiagMatrix &B) { @@ -599,6 +1128,17 @@ namespace DiagMatrixMultiplyMatrix { template struct Core { + /** + * @brief Computes the multiplication of a diagonal matrix with a dense + * matrix. + * + * This function recursively computes the product of a diagonal matrix A and a + * dense matrix B, storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result) { @@ -610,6 +1150,18 @@ struct Core { // Specialization for K == 0 template struct Core { + /** + * @brief Computes the multiplication of a diagonal matrix with a dense + * matrix. + * + * This function serves as the base case for the recursive multiplication of a + * diagonal matrix A and a dense matrix B, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result) { @@ -620,6 +1172,17 @@ struct Core { // Column-wise multiplication template struct Column { + /** + * @brief Computes the multiplication of a diagonal matrix with a dense + * matrix, column by column. + * + * This function recursively computes the product of a diagonal matrix A and a + * dense matrix B, storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result) { Core::compute(A, B, result); @@ -629,12 +1192,35 @@ struct Column { // Specialization for J == 0 template struct Column { + /** + * @brief Computes the multiplication of a diagonal matrix with a dense + * matrix, starting from the first column. + * + * This function serves as the base case for the recursive multiplication of a + * diagonal matrix A and a dense matrix B, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result) { Core::compute(A, B, result); } }; +/** + * @brief Computes the multiplication of a diagonal matrix with a dense matrix. + * + * This function initiates the recursive computation of the product of a + * diagonal matrix A and a dense matrix B, storing the result in the provided + * result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix. + * @param result The resulting dense matrix after multiplication. + */ template inline void compute(const DiagMatrix &A, const Matrix &B, Matrix &result) { @@ -643,6 +1229,17 @@ inline void compute(const DiagMatrix &A, const Matrix &B, } // namespace DiagMatrixMultiplyMatrix +/** + * @brief Multiplies a diagonal matrix by a dense matrix. + * + * This function computes the product of a diagonal matrix A and a dense matrix + * B, returning a new dense matrix that contains the products of the + * corresponding diagonal elements with the dense matrix elements. + * + * @param A The diagonal matrix to multiply. + * @param B The dense matrix to multiply with. + * @return Matrix The resulting dense matrix after multiplication. + */ template inline Matrix operator*(const DiagMatrix &A, const Matrix &B) { @@ -671,6 +1268,17 @@ namespace MatrixMultiplyDiagMatrix { template struct Core { + /** + * @brief Computes the multiplication of a dense matrix with a diagonal + * matrix. + * + * This function recursively computes the product of a dense matrix A and a + * diagonal matrix B, storing the result in the provided result matrix. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const Matrix &A, const DiagMatrix &B, Matrix &result) { @@ -682,6 +1290,18 @@ struct Core { // Specialization for J = 0 template struct Core { + /** + * @brief Computes the multiplication of a dense matrix with a diagonal + * matrix. + * + * This function serves as the base case for the recursive multiplication of a + * dense matrix A and a diagonal matrix B, storing the result in the provided + * result matrix. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const Matrix &A, const DiagMatrix &B, Matrix &result) { @@ -692,6 +1312,17 @@ struct Core { // Column-wise multiplication template struct Column { + /** + * @brief Computes the multiplication of a dense matrix with a diagonal + * matrix, column by column. + * + * This function recursively computes the product of a dense matrix A and a + * diagonal matrix B, storing the result in the provided result matrix. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const Matrix &A, const DiagMatrix &B, Matrix &result) { Core::compute(A, B, result); @@ -701,12 +1332,35 @@ struct Column { // Specialization for I = 0 template struct Column { + /** + * @brief Computes the multiplication of a dense matrix with a diagonal + * matrix, starting from the first column. + * + * This function serves as the base case for the recursive multiplication of a + * dense matrix A and a diagonal matrix B, storing the result in the provided + * result matrix. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @param result The resulting dense matrix after multiplication. + */ static void compute(const Matrix &A, const DiagMatrix &B, Matrix &result) { Core::compute(A, B, result); } }; +/** + * @brief Computes the multiplication of a dense matrix with a diagonal matrix. + * + * This function initiates the recursive computation of the product of a dense + * matrix A and a diagonal matrix B, storing the result in the provided result + * matrix. + * + * @param A The dense matrix. + * @param B The diagonal matrix. + * @param result The resulting dense matrix after multiplication. + */ template inline void compute(const Matrix &A, const DiagMatrix &B, Matrix &result) { @@ -715,6 +1369,17 @@ inline void compute(const Matrix &A, const DiagMatrix &B, } // namespace MatrixMultiplyDiagMatrix +/** + * @brief Multiplies a dense matrix by a diagonal matrix. + * + * This function computes the product of a dense matrix A and a diagonal matrix + * B, returning a new dense matrix that contains the products of the + * corresponding diagonal elements with the dense matrix elements. + * + * @param A The dense matrix to multiply. + * @param B The diagonal matrix to multiply with. + * @return Matrix The resulting dense matrix after multiplication. + */ template inline Matrix operator*(const Matrix &A, const DiagMatrix &B) { @@ -742,6 +1407,15 @@ namespace DiagMatrixTrace { // Base case: when index reaches 0 template struct Core { + /** + * @brief Computes the trace of a diagonal matrix. + * + * This function recursively computes the trace of a diagonal matrix A by + * summing its diagonal elements. + * + * @param A The diagonal matrix. + * @return T The computed trace of the diagonal matrix. + */ static T compute(const DiagMatrix &A) { return A[Index] + Core::compute(A); } @@ -749,9 +1423,27 @@ template struct Core { // Specialization for the base case when Index is 0 template struct Core { + /** + * @brief Computes the trace of a diagonal matrix when the index is 0. + * + * This function serves as the base case for the recursive computation of the + * trace of a diagonal matrix A, returning its first diagonal element. + * + * @param A The diagonal matrix. + * @return T The first diagonal element of the diagonal matrix. + */ static T compute(const DiagMatrix &A) { return A[0]; } }; +/** + * @brief Computes the trace of a diagonal matrix. + * + * This function initiates the recursive computation of the trace of a diagonal + * matrix A by summing its diagonal elements. + * + * @param A The diagonal matrix. + * @return T The computed trace of the diagonal matrix. + */ template inline T compute(const DiagMatrix &A) { return Core::compute(A); @@ -759,6 +1451,15 @@ inline T compute(const DiagMatrix &A) { } // namespace DiagMatrixTrace +/** + * @brief Computes the trace of a diagonal matrix. + * + * This function calculates the trace of a diagonal matrix A by summing its + * diagonal elements, returning the computed trace value. + * + * @param A The diagonal matrix. + * @return T The computed trace of the diagonal matrix. + */ template inline T output_trace(const DiagMatrix &A) { T trace = static_cast(0); @@ -783,6 +1484,16 @@ namespace DiagMatrixToDense { // Diagonal element assignment core template struct Core { + /** + * @brief Assigns the diagonal elements of a diagonal matrix to a dense + * matrix. + * + * This function recursively assigns the diagonal elements of a diagonal + * matrix A to the corresponding positions in a dense matrix result. + * + * @param result The resulting dense matrix. + * @param A The diagonal matrix from which to assign elements. + */ static void assign(Matrix &result, const DiagMatrix &A) { result.template set(A[I]); @@ -792,11 +1503,31 @@ template struct Core { // Base case for recursion termination template struct Core { + /** + * @brief Assigns the first diagonal element of a diagonal matrix to a dense + * matrix. + * + * This function serves as the base case for the recursive assignment of + * diagonal elements from a diagonal matrix A to a dense matrix result. + * + * @param result The resulting dense matrix. + * @param A The diagonal matrix from which to assign the first element. + */ static void assign(Matrix &result, const DiagMatrix &A) { result.template set<0, 0>(A[0]); } }; +/** + * @brief Computes the dense matrix representation of a diagonal matrix. + * + * This function initiates the recursive assignment of diagonal elements from a + * diagonal matrix A to a dense matrix result, effectively creating a dense + * representation of the diagonal matrix. + * + * @param A The diagonal matrix to convert to dense format. + * @param result The resulting dense matrix after conversion. + */ template inline void compute(const DiagMatrix &A, Matrix &result) { Core::assign(result, A); @@ -804,6 +1535,16 @@ inline void compute(const DiagMatrix &A, Matrix &result) { } // namespace DiagMatrixToDense +/** + * @brief Converts a diagonal matrix to a dense matrix. + * + * This function creates a dense matrix representation of a diagonal matrix A, + * where the diagonal elements of A are assigned to the corresponding diagonal + * positions in the resulting dense matrix. + * + * @param A The diagonal matrix to convert. + * @return Matrix The resulting dense matrix after conversion. + */ template inline Matrix output_dense_matrix(const DiagMatrix &A) { Matrix result; @@ -828,6 +1569,17 @@ namespace DiagMatrixDivideDiagMatrix { // M_idx < M template struct Core { + /** + * @brief Computes the element-wise division of two diagonal matrices. + * + * This function recursively computes the division of two diagonal matrices A + * and B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ static void compute(const DiagMatrix &A, const DiagMatrix B, DiagMatrix &result, const T division_min) { result[M_idx] = @@ -838,12 +1590,36 @@ template struct Core { // Termination condition: M_idx == 0 template struct Core { + /** + * @brief Computes the element-wise division of two diagonal matrices when the + * index is 0. + * + * This function serves as the base case for the recursive division of two + * diagonal matrices A and B, storing the result in the provided result + * matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ static void compute(const DiagMatrix &A, const DiagMatrix B, DiagMatrix &result, const T division_min) { result[0] = A[0] / Base::Utility::avoid_zero_divide(B[0], division_min); } }; +/** + * @brief Computes the element-wise division of two diagonal matrices. + * + * This function initiates the recursive computation of the division of two + * diagonal matrices A and B, storing the result in the provided result matrix. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param result The resulting diagonal matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ template inline void compute(const DiagMatrix &A, const DiagMatrix &B, DiagMatrix &result, const T division_min) { @@ -852,6 +1628,18 @@ inline void compute(const DiagMatrix &A, const DiagMatrix &B, } // namespace DiagMatrixDivideDiagMatrix +/** + * @brief Divides two diagonal matrices element-wise. + * + * This function computes the element-wise division of two diagonal matrices A + * and B, returning a new diagonal matrix that contains the results of the + * division of the corresponding diagonal elements. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param division_min The minimum value to avoid division by zero. + * @return DiagMatrix The resulting diagonal matrix after division. + */ template inline DiagMatrix diag_divide_diag(const DiagMatrix &A, const DiagMatrix &B, @@ -873,6 +1661,20 @@ inline DiagMatrix diag_divide_diag(const DiagMatrix &A, return result; } +/** + * @brief Divides two diagonal matrices element-wise with partitioning. + * + * This function computes the element-wise division of two diagonal matrices A + * and B, returning a new diagonal matrix that contains the results of the + * division of the corresponding diagonal elements, limited to a specified + * matrix size. + * + * @param A The first diagonal matrix. + * @param B The second diagonal matrix. + * @param division_min The minimum value to avoid division by zero. + * @param matrix_size The size of the matrices to consider for the operation. + * @return DiagMatrix The resulting diagonal matrix after division. + */ template inline DiagMatrix diag_divide_diag_partition(const DiagMatrix &A, const DiagMatrix &B, @@ -894,6 +1696,19 @@ namespace DiagMatrixInverseMultiplyMatrix { template struct Column { + /** + * @brief Computes the element-wise division of a dense matrix by a diagonal + * matrix. + * + * This function recursively computes the division of each element in a dense + * matrix B by the corresponding diagonal element in a diagonal matrix A, + * storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param result The resulting dense matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result, const T division_min) { result(J, K) = @@ -905,6 +1720,20 @@ struct Column { // if K == 0 template struct Column { + /** + * @brief Computes the element-wise division of the first column of a dense + * matrix by a diagonal matrix. + * + * This function serves as the base case for the recursive division of each + * element in the first column of a dense matrix B by the corresponding + * diagonal element in a diagonal matrix A, storing the result in the + * provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param result The resulting dense matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result, const T division_min) { result(J, 0) = @@ -914,6 +1743,19 @@ struct Column { // Column-wise multiplication template struct Row { + /** + * @brief Computes the element-wise division of a dense matrix by a diagonal + * matrix, row by row. + * + * This function recursively computes the division of each element in a dense + * matrix B by the corresponding diagonal element in a diagonal matrix A, + * storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param result The resulting dense matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result, const T division_min) { Column::compute(A, B, result, division_min); @@ -923,12 +1765,39 @@ template struct Row { // if J == 0 template struct Row { + /** + * @brief Computes the element-wise division of the first row of a dense + * matrix by a diagonal matrix. + * + * This function serves as the base case for the recursive division of each + * element in the first row of a dense matrix B by the corresponding diagonal + * element in a diagonal matrix A, storing the result in the provided result + * matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param result The resulting dense matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ static void compute(const DiagMatrix &A, const Matrix &B, Matrix &result, const T division_min) { Column::compute(A, B, result, division_min); } }; +/** + * @brief Computes the element-wise division of a dense matrix by a diagonal + * matrix. + * + * This function initiates the recursive computation of the division of each + * element in a dense matrix B by the corresponding diagonal element in a + * diagonal matrix A, storing the result in the provided result matrix. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param result The resulting dense matrix after division. + * @param division_min The minimum value to avoid division by zero. + */ template inline void compute(const DiagMatrix &A, const Matrix &B, Matrix &result, T division_min) { @@ -937,6 +1806,19 @@ inline void compute(const DiagMatrix &A, const Matrix &B, } // namespace DiagMatrixInverseMultiplyMatrix +/** + * @brief Divides each element of a dense matrix by the corresponding diagonal + * element of a diagonal matrix. + * + * This function computes the element-wise division of each element in a dense + * matrix B by the corresponding diagonal element in a diagonal matrix A, + * returning a new dense matrix that contains the results of the division. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param division_min The minimum value to avoid division by zero. + * @return Matrix The resulting dense matrix after division. + */ template inline Matrix diag_inv_multiply_dense(const DiagMatrix &A, const Matrix &B, @@ -961,6 +1843,21 @@ inline Matrix diag_inv_multiply_dense(const DiagMatrix &A, return result; } +/** + * @brief Divides each element of a dense matrix by the corresponding diagonal + * element of a diagonal matrix, with partitioning. + * + * This function computes the element-wise division of each element in a dense + * matrix B by the corresponding diagonal element in a diagonal matrix A, + * returning a new dense matrix that contains the results of the division, + * limited to a specified matrix size. + * + * @param A The diagonal matrix. + * @param B The dense matrix to divide. + * @param division_min The minimum value to avoid division by zero. + * @param matrix_size The size of the matrices to consider for the operation. + * @return Matrix The resulting dense matrix after division. + */ template inline Matrix diag_inv_multiply_dense_partition( const DiagMatrix &A, const Matrix &B, const T &division_min, @@ -983,6 +1880,16 @@ namespace DiagMatrixRealToComplex { /* Helper struct for unrolling the loop */ template struct DiagMatrixRealToComplexLoop { + /** + * @brief Converts a real diagonal matrix to a complex diagonal matrix. + * + * This function recursively converts each element of a real diagonal matrix + * to a complex diagonal matrix, where the real part is preserved and the + * imaginary part is set to zero. + * + * @param From_matrix The real diagonal matrix to convert. + * @param To_matrix The resulting complex diagonal matrix after conversion. + */ static void compute(const DiagMatrix &From_matrix, DiagMatrix, M> &To_matrix) { To_matrix[I].real = From_matrix[I]; @@ -993,12 +1900,33 @@ struct DiagMatrixRealToComplexLoop { /* Specialization to end the recursion */ template struct DiagMatrixRealToComplexLoop { + /** + * @brief Converts the first element of a real diagonal matrix to a complex + * diagonal matrix. + * + * This function serves as the base case for the recursive conversion of a + * real diagonal matrix to a complex diagonal matrix, where the first element + * is converted. + * + * @param From_matrix The real diagonal matrix to convert. + * @param To_matrix The resulting complex diagonal matrix after conversion. + */ static void compute(const DiagMatrix &From_matrix, DiagMatrix, M> &To_matrix) { To_matrix[0].real = From_matrix[0]; } }; +/** + * @brief Converts a real diagonal matrix to a complex diagonal matrix. + * + * This function initiates the recursive conversion of a real diagonal matrix + * to a complex diagonal matrix, where each element is converted to a complex + * number with the real part preserved and the imaginary part set to zero. + * + * @param From_matrix The real diagonal matrix to convert. + * @param To_matrix The resulting complex diagonal matrix after conversion. + */ template inline void compute(const DiagMatrix &From_matrix, DiagMatrix, M> &To_matrix) { @@ -1007,6 +1935,17 @@ inline void compute(const DiagMatrix &From_matrix, } // namespace DiagMatrixRealToComplex +/** + * @brief Converts a real diagonal matrix to a complex diagonal matrix. + * + * This function creates a complex diagonal matrix from a real diagonal matrix + * A, where each element of A is converted to a complex number with the real + * part preserved and the imaginary part set to zero. + * + * @param From_matrix The real diagonal matrix to convert. + * @return DiagMatrix, M> The resulting complex diagonal matrix after + * conversion. + */ template inline DiagMatrix, M> convert_matrix_real_to_complex(const DiagMatrix &From_matrix) { @@ -1033,6 +1972,17 @@ namespace GetRealDiagMatrixFromComplex { /* Helper struct for unrolling the loop */ template struct Loop { + /** + * @brief Extracts the real part of a complex diagonal matrix and stores it in + * a real diagonal matrix. + * + * This function recursively extracts the real part of each element in a + * complex diagonal matrix and stores it in a corresponding position in a real + * diagonal matrix. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @param To_matrix The resulting real diagonal matrix after extraction. + */ static void compute(const DiagMatrix, M> &From_matrix, DiagMatrix &To_matrix) { To_matrix[I] = From_matrix[I].real; @@ -1042,12 +1992,34 @@ template struct Loop { /* Specialization to end the recursion */ template struct Loop { + /** + * @brief Extracts the real part of the first element of a complex diagonal + * matrix and stores it in a real diagonal matrix. + * + * This function serves as the base case for the recursive extraction of the + * real part of a complex diagonal matrix, storing the first element in a real + * diagonal matrix. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @param To_matrix The resulting real diagonal matrix after extraction. + */ static void compute(const DiagMatrix, M> &From_matrix, DiagMatrix &To_matrix) { To_matrix[0] = From_matrix[0].real; } }; +/** + * @brief Extracts the real part of a complex diagonal matrix and stores it in a + * real diagonal matrix. + * + * This function initiates the recursive extraction of the real part of each + * element in a complex diagonal matrix and stores it in a corresponding + * position in a real diagonal matrix. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @param To_matrix The resulting real diagonal matrix after extraction. + */ template inline void compute(const DiagMatrix, 3> &From_matrix, DiagMatrix &To_matrix) { @@ -1056,6 +2028,16 @@ inline void compute(const DiagMatrix, 3> &From_matrix, } // namespace GetRealDiagMatrixFromComplex +/** + * @brief Extracts the real part of a complex diagonal matrix and stores it in a + * real diagonal matrix. + * + * This function creates a real diagonal matrix from a complex diagonal matrix + * A, where each element of A is converted to its real part. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @return DiagMatrix The resulting real diagonal matrix after extraction. + */ template inline auto get_real_matrix_from_complex_matrix( const DiagMatrix, M> &From_matrix) -> DiagMatrix { @@ -1082,6 +2064,17 @@ namespace GetImagDiagMatrixFromComplex { /* Helper struct for unrolling the loop */ template struct Loop { + /** + * @brief Extracts the imaginary part of a complex diagonal matrix and stores + * it in a real diagonal matrix. + * + * This function recursively extracts the imaginary part of each element in a + * complex diagonal matrix and stores it in a corresponding position in a real + * diagonal matrix. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @param To_matrix The resulting real diagonal matrix after extraction. + */ static void compute(const DiagMatrix, M> &From_matrix, DiagMatrix &To_matrix) { To_matrix[I] = From_matrix[I].imag; @@ -1091,12 +2084,34 @@ template struct Loop { /* Specialization to end the recursion */ template struct Loop { + /** + * @brief Extracts the imaginary part of the first element of a complex + * diagonal matrix and stores it in a real diagonal matrix. + * + * This function serves as the base case for the recursive extraction of the + * imaginary part of a complex diagonal matrix, storing the first element in a + * real diagonal matrix. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @param To_matrix The resulting real diagonal matrix after extraction. + */ static void compute(const DiagMatrix, M> &From_matrix, DiagMatrix &To_matrix) { To_matrix[0] = From_matrix[0].imag; } }; +/** + * @brief Extracts the imaginary part of a complex diagonal matrix and stores it + * in a real diagonal matrix. + * + * This function initiates the recursive extraction of the imaginary part of + * each element in a complex diagonal matrix and stores it in a corresponding + * position in a real diagonal matrix. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @param To_matrix The resulting real diagonal matrix after extraction. + */ template inline void compute(const DiagMatrix, M> &From_matrix, DiagMatrix &To_matrix) { @@ -1105,6 +2120,16 @@ inline void compute(const DiagMatrix, M> &From_matrix, } // namespace GetImagDiagMatrixFromComplex +/** + * @brief Extracts the imaginary part of a complex diagonal matrix and stores it + * in a real diagonal matrix. + * + * This function creates a real diagonal matrix from a complex diagonal matrix + * A, where each element of A is converted to its imaginary part. + * + * @param From_matrix The complex diagonal matrix to extract from. + * @return DiagMatrix The resulting real diagonal matrix after extraction. + */ template inline auto get_imag_matrix_from_complex_matrix( const DiagMatrix, M> &From_matrix) -> DiagMatrix { From 785474536782a71cf57bf7a2cee0b802dd82bb14 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 20:55:50 +0900 Subject: [PATCH 09/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_eigen_solver.hpp | 316 +++++++++++++++++++++++ 1 file changed, 316 insertions(+) diff --git a/base_matrix/base_matrix_eigen_solver.hpp b/base_matrix/base_matrix_eigen_solver.hpp index 927518d..9acf682 100644 --- a/base_matrix/base_matrix_eigen_solver.hpp +++ b/base_matrix/base_matrix_eigen_solver.hpp @@ -1,3 +1,21 @@ +/** + * @file base_matrix_eigen_solver.hpp + * @brief Eigenvalue and Eigenvector Solver for Real and Complex Matrices + * + * This header provides template classes for computing eigenvalues and + * eigenvectors of square matrices, supporting both real and complex types. The + * solvers use the QR algorithm (with optional Hessenberg reduction and + * Wilkinson shift) for eigenvalue computation, and the inverse iteration method + * for eigenvector computation. The implementation is compatible with both + * std::vector and std::array storage, controlled by the + * __BASE_MATRIX_USE_STD_VECTOR__ macro. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_EIGEN_SOLVER_HPP__ #define __BASE_MATRIX_EIGEN_SOLVER_HPP__ @@ -24,6 +42,17 @@ const std::size_t DEFAULT_ITERATION_MAX_EIGEN_SOLVER = 10; const double DEFAULT_DIVISION_MIN_EIGEN_SOLVER = 1.0e-20; const double EIGEN_SMALL_VALUE = 1.0e-6; +/** + * @brief EigenSolverReal is a template class for computing eigenvalues and + * eigenvectors of real square matrices. + * + * This class provides methods to solve eigenvalues and eigenvectors using the + * QR algorithm, with optional Hessenberg reduction and Wilkinson shift. It + * supports both std::vector and std::array storage for eigenvalues. + * + * @tparam T Type of the matrix elements (e.g., float, double). + * @tparam M Size of the square matrix (number of rows and columns). + */ template class EigenSolverReal { public: /* Constructor */ @@ -108,49 +137,141 @@ template class EigenSolverReal { public: /* Function */ + + /** + * @brief Computes the eigenvalues of a square matrix using the QR method. + * + * This function takes a square matrix as input and computes its eigenvalues + * by internally invoking the QR algorithm. The results are stored within the + * class instance. + * + * @tparam T The data type of the matrix elements. + * @tparam M The dimension of the square matrix. + * @param matrix The input square matrix whose eigenvalues are to be computed. + */ inline void solve_eigen_values(const Matrix &matrix) { this->_solve_values_with_qr_method(matrix); } + /** + * @brief Continues solving eigenvalues using the QR method. + * + * This function continues the process of computing eigenvalues using the QR + * method, which may have been started previously. It is useful for iterative + * refinement of eigenvalue calculations. + */ inline void continue_solving_eigen_values(void) { this->_continue_solving_values_with_qr_method(); } + /** + * @brief Computes the eigenvectors of a square matrix using the inverse + * iteration method. + * + * This function takes a square matrix as input and computes its eigenvectors + * by internally invoking the inverse iteration method. The results are stored + * within the class instance. + * + * @tparam T The data type of the matrix elements. + * @tparam M The dimension of the square matrix. + * @param matrix The input square matrix whose eigenvectors are to be + * computed. + */ inline void solve_eigen_vectors(const Matrix &matrix) { this->_solve_vectors_with_inverse_iteration_method(matrix); } + /** + * @brief Computes both eigenvalues and eigenvectors of a square matrix. + * + * This function computes the eigenvalues using the QR method and the + * eigenvectors using the inverse iteration method, storing the results + * within the class instance. + * + * @tparam T The data type of the matrix elements. + * @tparam M The dimension of the square matrix. + * @param matrix The input square matrix whose eigenvalues and eigenvectors + * are to be computed. + */ inline void solve_eigen_values_and_vectors(const Matrix &matrix) { this->_solve_values_with_qr_method(matrix); this->_solve_vectors_with_inverse_iteration_method(matrix); } #ifdef __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Retrieves the computed eigenvalues. + * This function returns the eigenvalues computed by the QR method as a + * vector. + * @return std::vector A vector containing the eigenvalues. + * */ inline std::vector get_eigen_values(void) { return this->_eigen_values; } #else // __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Retrieves the computed eigenvalues. + * This function returns the eigenvalues computed by the QR method as an + * array. + * @return std::array An array containing the eigenvalues. + * */ inline std::array get_eigen_values(void) { return this->_eigen_values; } #endif // __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Retrieves the computed eigenvectors. + * This function returns the eigenvectors computed by the inverse iteration + * method as a matrix. + * @return Matrix A matrix containing the eigenvectors. + */ inline Matrix get_eigen_vectors(void) { return this->_eigen_vectors; } + /** + * @brief Retrieves the Hessenberg form of the matrix. + * This function returns the Hessenberg form of the matrix computed during the + * QR method. + * @return Matrix A matrix in Hessenberg form. + */ inline void set_iteration_max(const std::size_t &iteration_max_in) { this->iteration_max = iteration_max_in; } + /** + * @brief Sets the minimum division value for numerical stability. + * This function sets the minimum value used to avoid division by zero in + * numerical computations. + * @param division_min_in The minimum division value. + */ inline void set_division_min(const T &division_min_in) { this->division_min = division_min_in; } + /** + * @brief Sets a small value for numerical stability. + * This function sets a small value used to determine convergence in the QR + * method. + * @param small_value_in The small value for numerical stability. + */ inline void set_small_value(const T &small_value_in) { this->small_value = small_value_in; } + /** + * @brief Sets the decay rate for GMRES k. + * This function sets the decay rate for the GMRES k method, which is used in + * iterative methods for solving linear systems. + * @param gmres_k_decay_rate_in The decay rate for GMRES k. + */ inline void set_gmres_k_decay_rate(const T &gmres_k_decay_rate_in) { this->gmres_k_decay_rate = gmres_k_decay_rate_in; } + /** + * @brief Retrieves the Hessenberg form of the matrix. + * This function returns the Hessenberg form of the matrix computed during the + * QR method. + * @return Matrix A matrix in Hessenberg form. + */ inline Matrix check_validity(const Matrix &matrix) { Matrix result = matrix * this->_eigen_vectors - @@ -184,6 +305,13 @@ template class EigenSolverReal { protected: /* Function */ + + /** + * @brief Solves eigenvalues using the QR method. + * This function computes the eigenvalues of a square matrix using the QR + * algorithm, with optional Hessenberg reduction and Wilkinson shift. + * @param A The input square matrix whose eigenvalues are to be computed. + */ inline void _hessenberg(const Matrix &A) { Matrix R = A; std::array u; @@ -244,6 +372,12 @@ template class EigenSolverReal { this->_Hessen = R; } + /** + * @brief Solves eigenvalues using the QR method. + * This function computes the eigenvalues of a square matrix using the QR + * algorithm, with optional Hessenberg reduction and Wilkinson shift. + * @param A The input square matrix whose eigenvalues are to be computed. + */ inline void _qr_decomposition(Matrix &Q, Matrix &R, const Matrix &A) { R = A; @@ -309,6 +443,14 @@ template class EigenSolverReal { } } + /** + * @brief Computes the Wilkinson shift for the QR method. + * This function computes the Wilkinson shift, which is used to improve the + * convergence of the QR algorithm for eigenvalue computation. + * @param A The input square matrix from which the Wilkinson shift is + * computed. + * @return T The computed Wilkinson shift value. + */ inline T _wilkinson_shift(const Matrix &A) { T a11 = A(M - 2, M - 2); T a12 = A(M - 2, M - 1); @@ -331,6 +473,12 @@ template class EigenSolverReal { return (dmu1 <= dmu2) ? mu1 : mu2; } + /** + * @brief Continues solving eigenvalues using the QR method. + * This function continues the process of computing eigenvalues using the QR + * method, which may have been started previously. It is useful for iterative + * refinement of eigenvalue calculations. + */ inline void _continue_solving_values_with_qr_method(void) { for (std::size_t k = M; k > 1; --k) { Matrix A = this->_Hessen; @@ -363,12 +511,26 @@ template class EigenSolverReal { } } + /** + * @brief Solves eigenvalues using the QR method. + * This function computes the eigenvalues of a square matrix using the QR + * algorithm, with optional Hessenberg reduction and Wilkinson shift. + * @param A0 The input square matrix whose eigenvalues are to be computed. + */ inline void _solve_values_with_qr_method(const Matrix &A0) { this->_hessenberg(A0); this->_continue_solving_values_with_qr_method(); } + /** + * @brief Solves eigenvectors using the inverse iteration method. + * This function computes the eigenvectors of a square matrix using the + * inverse iteration method, which is applied to each eigenvalue computed by + * the QR method. + * @param matrix The input square matrix whose eigenvectors are to be + * computed. + */ inline void _solve_vectors_with_inverse_iteration_method(const Matrix &matrix) { @@ -414,6 +576,17 @@ template class EigenSolverReal { } }; +/** + * @brief EigenSolverComplex is a template class for computing eigenvalues and + * eigenvectors of complex square matrices. + * + * This class provides methods to solve eigenvalues and eigenvectors using the + * QR algorithm, with optional Hessenberg reduction and Wilkinson shift. It + * supports both std::vector and std::array storage for eigenvalues. + * + * @tparam T Type of the matrix elements (e.g., float, double). + * @tparam M Size of the square matrix (number of rows and columns). + */ template class EigenSolverComplex { public: /* Constructor */ @@ -510,58 +683,160 @@ template class EigenSolverComplex { public: /* Function */ + + /** + * @brief Computes the eigenvalues of a complex square matrix using the QR + * method. + * + * This function takes a complex square matrix as input and computes its + * eigenvalues by internally invoking the QR algorithm. The results are stored + * within the class instance. + * + * @tparam T The data type of the matrix elements. + * @tparam M The dimension of the square matrix. + * @param matrix The input complex square matrix whose eigenvalues are to be + * computed. + */ inline void solve_eigen_values(const Matrix &matrix) { this->_solve_with_qr_method(matrix); } + /** + * @brief Continues solving eigenvalues using the QR method. + * + * This function continues the process of computing eigenvalues using the QR + * method, which may have been started previously. It is useful for iterative + * refinement of eigenvalue calculations. + */ inline void continue_solving_eigen_values(void) { this->_continue_solving_values_with_qr_method(); } + /** + * @brief Computes the eigenvectors of a complex square matrix using the + * inverse iteration method. + * + * This function takes a complex square matrix as input and computes its + * eigenvectors by internally invoking the inverse iteration method. The + * results are stored within the class instance. + * + * @tparam T The data type of the matrix elements. + * @tparam M The dimension of the square matrix. + * @param matrix The input complex square matrix whose eigenvectors are to be + * computed. + */ inline void solve_eigen_vectors(const Matrix &matrix) { this->_solve_vectors_with_inverse_iteration_method(matrix); } + /** + * @brief Computes both eigenvalues and eigenvectors of a complex square + * matrix. + * + * This function computes the eigenvalues using the QR method and the + * eigenvectors using the inverse iteration method, storing the results + * within the class instance. + * + * @tparam T The data type of the matrix elements. + * @tparam M The dimension of the square matrix. + * @param matrix The input complex square matrix whose eigenvalues and + * eigenvectors are to be computed. + */ inline void solve_eigen_values_and_vectors(const Matrix &matrix) { this->_solve_with_qr_method(matrix); this->_solve_vectors_with_inverse_iteration_method(matrix); } #ifdef __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Retrieves the computed eigenvalues. + * This function returns the eigenvalues computed by the QR method as a + * vector. + * @return std::vector> A vector containing the eigenvalues. + * */ inline std::vector> get_eigen_values(void) { return this->_eigen_values; } #else // __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Retrieves the computed eigenvalues. + * This function returns the eigenvalues computed by the QR method as an + * array. + * @return std::array, M> An array containing the eigenvalues. + * */ inline std::array, M> get_eigen_values(void) { return this->_eigen_values; } #endif // __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Retrieves the computed eigenvectors. + * This function returns the eigenvectors computed by the inverse iteration + * method as a matrix. + * @return Matrix, M, M> A matrix containing the eigenvectors. + */ inline Matrix, M, M> get_eigen_vectors(void) { return this->_eigen_vectors; } + /** + * @brief Retrieves the Hessenberg form of the matrix. + * This function returns the Hessenberg form of the matrix computed during the + * QR method. + * @return Matrix, M, M> A matrix in Hessenberg form. + */ inline void set_iteration_max(const std::size_t &iteration_max_in) { this->iteration_max = iteration_max_in; } + /** + * @brief Sets the maximum number of iterations for eigenvector computation. + * This function sets the maximum number of iterations for computing + * eigenvectors using the inverse iteration method. + * @param iteration_max_for_eigen_vector_in The maximum number of iterations + * for eigenvector computation. + */ inline void set_iteration_max_for_eigen_vector( const std::size_t &iteration_max_for_eigen_vector_in) { this->iteration_max_for_eigen_vector = iteration_max_for_eigen_vector_in; } + /** + * @brief Sets the minimum division value for numerical stability. + * This function sets the minimum value used to avoid division by zero in + * numerical computations. + * @param division_min_in The minimum division value. + */ inline void set_division_min(const T &division_min_in) { this->division_min = division_min_in; } + /** + * @brief Sets a small value for numerical stability. + * This function sets a small value used to determine convergence in the QR + * method. + * @param small_value_in The small value for numerical stability. + */ inline void set_small_value(const T &small_value_in) { this->small_value = small_value_in; } + /** + * @brief Sets the decay rate for GMRES k. + * This function sets the decay rate for the GMRES k method, which is used in + * iterative methods for solving linear systems. + * @param gmres_k_decay_rate_in The decay rate for GMRES k. + */ inline void set_gmres_k_decay_rate(const T &gmres_k_decay_rate_in) { this->gmres_k_decay_rate = gmres_k_decay_rate_in; } + /** + * @brief Retrieves the Hessenberg form of the matrix. + * This function returns the Hessenberg form of the matrix computed during the + * QR method. + * @return Matrix, M, M> A matrix in Hessenberg form. + */ inline Matrix, M, M> check_validity(const Matrix &matrix) { Matrix, M, M> result = @@ -597,6 +872,13 @@ template class EigenSolverComplex { protected: /* Function */ + + /** + * @brief Solves eigenvalues using the QR method. + * This function computes the eigenvalues of a square matrix using the QR + * algorithm, with optional Hessenberg reduction and Wilkinson shift. + * @param A The input square matrix whose eigenvalues are to be computed. + */ inline void _hessenberg(const Matrix &A) { Matrix R = A; std::array u; @@ -657,6 +939,12 @@ template class EigenSolverComplex { this->_Hessen = Base::Matrix::convert_matrix_real_to_complex(R); } + /** + * @brief Solves eigenvalues using the QR method. + * This function computes the eigenvalues of a square matrix using the QR + * algorithm, with optional Hessenberg reduction and Wilkinson shift. + * @param A The input square matrix whose eigenvalues are to be computed. + */ inline void _qr_decomposition(Matrix, M, M> &Q, Matrix, M, M> &R, const Matrix, M, M> &A) { @@ -728,6 +1016,14 @@ template class EigenSolverComplex { } } + /** + * @brief Computes the Wilkinson shift for the QR method. + * This function computes the Wilkinson shift, which is used to improve the + * convergence of the QR algorithm for eigenvalue computation. + * @param A The input square matrix from which the Wilkinson shift is + * computed. + * @return Complex The computed Wilkinson shift value. + */ inline Complex _wilkinson_shift(const Matrix, M, M> &A) { Complex a11 = A(M - 2, M - 2); Complex a12 = A(M - 2, M - 1); @@ -749,12 +1045,24 @@ template class EigenSolverComplex { } } + /** + * @brief Solves eigenvalues using the QR method. + * This function computes the eigenvalues of a square matrix using the QR + * algorithm, with optional Hessenberg reduction and Wilkinson shift. + * @param A0 The input square matrix whose eigenvalues are to be computed. + */ inline void _solve_with_qr_method(const Matrix &A0) { this->_hessenberg(A0); this->_continue_solving_values_with_qr_method(); } + /** + * @brief Continues solving eigenvalues using the QR method. + * This function continues the process of computing eigenvalues using the QR + * method, which may have been started previously. It is useful for iterative + * refinement of eigenvalue calculations. + */ inline void _continue_solving_values_with_qr_method(void) { for (std::size_t k = M; k > 1; --k) { Matrix, M, M> A = this->_Hessen; @@ -787,6 +1095,14 @@ template class EigenSolverComplex { } } + /** + * @brief Solves eigenvectors using the inverse iteration method. + * This function computes the eigenvectors of a square matrix using the + * inverse iteration method, which is applied to each eigenvalue computed by + * the QR method. + * @param matrix The input square matrix whose eigenvectors are to be + * computed. + */ inline void _solve_vectors_with_inverse_iteration_method(const Matrix &matrix) { From 2e1a3aa123c4c540d3cec1f7cfc3ffa4b87c6df3 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 21:51:44 +0900 Subject: [PATCH 10/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_inverse.hpp | 635 ++++++++++++++++++++++++++++ 1 file changed, 635 insertions(+) diff --git a/base_matrix/base_matrix_inverse.hpp b/base_matrix/base_matrix_inverse.hpp index 8296dad..0a9eec5 100644 --- a/base_matrix/base_matrix_inverse.hpp +++ b/base_matrix/base_matrix_inverse.hpp @@ -1,3 +1,21 @@ +/** + * @file base_matrix_inverse.hpp + * @brief GMRES-based matrix inverse and linear solver utilities for dense, + * sparse, and complex matrices. + * + * This header provides a collection of template functions for solving linear + * systems and computing matrix inverses using the GMRES (Generalized Minimal + * Residual) iterative method. It supports dense matrices, sparse matrices + * (CSR-like), and complex-valued matrices, as well as diagonal matrix + * inversion. The implementation is generic and works with fixed-size matrices + * and vectors, supporting both real and complex types. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_INVERSE_HPP__ #define __BASE_MATRIX_INVERSE_HPP__ @@ -19,6 +37,25 @@ namespace Matrix { namespace InverseOperation { +/** + * @brief Core function for GMRES K method for dense matrices. + * + * This function implements the GMRES K algorithm to solve the linear system + * Ax = b, where A is a square matrix, b is a vector, and x_1 is an initial + * guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The input matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @param matrix_size The size of the matrix (M). + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M > 1), Vector>::type gmres_k_core(const Matrix &A, const Vector &b, @@ -138,6 +175,25 @@ gmres_k_core(const Matrix &A, const Vector &b, return x; } +/** + * @brief Core function for GMRES K method for 1x1 matrices. + * + * This specialization handles the case where the matrix size is 1x1, which is a + * trivial case. It simply computes the inverse of the single element in the + * matrix. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M), must be 1 in this case. + * @param A The input matrix A, which is expected to be 1x1. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @param matrix_size The size of the matrix (M), must be 1 in this case. + * @return The computed solution vector x, which will also be a 1x1 vector. + */ template inline typename std::enable_if<(M <= 1), Vector>::type gmres_k_core(const Matrix &A, const Vector &b, @@ -163,6 +219,24 @@ gmres_k_core(const Matrix &A, const Vector &b, } // namespace InverseOperation +/** + * @brief GMRES K method for dense matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a square matrix, b is a vector, and x_1 is an initial guess. + * It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The input matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline Vector gmres_k(const Matrix &A, const Vector &b, const Vector &x_1, const T &decay_rate, @@ -173,6 +247,25 @@ inline Vector gmres_k(const Matrix &A, const Vector &b, division_min, rho, rep_num, M); } +/** + * @brief GMRES K method for partitioned matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a square matrix, b is a vector, and x_1 is an initial guess. + * It supports partitioning of the matrix and returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The input matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @param matrix_size The size of the matrix (M). + * @return The computed solution vector x. + */ template inline Vector gmres_k_partition(const Matrix &A, const Vector &b, @@ -185,6 +278,26 @@ gmres_k_partition(const Matrix &A, const Vector &b, } /* GMRES K for Matrix */ + +/** + * @brief GMRES K method for matrices. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a square matrix, B is a matrix with multiple right-hand side + * vectors, and X_1 is an initial guess. It returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam K The number of rows of matrix B. + * @param A The input matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each column. + * @param rep_num Output parameter to store the number of iterations performed + * for each column. + */ template inline void gmres_k_matrix(const Matrix &A, const Matrix &B, Matrix &X_1, const T &decay_rate, @@ -199,6 +312,24 @@ inline void gmres_k_matrix(const Matrix &A, const Matrix &B, } } +/** + * @brief GMRES K method for matrices with diagonal matrix B. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a square matrix, B is a diagonal matrix, and X_1 is an initial + * guess. It returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The input matrix A. + * @param B The diagonal matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void gmres_k_matrix(const Matrix &A, const DiagMatrix &B, Matrix &X_1, const T &decay_rate, @@ -213,6 +344,26 @@ inline void gmres_k_matrix(const Matrix &A, const DiagMatrix &B, } } +/** + * @brief GMRES K method for partitioned matrices. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a square matrix, B is a matrix with multiple right-hand side + * vectors, and X_1 is an initial guess. It supports partitioning of the matrix + * and returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam K The number of rows of matrix B. + * @param A The input matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each column. + * @param rep_num Output parameter to store the number of iterations performed + * for each column. + */ template inline void gmres_k_partition_matrix( const Matrix &A, const Matrix &B, Matrix &X_1, @@ -234,6 +385,25 @@ inline void gmres_k_partition_matrix( } } +/** + * @brief GMRES K method for partitioned matrices with diagonal matrix B. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a square matrix, B is a diagonal matrix, and X_1 is an initial + * guess. It supports partitioning of the matrix and returns the solution + * matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The input matrix A. + * @param B The diagonal matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void gmres_k_partition_matrix( const Matrix &A, const DiagMatrix &B, Matrix &X_1, @@ -249,6 +419,26 @@ inline void gmres_k_partition_matrix( } /* GMRES K for rectangular matrix */ + +/** + * @brief GMRES K method for rectangular matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a rectangular matrix (M x N), b is a vector, and x_1 is an + * initial guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The number of rows in the matrix A. + * @tparam N The number of columns in the matrix A. + * @param In_A The input matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline Vector gmres_k_rect(const Matrix &In_A, const Vector &b, const Vector &x_1, @@ -371,6 +561,28 @@ inline Vector gmres_k_rect(const Matrix &In_A, return x; } +/** + * @brief GMRES K method for rectangular matrices with multiple right-hand side + * vectors. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a rectangular matrix (M x N), B is a matrix with multiple right- + * hand side vectors, and X_1 is an initial guess. It returns the solution + * matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The number of columns in the matrix A. + * @tparam N The number of rows in the matrix A. + * @tparam K The number of rows of matrix B. + * @param A The input matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void gmres_k_rect_matrix(const Matrix &A, const Matrix &B, Matrix &X_1, @@ -402,6 +614,25 @@ inline void gmres_k_rect_matrix(const Matrix &A, } /* GMRES K for matrix inverse */ + +/** + * @brief GMRES K method for matrix inverse. + * + * This function computes the inverse of a square matrix A using the GMRES K + * method, where A is a square matrix, and X_1 is an initial guess. It returns + * the inverse matrix X. + * + * @tparam T The data type of the matrix elements. + * @tparam M The size of the square matrix (M x M). + * @param In_A The input matrix A. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + * @param X_1 The initial guess for the solution matrix X_1. + * @return The computed inverse matrix X. + */ template inline Matrix gmres_k_matrix_inv(const Matrix In_A, const T &decay_rate, @@ -425,6 +656,27 @@ gmres_k_matrix_inv(const Matrix In_A, const T &decay_rate, /* Sparse GMRES K */ namespace InverseOperation { +/** + * @brief Core function for GMRES K method for sparse matrices. + * + * This function implements the core logic of the GMRES K method for sparse + * matrices, handling the case where the matrix size is larger than 1x1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam RowIndices_A The type representing row indices of the sparse matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param SA The compiled sparse matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @param matrix_size The size of the matrix (M). + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M > 1), Vector>::type sparse_gmres_k_core( @@ -558,6 +810,24 @@ inline typename std::enable_if<(M > 1), Vector>::type sparse_gmres_k_core( return x; } +/** + * @brief Core function for GMRES K method for sparse matrices with size 1x1. + * + * This function implements the core logic of the GMRES K method for sparse + * matrices, handling the case where the matrix size is exactly 1x1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param SA The compiled sparse matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @param matrix_size The size of the matrix (M). + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M <= 1), Vector>::type @@ -585,6 +855,24 @@ sparse_gmres_k_core( } // namespace InverseOperation +/** + * @brief GMRES K method for sparse matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a sparse matrix, b is a vector, and x_1 is an initial guess. + * It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param SA The compiled sparse matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline Vector sparse_gmres_k( @@ -596,6 +884,25 @@ inline Vector sparse_gmres_k( SA, b, x_1, decay_rate, division_min, rho, rep_num, M); } +/** + * @brief GMRES K method for sparse matrices with partitioning. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a sparse matrix, b is a vector, and x_1 is an initial guess. + * It supports partitioning of the matrix and returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param SA The compiled sparse matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @param matrix_size The size of the matrix (M). + * @return The computed solution vector x. + */ template inline Vector sparse_gmres_k_partition( @@ -608,6 +915,26 @@ inline Vector sparse_gmres_k_partition( SA, b, x_1, decay_rate, division_min, rho, rep_num, matrix_size); } +/** + * @brief GMRES K method for sparse matrices with multiple right-hand side + * vectors. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a sparse matrix, B is a matrix with multiple right-hand side + * vectors, and X_1 is an initial guess. It returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam K The number of rows of matrix B. + * @param SA The compiled sparse matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void sparse_gmres_k_matrix( @@ -624,6 +951,28 @@ inline void sparse_gmres_k_matrix( } } +/** + * @brief GMRES K method for sparse matrices with multiple right-hand side + * vectors and partitioning. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a sparse matrix, B is a matrix with multiple right-hand side + * vectors, and X_1 is an initial guess. It supports partitioning of the matrix + * and returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam K The number of rows of matrix B. + * @param SA The compiled sparse matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + * @param matrix_size The size of the matrix (M). + */ template inline void sparse_gmres_k_partition_matrix( @@ -648,6 +997,26 @@ inline void sparse_gmres_k_partition_matrix( } /* Sparse GMRES K for rectangular matrix */ + +/** + * @brief GMRES K method for rectangular matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a rectangular matrix (M x N), b is a vector, and x_1 is an + * initial guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The number of rows in the matrix A. + * @tparam N The number of columns in the matrix A. + * @param In_SA The compiled sparse matrix A. + * @param b The right-hand side vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline Vector sparse_gmres_k_rect( @@ -769,6 +1138,28 @@ inline Vector sparse_gmres_k_rect( return x; } +/** + * @brief GMRES K method for rectangular matrices with multiple right-hand side + * vectors. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a rectangular matrix (M x N), B is a matrix with multiple right- + * hand side vectors, and X_1 is an initial guess. It returns the solution + * matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The number of columns in the matrix A. + * @tparam N The number of rows in the matrix A. + * @tparam K The number of rows of matrix B. + * @param In_SA The compiled sparse matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void sparse_gmres_k_rect_matrix( @@ -785,6 +1176,30 @@ inline void sparse_gmres_k_rect_matrix( } } +/** + * @brief GMRES K method for rectangular matrices with multiple right-hand side + * vectors and partitioning. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a rectangular matrix (M x N), B is a matrix with multiple right- + * hand side vectors, and X_1 is an initial guess. It supports partitioning of + * the matrix and returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The number of columns in the matrix A. + * @tparam N The number of rows in the matrix A. + * @tparam RowIndices_A The type representing row indices of the sparse matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param In_SA The compiled sparse matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void sparse_gmres_k_rect_matrix( @@ -802,6 +1217,30 @@ inline void sparse_gmres_k_rect_matrix( } /* Sparse GMRES K for matrix inverse */ + +/** + * @brief GMRES K method for sparse matrix inverse. + * + * This function computes the inverse of a sparse matrix using the GMRES K + * method, where A is a sparse matrix, decay_rate is the convergence criterion, + * division_min is a small value to avoid division by zero, rho stores the + * residual norms, rep_num stores the number of iterations performed, and X_1 is + * an initial guess for the solution matrix. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam RowIndices_A The type representing row indices of the sparse matrix. + * @tparam RowPointers_A The type representing row pointers of the sparse + * matrix. + * @param In_A The compiled sparse matrix A. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + * @param X_1 The initial guess for the solution matrix X_1. + * @return The computed inverse matrix X. + */ template inline Matrix sparse_gmres_k_matrix_inv( @@ -824,6 +1263,25 @@ inline Matrix sparse_gmres_k_matrix_inv( } /* Complex GMRES K */ + +/** + * @brief GMRES K method for complex matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a complex matrix, b is a complex vector, and x_1 is an initial + * guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The complex matrix A. + * @param b The right-hand side complex vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M > 1), Vector, M>>::type complex_gmres_k(const Matrix, M, M> &A, @@ -944,6 +1402,24 @@ complex_gmres_k(const Matrix, M, M> &A, return x; } +/** + * @brief GMRES K method for complex matrices with size 1x1. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a complex matrix of size 1x1, b is a complex vector, and x_1 is + * an initial guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The complex matrix A. + * @param b The right-hand side complex vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M <= 1), Vector, M>>::type complex_gmres_k(const Matrix, M, M> &A, @@ -965,6 +1441,26 @@ complex_gmres_k(const Matrix, M, M> &A, return x; } +/** + * @brief GMRES K method for complex matrices with multiple right-hand side + * vectors. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a complex matrix, B is a matrix with multiple right-hand side + * vectors, and X_1 is an initial guess. It returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam K The number of rows of matrix B. + * @param A The complex matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void complex_gmres_k_matrix(const Matrix, M, M> &A, const Matrix, M, K> &B, @@ -981,6 +1477,26 @@ inline void complex_gmres_k_matrix(const Matrix, M, M> &A, } } +/** + * @brief GMRES K method for complex matrices with multiple right-hand side + * vectors and partitioning. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a complex matrix, B is a matrix with multiple right-hand side + * vectors, and X_1 is an initial guess. It supports partitioning of the matrix + * and returns the solution matrix X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param A The complex matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + */ template inline void complex_gmres_k_matrix(const Matrix, M, M> &A, const DiagMatrix, M> &B, @@ -997,6 +1513,24 @@ inline void complex_gmres_k_matrix(const Matrix, M, M> &A, } } +/** + * @brief GMRES K method for complex sparse matrices. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a complex sparse matrix, b is a complex vector, and x_1 is an + * initial guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param SA The compiled sparse matrix A. + * @param b The right-hand side complex vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M > 1), Vector, M>>::type @@ -1107,6 +1641,24 @@ complex_sparse_gmres_k( return x; } +/** + * @brief GMRES K method for complex sparse matrices with size 1x1. + * + * This function solves the linear system Ax = b using the GMRES K method, + * where A is a complex sparse matrix of size 1x1, b is a complex vector, and + * x_1 is an initial guess. It returns the solution vector x. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param SA The compiled sparse matrix A. + * @param b The right-hand side complex vector b. + * @param x_1 The initial guess for the solution vector x. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norm. + * @param rep_num Output parameter to store the number of iterations performed. + * @return The computed solution vector x. + */ template inline typename std::enable_if<(M <= 1), Vector, M>>::type @@ -1130,6 +1682,24 @@ complex_sparse_gmres_k( return x; } +/** + * @brief GMRES K method for complex sparse matrices with multiple right-hand + * side vectors. + * + * This function solves the linear system Ax = B using the GMRES K method, + * where A is a complex sparse matrix, B is a matrix with multiple right-hand + * side vectors, and X_1 is an initial guess. It returns the solution matrix + * X_1. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @tparam K The number of rows of matrix B. + * @param In_A The compiled sparse matrix A. + * @param B The right-hand side matrix B. + * @param X_1 The initial guess for the solution matrix X_1. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + */ template inline Matrix, M, M> complex_sparse_gmres_k_matrix( @@ -1154,6 +1724,26 @@ inline Matrix, M, M> complex_sparse_gmres_k_matrix( return X; } +/** + * @brief GMRES K method for complex matrix inverse. + * + * This function computes the inverse of a complex matrix using the GMRES K + * method, where A is a complex matrix, decay_rate is the convergence criterion, + * division_min is a small value to avoid division by zero, rho stores the + * residual norms, rep_num stores the number of iterations performed, and X_1 is + * an initial guess for the solution matrix. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param In_A The complex matrix A. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + * @param X_1 The initial guess for the solution matrix X_1. + * @return The computed inverse matrix X. + */ template inline Matrix, M, M> complex_gmres_k_matrix_inv( const Matrix, M, M> In_A, const T &decay_rate, @@ -1174,6 +1764,26 @@ inline Matrix, M, M> complex_gmres_k_matrix_inv( return X; } +/** + * @brief GMRES K method for complex sparse matrix inverse. + * + * This function computes the inverse of a complex sparse matrix using the GMRES + * K method, where A is a complex sparse matrix, decay_rate is the convergence + * criterion, division_min is a small value to avoid division by zero, rho + * stores the residual norms, rep_num stores the number of iterations performed, + * and X_1 is an initial guess for the solution matrix. + * + * @tparam T The data type of the matrix and vector elements. + * @tparam M The size of the square matrix (M x M). + * @param In_A The compiled sparse matrix A. + * @param decay_rate The convergence criterion for the GMRES method. + * @param division_min A small value to avoid division by zero. + * @param rho Output parameter to store the residual norms for each row. + * @param rep_num Output parameter to store the number of iterations performed + * for each row. + * @param X_1 The initial guess for the solution matrix X_1. + * @return The computed inverse matrix X. + */ template inline Matrix, M, M> complex_sparse_gmres_k_matrix_inv( @@ -1197,6 +1807,19 @@ inline Matrix, M, M> complex_sparse_gmres_k_matrix_inv( } /* Diag Matrix inverse */ + +/** + * @brief Inverse of a diagonal matrix. + * + * This function computes the inverse of a diagonal matrix, where each element + * is divided by a small value to avoid division by zero. + * + * @tparam T The data type of the matrix elements. + * @tparam M The size of the diagonal matrix (M x M). + * @param input The input diagonal matrix to be inverted. + * @param division_min A small value to avoid division by zero. + * @return The inverted diagonal matrix. + */ template inline DiagMatrix inverse_diag_matrix(const DiagMatrix &input, const T &division_min) { @@ -1210,6 +1833,18 @@ inline DiagMatrix inverse_diag_matrix(const DiagMatrix &input, return result; } +/** + * @brief Inverse of a complex diagonal matrix. + * + * This function computes the inverse of a complex diagonal matrix, where each + * element is divided by a small value to avoid division by zero. + * + * @tparam T The data type of the matrix elements. + * @tparam M The size of the diagonal matrix (M x M). + * @param input The input complex diagonal matrix to be inverted. + * @param division_min A small value to avoid division by zero. + * @return The inverted complex diagonal matrix. + */ template inline DiagMatrix, M> inverse_complex_diag_matrix(const DiagMatrix, M> &input, From 9a6611ac22b4ea5a34b644e5b188ff2f66f6cdba Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 21:54:57 +0900 Subject: [PATCH 11/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_lu_decomposition.hpp | 97 ++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/base_matrix/base_matrix_lu_decomposition.hpp b/base_matrix/base_matrix_lu_decomposition.hpp index 9b0cb21..4724fe7 100644 --- a/base_matrix/base_matrix_lu_decomposition.hpp +++ b/base_matrix/base_matrix_lu_decomposition.hpp @@ -1,3 +1,14 @@ +/** + * @file base_matrix_lu_decomposition.hpp + * @brief Provides LU decomposition functionality for square matrices. + * + * This file defines the LUDecomposition class template within the Base::Matrix + * namespace. The LUDecomposition class implements LU decomposition for square + * matrices of fixed size, supporting operations such as matrix decomposition, + * solving linear systems, and computing determinants. The class is templated on + * the matrix element type and matrix size, and supports copy and move + * semantics. + */ #ifndef __BASE_MATRIX_LU_DECOMPOSITION_HPP__ #define __BASE_MATRIX_LU_DECOMPOSITION_HPP__ @@ -14,6 +25,16 @@ namespace Matrix { constexpr double DEFAULT_DIVISION_MIN_LU_DECOMPOSITION = 1.0e-10; +/** + * @brief LU decomposition class for square matrices. + * + * This class implements LU decomposition for square matrices of fixed size M. + * It provides methods to decompose a matrix into lower and upper triangular + * matrices, solve linear systems, and compute determinants. + * + * @tparam T The type of the matrix elements (e.g., float, double). + * @tparam M The size of the square matrix (M x M). + */ template class LUDecomposition { public: LUDecomposition() @@ -56,11 +77,36 @@ template class LUDecomposition { return *this; } +public: /* Function */ + + /** + * @brief Returns the lower triangular matrix (L) from the LU decomposition. + * + * @return Matrix The lower triangular matrix of the decomposition. + */ inline Matrix get_L() const { return _Lower; } + /** + * @brief Returns the upper triangular matrix (U) from the LU decomposition. + * + * @return Matrix The upper triangular matrix of the decomposition. + */ inline Matrix get_U() const { return _Upper; } + /** + * @brief Solves the linear system Ax = b using LU decomposition. + * + * This function performs LU decomposition of the input matrix A, applies the + * pivoting to the right-hand side vector b, and then solves the resulting + * lower and upper triangular systems using forward and backward substitution. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The dimension of the square matrix and vectors. + * @param A The coefficient matrix (assumed to be square of size MxM). + * @param b The right-hand side vector. + * @return Vector The solution vector x such that Ax = b. + */ inline Vector solve(const Matrix &A, const Vector &b) { this->_decompose(A); @@ -73,8 +119,26 @@ template class LUDecomposition { return this->_backward_substitution(y); } + /** + * @brief Solves the linear system Ax = b using LU decomposition. + * + * This function performs LU decomposition of the input matrix A and solves + * the linear system Ax = b using the decomposed matrices. + * + * @param A The coefficient matrix (assumed to be square of size MxM). + * @return Vector The solution vector x such that Ax = b. + */ inline void solve(const Matrix &A) { this->_decompose(A); } + /** + * @brief Computes the determinant of the matrix from the LU decomposition. + * + * This function calculates the determinant of the matrix by multiplying the + * diagonal elements of the lower and upper triangular matrices obtained from + * LU decomposition. + * + * @return T The determinant of the matrix. + */ inline T get_determinant() const { T det = static_cast(1); @@ -95,7 +159,19 @@ template class LUDecomposition { Matrix _Upper; Vector _pivot_index_vec; +protected: /* Function */ + + /** + * @brief Decomposes the input matrix into lower and upper triangular + * matrices. + * + * This function performs LU decomposition of the input square matrix, storing + * the results in the _Lower and _Upper member variables. It also handles + * pivoting to ensure numerical stability. + * + * @param matrix The input square matrix to be decomposed. + */ inline void _decompose(const Matrix &matrix) { this->_Lower = Matrix(); this->_Upper = matrix; @@ -138,6 +214,16 @@ template class LUDecomposition { } } + /** + * @brief Performs forward substitution to solve the lower triangular system. + * + * This function computes the intermediate vector y by solving the lower + * triangular system Ly = b, where L is the lower triangular matrix from LU + * decomposition and b is the right-hand side vector. + * + * @param b The right-hand side vector. + * @return Vector The intermediate vector y. + */ inline Vector _forward_substitution(const Vector &b) const { Vector y; for (std::size_t i = 0; i < M; ++i) { @@ -150,6 +236,17 @@ template class LUDecomposition { return y; } + /** + * @brief Performs backward substitution to solve the upper triangular system. + * + * This function computes the solution vector x by solving the upper + * triangular system Ux = y, where U is the upper triangular matrix from LU + * decomposition and y is the intermediate vector obtained from forward + * substitution. + * + * @param y The intermediate vector obtained from forward substitution. + * @return Vector The solution vector x. + */ inline Vector _backward_substitution(const Vector &y) const { Vector x; for (std::size_t i = M; i-- > 0;) { From 70d10088a5689b94e1623e402f512f8ed8136cb0 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 22:19:09 +0900 Subject: [PATCH 12/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_matrix.hpp | 1969 ++++++++++++++++++++++++++++ 1 file changed, 1969 insertions(+) diff --git a/base_matrix/base_matrix_matrix.hpp b/base_matrix/base_matrix_matrix.hpp index 5684a79..8e14c46 100644 --- a/base_matrix/base_matrix_matrix.hpp +++ b/base_matrix/base_matrix_matrix.hpp @@ -1,3 +1,23 @@ +/** + * @file base_matrix_matrix.hpp + * @brief Provides a generic, fixed-size or dynamic matrix class and a suite of + * matrix operations for numerical computing. + * + * This header defines the Base::Matrix::Matrix class template, which supports + * both static (std::array) and dynamic (std::vector) storage for matrices of + * arbitrary type and size. It includes a comprehensive set of matrix operations + * such as addition, subtraction, scalar multiplication, matrix multiplication, + * transpose, trace, row/column swapping, and conversions between real and + * complex matrices. The implementation leverages template metaprogramming for + * compile-time recursion as well as runtime for-loop alternatives, controlled + * by macros. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_MATRIX_HPP__ #define __BASE_MATRIX_MATRIX_HPP__ @@ -17,8 +37,21 @@ namespace Base { namespace Matrix { +/** + * @brief A fixed-size Matrix class template supporting various storage + backends. + * + * This Matrix class supports both std::vector and std::array as underlying + storage, + * controlled by the preprocessor macro __BASE_MATRIX_USE_STD_VECTOR__. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template class Matrix { public: + /* Constructor */ #ifdef __BASE_MATRIX_USE_STD_VECTOR__ Matrix() : data(N, std::vector(M, static_cast(0))) {} @@ -112,11 +145,22 @@ template class Matrix { return *this; } +public: /* Function */ + /* Identity */ + // P_idx < P template struct CreateIdentityCore { + /** + * @brief Recursively sets the diagonal element of the identity matrix. + * + * This function sets the diagonal element at position (P_idx, P_idx) to 1 + * and recursively calls itself to set the next diagonal element. + * + * @param identity The identity matrix being constructed. + */ static void compute(Matrix &identity) { identity.template set(static_cast(1)); @@ -126,17 +170,43 @@ template class Matrix { // Termination condition: P_idx == 0 template struct CreateIdentityCore { + /** + * @brief Sets the first diagonal element of the identity matrix to 1. + * + * This function is the base case for the recursive identity matrix + * construction, setting the element at (0, 0) to 1. + * + * @param identity The identity matrix being constructed. + */ static void compute(Matrix &identity) { identity.template set<0, 0>(static_cast(1)); } }; + /** + * @brief Constructs an identity matrix of size P x P. + * + * This function uses template metaprogramming to recursively set the diagonal + * elements of the identity matrix to 1. + * + * @tparam U The type of the matrix elements. + * @tparam P The size of the identity matrix (P x P). + * @param identity The identity matrix to be constructed. + */ template static inline void COMPILED_MATRIX_IDENTITY(Matrix &identity) { CreateIdentityCore::compute(identity); } + /** + * @brief Creates an identity matrix of size M x M. + * + * This function constructs an identity matrix where all diagonal elements are + * set to 1 and all off-diagonal elements are set to 0. + * + * @return Matrix The identity matrix of size M x M. + */ static inline Matrix identity() { Matrix identity; @@ -160,6 +230,15 @@ template class Matrix { template struct MatrixFullColumn { + /** + * @brief Recursively sets the elements of a full matrix. + * + * This function sets the element at position (I, J_idx) to the specified + * value and recursively calls itself to set the next element in the column. + * + * @param Full The full matrix being constructed. + * @param value The value to set in the matrix. + */ static void compute(Matrix &Full, const U &value) { Full.template set(value); @@ -170,6 +249,15 @@ template class Matrix { // column recursion termination template struct MatrixFullColumn { + /** + * @brief Sets the first element of a full matrix column. + * + * This function is the base case for the recursive column setting, + * setting the element at position (I, 0) to the specified value. + * + * @param Full The full matrix being constructed. + * @param value The value to set in the matrix. + */ static void compute(Matrix &Full, const U &value) { Full.template set(value); @@ -179,6 +267,15 @@ template class Matrix { // when I_idx < M template struct MatrixFullRow { + /** + * @brief Recursively sets the elements of a full matrix row. + * + * This function sets the elements of the row at index I_idx to the + * specified value by calling MatrixFullColumn for each column. + * + * @param Full The full matrix being constructed. + * @param value The value to set in the matrix. + */ static void compute(Matrix &Full, const U &value) { MatrixFullColumn::compute(Full, value); MatrixFullRow::compute(Full, value); @@ -188,17 +285,48 @@ template class Matrix { // row recursion termination template struct MatrixFullRow { + /** + * @brief Sets the first row of a full matrix. + * + * This function is the base case for the recursive row setting, + * setting all elements in the first row to the specified value. + * + * @param Full The full matrix being constructed. + * @param value The value to set in the matrix. + */ static void compute(Matrix &Full, const U &value) { MatrixFullColumn::compute(Full, value); } }; + /** + * @brief Constructs a full matrix of size O x P with all elements set to a + * specified value. + * + * This function uses template metaprogramming to recursively set all elements + * of the matrix to the given value. + * + * @tparam U The type of the matrix elements. + * @tparam O The number of columns in the matrix. + * @tparam P The number of rows in the matrix. + * @param Full The full matrix to be constructed. + * @param value The value to set in the matrix. + */ template static inline void COMPILED_MATRIX_FULL(Matrix &Full, const U &value) { MatrixFullRow::compute(Full, value); } + /** + * @brief Creates a full matrix of size M x N with all elements set to 1. + * + * This function constructs a matrix where all elements are initialized to + * the value 1. + * + * @return Matrix The full matrix of size M x N with all elements set + * to 1. + */ static inline Matrix ones() { Matrix Ones; @@ -219,6 +347,17 @@ template class Matrix { return Ones; } + /** + * @brief Creates a full matrix of size M x N with all elements set to a + * specified value. + * + * This function constructs a matrix where all elements are initialized to the + * given value. + * + * @param value The value to set in all elements of the matrix. + * @return Matrix The full matrix of size M x N with all elements set + * to the specified value. + */ static inline Matrix full(const T &value) { Matrix Full; @@ -239,6 +378,15 @@ template class Matrix { return Full; } + /** + * @brief Creates a row vector from a specified row of the matrix. + * + * This function extracts a row from the matrix and returns it as a Vector + * object. + * + * @param row The index of the row to extract (0-based). + * @return Vector The extracted row vector. + */ inline Vector create_row_vector(std::size_t row) const { Vector result; @@ -251,11 +399,30 @@ template class Matrix { return result; } + /** + * @brief Creates a column vector from a specified column of the matrix. + * + * This function extracts a column from the matrix and returns it as a Vector + * object. + * + * @param col The index of the column to extract (0-based). + * @return Vector The extracted column vector. + */ T &operator()(std::size_t col, std::size_t row) { return this->data[row][col]; } + /** + * @brief Accesses an element at the specified column and row indices. + * + * This function provides access to the element at the specified column and + * row indices, allowing both read and write operations. + * + * @param col The index of the column (0-based). + * @param row The index of the row (0-based). + * @return T& Reference to the element at the specified position. + */ const T &operator()(std::size_t col, std::size_t row) const { return this->data[row][col]; @@ -263,6 +430,17 @@ template class Matrix { #ifdef __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Accesses a row of the matrix. + * + * This function provides access to a specific row of the matrix, returning a + * reference to the vector representing that row. If the specified row index + * is out of bounds, it defaults to the last row. + * + * @param row The index of the row to access (0-based). + * @return std::vector& Reference to the vector representing the specified + * row. + */ std::vector &operator()(std::size_t row) { if (row >= N) { row = N - 1; @@ -271,6 +449,17 @@ template class Matrix { return this->data[row]; } + /** + * @brief Accesses a row of the matrix (const version). + * + * This function provides access to a specific row of the matrix, returning a + * const reference to the vector representing that row. If the specified row + * index is out of bounds, it defaults to the last row. + * + * @param row The index of the row to access (0-based). + * @return const std::vector& Const reference to the vector representing + * the specified row. + */ const std::vector &operator()(std::size_t row) const { if (row >= N) { row = N - 1; @@ -281,6 +470,17 @@ template class Matrix { #else // __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Accesses a row of the matrix. + * + * This function provides access to a specific row of the matrix, returning a + * reference to the array representing that row. If the specified row index is + * out of bounds, it defaults to the last row. + * + * @param row The index of the row to access (0-based). + * @return std::array& Reference to the array representing the specified + * row. + */ std::array &operator()(std::size_t row) { if (row >= N) { row = N - 1; @@ -289,6 +489,17 @@ template class Matrix { return this->data[row]; } + /** + * @brief Accesses a row of the matrix (const version). + * + * This function provides access to a specific row of the matrix, returning a + * const reference to the array representing that row. If the specified row + * index is out of bounds, it defaults to the last row. + * + * @param row The index of the row to access (0-based). + * @return const std::array& Const reference to the array representing + * the specified row. + */ const std::array &operator()(std::size_t row) const { if (row >= N) { row = N - 1; @@ -299,10 +510,34 @@ template class Matrix { #endif // __BASE_MATRIX_USE_STD_VECTOR__ + /** + * @brief Returns the number of rows in the matrix. + * + * This function returns the number of rows in the matrix, which is a + * compile-time constant. + * + * @return std::size_t The number of rows in the matrix. + */ constexpr std::size_t rows() const { return N; } + /** + * @brief Returns the number of columns in the matrix. + * + * This function returns the number of columns in the matrix, which is a + * compile-time constant. + * + * @return std::size_t The number of columns in the matrix. + */ constexpr std::size_t cols() const { return M; } + /** + * @brief Returns the number of elements in the matrix. + * + * This function returns the total number of elements in the matrix, which is + * the product of the number of rows and columns. + * + * @return std::size_t The total number of elements in the matrix. + */ inline Vector get_row(std::size_t row) const { if (row >= N) { row = N - 1; @@ -315,6 +550,16 @@ template class Matrix { return result; } + /** + * @brief Sets a specific row of the matrix to the values from a Vector. + * + * This function copies the values from the provided Vector into the specified + * row of the matrix. If the row index is out of bounds, it defaults to the + * last row. + * + * @param row The index of the row to set (0-based). + * @param row_vector The Vector containing the values to set in the row. + */ inline void set_row(std::size_t row, const Vector &row_vector) { if (row >= N) { row = N - 1; @@ -323,6 +568,14 @@ template class Matrix { Base::Utility::copy(row_vector.data, this->data[row]); } + /** + * @brief Returns the number of elements in the matrix. + * + * This function returns the total number of elements in the matrix, which is + * the product of the number of rows and columns. + * + * @return std::size_t The total number of elements in the matrix. + */ inline Matrix inv() const { Matrix X_temp = Matrix::identity(); std::array rho; @@ -336,6 +589,18 @@ template class Matrix { } /* Get Dense Matrix value */ + + /** + * @brief Gets the value at the specified column and row indices. + * + * This function retrieves the value at the specified column and row indices + * from the matrix. It performs compile-time checks to ensure that the indices + * are within bounds. + * + * @tparam COL The index of the column (0-based). + * @tparam ROW The index of the row (0-based). + * @return T The value at the specified position in the matrix. + */ template inline T get() const { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < N, "Row Index is out of range."); @@ -344,6 +609,18 @@ template class Matrix { } /* Set Dense Matrix value */ + + /** + * @brief Sets the value at the specified column and row indices. + * + * This function assigns a value to the specified column and row indices in + * the matrix. It performs compile-time checks to ensure that the indices are + * within bounds. + * + * @tparam COL The index of the column (0-based). + * @tparam ROW The index of the row (0-based). + * @param value The value to set at the specified position in the matrix. + */ template inline void set(const T &value) { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < N, "Row Index is out of range."); @@ -351,6 +628,15 @@ template class Matrix { data[ROW][COL] = value; } + /** + * @brief Gets the trace of the matrix. + * + * This function calculates the trace of the matrix, which is the sum of the + * diagonal elements. It performs a compile-time check to ensure that the + * matrix is square (M == N). + * + * @return T The trace of the matrix. + */ inline T get_trace() const { return output_matrix_trace(*this); } /* Variable */ @@ -367,6 +653,18 @@ namespace MatrixSwapColumns { // Swap N_idx < N template struct Core { + /** + * @brief Recursively swaps two columns in the matrix. + * + * This function swaps the elements of the specified columns in the matrix + * for the current row index N_idx and recursively calls itself for the next + * row index. + * + * @param col_1 The index of the first column to swap. + * @param col_2 The index of the second column to swap. + * @param mat The matrix in which the columns are swapped. + * @param temp A temporary variable to hold values during swapping. + */ static void compute(std::size_t col_1, std::size_t col_2, Matrix &mat, T temp) { @@ -379,6 +677,17 @@ struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Swaps two columns in the first row of the matrix. + * + * This function swaps the elements of the specified columns in the first row + * of the matrix. + * + * @param col_1 The index of the first column to swap. + * @param col_2 The index of the second column to swap. + * @param mat The matrix in which the columns are swapped. + * @param temp A temporary variable to hold values during swapping. + */ static void compute(std::size_t col_1, std::size_t col_2, Matrix &mat, T temp) { @@ -388,6 +697,20 @@ template struct Core { } }; +/** + * @brief Computes the column swap operation for a matrix. + * + * This function uses template metaprogramming to recursively swap two columns + * in the matrix, starting from the last row and moving upwards. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param col_1 The index of the first column to swap. + * @param col_2 The index of the second column to swap. + * @param mat The matrix in which the columns are swapped. + * @param temp A temporary variable to hold values during swapping. + */ template inline void compute(std::size_t col_1, std::size_t col_2, Matrix &mat, T &temp) { @@ -396,6 +719,20 @@ inline void compute(std::size_t col_1, std::size_t col_2, Matrix &mat, } // namespace MatrixSwapColumns +/** + * @brief Swaps two columns in a matrix. + * + * This function swaps the elements of two specified columns in the matrix. + * If the column indices are out of bounds, they are adjusted to the last valid + * index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param col_1 The index of the first column to swap (0-based). + * @param col_2 The index of the second column to swap (0-based). + * @param mat The matrix in which the columns are swapped. + */ template inline void matrix_col_swap(std::size_t col_1, std::size_t col_2, Matrix &mat) { @@ -425,6 +762,21 @@ inline void matrix_col_swap(std::size_t col_1, std::size_t col_2, } /* swap rows */ + +/** + * @brief Swaps two rows in a matrix. + * + * This function swaps the elements of two specified rows in the matrix. + * If the row indices are out of bounds, they are adjusted to the last valid + * index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param row_1 The index of the first row to swap (0-based). + * @param row_2 The index of the second row to swap (0-based). + * @param mat The matrix in which the rows are swapped. + */ template inline void matrix_row_swap(std::size_t row_1, std::size_t row_2, Matrix &mat) { @@ -447,6 +799,19 @@ namespace MatrixTrace { // calculate trace of matrix template struct Core { + /** + * @brief Recursively computes the trace of a square matrix. + * + * This function calculates the trace by summing the diagonal elements + * of the matrix. It uses template metaprogramming to recursively access the + * diagonal elements based on the current index I. + * + * @tparam T The type of the matrix elements. + * @tparam N The size of the square matrix (N x N). + * @tparam I The current index in the recursion (starting from N - 1). + * @param mat The square matrix from which the trace is computed. + * @return T The computed trace of the matrix. + */ static T compute(const Matrix &mat) { return mat.template get() + Core::compute(mat); } @@ -454,11 +819,33 @@ template struct Core { // if I == 0 template struct Core { + /** + * @brief Base case for the trace computation. + * + * This function returns the first diagonal element of the matrix when the + * recursion reaches the base case (I == 0). + * + * @tparam T The type of the matrix elements. + * @tparam N The size of the square matrix (N x N). + * @return T The first diagonal element of the matrix. + */ static T compute(const Matrix &mat) { return mat.template get<0, 0>(); } }; +/** + * @brief Computes the trace of a square matrix. + * + * This function uses template metaprogramming to recursively calculate the + * trace of a square matrix by summing its diagonal elements. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix (must be equal to N). + * @tparam N The number of rows in the matrix (must be equal to M). + * @param mat The square matrix from which the trace is computed. + * @return T The computed trace of the matrix. + */ template inline T compute(const Matrix &mat) { return Core::compute(mat); @@ -466,6 +853,19 @@ inline T compute(const Matrix &mat) { } // namespace MatrixTrace +/** + * @brief Computes the trace of a square matrix. + * + * This function calculates the trace of a square matrix by summing its diagonal + * elements. It performs a compile-time check to ensure that the matrix is + * square (M == N). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix (must be equal to N). + * @tparam N The number of rows in the matrix (must be equal to M). + * @param mat The square matrix from which the trace is computed. + * @return T The computed trace of the matrix. + */ template inline T output_matrix_trace(const Matrix &mat) { static_assert(M == N, "Matrix must be square matrix"); @@ -493,6 +893,23 @@ namespace MatrixAddMatrix { template struct Column { + /** + * @brief Recursively computes the sum of two matrices. + * + * This function adds the elements of two matrices at the specified indices + * and stores the result in the result matrix. It uses template + * metaprogramming to recursively access the elements based on the current + * column index J_idx. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @tparam J_idx The current column index in the recursion. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -505,6 +922,21 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column addition. + * + * This function adds the first element of the specified row in both matrices + * and stores the result in the result matrix when the recursion reaches the + * base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -515,6 +947,20 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the sum of two matrices row by row. + * + * This function adds the elements of two matrices for the current row index + * I_idx and recursively calls itself for the next row index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the recursion. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); @@ -524,12 +970,39 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row addition. + * + * This function adds the elements of the first row in both matrices and + * stores the result in the result matrix when the recursion reaches the base + * case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); } }; +/** + * @brief Computes the sum of two matrices. + * + * This function uses template metaprogramming to recursively add the elements + * of two matrices and store the result in a third matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @param result The matrix where the result is stored. + */ template inline void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -538,6 +1011,19 @@ inline void compute(const Matrix &A, const Matrix &B, } // namespace MatrixAddMatrix +/** + * @brief Adds two matrices of the same size. + * + * This function computes the element-wise sum of two matrices A and B, both of + * size M x N, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @return Matrix The resulting matrix after addition. + */ template inline Matrix operator+(const Matrix &A, const Matrix &B) { @@ -567,6 +1053,23 @@ namespace MatrixSubMatrix { template struct Column { + /** + * @brief Recursively computes the difference of two matrices. + * + * This function subtracts the elements of two matrices at the specified + * indices and stores the result in the result matrix. It uses template + * metaprogramming to recursively access the elements based on the current + * column index J_idx. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @tparam J_idx The current column index in the recursion. + * @param A The first matrix to subtract. + * @param B The second matrix to subtract. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -579,6 +1082,21 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column subtraction. + * + * This function subtracts the first element of the specified row in both + * matrices and stores the result in the result matrix when the recursion + * reaches the base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @param A The first matrix to subtract. + * @param B The second matrix to subtract. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -589,6 +1107,20 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the difference of two matrices row by row. + * + * This function subtracts the elements of two matrices for the current row + * index I_idx and recursively calls itself for the next row index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the recursion. + * @param A The first matrix to subtract. + * @param B The second matrix to subtract. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); @@ -598,12 +1130,39 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row subtraction. + * + * This function subtracts the elements of the first row in both matrices and + * stores the result in the result matrix when the recursion reaches the base + * case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The first matrix to subtract. + * @param B The second matrix to subtract. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); } }; +/** + * @brief Computes the difference of two matrices. + * + * This function uses template metaprogramming to recursively subtract the + * elements of two matrices and store the result in a third matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The first matrix to subtract. + * @param B The second matrix to subtract. + * @param result The matrix where the result is stored. + */ template inline void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -612,6 +1171,19 @@ inline void compute(const Matrix &A, const Matrix &B, } // namespace MatrixSubMatrix +/** + * @brief Subtracts two matrices of the same size. + * + * This function computes the element-wise difference of two matrices A and B, + * both of size M x N, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The first matrix to subtract. + * @param B The second matrix to subtract. + * @return Matrix The resulting matrix after subtraction. + */ template inline Matrix operator-(const Matrix &A, const Matrix &B) { @@ -640,6 +1212,22 @@ namespace MatrixMinus { template struct Column { + /** + * @brief Recursively computes the negation of a matrix. + * + * This function negates the elements of a matrix at the specified indices + * and stores the result in the result matrix. It uses template + * metaprogramming to recursively access the elements based on the current + * column index J_idx. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @tparam J_idx The current column index in the recursion. + * @param A The matrix to negate. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, Matrix &result) { result.template set(-A.template get()); @@ -650,6 +1238,20 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column negation. + * + * This function negates the first element of the specified row in the matrix + * and stores the result in the result matrix when the recursion reaches the + * base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @param A The matrix to negate. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, Matrix &result) { result.template set(-A.template get()); @@ -659,6 +1261,19 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the negation of a matrix row by row. + * + * This function negates the elements of a matrix for the current row index + * I_idx and recursively calls itself for the next row index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the recursion. + * @param A The matrix to negate. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, Matrix &result) { Column::compute(A, result); Row::compute(A, result); @@ -667,11 +1282,36 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row negation. + * + * This function negates the elements of the first row in the matrix and + * stores the result in the result matrix when the recursion reaches the base + * case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The matrix to negate. + * @param result The matrix where the result is stored. + */ static void compute(const Matrix &A, Matrix &result) { Column::compute(A, result); } }; +/** + * @brief Computes the negation of a matrix. + * + * This function uses template metaprogramming to recursively negate the + * elements of a matrix and store the result in another matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The matrix to negate. + * @param result The matrix where the result is stored. + */ template inline void compute(const Matrix &A, Matrix &result) { Row::compute(A, result); @@ -679,6 +1319,18 @@ inline void compute(const Matrix &A, Matrix &result) { } // namespace MatrixMinus +/** + * @brief Negates a matrix. + * + * This function computes the negation of a matrix A, which means it multiplies + * each element of the matrix by -1, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The matrix to negate. + * @return Matrix The resulting negated matrix. + */ template inline Matrix operator-(const Matrix &A) { Matrix result; @@ -707,6 +1359,23 @@ namespace MatrixMultiplyScalar { template struct Column { + /** + * @brief Recursively computes the product of a scalar and a matrix. + * + * This function multiplies the elements of a matrix by a scalar at the + * specified indices and stores the result in the result matrix. It uses + * template metaprogramming to recursively access the elements based on the + * current column index J_idx. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @tparam J_idx The current column index in the recursion. + * @param scalar The scalar value to multiply with. + * @param mat The matrix to multiply with the scalar. + * @param result The matrix where the result is stored. + */ static void compute(const T &scalar, const Matrix &mat, Matrix &result) { @@ -718,6 +1387,21 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column multiplication. + * + * This function multiplies the first element of the specified row in the + * matrix by the scalar and stores the result in the result matrix when the + * recursion reaches the base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @param scalar The scalar value to multiply with. + * @param mat The matrix to multiply with the scalar. + * @param result The matrix where the result is stored. + */ static void compute(const T &scalar, const Matrix &mat, Matrix &result) { @@ -728,6 +1412,21 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the product of a scalar and a matrix row by + * row. + * + * This function multiplies the elements of a matrix for the current row index + * I_idx by a scalar and recursively calls itself for the next row index. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the recursion. + * @param scalar The scalar value to multiply with. + * @param mat The matrix to multiply with the scalar. + * @param result The matrix where the result is stored. + */ static void compute(const T &scalar, const Matrix &mat, Matrix &result) { Column::compute(scalar, mat, result); @@ -737,12 +1436,39 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row multiplication. + * + * This function multiplies the elements of the first row in the matrix by + * the scalar and stores the result in the result matrix when the recursion + * reaches the base case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param scalar The scalar value to multiply with. + * @param mat The matrix to multiply with the scalar. + * @param result The matrix where the result is stored. + */ static void compute(const T &scalar, const Matrix &mat, Matrix &result) { Column::compute(scalar, mat, result); } }; +/** + * @brief Computes the product of a scalar and a matrix. + * + * This function uses template metaprogramming to recursively multiply the + * elements of a matrix by a scalar and store the result in another matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param scalar The scalar value to multiply with. + * @param mat The matrix to multiply with the scalar. + * @param result The matrix where the result is stored. + */ template inline void compute(const T &scalar, const Matrix &mat, Matrix &result) { @@ -751,6 +1477,20 @@ inline void compute(const T &scalar, const Matrix &mat, } // namespace MatrixMultiplyScalar +/** + * @brief Multiplies a scalar with a matrix. + * + * This function computes the product of a scalar and a matrix, which means it + * multiplies each element of the matrix by the scalar, and returns the + * resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param scalar The scalar value to multiply with. + * @param mat The matrix to multiply with the scalar. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix operator*(const T &scalar, const Matrix &mat) { Matrix result; @@ -772,6 +1512,20 @@ inline Matrix operator*(const T &scalar, const Matrix &mat) { return result; } +/** + * @brief Multiplies a matrix with a scalar. + * + * This function computes the product of a matrix and a scalar, which means it + * multiplies each element of the matrix by the scalar, and returns the + * resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param mat The matrix to multiply with the scalar. + * @param scalar The scalar value to multiply with. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix operator*(const Matrix &mat, const T &scalar) { Matrix result; @@ -800,6 +1554,22 @@ namespace MatrixMultiplyVector { template struct Core { + /** + * @brief Recursively computes the dot product of a matrix row and a vector. + * + * This function computes the dot product of the I-th row of the matrix with + * the vector at index J and recursively calls itself for the next column + * index J - 1. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @tparam J The current column index in the recursion. + * @param mat The matrix to multiply with the vector. + * @param vec The vector to multiply with the matrix. + * @return T The computed dot product for the specified row and column. + */ static T compute(const Matrix &mat, const Vector &vec) { return mat.template get() * vec[J] + @@ -817,6 +1587,20 @@ struct Core { // column recursion template struct Row { + /** + * @brief Recursively computes the dot product of a matrix row and a vector. + * + * This function computes the dot product for the I-th row of the matrix and + * recursively calls itself for the next row index I - 1. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the recursion. + * @param mat The matrix to multiply with the vector. + * @param vec The vector to multiply with the matrix. + * @param result The vector where the result is stored. + */ static void compute(const Matrix &mat, const Vector &vec, Vector &result) { result[I] = Core::compute(mat, vec); @@ -826,12 +1610,40 @@ template struct Row { // if I == 0 template struct Row { + /** + * @brief Base case for the row multiplication. + * + * This function computes the dot product for the first row of the matrix and + * stores the result in the result vector when the recursion reaches the base + * case (I == 0). + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param mat The matrix to multiply with the vector. + * @param vec The vector to multiply with the matrix. + * @param result The vector where the result is stored. + */ static void compute(const Matrix &mat, const Vector &vec, Vector &result) { result[0] = Core::compute(mat, vec); } }; +/** + * @brief Computes the product of a matrix and a vector. + * + * This function uses template metaprogramming to recursively compute the dot + * product of each row of the matrix with the vector and store the results in a + * new vector. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param mat The matrix to multiply with the vector. + * @param vec The vector to multiply with the matrix. + * @param result The vector where the result is stored. + */ template inline void compute(const Matrix &mat, const Vector &vec, Vector &result) { @@ -840,6 +1652,20 @@ inline void compute(const Matrix &mat, const Vector &vec, } // namespace MatrixMultiplyVector +/** + * @brief Multiplies a matrix with a vector. + * + * This function computes the product of a matrix and a vector, which means it + * performs the dot product of each row of the matrix with the vector, and + * returns the resulting vector. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param mat The matrix to multiply with the vector. + * @param vec The vector to multiply with the matrix. + * @return Vector The resulting vector after multiplication. + */ template inline Vector operator*(const Matrix &mat, const Vector &vec) { @@ -870,6 +1696,23 @@ namespace VectorMultiplyMatrix { template struct Core { + /** + * @brief Recursively computes the product of a vector element and a matrix + * column. + * + * This function multiplies the K_idx-th element of the vector with the J-th + * column of the matrix and recursively calls itself for the next index K_idx + * - 1. + * + * @tparam T The type of the vector and matrix elements. + * @tparam L The size of the vector. + * @tparam N The number of rows in the matrix. + * @tparam J The current column index in the recursion. + * @tparam K_idx The current index in the vector. + * @param vec The vector to multiply with the matrix. + * @param mat The matrix to multiply with the vector. + * @param result The matrix where the result is stored. + */ static void compute(const Vector &vec, const Matrix &mat, Matrix &result) { @@ -881,6 +1724,21 @@ struct Core { // if K_idx = 0 template struct Core { + /** + * @brief Base case for the vector-matrix multiplication. + * + * This function multiplies the first element of the vector with the J-th + * column of the matrix and stores the result in the result matrix when the + * recursion reaches the base case (K_idx == 0). + * + * @tparam T The type of the vector and matrix elements. + * @tparam L The size of the vector. + * @tparam N The number of rows in the matrix. + * @tparam J The current column index in the recursion. + * @param vec The vector to multiply with the matrix. + * @param mat The matrix to multiply with the vector. + * @param result The matrix where the result is stored. + */ static void compute(const Vector &vec, const Matrix &mat, Matrix &result) { result.template set<0, J>(vec[0] * mat.template get<0, J>()); @@ -890,6 +1748,20 @@ struct Core { // row recursion template struct Column { + /** + * @brief Recursively computes the product of a vector and a matrix column. + * + * This function computes the product for the J-th column of the matrix and + * recursively calls itself for the next column index J - 1. + * + * @tparam T The type of the vector and matrix elements. + * @tparam L The size of the vector. + * @tparam N The number of rows in the matrix. + * @tparam J The current column index in the recursion. + * @param vec The vector to multiply with the matrix. + * @param mat The matrix to multiply with the vector. + * @param result The matrix where the result is stored. + */ static void compute(const Vector &vec, const Matrix &mat, Matrix &result) { Core::compute(vec, mat, result); @@ -899,12 +1771,41 @@ struct Column { // if J = 0 template struct Column { + /** + * @brief Base case for the column multiplication. + * + * This function computes the product for the first column of the matrix and + * stores the result in the result matrix when the recursion reaches the base + * case (J == 0). + * + * @tparam T The type of the vector and matrix elements. + * @tparam L The size of the vector. + * @tparam N The number of rows in the matrix. + * @param vec The vector to multiply with the matrix. + * @param mat The matrix to multiply with the vector. + * @param result The matrix where the result is stored. + */ static void compute(const Vector &vec, const Matrix &mat, Matrix &result) { Core::compute(vec, mat, result); } }; +/** + * @brief Computes the product of a vector and a matrix. + * + * This function uses template metaprogramming to recursively compute the + * product of each element of the vector with the corresponding column of the + * matrix and store the results in a new matrix. + * + * @tparam T The type of the vector and matrix elements. + * @tparam L The size of the vector. + * @tparam M The number of rows in the matrix (should be 1). + * @tparam N The number of columns in the matrix. + * @param vec The vector to multiply with the matrix. + * @param mat The matrix to multiply with the vector. + * @param result The matrix where the result is stored. + */ template inline void compute(const Vector &vec, const Matrix &mat, Matrix &result) { @@ -913,6 +1814,21 @@ inline void compute(const Vector &vec, const Matrix &mat, } // namespace VectorMultiplyMatrix +/** + * @brief Multiplies a vector with a matrix. + * + * This function computes the product of a vector and a matrix, which means it + * multiplies each element of the vector with the corresponding column of the + * matrix, and returns the resulting matrix. + * + * @tparam T The type of the vector and matrix elements. + * @tparam L The size of the vector. + * @tparam M The number of columns in the matrix (should be 1). + * @tparam N The number of rows in the matrix. + * @param vec The vector to multiply with the matrix. + * @param mat The matrix to multiply with the vector. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix operator*(const Vector &vec, const Matrix &mat) { @@ -943,6 +1859,23 @@ namespace ColumnVectorMultiplyMatrix { template struct Core { + /** + * @brief Recursively computes the product of a column vector element and a + * matrix row. + * + * This function multiplies the I-th element of the column vector with the + * J-th row of the matrix and recursively calls itself for the next index I + * - 1. + * + * @tparam T The type of the column vector and matrix elements. + * @tparam M The size of the column vector. + * @tparam N The number of rows in the matrix. + * @tparam J The current row index in the recursion. + * @tparam I The current index in the column vector. + * @param vec The column vector to multiply with the matrix. + * @param mat The matrix to multiply with the column vector. + * @return T The computed product for the specified row and index. + */ static T compute(const ColVector &vec, const Matrix &mat) { return vec[I] * mat.template get() + Core::compute(vec, mat); @@ -952,6 +1885,21 @@ struct Core { // if I = 0 template struct Core { + /** + * @brief Base case for the column vector-matrix multiplication. + * + * This function multiplies the first element of the column vector with the + * J-th row of the matrix and returns the result when the recursion reaches + * the base case (I == 0). + * + * @tparam T The type of the column vector and matrix elements. + * @tparam M The size of the column vector. + * @tparam N The number of rows in the matrix. + * @tparam J The current row index in the recursion. + * @param vec The column vector to multiply with the matrix. + * @param mat The matrix to multiply with the column vector. + * @return T The computed product for the specified row and index. + */ static T compute(const ColVector &vec, const Matrix &mat) { return vec[0] * mat.template get<0, J>(); } @@ -960,6 +1908,21 @@ struct Core { // row recursion template struct Column { + /** + * @brief Recursively computes the product of a column vector and a matrix + * row by row. + * + * This function computes the product for the J-th row of the matrix and + * recursively calls itself for the next row index J - 1. + * + * @tparam T The type of the column vector and matrix elements. + * @tparam M The size of the column vector. + * @tparam N The number of rows in the matrix. + * @tparam J The current row index in the recursion. + * @param vec The column vector to multiply with the matrix. + * @param mat The matrix to multiply with the column vector. + * @param result The column vector where the result is stored. + */ static void compute(const ColVector &vec, const Matrix &mat, ColVector &result) { result[J] = Core::compute(vec, mat); @@ -969,12 +1932,40 @@ struct Column { // if J = 0 template struct Column { + /** + * @brief Base case for the column multiplication. + * + * This function computes the product for the first row of the matrix and + * stores the result in the result column vector when the recursion reaches + * the base case (J == 0). + * + * @tparam T The type of the column vector and matrix elements. + * @tparam M The size of the column vector. + * @tparam N The number of rows in the matrix. + * @param vec The column vector to multiply with the matrix. + * @param mat The matrix to multiply with the column vector. + * @param result The column vector where the result is stored. + */ static void compute(const ColVector &vec, const Matrix &mat, ColVector &result) { result[0] = Core::compute(vec, mat); } }; +/** + * @brief Computes the product of a column vector and a matrix. + * + * This function uses template metaprogramming to recursively compute the + * product of each element of the column vector with the corresponding row of + * the matrix and store the results in a new column vector. + * + * @tparam T The type of the column vector and matrix elements. + * @tparam M The size of the column vector. + * @tparam N The number of rows in the matrix. + * @param vec The column vector to multiply with the matrix. + * @param mat The matrix to multiply with the column vector. + * @param result The column vector where the result is stored. + */ template inline void compute(const ColVector &vec, const Matrix &mat, ColVector &result) { @@ -983,6 +1974,20 @@ inline void compute(const ColVector &vec, const Matrix &mat, } // namespace ColumnVectorMultiplyMatrix +/** + * @brief Multiplies a column vector with a matrix. + * + * This function computes the product of a column vector and a matrix, which + * means it multiplies each element of the column vector with the corresponding + * row of the matrix, and returns the resulting column vector. + * + * @tparam T The type of the column vector and matrix elements. + * @tparam M The size of the column vector. + * @tparam N The number of rows in the matrix. + * @param vec The column vector to multiply with the matrix. + * @param mat The matrix to multiply with the column vector. + * @return ColVector The resulting column vector after multiplication. + */ template inline ColVector operator*(const ColVector &vec, const Matrix &mat) { @@ -1014,6 +2019,24 @@ namespace MatrixMultiplyMatrix { template struct Core { + /** + * @brief Recursively computes the product of two matrices. + * + * This function computes the dot product of the I-th row of matrix A and the + * J-th column of matrix B, and recursively calls itself for the next index + * K_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @tparam K_idx The current index in the multiplication. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get() * B.template get() + @@ -1025,6 +2048,23 @@ struct Core { template struct Core { + /** + * @brief Base case for the matrix multiplication. + * + * This function computes the product of the I-th row of matrix A and the + * J-th column of matrix B when the recursion reaches the base case (K_idx == + * 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get() * B.template get<0, J>(); @@ -1035,6 +2075,22 @@ struct Core { template struct Column { + /** + * @brief Recursively computes the product of two matrices column by column. + * + * This function computes the product for the J-th column of matrix B and + * recursively calls itself for the next column index J - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1047,6 +2103,22 @@ struct Column { template struct Column { + /** + * @brief Base case for the column multiplication. + * + * This function computes the product for the first column of matrix B and + * stores the result in the result matrix when the recursion reaches the base + * case (J == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1058,6 +2130,21 @@ struct Column { template struct Row { + /** + * @brief Recursively computes the product of two matrices row by row. + * + * This function computes the product for the I-th row of matrix A and + * recursively calls itself for the next row index I - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); @@ -1068,12 +2155,42 @@ struct Row { // Column recursive termination template struct Row { + /** + * @brief Base case for the row multiplication. + * + * This function computes the product for the first row of matrix A and + * stores the result in the result matrix when the recursion reaches the base + * case (I == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); } }; +/** + * @brief Computes the product of two matrices. + * + * This function uses template metaprogramming to recursively compute the + * product of each row of matrix A with each column of matrix B and store the + * results in a new matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ template inline void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1082,6 +2199,20 @@ inline void compute(const Matrix &A, const Matrix &B, } // namespace MatrixMultiplyMatrix +/** + * @brief Multiplies two matrices. + * + * This function computes the product of two matrices A and B, which means it + * performs matrix multiplication, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of rows in matrix A. + * @tparam K The number of columns in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix operator*(const Matrix &A, const Matrix &B) { @@ -1115,6 +2246,20 @@ namespace MatrixTranspose { template struct Column { + /** + * @brief Recursively computes the transpose of a matrix column by column. + * + * This function computes the J_idx-th column of the transposed matrix + * and recursively calls itself for the next column index J_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @tparam J_idx The current column index in the recursion. + * @param A The matrix to transpose. + * @param result The resulting transposed matrix where the product is stored. + */ static void compute(const Matrix &A, Matrix &result) { result.template set(A.template get()); @@ -1125,6 +2270,20 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column transposition. + * + * This function computes the first column of the transposed matrix + * and stores the result in the result matrix when the recursion reaches + * the base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @param A The matrix to transpose. + * @param result The resulting transposed matrix where the product is stored. + */ static void compute(const Matrix &A, Matrix &result) { result.template set<0, I>(A.template get()); @@ -1134,6 +2293,19 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the transpose of a matrix row by row. + * + * This function computes the I_idx-th row of the transposed matrix + * and recursively calls itself for the next row index I_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the recursion. + * @param A The matrix to transpose. + * @param result The resulting transposed matrix where the product is stored. + */ static void compute(const Matrix &A, Matrix &result) { Column::compute(A, result); Row::compute(A, result); @@ -1142,11 +2314,37 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row transposition. + * + * This function computes the first row of the transposed matrix + * and stores the result in the result matrix when the recursion reaches + * the base case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The matrix to transpose. + * @param result The resulting transposed matrix where the product is stored. + */ static void compute(const Matrix &A, Matrix &result) { Column::compute(A, result); } }; +/** + * @brief Computes the transpose of a matrix. + * + * This function uses template metaprogramming to recursively compute the + * transpose of a matrix by swapping rows and columns, and store the results in + * a new matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The matrix to transpose. + * @param result The resulting transposed matrix where the product is stored. + */ template inline void compute(const Matrix &A, Matrix &result) { Row::compute(A, result); @@ -1154,6 +2352,18 @@ inline void compute(const Matrix &A, Matrix &result) { } // namespace MatrixTranspose +/** + * @brief Transposes a matrix. + * + * This function computes the transpose of a matrix A, which means it swaps its + * rows and columns, and returns the resulting transposed matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix A. + * @tparam N The number of rows in the matrix A. + * @param mat The matrix to transpose. + * @return Matrix The resulting transposed matrix. + */ template inline Matrix output_matrix_transpose(const Matrix &mat) { Matrix result; @@ -1182,6 +2392,25 @@ namespace UpperTriangularMultiplyMatrix { template struct Core { + /** + * @brief Recursively computes the product of two matrices for upper + * triangular matrices. + * + * This function computes the dot product of the I-th row of matrix A and the + * J-th column of matrix B, and recursively calls itself for the next index + * K_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @tparam K_idx The current index in the multiplication. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return (K_idx >= I) @@ -1195,6 +2424,22 @@ struct Core { template struct Core(-1)> { + /** + * @brief Base case for the upper triangular matrix multiplication. + * + * This function returns 0 when K_idx is less than I, indicating that the + * multiplication does not contribute to the result in the upper triangular + * matrix context. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @return T The computed product for the specified row and column, which is + * 0. + */ static T compute(const Matrix &, const Matrix &) { return static_cast(0); @@ -1205,6 +2450,21 @@ struct Core(-1)> { template struct Core { + /** + * @brief Base case for the upper triangular matrix multiplication. + * + * This function computes the product of the I-th row of matrix A and the + * J-th column of matrix B when K_idx reaches I, indicating that the + * multiplication is valid in the upper triangular matrix context. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get() * B.template get(); @@ -1215,6 +2475,23 @@ struct Core { template struct Column { + /** + * @brief Recursively computes the product of two matrices for upper + * triangular matrices column by column. + * + * This function computes the product for the J-th column of matrix B and + * recursively calls itself for the next column index J - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1227,6 +2504,23 @@ struct Column { template struct Column { + /** + * @brief Base case for the column multiplication in upper triangular matrix + * context. + * + * This function computes the product for the first column of matrix B and + * stores the result in the result matrix when the recursion reaches the base + * case (J == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1238,6 +2532,22 @@ struct Column { template struct Row { + /** + * @brief Recursively computes the product of two matrices for upper + * triangular matrices row by row. + * + * This function computes the product for the I-th row of matrix A and + * recursively calls itself for the next row index I - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); @@ -1248,12 +2558,43 @@ struct Row { // column recursion termination template struct Row { + /** + * @brief Base case for the row multiplication in upper triangular matrix + * context. + * + * This function computes the product for the first row of matrix A and + * stores the result in the result matrix when the recursion reaches the base + * case (I == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); } }; +/** + * @brief Computes the product of two matrices for upper triangular matrices. + * + * This function uses template metaprogramming to recursively compute the + * product of each row of matrix A with each column of matrix B, considering + * that A is an upper triangular matrix, and store the results in a new matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @param A The first matrix to multiply (upper triangular). + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ template inline void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1262,6 +2603,21 @@ inline void compute(const Matrix &A, const Matrix &B, } // namespace UpperTriangularMultiplyMatrix +/** + * @brief Multiplies an upper triangular matrix A with a matrix B. + * + * This function computes the product of an upper triangular matrix A and a + * matrix B, which means it performs matrix multiplication while considering + * that A is upper triangular, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of rows in matrix A. + * @tparam K The number of columns in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The upper triangular matrix to multiply. + * @param B The matrix to multiply with the upper triangular matrix. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix matrix_multiply_Upper_triangular_A_mul_B(const Matrix &A, @@ -1296,6 +2652,25 @@ namespace MatrixTransposeMultiplyMatrix { template struct Core { + /** + * @brief Recursively computes the product of two matrices for matrix + * transpose multiplication. + * + * This function computes the dot product of the I-th row of matrix A and the + * J-th column of matrix B, and recursively calls itself for the next index + * K_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @tparam K_idx The current index in the multiplication. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get() * B.template get() + @@ -1307,6 +2682,21 @@ struct Core { template struct Core { + /** + * @brief Base case for the matrix transpose multiplication. + * + * This function computes the product of the first row of matrix A and the + * first column of matrix B when K_idx reaches 0, indicating that the + * multiplication is valid in the context of matrix transpose multiplication. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get<0, I>() * B.template get<0, J>(); @@ -1317,6 +2707,23 @@ struct Core { template struct Column { + /** + * @brief Recursively computes the product of two matrices for matrix + * transpose multiplication column by column. + * + * This function computes the product for the J-th column of matrix B and + * recursively calls itself for the next column index J - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1329,6 +2736,23 @@ struct Column { template struct Column { + /** + * @brief Base case for the column multiplication in matrix transpose + * multiplication context. + * + * This function computes the product for the first column of matrix B and + * stores the result in the result matrix when the recursion reaches the base + * case (J == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1340,6 +2764,22 @@ struct Column { template struct Row { + /** + * @brief Recursively computes the product of two matrices for matrix + * transpose multiplication row by row. + * + * This function computes the product for the I-th row of matrix A and + * recursively calls itself for the next row index I - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); @@ -1350,12 +2790,44 @@ struct Row { // Column recursive termination template struct Row { + /** + * @brief Base case for the row multiplication in matrix transpose + * multiplication context. + * + * This function computes the product for the first row of matrix A and + * stores the result in the result matrix when the recursion reaches the base + * case (I == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); } }; +/** + * @brief Computes the product of two matrices for matrix transpose + * multiplication. + * + * This function uses template metaprogramming to recursively compute the + * product of each row of matrix A with each column of matrix B, considering + * that A is transposed, and store the results in a new matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and columns in matrix B. + * @tparam N The number of rows in matrix B. + * @param A The first matrix to multiply (transposed). + * @param B The second matrix to multiply. + * @param result The resulting matrix where the product is stored. + */ template inline void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1364,6 +2836,21 @@ inline void compute(const Matrix &A, const Matrix &B, } // namespace MatrixTransposeMultiplyMatrix +/** + * @brief Multiplies a transposed matrix A with a matrix B. + * + * This function computes the product of a transposed matrix A and a matrix B, + * which means it performs matrix multiplication while considering that A is + * transposed, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of rows in matrix A. + * @tparam K The number of columns in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The transposed matrix to multiply. + * @param B The matrix to multiply with the transposed matrix. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix matrix_multiply_AT_mul_B(const Matrix &A, const Matrix &B) { @@ -1397,6 +2884,23 @@ namespace MatrixTransposeMultiplyVector { template struct Core { + /** + * @brief Recursively computes the product of a transposed matrix and a + * vector. + * + * This function computes the dot product of the M_idx-th column of the + * transposed matrix and the vector, and recursively calls itself for the + * next index M_idx - 1. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns of matrix A. + * @tparam N The number of rows of matrix A. + * @tparam N_idx The current column index in the transposed matrix. + * @tparam M_idx The current row index in the transposed matrix. + * @param mat The transposed matrix to multiply. + * @param vec The vector to multiply with the transposed matrix. + * @return T The computed product for the specified column and vector. + */ static T compute(const Matrix &mat, const Vector &vec) { return mat.template get() * vec[M_idx] + @@ -1407,6 +2911,19 @@ struct Core { // if M_idx == 0 template struct Core { + /** + * @brief Base case for the product of a transposed matrix and a vector. + * + * This function computes the product of the first column of the transposed + * matrix and the vector when M_idx reaches 0, indicating that the + * multiplication is valid in the context of matrix transpose multiplication. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns of matrix A. + * @tparam N The number of rows of matrix A. + * @tparam N_idx The current column index in the transposed matrix. + * @return T The computed product for the specified column and vector. + */ static T compute(const Matrix &mat, const Vector &vec) { return mat.template get<0, N_idx>() * vec[0]; } @@ -1415,6 +2932,22 @@ struct Core { // column recursion template struct Column { + /** + * @brief Recursively computes the product of a transposed matrix and a vector + * column by column. + * + * This function computes the product for the N_idx-th column of the + * transposed matrix and recursively calls itself for the next column index + * N_idx - 1. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns of matrix A. + * @tparam N The number of rows of matrix A. + * @tparam N_idx The current column index in the transposed matrix. + * @param mat The transposed matrix to multiply. + * @param vec The vector to multiply with the transposed matrix. + * @param result The resulting vector where the product is stored. + */ static void compute(const Matrix &mat, const Vector &vec, Vector &result) { result[N_idx] = Core::compute(mat, vec); @@ -1424,12 +2957,41 @@ struct Column { // if N_idx == 0 template struct Column { + /** + * @brief Base case for the column multiplication in matrix transpose + * multiplication context. + * + * This function computes the product for the first column of the transposed + * matrix and the vector, and stores the result in the result vector when the + * recursion reaches the base case (N_idx == 0). + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns of matrix A. + * @tparam N The number of rows of matrix A. + * @param mat The transposed matrix to multiply. + * @param vec The vector to multiply with the transposed matrix. + * @param result The resulting vector where the product is stored. + */ static void compute(const Matrix &mat, const Vector &vec, Vector &result) { result[0] = Core::compute(mat, vec); } }; +/** + * @brief Computes the product of a transposed matrix and a vector. + * + * This function uses template metaprogramming to recursively compute the + * product of each column of the transposed matrix with the vector, and store + * the results in a new vector. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns of matrix A. + * @tparam N The number of rows of matrix A. + * @param mat The transposed matrix to multiply. + * @param vec The vector to multiply with the transposed matrix. + * @param result The resulting vector where the product is stored. + */ template inline void compute(const Matrix &mat, const Vector &vec, Vector &result) { @@ -1438,6 +3000,20 @@ inline void compute(const Matrix &mat, const Vector &vec, } // namespace MatrixTransposeMultiplyVector +/** + * @brief Multiplies a transposed matrix A with a vector b. + * + * This function computes the product of a transposed matrix A and a vector b, + * which means it performs matrix-vector multiplication while considering that + * A is transposed, and returns the resulting vector. + * + * @tparam T The type of the matrix and vector elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrix A. + * @param A The transposed matrix to multiply. + * @param b The vector to multiply with the transposed matrix. + * @return Vector The resulting vector after multiplication. + */ template inline Vector matrix_multiply_AT_mul_b(const Matrix &A, const Vector &b) { @@ -1469,6 +3045,25 @@ namespace MatrixMultiplyTransposeMatrix { template struct Core { + /** + * @brief Recursively computes the product of two matrices for matrix + * multiplication with a transposed matrix. + * + * This function computes the dot product of the I-th row of matrix A and the + * J-th column of matrix B, and recursively calls itself for the next index + * K_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @tparam K_idx The current index in the multiplication. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply (transposed). + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get() * B.template get() + @@ -1480,6 +3075,22 @@ struct Core { template struct Core { + /** + * @brief Base case for the matrix multiplication with a transposed matrix. + * + * This function computes the product of the first row of matrix A and the + * first column of matrix B when K_idx reaches 0, indicating that the + * multiplication is valid in the context of matrix multiplication with a + * transposed matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @return T The computed product for the specified row and column. + */ static T compute(const Matrix &A, const Matrix &B) { return A.template get() * B.template get(); @@ -1490,6 +3101,23 @@ struct Core { template struct Column { + /** + * @brief Recursively computes the product of two matrices for matrix + * multiplication with a transposed matrix column by column. + * + * This function computes the product for the J-th column of matrix B and + * recursively calls itself for the next column index J - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @tparam I The current row index in matrix A. + * @tparam J The current column index in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply (transposed). + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1502,6 +3130,23 @@ struct Column { template struct Column { + /** + * @brief Base case for the column multiplication in matrix multiplication + * with a transposed matrix context. + * + * This function computes the product for the first column of matrix B and + * stores the result in the result matrix when the recursion reaches the base + * case (J == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply (transposed). + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1513,6 +3158,22 @@ struct Column { template struct Row { + /** + * @brief Recursively computes the product of two matrices for matrix + * multiplication with a transposed matrix row by row. + * + * This function computes the product for the I-th row of matrix A and + * recursively calls itself for the next row index I - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @tparam I The current row index in matrix A. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply (transposed). + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); @@ -1523,12 +3184,44 @@ struct Row { // Column recursive termination template struct Row { + /** + * @brief Base case for the row multiplication in matrix multiplication with a + * transposed matrix context. + * + * This function computes the product for the first row of matrix A and + * stores the result in the result matrix when the recursion reaches the base + * case (I == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply (transposed). + * @param result The resulting matrix where the product is stored. + */ static void compute(const Matrix &A, const Matrix &B, Matrix &result) { Column::compute(A, B, result); } }; +/** + * @brief Computes the product of two matrices for matrix multiplication with a + * transposed matrix. + * + * This function uses template metaprogramming to recursively compute the + * product of each row of matrix A with each column of matrix B, considering + * that B is transposed, and store the results in a new matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The first matrix to multiply. + * @param B The second matrix to multiply (transposed). + * @param result The resulting matrix where the product is stored. + */ template inline void compute(const Matrix &A, const Matrix &B, Matrix &result) { @@ -1537,6 +3230,21 @@ inline void compute(const Matrix &A, const Matrix &B, } // namespace MatrixMultiplyTransposeMatrix +/** + * @brief Multiplies a matrix A with the transposed matrix B. + * + * This function computes the product of a matrix A and the transposed matrix + * B, which means it performs matrix multiplication while considering that B is + * transposed, and returns the resulting matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The matrix to multiply + * @param B The transposed matrix to multiply with A. + * @return Matrix The resulting matrix after multiplication. + */ template inline Matrix matrix_multiply_A_mul_BTranspose(const Matrix &A, @@ -1571,6 +3279,22 @@ namespace MatrixRealToComplex { template struct Column { + /** + * @brief Recursively computes the conversion from a real matrix to a complex + * matrix column by column. + * + * This function computes the conversion for the J_idx-th column of the real + * matrix and recursively calls itself for the next column index J_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @tparam J_idx The current column index in the matrix. + * @param From_matrix The real matrix to convert. + * @param To_matrix The resulting complex matrix where the conversion is + * stored. + */ static void compute(const Matrix &From_matrix, Matrix, M, N> &To_matrix) { @@ -1582,6 +3306,22 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column conversion in matrix real to complex + * context. + * + * This function computes the conversion for the first column of the real + * matrix and stores the result in the complex matrix when the recursion + * reaches the base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @param From_matrix The real matrix to convert. + * @param To_matrix The resulting complex matrix where the conversion is + * stored. + */ static void compute(const Matrix &From_matrix, Matrix, M, N> &To_matrix) { @@ -1592,6 +3332,21 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the conversion from a real matrix to a complex + * matrix row by row. + * + * This function computes the conversion for the I_idx-th row of the real + * matrix and recursively calls itself for the next row index I_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the matrix. + * @param From_matrix The real matrix to convert. + * @param To_matrix The resulting complex matrix where the conversion is + * stored. + */ static void compute(const Matrix &From_matrix, Matrix, M, N> &To_matrix) { Column::compute(From_matrix, To_matrix); @@ -1601,12 +3356,39 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row conversion in matrix real to complex context. + * + * This function computes the conversion for the first row of the real matrix + * and stores the result in the complex matrix when the recursion reaches the + * base case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The real matrix to convert. + * @param To_matrix The resulting complex matrix where the conversion is + * stored. + */ static void compute(const Matrix &From_matrix, Matrix, M, N> &To_matrix) { Column::compute(From_matrix, To_matrix); } }; +/** + * @brief Computes the conversion from a real matrix to a complex matrix. + * + * This function uses template metaprogramming to recursively convert each + * element of the real matrix to a complex number, where the imaginary part is + * set to zero, and store the results in a new complex matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The real matrix to convert. + * @param To_matrix The resulting complex matrix where the conversion is stored. + */ template inline void compute(const Matrix &From_matrix, Matrix, M, N> &To_matrix) { @@ -1615,6 +3397,20 @@ inline void compute(const Matrix &From_matrix, } // namespace MatrixRealToComplex +/** + * @brief Converts a real matrix to a complex matrix. + * + * This function converts each element of the real matrix to a complex number, + * where the imaginary part is set to zero, and returns the resulting complex + * matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The real matrix to convert. + * @return Matrix, M, N> The resulting complex matrix after + * conversion. + */ template inline Matrix, M, N> convert_matrix_real_to_complex(const Matrix &From_matrix) { @@ -1645,6 +3441,22 @@ namespace MatrixRealFromComplex { template struct Column { + /** + * @brief Recursively computes the conversion from a complex matrix to a real + * matrix column by column. + * + * This function computes the conversion for the J_idx-th column of the + * complex matrix and recursively calls itself for the next column index J_idx + * - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @tparam J_idx The current column index in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting real matrix where the conversion is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { @@ -1656,6 +3468,21 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column conversion in matrix complex to real + * context. + * + * This function computes the conversion for the first column of the complex + * matrix and stores the result in the real matrix when the recursion reaches + * the base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting real matrix where the conversion is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { @@ -1666,6 +3493,20 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the conversion from a complex matrix to a real + * matrix row by row. + * + * This function computes the conversion for the I_idx-th row of the complex + * matrix and recursively calls itself for the next row index I_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting real matrix where the conversion is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { Column::compute(From_matrix, To_matrix); @@ -1675,12 +3516,38 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row conversion in matrix complex to real context. + * + * This function computes the conversion for the first row of the complex + * matrix and stores the result in the real matrix when the recursion reaches + * the base case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting real matrix where the conversion is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { Column::compute(From_matrix, To_matrix); } }; +/** + * @brief Computes the conversion from a complex matrix to a real matrix. + * + * This function uses template metaprogramming to recursively convert each + * element of the complex matrix to its real part and store the results in a new + * real matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting real matrix where the conversion is stored. + */ template inline void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { @@ -1689,6 +3556,18 @@ inline void compute(const Matrix, M, N> &From_matrix, } // namespace MatrixRealFromComplex +/** + * @brief Converts a complex matrix to a real matrix. + * + * This function extracts the real part of each element in the complex matrix + * and returns the resulting real matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the complex matrix. + * @tparam N The number of rows in the complex matrix. + * @param From_matrix The complex matrix to convert. + * @return Matrix The resulting real matrix after conversion. + */ template inline Matrix get_real_matrix_from_complex_matrix( const Matrix, M, N> &From_matrix) { @@ -1719,6 +3598,23 @@ namespace MatrixImagFromComplex { template struct Column { + /** + * @brief Recursively computes the conversion from a complex matrix to an + * imaginary part matrix column by column. + * + * This function computes the conversion for the J_idx-th column of the + * complex matrix and recursively calls itself for the next column index J_idx + * - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @tparam J_idx The current column index in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting imaginary part matrix where the conversion + * is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { @@ -1730,6 +3626,22 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Base case for the column conversion in matrix complex to imaginary + * part context. + * + * This function computes the conversion for the first column of the complex + * matrix and stores the result in the imaginary part matrix when the + * recursion reaches the base case (J_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I The current row index in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting imaginary part matrix where the conversion + * is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { @@ -1740,6 +3652,21 @@ struct Column { // when I_idx < M template struct Row { + /** + * @brief Recursively computes the conversion from a complex matrix to an + * imaginary part matrix row by row. + * + * This function computes the conversion for the I_idx-th row of the complex + * matrix and recursively calls itself for the next row index I_idx - 1. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam I_idx The current row index in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting imaginary part matrix where the conversion + * is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { Column::compute(From_matrix, To_matrix); @@ -1749,12 +3676,42 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Base case for the row conversion in matrix complex to imaginary part + * context. + * + * This function computes the conversion for the first row of the complex + * matrix and stores the result in the imaginary part matrix when the + * recursion reaches the base case (I_idx == 0). + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting imaginary part matrix where the conversion + * is stored. + */ static void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { Column::compute(From_matrix, To_matrix); } }; +/** + * @brief Computes the conversion from a complex matrix to an imaginary part + * matrix. + * + * This function uses template metaprogramming to recursively extract the + * imaginary part of each element in the complex matrix and store the results + * in a new imaginary part matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param From_matrix The complex matrix to convert. + * @param To_matrix The resulting imaginary part matrix where the conversion is + * stored. + */ template inline void compute(const Matrix, M, N> &From_matrix, Matrix &To_matrix) { @@ -1763,6 +3720,18 @@ inline void compute(const Matrix, M, N> &From_matrix, } // namespace MatrixImagFromComplex +/** + * @brief Converts a complex matrix to an imaginary part matrix. + * + * This function extracts the imaginary part of each element in the complex + * matrix and returns the resulting imaginary part matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the complex matrix. + * @tparam N The number of rows in the complex matrix. + * @param From_matrix The complex matrix to convert. + * @return Matrix The resulting imaginary part matrix after conversion. + */ template inline Matrix get_imag_matrix_from_complex_matrix( const Matrix, M, N> &From_matrix) { From 6f20aa630fe99da24366be40da8bfa9ecdc3342f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 22:23:47 +0900 Subject: [PATCH 13/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_qr_decomposition.hpp | 243 +++++++++++++++++++ 1 file changed, 243 insertions(+) diff --git a/base_matrix/base_matrix_qr_decomposition.hpp b/base_matrix/base_matrix_qr_decomposition.hpp index 3c39c8b..ede07d9 100644 --- a/base_matrix/base_matrix_qr_decomposition.hpp +++ b/base_matrix/base_matrix_qr_decomposition.hpp @@ -1,3 +1,18 @@ +/** + * @file base_matrix_qr_decomposition.hpp + * @brief QR decomposition utilities for dense, diagonal, and sparse matrices. + * + * This header provides template classes and functions for performing QR + * decomposition using Givens rotations on various matrix types, including + * dense, diagonal, and compiled sparse matrices. The QR decomposition is a + * fundamental matrix factorization technique used in numerical linear algebra. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_QR_DECOMPOSITION_HPP__ #define __BASE_MATRIX_QR_DECOMPOSITION_HPP__ @@ -19,6 +34,23 @@ namespace Matrix { constexpr double DEFAULT_DIVISION_MIN_QR = 1.0e-10; /* Given's Rotation */ + +/** + * @brief Performs a Givens rotation on the specified rows of the matrix. + * + * This function applies a Givens rotation to the rows i and j of the R_matrix + * and updates the Q_matrix accordingly. The rotation is used to zero out the + * element at (i, j) in the R_matrix. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the Q_matrix. + * @tparam N The number of rows in the R_matrix. + * @param i The index of the first row to rotate. + * @param j The index of the second row to rotate. + * @param Q_matrix The orthogonal matrix being built. + * @param R_matrix The matrix being decomposed. + * @param division_min A small value to avoid division by zero. + */ template static inline void qr_givensRotation(std::size_t i, std::size_t j, Matrix &Q_matrix, @@ -67,8 +99,20 @@ qr_givensRotation(std::size_t i, std::size_t j, Matrix &Q_matrix, } } +/** + * @brief QR Decomposition using Givens rotations. + * + * This class performs QR decomposition on a matrix using Givens rotations. + * It decomposes the input matrix A into an orthogonal matrix Q and an upper + * triangular matrix R such that A = Q * R. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template class QRDecomposition { public: + /* Constructor */ QRDecomposition() : division_min(static_cast(DEFAULT_DIVISION_MIN_QR)), _Q_matrix(Matrix::identity()) {} @@ -104,14 +148,40 @@ template class QRDecomposition { return *this; } +public: /* Function */ + + /** + * @brief Solves the QR decomposition for the given matrix A. + * + * This function sets the internal R matrix to the provided matrix A + * and performs QR decomposition by calling the internal _decompose method. + * + * @param A The input matrix to decompose. + */ inline void solve(const Matrix &A) { this->_R_matrix = A; this->_decompose(); } + /** + * @brief Returns the orthogonal matrix Q from the QR decomposition. + * + * This function returns the orthogonal matrix Q that was computed during + * the QR decomposition process. + * + * @return The orthogonal matrix Q. + */ inline Matrix get_Q() const { return this->_Q_matrix; } + /** + * @brief Returns the upper triangular matrix R from the QR decomposition. + * + * This function returns the upper triangular matrix R that was computed + * during the QR decomposition process. + * + * @return The upper triangular matrix R. + */ inline Matrix get_R() const { return this->_R_matrix; } public: @@ -125,6 +195,15 @@ template class QRDecomposition { protected: /* Function */ + + /** + * @brief Performs QR decomposition of the matrix using Givens rotations. + * + * Iterates over the lower triangular part of the matrix and applies Givens + * rotations to zero out sub-diagonal elements, transforming the matrix into + * an upper triangular form. The method checks if each sub-diagonal element is + * not near zero (within a specified threshold) before applying the rotation. + */ inline void _decompose() { for (std::size_t j = 0; j < N; ++j) { for (std::size_t i = j + 1; i < M; ++i) { @@ -137,12 +216,32 @@ template class QRDecomposition { } } + /** + * @brief Applies a Givens rotation to the specified rows of the matrix. + * + * This function performs a Givens rotation on the rows i and j of the + * R_matrix and updates the Q_matrix accordingly. The rotation is used to zero + * out the element at (i, j) in the R_matrix. + * + * @param i The index of the first row to rotate. + * @param j The index of the second row to rotate. + */ inline void _givensRotation(std::size_t i, std::size_t j) { Base::Matrix::qr_givensRotation(i, j, this->_Q_matrix, this->_R_matrix, this->division_min); } }; +/** + * @brief QR Decomposition for Diagonal Matrices. + * + * This class provides a specialized implementation of QR decomposition for + * diagonal matrices. It assumes that the input matrix is diagonal and performs + * the decomposition accordingly. + * + * @tparam T The type of elements in the diagonal matrix. + * @tparam M The size of the diagonal matrix (number of rows/columns). + */ template class QRDecompositionDiag { public: /* Constructor */ @@ -150,12 +249,64 @@ template class QRDecompositionDiag { : division_min(static_cast(DEFAULT_DIVISION_MIN_QR)), _Q_matrix(DiagMatrix::identity()) {} + /* Copy Constructor */ + QRDecompositionDiag(const QRDecompositionDiag &other) + : division_min(other.division_min), _Q_matrix(other._Q_matrix), + _R_matrix(other._R_matrix) {} + QRDecompositionDiag &operator=(const QRDecompositionDiag &other) { + if (this != &other) { + this->division_min = other.division_min; + this->_Q_matrix = other._Q_matrix; + this->_R_matrix = other._R_matrix; + } + return *this; + } + + /* Move Constructor */ + QRDecompositionDiag(QRDecompositionDiag &&other) noexcept + : division_min(other.division_min), _Q_matrix(std::move(other._Q_matrix)), + _R_matrix(std::move(other._R_matrix)) {} + QRDecompositionDiag & + operator=(QRDecompositionDiag &&other) noexcept { + if (this != &other) { + this->division_min = other.division_min; + this->_Q_matrix = std::move(other._Q_matrix); + this->_R_matrix = std::move(other._R_matrix); + } + return *this; + } + public: /* Function */ + + /** + * @brief Solves the QR decomposition for the given diagonal matrix A. + * + * This function sets the internal R matrix to the provided diagonal matrix A + * and performs QR decomposition by calling the internal solve method. + * + * @param A The input diagonal matrix to decompose. + */ inline void solve(const DiagMatrix &A) { this->_R_matrix = A; } + /** + * @brief Returns the orthogonal matrix Q from the QR decomposition. + * + * This function returns the orthogonal matrix Q that was computed during + * the QR decomposition process. + * + * @return The orthogonal matrix Q. + */ inline DiagMatrix get_Q() const { return this->_Q_matrix; } + /** + * @brief Returns the upper triangular matrix R from the QR decomposition. + * + * This function returns the upper triangular matrix R that was computed + * during the QR decomposition process. + * + * @return The upper triangular matrix R. + */ inline DiagMatrix get_R() const { return this->_R_matrix; } public: @@ -168,6 +319,19 @@ template class QRDecompositionDiag { DiagMatrix _R_matrix; }; +/** + * @brief QR Decomposition for Sparse Matrices. + * + * This class provides a specialized implementation of QR decomposition for + * sparse matrices. It uses Givens rotations to decompose the input matrix into + * an orthogonal matrix Q and an upper triangular matrix R. + * + * @tparam T The type of elements in the sparse matrix. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix. + * @tparam RowIndices_A Type representing row indices of the sparse matrix. + * @tparam RowPointers_A Type representing row pointers of the sparse matrix. + */ template class QRDecompositionSparse { @@ -177,16 +341,73 @@ class QRDecompositionSparse { : division_min(static_cast(DEFAULT_DIVISION_MIN_QR)), _Q_matrix(Matrix::identity()) {} + /* Copy Constructor */ + QRDecompositionSparse( + const QRDecompositionSparse &other) + : division_min(other.division_min), _Q_matrix(other._Q_matrix), + _R_matrix(other._R_matrix) {} + QRDecompositionSparse & + operator=(const QRDecompositionSparse + &other) { + if (this != &other) { + this->division_min = other.division_min; + this->_Q_matrix = other._Q_matrix; + this->_R_matrix = other._R_matrix; + } + return *this; + } + + /* Move Constructor */ + QRDecompositionSparse(QRDecompositionSparse &&other) noexcept + : division_min(other.division_min), _Q_matrix(std::move(other._Q_matrix)), + _R_matrix(std::move(other._R_matrix)) {} + QRDecompositionSparse & + operator=(QRDecompositionSparse + &&other) noexcept { + if (this != &other) { + this->division_min = other.division_min; + this->_Q_matrix = std::move(other._Q_matrix); + this->_R_matrix = std::move(other._R_matrix); + } + return *this; + } + public: /* Function */ + + /** + * @brief Solves the QR decomposition for the given sparse matrix A. + * + * This function sets the internal R matrix to the provided sparse matrix A + * and performs QR decomposition by calling the internal _decompose method. + * + * @param A The input sparse matrix to decompose. + */ inline void solve(const CompiledSparseMatrix &A) { this->_R_matrix = Base::Matrix::output_dense_matrix(A); this->_decompose(A); } + /** + * @brief Returns the orthogonal matrix Q from the QR decomposition. + * + * This function returns the orthogonal matrix Q that was computed during + * the QR decomposition process. + * + * @return The orthogonal matrix Q. + */ inline Matrix get_Q() const { return this->_Q_matrix; } + /** + * @brief Returns the upper triangular matrix R from the QR decomposition. + * + * This function returns the upper triangular matrix R that was computed + * during the QR decomposition process. + * + * @return The upper triangular matrix R. + */ inline Matrix get_R() const { return this->_R_matrix; } public: @@ -200,6 +421,18 @@ class QRDecompositionSparse { protected: /* Function */ + + /** + * @brief Performs QR decomposition of the sparse matrix using Givens + * rotations. + * + * Iterates over the non-zero elements of the sparse matrix and applies Givens + * rotations to zero out sub-diagonal elements, transforming the matrix into + * an upper triangular form. The method checks if each sub-diagonal element is + * not near zero (within a specified threshold) before applying the rotation. + * + * @param A The input sparse matrix to decompose. + */ inline void _decompose( const CompiledSparseMatrix &A) { for (std::size_t i = 0; i < M; i++) { @@ -213,6 +446,16 @@ class QRDecompositionSparse { } } + /** + * @brief Applies a Givens rotation to the specified rows of the matrix. + * + * This function performs a Givens rotation on the rows i and j of the + * R_matrix and updates the Q_matrix accordingly. The rotation is used to zero + * out the element at (i, j) in the R_matrix. + * + * @param i The index of the first row to rotate. + * @param j The index of the second row to rotate. + */ inline void _givensRotation(std::size_t i, std::size_t j) { Base::Matrix::qr_givensRotation(i, j, this->_Q_matrix, this->_R_matrix, this->division_min); From 217862b739bf588d36886260d23b5307d08de077 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 22:29:40 +0900 Subject: [PATCH 14/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_sparse.hpp | 499 +++++++++++++++++++++++++++++ 1 file changed, 499 insertions(+) diff --git a/base_matrix/base_matrix_sparse.hpp b/base_matrix/base_matrix_sparse.hpp index 2e5dfbd..2281789 100644 --- a/base_matrix/base_matrix_sparse.hpp +++ b/base_matrix/base_matrix_sparse.hpp @@ -1,3 +1,23 @@ +/** + * @file base_matrix_sparse.hpp + * @brief Sparse matrix implementation and related operations for fixed-size and + * vector-based storage. + * + * This header provides a templated SparseMatrix class supporting both + * std::array and std::vector storage for efficient representation of sparse + * matrices. It includes constructors for various initialization methods, + * conversion to dense matrices, and arithmetic operations with dense matrices, + * diagonal matrices, vectors, and other sparse matrices. The file also provides + * a set of free functions for creating sparse matrices from dense or diagonal + * matrices, and for performing matrix-matrix and matrix-vector multiplications + * involving sparse matrices. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_SPARSE_HPP__ #define __BASE_MATRIX_SPARSE_HPP__ @@ -19,6 +39,19 @@ namespace Matrix { const double SPARSE_MATRIX_JUDGE_ZERO_LIMIT_VALUE = 1.0e-20; +/** + * @brief SparseMatrix class for fixed-size and vector-based storage. + * + * This class implements a sparse matrix using either std::array or std::vector + * for storage. It supports various operations such as addition, subtraction, + * multiplication with dense matrices, diagonal matrices, and other sparse + * matrices, as well as conversion to dense format. + * + * @tparam T The type of elements in the matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + */ template class SparseMatrix { public: @@ -162,7 +195,17 @@ class SparseMatrix { return *this; } +public: /* Function */ + + /** + * @brief Creates a dense matrix from the sparse matrix. + * + * This function iterates through the non-zero elements of the sparse matrix + * and fills a dense matrix with the corresponding values. + * + * @return A dense matrix representation of the sparse matrix. + */ inline Matrix create_dense() const { Matrix result; @@ -176,22 +219,73 @@ class SparseMatrix { return result; } + /** + * @brief Creates a diagonal matrix from the sparse matrix. + * + * This function extracts the diagonal elements from the sparse matrix and + * creates a diagonal matrix with those values. + * + * @return A diagonal matrix representation of the sparse matrix. + */ T value(std::size_t i) { return this->values[i]; } + /** + * @brief Returns the value at the specified index in the sparse matrix. + * + * This function retrieves the value at the given index in the sparse matrix. + * + * @param i The index of the value to retrieve. + * @return The value at the specified index. + */ const T value(std::size_t i) const { return this->values[i]; } + /** + * @brief Returns the index of the row corresponding to the specified index. + * + * This function retrieves the row index for the given index in the sparse + * matrix. + * + * @param i The index of the value to retrieve the row index for. + * @return The row index corresponding to the specified index. + */ std::size_t row_index(std::size_t i) { return this->row_indices[i]; } const std::size_t row_index(std::size_t i) const { return this->row_indices[i]; } + /** + * @brief Returns the pointer to the start of the specified row. + * + * This function retrieves the starting index of the specified row in the + * sparse matrix. + * + * @param i The index of the row to retrieve the pointer for. + * @return The starting index of the specified row. + */ std::size_t row_pointer(std::size_t i) { return this->row_pointers[i]; } + /** + * @brief Returns the pointer to the start of the specified row. + * + * This function retrieves the starting index of the specified row in the + * sparse matrix. + * + * @param i The index of the row to retrieve the pointer for. + * @return The starting index of the specified row. + */ const std::size_t row_pointer(std::size_t i) const { return this->row_pointers[i]; } + /** + * @brief Returns the number of non-zero values in the sparse matrix. + * + * This function returns the total number of non-zero values stored in the + * sparse matrix. + * + * @return The number of non-zero values in the sparse matrix. + */ inline Matrix transpose(void) const { Matrix Y; @@ -205,6 +299,14 @@ class SparseMatrix { return Y; } + /** + * @brief Returns the number of non-zero values in the sparse matrix. + * + * This function returns the total number of non-zero values stored in the + * sparse matrix. + * + * @return The number of non-zero values in the sparse matrix. + */ inline Matrix operator+(const Matrix &B) const { Matrix Y = B; @@ -218,6 +320,15 @@ class SparseMatrix { return Y; } + /** + * @brief Adds a diagonal matrix to the sparse matrix. + * + * This function creates a dense matrix from the diagonal matrix and adds it + * to the sparse matrix. + * + * @param B The diagonal matrix to add. + * @return A dense matrix resulting from the addition. + */ inline Matrix operator+(const DiagMatrix &B) const { Matrix Y = B.create_dense(); @@ -231,6 +342,15 @@ class SparseMatrix { return Y; } + /** + * @brief Adds a sparse matrix to the sparse matrix. + * + * This function creates a dense matrix from the sparse matrix and adds it to + * the current sparse matrix. + * + * @param mat The sparse matrix to add. + * @return A dense matrix resulting from the addition. + */ inline Matrix operator+(const SparseMatrix &mat) const { Matrix Y = this->create_dense(); @@ -244,6 +364,15 @@ class SparseMatrix { return Y; } + /** + * @brief Subtracts a dense matrix from the sparse matrix. + * + * This function creates a dense matrix from the sparse matrix and subtracts + * the given dense matrix from it. + * + * @param B The dense matrix to subtract. + * @return A dense matrix resulting from the subtraction. + */ inline Matrix operator-(const Matrix &B) const { Matrix Y = -B; @@ -257,6 +386,15 @@ class SparseMatrix { return Y; } + /** + * @brief Subtracts a diagonal matrix from the sparse matrix. + * + * This function creates a dense matrix from the diagonal matrix and subtracts + * it from the sparse matrix. + * + * @param B The diagonal matrix to subtract. + * @return A dense matrix resulting from the subtraction. + */ inline Matrix operator-(const DiagMatrix &B) const { Matrix Y = -(B.create_dense()); @@ -270,6 +408,15 @@ class SparseMatrix { return Y; } + /** + * @brief Subtracts a sparse matrix from the sparse matrix. + * + * This function creates a dense matrix from the sparse matrix and subtracts + * the given sparse matrix from it. + * + * @param mat The sparse matrix to subtract. + * @return A dense matrix resulting from the subtraction. + */ inline Matrix operator-(const SparseMatrix &mat) const { Matrix Y = this->create_dense(); @@ -283,6 +430,15 @@ class SparseMatrix { return Y; } + /** + * @brief Multiplies the sparse matrix by a dense matrix. + * + * This function performs matrix multiplication between the sparse matrix and + * the given dense matrix. + * + * @param B The dense matrix to multiply with. + * @return A dense matrix resulting from the multiplication. + */ inline SparseMatrix operator*(const T &scalar) const { SparseMatrix Y = *this; @@ -293,8 +449,22 @@ class SparseMatrix { return Y; } + /** + * @brief Returns the number of rows in the sparse matrix. + * + * This function returns the number of rows in the sparse matrix. + * + * @return The number of rows in the sparse matrix. + */ constexpr std::size_t rows() const { return N; } + /** + * @brief Returns the number of columns in the sparse matrix. + * + * This function returns the number of columns in the sparse matrix. + * + * @return The number of columns in the sparse matrix. + */ constexpr std::size_t cols() const { return M; } /* Variable */ @@ -309,6 +479,17 @@ class SparseMatrix { #endif // __BASE_MATRIX_USE_STD_VECTOR__ }; +/** * @brief Creates a sparse matrix from a dense matrix. + * + * This function converts a dense matrix into a sparse matrix by extracting + * non-zero elements and their corresponding row indices. + * + * @tparam T The type of elements in the matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param A The dense matrix to convert. + * @return A sparse matrix representation of the dense matrix. + */ template inline SparseMatrix create_sparse(const Matrix &A) { std::size_t consecutive_index = 0; @@ -338,6 +519,18 @@ inline SparseMatrix create_sparse(const Matrix &A) { } /* Create */ + +/** + * @brief Creates a sparse matrix from a diagonal matrix. + * + * This function converts a diagonal matrix into a sparse matrix by extracting + * the diagonal elements and their corresponding row indices. + * + * @tparam T The type of elements in the matrix. + * @tparam M The size of the diagonal matrix (number of rows and columns). + * @param A The diagonal matrix to convert. + * @return A sparse matrix representation of the diagonal matrix. + */ template inline SparseMatrix create_sparse(const DiagMatrix &A) { std::size_t consecutive_index = 0; @@ -365,6 +558,20 @@ inline SparseMatrix create_sparse(const DiagMatrix &A) { } /* Operator */ + +/** + * @brief Adds a dense matrix to a sparse matrix. + * + * This function creates a dense matrix from the sparse matrix and adds the + * given dense matrix to it. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param B The dense matrix to add. + * @param A The sparse matrix to add to. + * @return A dense matrix resulting from the addition. + */ template inline Matrix operator+(const DiagMatrix &B, const SparseMatrix &A) { @@ -379,6 +586,19 @@ inline Matrix operator+(const DiagMatrix &B, return Y; } +/** + * @brief Subtracts a sparse matrix from a diagonal matrix. + * + * This function creates a dense matrix from the diagonal matrix and subtracts + * the given sparse matrix from it. + * + * @tparam T The type of elements in the matrices. + * @tparam M The size of the diagonal matrix (number of rows and columns). + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param B The diagonal matrix to subtract from. + * @param A The sparse matrix to subtract. + * @return A dense matrix resulting from the subtraction. + */ template inline Matrix operator-(const DiagMatrix &B, const SparseMatrix &A) { @@ -394,6 +614,20 @@ inline Matrix operator-(const DiagMatrix &B, } /* Scalar * SparseMatrix */ + +/** + * @brief Multiplies a scalar with a sparse matrix. + * + * This function scales all values in the sparse matrix by the given scalar. + * + * @tparam T The type of elements in the matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param scalar The scalar value to multiply with. + * @param A The sparse matrix to scale. + * @return A new sparse matrix with scaled values. + */ template inline SparseMatrix operator*(const T &scalar, const SparseMatrix &A) { @@ -407,6 +641,22 @@ inline SparseMatrix operator*(const T &scalar, } /* SparseMatrix * Vector */ + +/** + * @brief Multiplies a sparse matrix with a vector. + * + * This function performs matrix-vector multiplication between the sparse matrix + * and the given vector, returning a new vector as the result. + * + * @tparam T The type of elements in the matrix and vector. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix (and size of the + * vector). + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The sparse matrix to multiply. + * @param b The vector to multiply with. + * @return A new vector resulting from the multiplication. + */ template inline Vector operator*(const SparseMatrix &A, const Vector &b) { @@ -423,6 +673,21 @@ inline Vector operator*(const SparseMatrix &A, return y; } +/** + * @brief Multiplies a vector with a sparse matrix. + * + * This function performs vector-matrix multiplication between the given vector + * and the sparse matrix, returning a new vector as the result. + * + * @tparam T The type of elements in the matrix and vector. + * @tparam N The number of columns in the sparse matrix (and size of the + * vector). + * @tparam K The number of rows in the sparse matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param a The vector to multiply with. + * @param B The sparse matrix to multiply. + * @return A new vector resulting from the multiplication. + */ template inline ColVector colVector_a_mul_SparseB(const ColVector &a, @@ -439,6 +704,23 @@ colVector_a_mul_SparseB(const ColVector &a, } /* SparseMatrix * Matrix */ + +/** + * @brief Multiplies a sparse matrix with a dense matrix. + * + * This function performs matrix multiplication between the sparse matrix and + * the given dense matrix, returning a new dense matrix as the result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix (and columns in the dense + * matrix). + * @tparam K The number of columns in the dense matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The sparse matrix to multiply. + * @param B The dense matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix operator*(const SparseMatrix &A, @@ -458,6 +740,23 @@ inline Matrix operator*(const SparseMatrix &A, return Y; } +/** + * @brief Multiplies a dense matrix with a sparse matrix. + * + * This function performs matrix multiplication between the given dense matrix + * and the sparse matrix, returning a new dense matrix as the result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the dense matrix (and columns in the + * sparse matrix). + * @tparam N The number of rows in the dense matrix (and rows in the sparse + * matrix). + * @tparam K The number of rows in the sparse matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The dense matrix to multiply. + * @param B The sparse matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix operator*(const Matrix &A, @@ -475,6 +774,23 @@ inline Matrix operator*(const Matrix &A, return Y; } +/** + * @brief Multiplies a sparse matrix with the transpose of a dense matrix. + * + * This function performs matrix multiplication between the sparse matrix and + * the transpose of the given dense matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix (and columns in the dense + * matrix). + * @tparam K The number of rows in the dense matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The sparse matrix to multiply. + * @param B The dense matrix to multiply with (transposed). + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix @@ -496,6 +812,23 @@ matrix_multiply_SparseA_mul_BTranspose(const SparseMatrix &A, } /* SparseMatrix * SparseMatrix */ + +/** + * @brief Multiplies two sparse matrices. + * + * This function performs matrix multiplication between two sparse matrices, + * returning a new dense matrix as the result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the first sparse matrix. + * @tparam N The number of rows in the first sparse matrix (and columns in the + * second sparse matrix). + * @tparam K The number of rows in the second sparse matrix. + * @tparam V The maximum number of non-zero values in the first sparse matrix. + * @param A The first sparse matrix to multiply. + * @param B The second sparse matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix operator*(const SparseMatrix &A, @@ -514,6 +847,24 @@ inline Matrix operator*(const SparseMatrix &A, return Y; } +/** + * @brief Multiplies a sparse matrix with the transpose of another sparse + * matrix. + * + * This function performs matrix multiplication between a sparse matrix and the + * transpose of another sparse matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the first sparse matrix. + * @tparam N The number of rows in the first sparse matrix (and rows in the + * second sparse matrix). + * @tparam K The number of columns in the second sparse matrix. + * @tparam V The maximum number of non-zero values in the first sparse matrix. + * @param A The first sparse matrix to multiply. + * @param B The second sparse matrix to multiply with (transposed). + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix matrix_multiply_SparseA_mul_SparseBTranspose( @@ -535,6 +886,25 @@ inline Matrix matrix_multiply_SparseA_mul_SparseBTranspose( return Y; } +/** + * @brief Multiplies the transpose of a sparse matrix with another sparse + * matrix. + * + * This function performs matrix multiplication between the transpose of a + * sparse matrix and another sparse matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the first sparse matrix (and columns in + * the second sparse matrix). + * @tparam N The number of rows in the first sparse matrix (and rows in the + * second sparse matrix). + * @tparam K The number of columns in the second sparse matrix. + * @tparam V The maximum number of non-zero values in the first sparse matrix. + * @param A The first sparse matrix to multiply (transposed). + * @param B The second sparse matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix matrix_multiply_SparseATranspose_mul_SparseB( @@ -553,6 +923,22 @@ inline Matrix matrix_multiply_SparseATranspose_mul_SparseB( } /* DiagMatrix */ + +/** + * @brief Multiplies a sparse matrix with a diagonal matrix. + * + * This function performs matrix multiplication between the sparse matrix and + * the diagonal matrix, returning a new dense matrix as the result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix (and size of the diagonal + * matrix). + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The sparse matrix to multiply. + * @param B The diagonal matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix operator*(const SparseMatrix &A, const DiagMatrix &B) { @@ -573,6 +959,20 @@ inline Matrix operator*(const SparseMatrix &A, return Y; } +/** + * @brief Multiplies a diagonal matrix with a sparse matrix. + * + * This function performs matrix multiplication between the diagonal matrix and + * the sparse matrix, returning a new dense matrix as the result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The size of the diagonal matrix (number of rows and columns). + * @tparam K The number of columns in the sparse matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The diagonal matrix to multiply. + * @param B The sparse matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix operator*(const DiagMatrix &A, const SparseMatrix &B) { @@ -591,6 +991,21 @@ inline Matrix operator*(const DiagMatrix &A, return Y; } +/** + * @brief Multiplies a diagonal matrix with the transpose of a sparse matrix. + * + * This function performs matrix multiplication between the diagonal matrix and + * the transpose of the sparse matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The size of the diagonal matrix (number of rows and columns). + * @tparam K The number of rows in the sparse matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The diagonal matrix to multiply. + * @param B The sparse matrix to multiply with (transposed). + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix matrix_multiply_Transpose_DiagA_mul_SparseB(const DiagMatrix &A, @@ -611,6 +1026,21 @@ matrix_multiply_Transpose_DiagA_mul_SparseB(const DiagMatrix &A, } /* Plus, Minus */ + +/** + * @brief Adds a sparse matrix to a dense matrix. + * + * This function creates a dense matrix from the sparse matrix and adds the + * given dense matrix to it. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param B The dense matrix to add. + * @param SA The sparse matrix to add to. + * @return A dense matrix resulting from the addition. + */ template inline Matrix operator+(const Matrix &B, const SparseMatrix SA) { @@ -625,6 +1055,20 @@ inline Matrix operator+(const Matrix &B, return Y; } +/** + * @brief Subtracts a sparse matrix from a dense matrix. + * + * This function creates a dense matrix from the sparse matrix and subtracts + * the given dense matrix from it. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param B The dense matrix to subtract. + * @param SA The sparse matrix to subtract from. + * @return A dense matrix resulting from the subtraction. + */ template inline Matrix operator-(const Matrix &B, const SparseMatrix SA) { @@ -640,6 +1084,25 @@ inline Matrix operator-(const Matrix &B, } /* Transpose */ + +/** + * @brief Multiplies a dense matrix with the transpose of a sparse matrix. + * + * This function performs matrix multiplication between the dense matrix and + * the transpose of the sparse matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam M The number of columns in the dense matrix (and rows in the sparse + * matrix). + * @tparam N The number of rows in the dense matrix (and columns in the sparse + * matrix). + * @tparam K The number of rows in the sparse matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The dense matrix to multiply. + * @param SB The sparse matrix to multiply with (transposed). + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix @@ -660,6 +1123,24 @@ matrix_multiply_A_mul_SparseBTranspose(const Matrix &A, return Y; } +/** + * @brief Multiplies the transpose of a sparse matrix with a dense matrix. + * + * This function performs matrix multiplication between the transpose of the + * sparse matrix and the dense matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam N The number of rows in the sparse matrix (and columns in the dense + * matrix). + * @tparam M The number of columns in the sparse matrix (and rows in the dense + * matrix). + * @tparam K The number of columns in the dense matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The sparse matrix to multiply (transposed). + * @param B The dense matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix @@ -678,6 +1159,24 @@ matrix_multiply_ATranspose_mul_SparseB(const Matrix &A, return Y; } +/** + * @brief Multiplies the transpose of a sparse matrix with a dense matrix. + * + * This function performs matrix multiplication between the transpose of the + * sparse matrix and the dense matrix, returning a new dense matrix as the + * result. + * + * @tparam T The type of elements in the matrices. + * @tparam N The number of rows in the sparse matrix (and columns in the dense + * matrix). + * @tparam M The number of columns in the sparse matrix (and rows in the dense + * matrix). + * @tparam K The number of columns in the dense matrix. + * @tparam V The maximum number of non-zero values in the sparse matrix. + * @param A The sparse matrix to multiply (transposed). + * @param B The dense matrix to multiply with. + * @return A new dense matrix resulting from the multiplication. + */ template inline Matrix From 35f3c7e28715e1ec88666c557ba615f9e6d9053c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 22:54:13 +0900 Subject: [PATCH 15/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_templates.hpp | 2160 ++++++++++++++++++++++++- 1 file changed, 2121 insertions(+), 39 deletions(-) diff --git a/base_matrix/base_matrix_templates.hpp b/base_matrix/base_matrix_templates.hpp index 1c01dd2..5948d8d 100644 --- a/base_matrix/base_matrix_templates.hpp +++ b/base_matrix/base_matrix_templates.hpp @@ -1,3 +1,25 @@ +/** + * @file base_matrix_templates.hpp + * @brief Template metaprogramming utilities for compile-time sparse and dense + * matrix structure manipulation. + * + * This header provides a comprehensive set of C++ template metaprogramming + * tools for representing, constructing, and manipulating matrix structures + * (both sparse and dense) at compile time. It enables the definition of matrix + * sparsity patterns, row/column indices, pointers, and various matrix + * operations (addition, multiplication, concatenation, transpose, triangular + * extraction, etc.) entirely through type-level programming. + * + * The utilities are designed to support static analysis and code generation for + * matrix operations, particularly for applications such as scientific + * computing, code automation, and symbolic manipulation of matrix structures. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_TEMPLATES_HPP__ #define __BASE_MATRIX_TEMPLATES_HPP__ @@ -8,13 +30,45 @@ namespace Base { namespace Matrix { +/** + * @namespace TemplatesOperation + * @brief Contains template utilities for compile-time array and sparse matrix + * operations. + * + * This namespace provides template structures and classes to facilitate + * compile-time manipulation of arrays and sparse matrix representations. + */ namespace TemplatesOperation { +/** + * @struct list_array + * @brief Represents a compile-time array of std::size_t values. + * + * @tparam Sizes Variadic template parameter pack representing the array + * elements. + * + * Provides: + * - A static constexpr member `size` indicating the number of elements. + * - A static constexpr array `value` containing the elements. + */ template struct list_array { static constexpr std::size_t size = sizeof...(Sizes); static constexpr std::size_t value[size] = {Sizes...}; }; +/** + * @class CompiledSparseMatrixList + * @brief Provides a compile-time interface to access a list of indices for + * sparse matrices. + * + * @tparam Array A type that provides static constexpr members `value` (pointer + * to array) and `size`. + * + * Provides: + * - A typedef `list_type` for a pointer to const std::size_t. + * - A static constexpr member `list` pointing to the array of indices. + * - A static constexpr member `size` indicating the number of indices. + */ template constexpr std::size_t list_array::value[list_array::size]; @@ -27,10 +81,34 @@ template class CompiledSparseMatrixList { } // namespace TemplatesOperation +/** + * @brief Alias template for creating a compiled sparse matrix list of row + * indices. + * + * This alias uses the TemplatesOperation::CompiledSparseMatrixList template, + * parameterized with a list of sizes provided as template arguments. + * It is typically used to represent a list of row indices for sparse matrix + * operations. + * + * @tparam Sizes Variadic template parameter pack representing the row indices. + */ template using RowIndices = TemplatesOperation::CompiledSparseMatrixList< TemplatesOperation::list_array>; +/** + * @brief Alias template for representing a list of row pointers for sparse + * matrices. + * + * This alias uses the TemplatesOperation::CompiledSparseMatrixList template, + * parameterized with a list of sizes provided via + * TemplatesOperation::list_array. It is typically used to define compile-time + * row pointer structures for sparse matrix representations, where the sizes + * correspond to the dimensions or non-zero counts per row. + * + * @tparam Sizes Variadic list of size_t values representing row sizes or + * indices. + */ template using RowPointers = TemplatesOperation::CompiledSparseMatrixList< TemplatesOperation::list_array>; @@ -38,15 +116,63 @@ using RowPointers = TemplatesOperation::CompiledSparseMatrixList< namespace TemplatesOperation { /* Create Sparse Matrix from Matrix Element List */ + +/** + * @brief A template struct that represents a compile-time list of boolean + * flags. + * + * This struct provides a static constexpr array `value` containing the boolean + * flags passed as template parameters, and a static constexpr `size` indicating + * the number of flags. + * + * @tparam Flags Variadic boolean template parameters representing the list of + * flags. + * + * Example usage: + * @code + * using MyFlags = available_list_array; + * // MyFlags::size == 3 + * // MyFlags::value == {true, false, true} + * @endcode + */ template struct available_list_array { static constexpr std::size_t size = sizeof...(Flags); static constexpr bool value[size] = {Flags...}; }; +/** + * @brief Static constexpr array indicating the availability of each flag in the + * template parameter pack. + * + * This member array, `value`, is defined for the `available_list_array` + * template class, parameterized by a variadic boolean template parameter pack + * (`Flags...`). The array has a size equal to the number of flags provided and + * contains the boolean values from the parameter pack, representing the + * availability or state of each corresponding flag. + * + * @tparam Flags Variadic boolean template parameters representing individual + * flags. + * @see available_list_array + */ template constexpr bool available_list_array::value[available_list_array::size]; +/** + * @brief Template class for representing a compiled list of sparse matrix + * elements. + * + * This class template provides a static interface to access a list of boolean + * values (typically representing the presence or absence of elements in a + * sparse matrix) and its size at compile time. The list and its size are + * extracted from the provided Array type, which must define a static constexpr + * pointer `value` to a boolean array and a static constexpr `size` indicating + * the number of elements. + * + * @tparam Array The type providing the static boolean array and its size. + * + * @note This class is intended for use with compile-time constant data. + */ template class CompiledSparseMatrixElementList { public: typedef const bool *list_type; @@ -56,67 +182,215 @@ template class CompiledSparseMatrixElementList { } // namespace TemplatesOperation +/** + * @brief Alias template for creating a compiled sparse matrix element list + * based on column availability flags. + * + * This template alias uses a parameter pack of boolean flags to specify the + * availability of columns. It leverages + * `TemplatesOperation::available_list_array` to generate a list array from the + * provided flags, and then passes this list to + * `TemplatesOperation::CompiledSparseMatrixElementList` to create the + * corresponding type. + * + * @tparam Flags Boolean flags indicating the availability of each column. + * + * @see TemplatesOperation::available_list_array + * @see TemplatesOperation::CompiledSparseMatrixElementList + */ template using ColumnAvailable = TemplatesOperation::CompiledSparseMatrixElementList< TemplatesOperation::available_list_array>; namespace TemplatesOperation { +/** + * @brief Template struct to represent a collection of available columns in a + * sparse matrix. + * + * This struct aggregates information about multiple columns, each represented + * by a type. It provides compile-time access to the number of columns, the + * lists associated with each column, and the size of the first column. + * + * @tparam Columns Variadic template parameter pack representing column types. + * + * Members: + * - number_of_columns: The total number of columns provided. + * - ExtractList: Helper struct to extract the list from each column type. + * - lists: Compile-time array of lists, one for each column. + * - column_size: The size of the first column type. + */ template struct SparseAvailableColumns { static constexpr std::size_t number_of_columns = sizeof...(Columns); - // Helper struct to extract the list from each ColumnAvailable + /** + * @brief Helper struct to extract the list type from a given Column type. + * + * This template struct defines a static constexpr member `value` that + * retrieves the `list` member from the specified `Column` type. The type of + * `value` is determined by `Column::list_type`. + * + * @tparam Column The type from which to extract the `list` member and its + * type. + */ template struct ExtractList { static constexpr typename Column::list_type value = Column::list; }; - // Array of lists from each ColumnAvailable + /** + * @brief Array of pointers to constant boolean lists, one for each column. + * + * This static constexpr array holds pointers to boolean lists extracted for + * each column using the ExtractList template. The size of the array is + * determined by the number of columns. Each entry in the array corresponds to + * the value provided by ExtractList::value. + * + * @tparam Columns Template parameter pack representing the columns. + * @tparam number_of_columns The total number of columns. + */ static constexpr const bool *lists[number_of_columns] = { ExtractList::value...}; - // Function to get the size of a specific column + /** + * @brief Represents an unsigned integral type used for sizes and counts. + * + * std::size_t is the unsigned integer type returned by the sizeof operator + * and is commonly used for array indexing and loop counting. It is guaranteed + * to be able to represent the size of any object in bytes. + */ static constexpr std::size_t column_size = std::tuple_element<0, std::tuple>::type::size; }; } // namespace TemplatesOperation +/** + * @brief Alias template to determine the availability of sparse columns. + * + * This alias uses the `SparseAvailableColumns` metafunction from the + * `TemplatesOperation` namespace to check or operate on the provided column + * types (`Columns...`). It is typically used in template metaprogramming to + * enable or disable features based on the properties of the given columns. + * + * @tparam Columns Variadic template parameter pack representing column types to + * be checked for sparse availability. + */ template using SparseAvailable = TemplatesOperation::SparseAvailableColumns; namespace TemplatesOperation { -// helper template to calculate the logical OR +/** + * @brief Compile-time logical OR operation for boolean template parameters. + * + * This struct computes the logical OR of two boolean template parameters `A` + * and `B` at compile time and exposes the result as a static constexpr boolean + * member `value`. + * + * @tparam A First boolean value. + * @tparam B Second boolean value. + * + * @note Useful for template metaprogramming where compile-time boolean logic is + * required. + */ template struct LogicalOr { static constexpr bool value = A || B; }; /* SparseAvailable check empty */ -// helper template to calculate the logical OR for multiple values + +/** + * @brief Template struct to compute the logical OR of multiple boolean values + * at compile time. + * + * This variadic template struct takes a parameter pack of boolean values and + * can be specialized to evaluate whether at least one of the provided values is + * true. + * + * @tparam Values Variadic list of boolean values to be logically OR'ed + * together. + */ template struct LogicalOrMultiple; -// base case for a single value +/** + * @brief Template specialization for LogicalOrMultiple with a single boolean + * value. + * + * This struct defines a static constexpr boolean member `value` that is set to + * the template parameter `Value`. It is typically used as a base case in + * recursive template metaprogramming to compute the logical OR of multiple + * boolean values at compile time. + * + * @tparam Value The boolean value to be represented. + */ template struct LogicalOrMultiple { static constexpr bool value = Value; }; -// recursive case for multiple values +/** + * @brief Computes the logical OR of multiple boolean template parameters. + * + * This template recursively evaluates the logical OR operation across all + * provided boolean template arguments. It uses the LogicalOr template to + * combine the first argument with the result of recursively applying + * LogicalOrMultiple to the rest. + * + * @tparam First The first boolean value in the parameter pack. + * @tparam Rest The remaining boolean values in the parameter pack. + * + * @note The base case for this recursion should be defined elsewhere to + * terminate the recursion. + */ template struct LogicalOrMultiple { static constexpr bool value = LogicalOr::value>::value; }; -// helper template to calculate the logical OR for SparseAvailable +/** + * @brief Template struct to check if any of the sparse columns are available. + * + * This struct is used to determine if at least one column in a sparse matrix is + * available (i.e., has non-zero entries). It is specialized for both + * ColumnAvailable and SparseAvailable types. + * + * @tparam SparseAvailable A type representing the availability of sparse + * columns. + */ template struct CheckSparseAvailableEmpty; -// partial specialization for ColumnAvailable +/** + * @brief Specialization of CheckSparseAvailableEmpty for ColumnAvailable. + * + * This template struct checks if any of the boolean template parameters in + * ColumnAvailable are true. It uses TemplatesOperation::LogicalOrMultiple to + * compute the logical OR of all Values. + * + * @tparam Values Variadic boolean template parameters representing column + * availability. + * + * The static constexpr bool 'value' will be true if at least one of the Values + * is true, indicating that at least one column is available (not empty). + */ template struct CheckSparseAvailableEmpty> { static constexpr bool value = TemplatesOperation::LogicalOrMultiple::value; }; -// partial specialization for SparseAvailable +/** + * @brief Trait to check if any of the provided columns are considered "sparse + * available and empty". + * + * This specialization of CheckSparseAvailableEmpty for SparseAvailable types + * recursively checks each column type in the parameter pack. It uses + * TemplatesOperation::LogicalOrMultiple to compute the logical OR of the + * CheckSparseAvailableEmpty value for each column. + * + * @tparam Columns Variadic template parameter pack representing column types. + * + * @note The resulting static constexpr bool 'value' will be true if at least + * one column is considered sparse available and empty, false otherwise. + */ template struct CheckSparseAvailableEmpty> { static constexpr bool value = TemplatesOperation::LogicalOrMultiple< @@ -125,36 +399,106 @@ struct CheckSparseAvailableEmpty> { /* Create Dense Available */ -// Generate false flags -// base case: N = 0 +/** + * @brief Metafunction to generate a parameter pack of boolean flags, all set to + * false. + * + * This template recursively constructs a type alias containing a parameter pack + * of N boolean values, all initialized to false. It is typically used in + * template metaprogramming to generate compile-time sequences of boolean flags. + * + * @tparam N The number of boolean flags to generate. + * @tparam Flags The accumulated boolean flags (used internally during + * recursion). + */ template struct GenerateFalseFlags { using type = typename GenerateFalseFlags::type; }; -// recursive termination case: N = 0 +/** + * @brief Template specialization for GenerateFalseFlags when the first template + * parameter is 0. + * + * This specialization defines a type alias 'type' that is set to + * ColumnAvailable, effectively propagating the boolean parameter pack + * 'Flags...' to the ColumnAvailable template. + * + * @tparam Flags Variadic boolean template parameters representing flag values. + */ template struct GenerateFalseFlags<0, Flags...> { using type = ColumnAvailable; }; +/** + * @brief Alias template to generate a type representing a sequence of 'false' + * flags for columns. + * + * This alias uses the GenerateFalseFlags template to create a type with N + * 'false' values, typically used to indicate the availability status of columns + * in a matrix (all unavailable). + * + * @tparam N The number of columns (flags) to generate. + * @see GenerateFalseFlags + */ template using GenerateFalseColumnAvailable = typename GenerateFalseFlags::type; -// Generate true flags -// base case: N = 0 +/** + * @brief Metafunction to generate a parameter pack of boolean template + * arguments, prepending 'true' N times to the pack. + * + * @tparam N The number of 'true' flags to prepend. + * @tparam Flags The existing boolean flags in the parameter pack. + * + * This primary template recursively prepends 'true' to the Flags parameter pack + * N times. The recursion is expected to be terminated by a specialization for N + * == 0. + */ template struct GenerateTrueFlags { using type = typename GenerateTrueFlags::type; }; -// recursive termination case: N = 0 +/** + * @brief Specialization of the GenerateTrueFlags template for the case when the + * first template parameter is 0. + * + * This specialization defines a type alias 'type' that is set to + * ColumnAvailable, effectively forwarding the remaining boolean + * template parameters (Flags...) to the ColumnAvailable template. + * + * @tparam Flags Variadic boolean template parameters representing flag values. + */ template struct GenerateTrueFlags<0, Flags...> { using type = ColumnAvailable; }; +/** + * @brief Alias template to generate a type representing a sequence of 'true' + * flags for a given size. + * + * This alias uses the GenerateTrueFlags metafunction to create a type + * (typically a type list or similar) containing N 'true' values, which can be + * used for compile-time flag management or template metaprogramming. + * + * @tparam N The number of 'true' flags to generate. + * + * @see GenerateTrueFlags + */ template using GenerateTrueColumnAvailable = typename GenerateTrueFlags::type; -// concatenate true flags vertically -// base case: N = 0 +/** + * @brief Recursively constructs a type by repeating the ColumnAvailable type M + * times in a parameter pack. + * + * @tparam M The number of times to repeat the ColumnAvailable type. + * @tparam ColumnAvailable The type to be repeated. + * @tparam Columns The parameter pack accumulating the repeated types. + * + * This template recursively instantiates itself, decrementing M each time, + * and prepending ColumnAvailable to the Columns parameter pack, until a base + * case is reached. + */ template struct RepeatColumnAvailable { using type = @@ -162,7 +506,19 @@ struct RepeatColumnAvailable { Columns...>::type; }; -// recursive termination case: N = 0 +/** + * @brief Specialization of RepeatColumnAvailable for the case when the first + * template parameter is 0. + * + * This specialization defines a type alias `type` that is set to + * `TemplatesOperation::SparseAvailableColumns`, effectively + * collecting the remaining columns into a sparse column representation. + * + * @tparam ColumnAvailable Unused in this specialization, but required for + * template matching. + * @tparam Columns Variadic template parameter pack representing the remaining + * columns. + */ template struct RepeatColumnAvailable<0, ColumnAvailable, Columns...> { using type = TemplatesOperation::SparseAvailableColumns; @@ -170,11 +526,37 @@ struct RepeatColumnAvailable<0, ColumnAvailable, Columns...> { } // namespace TemplatesOperation -// repeat ColumnAvailable M times +/** + * @brief Alias template to determine the availability of dense matrix columns. + * + * This alias uses TemplatesOperation utilities to generate a compile-time + * type indicating which columns are available in a dense matrix of size MxN. + * + * @tparam M Number of rows in the matrix. + * @tparam N Number of columns in the matrix. + * + * The resulting type is computed by repeating the column availability + * (as generated by GenerateTrueColumnAvailable) for M rows using + * RepeatColumnAvailable. + */ template using DenseAvailable = typename TemplatesOperation::RepeatColumnAvailable< M, TemplatesOperation::GenerateTrueColumnAvailable>::type; +/** + * @brief Alias template for creating a dense matrix availability mask with all + * entries set to false. + * + * This alias uses TemplatesOperation utilities to generate a column + * availability type for a matrix of size M x N, where all columns are marked as + * unavailable (false). + * + * @tparam M Number of rows in the matrix. + * @tparam N Number of columns in the matrix. + * + * @see TemplatesOperation::RepeatColumnAvailable + * @see TemplatesOperation::GenerateFalseColumnAvailable + */ template using DenseAvailableEmpty = typename TemplatesOperation::RepeatColumnAvailable< M, TemplatesOperation::GenerateFalseColumnAvailable>::type; @@ -183,25 +565,77 @@ using DenseAvailableEmpty = typename TemplatesOperation::RepeatColumnAvailable< namespace TemplatesOperation { -// base case: N = 0 +/** + * @brief Metafunction to generate a parameter pack of boolean flags with a + * single true value at a specified index. + * + * This template recursively constructs a boolean parameter pack of length N, + * where only the element at position Index is true, and all other elements are + * false. The generated type can be used for compile-time selection or + * specialization based on index. + * + * @tparam N The total number of boolean flags to generate. + * @tparam Index The index at which the flag should be set to true. + * @tparam Flags The accumulated boolean flags during recursion. + */ template struct GenerateIndexedTrueFlags { using type = typename GenerateIndexedTrueFlags< N - 1, Index, (N - 1 == Index ? true : false), Flags...>::type; }; -// recursive termination case: N = 0 +/** + * @brief Specialization of the GenerateIndexedTrueFlags template for the case + * when the first template parameter is 0. + * + * This specialization defines a type alias 'type' that is set to + * ColumnAvailable, effectively forwarding the boolean parameter pack + * Flags to the ColumnAvailable template. + * + * @tparam Index The index value (unused in this specialization). + * @tparam Flags A parameter pack of boolean values representing column + * availability. + */ template struct GenerateIndexedTrueFlags<0, Index, Flags...> { using type = ColumnAvailable; }; +/** + * @brief Alias template to generate a type representing a sequence of boolean + * flags, where the flag at position Index is set to true and all others are + * false. + * + * @tparam N The total number of flags (columns). + * @tparam Index The index of the flag to be set to true. + * + * This alias uses GenerateIndexedTrueFlags to produce a type (typically a + * std::integer_sequence or similar) with N elements, where only the element at + * position Index is true. + */ template using GenerateIndexedTrueColumnAvailable = typename GenerateIndexedTrueFlags::type; -// concatenate indexed true flags vertically -// base case: N = 0 +/** + * @brief Recursively constructs a type list by repeating a column availability + * type for a matrix. + * + * This template recursively builds a type list representing the availability of + * columns in a matrix, by prepending a new column availability type at each + * recursion step. The recursion continues until the base case (not shown here) + * is reached. + * + * @tparam M The number of columns remaining to process. + * @tparam N The number of rows in the matrix. + * @tparam Index The current index for which the column availability is being + * generated. + * @tparam ColumnAvailable The type representing the availability of the current + * column. + * @tparam Columns The accumulated list of column availability types. + * + * The resulting type is accessible via the nested ::type member. + */ template struct IndexedRepeatColumnAvailable { @@ -211,7 +645,22 @@ struct IndexedRepeatColumnAvailable { Columns...>::type; }; -// recursive termination case: N = 0 +/** + * @brief Specialization of IndexedRepeatColumnAvailable for the case when M is + * 0. + * + * This specialization defines a type alias 'type' that is set to + * TemplatesOperation::SparseAvailableColumns, effectively + * collecting the repeated column availability types into a sparse column + * representation. + * + * @tparam N The number of rows in the matrix (unused in this specialization). + * @tparam Index The current index (unused in this specialization). + * @tparam ColumnAvailable The type representing the availability of the current + * column (unused in this specialization). + * @tparam Columns Variadic template parameter pack representing the accumulated + * column availability types. + */ template struct IndexedRepeatColumnAvailable<0, N, Index, ColumnAvailable, Columns...> { @@ -220,7 +669,19 @@ struct IndexedRepeatColumnAvailable<0, N, Index, ColumnAvailable, Columns...> { } // namespace TemplatesOperation -// repeat ColumnAvailable M times +/** + * @brief Alias template to create a diagonal availability mask for a square + * matrix of size MxM. + * + * This alias uses the TemplatesOperation::IndexedRepeatColumnAvailable to + * generate a type representing the availability of columns in a diagonal + * matrix, where only the diagonal elements are available (true). + * + * @tparam M The size of the square matrix (number of rows and columns). + * + * The resulting type is a SparseAvailableColumns type with M columns, where + * each column has only one true value at its diagonal position. + */ template using DiagAvailable = typename TemplatesOperation::IndexedRepeatColumnAvailable< M, M, (M - 1), @@ -230,7 +691,19 @@ using DiagAvailable = typename TemplatesOperation::IndexedRepeatColumnAvailable< namespace TemplatesOperation { -// base case: N = 0 +/** + * @brief Metafunction to generate a compile-time list of boolean flags by + * performing a logical OR operation on two ColumnAvailable types. + * + * This template recursively combines the boolean flags from two ColumnAvailable + * types, producing a new ColumnAvailable type that reflects the logical OR of + * the flags at each index. + * + * @tparam ColumnAvailable_A The first ColumnAvailable type. + * @tparam ColumnAvailable_B The second ColumnAvailable type. + * @tparam N The number of columns (size of the lists). + * @tparam Flags The accumulated boolean flags during recursion. + */ template struct GenerateORTrueFlagsLoop { @@ -240,18 +713,59 @@ struct GenerateORTrueFlagsLoop { Flags...>::type; }; -// recursive termination case: N = 0 +/** + * @brief Specialization of GenerateORTrueFlagsLoop for the case when N is 0. + * + * This specialization defines a type alias 'type' that is set to + * ColumnAvailable, effectively collecting the accumulated boolean + * flags into a ColumnAvailable type. + * + * @tparam ColumnAvailable_A The first ColumnAvailable type (unused in this + * specialization). + * @tparam ColumnAvailable_B The second ColumnAvailable type (unused in this + * specialization). + * @tparam Flags The accumulated boolean flags representing the logical OR of + * the two ColumnAvailable types. + */ template struct GenerateORTrueFlagsLoop { using type = ColumnAvailable; }; +/** + * @brief Alias template to generate a ColumnAvailable type representing the + * logical OR of two ColumnAvailable types. + * + * This alias uses the GenerateORTrueFlagsLoop to compute the logical OR of the + * flags in ColumnAvailable_A and ColumnAvailable_B, producing a new + * ColumnAvailable type that reflects the combined availability of both columns. + * + * @tparam ColumnAvailable_A The first ColumnAvailable type. + * @tparam ColumnAvailable_B The second ColumnAvailable type. + * + * The resulting type is a ColumnAvailable type with the logical OR of the + * flags from both input ColumnAvailable types. + */ template using GenerateORTrueFlagsColumnAvailable = typename GenerateORTrueFlagsLoop::type; +/** + * @brief Metafunction to create a sparse pointers row loop for generating + * ColumnAvailable types based on row indices and pointers. + * + * This template recursively constructs a ColumnAvailable type for each row, + * based on the provided row indices and pointers. It generates a type that + * indicates which columns are available (true) or not (false) for each row. + * + * @tparam RowIndices A type representing the row indices. + * @tparam N The number of columns in the matrix. + * @tparam RowIndicesIndex The current index in the RowIndices list being + * processed. + * @tparam RowEndCount The number of rows remaining to process. + */ template struct CreateSparsePointersRowLoop { @@ -262,11 +776,41 @@ struct CreateSparsePointersRowLoop { N>::type; }; +/** + * @brief Specialization of CreateSparsePointersRowLoop for the case when + * RowEndCount is 0. + * + * This specialization defines a type alias 'type' that is set to + * GenerateFalseColumnAvailable, effectively creating a ColumnAvailable type + * with all columns marked as unavailable (false). + * + * @tparam RowIndices A type representing the row indices (unused in this + * specialization). + * @tparam N The number of columns in the matrix. + * @tparam RowIndicesIndex The current index in the RowIndices list (unused in + * this specialization). + */ template struct CreateSparsePointersRowLoop { using type = GenerateFalseColumnAvailable; }; +/** + * @brief Metafunction to create a loop for generating sparse pointers based on + * row indices and pointers. + * + * This template recursively constructs a SparseAvailableColumns type by + * iterating over the row pointers and generating ColumnAvailable types for each + * row based on the row indices and pointers. + * + * @tparam N The number of columns in the matrix. + * @tparam RowIndices A type representing the row indices. + * @tparam RowPointers A type representing the row pointers. + * @tparam EndCount The number of rows remaining to process. + * @tparam ConsecutiveIndex The current index in the RowPointers list being + * processed. + * @tparam Columns The accumulated ColumnAvailable types during recursion. + */ template @@ -280,6 +824,25 @@ struct CreateSparsePointersLoop { RowPointers::list[RowPointers::size - EndCount - 1])>::type>::type; }; +/** + * @brief Specialization of CreateSparsePointersLoop for the case when EndCount + * is 0. + * + * This specialization defines a type alias 'type' that is set to + * SparseAvailableColumns, effectively collecting the accumulated + * ColumnAvailable types into a sparse column representation. + * + * @tparam N The number of columns in the matrix (unused in this + * specialization). + * @tparam RowIndices A type representing the row indices (unused in this + * specialization). + * @tparam RowPointers A type representing the row pointers (unused in this + * specialization). + * @tparam ConsecutiveIndex The current index in the RowPointers list (unused in + * this specialization). + * @tparam Columns Variadic template parameter pack representing the accumulated + * ColumnAvailable types. + */ template struct CreateSparsePointersLoop using CreateSparseAvailableFromIndicesAndPointers = typename TemplatesOperation::CreateSparsePointersLoop< @@ -297,60 +877,212 @@ using CreateSparseAvailableFromIndicesAndPointers = namespace TemplatesOperation { /* Create Sparse Matrix from Dense Matrix */ + +/** + * @brief A template struct that represents a compile-time list of indices. + * + * This struct provides a static constexpr array `list` containing the indices + * passed as template parameters, and a static constexpr `size` indicating the + * number of indices. + * + * @tparam Seq Variadic template parameters representing the list of indices. + * + * Example usage: + * @code + * using MyIndices = IndexSequence<0, 1, 2>; + * // MyIndices::size == 3 + * // MyIndices::list == {0, 1, 2} + * @endcode + */ template struct IndexSequence { static constexpr std::size_t size = sizeof...(Seq); static constexpr std::size_t list[size] = {Seq...}; }; +/** + * @brief A template struct that represents an invalid sequence of indices. + * + * This struct provides a static constexpr array `list` containing the indices + * passed as template parameters, and a static constexpr `size` indicating the + * number of indices. It is used to represent an invalid or empty sequence. + * + * @tparam Seq Variadic template parameters representing the list of indices. + */ template struct InvalidSequence { static constexpr std::size_t size = sizeof...(Seq); static constexpr std::size_t list[size] = {Seq...}; }; +/** + * @brief A template struct to create a compile-time index sequence of size N. + * + * This struct recursively generates an IndexSequence of size N, where each + * index is filled with the corresponding value from 0 to N-1. + * + * @tparam N The size of the index sequence to be generated. + * @tparam Seq The accumulated indices during recursion. + */ template struct MakeIndexSequence : MakeIndexSequence {}; +/** + * @brief Specialization of MakeIndexSequence for the base case when N is 0. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence, effectively collecting the accumulated indices into an + * IndexSequence. + * + * @tparam Seq The accumulated indices during recursion. + */ template struct MakeIndexSequence<0, Seq...> { using type = IndexSequence; }; +/** + * @brief A template struct to create a compile-time index sequence of size N. + * + * This struct provides a type alias 'type' that is set to the result of + * MakeIndexSequence::type, effectively generating an IndexSequence of size + * N. + * + * @tparam N The size of the index sequence to be generated. + */ template struct IntegerSequenceList { using type = typename MakeIndexSequence::type; }; +/** + * @brief A template struct to create a compile-time list of row indices for a + * matrix with N columns. + * + * This struct provides a type alias 'type' that is set to IndexSequence<0, 1, + * ..., N-1>, effectively generating a list of row indices for a matrix with N + * columns. + * + * @tparam N The number of columns in the matrix. + */ template using MatrixRowNumbers = typename IntegerSequenceList::type; +/** + * @brief A template struct to concatenate two index sequences or invalid + * sequences. + * + * This struct provides a type alias 'type' that is the result of concatenating + * two IndexSequence or InvalidSequence types. If one of the sequences is an + * InvalidSequence, it will return the other sequence. + * + * @tparam Seq1 The first index sequence. + * @tparam Seq2 The second index sequence. + */ template struct Concatenate; +/** + * @brief Specialization of Concatenate for two IndexSequence types. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence, effectively concatenating the two index + * sequences. + * + * @tparam Seq1 The first index sequence. + * @tparam Seq2 The second index sequence. + */ template struct Concatenate, IndexSequence> { using type = IndexSequence; }; +/** + * @brief Specialization of Concatenate for an IndexSequence and an + * InvalidSequence. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence, effectively returning the first sequence when the + * second sequence is invalid. + * + * @tparam Seq1 The first index sequence. + * @tparam Seq2 The second invalid sequence. + */ template struct Concatenate, InvalidSequence> { using type = IndexSequence; }; +/** + * @brief Specialization of Concatenate for an InvalidSequence and an + * IndexSequence. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence, effectively returning the second sequence when the + * first sequence is invalid. + * + * @tparam Seq1 The first invalid sequence. + * @tparam Seq2 The second index sequence. + */ template struct Concatenate, IndexSequence> { using type = IndexSequence; }; /* Create Dense Matrix Row Indices and Pointers */ + +/** + * @brief Concatenates two index sequences representing matrix row numbers. + * + * This alias template uses the Concatenate metafunction to combine two index + * sequences, resulting in a new index sequence that contains all indices from + * both input sequences. + * + * @tparam IndexSequence_1 The first index sequence to concatenate. + * @tparam IndexSequence_2 The second index sequence to concatenate. + * + * The resulting type is a concatenated IndexSequence containing all indices + * from both input sequences. + */ template using ConcatenateMatrixRowNumbers = typename Concatenate::type; +/** + * @brief Recursively concatenates a given index sequence M times. + * + * This template recursively constructs a new index sequence by repeating the + * provided MatrixRowNumbers type M times. It is used to generate a sequence of + * row indices for a matrix with M rows and N columns. + * + * @tparam M The number of times to repeat the index sequence. + * @tparam MatrixRowNumbers The index sequence to be repeated. + */ template struct RepeatConcatenateIndexSequence; +/** + * @brief Specialization of RepeatConcatenateIndexSequence for the case when M + * is 1. + * + * This specialization defines a type alias 'type' that is set to + * MatrixRowNumbers, effectively returning the original index sequence when M is + * 1. + * + * @tparam MatrixRowNumbers The index sequence to be returned. + */ template struct RepeatConcatenateIndexSequence<1, MatrixRowNumbers> { using type = MatrixRowNumbers; }; +/** + * @brief Specialization of RepeatConcatenateIndexSequence for the case when M + * is greater than 1. + * + * This specialization defines a type alias 'type' that is set to + * ConcatenateMatrixRowNumbers, effectively concatenating the MatrixRowNumbers + * with the result of recursively calling RepeatConcatenateIndexSequence with M + * decremented by 1. + * + * @tparam M The number of times to repeat the index sequence (M > 1). + * @tparam MatrixRowNumbers The index sequence to be repeated. + */ template struct RepeatConcatenateIndexSequence { using type = ConcatenateMatrixRowNumbers< @@ -358,41 +1090,147 @@ struct RepeatConcatenateIndexSequence { typename RepeatConcatenateIndexSequence::type>; }; +/** + * @brief A template struct to convert an IndexSequence into a RowIndices type. + * + * This struct provides a type alias 'type' that is set to RowIndices, + * effectively converting the IndexSequence into a RowIndices type. + * + * @tparam Seq The index sequence to be converted. + */ template struct ToRowIndices; +/** + * @brief Specialization of ToRowIndices for IndexSequence types. + * + * This specialization defines a type alias 'type' that is set to + * RowIndices, effectively converting the IndexSequence into a + * RowIndices type. + * + * @tparam Seq The index sequence to be converted. + */ template struct ToRowIndices> { using type = RowIndices; }; +/** + * @brief A template struct to generate a sequence of row numbers for a matrix + * with M rows and N columns. + * + * This struct provides a type alias 'type' that is set to a repeated + * concatenation of MatrixRowNumbers M times, effectively generating a list + * of row indices for the matrix. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template using MatrixColumnRowNumbers = typename RepeatConcatenateIndexSequence>::type; } // namespace TemplatesOperation +/** + * @brief Alias template to create a list of row indices for a dense matrix of + * size MxN. + * + * This alias uses the TemplatesOperation::MatrixColumnRowNumbers to generate + * a type representing the row indices for a dense matrix with M rows and N + * columns. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * + * The resulting type is an IndexSequence containing the row indices for the + * dense matrix. + */ template using DenseMatrixRowIndices = typename TemplatesOperation::ToRowIndices< TemplatesOperation::MatrixColumnRowNumbers>::type; namespace TemplatesOperation { +/** + * @brief A template struct to create a list of pointers for a dense matrix of + * size MxN. + * + * This struct recursively generates a list of pointers for each row in the + * matrix, where each pointer points to the start of a row in the matrix. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam Seq The accumulated pointers during recursion. + * + * The resulting type is an IndexSequence containing the pointers for each row + * in the dense matrix. + */ template struct MakePointerList : MakePointerList {}; +/** + * @brief Specialization of MakePointerList for the case when M is 0. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence<0, Seq...>, effectively collecting the accumulated pointers + * into an IndexSequence. + * + * @tparam N The number of columns in the matrix (unused in this + * specialization). + * @tparam Seq The accumulated pointers during recursion. + */ template struct MakePointerList<0, N, Seq...> { using type = IndexSequence<0, Seq...>; }; +/** + * @brief A template struct to create a list of pointers for a dense matrix of + * size MxN. + * + * This struct provides a type alias 'type' that is set to the result of + * MakePointerList::type, effectively generating a list of pointers for + * each row in the dense matrix. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template struct MatrixDensePointerList { using type = typename MakePointerList::type; }; +/** + * @brief A template struct to create a list of row pointers for a dense matrix + * of size MxN. + * + * This struct provides a type alias 'type' that is set to RowPointers, + * effectively converting the IndexSequence generated by MakePointerList into a + * RowPointers type. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template using MatrixColumnRowPointers = typename MatrixDensePointerList::type; +/** + * @brief A template struct to convert an IndexSequence into a RowPointers type. + * + * This struct provides a type alias 'type' that is set to RowPointers, + * effectively converting the IndexSequence into a RowPointers type. + * + * @tparam Seq The index sequence to be converted. + */ template struct ToRowPointers; +/** + * @brief Specialization of ToRowPointers for IndexSequence types. + * + * This specialization defines a type alias 'type' that is set to + * RowPointers, effectively converting the IndexSequence into a + * RowPointers type. + * + * @tparam Seq The index sequence to be converted. + */ template struct ToRowPointers> { using type = RowPointers; @@ -400,6 +1238,20 @@ struct ToRowPointers> { } // namespace TemplatesOperation +/** + * @brief Alias template to create a list of row pointers for a dense matrix of + * size MxN. + * + * This alias uses the TemplatesOperation::MatrixColumnRowPointers to generate + * a type representing the row pointers for a dense matrix with M rows and N + * columns. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * + * The resulting type is a RowPointers type containing the row pointers for the + * dense matrix. + */ template using DenseMatrixRowPointers = typename TemplatesOperation::ToRowIndices< TemplatesOperation::MatrixColumnRowPointers>::type; @@ -408,9 +1260,34 @@ using DenseMatrixRowPointers = typename TemplatesOperation::ToRowIndices< namespace TemplatesOperation { +/** + * @brief A template struct to concatenate two ColumnAvailable types into a new + * ColumnAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * two ColumnAvailable types, effectively combining their boolean flags into a + * single ColumnAvailable type. + * + * @tparam ColumnAvailable_A The first ColumnAvailable type. + * @tparam ColumnAvailable_B The second ColumnAvailable type. + * + * The resulting type is a ColumnAvailable type that contains the combined + * boolean flags from both input ColumnAvailable types. + */ template struct ConcatenateColumnAvailableLists; +/** + * @brief Specialization of ConcatenateColumnAvailableLists for two + * ColumnAvailable types. + * + * This specialization defines a type alias 'type' that is set to + * ColumnAvailable, effectively concatenating the boolean + * flags from both ColumnAvailable types into a new ColumnAvailable type. + * + * @tparam Flags1 The boolean flags from the first ColumnAvailable type. + * @tparam Flags2 The boolean flags from the second ColumnAvailable type. + */ template struct ConcatenateColumnAvailableLists, ColumnAvailable> { @@ -419,6 +1296,20 @@ struct ConcatenateColumnAvailableLists, } // namespace TemplatesOperation +/** + * @brief Alias template to concatenate two ColumnAvailable types into a new + * ColumnAvailable type. + * + * This alias uses the TemplatesOperation::ConcatenateColumnAvailableLists to + * combine two ColumnAvailable types, effectively merging their boolean flags + * into a single ColumnAvailable type. + * + * @tparam ColumnAvailable_A The first ColumnAvailable type. + * @tparam ColumnAvailable_B The second ColumnAvailable type. + * + * The resulting type is a ColumnAvailable type that contains the combined + * boolean flags from both input ColumnAvailable types. + */ template using ConcatenateColumnAvailable = typename TemplatesOperation::ConcatenateColumnAvailableLists< @@ -428,8 +1319,30 @@ using ConcatenateColumnAvailable = namespace TemplatesOperation { +/** + * @brief A template struct to get the ColumnAvailable type at a specific index + * from a SparseAvailable type. + * + * This struct provides a type alias 'type' that is set to the ColumnAvailable + * type at index N in the SparseAvailable type, effectively extracting the + * availability information for that specific column. + * + * @tparam N The index of the column to retrieve. + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct GetColumnAvailable; +/** + * @brief Specialization of GetColumnAvailable for SparseAvailable types. + * + * This specialization defines a type alias 'type' that is set to the + * std::tuple_element>::type, effectively extracting + * the ColumnAvailable type at index N from the SparseAvailable type. + * + * @tparam N The index of the column to retrieve. + * @tparam Columns The variadic template parameter pack representing the columns + * in the SparseAvailable type. + */ template struct GetColumnAvailable> { using type = typename std::tuple_element>::type; @@ -438,9 +1351,36 @@ struct GetColumnAvailable> { } // namespace TemplatesOperation /* Concatenate SparseAvailable vertically */ + +/** + * @brief A template struct to concatenate two SparseAvailable types vertically. + * + * This struct provides a type alias 'type' that is the result of concatenating + * two SparseAvailable types, effectively merging their columns into a new + * SparseAvailable type. + * + * @tparam SparseAvailable1 The first SparseAvailable type. + * @tparam SparseAvailable2 The second SparseAvailable type. + * + * The resulting type is a SparseAvailable type that contains all columns from + * both input SparseAvailable types. + */ template struct ConcatenateSparseAvailable; +/** + * @brief Specialization of ConcatenateSparseAvailable for two SparseAvailable + * types. + * + * This specialization defines a type alias 'type' that is set to + * SparseAvailableColumns, effectively concatenating + * the columns from both SparseAvailable types into a new SparseAvailable type. + * + * @tparam Columns1 The variadic template parameter pack representing the + * columns in the first SparseAvailable type. + * @tparam Columns2 The variadic template parameter pack representing the + * columns in the second SparseAvailable type. + */ template struct ConcatenateSparseAvailable< TemplatesOperation::SparseAvailableColumns, @@ -449,6 +1389,18 @@ struct ConcatenateSparseAvailable< TemplatesOperation::SparseAvailableColumns; }; +/** + * @brief Alias template to concatenate two SparseAvailable types vertically. + * + * This alias uses the ConcatenateSparseAvailable to combine two SparseAvailable + * types, effectively merging their columns into a single SparseAvailable type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * + * The resulting type is a SparseAvailable type that contains all columns from + * both input SparseAvailable types. + */ template using ConcatenateSparseAvailableVertically = typename ConcatenateSparseAvailable struct ConcatenateSparseAvailableHorizontallyLoop { @@ -470,6 +1433,20 @@ struct ConcatenateSparseAvailableHorizontallyLoop { SparseAvailable_B>::type>::type>>::type; }; +/** + * @brief Specialization of ConcatenateSparseAvailableHorizontallyLoop for the + * base case when ColumnCount is 0. + * + * This specialization defines a type alias 'type' that is set to + * SparseAvailableColumns, effectively creating a SparseAvailableColumns type + * with the combined availability of the first columns from both SparseAvailable + * types. + * + * @tparam SparseAvailable_A The first SparseAvailable type (unused in this + * specialization). + * @tparam SparseAvailable_B The second SparseAvailable type (unused in + * this specialization). + */ template struct ConcatenateSparseAvailableHorizontallyLoop { @@ -480,6 +1457,20 @@ struct ConcatenateSparseAvailableHorizontallyLoop using ConcatenateSparseAvailableHorizontally = typename TemplatesOperation::ConcatenateSparseAvailableHorizontallyLoop< @@ -490,23 +1481,82 @@ using ConcatenateSparseAvailableHorizontally = namespace TemplatesOperation { -// Helper template to concatenate two template parameter packs +/** + * @brief A template struct to concatenate a ColumnAvailable type with a + * SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * a ColumnAvailable type with a SparseAvailable type, effectively merging their + * columns into a new SparseAvailable type. + * + * @tparam Column The ColumnAvailable type to be concatenated. + * @tparam Sparse The SparseAvailable type to be concatenated. + */ template struct ConcatTuple; +/** + * @brief Specialization of ConcatTuple for two std::tuple types. + * + * This specialization defines a type alias 'type' that is set to + * std::tuple, effectively concatenating the elements of two + * tuples into a new tuple. + * + * @tparam Ts1 The variadic template parameter pack representing the first + * tuple. + * @tparam Ts2 The variadic template parameter pack representing the second + * tuple. + */ template struct ConcatTuple, std::tuple> { using type = std::tuple; }; -// Template to concatenate ColumnAvailable and SparseAvailable +/** + * @brief A template struct to concatenate a ColumnAvailable type with a + * SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * a ColumnAvailable type with a SparseAvailable type, effectively merging their + * columns into a new SparseAvailable type. + * + * @tparam Column The ColumnAvailable type to be concatenated. + * @tparam Sparse The SparseAvailable type to be concatenated. + */ template struct ConcatColumnSparse; +/** + * @brief Specialization of ConcatColumnSparse for a ColumnAvailable type and a + * SparseAvailable type. + * + * This specialization defines a type alias 'type' that is set to + * SparseAvailableColumns::type...>, effectively + * concatenating the ColumnAvailable type with each column in the + * SparseAvailable type. + * + * @tparam Column The ColumnAvailable type to be concatenated. + * @tparam Columns The variadic template parameter pack representing the columns + * in the SparseAvailable type. + */ template struct ConcatColumnSparse> { using type = SparseAvailable; }; /* Get rest of SparseAvailable */ + +/** + * @brief A template struct to get the rest of SparseAvailable starting from a + * specific column index. + * + * This struct provides a type alias 'type' that is the result of concatenating + * the ColumnAvailable type at the specified column index with the rest of the + * SparseAvailable columns. + * + * @tparam SparseAvailable_In The SparseAvailable type containing multiple + * columns. + * @tparam Col_Index The index of the column to start from. + * @tparam Residual The number of remaining columns to process. + */ template struct GetRestOfSparseAvailableLoop { @@ -516,26 +1566,86 @@ struct GetRestOfSparseAvailableLoop { (Residual - 1)>::type>::type; }; +/** + * @brief Specialization of GetRestOfSparseAvailableLoop for the case when + * Residual is 0. + * + * This specialization defines a type alias 'type' that is set to + * SparseAvailable, effectively creating a SparseAvailable type + * with the ColumnAvailable type at the specified column index. + * + * @tparam SparseAvailable_In The SparseAvailable type containing multiple + * columns. + * @tparam Col_Index The index of the column to start from. + */ template struct GetRestOfSparseAvailableLoop { using type = SparseAvailable< typename GetColumnAvailable::type>; }; +/** + * @brief A template alias to get the rest of SparseAvailable starting from a + * specific column index. + * + * This alias uses the GetRestOfSparseAvailableLoop to generate a + * SparseAvailable type that contains the ColumnAvailable types starting from + * the specified column index. + * + * @tparam SparseAvailable_In The SparseAvailable type containing multiple + * columns. + * @tparam Col_Index The index of the column to start from. + * + * The resulting type is a SparseAvailable type that contains all columns from + * the specified index onward. + */ template using GetRestOfSparseAvailable = typename GetRestOfSparseAvailableLoop< SparseAvailable_In, Col_Index, ((SparseAvailable_In::number_of_columns - 1) - Col_Index)>::type; +/** + * @brief A template struct to check if a SparseAvailable type is empty. + * + * This struct provides a static constexpr boolean value 'value' that is + * true if the SparseAvailable type has no columns, and false otherwise. + * + * @tparam SparseAvailable The SparseAvailable type to check. + */ template struct AvoidEmptyColumnsSparseAvailableLoop; +/** + * @brief Specialization of AvoidEmptyColumnsSparseAvailableLoop for the case + * when the SparseAvailable type is empty. + * + * This specialization defines a type alias 'type' that is set to + * GetRestOfSparseAvailable, effectively + * returning the rest of the SparseAvailable starting from the specified column + * index when it is not empty. + * + * @tparam SparseAvailable_In The SparseAvailable type containing multiple + * columns. + * @tparam Col_Index The index of the column to start from. + */ template struct AvoidEmptyColumnsSparseAvailableLoop { using type = GetRestOfSparseAvailable; }; +/** + * @brief Specialization of AvoidEmptyColumnsSparseAvailableLoop for the case + * when the SparseAvailable type is not empty. + * + * This specialization defines a type alias 'type' that is set to the result of + * recursively calling AvoidEmptyColumnsSparseAvailableLoop with the next column + * index, effectively skipping empty columns in the SparseAvailable type. + * + * @tparam SparseAvailable_In The SparseAvailable type containing multiple + * columns. + * @tparam Col_Index The index of the column to start from. + */ template struct AvoidEmptyColumnsSparseAvailableLoop { @@ -545,6 +1655,19 @@ struct AvoidEmptyColumnsSparseAvailableLoop::type>::value>::type; }; +/** + * @brief A template alias to avoid empty columns in a SparseAvailable type. + * + * This alias uses the AvoidEmptyColumnsSparseAvailableLoop to generate a + * SparseAvailable type that contains only the non-empty columns from the input + * SparseAvailable type. + * + * @tparam SparseAvailable_In The SparseAvailable type containing multiple + * columns. + * + * The resulting type is a SparseAvailable type that contains only the columns + * that are not empty, effectively filtering out any empty columns. + */ template using AvoidEmptyColumnsSparseAvailable = typename AvoidEmptyColumnsSparseAvailableLoop< @@ -554,10 +1677,33 @@ using AvoidEmptyColumnsSparseAvailable = type; /* Create Row Indices */ + +/** + * @brief A template struct to create a sequence of row indices for a sparse + * matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a sequence of row indices for each column in the SparseAvailable + * type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct AssignSparseMatrixRowLoop; +/** + * @brief Specialization of AssignSparseMatrixRowLoop for the case when the row + * is active. + * + * This specialization defines a type alias 'type' that is set to the result of + * concatenating the current row index with the result of recursively calling + * AssignSparseMatrixRowLoop with the next row index. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ColumnElementNumber The index of the column being processed. + * @tparam RowElementNumber The index of the row being processed. + */ template struct AssignSparseMatrixRowLoop>::type; }; +/** + * @brief Specialization of AssignSparseMatrixRowLoop for the case when the row + * is not active. + * + * This specialization defines a type alias 'type' that is set to the result of + * recursively calling AssignSparseMatrixRowLoop with the next row index, + * effectively skipping the current row. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ColumnElementNumber The index of the column being processed. + * @tparam RowElementNumber The index of the row being processed. + */ template struct AssignSparseMatrixRowLoop::type; }; +/** + * @brief Specialization of AssignSparseMatrixRowLoop for the case when there + * are no more rows to process. + * + * This specialization defines a type alias 'type' that is set to an + * InvalidSequence, effectively indicating that there are no valid row indices + * to return. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct AssignSparseMatrixRowLoop { using type = InvalidSequence<0>; }; +/** + * @brief Specialization of AssignSparseMatrixRowLoop for the case when there + * are no more rows to process and the row is active. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence<0>, effectively indicating that there are no valid row indices + * to return. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct AssignSparseMatrixRowLoop { using type = IndexSequence<0>; }; +/** + * @brief A template struct to assign row indices for each column in a sparse + * matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating row indices for each column in the SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct AssignSparseMatrixColumnLoop { using type = typename Concatenate< @@ -604,6 +1794,16 @@ struct AssignSparseMatrixColumnLoop { (SparseAvailable::column_size - 1)>::type>::type; }; +/** + * @brief Specialization of AssignSparseMatrixColumnLoop for the case when + * ColumnElementNumber is 0. + * + * This specialization defines a type alias 'type' that is set to the result of + * AssignSparseMatrixRowLoop for the first column, effectively generating row + * indices for the first column in the SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct AssignSparseMatrixColumnLoop { using type = typename AssignSparseMatrixRowLoop< @@ -612,9 +1812,31 @@ struct AssignSparseMatrixColumnLoop { (SparseAvailable::column_size - 1)>::type; }; +/** + * @brief A template struct to create a sequence of row indices from a + * SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of generating a + * sequence of row indices based on the SparseAvailable type, taking into + * account whether the SparseAvailable type is empty or not. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam NotEmpty A boolean value indicating whether the SparseAvailable type + * is empty or not. + */ template struct RowIndicesSequenceFromSparseAvailable; +/** + * @brief Specialization of RowIndicesSequenceFromSparseAvailable for the case + * when the SparseAvailable type is not empty. + * + * This specialization defines a type alias 'type' that is set to the result of + * AssignSparseMatrixColumnLoop for the last column in the SparseAvailable type, + * effectively generating a sequence of row indices for the non-empty columns. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct RowIndicesSequenceFromSparseAvailable { using type = typename AssignSparseMatrixColumnLoop< @@ -623,6 +1845,16 @@ struct RowIndicesSequenceFromSparseAvailable { 1)>::type; }; +/** + * @brief Specialization of RowIndicesSequenceFromSparseAvailable for the case + * when the SparseAvailable type is empty. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence<0>, effectively indicating that there are no valid row indices + * to return when the SparseAvailable type is empty. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct RowIndicesSequenceFromSparseAvailable { using type = IndexSequence<0>; @@ -630,6 +1862,19 @@ struct RowIndicesSequenceFromSparseAvailable { } // namespace TemplatesOperation +/** + * @brief Alias template to create a sequence of row indices from a + * SparseAvailable type. + * + * This alias uses the TemplatesOperation::RowIndicesSequenceFromSparseAvailable + * to generate a type representing the row indices for the SparseAvailable type, + * taking into account whether it is empty or not. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * + * The resulting type is an IndexSequence containing the row indices for the + * SparseAvailable type. + */ template using RowIndicesFromSparseAvailable = typename TemplatesOperation::ToRowIndices< typename TemplatesOperation::RowIndicesSequenceFromSparseAvailable< @@ -640,11 +1885,38 @@ using RowIndicesFromSparseAvailable = typename TemplatesOperation::ToRowIndices< namespace TemplatesOperation { +/** + * @brief A template struct to count the number of elements in each row of a + * sparse matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * counting the number of elements in each row for each column in the + * SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ElementCount The current count of elements processed. + * @tparam ColumnElementNumber The index of the column being processed. + * @tparam Active A boolean indicating whether the current row is active or not. + * @tparam RowElementNumber The index of the row being processed. + */ template struct CountSparseMatrixRowLoop; +/** + * @brief Specialization of CountSparseMatrixRowLoop for the case when the row + * is active. + * + * This specialization defines a type alias 'type' that is set to the result of + * recursively counting the number of elements in the current row, incrementing + * the ElementCount if the row is active. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ElementCount The current count of elements processed. + * @tparam ColumnElementNumber The index of the column being processed. + * @tparam RowElementNumber The index of the row being processed. + */ template struct CountSparseMatrixRowLoop::type; }; +/** + * @brief Specialization of CountSparseMatrixRowLoop for the case when the row + * is not active. + * + * This specialization defines a type alias 'type' that is set to the result of + * recursively counting the number of elements in the current row without + * incrementing the ElementCount if the row is not active. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ElementCount The current count of elements processed. + * @tparam ColumnElementNumber The index of the column being processed. + * @tparam RowElementNumber The index of the row being processed. + */ template struct CountSparseMatrixRowLoop::type; }; +/** + * @brief Specialization of CountSparseMatrixRowLoop for the case when there + * are no more rows to process. + * + * This specialization defines a type alias 'type' that is set to an + * IndexSequence containing the ElementCount, effectively indicating that there + * are no more rows to process and returning the count of elements in the last + * row. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ElementCount The current count of elements processed. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct CountSparseMatrixRowLoop; }; +/** + * @brief Specialization of CountSparseMatrixRowLoop for the case when there + * are no more rows to process and the row is active. + * + * This specialization defines a type alias 'type' that is set to an + * IndexSequence containing (ElementCount + 1), effectively indicating that + * there are no more rows to process and returning the count of elements in the + * last row incremented by one. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ElementCount The current count of elements processed. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct CountSparseMatrixRowLoop; }; +/** + * @brief A template struct to count the number of elements in each column of a + * sparse matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * counting the number of elements in each column for the SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct CountSparseMatrixColumnLoop { using type = typename Concatenate< @@ -691,6 +2012,16 @@ struct CountSparseMatrixColumnLoop { (SparseAvailable::column_size - 1)>::type>::type; }; +/** + * @brief Specialization of CountSparseMatrixColumnLoop for the case when + * ColumnElementNumber is 0. + * + * This specialization defines a type alias 'type' that is set to the result of + * CountSparseMatrixRowLoop for the first column, effectively counting the + * number of elements in the first column of the SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct CountSparseMatrixColumnLoop { using type = typename Concatenate< @@ -701,6 +2032,16 @@ struct CountSparseMatrixColumnLoop { (SparseAvailable::column_size - 1)>::type>::type; }; +/** + * @brief A template struct to count the number of elements in each column of a + * sparse matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * counting the number of elements in each column for the SparseAvailable type, + * taking into account whether the SparseAvailable type is empty or not. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct AccumulateElementNumberLoop { static constexpr std::size_t compute() { @@ -710,11 +2051,34 @@ struct AccumulateElementNumberLoop { } }; +/** + * @brief Specialization of AccumulateElementNumberLoop for the case when + * ColumnElementNumber is 0. + * + * This specialization defines a static constexpr function 'compute' that + * returns 0, effectively indicating that there are no elements to accumulate + * when the column index is 0. + * + * @tparam CountSparseMatrixColumnLoop The CountSparseMatrixColumnLoop type + * containing the counts of elements in each column. + */ template struct AccumulateElementNumberLoop { static constexpr std::size_t compute() { return 0; } }; +/** + * @brief A template struct to accumulate the number of elements in each column + * of a sparse matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * accumulating the number of elements in each column for the SparseAvailable + * type. + * + * @tparam CountSparseMatrixColumnLoop The CountSparseMatrixColumnLoop type + * containing the counts of elements in each column. + * @tparam ColumnElementNumber The index of the column being processed. + */ template struct AccumulateSparseMatrixElementNumberLoop { using type = typename Concatenate< @@ -724,11 +2088,34 @@ struct AccumulateSparseMatrixElementNumberLoop { CountSparseMatrixColumnLoop, ColumnElementNumber>::compute()>>::type; }; +/** + * @brief Specialization of AccumulateSparseMatrixElementNumberLoop for the case + * when ColumnElementNumber is 0. + * + * This specialization defines a type alias 'type' that is set to + * IndexSequence, effectively returning + * the count of elements in the first column of the SparseAvailable type. + * + * @tparam CountSparseMatrixColumnLoop The CountSparseMatrixColumnLoop type + * containing the counts of elements in each column. + */ template struct AccumulateSparseMatrixElementNumberLoop { using type = IndexSequence; }; +/** + * @brief A template struct to accumulate the number of elements in each column + * of a sparse matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * accumulating the number of elements in each column for the SparseAvailable + * type, taking into account whether the SparseAvailable type is empty or not. + * + * @tparam CountSparseMatrixColumnLoop The CountSparseMatrixColumnLoop type + * containing the counts of elements in each column. + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct AccumulateSparseMatrixElementNumberStruct { using type = typename AccumulateSparseMatrixElementNumberLoop< @@ -737,6 +2124,18 @@ struct AccumulateSparseMatrixElementNumberStruct { } // namespace TemplatesOperation +/** + * @brief Alias template to create row pointers from a SparseAvailable type. + * + * This alias uses the + * TemplatesOperation::AccumulateSparseMatrixElementNumberStruct to generate a + * type representing the row pointers for the SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * + * The resulting type is a RowPointers type that contains the accumulated number + * of elements in each row of the SparseAvailable type. + */ template using RowPointersFromSparseAvailable = typename TemplatesOperation::ToRowPointers< @@ -749,6 +2148,16 @@ using RowPointersFromSparseAvailable = namespace TemplatesOperation { /* Sequence for Triangular */ + +/** * @brief A template struct to create a triangular index sequence. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a triangular index sequence for a given range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + * @tparam E_S The current size of the sequence. + */ template struct MakeTriangularIndexSequence { using type = typename Concatenate< @@ -756,17 +2165,49 @@ struct MakeTriangularIndexSequence { IndexSequence<(End - 1)>>::type; }; +/** + * @brief Specialization of MakeTriangularIndexSequence for the case when E_S is + * 0. + * + * This specialization defines a type alias 'type' that is set to IndexSequence + * containing (End - 1), effectively creating a triangular index sequence for + * the specified range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct MakeTriangularIndexSequence { using type = IndexSequence<(End - 1)>; }; +/** + * @brief A template struct to create a triangular sequence list. + * + * This struct provides a type alias 'type' that is the result of generating a + * triangular index sequence for a given range, starting from Start and ending + * at End. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct TriangularSequenceList { using type = typename MakeTriangularIndexSequence::type; }; /* Count for Triangular */ + +/** + * @brief A template struct to create a triangular count sequence. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a triangular count sequence for a given range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + * @tparam E_S The current size of the sequence. + */ template struct MakeTriangularCountSequence { using type = @@ -775,16 +2216,48 @@ struct MakeTriangularCountSequence { Start, (End - 1), (E_S - 1)>::type>::type; }; +/** + * @brief Specialization of MakeTriangularCountSequence for the case when E_S is + * 0. + * + * This specialization defines a type alias 'type' that is set to IndexSequence + * containing End, effectively creating a triangular count sequence for the + * specified range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct MakeTriangularCountSequence { using type = IndexSequence; }; +/** + * @brief A template struct to create a triangular count list. + * + * This struct provides a type alias 'type' that is the result of generating a + * triangular count sequence for a given range, starting from Start and ending + * at End. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct TriangularCountList { using type = typename MakeTriangularCountSequence::type; }; +/** + * @brief A template struct to count the number of elements in a triangular + * sequence. + * + * This struct provides a type alias 'type' that is the result of generating a + * triangular count sequence for a given range, starting from 0 and ending at N, + * and concatenating it with an IndexSequence containing 0. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + */ template struct TriangularCountNumbers { using type = typename Concatenate, @@ -793,6 +2266,17 @@ template struct TriangularCountNumbers { }; /* Create Upper Triangular Sparse Matrix Row Indices */ + +/** + * @brief A template struct to create a sequence of upper triangular row + * numbers. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a sequence of upper triangular row numbers for a given range. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + */ template struct ConcatenateUpperTriangularRowNumbers { using type = typename Concatenate< @@ -800,10 +2284,33 @@ struct ConcatenateUpperTriangularRowNumbers { typename TriangularSequenceList::type>::type; }; +/** + * @brief Specialization of ConcatenateUpperTriangularRowNumbers for the case + * when M is 1. + * + * This specialization defines a type alias 'type' that is set to the result of + * TriangularSequenceList<1, N>, effectively creating a sequence of upper + * triangular row numbers for the specified range. + * + * @tparam N The ending index of the sequence. + */ template struct ConcatenateUpperTriangularRowNumbers<1, N> { using type = typename TriangularSequenceList<1, N>::type; }; +/** + * @brief A template alias to create a sequence of upper triangular row numbers + * for a given range. + * + * This alias uses the ConcatenateUpperTriangularRowNumbers to generate a type + * representing the upper triangular row numbers for the specified range. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + * + * The resulting type is a sequence of upper triangular row numbers for the + * specified range. + */ template using UpperTriangularRowNumbers = typename ConcatenateUpperTriangularRowNumbers<((N < M) ? N : M), N>::type; @@ -819,10 +2326,38 @@ struct AccumulateTriangularElementNumberStruct { /* Create Upper Triangular Sparse Matrix Row Indices and Pointers */ +/** + * @brief A template alias to create a sequence of upper triangular row indices + * for a given range. + * + * This alias uses the TemplatesOperation::UpperTriangularRowNumbers to + * generate a type representing the upper triangular row indices for the + * specified range. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + * + * The resulting type is an IndexSequence containing the upper triangular row + * indices for the specified range. + */ template using UpperTriangularRowIndices = typename TemplatesOperation::ToRowIndices< TemplatesOperation::UpperTriangularRowNumbers>::type; +/** + * @brief A template alias to create row pointers for an upper triangular sparse + * matrix. + * + * This alias uses the + * TemplatesOperation::AccumulateTriangularElementNumberStruct to generate a + * type representing the row pointers for the upper triangular sparse matrix. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + * + * The resulting type is a RowPointers type that contains the accumulated number + * of elements in each row of the upper triangular sparse matrix. + */ template using UpperTriangularRowPointers = typename TemplatesOperation::ToRowPointers< typename TemplatesOperation::AccumulateTriangularElementNumberStruct< @@ -832,6 +2367,16 @@ using UpperTriangularRowPointers = typename TemplatesOperation::ToRowPointers< namespace TemplatesOperation { /* Create Lower Triangular Sparse Matrix Row Indices */ + +/** @brief A template struct to create a lower triangular index sequence. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a lower triangular index sequence for a given range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + * @tparam E_S The current size of the sequence. + */ template struct MakeLowerTriangularIndexSequence { using type = typename Concatenate>::type; }; +/** + * @brief Specialization of MakeLowerTriangularIndexSequence for the case when + * E_S is 0. + * + * This specialization defines a type alias 'type' that is set to IndexSequence + * containing 0, effectively creating a lower triangular index sequence for the + * specified range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct MakeLowerTriangularIndexSequence { using type = IndexSequence<0>; }; +/** + * @brief A template struct to create a lower triangular sequence list. + * + * This struct provides a type alias 'type' that is the result of generating a + * lower triangular index sequence for a given range, starting from Start and + * ending at End. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct LowerTriangularSequenceList { using type = typename MakeLowerTriangularIndexSequence::type; }; +/** + * @brief A template struct to create a sequence of lower triangular row + * numbers. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a sequence of lower triangular row numbers for a given range. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + */ template struct ConcatenateLowerTriangularRowNumbers { using type = typename Concatenate< @@ -857,10 +2433,33 @@ struct ConcatenateLowerTriangularRowNumbers { typename ConcatenateLowerTriangularRowNumbers<(M - 1), N>::type>::type; }; +/** + * @brief Specialization of ConcatenateLowerTriangularRowNumbers for the case + * when M is 1. + * + * This specialization defines a type alias 'type' that is set to the result of + * LowerTriangularSequenceList<1, N>, effectively creating a sequence of lower + * triangular row numbers for the specified range. + * + * @tparam N The ending index of the sequence. + */ template struct ConcatenateLowerTriangularRowNumbers<1, N> { using type = typename LowerTriangularSequenceList<1, N>::type; }; +/** + * @brief A template alias to create a sequence of lower triangular row numbers + * for a given range. + * + * This alias uses the ConcatenateLowerTriangularRowNumbers to generate a type + * representing the lower triangular row numbers for the specified range. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + * + * The resulting type is a sequence of lower triangular row numbers for the + * specified range. + */ template using LowerTriangularRowNumbers = typename TemplatesOperation::ConcatenateLowerTriangularRowNumbers< @@ -868,6 +2467,20 @@ using LowerTriangularRowNumbers = } // namespace TemplatesOperation +/** + * @brief A template alias to create a sequence of lower triangular row indices + * for a given range. + * + * This alias uses the TemplatesOperation::LowerTriangularRowNumbers to + * generate a type representing the lower triangular row indices for the + * specified range. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + * + * The resulting type is an IndexSequence containing the lower triangular row + * indices for the specified range. + */ template using LowerTriangularRowIndices = typename TemplatesOperation::ToRowIndices< TemplatesOperation::LowerTriangularRowNumbers>::type; @@ -875,6 +2488,18 @@ using LowerTriangularRowIndices = typename TemplatesOperation::ToRowIndices< namespace TemplatesOperation { /* Create Lower Triangular Sparse Matrix Row Pointers */ + +/** + * @brief A template struct to create a sequence of lower triangular count + * numbers. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a lower triangular count sequence for a given range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + * @tparam E_S The current size of the sequence. + */ template struct MakeLowerTriangularCountSequence { using type = @@ -883,22 +2508,66 @@ struct MakeLowerTriangularCountSequence { (Start + 1), (End - 1), (E_S - 1)>::type>::type; }; +/** + * @brief Specialization of MakeLowerTriangularCountSequence for the case when + * E_S is 0. + * + * This specialization defines a type alias 'type' that is set to IndexSequence + * containing Start, effectively creating a lower triangular count sequence for + * the specified range. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct MakeLowerTriangularCountSequence { using type = IndexSequence; }; +/** + * @brief A template struct to create a lower triangular count list. + * + * This struct provides a type alias 'type' that is the result of generating a + * lower triangular count sequence for a given range, starting from Start and + * ending at End. + * + * @tparam Start The starting index of the sequence. + * @tparam End The ending index of the sequence. + */ template struct LowerTriangularCountList { using type = typename MakeLowerTriangularCountSequence::type; }; +/** + * @brief A template struct to count the number of elements in a lower + * triangular sequence. + * + * This struct provides a type alias 'type' that is the result of generating a + * lower triangular count sequence for a given range, starting from 0 and + * ending at N, and concatenating it with an IndexSequence containing 0. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + */ template struct LowerTriangularCountNumbers { using type = typename Concatenate< IndexSequence<0>, typename LowerTriangularCountList<1, ((M < N) ? M : N)>::type>::type; }; +/** + * @brief A template struct to accumulate the number of elements in each column + * of a lower triangular sparse matrix. + * + * This struct provides a type alias 'type' that is the result of recursively + * accumulating the number of elements in each column for the lower triangular + * sparse matrix. + * + * @tparam LowerTriangularCountNumbers The LowerTriangularCountNumbers type + * containing the counts of elements in each column. + * @tparam M The index of the column being processed. + */ template struct AccumulateLowerTriangularElementNumberStruct { using type = typename AccumulateSparseMatrixElementNumberLoop< @@ -907,6 +2576,20 @@ struct AccumulateLowerTriangularElementNumberStruct { } // namespace TemplatesOperation +/** + * @brief A template alias to create row pointers for a lower triangular sparse + * matrix. + * + * This alias uses the + * TemplatesOperation::AccumulateLowerTriangularElementNumberStruct to generate + * a type representing the row pointers for the lower triangular sparse matrix. + * + * @tparam M The starting index of the sequence. + * @tparam N The ending index of the sequence. + * + * The resulting type is a RowPointers type that contains the accumulated number + * of elements in each row of the lower triangular sparse matrix. + */ template using LowerTriangularRowPointers = typename TemplatesOperation::ToRowPointers< typename TemplatesOperation::AccumulateLowerTriangularElementNumberStruct< @@ -917,11 +2600,33 @@ using LowerTriangularRowPointers = typename TemplatesOperation::ToRowPointers< namespace TemplatesOperation { -// helper template to calculate the matrix product +/** + * @brief A helper template to determine if the addition or subtraction of two + * SparseAvailable types is available. + * + * This struct provides a type alias 'type' that is the result of recursively + * checking if the addition or subtraction of two SparseAvailable types is + * available for each column. + * + * @tparam MatrixA The first SparseAvailable type. + * @tparam MatrixB The second SparseAvailable type. + */ template struct MatrixAddSubSparseAvailableHelper; -// partial specialization for ColumnAvailable +/** + * @brief Partial specialization of MatrixAddSubSparseAvailableHelper for the + * case when both SparseAvailable types are ColumnAvailable. + * + * This specialization defines a type alias 'type' that is set to a + * ColumnAvailable type containing the logical OR of the availability of each + * column in the two SparseAvailable types. + * + * @tparam ValuesA The boolean values representing the availability of columns + * in the first SparseAvailable type. + * @tparam ValuesB The boolean values representing the availability of columns + * in the second SparseAvailable type. + */ template struct MatrixAddSubSparseAvailableHelper, ColumnAvailable> { @@ -929,7 +2634,20 @@ struct MatrixAddSubSparseAvailableHelper, TemplatesOperation::LogicalOr::value...>; }; -// partial specialization for SparseAvailable +/** + * @brief Partial specialization of MatrixAddSubSparseAvailableHelper for the + * case when one of the SparseAvailable types is ColumnAvailable and the other + * is SparseAvailable. + * + * This specialization defines a type alias 'type' that is set to a + * SparseAvailable type containing the logical OR of the availability of each + * column in the two SparseAvailable types. + * + * @tparam ValuesA The boolean values representing the availability of columns + * in the first SparseAvailable type. + * @tparam ValuesB The boolean values representing the availability of columns + * in the second SparseAvailable type. + */ template struct MatrixAddSubSparseAvailableHelper, SparseAvailable> { @@ -939,7 +2657,20 @@ struct MatrixAddSubSparseAvailableHelper, } // namespace TemplatesOperation -// template to check if the matrix product is available +/** + * @brief A template alias to determine if the addition or subtraction of two + * SparseAvailable types is available. + * + * This alias uses the TemplatesOperation::MatrixAddSubSparseAvailableHelper to + * generate a type representing the availability of addition or subtraction for + * the two SparseAvailable types. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * + * The resulting type is a SparseAvailable type containing the logical OR of + * the availability of each column in the two SparseAvailable types. + */ template using MatrixAddSubSparseAvailable = typename TemplatesOperation::MatrixAddSubSparseAvailableHelper< @@ -949,11 +2680,30 @@ using MatrixAddSubSparseAvailable = namespace TemplatesOperation { -// helper template to calculate the logical AND +/** + * @brief A template struct to perform logical OR operation on two boolean + * values. + * + * This struct provides a static constexpr member 'value' that is the result of + * performing a logical OR operation on the two boolean values A and B. + * + * @tparam A The first boolean value. + * @tparam B The second boolean value. + */ template struct LogicalAnd { static constexpr bool value = A && B; }; +/** + * @brief A template struct to perform logical OR operation on two boolean + * values. + * + * This struct provides a static constexpr member 'value' that is the result of + * performing a logical OR operation on the two boolean values A and B. + * + * @tparam A The first boolean value. + * @tparam B The second boolean value. + */ template struct SparseAvailableMatrixMultiplyElement { @@ -962,6 +2712,20 @@ struct SparseAvailableMatrixMultiplyElement { SparseAvailable_B::lists[N_Idx][ROW]>::value; }; +/** + * @brief A template struct to perform the multiplication of two SparseAvailable + * types. + * + * This struct provides a type alias 'type' that is the result of recursively + * multiplying the elements of two SparseAvailable types for each column and + * row. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * @tparam COL The index of the column being processed. + * @tparam ROW The index of the row being processed. + * @tparam N_Idx The index of the element being processed. + */ template struct SparseAvailableMatrixMultiplyMultiplyLoop { @@ -972,6 +2736,19 @@ struct SparseAvailableMatrixMultiplyMultiplyLoop { SparseAvailable_A, SparseAvailable_B, COL, ROW, N_Idx>::value>>; }; +/** + * @brief Specialization of SparseAvailableMatrixMultiplyMultiplyLoop for the + * case when N_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * ColumnAvailable type containing the result of the multiplication for the + * first element. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * @tparam COL The index of the column being processed. + * @tparam ROW The index of the row being processed. + */ template struct SparseAvailableMatrixMultiplyMultiplyLoop< @@ -980,6 +2757,16 @@ struct SparseAvailableMatrixMultiplyMultiplyLoop< SparseAvailable_A, SparseAvailable_B, COL, ROW, 0>::value>; }; +/** + * @brief A template struct to concatenate multiple ColumnAvailable types into a + * single SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * multiple ColumnAvailable types into a SparseAvailable type. + * + * @tparam ColumnAvailableList The list of ColumnAvailable types to be + * concatenated. + */ template struct ColumnAvailableElementWiseOr { static constexpr bool value = LogicalOr< @@ -987,12 +2774,30 @@ struct ColumnAvailableElementWiseOr { ColumnAvailableElementWiseOr::value>::value; }; +/** + * @brief Specialization of ColumnAvailableElementWiseOr for the case when N_Idx + * is 0. + * + * This specialization defines a static constexpr member 'value' that is set to + * the first element of the ColumnAvailable type, effectively returning the + * availability of the first column. + * + * @tparam ColumnAvailable The ColumnAvailable type containing multiple columns. + */ template struct ColumnAvailableElementWiseOr { static constexpr bool value = ColumnAvailable::list[0]; }; -// ColumnAvailable from SparseAvailable Row vector +/** + * @brief A template struct to concatenate multiple ColumnAvailable types into a + * single SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * multiple ColumnAvailable types into a SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + */ template struct ColumnAvailableFromSparseAvailableColumLoop { using type = ConcatenateColumnAvailable< @@ -1001,11 +2806,34 @@ struct ColumnAvailableFromSparseAvailableColumLoop { ColumnAvailable>; }; +/** + * @brief Specialization of ColumnAvailableFromSparseAvailableColumLoop for the + * case when M_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * ColumnAvailable type containing the first column of the SparseAvailable type, + * effectively returning the availability of the first column. + * + * @tparam SparseAvailable The SparseAvailable type containing multiple columns. + * @tparam ROW The index of the row being processed. + */ template struct ColumnAvailableFromSparseAvailableColumLoop { using type = ColumnAvailable; }; +/** + * @brief A template struct to concatenate multiple ColumnAvailable types into a + * SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * multiple ColumnAvailable types into a SparseAvailable type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * @tparam COL The index of the column being processed. + * @tparam J_Idx The index of the row being processed. + */ template struct SparseAvailableMatrixMultiplyRowLoop { @@ -1019,6 +2847,18 @@ struct SparseAvailableMatrixMultiplyRowLoop { (SparseAvailable_A::column_size - 1)>::value>>; }; +/** + * @brief Specialization of SparseAvailableMatrixMultiplyRowLoop for the case + * when J_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * ColumnAvailable type containing the result of the multiplication for the + * first row. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * @tparam COL The index of the column being processed. + */ template struct SparseAvailableMatrixMultiplyRowLoop::value>; }; +/** + * @brief A template struct to concatenate multiple SparseAvailable types into a + * single SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * multiple SparseAvailable types into a SparseAvailable type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + * @tparam I_Idx The index of the column being processed. + */ template struct SparseAvailableMatrixMultiplyColumnLoop { @@ -1041,6 +2892,17 @@ struct SparseAvailableMatrixMultiplyColumnLoop { (SparseAvailable_B::column_size - 1)>::type>>; }; +/** + * @brief Specialization of SparseAvailableMatrixMultiplyColumnLoop for the case + * when I_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * SparseAvailableColumns type containing the result of the multiplication for + * the first column. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_B The second SparseAvailable type. + */ template struct SparseAvailableMatrixMultiplyColumnLoop { @@ -1052,6 +2914,20 @@ struct SparseAvailableMatrixMultiplyColumnLoop using SparseAvailableMatrixMultiply = typename TemplatesOperation::SparseAvailableMatrixMultiplyColumnLoop< @@ -1063,6 +2939,16 @@ using SparseAvailableMatrixMultiply = namespace TemplatesOperation { +/** + * @brief A template struct to perform logical AND operation on two boolean + * values. + * + * This struct provides a static constexpr member 'value' that is the result of + * performing a logical AND operation on the two boolean values A and B. + * + * @tparam A The first boolean value. + * @tparam B The second boolean value. + */ template struct SparseAvailableMatrixMultiplyTransposeElement { @@ -1071,6 +2957,20 @@ struct SparseAvailableMatrixMultiplyTransposeElement { SparseAvailable_BT::lists[ROW][N_Idx]>::value; }; +/** + * @brief A template struct to perform the multiplication of two SparseAvailable + * types with transpose. + * + * This struct provides a type alias 'type' that is the result of recursively + * multiplying the elements of two SparseAvailable types for each column and + * row, considering the transpose of the second SparseAvailable type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + * @tparam COL The index of the column being processed. + * @tparam ROW The index of the row being processed. + * @tparam N_Idx The index of the element being processed. + */ template struct SparseAvailableMatrixMultiplyTransposeMultiplyLoop { @@ -1081,6 +2981,19 @@ struct SparseAvailableMatrixMultiplyTransposeMultiplyLoop { SparseAvailable_A, SparseAvailable_BT, COL, ROW, N_Idx>::value>>; }; +/** + * @brief Specialization of SparseAvailableMatrixMultiplyTransposeMultiplyLoop + * for the case when N_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * ColumnAvailable type containing the result of the multiplication for the + * first element. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + * @tparam COL The index of the column being processed. + * @tparam ROW The index of the row being processed. + */ template struct SparseAvailableMatrixMultiplyTransposeMultiplyLoop< @@ -1089,6 +3002,18 @@ struct SparseAvailableMatrixMultiplyTransposeMultiplyLoop< SparseAvailable_A, SparseAvailable_BT, COL, ROW, 0>::value>; }; +/** + * @brief A template struct to concatenate multiple ColumnAvailable types into a + * SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * multiple ColumnAvailable types into a SparseAvailable type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + * @tparam COL The index of the column being processed. + * @tparam J_Idx The index of the row being processed. + */ template struct SparseAvailableMatrixMultiplyTransposeRowLoop { @@ -1102,6 +3027,18 @@ struct SparseAvailableMatrixMultiplyTransposeRowLoop { (SparseAvailable_A::column_size - 1)>::value>>; }; +/** + * @brief Specialization of SparseAvailableMatrixMultiplyTransposeRowLoop for + * the case when J_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * ColumnAvailable type containing the result of the multiplication for the + * first row. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + * @tparam COL The index of the column being processed. + */ template struct SparseAvailableMatrixMultiplyTransposeRowLoop< @@ -1113,6 +3050,17 @@ struct SparseAvailableMatrixMultiplyTransposeRowLoop< (SparseAvailable_A::column_size - 1)>::value>; }; +/** + * @brief A template struct to concatenate multiple SparseAvailable types into a + * SparseAvailableColumns type. + * + * This struct provides a type alias 'type' that is the result of concatenating + * multiple SparseAvailable types into a SparseAvailableColumns type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + * @tparam I_Idx The index of the column being processed. + */ template struct SparseAvailableMatrixMultiplyTransposeColumnLoop { @@ -1125,6 +3073,17 @@ struct SparseAvailableMatrixMultiplyTransposeColumnLoop { (SparseAvailable_BT::number_of_columns - 1)>::type>>; }; +/** + * @brief Specialization of SparseAvailableMatrixMultiplyTransposeColumnLoop + * for the case when I_Idx is 0. + * + * This specialization defines a type alias 'type' that is set to a + * SparseAvailableColumns type containing the result of the multiplication for + * the first column. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + */ template struct SparseAvailableMatrixMultiplyTransposeColumnLoop { @@ -1134,6 +3093,21 @@ struct SparseAvailableMatrixMultiplyTransposeColumnLoop::type>; }; +/** + * @brief A template alias to perform matrix multiplication of a SparseAvailable + * type with the transpose of another SparseAvailable type. + * + * This alias uses the + * TemplatesOperation::SparseAvailableMatrixMultiplyTransposeColumnLoop to + * generate a type representing the result of multiplying a SparseAvailable type + * with the transpose of another SparseAvailable type. + * + * @tparam SparseAvailable_A The first SparseAvailable type. + * @tparam SparseAvailable_BT The second SparseAvailable type (transposed). + * + * The resulting type is a SparseAvailable type containing the result of the + * multiplication for each column. + */ template using SparseAvailableMatrixMultiplyTranspose = typename SparseAvailableMatrixMultiplyTransposeColumnLoop< @@ -1142,6 +3116,21 @@ using SparseAvailableMatrixMultiplyTranspose = } // namespace TemplatesOperation +/** + * @brief A template alias to perform matrix multiplication of a + * SparseAvailable type with the transpose of another SparseAvailable type, + * where the first SparseAvailable type is diagonal. + * + * This alias uses the + * TemplatesOperation::SparseAvailableMatrixMultiplyTranspose to generate a type + * representing the result of multiplying a diagonal SparseAvailable type with + * the transpose of another SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type to be multiplied. + * + * The resulting type is a SparseAvailable type containing the result of the + * multiplication for each column. + */ template using SparseAvailableTranspose = TemplatesOperation::SparseAvailableMatrixMultiplyTranspose< @@ -1151,6 +3140,16 @@ using SparseAvailableTranspose = namespace TemplatesOperation { +/** + * @brief A template struct to validate the SparseAvailable type. + * + * This struct provides a type alias 'type' that is the result of recursively + * validating the SparseAvailable type for each column. + * + * @tparam SparseAvailable The SparseAvailable type to be validated. + * @tparam NumberOfRowsFirst The number of rows in the first column. + * @tparam ColumnIndex The index of the column being processed. + */ template struct ValidateSparseAvailableLoop { @@ -1164,6 +3163,17 @@ struct ValidateSparseAvailableLoop { (ColumnIndex - 1)>::type; }; +/** + * @brief Specialization of ValidateSparseAvailableLoop for the case when + * ColumnIndex is 0. + * + * This specialization defines a type alias 'type' that is set to the + * SparseAvailable type itself, effectively validating the SparseAvailable type + * for the first column. + * + * @tparam SparseAvailable The SparseAvailable type to be validated. + * @tparam NumberOfRowsFirst The number of rows in the first column. + */ template struct ValidateSparseAvailableLoop { static_assert(SparseAvailable::column_size == NumberOfRowsFirst, @@ -1174,6 +3184,17 @@ struct ValidateSparseAvailableLoop { } // namespace TemplatesOperation +/** + * @brief A template alias to validate the SparseAvailable type. + * + * This alias uses the TemplatesOperation::ValidateSparseAvailableLoop to + * generate a type representing the validated SparseAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type to be validated. + * + * The resulting type is the validated SparseAvailable type if it is valid, or + * it will trigger a static assertion if it is not valid. + */ template using ValidateSparseAvailable = typename TemplatesOperation::ValidateSparseAvailableLoop< @@ -1183,12 +3204,31 @@ using ValidateSparseAvailable = /* SparseAvailable get row */ namespace TemplatesOperation { +/** @brief A template struct to generate a ColumnAvailable type with a + * specific index set to true. + * + * This struct provides a type alias 'type' that is set to a ColumnAvailable + * type with the specified index set to true, and all other indices set to + * false. + * + * @tparam M The total number of columns. + * @tparam Index The index to be set to true. + */ template using GenerateIndexedRowTrueColumnAvailable = ColumnAvailable<(M == Index ? true : false)>; -// concatenate indexed true flags vertically -// base case: N = 0 +/** + * @brief A template struct to recursively generate a ColumnAvailable type with + * a specific index set to true for each column. + * + * This struct provides a type alias 'type' that is the result of recursively + * generating a ColumnAvailable type with the specified index set to true for + * each column. + * + * @tparam M The total number of columns. + * @tparam Index The index to be set to true. + */ template struct IndexedRowRepeatColumnAvailable { @@ -1197,7 +3237,18 @@ struct IndexedRowRepeatColumnAvailable { ColumnAvailable, Columns...>::type; }; -// recursive termination case: N = 0 +/** + * @brief Specialization of IndexedRowRepeatColumnAvailable for the case when M + * is 0. + * + * This specialization defines a type alias 'type' that is set to a + * SparseAvailable type containing the ColumnAvailable types generated for each + * column. + * + * @tparam Index The index to be set to true. + * @tparam ColumnAvailable The ColumnAvailable type generated so far. + * @tparam Columns The remaining ColumnAvailable types. + */ template struct IndexedRowRepeatColumnAvailable<0, Index, ColumnAvailable, Columns...> { using type = TemplatesOperation::SparseAvailableColumns< @@ -1206,6 +3257,20 @@ struct IndexedRowRepeatColumnAvailable<0, Index, ColumnAvailable, Columns...> { } // namespace TemplatesOperation +/** + * @brief A template alias to generate a SparseAvailable type with a specific + * row index set to true. + * + * This alias uses the TemplatesOperation::IndexedRowRepeatColumnAvailable to + * generate a type representing the SparseAvailable type with the specified row + * index set to true for each column. + * + * @tparam M The total number of columns. + * @tparam Index The index of the row to be set to true. + * + * The resulting type is a SparseAvailable type containing the ColumnAvailable + * types generated for each column, with the specified row index set to true. + */ template using RowAvailable = typename TemplatesOperation::IndexedRowRepeatColumnAvailable< @@ -1213,6 +3278,23 @@ using RowAvailable = TemplatesOperation::GenerateIndexedRowTrueColumnAvailable<(M - 1), Index>>::type; +/** + * @brief A template alias to perform matrix multiplication of a SparseAvailable + * type with a RowAvailable type. + * + * This alias uses the TemplatesOperation::SparseAvailableMatrixMultiply to + * generate a type representing the result of multiplying a SparseAvailable type + * with a RowAvailable type. + * + * @tparam SparseAvailable The SparseAvailable type to be multiplied. + * @tparam M The total number of columns in the RowAvailable type. + * @tparam Index The index of the row to be set to true in the RowAvailable + * type. + * + * The resulting type is a SparseAvailable type containing the result of the + * multiplication for each column, where the specified row index in the + * RowAvailable type is set to true. + */ template using SparseAvailableGetRow = SparseAvailableMatrixMultiply>; From 9c83316d28539a4da6e92b003a5410ca34c7e9e2 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 23:00:04 +0900 Subject: [PATCH 16/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_triangular_sparse.hpp | 230 +++++++++++++++++- 1 file changed, 226 insertions(+), 4 deletions(-) diff --git a/base_matrix/base_matrix_triangular_sparse.hpp b/base_matrix/base_matrix_triangular_sparse.hpp index 3d9d48e..6dc7d44 100644 --- a/base_matrix/base_matrix_triangular_sparse.hpp +++ b/base_matrix/base_matrix_triangular_sparse.hpp @@ -1,3 +1,21 @@ +/** + * @file base_matrix_triangular_sparse.hpp + * @brief Utilities for handling upper and lower triangular sparse matrices in a + * compile-time optimized manner. + * + * This file provides template metaprogramming utilities and classes for + * constructing and manipulating upper and lower triangular sparse matrices. The + * implementation supports both compile-time recursion and runtime for-loop + * approaches (controlled by the __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ macro). + * The code is designed for high-performance scenarios where matrix dimensions + * are known at compile time. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_TRIANGULAR_SPARSE_HPP__ #define __BASE_MATRIX_TRIANGULAR_SPARSE_HPP__ @@ -21,12 +39,29 @@ namespace Matrix { /* Set values for Upper Triangular Sparse Matrix */ namespace SetValuesForUpperTriangularSparseMatrix { -// Calculate consecutive index at compile time +/** + * @brief Calculate consecutive index at compile time for upper triangular + * matrix. + * + * This struct computes the index in a flattened array representation of an + * upper triangular matrix, given the row (I) and column (J) indices. + * + * @tparam I Row index. + * @tparam J Column index. + * @tparam N Total number of columns in the matrix. + */ template struct ConsecutiveIndex { static constexpr std::size_t value = (I * (2 * N - I + 1)) / 2 + (J - I); }; -// Specialization for the base case +/** + * @brief Specialization for the base case when both I and J are 0. + * + * This specialization provides a base case for the ConsecutiveIndex struct, + * returning 0 when both indices are 0. + * + * @tparam N Total number of columns in the matrix. + */ template struct ConsecutiveIndex<0, 0, N> { static constexpr std::size_t value = 0; }; @@ -35,6 +70,17 @@ template struct ConsecutiveIndex<0, 0, N> { template struct SetUpperValues { + /** + * @brief Computes the index for the upper triangular matrix and sets the + * value. + * + * This function computes the consecutive index for the given row (I) and + * column (J) in an upper triangular matrix and sets the corresponding value + * in the CompiledSparseMatrix. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, UpperTriangularRowPointers> &A, @@ -48,6 +94,17 @@ struct SetUpperValues { // Specialization for the end of a row template struct SetUpperValues { + /** + * @brief Computes the index for the last element in a row of the upper + * triangular matrix and sets the value. + * + * This function computes the consecutive index for the last element in the + * row (I) of an upper triangular matrix and sets the corresponding value in + * the CompiledSparseMatrix. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, UpperTriangularRowPointers> &A, @@ -60,6 +117,16 @@ struct SetUpperValues { // Set values for each row template struct SetUpperRow { + /** + * @brief Computes the values for a specific row (I) in the upper triangular + * matrix. + * + * This function sets the values for all elements in row I of the upper + * triangular matrix and recursively calls itself for the previous row. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, UpperTriangularRowPointers> &A, @@ -72,6 +139,16 @@ struct SetUpperRow { // Specialization for the first row template struct SetUpperRow { + /** + * @brief Computes the values for the first row (0) in the upper triangular + * matrix. + * + * This function sets the values for all elements in the first row of the + * upper triangular matrix. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, UpperTriangularRowPointers> &A, @@ -80,6 +157,15 @@ struct SetUpperRow { } }; +/** + * @brief Computes the values for an upper triangular sparse matrix. + * + * This function initializes the CompiledSparseMatrix with values from the + * provided Matrix, specifically for the upper triangular part. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ template inline void compute(CompiledSparseMatrix, @@ -93,13 +179,30 @@ compute(CompiledSparseMatrix, /* Set values for Lower Triangular Sparse Matrix */ namespace SetValuesForLowerTriangularSparseMatrix { -// Calculate consecutive index at compile time for lower triangular matrix +/** + * @brief Calculate consecutive index at compile time for lower triangular + * matrix. + * + * This struct computes the index in a flattened array representation of a + * lower triangular matrix, given the row (I) and column (J) indices. + * + * @tparam I Column index. + * @tparam J Row index. + * @tparam N Total number of columns in the matrix. + */ template struct ConsecutiveIndexLower { static constexpr std::size_t value = (I * (I + 1)) / 2 + J; }; -// Specialization for the base case +/** + * @brief Specialization for the base case when both I and J are 0. + * + * This specialization provides a base case for the ConsecutiveIndexLower + * struct, returning 0 when both indices are 0. + * + * @tparam N Total number of columns in the matrix. + */ template struct ConsecutiveIndexLower<0, 0, N> { static constexpr std::size_t value = 0; }; @@ -108,6 +211,17 @@ template struct ConsecutiveIndexLower<0, 0, N> { template struct SetLowerValues { + /** + * @brief Computes the index for the lower triangular matrix and sets the + * value. + * + * This function computes the consecutive index for the given column (I) and + * row (J) in a lower triangular matrix and sets the corresponding value + * in the CompiledSparseMatrix. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, LowerTriangularRowPointers> &A, @@ -121,6 +235,17 @@ struct SetLowerValues { // Specialization for the end of a row template struct SetLowerValues { + /** + * @brief Computes the index for the last element in a row of the lower + * triangular matrix and sets the value. + * + * This function computes the consecutive index for the last element in the + * column (I) of a lower triangular matrix and sets the corresponding value + * in the CompiledSparseMatrix. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, LowerTriangularRowPointers> &A, @@ -133,6 +258,16 @@ struct SetLowerValues { // Set values for each row template struct SetLowerRow { + /** + * @brief Computes the values for a specific row (I) in the lower triangular + * matrix. + * + * This function sets the values for all elements in row I of the lower + * triangular matrix and recursively calls itself for the previous row. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, LowerTriangularRowPointers> &A, @@ -145,6 +280,16 @@ struct SetLowerRow { // Specialization for the first row template struct SetLowerRow { + /** + * @brief Computes the values for the first row (0) in the lower triangular + * matrix. + * + * This function sets the values for all elements in the first row of the + * lower triangular matrix. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static void compute(CompiledSparseMatrix, LowerTriangularRowPointers> &A, @@ -153,6 +298,15 @@ struct SetLowerRow { } }; +/** + * @brief Computes the values for a lower triangular sparse matrix. + * + * This function initializes the CompiledSparseMatrix with values from the + * provided Matrix, specifically for the lower triangular part. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ template inline void compute(CompiledSparseMatrix, @@ -163,11 +317,32 @@ compute(CompiledSparseMatrix, } // namespace SetValuesForLowerTriangularSparseMatrix +/** + * @brief Class for handling triangular sparse matrices. + * + * This class provides static methods to create and manipulate upper and lower + * triangular sparse matrices. It supports compile-time optimizations for + * matrix dimensions and provides methods to set values from a regular matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template class TriangularSparse { public: + /* Constructor */ TriangularSparse() {} /* Upper */ + + /** + * @brief Creates an upper triangular sparse matrix. + * + * This function initializes a CompiledSparseMatrix with the appropriate row + * indices and row pointers for an upper triangular matrix. + * + * @return A CompiledSparseMatrix representing an upper triangular matrix. + */ static inline auto create_upper(void) -> CompiledSparseMatrix, UpperTriangularRowPointers> { @@ -181,6 +356,15 @@ template class TriangularSparse { return Y; } + /** + * @brief Creates an upper triangular sparse matrix from a regular matrix. + * + * This function initializes a CompiledSparseMatrix with values from the + * provided Matrix, specifically for the upper triangular part. + * + * @param A The source Matrix from which values are taken. + * @return A CompiledSparseMatrix representing an upper triangular matrix. + */ static inline auto create_upper(const Matrix &A) -> CompiledSparseMatrix, UpperTriangularRowPointers> { @@ -212,6 +396,16 @@ template class TriangularSparse { return Y; } + /** + * @brief Sets values in an upper triangular sparse matrix from a regular + * matrix. + * + * This function populates the CompiledSparseMatrix with values from the + * provided Matrix, specifically for the upper triangular part. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static inline void set_values_upper( CompiledSparseMatrix, UpperTriangularRowPointers> &A, @@ -238,6 +432,15 @@ template class TriangularSparse { } /* Lower */ + + /** + * @brief Creates a lower triangular sparse matrix. + * + * This function initializes a CompiledSparseMatrix with the appropriate row + * indices and row pointers for a lower triangular matrix. + * + * @return A CompiledSparseMatrix representing a lower triangular matrix. + */ static inline auto create_lower(void) -> CompiledSparseMatrix, LowerTriangularRowPointers> { @@ -251,6 +454,15 @@ template class TriangularSparse { return Y; } + /** + * @brief Creates a lower triangular sparse matrix from a regular matrix. + * + * This function initializes a CompiledSparseMatrix with values from the + * provided Matrix, specifically for the lower triangular part. + * + * @param A The source Matrix from which values are taken. + * @return A CompiledSparseMatrix representing a lower triangular matrix. + */ static inline auto create_lower(const Matrix &A) -> CompiledSparseMatrix, LowerTriangularRowPointers> { @@ -282,6 +494,16 @@ template class TriangularSparse { return Y; } + /** + * @brief Sets values in a lower triangular sparse matrix from a regular + * matrix. + * + * This function populates the CompiledSparseMatrix with values from the + * provided Matrix, specifically for the lower triangular part. + * + * @param A The CompiledSparseMatrix where values are set. + * @param B The source Matrix from which values are taken. + */ static inline void set_values_lower( CompiledSparseMatrix, LowerTriangularRowPointers> &A, From 084066fb2ba9e622a32718f7e29329c3f17fa128 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 23:03:30 +0900 Subject: [PATCH 17/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_variable_sparse.hpp | 176 ++++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/base_matrix/base_matrix_variable_sparse.hpp b/base_matrix/base_matrix_variable_sparse.hpp index 66012d3..d4f8817 100644 --- a/base_matrix/base_matrix_variable_sparse.hpp +++ b/base_matrix/base_matrix_variable_sparse.hpp @@ -1,3 +1,25 @@ +/** + * @file base_matrix_variable_sparse.hpp + * @brief Defines the VariableSparseMatrix class template and related matrix + * multiplication operators for sparse matrix computations. + * + * This file provides the implementation of a variable-size sparse matrix class, + * `VariableSparseMatrix`, which supports both standard vector and fixed-size + * array storage depending on compile-time flags. The class is designed for + * efficient storage and manipulation of sparse matrices, and includes copy/move + * constructors and assignment operators. + * + * The file also defines several overloaded operator* functions to enable + * multiplication between VariableSparseMatrix, dense Matrix, and SparseMatrix + * types, supporting various combinations of sparse and dense matrix + * multiplication. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __BASE_MATRIX_VARIABLE_SPARSE_HPP__ #define __BASE_MATRIX_VARIABLE_SPARSE_HPP__ @@ -15,8 +37,23 @@ namespace Base { namespace Matrix { +/** + * @brief VariableSparseMatrix class template for sparse matrix representation. + * + * This class template provides a sparse matrix representation that can be + * configured to use either `std::vector` or fixed-size `std::array` for + * storage, depending on the compile-time flag `__BASE_MATRIX_USE_STD_VECTOR__`. + * + * It supports basic operations such as copy and move semantics, and provides + * access to matrix values, row indices, and row pointers. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template class VariableSparseMatrix { public: +/* Constructor */ #ifdef __BASE_MATRIX_USE_STD_VECTOR__ VariableSparseMatrix() : values(M * N, static_cast(0)), @@ -57,19 +94,75 @@ template class VariableSparseMatrix { return *this; } +public: /* Function */ + + /** + * @brief Accessor for the value at index i. + * + * This function returns the value at the specified index i in the sparse + * matrix. + * + * @param i The index of the value to access. + * @return The value at index i. + */ T value(std::size_t i) { return this->values[i]; } + /** + * @brief Const accessor for the value at index i. + * + * This function returns the value at the specified index i in the sparse + * matrix, without allowing modification. + * + * @param i The index of the value to access. + * @return The value at index i. + */ const T value(std::size_t i) const { return this->values[i]; } + /** + * @brief Accessor for the row index at index i. + * + * This function returns the row index at the specified index i in the sparse + * matrix. + * + * @param i The index of the row index to access. + * @return The row index at index i. + */ std::size_t row_index(std::size_t i) { return this->row_indices[i]; } + /** + * @brief Const accessor for the row index at index i. + * + * This function returns the row index at the specified index i in the sparse + * matrix, without allowing modification. + * + * @param i The index of the row index to access. + * @return The row index at index i. + */ const std::size_t row_index(std::size_t i) const { return this->row_indices[i]; } + /** + * @brief Accessor for the row pointer at index i. + * + * This function returns the row pointer at the specified index i in the + * sparse matrix. + * + * @param i The index of the row pointer to access. + * @return The row pointer at index i. + */ std::size_t row_pointer(std::size_t i) { return this->row_pointers[i]; } + /** + * @brief Const accessor for the row pointer at index i. + * + * This function returns the row pointer at the specified index i in the + * sparse matrix, without allowing modification. + * + * @param i The index of the row pointer to access. + * @return The row pointer at index i. + */ const std::size_t row_pointer(std::size_t i) const { return this->row_pointers[i]; } @@ -87,6 +180,23 @@ template class VariableSparseMatrix { }; /* SparseMatrix * Matrix */ + +/** + * @brief Overloaded operator* for multiplying a VariableSparseMatrix with a + * Matrix. + * + * This function performs matrix multiplication between a VariableSparseMatrix + * and a Matrix, returning the resulting Matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the VariableSparseMatrix. + * @tparam N The number of rows in the VariableSparseMatrix and rows in the + * Matrix. + * @tparam K The number of columns in the Matrix. + * @param A The VariableSparseMatrix to multiply. + * @param B The Matrix to multiply with. + * @return The resulting Matrix after multiplication. + */ template inline Matrix operator*(const VariableSparseMatrix &A, const Matrix &B) { @@ -105,6 +215,22 @@ inline Matrix operator*(const VariableSparseMatrix &A, return Y; } +/** + * @brief Overloaded operator* for multiplying a SparseMatrix with a + * VariableSparseMatrix. + * + * This function performs matrix multiplication between a SparseMatrix and a + * VariableSparseMatrix, returning the resulting Matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the SparseMatrix. + * @tparam N The number of rows in the SparseMatrix and rows in the + * VariableSparseMatrix. + * @tparam K The number of columns in the VariableSparseMatrix. + * @param A The SparseMatrix to multiply. + * @param B The VariableSparseMatrix to multiply with. + * @return The resulting Matrix after multiplication. + */ template inline Matrix operator*(const Matrix &A, const VariableSparseMatrix &B) { @@ -122,6 +248,23 @@ inline Matrix operator*(const Matrix &A, } /* SparseMatrix * SparseMatrix */ + +/** + * @brief Overloaded operator* for multiplying two SparseMatrix objects. + * + * This function performs matrix multiplication between two SparseMatrix + * objects, returning the resulting Matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the first SparseMatrix. + * @tparam N The number of rows in the first SparseMatrix and rows in the + * second SparseMatrix. + * @tparam K The number of columns in the second SparseMatrix. + * @tparam V The maximum number of non-zero values in the SparseMatrix. + * @param A The first SparseMatrix to multiply. + * @param B The second SparseMatrix to multiply with. + * @return The resulting Matrix after multiplication. + */ template inline Matrix operator*(const VariableSparseMatrix &A, @@ -146,6 +289,23 @@ inline Matrix operator*(const VariableSparseMatrix &A, return Y; } +/** + * @brief Overloaded operator* for multiplying a SparseMatrix with a + * VariableSparseMatrix. + * + * This function performs matrix multiplication between a SparseMatrix and a + * VariableSparseMatrix, returning the resulting Matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the SparseMatrix. + * @tparam N The number of rows in the SparseMatrix and rows in the + * VariableSparseMatrix. + * @tparam K The number of columns in the VariableSparseMatrix. + * @tparam V The maximum number of non-zero values in the SparseMatrix. + * @param A The SparseMatrix to multiply. + * @param B The VariableSparseMatrix to multiply with. + * @return The resulting Matrix after multiplication. + */ template inline Matrix operator*(const SparseMatrix &A, @@ -170,6 +330,22 @@ inline Matrix operator*(const SparseMatrix &A, return Y; } +/** + * @brief Overloaded operator* for multiplying two VariableSparseMatrix + * objects. + * + * This function performs matrix multiplication between two VariableSparseMatrix + * objects, returning the resulting Matrix. + * + * @tparam T The type of the matrix elements. + * @tparam M The number of columns in the first VariableSparseMatrix. + * @tparam N The number of rows in the first VariableSparseMatrix and rows in + * the second VariableSparseMatrix. + * @tparam K The number of columns in the second VariableSparseMatrix. + * @param A The first VariableSparseMatrix to multiply. + * @param B The second VariableSparseMatrix to multiply with. + * @return The resulting Matrix after multiplication. + */ template inline Matrix operator*(const VariableSparseMatrix &A, const VariableSparseMatrix &B) { From 4be3a9542e17db4a87cf8c0bb2dc428296b992d9 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 13 Jun 2025 23:14:38 +0900 Subject: [PATCH 18/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base_matrix/base_matrix_vector.hpp | 1043 +++++++++++++++++++++++++++- 1 file changed, 1030 insertions(+), 13 deletions(-) diff --git a/base_matrix/base_matrix_vector.hpp b/base_matrix/base_matrix_vector.hpp index b025d84..76c328c 100644 --- a/base_matrix/base_matrix_vector.hpp +++ b/base_matrix/base_matrix_vector.hpp @@ -1,3 +1,21 @@ +/** + * @file base_matrix_vector.hpp + * @brief Provides a templated fixed-size vector class and related operations + * for mathematical computations. + * + * This file defines the Base::Matrix::Vector class template and a set of + * associated functions and operator overloads for performing mathematical + * operations on vectors, including addition, subtraction, multiplication, + * normalization, dot product, and norm calculations. The implementation + * supports both real and complex vectors, and can be configured to use either + * std::vector or std::array as the underlying storage, depending on the + * compilation flags. + * + * The code also includes utility functions for extracting real and imaginary + * parts from complex vectors, as well as specialized operations for column + * vectors and normalization routines. + * + */ #ifndef __BASE_MATRIX_VECTOR_HPP__ #define __BASE_MATRIX_VECTOR_HPP__ @@ -18,8 +36,21 @@ namespace Base { namespace Matrix { /* Vector */ + +/** + * @brief Templated fixed-size vector class for mathematical computations. + * + * This class provides a fixed-size vector implementation with various + * mathematical operations, including dot product, normalization, and scalar + * addition/subtraction. It can be configured to use either std::vector or + * std::array as the underlying storage. + * + * @tparam T The type of the vector elements (e.g., float, double). + * @tparam N The size of the vector. + */ template class Vector { public: +/* Constructor */ #ifdef __BASE_MATRIX_USE_STD_VECTOR__ Vector() : data(N, static_cast(0)) {} @@ -68,7 +99,18 @@ template class Vector { return *this; } +public: /* Function */ + + /** + * @brief Accessor for vector elements. + * + * This function provides access to the vector elements using the subscript + * operator. If the index is out of bounds, it returns the last element. + * + * @param index The index of the element to access. + * @return A reference to the element at the specified index. + */ T &operator[](std::size_t index) { if (index >= N) { index = N - 1; @@ -76,6 +118,16 @@ template class Vector { return this->data[index]; } + /** + * @brief Const accessor for vector elements. + * + * This function provides const access to the vector elements using the + * subscript operator. If the index is out of bounds, it returns the last + * element. + * + * @param index The index of the element to access. + * @return A const reference to the element at the specified index. + */ const T &operator[](std::size_t index) const { if (index >= N) { index = N - 1; @@ -83,13 +135,42 @@ template class Vector { return this->data[index]; } + /** + * @brief Accessor for vector elements using the subscript operator. + * + * This function provides access to the vector elements using the subscript + * operator. If the index is out of bounds, it returns the last element. + * + * @param index The index of the element to access. + * @return A reference to the element at the specified index. + */ constexpr std::size_t size() const { return N; } + /** + * @brief Accessor for the first element of the vector. + * + * This function returns a reference to the first element of the vector. + * + * @return A reference to the first element. + */ static inline Vector Ones(void) { return Vector(std::vector(N, static_cast(1))); } /* dot */ + + /** + * @brief Computes the dot product of two vectors. + * + * This function computes the dot product of two vectors using a recursive + * template structure for efficiency. + * + * @tparam U The type of the vector elements (should match T). + * @tparam P The size of the vectors (should match N). + * @param a The first vector. + * @param b The second vector. + * @return The dot product of the two vectors. + */ template struct VectorDotCore { static T compute(const Vector &a, const Vector &b) { return a[P_idx] * b[P_idx] + @@ -97,18 +178,49 @@ template class Vector { } }; - // Termination condition: P_idx == 0 + /** + * @brief Specialization for the base case of the recursive dot product + * computation. + * + * This specialization handles the case when the index reaches 0, returning + * the product of the first elements of both vectors. + * + * @tparam U The type of the vector elements (should match T). + * @tparam P The size of the vectors (should match N). + */ template struct VectorDotCore { static T compute(const Vector &a, const Vector &b) { return a[0] * b[0]; } }; + /** + * @brief Computes the dot product of two vectors. + * + * This function provides a convenient interface for computing the dot + * product of two vectors using the VectorDotCore structure. + * + * @tparam U The type of the vector elements (should match T). + * @tparam P The size of the vectors (should match N). + * @param a The first vector. + * @param b The second vector. + * @return The dot product of the two vectors. + */ template static inline T VECTOR_DOT(const Vector &a, const Vector &b) { return VectorDotCore::compute(a, b); } + /** + * @brief Computes the dot product of this vector with another vector. + * + * This function computes the dot product of this vector with another vector + * using either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @param other The other vector to compute the dot product with. + * @return The dot product of the two vectors. + */ inline T dot(const Vector &other) const { T result = static_cast(0); @@ -127,6 +239,16 @@ template class Vector { return result; } + /** + * @brief Computes the dot product of this vector with another vector. + * + * This function computes the dot product of this vector with another vector + * using either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @param other The other vector to compute the dot product with. + * @return The dot product of the two vectors. + */ inline T norm() const { T sum = this->dot(*this); @@ -134,6 +256,18 @@ template class Vector { return Base::Math::sqrt(sum); } + /** + * @brief Computes the dot product of this vector with another vector. + * + * This function computes the dot product of this vector with another vector + * using either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @param other The other vector to compute the dot product with. + * @param division_min The minimum value for division to avoid division by + * zero. + * @return The dot product of the two vectors. + */ inline T norm(const T &division_min) const { T sum = this->dot(*this); @@ -141,6 +275,14 @@ template class Vector { return Base::Math::sqrt(sum, division_min); } + /** + * @brief Computes the inverse of the norm of this vector. + * + * This function computes the inverse of the norm of this vector, which is + * useful for normalization. + * + * @return The inverse of the norm of this vector. + */ inline T norm_inv() const { T sum = this->dot(*this); @@ -148,6 +290,18 @@ template class Vector { return Base::Math::rsqrt(sum); } + /** + * @brief Computes the inverse of the norm of this vector with a minimum + * division value. + * + * This function computes the inverse of the norm of this vector, which is + * useful for normalization, while ensuring that division by zero is avoided + * by using a minimum division value. + * + * @param division_min The minimum value for division to avoid division by + * zero. + * @return The inverse of the norm of this vector. + */ inline T norm_inv(const T &division_min) const { T sum = this->dot(*this); @@ -163,12 +317,52 @@ template class Vector { #endif // __BASE_MATRIX_USE_STD_VECTOR__ }; +/** + * @brief Specialization of the Vector class for complex numbers. + * + * This specialization provides additional functionality for complex vectors, + * including methods to extract real and imaginary parts. + * + * @tparam T The type of the complex vector elements (e.g., + * std::complex). + * @tparam N The size of the vector. + */ template class ColVector : public Vector { public: + /* Constructor */ ColVector() : Vector() {} ColVector(const Vector &vec) : Vector(vec) {} + /* Copy Constructor */ + ColVector(const ColVector &vec) : Vector(vec) {} + ColVector &operator=(const ColVector &vec) { + if (this != &vec) { + this->data = vec.data; + } + return *this; + } + + /* Move Constructor */ + ColVector(ColVector &&vec) noexcept : Vector(std::move(vec)) {} + ColVector &operator=(ColVector &&vec) noexcept { + if (this != &vec) { + this->data = std::move(vec.data); + } + return *this; + } + +public: + /* Function */ + + /** + * @brief Extracts the real part of the complex vector. + * + * This function returns a new vector containing the real parts of the + * complex numbers in this vector. + * + * @return A vector containing the real parts of the complex numbers. + */ inline Vector transpose() const { Vector result; @@ -183,6 +377,17 @@ namespace VectorNormalize { template struct VectorNormalizeCore { + /** + * @brief Normalizes the vector by multiplying each element with the inverse + * of the norm. + * + * This function recursively normalizes the vector by multiplying each + * element with the inverse of the norm, starting from the last index and + * moving towards the first. + * + * @param vec The vector to normalize. + * @param norm_inv The inverse of the norm to multiply each element with. + */ static void compute(Vector &vec, T norm_inv) { vec[Index] *= norm_inv; VectorNormalizeCore::compute(vec, norm_inv); @@ -191,9 +396,31 @@ struct VectorNormalizeCore { // Specialization to end the recursion template struct VectorNormalizeCore { + /** + * @brief Normalizes the first element of the vector by multiplying it with + * the inverse of the norm. + * + * This function is called when the recursion reaches the first index, and it + * multiplies the first element of the vector with the inverse of the norm. + * + * @param vec The vector to normalize. + * @param norm_inv The inverse of the norm to multiply the first element with. + */ static void compute(Vector &vec, T norm_inv) { vec[0] *= norm_inv; } }; +/** + * @brief Computes the normalization of a vector by multiplying each element + * with the inverse of the norm. + * + * This function provides a convenient interface for normalizing a vector by + * calling the VectorNormalizeCore structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to normalize. + * @param norm_inv The inverse of the norm to multiply each element with. + */ template inline void compute(Vector &vec, T norm_inv) { VectorNormalizeCore::compute(vec, norm_inv); @@ -201,6 +428,18 @@ inline void compute(Vector &vec, T norm_inv) { } // namespace VectorNormalize +/** + * @brief Normalizes a vector by multiplying each element with the inverse of + * the norm. + * + * This function normalizes the vector by multiplying each element with the + * inverse of the norm, which is computed using the dot product of the vector + * with itself. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to normalize. + */ template inline void vector_normalize(Vector &vec) { T norm_inv = vec.norm_inv(); @@ -218,6 +457,21 @@ inline void vector_normalize(Vector &vec) { #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Normalizes a vector by multiplying each element with the inverse of + * the norm, using a minimum division value to avoid division by zero. + * + * This function normalizes the vector by multiplying each element with the + * inverse of the norm, which is computed using the dot product of the vector + * with itself, while ensuring that division by zero is avoided by using a + * minimum division value. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to normalize. + * @param division_min The minimum value for division to avoid division by + * zero. + */ template inline void vector_normalize(Vector &vec, const T &division_min) { T norm_inv = vec.norm_inv(division_min); @@ -240,6 +494,16 @@ namespace VectorAddScalar { // Vector Add Scalar Core Template: N_idx < N template struct Core { + /** + * @brief Adds a scalar to each element of the vector. + * + * This function recursively adds a scalar to each element of the vector, + * starting from the last index and moving towards the first. + * + * @param vec The vector to which the scalar is added. + * @param scalar The scalar value to add to each element of the vector. + * @param result The resulting vector after adding the scalar. + */ static void compute(const Vector &vec, T scalar, Vector &result) { result[N_idx] = vec[N_idx] + scalar; Core::compute(vec, scalar, result); @@ -248,11 +512,33 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Adds a scalar to the first element of the vector. + * + * This function is called when the recursion reaches the first index, and it + * adds the scalar to the first element of the vector. + * + * @param vec The vector to which the scalar is added. + * @param scalar The scalar value to add to the first element of the vector. + * @param result The resulting vector after adding the scalar. + */ static void compute(const Vector &vec, T scalar, Vector &result) { result[0] = vec[0] + scalar; } }; +/** + * @brief Computes the addition of a scalar to each element of the vector. + * + * This function provides a convenient interface for adding a scalar to each + * element of the vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to which the scalar is added. + * @param scalar The scalar value to add to each element of the vector. + * @param result The resulting vector after adding the scalar. + */ template inline void compute(const Vector &vec, T scalar, Vector &result) { Core::compute(vec, scalar, result); @@ -260,6 +546,19 @@ inline void compute(const Vector &vec, T scalar, Vector &result) { } // namespace VectorAddScalar +/** + * @brief Adds a scalar to each element of the vector. + * + * This function adds a scalar to each element of the vector, using either a + * for loop or a recursive template structure, depending on the compilation + * flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to which the scalar is added. + * @param scalar The scalar value to add to each element of the vector. + * @return A new vector with the scalar added to each element. + */ template inline Vector operator+(const Vector &vec, const T &scalar) { Vector result; @@ -279,6 +578,19 @@ inline Vector operator+(const Vector &vec, const T &scalar) { return result; } +/** + * @brief Adds a scalar to each element of the vector. + * + * This function adds a scalar to each element of the vector, using either a + * for loop or a recursive template structure, depending on the compilation + * flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param scalar The scalar value to add to each element of the vector. + * @param vec The vector to which the scalar is added. + * @return A new vector with the scalar added to each element. + */ template inline Vector operator+(const T &scalar, const Vector &vec) { Vector result; @@ -303,6 +615,16 @@ namespace VectorSubScalar { // Vector Sub Scalar Core Template: N_idx < N template struct Core { + /** + * @brief Subtracts a scalar from each element of the vector. + * + * This function recursively subtracts a scalar from each element of the + * vector, starting from the last index and moving towards the first. + * + * @param vec The vector from which the scalar is subtracted. + * @param scalar The scalar value to subtract from each element of the vector. + * @param result The resulting vector after subtracting the scalar. + */ static void compute(const Vector &vec, T scalar, Vector &result) { result[N_idx] = vec[N_idx] - scalar; Core::compute(vec, scalar, result); @@ -311,11 +633,34 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Subtracts a scalar from the first element of the vector. + * + * This function is called when the recursion reaches the first index, and it + * subtracts the scalar from the first element of the vector. + * + * @param vec The vector from which the scalar is subtracted. + * @param scalar The scalar value to subtract from the first element of the + * vector. + * @param result The resulting vector after subtracting the scalar. + */ static void compute(const Vector &vec, T scalar, Vector &result) { result[0] = vec[0] - scalar; } }; +/** + * @brief Computes the subtraction of a scalar from each element of the vector. + * + * This function provides a convenient interface for subtracting a scalar from + * each element of the vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector from which the scalar is subtracted. + * @param scalar The scalar value to subtract from each element of the vector. + * @param result The resulting vector after subtracting the scalar. + */ template inline void compute(const Vector &vec, const T &scalar, Vector &result) { @@ -324,6 +669,19 @@ inline void compute(const Vector &vec, const T &scalar, } // namespace VectorSubScalar +/** + * @brief Subtracts a scalar from each element of the vector. + * + * This function subtracts a scalar from each element of the vector, using + * either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector from which the scalar is subtracted. + * @param scalar The scalar value to subtract from each element of the vector. + * @return A new vector with the scalar subtracted from each element. + */ template inline Vector operator-(const Vector &vec, const T &scalar) { Vector result; @@ -347,6 +705,17 @@ namespace ScalarSubVector { // Scalar Sub Vector Core Template: N_idx < N template struct Core { + /** + * @brief Subtracts a vector from a scalar. + * + * This function recursively subtracts each element of the vector from the + * scalar, starting from the last index and moving towards the first. + * + * @param scalar The scalar value from which the vector is subtracted. + * @param vec The vector to subtract from the scalar. + * @param result The resulting vector after subtracting the vector from the + * scalar. + */ static void compute(T scalar, const Vector &vec, Vector &result) { result[N_idx] = scalar - vec[N_idx]; Core::compute(vec, scalar, result); @@ -355,11 +724,36 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Subtracts the first element of the vector from the scalar. + * + * This function is called when the recursion reaches the first index, and it + * subtracts the first element of the vector from the scalar. + * + * @param scalar The scalar value from which the first element of the vector + * is subtracted. + * @param vec The vector whose first element is subtracted from the scalar. + * @param result The resulting vector after subtracting the first element of + * the vector from the scalar. + */ static void compute(T scalar, const Vector &vec, Vector &result) { result[0] = scalar - vec[0]; } }; +/** + * @brief Computes the subtraction of a vector from a scalar. + * + * This function provides a convenient interface for subtracting a vector from + * a scalar using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param scalar The scalar value from which the vector is subtracted. + * @param vec The vector to subtract from the scalar. + * @param result The resulting vector after subtracting the vector from the + * scalar. + */ template inline void compute(const T &scalar, const Vector &vec, Vector &result) { @@ -368,6 +762,20 @@ inline void compute(const T &scalar, const Vector &vec, } // namespace ScalarSubVector +/** + * @brief Subtracts a vector from a scalar. + * + * This function subtracts each element of the vector from the scalar, using + * either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param scalar The scalar value from which the vector is subtracted. + * @param vec The vector to subtract from the scalar. + * @return A new vector with the scalar subtracted from each element of the + * vector. + */ template inline Vector operator-(const T &scalar, const Vector &vec) { Vector result; @@ -392,6 +800,16 @@ namespace VectorAddVector { // Vector Add Scalar Core Template: N_idx < N template struct Core { + /** + * @brief Adds two vectors element-wise. + * + * This function recursively adds the corresponding elements of two vectors, + * starting from the last index and moving towards the first. + * + * @param a The first vector to add. + * @param b The second vector to add. + * @param result The resulting vector after adding the two vectors. + */ static void compute(const Vector &a, const Vector b, Vector &result) { result[N_idx] = a[N_idx] + b[N_idx]; @@ -401,12 +819,35 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Adds the first elements of two vectors. + * + * This function is called when the recursion reaches the first index, and it + * adds the first elements of the two vectors. + * + * @param a The first vector to add. + * @param b The second vector to add. + * @param result The resulting vector after adding the first elements of the + * two vectors. + */ static void compute(const Vector &a, const Vector b, Vector &result) { result[0] = a[0] + b[0]; } }; +/** + * @brief Computes the addition of two vectors element-wise. + * + * This function provides a convenient interface for adding two vectors using + * the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vectors. + * @param a The first vector to add. + * @param b The second vector to add. + * @param result The resulting vector after adding the two vectors. + */ template inline void compute(const Vector &a, const Vector &b, Vector &result) { @@ -415,6 +856,18 @@ inline void compute(const Vector &a, const Vector &b, } // namespace VectorAddVector +/** + * @brief Adds two vectors element-wise. + * + * This function adds two vectors element-wise, using either a for loop or a + * recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vectors. + * @param a The first vector to add. + * @param b The second vector to add. + * @return A new vector with the result of the addition. + */ template inline Vector operator+(const Vector &a, const Vector &b) { Vector result; @@ -439,6 +892,16 @@ namespace VectorSubVector { // Vector Sub Scalar Core Template: N_idx < N template struct Core { + /** + * @brief Subtracts two vectors element-wise. + * + * This function recursively subtracts the corresponding elements of two + * vectors, starting from the last index and moving towards the first. + * + * @param a The first vector from which the second vector is subtracted. + * @param b The second vector to subtract from the first vector. + * @param result The resulting vector after subtracting the two vectors. + */ static void compute(const Vector &a, const Vector b, Vector &result) { result[N_idx] = a[N_idx] - b[N_idx]; @@ -448,12 +911,35 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Subtracts the first elements of two vectors. + * + * This function is called when the recursion reaches the first index, and it + * subtracts the first element of the second vector from the first element of + * the first vector. + * + * @param a The first vector from which the second vector is subtracted. + * @param b The second vector to subtract from the first vector. + * @param result The resulting vector after subtracting the two vectors. + */ static void compute(const Vector &a, const Vector b, Vector &result) { result[0] = a[0] - b[0]; } }; +/** + * @brief Computes the subtraction of two vectors element-wise. + * + * This function provides a convenient interface for subtracting two vectors + * using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vectors. + * @param a The first vector from which the second vector is subtracted. + * @param b The second vector to subtract from the first vector. + * @param result The resulting vector after subtracting the two vectors. + */ template inline void compute(const Vector &a, const Vector &b, Vector &result) { @@ -462,6 +948,18 @@ inline void compute(const Vector &a, const Vector &b, } // namespace VectorSubVector +/** + * @brief Subtracts two vectors element-wise. + * + * This function subtracts two vectors element-wise, using either a for loop or + * a recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vectors. + * @param a The first vector from which the second vector is subtracted. + * @param b The second vector to subtract from the first vector. + * @return A new vector with the result of the subtraction. + */ template inline Vector operator-(const Vector &a, const Vector &b) { Vector result; @@ -486,6 +984,16 @@ namespace VectorMultiplyScalar { // Vector Multiply Scalar Core Template: N_idx < N template struct Core { + /** + * @brief Multiplies each element of the vector by a scalar. + * + * This function recursively multiplies each element of the vector by a + * scalar, starting from the last index and moving towards the first. + * + * @param vec The vector to multiply by the scalar. + * @param scalar The scalar value to multiply each element of the vector with. + * @param result The resulting vector after multiplying by the scalar. + */ static void compute(const Vector &vec, T scalar, Vector &result) { result[N_idx] = vec[N_idx] * scalar; Core::compute(vec, scalar, result); @@ -494,11 +1002,34 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Multiplies the first element of the vector by a scalar. + * + * This function is called when the recursion reaches the first index, and it + * multiplies the first element of the vector by the scalar. + * + * @param vec The vector to multiply by the scalar. + * @param scalar The scalar value to multiply the first element of the vector + * with. + * @param result The resulting vector after multiplying by the scalar. + */ static void compute(const Vector &vec, T scalar, Vector &result) { result[0] = vec[0] * scalar; } }; +/** + * @brief Computes the multiplication of a vector by a scalar. + * + * This function provides a convenient interface for multiplying a vector by a + * scalar using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to multiply by the scalar. + * @param scalar The scalar value to multiply each element of the vector with. + * @param result The resulting vector after multiplying by the scalar. + */ template inline void compute(const Vector &vec, T scalar, Vector &result) { Core::compute(vec, scalar, result); @@ -506,6 +1037,19 @@ inline void compute(const Vector &vec, T scalar, Vector &result) { } // namespace VectorMultiplyScalar +/** + * @brief Multiplies a vector by a scalar. + * + * This function multiplies each element of the vector by a scalar, using + * either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The vector to multiply by the scalar. + * @param scalar The scalar value to multiply each element of the vector with. + * @return A new vector with each element multiplied by the scalar. + */ template inline Vector operator*(const Vector &vec, const T &scalar) { Vector result; @@ -525,6 +1069,19 @@ inline Vector operator*(const Vector &vec, const T &scalar) { return result; } +/** + * @brief Multiplies a scalar by a vector. + * + * This function multiplies each element of the vector by a scalar, using + * either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param scalar The scalar value to multiply each element of the vector with. + * @param vec The vector to multiply by the scalar. + * @return A new vector with each element multiplied by the scalar. + */ template inline Vector operator*(const T &scalar, const Vector &vec) { Vector result; @@ -548,6 +1105,16 @@ inline Vector operator*(const T &scalar, const Vector &vec) { namespace VectorMultiply { template struct Core { + /** + * @brief Multiplies two vectors element-wise. + * + * This function recursively multiplies the corresponding elements of two + * vectors, starting from the last index and moving towards the first. + * + * @param a The first vector to multiply. + * @param b The second vector to multiply. + * @param result The resulting vector after multiplying the two vectors. + */ static void compute(const Vector &a, const Vector &b, Vector &result) { result[N_idx] = a[N_idx] * b[N_idx]; @@ -557,12 +1124,35 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Multiplies the first elements of two vectors. + * + * This function is called when the recursion reaches the first index, and it + * multiplies the first elements of the two vectors. + * + * @param a The first vector to multiply. + * @param b The second vector to multiply. + * @param result The resulting vector after multiplying the first elements of + * the two vectors. + */ static void compute(const Vector &a, const Vector &b, Vector &result) { result[0] = a[0] * b[0]; } }; +/** + * @brief Computes the multiplication of two vectors element-wise. + * + * This function provides a convenient interface for multiplying two vectors + * using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vectors. + * @param a The first vector to multiply. + * @param b The second vector to multiply. + * @param result The resulting vector after multiplying the two vectors. + */ template inline void compute(const Vector &a, const Vector &b, Vector &result) { @@ -571,6 +1161,18 @@ inline void compute(const Vector &a, const Vector &b, } // namespace VectorMultiply +/** + * @brief Multiplies two vectors element-wise. + * + * This function multiplies two vectors element-wise, using either a for loop or + * a recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vectors. + * @param a The first vector to multiply. + * @param b The second vector to multiply. + * @return A new vector with the result of the multiplication. + */ template inline Vector operator*(const Vector &a, const Vector &b) { Vector result; @@ -595,6 +1197,15 @@ namespace ComplexVectorNorm { // Vector Multiply Scalar Core Template: N_idx < N template struct Core { + /** + * @brief Computes the squared norm of a complex vector. + * + * This function recursively computes the squared norm of a complex vector, + * starting from the last index and moving towards the first. + * + * @param vec_comp The complex vector whose norm is computed. + * @return The squared norm of the complex vector. + */ static T compute(const Vector, N> &vec_comp) { return vec_comp[N_idx].real * vec_comp[N_idx].real + vec_comp[N_idx].imag * vec_comp[N_idx].imag + @@ -604,12 +1215,32 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { + /** + * @brief Computes the squared norm of the first element of a complex vector. + * + * This function is called when the recursion reaches the first index, and it + * computes the squared norm of the first element of the complex vector. + * + * @param vec_comp The complex vector whose norm is computed. + * @return The squared norm of the first element of the complex vector. + */ static T compute(const Vector, N> &vec_comp) { return vec_comp[0].real * vec_comp[0].real + vec_comp[0].imag * vec_comp[0].imag; } }; +/** + * @brief Computes the squared norm of a complex vector. + * + * This function provides a convenient interface for computing the squared norm + * of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec_comp The complex vector whose norm is computed. + * @return The squared norm of the complex vector. + */ template inline T compute(const Vector, N> &vec_comp) { return Core::compute(vec_comp); @@ -617,6 +1248,17 @@ inline T compute(const Vector, N> &vec_comp) { } // namespace ComplexVectorNorm +/** + * @brief Computes the norm of a complex vector. + * + * This function computes the norm of a complex vector, using either a for loop + * or a recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec_comp The complex vector whose norm is computed. + * @return The norm of the complex vector. + */ template inline T complex_vector_norm(const Vector, N> &vec_comp) { T sum = static_cast(0); @@ -638,6 +1280,18 @@ inline T complex_vector_norm(const Vector, N> &vec_comp) { sum); } +/** + * @brief Computes the norm of a complex vector with a minimum division value. + * + * This function computes the norm of a complex vector, using either a for loop + * or a recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec_comp The complex vector whose norm is computed. + * @param division_min The minimum value for division to avoid division by zero. + * @return The norm of the complex vector. + */ template inline T complex_vector_norm(const Vector, N> &vec_comp, const T &division_min) { @@ -659,6 +1313,18 @@ inline T complex_vector_norm(const Vector, N> &vec_comp, return Base::Math::sqrt(sum, division_min); } +/** + * @brief Computes the inverse norm of a complex vector. + * + * This function computes the inverse norm of a complex vector, using either a + * for loop or a recursive template structure, depending on the compilation + * flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec_comp The complex vector whose inverse norm is computed. + * @return The inverse norm of the complex vector. + */ template inline T complex_vector_norm_inv(const Vector, N> &vec_comp) { T sum = static_cast(0); @@ -679,6 +1345,20 @@ inline T complex_vector_norm_inv(const Vector, N> &vec_comp) { return Base::Math::rsqrt(sum); } +/** + * @brief Computes the inverse norm of a complex vector with a minimum division + * value. + * + * This function computes the inverse norm of a complex vector, using either a + * for loop or a recursive template structure, depending on the compilation + * flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec_comp The complex vector whose inverse norm is computed. + * @param division_min The minimum value for division to avoid division by zero. + * @return The inverse norm of the complex vector. + */ template inline T complex_vector_norm_inv(const Vector, N> &vec_comp, const T &division_min) { @@ -704,6 +1384,16 @@ inline T complex_vector_norm_inv(const Vector, N> &vec_comp, namespace ComplexVectorNormalize { template struct Core { + /** + * @brief Normalizes a complex vector by multiplying each element by the + * inverse norm. + * + * This function recursively normalizes each element of the complex vector, + * starting from the last index and moving towards the first. + * + * @param vec The complex vector to normalize. + * @param norm_inv The inverse norm value to multiply each element by. + */ static void compute(Vector, N> &vec, T norm_inv) { vec[Index] *= norm_inv; Core::compute(vec, norm_inv); @@ -712,11 +1402,32 @@ template struct Core { // Specialization to end the recursion template struct Core { + /** + * @brief Normalizes the first element of a complex vector by multiplying it + * by the inverse norm. + * + * This function is called when the recursion reaches the first index, and it + * multiplies the first element of the complex vector by the inverse norm. + * + * @param vec The complex vector to normalize. + * @param norm_inv The inverse norm value to multiply the first element by. + */ static void compute(Vector, N> &vec, T norm_inv) { vec[0] *= norm_inv; } }; +/** + * @brief Computes the normalization of a complex vector. + * + * This function provides a convenient interface for normalizing a complex + * vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The complex vector to normalize. + * @param norm_inv The inverse norm value to multiply each element by. + */ template inline void compute(Vector, N> &vec, T norm_inv) { Core::compute(vec, norm_inv); @@ -724,6 +1435,17 @@ inline void compute(Vector, N> &vec, T norm_inv) { } // namespace ComplexVectorNormalize +/** + * @brief Normalizes a complex vector by multiplying each element by the inverse + * norm. + * + * This function normalizes a complex vector, using either a for loop or a + * recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The complex vector to normalize. + */ template inline void complex_vector_normalize(Vector, N> &vec) { T norm_inv = complex_vector_norm_inv(vec); @@ -741,6 +1463,18 @@ inline void complex_vector_normalize(Vector, N> &vec) { #endif // __BASE_MATRIX_USE_FOR_LOOP_OPERATION__ } +/** + * @brief Normalizes a complex vector by multiplying each element by the inverse + * norm with a minimum division value. + * + * This function normalizes a complex vector, using either a for loop or a + * recursive template structure, depending on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param vec The complex vector to normalize. + * @param division_min The minimum value for division to avoid division by zero. + */ template inline void complex_vector_normalize(Vector, N> &vec, const T &division_min) { @@ -764,21 +1498,51 @@ namespace GetRealFromComplexVector { // Get Real from Complex Vector Core Template: N_idx < N template struct Core { - // For std::vector + /** + * @brief Extracts the real part from a complex vector and stores it in a + * vector. + * + * This function recursively extracts the real part of each element of a + * complex vector, starting from the last index and moving towards the first. + * + * @param To_vector The vector to store the real parts. + * @param From_vector The complex vector from which the real parts are + * extracted. + */ static void compute(std::vector &To_vector, const std::vector> &From_vector) { To_vector[N_idx] = From_vector[N_idx].real; Core::compute(To_vector, From_vector); } - // For std::array + /** + * @brief Extracts the real part from a complex vector and stores it in an + * array. + * + * This function recursively extracts the real part of each element of a + * complex vector, starting from the last index and moving towards the first. + * + * @param To_vector The array to store the real parts. + * @param From_vector The complex vector from which the real parts are + * extracted. + */ static void compute(std::array &To_vector, const std::array, N> &From_vector) { To_vector[N_idx] = From_vector[N_idx].real; Core::compute(To_vector, From_vector); } - // For Vector + /** + * @brief Extracts the real part from a complex vector and stores it in a + * Vector. + * + * This function recursively extracts the real part of each element of a + * complex vector, starting from the last index and moving towards the first. + * + * @param To_vector The Vector to store the real parts. + * @param From_vector The complex vector from which the real parts are + * extracted. + */ static void compute(Vector &To_vector, const Vector, N> &From_vector) { To_vector[N_idx] = From_vector[N_idx].real; @@ -788,37 +1552,105 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { - // For std::vector + /** + * @brief Extracts the real part from the first element of a complex vector + * and stores it in a vector. + * + * This function is called when the recursion reaches the first index, and it + * extracts the real part of the first element of the complex vector. + * + * @param To_vector The vector to store the real part. + * @param From_vector The complex vector from which the real part is + * extracted. + */ static void compute(std::vector &To_vector, const std::vector> &From_vector) { To_vector[0] = From_vector[0].real; } - // For std::array + /** + * @brief Extracts the real part from the first element of a complex vector + * and stores it in an array. + * + * This function is called when the recursion reaches the first index, and it + * extracts the real part of the first element of the complex vector. + * + * @param To_vector The array to store the real part. + * @param From_vector The complex vector from which the real part is + * extracted. + */ static void compute(std::array &To_vector, const std::array, N> &From_vector) { To_vector[0] = From_vector[0].real; } - // For Vector + /** + * @brief Extracts the real part from the first element of a complex vector + * and stores it in a Vector. + * + * This function is called when the recursion reaches the first index, and it + * extracts the real part of the first element of the complex vector. + * + * @param To_vector The Vector to store the real part. + * @param From_vector The complex vector from which the real part is + * extracted. + */ static void compute(Vector &To_vector, const Vector, N> &From_vector) { To_vector[0] = From_vector[0].real; } }; +/** + * @brief Computes the real part of a complex vector and stores it in a vector, + * array, or Vector. + * + * This function provides a convenient interface for extracting the real part + * of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param To_vector The vector, array, or Vector to store the real parts. + * @param From_vector The complex vector from which the real parts are + * extracted. + */ template inline void compute(std::vector &To_vector, const std::vector> &From_vector) { Core::compute(To_vector, From_vector); } +/** + * @brief Computes the real part of a complex vector and stores it in an array + * or Vector. + * + * This function provides a convenient interface for extracting the real part + * of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param To_vector The array or Vector to store the real parts. + * @param From_vector The complex vector from which the real parts are + * extracted. + */ template inline void compute(std::array &To_vector, const std::array, N> &From_vector) { Core::compute(To_vector, From_vector); } +/** + * @brief Computes the real part of a complex vector and stores it in a Vector. + * + * This function provides a convenient interface for extracting the real part + * of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param To_vector The Vector to store the real parts. + * @param From_vector The complex vector from which the real parts are + * extracted. + */ template inline void compute(Vector &To_vector, const Vector, N> &From_vector) { @@ -831,21 +1663,51 @@ namespace GetImagFromComplexVector { // Get Imag from Complex Vector Core Template: N_idx < N template struct Core { - // For std::vector + /** + * @brief Extracts the imaginary part from a complex vector and stores it in a + * vector. + * + * This function recursively extracts the imaginary part of each element of a + * complex vector, starting from the last index and moving towards the first. + * + * @param To_vector The vector to store the imaginary parts. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + */ static void compute(std::vector &To_vector, const std::vector> &From_vector) { To_vector[N_idx] = From_vector[N_idx].imag; Core::compute(To_vector, From_vector); } - // For std::array + /** + * @brief Extracts the imaginary part from a complex vector and stores it in + * an array. + * + * This function recursively extracts the imaginary part of each element of a + * complex vector, starting from the last index and moving towards the first. + * + * @param To_vector The array to store the imaginary parts. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + */ static void compute(std::array &To_vector, const std::array, N> &From_vector) { To_vector[N_idx] = From_vector[N_idx].imag; Core::compute(To_vector, From_vector); } - // For Vector + /** + * @brief Extracts the imaginary part from a complex vector and stores it in a + * Vector. + * + * This function recursively extracts the imaginary part of each element of a + * complex vector, starting from the last index and moving towards the first. + * + * @param To_vector The Vector to store the imaginary parts. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + */ static void compute(Vector &To_vector, const Vector, N> &From_vector) { To_vector[N_idx] = From_vector[N_idx].imag; @@ -855,37 +1717,106 @@ template struct Core { // Termination condition: N_idx == 0 template struct Core { - // For std::vector + /** + * @brief Extracts the imaginary part from the first element of a complex + * vector and stores it in a vector. + * + * This function is called when the recursion reaches the first index, and it + * extracts the imaginary part of the first element of the complex vector. + * + * @param To_vector The vector to store the imaginary part. + * @param From_vector The complex vector from which the imaginary part is + * extracted. + */ static void compute(std::vector &To_vector, const std::vector> &From_vector) { To_vector[0] = From_vector[0].imag; } - // For std::array + /** + * @brief Extracts the imaginary part from the first element of a complex + * vector and stores it in an array. + * + * This function is called when the recursion reaches the first index, and it + * extracts the imaginary part of the first element of the complex vector. + * + * @param To_vector The array to store the imaginary part. + * @param From_vector The complex vector from which the imaginary part is + * extracted. + */ static void compute(std::array &To_vector, const std::array, N> &From_vector) { To_vector[0] = From_vector[0].imag; } - // For Vector + /** + * @brief Extracts the imaginary part from the first element of a complex + * vector and stores it in a Vector. + * + * This function is called when the recursion reaches the first index, and it + * extracts the imaginary part of the first element of the complex vector. + * + * @param To_vector The Vector to store the imaginary part. + * @param From_vector The complex vector from which the imaginary part is + * extracted. + */ static void compute(Vector &To_vector, const Vector, N> &From_vector) { To_vector[0] = From_vector[0].imag; } }; +/** + * @brief Computes the imaginary part of a complex vector and stores it in a + * vector, array, or Vector. + * + * This function provides a convenient interface for extracting the imaginary + * part of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param To_vector The vector, array, or Vector to store the imaginary parts. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + */ template inline void compute(std::vector &To_vector, const std::vector> &From_vector) { Core::compute(To_vector, From_vector); } +/** + * @brief Computes the imaginary part of a complex vector and stores it in an + * array or Vector. + * + * This function provides a convenient interface for extracting the imaginary + * part of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param To_vector The array or Vector to store the imaginary parts. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + */ template inline void compute(std::array &To_vector, const std::array, N> &From_vector) { Core::compute(To_vector, From_vector); } +/** + * @brief Computes the imaginary part of a complex vector and stores it in a + * Vector. + * + * This function provides a convenient interface for extracting the imaginary + * part of a complex vector using the Core structure. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param To_vector The Vector to store the imaginary parts. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + */ template inline void compute(Vector &To_vector, const Vector, N> &From_vector) { @@ -896,6 +1827,20 @@ inline void compute(Vector &To_vector, #ifdef __BASE_MATRIX_USE_STD_VECTOR__ +/** + * @brief Extracts the real part from a complex vector and stores it in a + * std::vector. + * + * This function extracts the real part of each element of a complex vector, + * using either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param From_vector The complex vector from which the real parts are + * extracted. + * @return A new std::vector containing the real parts of the complex vector. + */ template inline std::vector get_real_vector_from_complex_vector( const std::vector> &From_vector) { @@ -917,6 +1862,21 @@ inline std::vector get_real_vector_from_complex_vector( return To_vector; } +/** + * @brief Extracts the imaginary part from a complex vector and stores it in a + * std::vector. + * + * This function extracts the imaginary part of each element of a complex + * vector, using either a for loop or a recursive template structure, depending + * on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + * @return A new std::vector containing the imaginary parts of the complex + * vector. + */ template inline std::vector get_imag_vector_from_complex_vector( const std::vector> &From_vector) { @@ -940,6 +1900,20 @@ inline std::vector get_imag_vector_from_complex_vector( #else // __BASE_MATRIX_USE_STD_VECTOR__ +/** + * @brief Extracts the real part from a complex vector and stores it in an + * std::array. + * + * This function extracts the real part of each element of a complex vector, + * using either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param From_vector The complex vector from which the real parts are + * extracted. + * @return A new std::array containing the real parts of the complex vector. + */ template inline std::array get_real_vector_from_complex_vector( const std::array, N> &From_vector) { @@ -961,6 +1935,21 @@ inline std::array get_real_vector_from_complex_vector( return To_vector; } +/** + * @brief Extracts the imaginary part from a complex vector and stores it in an + * std::array. + * + * This function extracts the imaginary part of each element of a complex + * vector, using either a for loop or a recursive template structure, depending + * on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + * @return A new std::array containing the imaginary parts of the complex + * vector. + */ template inline std::array get_imag_vector_from_complex_vector( const std::array, N> &From_vector) { @@ -984,6 +1973,20 @@ inline std::array get_imag_vector_from_complex_vector( #endif // __BASE_MATRIX_USE_STD_VECTOR__ +/** + * @brief Extracts the real part from a complex vector and stores it in a + * Vector. + * + * This function extracts the real part of each element of a complex vector, + * using either a for loop or a recursive template structure, depending on the + * compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param From_vector The complex vector from which the real parts are + * extracted. + * @return A new Vector containing the real parts of the complex vector. + */ template inline Vector get_real_vector_from_complex_vector(const Vector, N> &From_vector) { @@ -1005,6 +2008,20 @@ get_real_vector_from_complex_vector(const Vector, N> &From_vector) { return To_vector; } +/** + * @brief Extracts the imaginary part from a complex vector and stores it in a + * Vector. + * + * This function extracts the imaginary part of each element of a complex + * vector, using either a for loop or a recursive template structure, depending + * on the compilation flags. + * + * @tparam T The type of the vector elements. + * @tparam N The size of the vector. + * @param From_vector The complex vector from which the imaginary parts are + * extracted. + * @return A new Vector containing the imaginary parts of the complex vector. + */ template inline Vector get_imag_vector_from_complex_vector(const Vector, N> &From_vector) { From acf539f19393843baee599e908876ee2c42f3a83 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 07:59:07 +0900 Subject: [PATCH 19/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../python_numpy_base_simplification.hpp | 295 +++++++ .../python_numpy_base_simplified_action.hpp | 746 ++++++++++++++++++ 2 files changed, 1041 insertions(+) diff --git a/python_numpy/python_numpy_base_simplification.hpp b/python_numpy/python_numpy_base_simplification.hpp index 648ad2f..e8a0b70 100644 --- a/python_numpy/python_numpy_base_simplification.hpp +++ b/python_numpy/python_numpy_base_simplification.hpp @@ -1,3 +1,24 @@ +/** + * @file python_numpy_base_simplification.hpp + * @brief Provides a set of utility functions and type aliases for creating and + * manipulating dense, diagonal, and sparse matrices in a style similar to + * NumPy, using C++ templates. + * + * This header defines the `PythonNumpy` namespace, which contains a collection + * of template functions and type aliases to facilitate the creation and + * initialization of matrices with various storage types (dense, diagonal, + * sparse). The utilities support zero, one, full, and custom value + * initialization, as well as assignment of values to matrix elements in a + * variadic, type-safe manner. The design is inspired by Python's NumPy library, + * aiming to provide a familiar and expressive interface for matrix operations + * in C++. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_BASE_SIMPLIFICATION_HPP__ #define __PYTHON_NUMPY_BASE_SIMPLIFICATION_HPP__ @@ -13,6 +34,19 @@ namespace PythonNumpy { +/** + * @brief Creates a dense matrix of zeros. + * + * This function template constructs and returns a dense matrix of the specified + * type and dimensions, initialized with zeros. The matrix type is determined by + * the template parameters. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @return Matrix A dense matrix of zeros with dimensions M x + * N. + */ template inline auto make_DenseMatrixZeros(void) -> Matrix { @@ -20,18 +54,57 @@ inline auto make_DenseMatrixZeros(void) -> Matrix { return result; } +/** + * @brief Creates a dense matrix of ones. + * + * This function template constructs and returns a dense matrix of the specified + * type and dimensions, initialized with ones. The matrix type is determined by + * the template parameters. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @return Matrix A dense matrix of ones with dimensions M x + * N. + */ template inline auto make_DenseMatrixOnes(void) -> Matrix { return Matrix::ones(); } +/** + * @brief Creates a dense matrix filled with a specified value. + * + * This function template constructs and returns a dense matrix of the specified + * type and dimensions, initialized with a given value. The matrix type is + * determined by the template parameters. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam T The data type of the matrix elements. + * @param value The value to fill the matrix with. + * @return Matrix A dense matrix filled with the specified + * value, with dimensions M x N. + */ template inline auto make_DenseMatrixFull(const T &value) -> Matrix { return Matrix::full(value); } +/** + * @brief Creates a diagonal matrix of zeros. + * + * This function template constructs and returns a diagonal matrix of the + * specified type and dimensions, initialized with zeros. The matrix type is + * determined by the template parameters. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @return Matrix A diagonal matrix of zeros with dimensions M x + * M. + */ template inline auto make_DiagMatrixZeros(void) -> Matrix { @@ -39,18 +112,56 @@ inline auto make_DiagMatrixZeros(void) -> Matrix { return result; } +/** + * @brief Creates a diagonal matrix of ones. + * + * This function template constructs and returns a diagonal matrix of the + * specified type and dimensions, initialized with ones. The matrix type is + * determined by the template parameters. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @return Matrix A diagonal matrix of ones with dimensions M x + * M. + */ template inline auto make_DiagMatrixIdentity(void) -> Matrix { return Matrix::identity(); } +/** + * @brief Creates a diagonal matrix filled with a specified value. + * + * This function template constructs and returns a diagonal matrix of the + * specified type and dimensions, initialized with a given value. The matrix + * type is determined by the template parameters. + * + * @tparam M The number of columns in the matrix. + * @tparam T The data type of the matrix elements. + * @param value The value to fill the diagonal of the matrix with. + * @return Matrix A diagonal matrix filled with the specified + * value, with dimensions M x M. + */ template inline auto make_DiagMatrixFull(const T &value) -> Matrix { return Matrix::full(value); } +/** + * @brief Creates an empty sparse matrix. + * + * This function template constructs and returns an empty sparse matrix of the + * specified type and dimensions. The matrix type is determined by the template + * parameters. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @return Matrix> An empty + * sparse matrix with dimensions M x N. + */ template inline auto make_SparseMatrixEmpty(void) -> Matrix> { @@ -60,6 +171,19 @@ inline auto make_SparseMatrixEmpty(void) namespace MakeDenseMatrixOperation { +/** + * @brief Assigns values to a dense matrix. + * + * This function template assigns values to a dense matrix at specified + * indices. It supports variadic arguments for multiple values. + * + * @tparam ColumnCount The current column index for assignment. + * @tparam RowCount The current row index for assignment. + * @tparam DenseMatrix_Type The type of the dense matrix. + * @tparam T The type of the first value to assign. + * @param matrix The dense matrix to which values are assigned. + * @param value_1 The first value to assign. + */ template inline void assign_values(DenseMatrix_Type &matrix, T value_1) { @@ -74,6 +198,23 @@ inline void assign_values(DenseMatrix_Type &matrix, T value_1) { matrix.template set(value_1); } +/** + * @brief Assigns multiple values to a dense matrix. + * + * This function template assigns multiple values to a dense matrix at + * specified indices. It supports variadic arguments for multiple values and + * ensures that all values are of the same type. + * + * @tparam ColumnCount The current column index for assignment. + * @tparam RowCount The current row index for assignment. + * @tparam DenseMatrix_Type The type of the dense matrix. + * @tparam T The type of the first value to assign. + * @tparam U The type of the second value to assign. + * @param matrix The dense matrix to which values are assigned. + * @param value_1 The first value to assign. + * @param value_2 The second value to assign. + * @param args Additional values to assign, if any. + */ template inline void assign_values(DenseMatrix_Type &matrix, T value_1, U value_2, @@ -96,6 +237,21 @@ inline void assign_values(DenseMatrix_Type &matrix, T value_1, U value_2, } // namespace MakeDenseMatrixOperation +/** + * @brief Creates a dense matrix with specified values. + * + * This function template constructs and returns a dense matrix of the specified + * type and dimensions, initialized with given values. The matrix type is + * determined by the template parameters. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam T The data type of the matrix elements. + * @param value_1 The first value to fill the matrix with. + * @param args Additional values to fill the matrix with, if any. + * @return Matrix A dense matrix filled with the specified + * values, with dimensions M x N. + */ template inline auto make_DenseMatrix(T value_1, Args... args) -> Matrix { @@ -109,6 +265,18 @@ inline auto make_DenseMatrix(T value_1, Args... args) namespace MakeDiagMatrixOperation { +/** + * @brief Assigns values to a diagonal matrix. + * + * This function template assigns values to a diagonal matrix at specified + * indices. It supports variadic arguments for multiple values. + * + * @tparam IndexCount The current index for assignment. + * @tparam DiagMatrix_Type The type of the diagonal matrix. + * @tparam T The type of the first value to assign. + * @param matrix The diagonal matrix to which values are assigned. + * @param value_1 The first value to assign. + */ template inline void assign_values(DiagMatrix_Type &matrix, T value_1) { @@ -118,6 +286,22 @@ inline void assign_values(DiagMatrix_Type &matrix, T value_1) { matrix.template set(value_1); } +/** + * @brief Assigns multiple values to a diagonal matrix. + * + * This function template assigns multiple values to a diagonal matrix at + * specified indices. It supports variadic arguments for multiple values and + * ensures that all values are of the same type. + * + * @tparam IndexCount The current index for assignment. + * @tparam DiagMatrix_Type The type of the diagonal matrix. + * @tparam T The type of the first value to assign. + * @tparam U The type of the second value to assign. + * @param matrix The diagonal matrix to which values are assigned. + * @param value_1 The first value to assign. + * @param value_2 The second value to assign. + * @param args Additional values to assign, if any. + */ template inline void assign_values(DiagMatrix_Type &matrix, T value_1, U value_2, @@ -134,6 +318,20 @@ inline void assign_values(DiagMatrix_Type &matrix, T value_1, U value_2, } // namespace MakeDiagMatrixOperation +/** + * @brief Creates a diagonal matrix with specified values. + * + * This function template constructs and returns a diagonal matrix of the + * specified type and dimensions, initialized with given values. The matrix type + * is determined by the template parameters. + * + * @tparam M The number of columns in the matrix. + * @tparam T The data type of the matrix elements. + * @param value_1 The first value to fill the diagonal with. + * @param args Additional values to fill the diagonal with, if any. + * @return Matrix A diagonal matrix filled with the specified + * values, with dimensions M x M. + */ template inline auto make_DiagMatrix(T value_1, Args... args) -> Matrix { @@ -144,6 +342,19 @@ inline auto make_DiagMatrix(T value_1, Args... args) -> Matrix { return result; } +/** + * @brief Creates a sparse matrix of zeros. + * + * This function template constructs and returns a sparse matrix of the + * specified type and dimensions, initialized with zeros. The matrix type is + * determined by the template parameters. + * + * @tparam T The data type of the matrix elements. + * @tparam SparseAvailable The sparse matrix availability type. + * @return Matrix A sparse matrix of zeros with + * dimensions defined by SparseAvailable. + */ template inline auto make_SparseMatrixZeros(void) -> Matrix A sparse matrix of ones with + * dimensions defined by SparseAvailable. + */ template inline auto make_SparseMatrixOnes(void) -> Matrix::full(static_cast(1)); } +/** + * @brief Creates a sparse matrix filled with a specified value. + * + * This function template constructs and returns a sparse matrix of the + * specified type and dimensions, initialized with a given value. The matrix + * type is determined by the template parameters. + * + * @tparam SparseAvailable The sparse matrix availability type. + * @tparam T The data type of the matrix elements. + * @param value The value to fill the sparse matrix with. + * @return Matrix A sparse matrix filled with + * the specified value, with dimensions defined by SparseAvailable. + */ template inline auto make_SparseMatrixFull(const T &value) -> Matrix inline void assign_values(SparseMatrix_Type &matrix, T value_1) { @@ -187,6 +437,22 @@ inline void assign_values(SparseMatrix_Type &matrix, T value_1) { matrix.template set(value_1); } +/** + * @brief Assigns multiple values to a sparse matrix. + * + * This function template assigns multiple values to a sparse matrix at + * specified indices. It supports variadic arguments for multiple values and + * ensures that all values are of the same type. + * + * @tparam IndexCount The current index for assignment. + * @tparam SparseMatrix_Type The type of the sparse matrix. + * @tparam T The type of the first value to assign. + * @tparam U The type of the second value to assign. + * @param matrix The sparse matrix to which values are assigned. + * @param value_1 The first value to assign. + * @param value_2 The second value to assign. + * @param args Additional values to assign, if any. + */ template inline void assign_values(SparseMatrix_Type &matrix, T value_1, U value_2, @@ -205,6 +471,21 @@ inline void assign_values(SparseMatrix_Type &matrix, T value_1, U value_2, } // namespace MakeSparseMatrixOperation +/** + * @brief Creates a sparse matrix with specified values. + * + * This function template constructs and returns a sparse matrix of the + * specified type and dimensions, initialized with given values. The matrix type + * is determined by the template parameters. + * + * @tparam SparseAvailable The sparse matrix availability type. + * @tparam T The data type of the matrix elements. + * @param value_1 The first value to fill the sparse matrix with. + * @param args Additional values to fill the sparse matrix with, if any. + * @return Matrix A sparse matrix filled with + * the specified values, with dimensions defined by SparseAvailable. + */ template inline auto make_SparseMatrix(T value_1, Args... args) -> Matrix> A sparse matrix + * created from the dense matrix, with dimensions M x N. + */ template inline auto make_SparseMatrixFromDenseMatrix(Matrix &dense_matrix) diff --git a/python_numpy/python_numpy_base_simplified_action.hpp b/python_numpy/python_numpy_base_simplified_action.hpp index 3b86759..9b9d9ab 100644 --- a/python_numpy/python_numpy_base_simplified_action.hpp +++ b/python_numpy/python_numpy_base_simplified_action.hpp @@ -1,3 +1,23 @@ +/** + * @file python_numpy_base_simplified_action.hpp + * @brief Provides a set of template-based utilities for matrix manipulation, + * inspired by Python's NumPy, for C++ static matrices. + * + * This header defines the PythonNumpy namespace, which contains a comprehensive + * suite of template meta-programming utilities for manipulating matrices at + * compile time. The utilities support dense, diagonal, and sparse matrices, and + * provide operations such as row extraction, row setting, matrix substitution, + * block concatenation, tiling, and reshaping. The design is highly generic and + * leverages C++ templates for static size and type safety, enabling efficient + * and flexible matrix operations similar to those found in Python's NumPy + * library, but at compile time. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_BASE_SIMPLIFIED_ACTION_HPP__ #define __PYTHON_NUMPY_BASE_SIMPLIFIED_ACTION_HPP__ @@ -20,6 +40,24 @@ namespace GetDenseMatrixOperation { template struct GetRow_Loop { + /** + * @brief Computes a specific operation on the input matrix and stores the + * result in the provided result vector. + * + * This static function extracts a particular column (indexed by COL_Index) + * from the input matrix and assigns it to the result vector. It then + * recursively processes the remaining columns by invoking GetRow_Loop with a + * decremented column index. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam ROW The row index to operate on. + * @tparam COL_Index The current column index being processed. + * @param matrix The input dense matrix of type DenseMatrix_Type. + * @param result The output dense matrix (vector) of type + * DenseMatrix_Type to store the computed result. + */ static void compute(const DenseMatrix_Type &matrix, DenseMatrix_Type &result) { @@ -30,6 +68,22 @@ struct GetRow_Loop { template struct GetRow_Loop { + /** + * @brief Computes a specific operation on the input matrix and stores the + * result in the provided result vector. + * + * This static function extracts the first column (index 0) from the input + * matrix and assigns it to the result vector. It serves as the base case for + * the recursive processing of columns. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam ROW The row index to operate on. + * @param matrix The input dense matrix of type DenseMatrix_Type. + * @param result The output dense matrix (vector) of type + * DenseMatrix_Type to store the computed result. + */ static void compute(const DenseMatrix_Type &matrix, DenseMatrix_Type &result) { @@ -42,6 +96,20 @@ using GetRow = GetRow_Loop; } // namespace GetDenseMatrixOperation +/** + * @brief Extracts a specific row from a dense matrix. + * + * This function template retrieves a specified row from a dense matrix and + * returns it as a new dense matrix (vector) of size M x 1. + * + * @tparam ROW The index of the row to extract. + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param matrix The input dense matrix from which to extract the row. + * @return DenseMatrix_Type A dense matrix (vector) containing the + * extracted row. + */ template inline auto get_row(const DenseMatrix_Type &matrix) -> DenseMatrix_Type { @@ -55,14 +123,53 @@ inline auto get_row(const DenseMatrix_Type &matrix) namespace GetDiagMatrixOperation { +/** + * @brief Retrieves a specific row from a diagonal matrix. + * + * This function template extracts a specified row from a diagonal matrix and + * returns it as a new sparse matrix (vector) of size M x 1. + * + * @tparam M The number of columns in the diagonal matrix. + * @tparam Index The index of the row to extract. + * @return SparseAvailableGetRow, Index> A sparse matrix + * (vector) containing the extracted row. + */ template using DiagAvailableRow = SparseAvailableGetRow, Index>; +/** + * @brief Defines the type of a sparse matrix that can hold a specific row from + * a diagonal matrix. + * + * This type alias creates a sparse matrix type that can hold a specific row + * from a diagonal matrix, indexed by Index. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the diagonal matrix. + * @tparam Index The index of the row to extract. + */ template using DiagAvailableRow_Type = SparseMatrix_Type>; template struct GetRow_Loop { + /** + * @brief Computes a specific operation on the input diagonal matrix and + * stores the result in the provided result vector. + * + * This static function extracts a particular column (indexed by COL_Index) + * from the input diagonal matrix and assigns it to the result vector. It then + * recursively processes the remaining columns by invoking GetRow_Loop with a + * decremented column index. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam ROW The row index to operate on. + * @tparam COL_Index The current column index being processed. + * @param matrix The input diagonal matrix of type DiagMatrix_Type. + * @param result The output diagonal matrix (vector) of type + * DiagAvailableRow_Type to store the computed result. + */ static void compute(const DiagMatrix_Type &matrix, DiagAvailableRow_Type &result) { @@ -73,6 +180,21 @@ struct GetRow_Loop { template struct GetRow_Loop { + /** + * @brief Computes a specific operation on the input diagonal matrix and + * stores the result in the provided result vector. + * + * This static function extracts the first column (index 0) from the input + * diagonal matrix and assigns it to the result vector. It serves as the base + * case for the recursive processing of columns. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam ROW The row index to operate on. + * @param matrix The input diagonal matrix of type DiagMatrix_Type. + * @param result The output diagonal matrix (vector) of type + * DiagAvailableRow_Type to store the computed result. + */ static void compute(const DiagMatrix_Type &matrix, DiagAvailableRow_Type &result) { @@ -80,11 +202,36 @@ struct GetRow_Loop { } }; +/** + * @brief Retrieves a specific row from a diagonal matrix. + * + * This function template extracts a specified row from a diagonal matrix and + * returns it as a new sparse matrix (vector) of size M x 1. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the diagonal matrix. + * @tparam ROW The index of the row to extract. + * @return DiagAvailableRow_Type A sparse matrix (vector) containing + * the extracted row. + */ template using GetRow = GetRow_Loop; } // namespace GetDiagMatrixOperation +/** + * @brief Extracts a specific row from a diagonal matrix. + * + * This function template retrieves a specified row from a diagonal matrix and + * returns it as a new sparse matrix (vector) of size M x 1. + * + * @tparam ROW The index of the row to extract. + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the diagonal matrix. + * @param matrix The input diagonal matrix from which to extract the row. + * @return GetDiagMatrixOperation::DiagAvailableRow_Type A sparse + * matrix (vector) containing the extracted row. + */ template inline auto get_row(const DiagMatrix_Type &matrix) -> GetDiagMatrixOperation::DiagAvailableRow_Type { @@ -98,9 +245,32 @@ inline auto get_row(const DiagMatrix_Type &matrix) namespace GetSparseMatrixOperation { +/** + * @brief Retrieves a specific row from a sparse matrix. + * + * This function template extracts a specified row from a sparse matrix and + * returns it as a new sparse matrix (vector) of size M x 1. + * + * @tparam M The number of columns in the sparse matrix. + * @tparam Index The index of the row to extract. + * @return SparseAvailableGetRow A sparse matrix + * (vector) containing the extracted row. + */ template using SparseAvailableRow = SparseAvailableGetRow; +/** + * @brief Defines the type of a sparse matrix that can hold a specific row from + * a sparse matrix. + * + * This type alias creates a sparse matrix type that can hold a specific row + * from a sparse matrix, indexed by Index. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix. + * @tparam Index The index of the row to extract. + * @tparam SparseAvailable The sparse matrix availability type. + */ template using SparseRow_Type = @@ -109,6 +279,26 @@ using SparseRow_Type = template struct GetRow_Loop { + /** + * @brief Computes a specific operation on the input sparse matrix and stores + * the result in the provided result vector. + * + * This static function extracts a particular column (indexed by COL_Index) + * from the input sparse matrix and assigns it to the result vector. It then + * recursively processes the remaining columns by invoking GetRow_Loop with a + * decremented column index. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam SparseAvailable The sparse matrix availability type. + * @tparam ROW The row index to operate on. + * @tparam COL_Index The current column index being processed. + * @param matrix The input sparse matrix of type Matrix. + * @param result The output sparse matrix (vector) of type + * SparseRow_Type to store the computed result. + */ static void compute(const Matrix &matrix, SparseRow_Type &result) { @@ -121,6 +311,24 @@ struct GetRow_Loop { template struct GetRow_Loop { + /** + * @brief Computes a specific operation on the input sparse matrix and stores + * the result in the provided result vector. + * + * This static function extracts the first column (index 0) from the input + * sparse matrix and assigns it to the result vector. It serves as the base + * case for the recursive processing of columns. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam SparseAvailable The sparse matrix availability type. + * @tparam ROW The row index to operate on. + * @param matrix The input sparse matrix of type Matrix. + * @param result The output sparse matrix (vector) of type + * SparseRow_Type to store the computed result. + */ static void compute(const Matrix &matrix, SparseRow_Type &result) { @@ -128,12 +336,41 @@ struct GetRow_Loop { } }; +/** + * @brief Retrieves a specific row from a sparse matrix. + * + * This function template extracts a specified row from a sparse matrix and + * returns it as a new sparse matrix (vector) of size M x 1. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix. + * @tparam SparseAvailable The sparse matrix availability type. + * @tparam ROW The index of the row to extract. + * @return SparseRow_Type A sparse matrix (vector) + * containing the extracted row. + */ template using GetRow = GetRow_Loop; } // namespace GetSparseMatrixOperation +/** + * @brief Extracts a specific row from a sparse matrix. + * + * This function template retrieves a specified row from a sparse matrix and + * returns it as a new sparse matrix (vector) of size M x 1. + * + * @tparam ROW The index of the row to extract. + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix. + * @tparam SparseAvailable The sparse matrix availability type. + * @param matrix The input sparse matrix from which to extract the row. + * @return GetSparseMatrixOperation::SparseRow_Type A sparse matrix (vector) containing the extracted row. + */ template inline auto get_row(const Matrix &matrix) @@ -153,6 +390,22 @@ namespace SetMatrixOperation { template struct SetRow_Loop { + /** + * @brief Computes a specific operation on the input matrix and sets the + * corresponding row vector values. + * + * This static function assigns a particular column (indexed by COL_Index) of + * the row vector to the specified row of the matrix. It then recursively + * processes the remaining columns by invoking SetRow_Loop with a decremented + * column index. + * + * @tparam Matrix_Type The type of the matrix to set values in. + * @tparam RowVector_Type The type of the row vector containing values to set. + * @tparam ROW The row index to operate on. + * @tparam COL_Index The current column index being processed. + * @param matrix The input matrix of type Matrix_Type. + * @param row_vector The row vector containing values to set in the matrix. + */ static void compute(Matrix_Type &matrix, const RowVector_Type &row_vector) { matrix.template set( @@ -164,18 +417,55 @@ struct SetRow_Loop { template struct SetRow_Loop { + /** + * @brief Computes a specific operation on the input matrix and sets the + * corresponding row vector values. + * + * This static function assigns the first column (index 0) of the row vector + * to the specified row of the matrix. It serves as the base case for the + * recursive processing of columns. + * + * @tparam Matrix_Type The type of the matrix to set values in. + * @tparam RowVector_Type The type of the row vector containing values to set. + * @tparam ROW The row index to operate on. + * @param matrix The input matrix of type Matrix_Type. + * @param row_vector The row vector containing values to set in the matrix. + */ static void compute(Matrix_Type &matrix, const RowVector_Type &row_vector) { matrix.template set<0, ROW>(row_vector.template get<0, 0>()); } }; +/** + * @brief Sets a specific row in a matrix with values from a row vector. + * + * This type alias defines a loop structure that sets the specified row of the + * matrix with values from the provided row vector. It processes all columns + * of the row vector. + * + * @tparam Matrix_Type The type of the matrix to set values in. + * @tparam RowVector_Type The type of the row vector containing values to set. + * @tparam ROW The row index to operate on. + */ template using SetRow = SetRow_Loop; } // namespace SetMatrixOperation +/** + * @brief Sets a specific row in a matrix with values from a row vector. + * + * This function template assigns the values from a row vector to a specified + * row in the matrix. It processes all columns of the row vector. + * + * @tparam ROW The index of the row to set in the matrix. + * @tparam Matrix_Type The type of the matrix to set values in. + * @tparam RowVector_Type The type of the row vector containing values to set. + * @param matrix The input matrix where the row will be set. + * @param row_vector The row vector containing values to set in the matrix. + */ template inline void set_row(Matrix_Type &matrix, const RowVector_Type &row_vector) { @@ -191,6 +481,26 @@ template struct SubstituteColumn { + /** + * @brief Computes a specific operation on the input part matrix and + * substitutes its values into the corresponding position in the All matrix. + * + * This static function assigns a particular value from the part matrix + * (indexed by I and J_idx) to the All matrix at the position (Col_Offset + I, + * Row_Offset + J_idx). It then recursively processes the remaining columns by + * invoking SubstituteColumn with a decremented column index. + * + * @tparam Col_Offset The column offset for substitution. + * @tparam Row_Offset The row offset for substitution. + * @tparam All_Type The type of the All matrix. + * @tparam Part_Type The type of the part matrix. + * @tparam M The number of columns in the part matrix. + * @tparam N The number of rows in the part matrix. + * @tparam I The current row index being processed. + * @tparam J_idx The current column index being processed. + * @param All The All matrix where values are substituted. + * @param Part The part matrix containing values to substitute. + */ static void compute(All_Type &All, const Part_Type &Part) { All.template set<(Col_Offset + I), (Row_Offset + J_idx)>( @@ -206,6 +516,23 @@ template struct SubstituteColumn { + /** + * @brief Computes a specific operation on the input part matrix and + * substitutes its values into the corresponding position in the All matrix. + * + * This static function assigns the first column (index 0) from the part + * matrix to the All matrix at the position (Col_Offset + I, Row_Offset). + * + * @tparam Col_Offset The column offset for substitution. + * @tparam Row_Offset The row offset for substitution. + * @tparam All_Type The type of the All matrix. + * @tparam Part_Type The type of the part matrix. + * @tparam M The number of columns in the part matrix. + * @tparam N The number of rows in the part matrix. + * @tparam I The current row index being processed. + * @param All The All matrix where values are substituted. + * @param Part The part matrix containing values to substitute. + */ static void compute(All_Type &All, const Part_Type &Part) { All.template set<(Col_Offset + I), Row_Offset>(Part.template get()); @@ -216,6 +543,25 @@ struct SubstituteColumn struct SubstituteRow { + /** + * @brief Computes a specific operation on the input part matrix and + * substitutes its values into the corresponding position in the All matrix. + * + * This static function assigns a particular value from the part matrix + * (indexed by I_idx and J_idx) to the All matrix at the position (Col_Offset, + * Row_Offset + I_idx). It then recursively processes the remaining rows by + * invoking SubstituteRow with a decremented row index. + * + * @tparam Col_Offset The column offset for substitution. + * @tparam Row_Offset The row offset for substitution. + * @tparam All_Type The type of the All matrix. + * @tparam Part_Type The type of the part matrix. + * @tparam M The number of columns in the part matrix. + * @tparam N The number of rows in the part matrix. + * @tparam I_idx The current row index being processed. + * @param All The All matrix where values are substituted. + * @param Part The part matrix containing values to substitute. + */ static void compute(All_Type &All, const Part_Type &Part) { SubstituteColumn struct SubstituteRow { + /** + * @brief Computes a specific operation on the input part matrix and + * substitutes its values into the corresponding position in the All matrix. + * + * This static function assigns the first row (index 0) from the part matrix + * to the All matrix at the position (Col_Offset, Row_Offset). + * + * @tparam Col_Offset The column offset for substitution. + * @tparam Row_Offset The row offset for substitution. + * @tparam All_Type The type of the All matrix. + * @tparam Part_Type The type of the part matrix. + * @tparam M The number of columns in the part matrix. + * @tparam N The number of rows in the part matrix. + * @param All The All matrix where values are substituted. + * @param Part The part matrix containing values to substitute. + */ static void compute(All_Type &All, const Part_Type &Part) { SubstituteColumn { } }; +/** + * @brief Substitutes a part matrix into a larger matrix at specified offsets. + * + * This function template substitutes the values from a part matrix into a + * larger matrix (All) at specified column and row offsets. It ensures that the + * All matrix has enough space to accommodate the part matrix. + * + * @tparam Col_Offset The column offset for substitution. + * @tparam Row_Offset The row offset for substitution. + * @tparam All_Type The type of the All matrix. + * @tparam Part_Type The type of the part matrix. + * @param All The All matrix where values are substituted. + * @param Part The part matrix containing values to substitute. + */ template inline void substitute_each(All_Type &All, const Part_Type &Part) { @@ -256,6 +632,25 @@ template struct TupleColumn { + /** + * @brief Substitutes a specific column of a tuple into the All matrix. + * + * This static function substitutes the values from a specific column of the + * tuple (indexed by THIS_TUPLE_INDEX) into the All matrix at the specified + * offsets. It then recursively processes the remaining rows by invoking + * substitute with a decremented row index. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam All_Type The type of the All matrix. + * @tparam ArgsTuple_Type The type of the tuple containing arguments. + * @tparam TupleCol_Count The number of columns in the tuple. + * @tparam TupleCol_Offset The column offset for substitution. + * @tparam TupleRow_Offset The row offset for substitution. + * @tparam TupleRow_Index The current row index being processed. + * @param All The All matrix where values are substituted. + * @param args The tuple containing arguments to substitute. + */ static void substitute(All_Type &All, const ArgsTuple_Type &args) { constexpr std::size_t THIS_TUPLE_INDEX = @@ -280,6 +675,23 @@ template struct TupleColumn { + /** + * @brief Substitutes a specific column of a tuple into the All matrix. + * + * This static function does nothing when the row index is 0, serving as the + * base case for the recursive processing of rows. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam All_Type The type of the All matrix. + * @tparam ArgsTuple_Type The type of the tuple containing arguments. + * @tparam TupleCol_Count The number of columns in the tuple. + * @tparam TupleCol_Offset The column offset for substitution. + * @tparam TupleRow_Offset The row offset for substitution. + * @param All The All matrix where values would be substituted (not used + * here). + * @param args The tuple containing arguments to substitute (not used here). + */ static void substitute(All_Type &All, const ArgsTuple_Type &args) { // Do Nothing static_cast(All); @@ -291,6 +703,23 @@ template struct TupleRow { + /** + * @brief Substitutes a specific row of a tuple into the All matrix. + * + * This static function substitutes the values from a specific row of the + * tuple (indexed by THIS_TUPLE_INDEX) into the All matrix at the specified + * offsets. It then recursively processes the remaining columns by invoking + * substitute with a decremented column index. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam All_Type The type of the All matrix. + * @tparam ArgsTuple_Type The type of the tuple containing arguments. + * @tparam TupleCol_Offset The column offset for substitution. + * @tparam TupleCol_Index The current column index being processed. + * @param All The All matrix where values are substituted. + * @param args The tuple containing arguments to substitute. + */ static void substitute(All_Type &All, const ArgsTuple_Type &args) { constexpr std::size_t TUPLECOL_COUNT = M - TupleCol_Index; @@ -314,6 +743,21 @@ struct TupleRow { template struct TupleRow { + /** + * @brief Substitutes a specific row of a tuple into the All matrix. + * + * This static function does nothing when the column index is 0, serving as + * the base case for the recursive processing of columns. + * + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @tparam All_Type The type of the All matrix. + * @tparam ArgsTuple_Type The type of the tuple containing arguments. + * @tparam TupleCol_Offset The column offset for substitution. + * @param All The All matrix where values would be substituted (not used + * here). + * @param args The tuple containing arguments to substitute (not used here). + */ static void substitute(All_Type &All, const ArgsTuple_Type &args) { // Do Nothing static_cast(All); @@ -324,6 +768,20 @@ struct TupleRow { } // namespace PartMatrixOperation /* Substitute same size Matrix */ + +/** + * @brief Substitutes a source matrix into a destination matrix of the same + * size. + * + * This function template substitutes the values from a source matrix into a + * destination matrix, ensuring that both matrices have the same number of + * elements. + * + * @tparam From_Type The type of the source matrix. + * @tparam To_Type The type of the destination matrix. + * @param to_matrix The destination matrix where values are substituted. + * @param from_matrix The source matrix containing values to substitute. + */ template inline void substitute_matrix(To_Type &to_matrix, const From_Type &from_matrix) { @@ -337,6 +795,21 @@ inline void substitute_matrix(To_Type &to_matrix, } /* Substitute small size Matrix to large size Matrix */ + +/** + * @brief Substitutes a small matrix into a larger matrix at specified offsets. + * + * This function template substitutes the values from a small matrix into a + * larger matrix at specified column and row offsets. It ensures that the large + * matrix has enough space to accommodate the small matrix. + * + * @tparam Col_Offset The column offset for substitution. + * @tparam Row_Offset The row offset for substitution. + * @tparam Large_Type The type of the large matrix. + * @tparam Small_Type The type of the small matrix. + * @param Large The large matrix where values are substituted. + * @param Small The small matrix containing values to substitute. + */ template inline void substitute_part_matrix(Large_Type &Large, const Small_Type &Small) { @@ -407,9 +880,34 @@ struct ConcatenateArgsType { std::make_tuple(std::declval()...)))>::type; }; +/** + * @brief Type alias for the concatenated arguments type. + * + * This type alias defines the type of the concatenated arguments based on the + * specified M, N, Tuple, and Args. + * + * @tparam M The number of columns in the block. + * @tparam N The number of rows in the block. + * @tparam Tuple The tuple containing previous arguments. + * @tparam Args The additional arguments to concatenate. + */ template using ArgsType_t = typename ConcatenateArgsType::type; +/** + * @brief Concatenates arguments into a block matrix. + * + * This function template concatenates the provided arguments into a block + * matrix of size M x N, ensuring that the total number of arguments matches M + * * N. + * + * @tparam M The number of columns in the block. + * @tparam N The number of rows in the block. + * @tparam Concatenate_Type The type of the concatenated matrix. + * @tparam Tuple The tuple containing previous arguments. + * @param Concatenated The concatenated matrix to update. + * @param previousArgs The tuple containing previously concatenated arguments. + */ template inline void concatenate_args(Concatenate_Type &Concatenated, @@ -431,6 +929,20 @@ inline void concatenate_args(Concatenate_Type &Concatenated, 0, M>::substitute(Concatenated, all_args); } +/** + * @brief Concatenates multiple arguments into a block matrix. + * + * This function template recursively concatenates the provided arguments into + * a block matrix of size M x N, ensuring that the total number of arguments + * matches M * N. + * + * @tparam M The number of columns in the block. + * @tparam N The number of rows in the block. + * @tparam Concatenate_Type The type of the concatenated matrix. + * @tparam Tuple The tuple containing previous arguments. + * @tparam First The first argument to concatenate. + * @tparam Rest The remaining arguments to concatenate. + */ template inline void concatenate_args(Concatenate_Type &Concatenated, @@ -442,6 +954,18 @@ inline void concatenate_args(Concatenate_Type &Concatenated, return concatenate_args(Concatenated, updatedArgs, rest...); } +/** + * @brief Calculates the concatenated block matrix from the provided arguments. + * + * This function template initializes the concatenated block matrix and + * recursively concatenates the provided arguments into it. + * + * @tparam M The number of columns in the block. + * @tparam N The number of rows in the block. + * @tparam Args The types of the arguments to concatenate. + * @param Concatenated The concatenated matrix to update. + * @param args The arguments to concatenate into the block matrix. + */ template inline void calculate(ArgsType_t, Args...> &Concatenated, Args... args) { @@ -453,6 +977,20 @@ inline void calculate(ArgsType_t, Args...> &Concatenated, } // namespace ConcatenateBlockOperation +/** + * @brief Updates a concatenated block matrix with new arguments. + * + * This function template updates the provided concatenated block matrix with + * the specified arguments, ensuring that the total number of arguments matches + * M * N. + * + * @tparam M The number of columns in the block. + * @tparam N The number of rows in the block. + * @tparam Concatenate_Type The type of the concatenated matrix. + * @tparam Args The types of the arguments to concatenate. + * @param Concatenated The concatenated matrix to update. + * @param args The arguments to concatenate into the block matrix. + */ template inline void update_block_concatenated_matrix(Concatenate_Type &Concatenated, @@ -464,6 +1002,19 @@ inline void update_block_concatenated_matrix(Concatenate_Type &Concatenated, ConcatenateBlockOperation::calculate(Concatenated, args...); } +/** + * @brief Concatenates multiple arguments into a block matrix. + * + * This function template concatenates the provided arguments into a block + * matrix of size M x N, ensuring that the total number of arguments matches M + * * N. + * + * @tparam M The number of columns in the block. + * @tparam N The number of rows in the block. + * @tparam Args The types of the arguments to concatenate. + * @return ConcatenateBlockOperation::ArgsType_t, Args...> + * A concatenated block matrix containing the provided arguments. + */ template inline auto concatenate_block(Args... args) -> ConcatenateBlockOperation::ArgsType_t, Args...> { @@ -498,38 +1049,116 @@ struct GenerateTileTypes { } // namespace TileOperation +/** + * @brief Type alias for a tile type with specified dimensions and matrix type. + * + * This type alias defines a tile type based on the specified M, N, and + * MATRIX_Type. It uses the TileOperation::GenerateTileTypes to generate the + * appropriate tile type. + * + * @tparam M The number of columns in the tile. + * @tparam N The number of rows in the tile. + * @tparam MATRIX_Type The type of the matrix used in the tile. + */ template using Tile_Type = typename TileOperation::GenerateTileTypes::type; namespace TileOperation { +/** + * @brief Index sequence for tile operations. + * + * This struct defines an index sequence for tile operations, allowing for + * compile-time generation of indices for tile elements. + * + * @tparam Indices The indices for the tile elements. + */ template struct index_sequence_for_tile {}; +/** + * @brief Helper struct to generate an index sequence for tile operations. + * + * This struct recursively generates an index sequence for tile operations, + * allowing for compile-time generation of indices based on the specified size + * N. + * + * @tparam N The size of the index sequence to generate. + * @tparam Indices The indices for the tile elements (used in recursion). + */ template struct make_index_sequence_for_tile_impl : make_index_sequence_for_tile_impl {}; +/** + * @brief Specialization of make_index_sequence_for_tile_impl for the base case. + * + * This specialization defines the index sequence when N is 0, providing the + * final type for the index sequence. + * + * @tparam Indices The indices for the tile elements (used in recursion). + */ template struct make_index_sequence_for_tile_impl<0, Indices...> { using type = index_sequence_for_tile; }; +/** + * @brief Generates an index sequence for tile operations. + * + * This type alias generates an index sequence for tile operations based on the + * specified size N. It uses the make_index_sequence_for_tile_impl to create + * the appropriate index sequence type. + * + * @tparam N The size of the index sequence to generate. + */ template using make_index_sequence_for_tile = typename make_index_sequence_for_tile_impl::type; +/** + * @brief Repeats a matrix type a specified number of times. + * + * This struct recursively generates a type that represents a tuple of the + * specified matrix type repeated a given number of times. + * + * @tparam Count The number of times to repeat the matrix type. + * @tparam MATRIX_Type The type of the matrix to repeat. + * @tparam Args The types of the arguments to include in the tuple. + */ template struct RepeatMatrix { using type = typename RepeatMatrix::type; }; +/** + * @brief Specialization of RepeatMatrix for the base case. + * + * This specialization defines the type when Count is 0, providing a tuple of + * the specified argument types. + * + * @tparam MATRIX_Type The type of the matrix to repeat (not used here). + * @tparam Args The types of the arguments to include in the tuple. + */ template struct RepeatMatrix<0, MATRIX_Type, Args...> { using type = std::tuple; }; +/** + * @brief Updates a concatenated matrix with multiple matrices. + * + * This function template updates the provided concatenated matrix with the + * specified matrices, ensuring that the total number of matrices matches M * N. + * + * @tparam M The number of columns in the tile. + * @tparam N The number of rows in the tile. + * @tparam MATRIX_Type The type of the matrices to concatenate. + * @tparam Indices The indices for the tile elements (used in recursion). + * @param Concatenated The concatenated matrix to update. + * @param matrix The matrices to concatenate into the block matrix. + */ template inline void calculate(Tile_Type &Concatenated, @@ -542,6 +1171,18 @@ inline void calculate(Tile_Type &Concatenated, } // namespace TileOperation +/** + * @brief Updates a concatenated matrix with a single matrix. + * + * This function template updates the provided concatenated matrix with the + * specified matrix, ensuring that the total number of elements matches M * N. + * + * @tparam M The number of columns in the tile. + * @tparam N The number of rows in the tile. + * @tparam MATRIX_Type The type of the matrix to concatenate. + * @param Concatenated The concatenated matrix to update. + * @param matrix The matrix to concatenate into the block matrix. + */ template inline void update_tile_concatenated_matrix(Tile_Type &Concatenated, @@ -557,6 +1198,19 @@ update_tile_concatenated_matrix(Tile_Type &Concatenated, TileOperation::make_index_sequence_for_tile{}); } +/** + * @brief Concatenates a matrix into a tile of specified dimensions. + * + * This function template concatenates the provided matrix into a tile of size + * M x N, ensuring that the total number of elements matches M * N. + * + * @tparam M The number of columns in the tile. + * @tparam N The number of rows in the tile. + * @tparam MATRIX_Type The type of the matrix to concatenate. + * @param matrix The matrix to concatenate into the tile. + * @return Tile_Type A tile containing the concatenated + * matrix. + */ template inline auto concatenate_tile(const MATRIX_Type &matrix) -> Tile_Type { @@ -574,6 +1228,21 @@ namespace ReshapeOperation { template struct Column { + /** + * @brief Substitutes a specific column of the from_matrix into the to_matrix. + * + * This static function substitutes the values from a specific column of the + * from_matrix (indexed by I and J_idx) into the to_matrix at the + * corresponding position. It then recursively processes the remaining columns + * by invoking substitute with a decremented column index. + * + * @tparam To_Type The type of the destination matrix. + * @tparam From_Type The type of the source matrix. + * @tparam I The current row index being processed. + * @tparam J_idx The current column index being processed. + * @param to_matrix The destination matrix where values are substituted. + * @param from_matrix The source matrix containing values to substitute. + */ static void substitute(To_Type &to_matrix, const From_Type &from_matrix) { constexpr std::size_t NUMBER_OF_ELEMENT = I * From_Type::ROWS + J_idx; @@ -609,6 +1278,17 @@ struct Column { // column recursion termination template struct Column { + /** + * @brief Substitutes the first column of the from_matrix into the to_matrix. + * + * This static function substitutes the values from the first column (index 0) + * of the from_matrix into the to_matrix at the corresponding position. + * + * @tparam To_Type The type of the destination matrix. + * @tparam From_Type The type of the source matrix. + * @param to_matrix The destination matrix where values are substituted. + * @param from_matrix The source matrix containing values to substitute. + */ static void substitute(To_Type &to_matrix, const From_Type &from_matrix) { constexpr std::size_t NUMBER_OF_ELEMENT = I * From_Type::ROWS; @@ -643,6 +1323,22 @@ struct Column { template struct Row { + /** + * @brief Substitutes a specific row of the from_matrix into the to_matrix. + * + * This static function substitutes the values from a specific row of the + * from_matrix (indexed by I_idx) into the to_matrix at the corresponding + * position. It then recursively processes the remaining rows by invoking + * substitute with a decremented row index. + * + * @tparam To_Type The type of the destination matrix. + * @tparam From_Type The type of the source matrix. + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @tparam I_idx The current row index being processed. + * @param to_matrix The destination matrix where values are substituted. + * @param from_matrix The source matrix containing values to substitute. + */ static void substitute(To_Type &to_matrix, const From_Type &from_matrix) { Column::substitute(to_matrix, from_matrix); @@ -654,11 +1350,34 @@ struct Row { // row recursion termination template struct Row { + /** + * @brief Substitutes the first row of the from_matrix into the to_matrix. + * + * This static function substitutes the values from the first row (index 0) of + * the from_matrix into the to_matrix at the corresponding position. + * + * @tparam To_Type The type of the destination matrix. + * @tparam From_Type The type of the source matrix. + * @param to_matrix The destination matrix where values are substituted. + * @param from_matrix The source matrix containing values to substitute. + */ static void substitute(To_Type &to_matrix, const From_Type &from_matrix) { Column::substitute(to_matrix, from_matrix); } }; +/** + * @brief Substitutes values from a source matrix into a destination matrix. + * + * This function template substitutes the values from a source matrix into a + * destination matrix, ensuring that both matrices have the same number of + * elements. + * + * @tparam To_Type The type of the destination matrix. + * @tparam From_Type The type of the source matrix. + * @param to_matrix The destination matrix where values are substituted. + * @param from_matrix The source matrix containing values to substitute. + */ template inline void substitute(To_Type &to_matrix, const From_Type &from_matrix) { Row inline void update_reshaped_matrix(To_Type &to_matrix, const From_Type &from_matrix) { @@ -680,6 +1411,21 @@ inline void update_reshaped_matrix(To_Type &to_matrix, } /* Reshape (So far, it can only outputs Dense Matrix.) */ + +/** + * @brief Reshapes a source matrix into a destination matrix of specified size. + * + * This function template reshapes the provided source matrix into a new + * destination matrix of size M x N, ensuring that the total number of elements + * matches between the source and destination matrices. + * + * @tparam M The number of columns in the destination matrix. + * @tparam N The number of rows in the destination matrix. + * @tparam From_Type The type of the source matrix. + * @param from_matrix The source matrix to reshape. + * @return DenseMatrix_Type + * A reshaped matrix containing the values from the source matrix. + */ template inline auto reshape(const From_Type &from_matrix) -> DenseMatrix_Type { From 338b5c8ce938a6eefdbe911a3f7c859569ffafab Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 08:11:19 +0900 Subject: [PATCH 20/31] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_base.hpp | 1047 +++++++++++++++++++++++++++- 1 file changed, 1042 insertions(+), 5 deletions(-) diff --git a/python_numpy/python_numpy_base.hpp b/python_numpy/python_numpy_base.hpp index 426b346..7eb9746 100644 --- a/python_numpy/python_numpy_base.hpp +++ b/python_numpy/python_numpy_base.hpp @@ -1,3 +1,21 @@ +/** + * @file python_numpy_base.hpp + * @brief Core matrix class templates and operations for PythonNumpy C++ + * library. + * + * This file defines the main matrix class templates and arithmetic operations + * for the PythonNumpy namespace, providing dense, diagonal, and sparse matrix + * types with static shape and type information. The classes are designed to + * mimic the behavior and flexibility of NumPy matrices in C++, supporting a + * wide range of matrix operations (addition, subtraction, multiplication, + * transpose, etc.) and conversions between matrix types. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_BASE_HPP__ #define __PYTHON_NUMPY_BASE_HPP__ @@ -22,6 +40,24 @@ template class Matrix; +/** + * @brief Dense matrix class template specialization for DefDense storage. + * + * This class represents a fixed-size dense matrix with elements of type T, + * with dimensions M (columns) x N (rows). It provides constructors for various + * initialization methods, copy/move semantics, element access, and common + * matrix operations such as transpose and conversion to complex types. + * + * @tparam T Element type of the matrix (e.g., float, double, std::complex). + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * + * @note + * - The underlying storage is provided by Base::Matrix::Matrix. + * - Provides compile-time dimension checks for element access. + * - Supports creation of zero, one, and full-valued matrices. + * - Supports conversion to complex, and extraction of real/imaginary parts. + */ template class Matrix { public: @@ -67,8 +103,20 @@ class Matrix { return *this; } +public: /* Function */ - /* Get Dense Matrix value */ + + /** + * @brief Retrieves the element at the specified column and row indices from + * the matrix. + * + * @tparam COL The zero-based column index (must be less than M). + * @tparam ROW The zero-based row index (must be less than N). + * @return T The value at the specified column and row. + * + * @note Compile-time assertions ensure that the provided indices are within + * valid bounds. + */ template inline T get() const { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < N, "Row Index is out of range."); @@ -76,7 +124,17 @@ class Matrix { return matrix.template get(); } - /* Set Dense Matrix value */ + /** + * @brief Sets the element at the specified column and row indices to the + * provided value. + * + * @tparam COL The zero-based column index (must be less than M). + * @tparam ROW The zero-based row index (must be less than N). + * @param value The value to set at the specified column and row. + * + * @note Compile-time assertions ensure that the provided indices are within + * valid bounds. + */ template inline void set(const T &value) { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < N, "Row Index is out of range."); @@ -84,10 +142,25 @@ class Matrix { matrix.template set(value); } + /** + * @brief Retrieves the underlying matrix data. + * + * @return Base::Matrix::Matrix The underlying matrix data. + */ constexpr std::size_t rows() const { return ROWS; } + /** + * @brief Retrieves the number of columns in the matrix. + * + * @return std::size_t The number of columns in the matrix. + */ constexpr std::size_t cols() const { return COLS; } + /** + * @brief Retrieves the number of elements in the matrix. + * + * @return std::size_t The total number of elements in the matrix (M * N). + */ T &operator()(std::size_t col, std::size_t row) { if (col >= M) { col = M - 1; @@ -99,6 +172,16 @@ class Matrix { return this->matrix(col, row); } + /** + * @brief Retrieves the element at the specified column and row indices. + * + * @param col The zero-based column index (must be less than M). + * @param row The zero-based row index (must be less than N). + * @return const T& The value at the specified column and row. + * + * @note If the indices are out of bounds, they are clamped to the maximum + * valid index. + */ const T &operator()(std::size_t col, std::size_t row) const { if (col >= M) { col = M - 1; @@ -110,42 +193,117 @@ class Matrix { return this->matrix(col, row); } + /** + * @brief Accesses the element at the specified column and row indices. + * + * @param col The zero-based column index (must be less than M). + * @param row The zero-based row index (must be less than N). + * @return T& A reference to the value at the specified column and row. + * + * @note This method is fast but may cause segmentation faults if indices are + * out of bounds. + */ inline T &access(const std::size_t &col, const std::size_t &row) { - // This is fast but may cause segmentation fault. return this->matrix(row)[col]; } + /** + * @brief Accesses the element at the specified column and row indices. + * + * @param col The zero-based column index (must be less than M). + * @param row The zero-based row index (must be less than N). + * @return const T& A constant reference to the value at the specified column + * and row. + * + * @note This method is fast but may cause segmentation faults if indices are + * out of bounds. + */ static inline auto zeros(void) -> Matrix { return Matrix(); } + /** + * @brief Creates and returns a dense matrix of ones. + * + * This static method constructs a dense matrix of size M x N, + * where all elements are set to 1 (of type T). + * + * @return Matrix A dense matrix of ones with dimensions M + * x N. + */ static inline auto ones(void) -> Matrix { return Matrix(Base::Matrix::Matrix::ones()); } + /** + * @brief Creates and returns a dense matrix filled with a specified value. + * + * This static method constructs a dense matrix of size M x N, + * where all elements are set to the specified value. + * + * @param value The value to fill the matrix with. + * @return Matrix A dense matrix filled with the specified + * value, with dimensions M x N. + */ static inline auto full(const T &value) -> Matrix { return Matrix( Base::Matrix::Matrix::full(value)); } + /** + * @brief Transposes the matrix. + * + * This method returns a new matrix that is the transpose of the current + * matrix, swapping rows and columns. + * + * @return Matrix A new matrix that is the transpose of the + * current matrix. + */ inline auto transpose(void) const -> Matrix { return Matrix( Base::Matrix::output_matrix_transpose(this->matrix)); } + /** + * @brief Creates a complex matrix from the current matrix. + * + * This method converts the current real matrix into a complex matrix, + * where the real part is the current matrix and the imaginary part is zero. + * + * @return Matrix, M, N> A new complex matrix with the + * same dimensions as the current matrix. + */ inline auto create_complex(void) const -> Matrix, M, N> { return Matrix, M, N>( Base::Matrix::convert_matrix_real_to_complex(this->matrix)); } + /** + * @brief Extracts the real part of a complex matrix. + * + * This method returns a new matrix containing only the real part of the + * current complex matrix. + * + * @return Matrix A new matrix containing the real + * part of the current complex matrix. + */ inline auto real(void) const -> Matrix { return Matrix( ComplexOperation::GetRealFromComplexDenseMatrix< Value_Type, T, M, N, IS_COMPLEX>::get(this->matrix)); } + /** + * @brief Extracts the imaginary part of a complex matrix. + * + * This method returns a new matrix containing only the imaginary part of the + * current complex matrix. + * + * @return Matrix A new matrix containing the + * imaginary part of the current complex matrix. + */ inline auto imag(void) const -> Matrix { return Matrix( ComplexOperation::GetImagFromComplexDenseMatrix< @@ -164,6 +322,23 @@ class Matrix { Base::Matrix::Matrix matrix; }; +/** + * @brief Diagonal matrix class template specialization for DefDiag storage. + * + * This class represents a fixed-size diagonal matrix with elements of type T, + * with dimensions M x M. It provides constructors for various initialization + * methods, copy/move semantics, element access, and common matrix operations + * such as transpose and conversion to complex types. + * + * @tparam T Element type of the matrix (e.g., float, double, std::complex). + * @tparam M Number of columns/rows in the square matrix. + * + * @note + * - The underlying storage is provided by Base::Matrix::DiagMatrix. + * - Provides compile-time dimension checks for element access. + * - Supports creation of identity, full-valued matrices. + * - Supports conversion to complex, and extraction of real/imaginary parts. + */ template class Matrix { public: /* Type */ @@ -211,15 +386,32 @@ template class Matrix { return *this; } +public: /* Function */ - /* Get Diag Matrix value */ + template struct GetSetDiagMatrix { + /** @brief Gets the value at the specified column and row indices from the + * diagonal matrix. + * + * @param matrix The diagonal matrix to get the value from. + * @return U The value at the specified column and row. + * + * @note This function returns zero for non-diagonal elements. + */ static U get_value(const Base::Matrix::DiagMatrix &matrix) { static_cast(matrix); return static_cast(0); } + /** @brief Sets the element at the specified column and row indices to the + * provided value. + * + * @param matrix The diagonal matrix to set the value in. + * @param value The value to set at the specified column and row. + * + * @note This function does nothing for non-diagonal elements. + */ static void set_value(Base::Matrix::DiagMatrix &matrix, T value) { static_cast(matrix); static_cast(value); @@ -228,17 +420,48 @@ template class Matrix { template struct GetSetDiagMatrix { + /** + * @brief Gets the value at the specified column index from the diagonal + * matrix. + * + * @param matrix The diagonal matrix to get the value from. + * @return U The value at the specified column index. + * + * @note This function returns the diagonal element for the specified column + * index. + */ static T get_value(const Base::Matrix::DiagMatrix &matrix) { return matrix.data[I_Col]; } + /** + * @brief Sets the element at the specified column index to the provided + * value. + * + * @param matrix The diagonal matrix to set the value in. + * @param value The value to set at the specified column index. + * + * @note This function sets the diagonal element for the specified column + * index. + */ static void set_value(Base::Matrix::DiagMatrix &matrix, T value) { matrix.data[I_Col] = value; } }; + /** + * @brief Gets the value at the specified column and row indices from the + * diagonal matrix. + * + * @tparam COL The zero-based column index (must be less than M). + * @tparam ROW The zero-based row index (must be less than M). + * @return T The value at the specified column and row. + * + * @note Compile-time assertions ensure that the provided indices are within + * valid bounds. + */ template inline T get() const { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < M, "Row Index is out of range."); @@ -246,7 +469,17 @@ template class Matrix { return GetSetDiagMatrix::get_value(this->matrix); } - /* Set Diag Matrix value */ + /** + * @brief Sets the element at the specified column and row indices to the + * provided value. + * + * @tparam COL The zero-based column index (must be less than M). + * @tparam ROW The zero-based row index (must be less than M). + * @param value The value to set at the specified column and row. + * + * @note Compile-time assertions ensure that the provided indices are within + * valid bounds. + */ template inline void set(const T &value) { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < M, "Row Index is out of range."); @@ -254,10 +487,27 @@ template class Matrix { GetSetDiagMatrix::set_value(this->matrix, value); } + /** + * @brief Retrieves the underlying diagonal matrix data. + * + * @return Base::Matrix::DiagMatrix The underlying diagonal matrix data. + */ constexpr std::size_t rows() const { return ROWS; } + /** + * @brief Retrieves the number of columns in the diagonal matrix. + * + * @return std::size_t The number of columns in the diagonal matrix (equal to + * M). + */ constexpr std::size_t cols() const { return COLS; } + /** + * @brief Retrieves the number of elements in the diagonal matrix. + * + * @return std::size_t The total number of elements in the diagonal matrix + * (M). + */ T &operator()(std::size_t index) { if (index >= M) { index = M - 1; @@ -266,6 +516,15 @@ template class Matrix { return this->matrix[index]; } + /** + * @brief Retrieves the element at the specified index in the diagonal matrix. + * + * @param index The zero-based index (must be less than M). + * @return const T& The value at the specified index. + * + * @note If the index is out of bounds, it is clamped to the maximum valid + * index (M - 1). + */ const T &operator()(std::size_t index) const { if (index >= M) { index = M - 1; @@ -274,39 +533,115 @@ template class Matrix { return this->matrix[index]; } + /** + * @brief Accesses the element at the specified index in the diagonal matrix. + * + * @param index The zero-based index (must be less than M). + * @return T& A reference to the value at the specified index. + * + * @note This method is fast but may cause segmentation faults if the index is + * out of bounds. + */ inline T &access(const std::size_t &index) { // This is fast but may cause segmentation fault. return this->matrix[index]; } + /** + * @brief Accesses the element at the specified index in the diagonal matrix. + * + * @param index The zero-based index (must be less than M). + * @return const T& A constant reference to the value at the specified index. + * + * @note This method is fast but may cause segmentation faults if the index is + * out of bounds. + */ static inline auto identity(void) -> Matrix { return Matrix(Base::Matrix::DiagMatrix::identity()); } + /** + * @brief Creates and returns a diagonal matrix filled with zeros. + * + * This static method constructs a diagonal matrix of size M x M, + * where all diagonal elements are set to zero, and all off-diagonal + * elements are also zero. + * + * @return Matrix A diagonal matrix of zeros with dimensions M + * x M. + */ static inline auto full(const T &value) -> Matrix { return Matrix(Base::Matrix::DiagMatrix::full(value)); } + /** + * @brief Creates a diagonal matrix filled with zeros. + * + * This static method constructs a diagonal matrix of size M x M, + * where all diagonal elements are set to zero, and all off-diagonal + * elements are also zero. + * + * @return Matrix A diagonal matrix of zeros with dimensions M + * x M. + */ inline auto create_dense(void) const -> Matrix { return Matrix( Base::Matrix::output_dense_matrix(this->matrix)); } + /** + * @brief Transposes the diagonal matrix. + * + * This method returns a new diagonal matrix that is the transpose of the + * current matrix. For diagonal matrices, the transpose is the same as the + * original matrix. + * + * @return Matrix A new diagonal matrix that is the transpose + * of the current matrix. + */ inline auto transpose(void) const -> Matrix { return *this; } + /** + * @brief Creates a complex diagonal matrix from the current diagonal matrix. + * + * This method converts the current real diagonal matrix into a complex + * diagonal matrix, where the real part is the current matrix and the + * imaginary part is zero. + * + * @return Matrix, M> A new complex diagonal matrix with + * the same dimensions as the current matrix. + */ inline auto create_complex(void) const -> Matrix, M> { return Matrix, M>( Base::Matrix::convert_matrix_real_to_complex(this->matrix)); } + /** + * @brief Extracts the real part of a complex diagonal matrix. + * + * This method returns a new diagonal matrix containing only the real part of + * the current complex diagonal matrix. + * + * @return Matrix A new diagonal matrix containing the + * real part of the current complex diagonal matrix. + */ inline auto real(void) const -> Matrix { return Matrix( ComplexOperation::GetRealFromComplexDiagMatrix< Value_Type, T, M, IS_COMPLEX>::get(this->matrix)); } + /** + * @brief Extracts the imaginary part of a complex diagonal matrix. + * + * This method returns a new diagonal matrix containing only the imaginary + * part of the current complex diagonal matrix. + * + * @return Matrix A new diagonal matrix containing the + * imaginary part of the current complex diagonal matrix. + */ inline auto imag(void) const -> Matrix { return Matrix( ComplexOperation::GetImagFromComplexDiagMatrix< @@ -325,6 +660,26 @@ template class Matrix { Base::Matrix::DiagMatrix matrix; }; +/** + * @brief Sparse matrix class template specialization for DefSparse storage. + * + * This class represents a fixed-size sparse matrix with elements of type T, + * with dimensions M x N. It provides constructors for various initialization + * methods, copy/move semantics, element access, and common matrix operations + * such as transpose and conversion to complex types. + * + * @tparam T Element type of the matrix (e.g., float, double, std::complex). + * @tparam M Number of columns in the matrix. + * @tparam N Number of rows in the matrix. + * @tparam SparseAvailable Type indicating the availability of sparse features. + * + * @note + * - The underlying storage is provided by Base::Matrix::CompiledSparseMatrix. + * - Provides compile-time dimension checks for element access. + * - Supports creation of full-valued matrices. + * - Supports conversion to complex, and extraction of real/imaginary parts. + */ template class Matrix { public: @@ -378,12 +733,34 @@ class Matrix { return *this; } +public: /* Function */ + + /** + * @brief Creates a dense matrix from the sparse matrix. + * + * This method converts the current sparse matrix into a dense matrix, + * where all elements are represented explicitly. + * + * @return Matrix A new dense matrix with the same + * dimensions as the current sparse matrix. + */ inline auto create_dense(void) const -> Matrix { return Matrix( Base::Matrix::output_dense_matrix(this->matrix)); } + /** + * @brief Retrieves the element at the specified column and row indices from + * the sparse matrix. + * + * @tparam COL The zero-based column index (must be less than M). + * @tparam ROW The zero-based row index (must be less than N). + * @return T The value at the specified column and row. + * + * @note Compile-time assertions ensure that the provided indices are within + * valid bounds. + */ template inline T get() const { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < N, "Row Index is out of range."); @@ -391,6 +768,16 @@ class Matrix { return Base::Matrix::get_sparse_matrix_value(this->matrix); } + /** + * @brief Retrieves the value of a specific element in the sparse matrix. + * + * @tparam ELEMENT The zero-based index of the element (must be less than + * NumberOfValues). + * @return T The value of the specified element. + * + * @note Compile-time assertions ensure that the provided index is within + * valid bounds. + */ template inline T get() const { static_assert(ELEMENT < NumberOfValues, "ELEMENT must be the same or less than the number " @@ -399,6 +786,17 @@ class Matrix { return Base::Matrix::get_sparse_matrix_element_value(this->matrix); } + /** + * @brief Sets the element at the specified column and row indices to the + * provided value. + * + * @tparam COL The zero-based column index (must be less than M). + * @tparam ROW The zero-based row index (must be less than N). + * @param value The value to set at the specified column and row. + * + * @note Compile-time assertions ensure that the provided indices are within + * valid bounds. + */ template inline void set(const T &value) { static_assert(COL < M, "Column Index is out of range."); static_assert(ROW < N, "Row Index is out of range."); @@ -406,6 +804,16 @@ class Matrix { Base::Matrix::set_sparse_matrix_value(this->matrix, value); } + /** + * @brief Sets the value of a specific element in the sparse matrix. + * + * @tparam ELEMENT The zero-based index of the element (must be less than + * NumberOfValues). + * @param value The value to set for the specified element. + * + * @note Compile-time assertions ensure that the provided index is within + * valid bounds. + */ template inline void set(const T &value) { static_assert(ELEMENT < NumberOfValues, "ELEMENT must be the same or less than the number " @@ -414,10 +822,27 @@ class Matrix { Base::Matrix::set_sparse_matrix_element_value(this->matrix, value); } + /** + * @brief Retrieves the underlying sparse matrix data. + * + * @return _BaseMatrix_Type The underlying sparse matrix data. + */ constexpr std::size_t rows() const { return ROWS; } + /** + * @brief Retrieves the number of columns in the sparse matrix. + * + * @return std::size_t The number of columns in the sparse matrix (equal to + * M). + */ constexpr std::size_t cols() const { return COLS; } + /** + * @brief Retrieves the number of elements in the sparse matrix. + * + * @return std::size_t The total number of elements in the sparse matrix (M * + * N). + */ T &operator()(std::size_t value_index) { if (value_index >= this->matrix.values.size()) { value_index = this->matrix.values.size() - 1; @@ -426,6 +851,16 @@ class Matrix { return this->matrix[value_index]; } + /** + * @brief Retrieves the element at the specified index in the sparse matrix. + * + * @param value_index The zero-based index of the element (must be less than + * NumberOfValues). + * @return const T& The value at the specified index. + * + * @note If the index is out of bounds, it is clamped to the maximum valid + * index (NumberOfValues - 1). + */ const T &operator()(std::size_t value_index) const { if (value_index >= this->matrix.values.size()) { value_index = this->matrix.values.size() - 1; @@ -434,18 +869,48 @@ class Matrix { return this->matrix[value_index]; } + /** + * @brief Accesses the element at the specified index in the sparse matrix. + * + * @param value_index The zero-based index of the element (must be less than + * NumberOfValues). + * @return T& A reference to the value at the specified index. + * + * @note This method is fast but may cause segmentation faults if the index is + * out of bounds. + */ inline T &access(const std::size_t &value_index) { // This is fast but may cause segmentation fault. return this->matrix[value_index]; } + /** + * @brief Accesses the element at the specified index in the sparse matrix. + * + * @param value_index The zero-based index of the element (must be less than + * NumberOfValues). + * @return const T& A constant reference to the value at the specified index. + * + * @note This method is fast but may cause segmentation faults if the index is + * out of bounds. + */ static inline auto full(const T &value) -> Matrix { return Matrix( _BaseMatrix_Type::full(value)); } + /** + * @brief Transposes the sparse matrix. + * + * This method returns a new sparse matrix that is the transpose of the + * current matrix, swapping rows and columns. + * + * @return Matrix> A new sparse matrix that is the + * transpose of the current matrix. + */ inline auto transpose(void) const -> Matrix> { @@ -454,6 +919,16 @@ class Matrix { Base::Matrix::output_matrix_transpose(this->matrix)); } + /** + * @brief Creates a complex sparse matrix from the current sparse matrix. + * + * This method converts the current real sparse matrix into a complex sparse + * matrix, where the real part is the current matrix and the imaginary part is + * zero. + * + * @return Matrix, M, N, SparseAvailable> A new complex + * sparse matrix with the same dimensions as the current matrix. + */ inline auto create_complex(void) const -> Matrix, M, N, SparseAvailable> { @@ -461,6 +936,15 @@ class Matrix { Base::Matrix::convert_matrix_real_to_complex(this->matrix)); } + /** + * @brief Extracts the real part of a complex sparse matrix. + * + * This method returns a new sparse matrix containing only the real part of + * the current complex sparse matrix. + * + * @return Matrix A new sparse + * matrix containing the real part of the current complex sparse matrix. + */ inline auto real(void) const -> Matrix { return Matrix( @@ -469,6 +953,15 @@ class Matrix { IS_COMPLEX>::get(this->matrix)); } + /** + * @brief Extracts the imaginary part of a complex sparse matrix. + * + * This method returns a new sparse matrix containing only the imaginary part + * of the current complex sparse matrix. + * + * @return Matrix A new sparse + * matrix containing the imaginary part of the current complex sparse matrix. + */ inline auto imag(void) const -> Matrix { return Matrix( @@ -492,6 +985,21 @@ class Matrix { }; /* Matrix Addition */ + +/** + * @brief Adds two matrices together. + * + * This function overloads the '+' operator to add two matrices of the same type + * and dimensions. It returns a new matrix containing the sum of the two input + * matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The first matrix to add. + * @param B The second matrix to add. + * @return Matrix A new matrix containing the sum of A and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -500,6 +1008,20 @@ inline auto operator+(const Matrix &A, return Matrix(std::move(A.matrix + B.matrix)); } +/** + * @brief Adds a dense matrix and a diagonal matrix together. + * + * This function overloads the '+' operator to add a dense matrix and a + * diagonal matrix of the same size. It returns a new dense matrix containing + * the sum of the two matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The dense matrix to add. + * @param B The diagonal matrix to add. + * @return Matrix A new dense matrix containing the sum of A + * and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -509,6 +1031,21 @@ inline auto operator+(const Matrix &A, return Matrix(std::move(A.matrix + B.matrix)); } +/** + * @brief Adds a dense matrix and a sparse matrix together. + * + * This function overloads the '+' operator to add a dense matrix and a sparse + * matrix of the same size. It returns a new dense matrix containing the sum of + * the two matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The dense matrix to add. + * @param B The sparse matrix to add. + * @return Matrix A new dense matrix containing the sum of A + * and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -517,6 +1054,20 @@ inline auto operator+(const Matrix &A, return Matrix(std::move(A.matrix + B.matrix)); } +/** + * @brief Adds a diagonal matrix and a dense matrix together. + * + * This function overloads the '+' operator to add a diagonal matrix and a + * dense matrix of the same size. It returns a new dense matrix containing the + * sum of the two matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The diagonal matrix to add. + * @param B The dense matrix to add. + * @return Matrix A new dense matrix containing the sum of A + * and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -526,6 +1077,20 @@ inline auto operator+(const Matrix &A, return Matrix(std::move(A.matrix + B.matrix)); } +/** + * @brief Adds two diagonal matrices together. + * + * This function overloads the '+' operator to add two diagonal matrices of the + * same size. It returns a new diagonal matrix containing the sum of the two + * matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The first diagonal matrix to add. + * @param B The second diagonal matrix to add. + * @return Matrix A new diagonal matrix containing the sum of A + * and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) -> Matrix { @@ -533,6 +1098,20 @@ inline auto operator+(const Matrix &A, return Matrix(std::move(A.matrix + B.matrix)); } +/** + * @brief Adds a diagonal matrix and a sparse matrix together. + * + * This function overloads the '+' operator to add a diagonal matrix and a + * sparse matrix of the same size. It returns a new sparse matrix containing the + * sum of the two matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The diagonal matrix to add. + * @param B The sparse matrix to add. + * @return Matrix A new sparse matrix containing the sum of + * A and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -545,6 +1124,21 @@ inline auto operator+(const Matrix &A, std::move(A.matrix + B.matrix)); } +/** + * @brief Adds a sparse matrix and a dense matrix together. + * + * This function overloads the '+' operator to add a sparse matrix and a dense + * matrix of the same size. It returns a new dense matrix containing the sum of + * the two matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The sparse matrix to add. + * @param B The dense matrix to add. + * @return Matrix A new dense matrix containing the sum of A + * and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -553,6 +1147,20 @@ inline auto operator+(const Matrix &A, return Matrix(std::move(A.matrix + B.matrix)); } +/** + * @brief Adds a sparse matrix and a diagonal matrix together. + * + * This function overloads the '+' operator to add a sparse matrix and a + * diagonal matrix of the same size. It returns a new sparse matrix containing + * the sum of the two matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The sparse matrix to add. + * @param B The diagonal matrix to add. + * @return Matrix A new sparse matrix containing the sum of + * A and B. + */ template inline auto operator+(const Matrix &A, const Matrix &B) @@ -565,6 +1173,21 @@ inline auto operator+(const Matrix &A, std::move(A.matrix + B.matrix)); } +/** + * @brief Adds two sparse matrices together. + * + * This function overloads the '+' operator to add two sparse matrices of the + * same type and dimensions. It returns a new sparse matrix containing the sum + * of the two input matrices. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The first sparse matrix to add. + * @param B The second sparse matrix to add. + * @return Matrix A new sparse matrix containing the sum of + * A and B. + */ template inline auto operator+(const Matrix &A, @@ -580,6 +1203,22 @@ inline auto operator+(const Matrix &A, } /* Matrix Subtraction */ + +/** + * @brief Subtracts two matrices. + * + * This function overloads the '-' operator to subtract one matrix from another + * of the same type and dimensions. It returns a new matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The matrix to subtract from. + * @param B The matrix to subtract. + * @return Matrix A new matrix containing the result of A - + * B. + */ template inline auto operator-(const Matrix &A) -> Matrix { @@ -587,12 +1226,41 @@ inline auto operator-(const Matrix &A) return Matrix(std::move(-A.matrix)); } +/** + * @brief Subtracts a dense matrix from a diagonal matrix. + * + * This function overloads the '-' operator to subtract a dense matrix from a + * diagonal matrix of the same size. It returns a new dense matrix containing + * the result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The diagonal matrix to subtract from. + * @param B The dense matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A) -> Matrix { return Matrix(std::move(-A.matrix)); } +/** + * @brief Subtracts a dense matrix from a sparse matrix. + * + * This function overloads the '-' operator to subtract a dense matrix from a + * sparse matrix of the same size. It returns a new dense matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The dense matrix to subtract from. + * @param B The sparse matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A) -> Matrix { @@ -600,6 +1268,21 @@ inline auto operator-(const Matrix &A) return Matrix(std::move(-A.matrix)); } +/** + * @brief Subtracts a dense matrix from another dense matrix. + * + * This function overloads the '-' operator to subtract one dense matrix from + * another of the same type and dimensions. It returns a new dense matrix + * containing the result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The first dense matrix to subtract from. + * @param B The second dense matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -608,6 +1291,20 @@ inline auto operator-(const Matrix &A, return Matrix(std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts a diagonal matrix from a dense matrix. + * + * This function overloads the '-' operator to subtract a diagonal matrix from + * a dense matrix of the same size. It returns a new dense matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The dense matrix to subtract from. + * @param B The diagonal matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -617,6 +1314,21 @@ inline auto operator-(const Matrix &A, return Matrix(std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts a dense matrix from a sparse matrix. + * + * This function overloads the '-' operator to subtract a dense matrix from a + * sparse matrix of the same size. It returns a new dense matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The dense matrix to subtract from. + * @param B The sparse matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -625,6 +1337,20 @@ inline auto operator-(const Matrix &A, return Matrix(std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts a diagonal matrix from a dense matrix. + * + * This function overloads the '-' operator to subtract a diagonal matrix from + * a dense matrix of the same size. It returns a new dense matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The diagonal matrix to subtract from. + * @param B The dense matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -634,6 +1360,21 @@ inline auto operator-(const Matrix &A, return Matrix(std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts two diagonal matrices. + * + * This function overloads the '-' operator to subtract one diagonal matrix + * from another of the same type and dimensions. It returns a new diagonal + * matrix containing the result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The first diagonal matrix to subtract from. + * @param B The second diagonal matrix to subtract. + * @return Matrix A new diagonal matrix containing the result of + * A + * - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) -> Matrix { @@ -641,6 +1382,20 @@ inline auto operator-(const Matrix &A, return Matrix(std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts a diagonal matrix from a sparse matrix. + * + * This function overloads the '-' operator to subtract a diagonal matrix from + * a sparse matrix of the same size. It returns a new sparse matrix containing + * the result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The diagonal matrix to subtract from. + * @param B The sparse matrix to subtract. + * @return Matrix A new sparse matrix containing the result + * of A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -653,6 +1408,21 @@ inline auto operator-(const Matrix &A, std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts a sparse matrix from a dense matrix. + * + * This function overloads the '-' operator to subtract a sparse matrix from a + * dense matrix of the same size. It returns a new dense matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The sparse matrix to subtract from. + * @param B The dense matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -661,6 +1431,21 @@ inline auto operator-(const Matrix &A, return Matrix(std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts a sparse matrix from a dense matrix. + * + * This function overloads the '-' operator to subtract a dense matrix from a + * sparse matrix of the same size. It returns a new sparse matrix containing the + * result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The sparse matrix to subtract from. + * @param B The dense matrix to subtract. + * @return Matrix A new dense matrix containing the result of + * A - B. + */ template inline auto operator-(const Matrix &A, const Matrix &B) @@ -673,6 +1458,21 @@ inline auto operator-(const Matrix &A, std::move(A.matrix - B.matrix)); } +/** + * @brief Subtracts two sparse matrices. + * + * This function overloads the '-' operator to subtract one sparse matrix from + * another of the same type and dimensions. It returns a new sparse matrix + * containing the result of the subtraction. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param A The first sparse matrix to subtract from. + * @param B The second sparse matrix to subtract. + * @return Matrix A new sparse matrix containing the result + * of A - B. + */ template inline auto operator-(const Matrix &A, @@ -688,6 +1488,22 @@ inline auto operator-(const Matrix &A, } /* Matrix Multiply Scalar */ + +/** + * @brief Multiplies a scalar with a dense matrix. + * + * This function overloads the '*' operator to multiply a scalar with a dense + * matrix. It returns a new dense matrix containing the result of the + * multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param a The scalar to multiply with. + * @param B The dense matrix to multiply. + * @return Matrix A new dense matrix containing the result of + * a * B. + */ template inline auto operator*(const T &a, const Matrix &B) -> Matrix { @@ -695,6 +1511,21 @@ inline auto operator*(const T &a, const Matrix &B) return Matrix(std::move(a * B.matrix)); } +/** + * @brief Multiplies a dense matrix with a scalar. + * + * This function overloads the '*' operator to multiply a dense matrix with a + * scalar. It returns a new dense matrix containing the result of the + * multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param B The dense matrix to multiply. + * @param a The scalar to multiply with. + * @return Matrix A new dense matrix containing the result of + * B * a. + */ template inline auto operator*(const Matrix &B, const T &a) -> Matrix { @@ -702,6 +1533,20 @@ inline auto operator*(const Matrix &B, const T &a) return Matrix(std::move(B.matrix * a)); } +/** + * @brief Multiplies a scalar with a diagonal matrix. + * + * This function overloads the '*' operator to multiply a scalar with a + * diagonal matrix. It returns a new diagonal matrix containing the result of + * the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param a The scalar to multiply with. + * @param B The diagonal matrix to multiply. + * @return Matrix A new diagonal matrix containing the result of + * a * B. + */ template inline auto operator*(const T &a, const Matrix &B) -> Matrix { @@ -709,6 +1554,20 @@ inline auto operator*(const T &a, const Matrix &B) return Matrix(std::move(a * B.matrix)); } +/** + * @brief Multiplies a diagonal matrix with a scalar. + * + * This function overloads the '*' operator to multiply a diagonal matrix with + * a scalar. It returns a new diagonal matrix containing the result of the + * multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param B The diagonal matrix to multiply. + * @param a The scalar to multiply with. + * @return Matrix A new diagonal matrix containing the result of + * B * a. + */ template inline auto operator*(const Matrix &B, const T &a) -> Matrix { @@ -716,6 +1575,21 @@ inline auto operator*(const Matrix &B, const T &a) return Matrix(std::move(B.matrix * a)); } +/** + * @brief Multiplies a scalar with a sparse matrix. + * + * This function overloads the '*' operator to multiply a scalar with a sparse + * matrix. It returns a new sparse matrix containing the result of the + * multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param a The scalar to multiply with. + * @param B The sparse matrix to multiply. + * @return Matrix A new sparse matrix containing the result + * of a * B. + */ template inline auto operator*(const T &a, const Matrix &B) @@ -724,6 +1598,21 @@ inline auto operator*(const T &a, return Matrix(std::move(a * B.matrix)); } +/** + * @brief Multiplies a sparse matrix with a scalar. + * + * This function overloads the '*' operator to multiply a sparse matrix with a + * scalar. It returns a new sparse matrix containing the result of the + * multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the matrices. + * @tparam N The number of rows in the matrices. + * @param B The sparse matrix to multiply. + * @param a The scalar to multiply with. + * @return Matrix A new sparse matrix containing the result + * of B * a. + */ template inline auto operator*(const Matrix &B, const T &a) @@ -733,6 +1622,24 @@ inline auto operator*(const Matrix &B, } /* Matrix Multiply Matrix */ + +/** + * @brief Multiplies two dense matrices. + * + * This function overloads the '*' operator to multiply two dense matrices of + * compatible dimensions. It returns a new dense matrix containing the result of + * the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the first matrix. + * @tparam N The number of rows in the first matrix and columns in the second + * matrix. + * @tparam K The number of rows in the second matrix. + * @param A The first dense matrix to multiply. + * @param B The second dense matrix to multiply. + * @return Matrix A new dense matrix containing the result of + * A * B. + */ template inline auto operator*(const Matrix &A, const Matrix &B) @@ -741,6 +1648,23 @@ inline auto operator*(const Matrix &A, return Matrix(std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies a dense matrix and a diagonal matrix. + * + * This function overloads the '*' operator to multiply a dense matrix and a + * diagonal matrix of compatible dimensions. It returns a new dense matrix + * containing the result of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the dense matrix and rows in the diagonal + * matrix. + * @tparam N The number of rows in the dense matrix and columns in the diagonal + * matrix. + * @param A The dense matrix to multiply. + * @param B The diagonal matrix to multiply. + * @return Matrix A new dense matrix containing the result of + * A * B. + */ template inline auto operator*(const Matrix &A, const Matrix &B) @@ -749,6 +1673,23 @@ inline auto operator*(const Matrix &A, return Matrix(std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies a dense matrix and a sparse matrix. + * + * This function overloads the '*' operator to multiply a dense matrix and a + * sparse matrix of compatible dimensions. It returns a new dense matrix + * containing the result of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the dense matrix and rows in the sparse + * matrix. + * @tparam N The number of rows in the dense matrix and columns in the sparse + * matrix. + * @param A The dense matrix to multiply. + * @param B The sparse matrix to multiply. + * @return Matrix A new dense matrix containing the result of + * A * B. + */ template inline auto operator*(const Matrix &A, @@ -758,6 +1699,23 @@ inline auto operator*(const Matrix &A, return Matrix(std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies a diagonal matrix and a dense matrix. + * + * This function overloads the '*' operator to multiply a diagonal matrix and a + * dense matrix of compatible dimensions. It returns a new dense matrix + * containing the result of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the diagonal matrix and rows in the dense + * matrix. + * @tparam N The number of rows in the diagonal matrix and columns in the dense + * matrix. + * @param A The diagonal matrix to multiply. + * @param B The dense matrix to multiply. + * @return Matrix A new dense matrix containing the result of + * A * B. + */ template inline auto operator*(const Matrix &A, const Matrix &B) @@ -766,6 +1724,20 @@ inline auto operator*(const Matrix &A, return Matrix(std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies two diagonal matrices. + * + * This function overloads the '*' operator to multiply two diagonal matrices + * of the same size. It returns a new diagonal matrix containing the result of + * the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The first diagonal matrix to multiply. + * @param B The second diagonal matrix to multiply. + * @return Matrix A new diagonal matrix containing the result of + * A * B. + */ template inline auto operator*(const Matrix &A, const Matrix &B) -> Matrix { @@ -773,6 +1745,20 @@ inline auto operator*(const Matrix &A, return Matrix(std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies a diagonal matrix and a sparse matrix. + * + * This function overloads the '*' operator to multiply a diagonal matrix and a + * sparse matrix of compatible dimensions. It returns a new sparse matrix + * containing the result of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns/rows in the square matrices. + * @param A The diagonal matrix to multiply. + * @param B The sparse matrix to multiply. + * @return Matrix A new sparse matrix containing the result + * of A * B. + */ template inline auto operator*(const Matrix &A, const Matrix &B) @@ -782,6 +1768,23 @@ inline auto operator*(const Matrix &A, std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies a sparse matrix and a dense matrix. + * + * This function overloads the '*' operator to multiply a sparse matrix and a + * dense matrix of compatible dimensions. It returns a new dense matrix + * containing the result of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the sparse matrix and rows in the dense + * matrix. + * @tparam N The number of rows in the sparse matrix and columns in the dense + * matrix. + * @param A The sparse matrix to multiply. + * @param B The dense matrix to multiply. + * @return Matrix A new dense matrix containing the result of + * A * B. + */ template inline auto operator*(const Matrix &A, @@ -791,6 +1794,23 @@ inline auto operator*(const Matrix &A, return Matrix(std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies a sparse matrix and a diagonal matrix. + * + * This function overloads the '*' operator to multiply a sparse matrix and a + * diagonal matrix of compatible dimensions. It returns a new sparse matrix + * containing the result of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the sparse matrix and rows in the diagonal + * matrix. + * @tparam N The number of rows in the sparse matrix and columns in the diagonal + * matrix. + * @param A The sparse matrix to multiply. + * @param B The diagonal matrix to multiply. + * @return Matrix A new sparse matrix containing the result + * of A * B. + */ template inline auto operator*(const Matrix &A, const Matrix &B) @@ -800,6 +1820,23 @@ inline auto operator*(const Matrix &A, std::move(A.matrix * B.matrix)); } +/** + * @brief Multiplies two sparse matrices. + * + * This function overloads the '*' operator to multiply two sparse matrices of + * compatible dimensions. It returns a new sparse matrix containing the result + * of the multiplication. + * + * @tparam T The type of elements in the matrices (e.g., float, double). + * @tparam M The number of columns in the first sparse matrix. + * @tparam N The number of rows in the first sparse matrix and columns in the + * second sparse matrix. + * @tparam K The number of rows in the second sparse matrix. + * @param A The first sparse matrix to multiply. + * @param B The second sparse matrix to multiply. + * @return Matrix A new sparse matrix containing the result + * of A * B. + */ template inline auto operator*(const Matrix &A, From 5d21d20e21ac4d309ce9fb1e5e642d63ba24b20a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 11:31:12 +0900 Subject: [PATCH 21/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_complex.hpp | 192 ++++++++++++++++++++++++++ 1 file changed, 192 insertions(+) diff --git a/python_numpy/python_numpy_complex.hpp b/python_numpy/python_numpy_complex.hpp index ed6cb36..93fcb22 100644 --- a/python_numpy/python_numpy_complex.hpp +++ b/python_numpy/python_numpy_complex.hpp @@ -1,3 +1,20 @@ +/** + * @file python_numpy_complex.hpp + * @brief Utilities for handling real and imaginary parts of complex matrices in + * the PythonNumpy namespace. + * + * This header provides a set of template structures and type traits for + * extracting real and imaginary components from dense, diagonal, and sparse + * matrices that may contain complex numbers. The utilities are designed to work + * with matrices defined in the Base::Matrix namespace and support both real and + * complex types through template specialization and SFINAE. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_COMPLEX_HPP__ #define __PYTHON_NUMPY_COMPLEX_HPP__ @@ -24,6 +41,21 @@ struct GetRealFromComplexDenseMatrix {}; template struct GetRealFromComplexDenseMatrix { + + /** + * @brief Extracts the real part from a matrix of complex numbers. + * + * This static function takes a matrix of complex numbers as input and returns + * a matrix containing only the real parts of each element, preserving the + * original matrix dimensions. + * + * @tparam T The type of the real part of the complex numbers. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input matrix containing complex numbers. + * @return Base::Matrix::Matrix A matrix containing the real parts of + * the input matrix. + */ static Base::Matrix::Matrix get(const Base::Matrix::Matrix &input) { @@ -33,6 +65,18 @@ struct GetRealFromComplexDenseMatrix { template struct GetRealFromComplexDenseMatrix { + /** + * @brief Returns the input matrix as it is when it is not complex. + * + * This static function simply returns the input matrix without any changes, + * as it is assumed to be a matrix of real numbers. + * + * @tparam T The type of the elements in the matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input matrix containing real numbers. + * @return Base::Matrix::Matrix The input matrix itself. + */ static Base::Matrix::Matrix get(const Base::Matrix::Matrix &input) { @@ -46,6 +90,20 @@ struct GetImagFromComplexDenseMatrix {}; template struct GetImagFromComplexDenseMatrix { + /** + * @brief Extracts the imaginary part from a matrix of complex numbers. + * + * This static function takes a matrix of complex numbers as input and returns + * a matrix containing only the imaginary parts of each element, preserving + * the original matrix dimensions. + * + * @tparam T The type of the imaginary part of the complex numbers. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input matrix containing complex numbers. + * @return Base::Matrix::Matrix A matrix containing the imaginary + * parts of the input matrix. + */ static Base::Matrix::Matrix get(const Base::Matrix::Matrix &input) { @@ -55,6 +113,20 @@ struct GetImagFromComplexDenseMatrix { template struct GetImagFromComplexDenseMatrix { + /** + * @brief Returns an empty matrix when the input is not complex. + * + * This static function returns an empty matrix of type T with dimensions M x + * N when the input matrix is not complex, as there are no imaginary parts to + * extract. + * + * @tparam T The type of the elements in the matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input matrix containing real numbers. + * @return Base::Matrix::Matrix An empty matrix of type T with + * dimensions M x N. + */ static Base::Matrix::Matrix get(const Base::Matrix::Matrix &input) { @@ -68,6 +140,19 @@ struct GetRealFromComplexDiagMatrix {}; template struct GetRealFromComplexDiagMatrix { + /** + * @brief Extracts the real part from a diagonal matrix of complex numbers. + * + * This static function takes a diagonal matrix of complex numbers as input + * and returns a diagonal matrix containing only the real parts of each + * diagonal element, preserving the original matrix dimensions. + * + * @tparam T The type of the real part of the complex numbers. + * @tparam M The size of the diagonal matrix. + * @param input The input diagonal matrix containing complex numbers. + * @return Base::Matrix::DiagMatrix A diagonal matrix containing the + * real parts of the input matrix. + */ static Base::Matrix::DiagMatrix get(const Base::Matrix::DiagMatrix &input) { @@ -77,6 +162,17 @@ struct GetRealFromComplexDiagMatrix { template struct GetRealFromComplexDiagMatrix { + /** + * @brief Returns the input diagonal matrix as it is when it is not complex. + * + * This static function simply returns the input diagonal matrix without any + * changes, as it is assumed to be a matrix of real numbers. + * + * @tparam T The type of the elements in the diagonal matrix. + * @tparam M The size of the diagonal matrix. + * @param input The input diagonal matrix containing real numbers. + * @return Base::Matrix::DiagMatrix The input diagonal matrix itself. + */ static Base::Matrix::DiagMatrix get(const Base::Matrix::DiagMatrix &input) { @@ -89,6 +185,20 @@ struct GetImagFromComplexDiagMatrix {}; template struct GetImagFromComplexDiagMatrix { + /** + * @brief Extracts the imaginary part from a diagonal matrix of complex + * numbers. + * + * This static function takes a diagonal matrix of complex numbers as input + * and returns a diagonal matrix containing only the imaginary parts of each + * diagonal element, preserving the original matrix dimensions. + * + * @tparam T The type of the imaginary part of the complex numbers. + * @tparam M The size of the diagonal matrix. + * @param input The input diagonal matrix containing complex numbers. + * @return Base::Matrix::DiagMatrix A diagonal matrix containing the + * imaginary parts of the input matrix. + */ static Base::Matrix::DiagMatrix get(const Base::Matrix::DiagMatrix &input) { @@ -98,6 +208,19 @@ struct GetImagFromComplexDiagMatrix { template struct GetImagFromComplexDiagMatrix { + /** + * @brief Returns an empty diagonal matrix when the input is not complex. + * + * This static function returns an empty diagonal matrix of type T with size M + * when the input matrix is not complex, as there are no imaginary parts to + * extract. + * + * @tparam T The type of the elements in the diagonal matrix. + * @tparam M The size of the diagonal matrix. + * @param input The input diagonal matrix containing real numbers. + * @return Base::Matrix::DiagMatrix An empty diagonal matrix of type T + * with size M. + */ static Base::Matrix::DiagMatrix get(const Base::Matrix::DiagMatrix &input) { @@ -106,6 +229,20 @@ struct GetImagFromComplexDiagMatrix { } }; +/** * @brief Type alias for a sparse matrix type based on the provided template + * parameters. + * + * This type alias defines a sparse matrix type using the + * Base::Matrix::CompiledSparseMatrix class template, with the specified element + * type T, dimensions M and N, and row indices and pointers derived from the + * SparseAvailable type. + * + * @tparam T The type of the elements in the sparse matrix. + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix. + * @tparam SparseAvailable A type that provides row indices and pointers for + * the sparse matrix. + */ template using BaseMatrixSparseMatrix_Type = Base::Matrix::CompiledSparseMatrix< T, M, N, RowIndicesFromSparseAvailable, @@ -119,6 +256,20 @@ template struct GetRealFromComplexSparseMatrix { + /** + * @brief Extracts the real part from a sparse matrix of complex numbers. + * + * This static function takes a sparse matrix of complex numbers as input and + * returns a sparse matrix containing only the real parts of each element, + * preserving the original matrix dimensions. + * + * @tparam T The type of the real part of the complex numbers. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input sparse matrix containing complex numbers. + * @return BaseMatrixSparseMatrix_Type A sparse + * matrix containing the real parts of the input matrix. + */ static BaseMatrixSparseMatrix_Type get(const BaseMatrixSparseMatrix_Type &input) { @@ -131,6 +282,19 @@ template struct GetRealFromComplexSparseMatrix { + /** + * @brief Returns the input sparse matrix as it is when it is not complex. + * + * This static function simply returns the input sparse matrix without any + * changes, as it is assumed to be a matrix of real numbers. + * + * @tparam T The type of the elements in the sparse matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input sparse matrix containing real numbers. + * @return BaseMatrixSparseMatrix_Type The input + * sparse matrix itself. + */ static BaseMatrixSparseMatrix_Type get(const BaseMatrixSparseMatrix_Type &input) { @@ -146,6 +310,20 @@ template struct GetImagFromComplexSparseMatrix { + /** + * @brief Extracts the imaginary part from a sparse matrix of complex numbers. + * + * This static function takes a sparse matrix of complex numbers as input and + * returns a sparse matrix containing only the imaginary parts of each + * element, preserving the original matrix dimensions. + * + * @tparam T The type of the imaginary part of the complex numbers. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input sparse matrix containing complex numbers. + * @return BaseMatrixSparseMatrix_Type A sparse + * matrix containing the imaginary parts of the input matrix. + */ static BaseMatrixSparseMatrix_Type get(const BaseMatrixSparseMatrix_Type &input) { @@ -158,6 +336,20 @@ template struct GetImagFromComplexSparseMatrix { + /** + * @brief Returns an empty sparse matrix when the input is not complex. + * + * This static function returns an empty sparse matrix of type T with + * dimensions M x N when the input matrix is not complex, as there are no + * imaginary parts to extract. + * + * @tparam T The type of the elements in the sparse matrix. + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + * @param input The input sparse matrix containing real numbers. + * @return BaseMatrixSparseMatrix_Type An empty + * sparse matrix of type T with dimensions M x N. + */ static BaseMatrixSparseMatrix_Type get(const BaseMatrixSparseMatrix_Type &input) { From 07d16725fde7999b686c4bc2d2876e0f2721da87 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 11:40:24 +0900 Subject: [PATCH 22/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_concatenate.hpp | 686 ++++++++++++++++++++++ 1 file changed, 686 insertions(+) diff --git a/python_numpy/python_numpy_concatenate.hpp b/python_numpy/python_numpy_concatenate.hpp index abb370a..20029ea 100644 --- a/python_numpy/python_numpy_concatenate.hpp +++ b/python_numpy/python_numpy_concatenate.hpp @@ -1,3 +1,31 @@ +/** + * @file python_numpy_concatenate.hpp + * @brief Provides template utilities for concatenating matrices in a manner + * similar to NumPy's concatenate functionality. + * + * This header defines a set of template functions and type aliases within the + * PythonNumpy namespace to perform vertical and horizontal concatenation of + * matrices. The concatenation supports various matrix types, including dense, + * diagonal, and sparse matrices, and handles all combinations of these types. + * The resulting matrix type is deduced at compile time based on the input + * types, ensuring efficient and type-safe concatenation. + * + * The main features include: + * - Vertical concatenation (stacking matrices row-wise) for all combinations of + * dense, diagonal, and sparse matrices. + * - Horizontal concatenation (stacking matrices column-wise) for all + * combinations of dense, diagonal, and sparse matrices. + * - Type deduction utilities for determining the result type of concatenation + * at compile time. + * - Block 2x2 concatenation, allowing construction of a larger matrix from four + * submatrices. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_CONCATENATE_HPP__ #define __PYTHON_NUMPY_CONCATENATE_HPP__ @@ -11,6 +39,25 @@ namespace PythonNumpy { /* Concatenate vertically */ + +/** + * @brief Updates a matrix Y to be the vertical concatenation of matrices A and + * B. + * + * This function takes two input matrices, A and B, with the same number of + * columns (N) and concatenates them vertically to form the output matrix Y. The + * resulting matrix Y will have (M + P) rows and N columns, where M and P are + * the number of rows in A and B, respectively. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A, B, and Y. + * @tparam P The number of columns in matrix B. + * @param[out] Y The output matrix to store the vertically concatenated result + * (size: (M + P) x N). + * @param[in] A The first input matrix (size: M x N). + * @param[in] B The second input matrix (size: P x N). + */ template inline void update_vertically_concatenated_matrix(Matrix &Y, @@ -21,6 +68,23 @@ update_vertically_concatenated_matrix(Matrix &Y, B.matrix); } +/** + * @brief Concatenates two matrices A and B vertically to form a new matrix. + * + * This function takes two input matrices, A and B, with the same number of + * columns (N) and concatenates them vertically to form a new matrix. The + * resulting matrix will have (M + P) rows and N columns, where M and P are the + * number of rows in A and B, respectively. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A, B, and the result. + * @tparam P The number of columns in matrix B. + * @param[in] A The first input matrix (size: M x N). + * @param[in] B The second input matrix (size: P x N). + * @return A new matrix containing the vertically concatenated result (size: + * (M + P) x N). + */ template inline auto concatenate_vertically(const Matrix &A, const Matrix &B) @@ -30,6 +94,23 @@ inline auto concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of a dense + * matrix A and a diagonal matrix B. + * + * This function takes a dense matrix A and a diagonal matrix B, and updates the + * sparse matrix Y to be their vertical concatenation. The resulting matrix Y + * will have (M + N) rows and N columns, where M is the number of rows in A and + * N is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + N) x N). + * @param[in] A The input dense matrix (size: M x N). + * @param[in] B The input diagonal matrix (size: N x N). + */ template inline void update_vertically_concatenated_matrix( Matrix inline auto concatenate_vertically(const Matrix &A, const Matrix &B) @@ -54,6 +152,24 @@ inline auto concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of a dense + * matrix A and a sparse matrix B. + * + * This function takes a dense matrix A and a sparse matrix B, and updates the + * sparse matrix Y to be their vertical concatenation. The resulting matrix Y + * will have (M + P) rows and N columns, where M is the number of rows in A and + * P is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam P The number of columns in matrix B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + P) x N). + * @param[in] A The input dense matrix (size: M x N). + * @param[in] B The input sparse matrix (size: P x N). + */ template inline void update_vertically_concatenated_matrix( @@ -67,6 +183,24 @@ inline void update_vertically_concatenated_matrix( B.matrix); } +/** + * @brief Concatenates a dense matrix A and a sparse matrix B vertically to form + * a new sparse matrix. + * + * This function takes a dense matrix A and a sparse matrix B, and returns a new + * sparse matrix that is their vertical concatenation. The resulting matrix will + * have (M + P) rows and N columns, where M is the number of rows in A and P is + * the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam P The number of columns in matrix B. + * @param[in] A The input dense matrix (size: M x N). + * @param[in] B The input sparse matrix (size: P x N). + * @return A new sparse matrix containing the vertically concatenated result + * (size: (M + P) x N). + */ template inline auto @@ -82,6 +216,23 @@ concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of a + * diagonal matrix A and a dense matrix B. + * + * This function takes a diagonal matrix A and a dense matrix B, and updates the + * sparse matrix Y to be their vertical concatenation. The resulting matrix Y + * will have (M + P) rows and M columns, where M is the number of rows in A and + * P is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + P) x M). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input dense matrix (size: P x M). + */ template inline void update_vertically_concatenated_matrix( Matrix inline auto concatenate_vertically(const Matrix &A, const Matrix &B) @@ -106,6 +274,21 @@ inline auto concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of a + * diagonal matrix A and a diagonal matrix B. + * + * This function takes two diagonal matrices A and B, and updates the sparse + * matrix Y to be their vertical concatenation. The resulting matrix Y will have + * (2 * M) rows and M columns, where M is the number of rows in both A and B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A and B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (2 * M) x M). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input diagonal matrix (size: M x M). + */ template inline void update_vertically_concatenated_matrix( Matrix inline auto concatenate_vertically(const Matrix &A, const Matrix &B) @@ -130,6 +328,23 @@ inline auto concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of a + * diagonal matrix A and a sparse matrix B. + * + * This function takes a diagonal matrix A and a sparse matrix B, and updates + * the sparse matrix Y to be their vertical concatenation. The resulting matrix + * Y will have (M + P) rows and M columns, where M is the number of rows in A + * and P is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam P The number of columns in matrix B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + P) x M). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input sparse matrix (size: P x M). + */ template inline void update_vertically_concatenated_matrix( Matrix inline auto concatenate_vertically(const Matrix &A, @@ -156,6 +388,24 @@ concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of two + * sparse matrices A and B. + * + * This function takes two sparse matrices A and B, and updates the sparse + * matrix Y to be their vertical concatenation. The resulting matrix Y will have + * (M + P) rows and N columns, where M is the number of rows in A and P is the + * number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam P The number of columns in matrix B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + P) x N). + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input sparse matrix (size: P x N). + */ template inline void update_vertically_concatenated_matrix( @@ -169,6 +419,24 @@ inline void update_vertically_concatenated_matrix( B.matrix); } +/** + * @brief Concatenates a sparse matrix A and a dense matrix B vertically to form + * a new sparse matrix. + * + * This function takes a sparse matrix A and a dense matrix B, and returns a new + * sparse matrix that is their vertical concatenation. The resulting matrix will + * have (M + P) rows and N columns, where M is the number of rows in A and P is + * the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam P The number of columns in matrix B. + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input dense matrix (size: P x N). + * @return A new sparse matrix containing the vertically concatenated result + * (size: (M + P) x N). + */ template inline auto @@ -184,6 +452,23 @@ concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of a sparse + * matrix A and a diagonal matrix B. + * + * This function takes a sparse matrix A and a diagonal matrix B, and updates + * the sparse matrix Y to be their vertical concatenation. The resulting matrix + * Y will have (M + N) rows and N columns, where M is the number of rows in A + * and N is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + N) x N). + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input diagonal matrix (size: N x N). + */ template inline void update_vertically_concatenated_matrix( Matrix inline auto concatenate_vertically(const Matrix &A, @@ -210,6 +512,24 @@ concatenate_vertically(const Matrix &A, Base::Matrix::concatenate_vertically(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the vertical concatenation of two + * sparse matrices A and B. + * + * This function takes two sparse matrices A and B, and updates the sparse + * matrix Y to be their vertical concatenation. The resulting matrix Y will have + * (M + P) rows and N columns, where M is the number of rows in A and P is the + * number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam P The number of columns in matrix B. + * @param[out] Y The output sparse matrix to store the vertically concatenated + * result (size: (M + P) x N). + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input sparse matrix (size: P x N). + */ template inline void update_vertically_concatenated_matrix( @@ -223,6 +543,24 @@ inline void update_vertically_concatenated_matrix( B.matrix); } +/** + * @brief Concatenates two sparse matrices A and B vertically to form a new + * sparse matrix. + * + * This function takes two sparse matrices A and B, and returns a new sparse + * matrix that is their vertical concatenation. The resulting matrix will have + * (M + P) rows and N columns, where M is the number of rows in A and P is the + * number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam P The number of columns in matrix B. + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input sparse matrix (size: P x N). + * @return A new sparse matrix containing the vertically concatenated result + * (size: (M + P) x N). + */ template inline auto @@ -239,6 +577,25 @@ concatenate_vertically(const Matrix &A, } /* Concatenate horizontally */ + +/** + * @brief Updates a matrix Y to be the horizontal concatenation of matrices A + * and B. + * + * This function takes two input matrices, A and B, with the same number of rows + * (M) and concatenates them horizontally to form the output matrix Y. The + * resulting matrix Y will have M rows and (N + L) columns, where N and L are + * the number of columns in A and B, respectively. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A, B, and Y. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param[out] Y The output matrix to store the horizontally concatenated result + * (size: M x (N + L)). + * @param[in] A The first input matrix (size: M x N). + * @param[in] B The second input matrix (size: M x L). + */ template inline void update_horizontally_concatenated_matrix(Matrix &Y, @@ -249,6 +606,23 @@ update_horizontally_concatenated_matrix(Matrix &Y, B.matrix); } +/** + * @brief Concatenates two matrices A and B horizontally to form a new matrix. + * + * This function takes two input matrices, A and B, with the same number of rows + * (M) and concatenates them horizontally to form a new matrix. The resulting + * matrix will have M rows and (N + L) columns, where N and L are the number of + * columns in A and B, respectively. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A, B, and the result. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param[in] A The first input matrix (size: M x N). + * @param[in] B The second input matrix (size: M x L). + * @return A new matrix containing the horizontally concatenated result (size: + * M x (N + L)). + */ template inline auto concatenate_horizontally(const Matrix &A, const Matrix &B) @@ -258,6 +632,23 @@ inline auto concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * dense matrix A and a diagonal matrix B. + * + * This function takes a dense matrix A and a diagonal matrix B, and updates the + * sparse matrix Y to be their horizontal concatenation. The resulting matrix Y + * will have M rows and (M + N) columns, where M is the number of rows in A and + * N is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (M + N)). + * @param[in] A The input dense matrix (size: M x N). + * @param[in] B The input diagonal matrix (size: M x M). + */ template inline void update_horizontally_concatenated_matrix( Matrix inline auto concatenate_horizontally(const Matrix &A, const Matrix &B) @@ -282,6 +690,23 @@ inline auto concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * diagonal matrix A and a dense matrix B. + * + * This function takes a diagonal matrix A and a dense matrix B, and updates the + * sparse matrix Y to be their horizontal concatenation. The resulting matrix Y + * will have M rows and (M + N) columns, where M is the number of rows in A and + * N is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (M + N)). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input dense matrix (size: M x N). + */ template inline void update_horizontally_concatenated_matrix( @@ -295,6 +720,24 @@ inline void update_horizontally_concatenated_matrix( B.matrix); } +/** + * @brief Concatenates a dense matrix A and a sparse matrix B horizontally to + * form a new sparse matrix. + * + * This function takes a dense matrix A and a sparse matrix B, and returns a new + * sparse matrix that is their horizontal concatenation. The resulting matrix + * will have M rows and (N + L) columns, where M is the number of rows in A and + * L is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam L The number of columns in matrix B. + * @param[in] A The input dense matrix (size: M x N). + * @param[in] B The input sparse matrix (size: M x L). + * @return A new sparse matrix containing the horizontally concatenated result + * (size: M x (N + L)). + */ template inline auto @@ -310,6 +753,22 @@ concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * diagonal matrix A and a diagonal matrix B. + * + * This function takes two diagonal matrices A and B, and updates the sparse + * matrix Y to be their horizontal concatenation. The resulting matrix Y will + * have M rows and (2 * M) columns, where M is the number of rows in both A and + * B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A and B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (2 * M)). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input diagonal matrix (size: M x M). + */ template inline void update_horizontally_concatenated_matrix( Matrix inline auto concatenate_horizontally(const Matrix &A, const Matrix &B) @@ -334,6 +810,22 @@ inline auto concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * diagonal matrix A and a diagonal matrix B. + * + * This function takes two diagonal matrices A and B, and updates the sparse + * matrix Y to be their horizontal concatenation. The resulting matrix Y will + * have M rows and (2 * M) columns, where M is the number of rows in both A and + * B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A and B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (2 * M)). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input diagonal matrix (size: M x M). + */ template inline void update_horizontally_concatenated_matrix( Matrix inline auto concatenate_horizontally(const Matrix &A, const Matrix &B) @@ -358,6 +865,23 @@ inline auto concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * diagonal matrix A and a sparse matrix B. + * + * This function takes a diagonal matrix A and a sparse matrix B, and updates + * the sparse matrix Y to be their horizontal concatenation. The resulting + * matrix Y will have M rows and (M + N) columns, where M is the number of rows + * in A and N is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (M + N)). + * @param[in] A The input diagonal matrix (size: M x M). + * @param[in] B The input sparse matrix (size: M x N). + */ template inline void update_horizontally_concatenated_matrix( Matrix inline auto concatenate_horizontally(const Matrix &A, @@ -384,6 +925,24 @@ concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * sparse matrix A and a dense matrix B. + * + * This function takes a sparse matrix A and a dense matrix B, and updates the + * sparse matrix Y to be their horizontal concatenation. The resulting matrix Y + * will have M rows and (N + L) columns, where M is the number of rows in A and + * L is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam L The number of roes in matrix A. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (N + L)). + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input dense matrix (size: M x L). + */ template inline void update_horizontally_concatenated_matrix( @@ -397,6 +956,24 @@ inline void update_horizontally_concatenated_matrix( B.matrix); } +/** + * @brief Concatenates a sparse matrix A and a dense matrix B horizontally to + * form a new sparse matrix. + * + * This function takes a sparse matrix A and a dense matrix B, and returns a new + * sparse matrix that is their horizontal concatenation. The resulting matrix + * will have M rows and (N + L) columns, where M is the number of rows in A and + * L is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @tparam L The number of rows in matrix B. + * @param[in] A The input sparse matrix (size: M x L). + * @param[in] B The input dense matrix (size: M x N). + * @return A new sparse matrix containing the horizontally concatenated result + * (size: M x (N + L)). + */ template inline auto @@ -412,6 +989,23 @@ concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of a + * sparse matrix A and a diagonal matrix B. + * + * This function takes a sparse matrix A and a diagonal matrix B, and updates + * the sparse matrix Y to be their horizontal concatenation. The resulting + * matrix Y will have M rows and (M + N) columns, where M is the number of rows + * in A and N is the number of rows in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrices A and B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (M + N)). + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input diagonal matrix (size: M x M). + */ template inline void update_horizontally_concatenated_matrix( Matrix inline auto concatenate_horizontally(const Matrix &A, @@ -438,6 +1049,24 @@ concatenate_horizontally(const Matrix &A, Base::Matrix::concatenate_horizontally(A.matrix, B.matrix)); } +/** + * @brief Updates a sparse matrix Y to be the horizontal concatenation of two + * sparse matrices A and B. + * + * This function takes two sparse matrices A and B, and updates the sparse + * matrix Y to be their horizontal concatenation. The resulting matrix Y will + * have M rows and (N + L) columns, where M is the number of rows in A and B, N + * is the number of columns in A, and L is the number of columns in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A, B, and Y. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param[out] Y The output sparse matrix to store the horizontally concatenated + * result (size: M x (N + L)). + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input sparse matrix (size: M x L). + */ template inline void update_horizontally_concatenated_matrix( @@ -451,6 +1080,24 @@ inline void update_horizontally_concatenated_matrix( B.matrix); } +/** + * @brief Concatenates two sparse matrices A and B horizontally to form a new + * sparse matrix. + * + * This function takes two sparse matrices A and B, and returns a new sparse + * matrix that is their horizontal concatenation. The resulting matrix will have + * M rows and (N + L) columns, where M is the number of rows in A and B, N is + * the number of columns in A, and L is the number of columns in B. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrices A, B, and the result. + * @tparam N The number of rows in matrix A. + * @tparam L The number of rows in matrix B. + * @param[in] A The input sparse matrix (size: M x N). + * @param[in] B The input sparse matrix (size: M x L). + * @return A new sparse matrix containing the horizontally concatenated result + * (size: M x (N + L)). + */ template inline auto @@ -481,6 +1128,26 @@ using ConcatenateBlock2X2_Type = ConcatenateHorizontally_Type, ConcatenateVertically_Type>; +/** + * @brief Updates a concatenated block 2x2 matrix Y with matrices A, B, C, and + * D. + * + * This function takes four matrices A, B, C, and D, and updates the + * concatenated block 2x2 matrix Y to be their concatenation. The resulting + * matrix Y will have (M + P) rows and (N + L) columns, where M is the number of + * rows in A and C, N is the number of columns in A and B, P is the number of + * rows in B and D, and L is the number of columns in B and D. + * + * @tparam A_Type The type of matrix A. + * @tparam B_Type The type of matrix B. + * @tparam C_Type The type of matrix C. + * @tparam D_Type The type of matrix D. + * @param[out] Y The output concatenated block 2x2 matrix to store the result. + * @param[in] A The input matrix A. + * @param[in] B The input matrix B. + * @param[in] C The input matrix C. + * @param[in] D The input matrix D. + */ template inline void update_block_2x2_concatenated_matrix( ConcatenateBlock2X2_Type &Y, @@ -495,6 +1162,25 @@ inline void update_block_2x2_concatenated_matrix( B_v_D.matrix); } +/** + * @brief Concatenates four matrices A, B, C, and D into a block 2x2 matrix. + * + * This function takes four matrices A, B, C, and D, and returns a new + * concatenated block 2x2 matrix that is their concatenation. The resulting + * matrix will have (M + P) rows and (N + L) columns, where M is the number of + * rows in A and C, N is the number of columns in A and B, P is the number of + * rows in B and D, and L is the number of columns in B and D. + * + * @tparam A_Type The type of matrix A. + * @tparam B_Type The type of matrix B. + * @tparam C_Type The type of matrix C. + * @tparam D_Type The type of matrix D. + * @param[in] A The input matrix A. + * @param[in] B The input matrix B. + * @param[in] C The input matrix C. + * @param[in] D The input matrix D. + * @return A new concatenated block 2x2 matrix containing the result. + */ template inline auto concatenate_block_2x2(const A_Type &A, const B_Type &B, const C_Type &C, const D_Type &D) From de74d82c79d1e46990aa6ff32880c9b86a2c2641 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 12:16:24 +0900 Subject: [PATCH 23/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_linalg_cholesky.hpp | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/python_numpy/python_numpy_linalg_cholesky.hpp b/python_numpy/python_numpy_linalg_cholesky.hpp index 14c8fc1..cfd1042 100644 --- a/python_numpy/python_numpy_linalg_cholesky.hpp +++ b/python_numpy/python_numpy_linalg_cholesky.hpp @@ -1,3 +1,22 @@ +/** + * @file python_numpy_linalg_cholesky.hpp + * @brief Provides a C++ implementation of Cholesky decomposition solvers, + * inspired by Python's NumPy linear algebra routines. + * + * This header defines the `PythonNumpy` namespace, which contains the + * `LinalgSolverCholesky` template class and related utilities for performing + * Cholesky decomposition on dense, diagonal, and sparse matrices. The + * implementation is designed to be type-safe, supporting both `float` and + * `double` value types, and is intended for use in numerical and scientific + * computing applications where efficient linear algebra operations are + * required. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_LINALG_CHOLESKY_HPP__ #define __PYTHON_NUMPY_LINALG_CHOLESKY_HPP__ @@ -11,6 +30,27 @@ namespace PythonNumpy { constexpr double DEFAULT_DIVISION_MIN_LINALG_CHOLESKY = 1.0e-10; +/** + * @brief Cholesky decomposition linear algebra solver class template. + * + * This class provides methods to perform Cholesky decomposition on matrices of + * various formats (dense, diagonal, sparse) and solve linear systems using the + * decomposed matrix. The decomposition is only supported for real-valued (float + * or double) matrices and does not support complex types. + * + * @tparam A_Type Matrix type traits, must define Value_Type, COLS, ROWS, and + * SparseAvailable_Type. + * + * @note The class supports copy and move semantics. + * + * @section Usage + * - Use the `solve` method with a supported matrix type to obtain the + * upper-triangular Cholesky factor. + * - Use `set_division_min` to set the minimum divisor threshold for numerical + * stability. + * - Use `get_zero_div_flag` to check if a zero division was detected during + * decomposition. + */ template class LinalgSolverCholesky { public: /* Type */ @@ -85,7 +125,26 @@ template class LinalgSolverCholesky { return *this; } +public: /* Solve function */ + + /** + * @brief + * upper triangular sparse matrix. + * + * This function performs Cholesky decomposition on the input dense square + * matrix A, stores the decomposed matrix, and sets the values of the upper + * triangular part in a sparse matrix format. The resulting upper triangular + * sparse matrix is returned. + * + * @tparam _T The data type of the matrix elements (e.g., float, double). + * @tparam A_Type::COLS The number of columns (and rows) of the square matrix + * A. + * @param A The input dense square matrix to decompose. + * @return Matrix The upper triangular matrix in sparse + * format resulting from the Cholesky decomposition. + */ inline auto solve(const Matrix &A) -> Matrix { @@ -104,6 +163,22 @@ template class LinalgSolverCholesky { this->_cholesky_decomposed_triangular); } + /** + * @brief + * diagonal matrix. + * + * This function performs Cholesky decomposition on the input diagonal matrix + * A, stores the decomposed matrix, and returns a diagonal matrix containing + * the square roots of the diagonal elements of A. The resulting matrix is + * suitable for solving linear systems where A is a diagonal matrix. + * + * @tparam _T The data type of the matrix elements (e.g., float, double). + * @tparam A_Type::COLS The number of columns (and rows) of the diagonal + * matrix A. + * @param A The input diagonal matrix to decompose. + * @return Matrix The diagonal matrix resulting + * from the Cholesky decomposition. + */ inline auto solve(const Matrix &A) -> Matrix { @@ -118,6 +193,23 @@ template class LinalgSolverCholesky { return Matrix(Diag); } + /** + * @brief + * sparse matrix. + * + * This function performs Cholesky decomposition on the input sparse square + * matrix A, stores the decomposed matrix, and sets the values of the upper + * triangular part in a sparse matrix format. The resulting upper triangular + * sparse matrix is returned. + * + * @tparam _T The data type of the matrix elements (e.g., float, double). + * @tparam A_Type::COLS The number of columns (and rows) of the square matrix + * A. + * @param A The input sparse square matrix to decompose. + * @return Matrix The upper triangular matrix in sparse + * format resulting from the Cholesky decomposition. + */ inline auto solve(const Matrix &A) -> Matrix class LinalgSolverCholesky { public: /* Function */ + + /** + * @brief Checks if a zero division occurred during the Cholesky + * decomposition. + * + * This function returns a boolean flag indicating whether a zero division + * was detected during the decomposition process. If true, it indicates that + * the input matrix was not positive definite. + * + * @return true if a zero division occurred, false otherwise. + */ inline bool get_zero_div_flag() const { return this->_zero_div_flag; } + /** + * @brief Sets the minimum division threshold for numerical stability. + * + * This function allows the user to set a custom minimum value for division + * operations during the Cholesky decomposition. This can help avoid numerical + * instability in cases where matrix elements are very small. + * + * @param division_min_in The minimum value to set for division operations. + */ inline void set_division_min(const _T &division_min_in) { this->division_min = division_min_in; } @@ -171,6 +283,17 @@ template class LinalgSolverCholesky { }; /* make LinalgSolverCholesky */ + +/** + * @brief Factory function to create an instance of LinalgSolverCholesky. + * + * This function creates and returns an instance of the LinalgSolverCholesky + * class template for the specified matrix type A_Type. + * + * @tparam A_Type The matrix type traits, must define Value_Type, COLS, ROWS, + * and SparseAvailable_Type. + * @return An instance of LinalgSolverCholesky. + */ template inline auto make_LinalgSolverCholesky(void) -> LinalgSolverCholesky { From 9bdcea210485eca6192598bb5d0b5514bed000ca Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 12:34:56 +0900 Subject: [PATCH 24/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_linalg_eig.hpp | 622 +++++++++++++++++++++++ 1 file changed, 622 insertions(+) diff --git a/python_numpy/python_numpy_linalg_eig.hpp b/python_numpy/python_numpy_linalg_eig.hpp index 3a0d86d..789c024 100644 --- a/python_numpy/python_numpy_linalg_eig.hpp +++ b/python_numpy/python_numpy_linalg_eig.hpp @@ -1,3 +1,19 @@ +/** + * @file python_numpy_linalg_eig.hpp + * @brief Linear algebra solvers for eigenvalues and eigenvectors, supporting + * dense, diagonal, and sparse matrices with real and complex types. + * + * This header provides template classes and utility functions for computing + * eigenvalues and eigenvectors of matrices in various storage formats (dense, + * diagonal, sparse) and for both real and complex number types. It is designed + * to be used as part of a Python/Numpy-like C++ numerical library. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_LINALG_EIG_HPP__ #define __PYTHON_NUMPY_LINALG_EIG_HPP__ @@ -20,6 +36,16 @@ using EigenVectors_Type = Matrix; } // namespace ForLinalgSolverEigReal /* Linalg solver for Real Eigen values and vectors of Dense Matrix */ + +/** * @brief Linalg solver for Real Eigen values and vectors of Dense Matrix. + * + * This class provides methods to compute eigenvalues and eigenvectors of dense + * matrices with real number types (float or double). It uses the Eigen library + * for the underlying computations. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square matrix. + */ template class LinalgSolverEigRealDense { public: /* Type */ @@ -58,50 +84,151 @@ template class LinalgSolverEigRealDense { return *this; } +public: /* Solve function */ + + /** + * @brief Computes the eigenvalues of the given matrix. + * + * This function uses the internal Eigen solver to compute the eigenvalues + * of the matrix contained in the input parameter A. The results are stored + * within the solver for later retrieval. + * + * @tparam A_Type Type of the input matrix wrapper. + * @param A An object containing the matrix for which eigenvalues are to be + * computed. + */ inline void solve_eigen_values(const A_Type &A) { this->_Eigen_solver.solve_eigen_values(A.matrix); } + /** + * @brief Continues solving for eigenvalues if the solver supports it. + * + * This function allows the solver to continue computing eigenvalues if it + * has not yet completed the process. It is useful for iterative solvers. + */ inline void continue_solving_eigen_values(void) { this->_Eigen_solver.continue_solving_eigen_values(); } + /** + * @brief Computes the eigenvectors of the given matrix. + * + * This function uses the internal Eigen solver to compute the eigenvectors + * of the matrix contained in the input parameter A. The results are stored + * within the solver for later retrieval. + * + * @tparam A_Type Type of the input matrix wrapper. + * @param A An object containing the matrix for which eigenvectors are to be + * computed. + */ inline void solve_eigen_vectors(const A_Type &A) { this->_Eigen_solver.solve_eigen_vectors(A.matrix); } /* Get */ + + /** + * @brief Retrieves the computed eigenvalues. + * + * This function returns the eigenvalues computed by the solver as a + * ForLinalgSolverEigReal::EigenValues_Type object. + * + * @return A ForLinalgSolverEigReal::EigenValues_Type object containing the + * eigenvalues. + */ inline auto get_eigen_values(void) -> ForLinalgSolverEigReal::EigenValues_Type { return ForLinalgSolverEigReal::EigenValues_Type( Base::Matrix::Matrix(this->_Eigen_solver.get_eigen_values())); } + /** + * @brief Retrieves the computed eigenvectors. + * + * This function returns the eigenvectors computed by the solver as a + * ForLinalgSolverEigReal::EigenVectors_Type object. + * + * @return A ForLinalgSolverEigReal::EigenVectors_Type object containing the + * eigenvectors. + */ inline auto get_eigen_vectors(void) -> ForLinalgSolverEigReal::EigenVectors_Type { return ForLinalgSolverEigReal::EigenVectors_Type( this->_Eigen_solver.get_eigen_vectors()); } + /** + * @brief Retrieves the maximum number of iterations allowed for the solver. + * + * This function returns the maximum number of iterations that the solver is + * allowed to perform when computing eigenvalues and eigenvectors. + * + * @return The maximum number of iterations. + */ inline std::size_t get_iteration_max(void) { return this->_Eigen_solver.iteration_max; } /* Set */ + + /** + * @brief Sets the maximum number of iterations for the solver. + * + * This function allows the user to specify the maximum number of iterations + * that the solver should perform when computing eigenvalues and + * eigenvectors. + * + * @param iteration_max The maximum number of iterations to set. + */ inline void set_iteration_max(std::size_t iteration_max) { this->_Eigen_solver.iteration_max = iteration_max; } + /** + * @brief Sets the minimum division value for the Eigen solver. + * + * This function assigns the specified value to the `division_min` parameter + * of the internal Eigen solver. The `division_min` parameter is typically + * used to avoid division by very small numbers, which can lead to numerical + * instability. + * + * @tparam T The type of the division minimum value. + * @param division_min_in The minimum value to be used for divisions in the + * Eigen solver. + */ inline void set_division_min(const T &division_min_in) { this->_Eigen_solver.division_min = division_min_in; } + /** + * @brief Sets a small value threshold for numerical stability. + * + * This function allows the user to specify a small value that can be used + * to avoid numerical instability during computations, such as division by + * very small numbers. + * + * @param small_value_in The small value to set for numerical stability. + */ inline void set_small_value(const T &small_value_in) { this->_Eigen_solver.small_value = small_value_in; } /* Check */ + + /** + * @brief Checks the validity of the eigenvalues and eigenvectors for the + * given matrix. + * + * This function checks whether the computed eigenvalues and eigenvectors + * are valid for the provided matrix A. It returns a + * ForLinalgSolverEigReal::EigenVectors_Type object containing the results. + * + * @param A The input matrix for which validity is to be checked. + * @return A ForLinalgSolverEigReal::EigenVectors_Type object containing the + * validity check results. + */ inline auto check_validity(const A_Type &A) -> ForLinalgSolverEigReal::EigenVectors_Type { @@ -123,6 +250,17 @@ template class LinalgSolverEigRealDense { }; /* Linalg solver for Real Eigen values and vectors of Diag Matrix */ + +/** + * @brief Linalg solver for Real Eigen values and vectors of Diag Matrix. + * + * This class provides methods to compute eigenvalues and eigenvectors of + * diagonal matrices with real number types (float or double). It is designed + * to work with matrices that have a diagonal structure. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square diagonal matrix. + */ template class LinalgSolverEigRealDiag { public: /* Type */ @@ -164,19 +302,54 @@ template class LinalgSolverEigRealDiag { public: /* Function */ + + /** + * @brief Computes the eigenvalues of the given diagonal matrix. + * + * This function extracts the diagonal elements of the matrix A and stores + * them as eigenvalues in the internal _eigen_values member variable. + * + * @param A The input diagonal matrix for which eigenvalues are to be + * computed. + */ inline void solve_eigen_values(const A_Type &A) { this->_eigen_values = Base::Matrix::Matrix(A.matrix.data); } + /** + * @brief Continues solving for eigenvalues if the solver supports it. + * + * This function is a placeholder for compatibility with the interface but + * does not perform any action since eigenvalues of diagonal matrices are + * directly available. + */ inline void solve_eigen_vectors(const A_Type &A) { this->_eigen_values = Base::Matrix::Matrix(A.matrix.data); } /* Get */ + + /** + * @brief Retrieves the computed eigenvalues. + * + * This function returns the eigenvalues computed by the solver as an + * EigenValues_Type object. + * + * @return An EigenValues_Type object containing the eigenvalues. + */ inline auto get_eigen_values(void) -> EigenValues_Type { return EigenValues_Type(this->_eigen_values); } + /** + * @brief Retrieves the eigenvectors of the diagonal matrix. + * + * Since the eigenvectors of a diagonal matrix are simply the identity + * matrix, this function returns an EigenVectors_Type object representing + * the identity matrix. + * + * @return An EigenVectors_Type object representing the identity matrix. + */ inline auto get_eigen_vectors(void) -> EigenVectors_Type { return EigenVectors_Type(Base::Matrix::DiagMatrix::identity()); } @@ -195,6 +368,19 @@ template class LinalgSolverEigRealDiag { }; /* Linalg solver for Real Eigen values and vectors of Sparse Matrix */ + +/** @brief Linalg solver for Real Eigen values and vectors of Sparse Matrix. + * + * This class provides methods to compute eigenvalues and eigenvectors of + * sparse matrices with real number types (float or double). It is designed to + * work with matrices that have a sparse structure, utilizing the Eigen library + * for efficient computations. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square sparse matrix. + * @tparam SparseAvailable A type indicating whether sparse operations are + * available. + */ template class LinalgSolverEigRealSparse { public: @@ -237,27 +423,73 @@ class LinalgSolverEigRealSparse { } /* Solve method */ + + /** + * @brief Computes the eigenvalues of the given sparse matrix. + * + * This function uses the internal Eigen solver to compute the eigenvalues + * of the sparse matrix contained in the input parameter A. The results are + * stored within the solver for later retrieval. + * + * @param A An object containing the sparse matrix for which eigenvalues are + * to be computed. + */ inline void solve_eigen_values(const A_Type &A) { this->_Eigen_solver.solve_eigen_values( Base::Matrix::output_dense_matrix(A.matrix)); } + /** + * @brief Continues solving for eigenvalues if the solver supports it. + * + * This function allows the solver to continue computing eigenvalues if it + * has not yet completed the process. It is useful for iterative solvers. + */ inline void continue_solving_eigen_values(void) { this->_Eigen_solver.continue_solving_eigen_values(); } + /** + * @brief Computes the eigenvectors of the given sparse matrix. + * + * This function uses the internal Eigen solver to compute the eigenvectors + * of the sparse matrix contained in the input parameter A. The results are + * stored within the solver for later retrieval. + * + * @param A An object containing the sparse matrix for which eigenvectors are + * to be computed. + */ inline void solve_eigen_vectors(const A_Type &A) { this->_Eigen_solver.solve_eigen_vectors( Base::Matrix::output_dense_matrix(A.matrix)); } /* Get */ + + /** + * @brief Retrieves the computed eigenvalues. + * + * This function returns the eigenvalues computed by the solver as a + * ForLinalgSolverEigReal::EigenValues_Type object. + * + * @return A ForLinalgSolverEigReal::EigenValues_Type object containing the + * eigenvalues. + */ inline auto get_eigen_values(void) -> ForLinalgSolverEigReal::EigenValues_Type { return ForLinalgSolverEigReal::EigenValues_Type( Base::Matrix::Matrix(this->_Eigen_solver.get_eigen_values())); } + /** + * @brief Retrieves the computed eigenvectors. + * + * This function returns the eigenvectors computed by the solver as a + * ForLinalgSolverEigReal::EigenVectors_Type object. + * + * @return A ForLinalgSolverEigReal::EigenVectors_Type object containing the + * eigenvectors. + */ inline auto get_eigen_vectors(void) -> ForLinalgSolverEigReal::EigenVectors_Type { return ForLinalgSolverEigReal::EigenVectors_Type( @@ -265,19 +497,62 @@ class LinalgSolverEigRealSparse { } /* Set */ + + /** + * @brief Sets the maximum number of iterations for the solver. + * + * This function allows the user to specify the maximum number of iterations + * that the solver should perform when computing eigenvalues and + * eigenvectors. + * + * @param iteration_max The maximum number of iterations to set. + */ inline void set_iteration_max(std::size_t iteration_max) { this->_Eigen_solver.iteration_max = iteration_max; } + /** + * @brief Sets the minimum division value for the Eigen solver. + * + * This function assigns the specified value to the `division_min` parameter + * of the internal Eigen solver. The `division_min` parameter is typically + * used to avoid division by very small numbers, which can lead to numerical + * instability. + * + * @param division_min_in The minimum value to be used for divisions in the + * Eigen solver. + */ inline void set_division_min(const T &division_min_in) { this->_Eigen_solver.division_min = division_min_in; } + /** + * @brief Sets a small value threshold for numerical stability. + * + * This function allows the user to specify a small value that can be used + * to avoid numerical instability during computations, such as division by + * very small numbers. + * + * @param small_value_in The small value to set for numerical stability. + */ inline void set_small_value(const T &small_value_in) { this->_Eigen_solver.small_value = small_value_in; } /* Check */ + + /** + * @brief Checks the validity of the eigenvalues and eigenvectors for the + * given sparse matrix. + * + * This function checks whether the computed eigenvalues and eigenvectors + * are valid for the provided matrix A. It returns a + * ForLinalgSolverEigReal::EigenVectors_Type object containing the results. + * + * @param A The input sparse matrix for which validity is to be checked. + * @return A ForLinalgSolverEigReal::EigenVectors_Type object containing the + * validity check results. + */ inline auto check_validity(const A_Type &A) -> ForLinalgSolverEigReal::EigenVectors_Type { @@ -300,6 +575,17 @@ class LinalgSolverEigRealSparse { }; /* make LinalgSolverEig Real */ + +/** + * @brief Factory function to create a LinalgSolverEigReal instance based on + * the type of matrix provided. + * + * This function uses SFINAE (Substitution Failure Is Not An Error) to determine + * the type of matrix and return the appropriate LinalgSolverEigReal instance. + * + * @tparam A_Type The type of the matrix for which the solver is to be created. + * @return A LinalgSolverEigReal instance suitable for the given matrix type. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> @@ -311,6 +597,17 @@ inline auto make_LinalgSolverEigReal() A_Type::COLS>(); } +/** @brief Factory function to create a LinalgSolverEigReal instance for + * diagonal matrices. + * + * This function is specialized for diagonal matrices and returns a + * LinalgSolverEigRealDiag instance. + * + * @tparam A_Type The type of the diagonal matrix for which the solver is to be + * created. + * @return A LinalgSolverEigRealDiag instance suitable for the given diagonal + * matrix type. + */ template ::value>::type * = nullptr> inline auto make_LinalgSolverEigReal() @@ -321,6 +618,17 @@ inline auto make_LinalgSolverEigReal() A_Type::COLS>(); } +/** @brief Factory function to create a LinalgSolverEigReal instance for + * sparse matrices. + * + * This function is specialized for sparse matrices and returns a + * LinalgSolverEigRealSparse instance. + * + * @tparam A_Type The type of the sparse matrix for which the solver is to be + * created. + * @return A LinalgSolverEigRealSparse instance suitable for the given sparse + * matrix type. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> @@ -350,6 +658,15 @@ using EigenVectors_Type = Matrix, M, M>; /* Linalg solver for Complex Eigen values and vectors of Dense and Sparse Matrix */ + +/** @brief Linalg solver for Complex Eigen values and vectors of Dense Matrix. + * This class provides methods to compute eigenvalues and eigenvectors of dense + * matrices with complex number types (float or double). It uses the Eigen + * library + * for the underlying computations. + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square matrix. + */ template class LinalgSolverEigDense { public: /* Type */ @@ -389,19 +706,56 @@ template class LinalgSolverEigDense { } /* Solve method */ + + /** + * @brief Computes the eigenvalues of the given matrix. + * + * This function uses the internal Eigen solver to compute the eigenvalues + * of the matrix contained in the input parameter A. The results are stored + * within the solver for later retrieval. + * + * @param A An object containing the matrix for which eigenvalues are to be + * computed. + */ inline void solve_eigen_values(const A_Type &A) { this->_Eigen_solver.solve_eigen_values(A.matrix); } + /** + * @brief Continues solving for eigenvalues if the solver supports it. + * + * This function allows the solver to continue computing eigenvalues if it + * has not yet completed the process. It is useful for iterative solvers. + */ inline void continue_solving_eigen_values(void) { this->_Eigen_solver.continue_solving_eigen_values(); } + /** + * @brief Computes the eigenvectors of the given matrix. + * + * This function uses the internal Eigen solver to compute the eigenvectors + * of the matrix contained in the input parameter A. The results are stored + * within the solver for later retrieval. + * + * @param A An object containing the matrix for which eigenvectors are to be + * computed. + */ inline void solve_eigen_vectors(const A_Type &A) { this->_Eigen_solver.solve_eigen_vectors(A.matrix); } /* Get */ + + /** + * @brief Retrieves the computed eigenvalues. + * + * This function returns the eigenvalues computed by the solver as a + * ForLinalgSolverEig::EigenValues_Type object. + * + * @return A ForLinalgSolverEig::EigenValues_Type object containing the + * eigenvalues. + */ inline auto get_eigen_values(void) -> ForLinalgSolverEig::EigenValues_Type { return ForLinalgSolverEig::EigenValues_Type( @@ -409,40 +763,118 @@ template class LinalgSolverEigDense { this->_Eigen_solver.get_eigen_values())); } + /** + * @brief Retrieves the computed eigenvectors. + * + * This function returns the eigenvectors computed by the solver as a + * ForLinalgSolverEig::EigenVectors_Type object. + * + * @return A ForLinalgSolverEig::EigenVectors_Type object containing the + * eigenvectors. + */ inline auto get_eigen_vectors(void) -> ForLinalgSolverEig::EigenVectors_Type { return ForLinalgSolverEig::EigenVectors_Type( this->_Eigen_solver.get_eigen_vectors()); } + /** + * @brief Retrieves the maximum number of iterations allowed for the solver. + * + * This function returns the maximum number of iterations that the solver is + * allowed to perform when computing eigenvalues and eigenvectors. + * + * @return The maximum number of iterations. + */ inline std::size_t get_iteration_max(void) { return this->_Eigen_solver.iteration_max; } /* Set */ + + /** + * @brief Sets the maximum number of iterations for the solver. + * + * This function allows the user to specify the maximum number of iterations + * that the solver should perform when computing eigenvalues and + * eigenvectors. + * + * @param iteration_max The maximum number of iterations to set. + */ inline void set_iteration_max(std::size_t iteration_max) { this->_Eigen_solver.iteration_max = iteration_max; } + /** + * @brief Sets the maximum number of iterations for eigenvector computation. + * + * This function allows the user to specify the maximum number of iterations + * that the solver should perform when computing eigenvectors. + * + * @param iteration_max_for_eigen_vector The maximum number of iterations to + * set for eigenvector computation. + */ inline void set_iteration_max_for_eigen_vector( std::size_t iteration_max_for_eigen_vector) { this->_Eigen_solver.iteration_max_for_eigen_vector = iteration_max_for_eigen_vector; } + /** + * @brief Sets the minimum division value for the Eigen solver. + * + * This function assigns the specified value to the `division_min` parameter + * of the internal Eigen solver. The `division_min` parameter is typically + * used to avoid division by very small numbers, which can lead to numerical + * instability. + * + * @param division_min_in The minimum value to be used for divisions in the + * Eigen solver. + */ inline void set_division_min(const T &division_min_in) { this->_Eigen_solver.division_min = division_min_in; } + /** + * @brief Sets a small value threshold for numerical stability. + * + * This function allows the user to specify a small value that can be used + * to avoid numerical instability during computations, such as division by + * very small numbers. + * + * @param small_value_in The small value to set for numerical stability. + */ inline void set_small_value(const T &small_value_in) { this->_Eigen_solver.small_value = small_value_in; } + /** + * @brief Sets the decay rate for the GMRES method. + * + * This function allows the user to specify the decay rate for the GMRES + * method used in the Eigen solver. The decay rate can affect the convergence + * behavior of the solver. + * + * @param gmres_k_decay_rate_in The decay rate to set for the GMRES method. + */ inline void set_gmres_k_decay_rate(const T &gmres_k_decay_rate_in) { this->_Eigen_solver.gmres_k_decay_rate = gmres_k_decay_rate_in; } /* Check */ + + /** + * @brief Checks the validity of the eigenvalues and eigenvectors for the + * given matrix. + * + * This function checks whether the computed eigenvalues and eigenvectors + * are valid for the provided matrix A. It returns a + * ForLinalgSolverEig::EigenVectors_Type object containing the results. + * + * @param A The input matrix for which validity is to be checked. + * @return A ForLinalgSolverEig::EigenVectors_Type object containing the + * validity check results. + */ inline auto check_validity(const A_Type &A) -> ForLinalgSolverEig::EigenVectors_Type { @@ -464,6 +896,15 @@ template class LinalgSolverEigDense { }; /* Linalg solver for Complex Eigen values and vectors of Diag Matrix */ + +/** @brief Linalg solver for Complex Eigen values and vectors of Diag Matrix. + * This class provides methods to compute eigenvalues and eigenvectors of + * diagonal matrices with complex number types (float or double). It is designed + * to work with matrices that have a diagonal structure. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square diagonal matrix. + */ template class LinalgSolverEigDiag { public: /* Type */ @@ -503,12 +944,31 @@ template class LinalgSolverEigDiag { } /* Solve method */ + + /** + * @brief Computes the eigenvalues of the given diagonal matrix. + * + * This function extracts the diagonal elements of the matrix A and stores + * them as eigenvalues in the internal _eigen_values member variable. + * + * @param A The input diagonal matrix for which eigenvalues are to be + * computed. + */ inline void solve_eigen_values(const Matrix &A) { this->_eigen_values = EigenValues_Type(Base::Matrix::convert_matrix_real_to_complex( Base::Matrix::Matrix(A.matrix.data))); } + /** + * @brief Computes the eigenvectors of the given diagonal matrix. + * + * This function extracts the diagonal elements of the matrix A and stores + * them as eigenvectors in the internal _eigen_values member variable. + * + * @param A The input diagonal matrix for which eigenvectors are to be + * computed. + */ inline void solve_eigen_vectors(const Matrix &A) { this->_eigen_values = EigenValues_Type(Base::Matrix::convert_matrix_real_to_complex( @@ -516,10 +976,28 @@ template class LinalgSolverEigDiag { } /* Get */ + + /** + * @brief Retrieves the computed eigenvalues. + * + * This function returns the eigenvalues computed by the solver as an + * EigenValues_Type object. + * + * @return An EigenValues_Type object containing the eigenvalues. + */ inline auto get_eigen_values(void) -> EigenValues_Type { return EigenValues_Type(this->_eigen_values); } + /** + * @brief Retrieves the eigenvectors of the diagonal matrix. + * + * Since the eigenvectors of a diagonal matrix are simply the identity + * matrix, this function returns an EigenVectors_Type object representing + * the identity matrix. + * + * @return An EigenVectors_Type object representing the identity matrix. + */ inline auto get_eigen_vectors(void) -> EigenVectors_Type { return EigenVectors_Type(Base::Matrix::convert_matrix_real_to_complex( Base::Matrix::DiagMatrix::identity())); @@ -539,6 +1017,18 @@ template class LinalgSolverEigDiag { }; /* Linalg solver for Complex Eigen values and vectors of Sparse Matrix */ + +/** @brief Linalg solver for Complex Eigen values and vectors of Sparse Matrix. + * This class provides methods to compute eigenvalues and eigenvectors of + * sparse matrices with complex number types (float or double). It is designed + * to work with matrices that have a sparse structure, utilizing the Eigen + * library for efficient computations. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square sparse matrix. + * @tparam SparseAvailable A type indicating whether sparse operations are + * available. + */ template class LinalgSolverEigSparse { public: @@ -580,22 +1070,60 @@ class LinalgSolverEigSparse { return *this; } +public: /* Solve method */ + + /** + * @brief Computes the eigenvalues of the given sparse matrix. + * + * This function uses the internal Eigen solver to compute the eigenvalues + * of the sparse matrix contained in the input parameter A. The results are + * stored within the solver for later retrieval. + * + * @param A An object containing the sparse matrix for which eigenvalues are + * to be computed. + */ inline void solve_eigen_values(const A_Type &A) { this->_Eigen_solver.solve_eigen_values( Base::Matrix::output_dense_matrix(A.matrix)); } + /** + * @brief Continues solving for eigenvalues if the solver supports it. + * + * This function allows the solver to continue computing eigenvalues if it + * has not yet completed the process. It is useful for iterative solvers. + */ inline void continue_solving_eigen_values(void) { this->_Eigen_solver.continue_solving_eigen_values(); } + /** + * @brief Computes the eigenvectors of the given sparse matrix. + * + * This function uses the internal Eigen solver to compute the eigenvectors + * of the sparse matrix contained in the input parameter A. The results are + * stored within the solver for later retrieval. + * + * @param A An object containing the sparse matrix for which eigenvectors are + * to be computed. + */ inline void solve_eigen_vectors(const A_Type &A) { this->_Eigen_solver.solve_eigen_vectors( Base::Matrix::output_dense_matrix(A.matrix)); } /* Get */ + + /** + * @brief Retrieves the computed eigenvalues. + * + * This function returns the eigenvalues computed by the solver as a + * ForLinalgSolverEig::EigenValues_Type object. + * + * @return A ForLinalgSolverEig::EigenValues_Type object containing the + * eigenvalues. + */ inline auto get_eigen_values(void) -> ForLinalgSolverEig::EigenValues_Type { return ForLinalgSolverEig::EigenValues_Type( @@ -603,6 +1131,15 @@ class LinalgSolverEigSparse { this->_Eigen_solver.get_eigen_values())); } + /** + * @brief Retrieves the computed eigenvectors. + * + * This function returns the eigenvectors computed by the solver as a + * ForLinalgSolverEig::EigenVectors_Type object. + * + * @return A ForLinalgSolverEig::EigenVectors_Type object containing the + * eigenvectors. + */ inline auto get_eigen_vectors(void) -> ForLinalgSolverEig::EigenVectors_Type { return ForLinalgSolverEig::EigenVectors_Type( @@ -610,25 +1147,77 @@ class LinalgSolverEigSparse { } /* Set */ + + /** + * @brief Sets the maximum number of iterations for the solver. + * + * This function allows the user to specify the maximum number of iterations + * that the solver should perform when computing eigenvalues and + * eigenvectors. + * + * @param iteration_max The maximum number of iterations to set. + */ inline void set_iteration_max(const std::size_t &iteration_max) { this->_Eigen_solver.iteration_max = iteration_max; } + /** + * @brief Sets the maximum number of iterations for eigenvector computation. + * + * This function allows the user to specify the maximum number of iterations + * that the solver should perform when computing eigenvectors. + * + * @param iteration_max_for_eigen_vector The maximum number of iterations to + * set for eigenvector computation. + */ inline void set_iteration_max_for_eigen_vector( const std::size_t &iteration_max_for_eigen_vector) { this->_Eigen_solver.iteration_max_for_eigen_vector = iteration_max_for_eigen_vector; } + /** + * @brief Sets the minimum division value for the Eigen solver. + * + * This function assigns the specified value to the `division_min` parameter + * of the internal Eigen solver. The `division_min` parameter is typically + * used to avoid division by very small numbers, which can lead to numerical + * instability. + * + * @param division_min_in The minimum value to be used for divisions in the + * Eigen solver. + */ inline void set_division_min(const T &division_min_in) { this->_Eigen_solver.division_min = division_min_in; } + /** + * @brief Sets a small value threshold for numerical stability. + * + * This function allows the user to specify a small value that can be used + * to avoid numerical instability during computations, such as division by + * very small numbers. + * + * @param small_value_in The small value to set for numerical stability. + */ inline void set_small_value(const T &small_value_in) { this->_Eigen_solver.small_value = small_value_in; } /* Check */ + + /** + * @brief Checks the validity of the eigenvalues and eigenvectors for the + * given sparse matrix. + * + * This function checks whether the computed eigenvalues and eigenvectors + * are valid for the provided matrix A. It returns a + * ForLinalgSolverEig::EigenVectors_Type object containing the results. + * + * @param A The input sparse matrix for which validity is to be checked. + * @return A ForLinalgSolverEig::EigenVectors_Type object containing the + * validity check results. + */ inline auto check_validity(const A_Type &A) -> ForLinalgSolverEig::EigenVectors_Type { @@ -651,6 +1240,17 @@ class LinalgSolverEigSparse { }; /* make LinalgSolverEig Complex */ + +/** + * @brief Factory function to create a LinalgSolverEig instance based on the + * type of matrix provided. + * + * This function uses SFINAE (Substitution Failure Is Not An Error) to determine + * the type of matrix and return the appropriate LinalgSolverEig instance. + * + * @tparam A_Type The type of the matrix for which the solver is to be created. + * @return A LinalgSolverEig instance suitable for the given matrix type. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> @@ -661,6 +1261,17 @@ inline auto make_LinalgSolverEig() A_Type::COLS>(); } +/** @brief Factory function to create a LinalgSolverEig instance for diagonal + * matrices. + * + * This function is specialized for diagonal matrices and returns a + * LinalgSolverEigDiag instance. + * + * @tparam A_Type The type of the diagonal matrix for which the solver is to be + * created. + * @return A LinalgSolverEigDiag instance suitable for the given diagonal matrix + * type. + */ template ::value>::type * = nullptr> inline auto make_LinalgSolverEig() @@ -670,6 +1281,17 @@ inline auto make_LinalgSolverEig() A_Type::COLS>(); } +/** @brief Factory function to create a LinalgSolverEig instance for sparse + * matrices. + * + * This function is specialized for sparse matrices and returns a + * LinalgSolverEigSparse instance. + * + * @tparam A_Type The type of the sparse matrix for which the solver is to be + * created. + * @return A LinalgSolverEigSparse instance suitable for the given sparse matrix + * type. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> From 78e41a71f824760522cdb7b7175cef30df25daee Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 12:37:31 +0900 Subject: [PATCH 25/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_linalg_lu.hpp | 111 ++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/python_numpy/python_numpy_linalg_lu.hpp b/python_numpy/python_numpy_linalg_lu.hpp index 20a0978..ab4ac7e 100644 --- a/python_numpy/python_numpy_linalg_lu.hpp +++ b/python_numpy/python_numpy_linalg_lu.hpp @@ -1,3 +1,21 @@ +/** + * @file python_numpy_linalg_lu.hpp + * @brief LU Decomposition Solver for Python-like Numpy Linear Algebra in C++ + * + * This file defines the `PythonNumpy` namespace, which provides a generic LU + * decomposition solver for dense, diagonal, and sparse matrices, mimicking + * Python's numpy.linalg functionality in C++. The main class, `LinalgSolverLU`, + * is a template class designed to work with various matrix types, supporting + * both single and double precision floating point values. The solver provides + * methods for factorization, solving, and extracting L and U matrices, as well + * as determinant calculation. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_LINALG_LU_HPP__ #define __PYTHON_NUMPY_LINALG_LU_HPP__ @@ -13,6 +31,16 @@ namespace PythonNumpy { const double DEFAULT_DIVISION_MIN_LINALG_LU = 1.0e-10; +/** + * @brief LinalgSolverLU class for LU decomposition and solving linear systems. + * + * This class provides methods to perform LU decomposition on matrices and solve + * linear systems using the decomposed matrices. It supports dense, diagonal, + * and sparse matrices, and can handle both single and double precision floating + * point values. + * + * @tparam A_Type The type of the matrix (dense, diagonal, or sparse). + */ template class LinalgSolverLU { public: /* Type */ @@ -119,16 +147,48 @@ template class LinalgSolverLU { return *this; } +public: /* Solve function */ + + /** + * @brief Performs LU decomposition on the given matrix and prepares the + * solver for solving linear systems. + * + * This function initializes the LU decomposition of the input matrix A, + * allowing subsequent calls to `solve` to efficiently solve linear systems + * involving A. + * + * @param A The input matrix for LU decomposition. + */ inline void solve(const Matrix &A) { this->_LU_decomposer.solve(A.matrix); } + /** + * @brief Performs LU decomposition on the given diagonal matrix and prepares + * the solver for solving linear systems. + * + * This function initializes the LU decomposition of the input diagonal matrix + * A, allowing subsequent calls to `solve` to efficiently solve linear systems + * involving A. + * + * @param A The input diagonal matrix for LU decomposition. + */ inline void solve(const Matrix &A) { this->_LU_decomposer = Base::Matrix::LUDecomposition<_T, A_Type::COLS>(A.matrix); } + /** + * @brief Performs LU decomposition on the given sparse matrix and prepares + * the solver for solving linear systems. + * + * This function initializes the LU decomposition of the input sparse matrix + * A, allowing subsequent calls to `solve` to efficiently solve linear systems + * involving A. + * + * @param A The input sparse matrix for LU decomposition. + */ inline void solve(const Matrix &A) { @@ -137,6 +197,18 @@ template class LinalgSolverLU { } /* Get */ + + /** + * @brief Returns the lower triangular matrix (L) and upper triangular matrix + * (U) from the LU decomposition. + * + * This function retrieves the L and U matrices from the LU decomposition + * performed on the input matrix A. The matrices are returned as sparse + * matrices if A is sparse, or as dense matrices otherwise. + * + * @return A pair containing the lower triangular matrix L and upper + * triangular matrix U. + */ inline auto get_L() -> Matrix const { @@ -147,6 +219,15 @@ template class LinalgSolverLU { LowerTriangular_SparseAvailable_Type>(this->_L_triangular); } + /** + * @brief Returns the upper triangular matrix (U) from the LU decomposition. + * + * This function retrieves the U matrix from the LU decomposition performed on + * the input matrix A. The matrix is returned as a sparse matrix if A is + * sparse, or as a dense matrix otherwise. + * + * @return The upper triangular matrix U. + */ inline auto get_U() -> Matrix const { @@ -157,9 +238,28 @@ template class LinalgSolverLU { UpperTriangular_SparseAvailable_Type>(this->_U_triangular); } + /** + * @brief Solves the linear system Ax = b using the LU decomposition. + * + * This function solves the linear system represented by the matrix A and + * vector b using the LU decomposition. It returns the solution vector x. + * + * @param b The right-hand side vector of the linear system. + * @return The solution vector x such that Ax = b. + */ inline _T get_det() { return this->_LU_decomposer.get_determinant(); } /* Set */ + + /** + * @brief Sets the minimum value for division in the LU decomposition. + * + * This function allows the user to specify a minimum value for division + * during the LU decomposition process. It can help avoid numerical issues + * when dealing with very small values. + * + * @param division_min The minimum value for division. + */ inline void set_division_min(const _T &division_min) { this->_LU_decomposer.division_min = division_min; } @@ -186,6 +286,17 @@ template class LinalgSolverLU { }; /* make LinalgSolverLU */ + +/** + * @brief Creates an instance of LinalgSolverLU for the specified matrix type. + * + * This function is a factory function that creates and returns an instance of + * LinalgSolverLU for the given matrix type A_Type. It is used to simplify the + * creation of the solver object. + * + * @tparam A_Type The type of the matrix (dense, diagonal, or sparse). + * @return An instance of LinalgSolverLU for the specified matrix type. + */ template inline auto make_LinalgSolverLU(void) -> LinalgSolverLU { From 6cfd761718f9e8c9a40ccb0c1fe29492bccca3c4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 12:40:21 +0900 Subject: [PATCH 26/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_linalg_qr.hpp | 181 ++++++++++++++++++++++++ 1 file changed, 181 insertions(+) diff --git a/python_numpy/python_numpy_linalg_qr.hpp b/python_numpy/python_numpy_linalg_qr.hpp index a7e43f1..ca0c3a0 100644 --- a/python_numpy/python_numpy_linalg_qr.hpp +++ b/python_numpy/python_numpy_linalg_qr.hpp @@ -1,3 +1,19 @@ +/** + * @file python_numpy_linalg_qr.hpp + * @brief QR decomposition solvers for dense, diagonal, and sparse matrices in a + * NumPy-like C++ linear algebra library. + * + * This header defines template classes and factory functions for performing QR + * decomposition on matrices of various types (dense, diagonal, sparse). The + * solvers are designed to work with the matrix types defined in the library, + * providing interfaces to compute and retrieve the Q and R factors. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_LINALG_QR_HPP__ #define __PYTHON_NUMPY_LINALG_QR_HPP__ @@ -13,6 +29,18 @@ namespace PythonNumpy { const double DEFAULT_DIVISION_MIN_LINALG_QR = 1.0e-10; +/** + * @brief LinalgSolverQR class for QR decomposition and solving linear systems. + * + * This class provides methods to perform QR decomposition on matrices and solve + * linear systems using the decomposed matrices. It supports dense, diagonal, + * and sparse matrices, and can handle both single and double precision floating + * point values. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of columns in the matrix. + * @tparam N The number of rows in the matrix. + */ template class LinalgSolverQR { public: /* Type */ @@ -59,12 +87,32 @@ template class LinalgSolverQR { return *this; } +public: /* Solve function */ + + /** + * @brief Solves the QR decomposition for the given matrix A. + * + * This function sets the internal R matrix to the provided matrix A + * and performs QR decomposition by calling the internal _decompose method. + * + * @param A The input matrix to decompose. + */ inline void solve(const Matrix &A) { this->_QR_decomposer.solve(A.matrix); } /* Get Q, R */ + + /** + * @brief Returns the upper triangular matrix R from the QR decomposition. + * + * This function returns the upper triangular matrix R that was computed + * during the QR decomposition process. The matrix is represented as a sparse + * matrix with pre-defined row indices and pointers. + * + * @return A sparse matrix representing the upper triangular part of R. + */ inline auto get_R(void) -> Matrix< DefSparse, T, M, N, CreateSparseAvailableFromIndicesAndPointers< @@ -79,10 +127,27 @@ template class LinalgSolverQR { this->_R_triangular); } + /** + * @brief Returns the orthogonal matrix Q from the QR decomposition. + * + * This function returns the orthogonal matrix Q that was computed during + * the QR decomposition process. The matrix is represented as a dense matrix. + * + * @return A dense matrix representing the orthogonal part of Q. + */ inline auto get_Q(void) -> Matrix const { return Matrix(this->_QR_decomposer.get_Q()); } + /** + * @brief Sets the minimum division threshold for QR decomposition. + * + * This function allows the user to set a custom minimum division threshold + * for the QR decomposition process. It is useful for controlling numerical + * stability and precision. + * + * @param division_min The minimum division threshold to set. + */ inline void set_division_min(const T &division_min) { this->_QR_decomposer.division_min = division_min; } @@ -104,6 +169,16 @@ template class LinalgSolverQR { _R_triangular; }; +/** + * @brief LinalgSolverQRDiag class for QR decomposition of diagonal matrices. + * + * This class provides methods to perform QR decomposition specifically for + * diagonal matrices. It supports both single and double precision floating + * point values. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of columns in the diagonal matrix. + */ template class LinalgSolverQRDiag { public: /* Constructor */ @@ -133,10 +208,35 @@ template class LinalgSolverQRDiag { public: /* Function */ + + /** + * @brief Solves the QR decomposition for the given diagonal matrix A. + * + * This function sets the internal R matrix to the provided diagonal matrix A. + * + * @param A The input diagonal matrix to decompose. + */ inline void solve(const Matrix &A) { this->_R = A; } + /** + * @brief Returns the diagonal matrix R from the QR decomposition. + * + * This function returns the diagonal matrix R that was computed during + * the QR decomposition process. The matrix is represented as a diagonal + * matrix. + * + * @return A diagonal matrix representing R. + */ inline auto get_R(void) -> Matrix const { return this->_R; } + /** + * @brief Returns the identity matrix Q from the QR decomposition. + * + * This function returns the identity matrix Q, which is a property of the + * QR decomposition for diagonal matrices. + * + * @return An identity matrix representing Q. + */ inline auto get_Q(void) -> Matrix const { return Matrix::identity(); } @@ -146,6 +246,18 @@ template class LinalgSolverQRDiag { Matrix _R; }; +/** + * @brief LinalgSolverQRSparse class for QR decomposition of sparse matrices. + * + * This class provides methods to perform QR decomposition specifically for + * sparse matrices. It supports both single and double precision floating + * point values, and uses a sparse representation for the R matrix. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of columns in the sparse matrix. + * @tparam N The number of rows in the sparse matrix. + * @tparam SparseAvailable A type indicating the availability of sparse storage. + */ template class LinalgSolverQRSparse { public: @@ -196,10 +308,28 @@ class LinalgSolverQRSparse { public: /* Function */ + + /** + * @brief Solves the QR decomposition for the given sparse matrix A. + * + * This function sets the internal R matrix to the provided sparse matrix A + * and performs QR decomposition by calling the internal _decompose method. + * + * @param A The input sparse matrix to decompose. + */ inline void solve(const Matrix &A) { this->_QR_decomposer.solve(A.matrix); } + /** + * @brief Returns the upper triangular matrix R from the QR decomposition. + * + * This function returns the upper triangular matrix R that was computed + * during the QR decomposition process. The matrix is represented as a sparse + * matrix with pre-defined row indices and pointers. + * + * @return A sparse matrix representing the upper triangular part of R. + */ inline auto get_R(void) -> Matrix< DefSparse, T, M, N, CreateSparseAvailableFromIndicesAndPointers< @@ -213,10 +343,27 @@ class LinalgSolverQRSparse { _R_TriangluarRowPointers>(this->_R_triangular); } + /** + * @brief Returns the orthogonal matrix Q from the QR decomposition. + * + * This function returns the orthogonal matrix Q that was computed during + * the QR decomposition process. The matrix is represented as a dense matrix. + * + * @return A dense matrix representing the orthogonal part of Q. + */ inline auto get_Q(void) -> Matrix const { return Matrix(this->_QR_decomposer.get_Q()); } + /** + * @brief Sets the minimum division threshold for QR decomposition. + * + * This function allows the user to set a custom minimum division threshold + * for the QR decomposition process. It is useful for controlling numerical + * stability and precision. + * + * @param division_min The minimum division threshold to set. + */ inline void set_division_min(const T &division_min) { this->_QR_decomposer.division_min = division_min; } @@ -242,6 +389,18 @@ class LinalgSolverQRSparse { }; /* make LinalgSolverQR */ + +/** + * @brief Factory function to create a LinalgSolverQR instance based on the + * matrix type. + * + * This function uses SFINAE to determine the appropriate LinalgSolverQR type + * based on the input matrix type (dense, diagonal, or sparse). + * + * @tparam A_Type The type of the input matrix. + * @return An instance of LinalgSolverQR, LinalgSolverQRDiag, or + * LinalgSolverQRSparse. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> @@ -252,6 +411,17 @@ inline auto make_LinalgSolverQR(void) A_Type::ROWS>(); } +/** + * @brief Factory function to create a LinalgSolverQRDiag instance for diagonal + * matrices. + * + * This function creates an instance of LinalgSolverQRDiag specifically for + * diagonal matrices, using the value type and number of columns from the input + * matrix type. + * + * @tparam A_Type The type of the input diagonal matrix. + * @return An instance of LinalgSolverQRDiag. + */ template ::value>::type * = nullptr> inline auto make_LinalgSolverQR(void) @@ -260,6 +430,17 @@ inline auto make_LinalgSolverQR(void) return LinalgSolverQRDiag(); } +/** + * @brief Factory function to create a LinalgSolverQRSparse instance for sparse + * matrices. + * + * This function creates an instance of LinalgSolverQRSparse specifically for + * sparse matrices, using the value type, number of columns, rows, and sparse + * availability from the input matrix type. + * + * @tparam A_Type The type of the input sparse matrix. + * @return An instance of LinalgSolverQRSparse. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> From bc5f238d2d7ce52aad8670fd5de0fab02b22c159 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 12:52:50 +0900 Subject: [PATCH 27/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_linalg_solver.hpp | 1107 +++++++++++++++++++ 1 file changed, 1107 insertions(+) diff --git a/python_numpy/python_numpy_linalg_solver.hpp b/python_numpy/python_numpy_linalg_solver.hpp index d4fc598..f2342ce 100644 --- a/python_numpy/python_numpy_linalg_solver.hpp +++ b/python_numpy/python_numpy_linalg_solver.hpp @@ -1,3 +1,21 @@ +/** + * @file python_numpy_linalg_solver.hpp + * @brief Linear algebra solver utilities for Python-like Numpy matrix + * operations in C++. + * + * This header provides a set of template classes and factory functions to + * perform linear algebra operations such as solving linear systems, matrix + * inversion, partitioned solving, and least-squares solutions. The solvers + * support dense, diagonal, and sparse matrix types, and are designed to mimic + * the behavior of Python's Numpy linalg module, but in a statically-typed, + * template-based C++ environment. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_LINALG_SOLVER_HPP__ #define __PYTHON_NUMPY_LINALG_SOLVER_HPP__ @@ -21,6 +39,33 @@ struct InverseDense {}; template struct InverseDense { + /** + * @brief Computes the inverse of a complex matrix using a custom GMRES-based + * solver. + * + * This static function applies a GMRES-based matrix inversion algorithm to + * the input matrix `A`. It utilizes decay rate and division minimum + * parameters to control the solver's behavior. The function updates the + * provided `X_1` matrix with the computed result and returns it as a new + * matrix object. + * + * @tparam DefDense The matrix storage type. + * @tparam Complex_T The complex number type. + * @tparam M The number of rows and columns in the square matrix. + * @tparam T The floating-point type for decay and division + * parameters. + * @tparam K The size of the `rho` and `rep_num` arrays. + * + * @param A The input square matrix to invert (of size MxM). + * @param decay_rate The decay rate parameter for the GMRES solver. + * @param division_min The minimum division threshold for numerical stability. + * @param rho Array of parameters influencing the solver (size K). + * @param rep_num Array of repetition numbers for the solver (size K). + * @param X_1 Matrix to store the intermediate and final results (size + * MxK). + * + * @return Matrix The computed inverse matrix. + */ static auto compute(const Matrix &A, const T &decay_rate, const T &division_min, std::array &rho, @@ -37,6 +82,32 @@ struct InverseDense { template struct InverseDense { + /** + * @brief Computes the inverse of a real matrix using a custom GMRES-based + * solver. + * + * This static function applies a GMRES-based matrix inversion algorithm to + * the input matrix `A`. It utilizes decay rate and division minimum + * parameters to control the solver's behavior. The function updates the + * provided `X_1` matrix with the computed result and returns it as a new + * matrix object. + * + * @tparam DefDense The matrix storage type. + * @tparam T The floating-point type for decay and division + * parameters. + * @tparam M The number of rows and columns in the square matrix. + * @tparam K The size of the `rho` and `rep_num` arrays. + * + * @param A The input square matrix to invert (of size MxM). + * @param decay_rate The decay rate parameter for the GMRES solver. + * @param division_min The minimum division threshold for numerical stability. + * @param rho Array of parameters influencing the solver (size K). + * @param rep_num Array of repetition numbers for the solver (size K). + * @param X_1 Matrix to store the intermediate and final results (size + * MxK). + * + * @return Matrix The computed inverse matrix. + */ static auto compute(const Matrix &A, const T &decay_rate, const T &division_min, std::array &rho, std::array &rep_num, @@ -55,6 +126,26 @@ struct InverseDiag {}; template struct InverseDiag { + /** + * @brief Computes the inverse of a complex diagonal matrix. + * + * This static function computes the inverse of a diagonal matrix `A` using + * a custom method that handles complex numbers. It updates the provided + * `X_1` matrix with the computed result and returns it as a new matrix + * object. + * + * @tparam DefDiag The matrix storage type for diagonal matrices. + * @tparam Complex_T The complex number type. + * @tparam M The size of the square diagonal matrix. + * @tparam T The floating-point type for division parameters. + * + * @param A The input diagonal matrix to invert (of size MxM). + * @param division_min The minimum division threshold for numerical stability. + * @param X_1 Matrix to store the intermediate and final results (size + * MxM). + * + * @return Matrix The computed inverse diagonal matrix. + */ static auto compute(const Matrix &A, const T &division_min, Base::Matrix::DiagMatrix &X_1) @@ -68,6 +159,24 @@ struct InverseDiag { template struct InverseDiag { + /** + * @brief Computes the inverse of a real diagonal matrix. + * + * This static function computes the inverse of a diagonal matrix `A` using + * a custom method that handles real numbers. It updates the provided `X_1` + * matrix with the computed result and returns it as a new matrix object. + * + * @tparam DefDiag The matrix storage type for diagonal matrices. + * @tparam T The floating-point type for division parameters. + * @tparam M The size of the square diagonal matrix. + * + * @param A The input diagonal matrix to invert (of size MxM). + * @param division_min The minimum division threshold for numerical stability. + * @param X_1 Matrix to store the intermediate and final results (size + * MxM). + * + * @return Matrix The computed inverse diagonal matrix. + */ static auto compute(const Matrix &A, const T &division_min, Base::Matrix::DiagMatrix &X_1) -> Matrix { @@ -85,6 +194,33 @@ struct InverseSparse {}; template struct InverseSparse { + /** + * @brief Computes the inverse of a complex sparse matrix using a custom GMRES + * solver. + * + * This static function applies a GMRES-based matrix inversion algorithm to + * the input sparse matrix `A`. It utilizes decay rate and division minimum + * parameters to control the solver's behavior. The function updates the + * provided `X_1` matrix with the computed result and returns it as a new + * matrix object. + * + * @tparam DefSparse The matrix storage type for sparse matrices. + * @tparam Complex_T The complex number type. + * @tparam M The number of rows and columns in the square matrix. + * @tparam T The floating-point type for decay and division + * parameters. + * @tparam K The size of the `rho` and `rep_num` arrays. + * + * @param A The input square sparse matrix to invert (of size MxM). + * @param decay_rate The decay rate parameter for the GMRES solver. + * @param division_min The minimum division threshold for numerical stability. + * @param rho Array of parameters influencing the solver (size K). + * @param rep_num Array of repetition numbers for the solver (size K). + * @param X_1 Matrix to store the intermediate and final results (size + * MxK). + * + * @return Matrix The computed inverse matrix. + */ static auto compute(const Matrix &A, const T &decay_rate, const T &division_min, std::array &rho, @@ -102,6 +238,32 @@ struct InverseSparse { template struct InverseSparse { + /** + * @brief Computes the inverse of a real sparse matrix using a custom GMRES + * solver. + * + * This static function applies a GMRES-based matrix inversion algorithm to + * the input sparse matrix `A`. It utilizes decay rate and division minimum + * parameters to control the solver's behavior. The function updates the + * provided `X_1` matrix with the computed result and returns it as a new + * matrix object. + * + * @tparam DefSparse The matrix storage type for sparse matrices. + * @tparam T The floating-point type for decay and division + * parameters. + * @tparam M The number of rows and columns in the square matrix. + * @tparam K The size of the `rho` and `rep_num` arrays. + * + * @param A The input square sparse matrix to invert (of size MxM). + * @param decay_rate The decay rate parameter for the GMRES solver. + * @param division_min The minimum division threshold for numerical stability. + * @param rho Array of parameters influencing the solver (size K). + * @param rep_num Array of repetition numbers for the solver (size K). + * @param X_1 Matrix to store the intermediate and final results (size + * MxK). + * + * @return Matrix The computed inverse matrix. + */ static auto compute(const Matrix &A, const T &decay_rate, const T &division_min, std::array &rho, @@ -119,6 +281,20 @@ struct InverseSparse { } // namespace InverseOperation /* Linalg Solver */ + +/** + * @brief Linalg solver for various matrix types. + * + * This class provides methods to solve linear systems, compute inverses, and + * perform other linear algebra operations on dense, diagonal, and sparse + * matrices. It supports both real and complex number types (float or double). + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square matrix. + * @tparam K The number of columns in the right-hand side matrix. + * @tparam SparseAvailable_A Indicates if the first matrix is sparse. + * @tparam SparseAvailable_B Indicates if the second matrix is sparse. + */ template class LinalgSolver { @@ -177,7 +353,20 @@ class LinalgSolver { return *this; } +public: /* Solve function */ + + /** + * @brief Solves the linear system Ax = B using GMRES method. + * + * This function applies the GMRES method to solve the linear system + * represented by the matrix A and the right-hand side matrix B. It updates + * the internal state of the solver with the computed solution. + * + * @param A The coefficient matrix (of size MxM). + * @param B The right-hand side matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -189,6 +378,18 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B using GMRES method for diagonal + * matrices. + * + * This function applies the GMRES method to solve the linear system + * represented by the diagonal matrix A and the right-hand side matrix B. + * It updates the internal state of the solver with the computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size M). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -200,6 +401,18 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B using GMRES method for sparse + * matrices. + * + * This function applies the GMRES method to solve the linear system + * represented by the sparse matrix A and the right-hand side matrix B. + * It updates the internal state of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -214,6 +427,18 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B using GMRES method for diagonal + * matrices. + * + * This function applies the GMRES method to solve the linear system + * represented by the diagonal matrix A and the right-hand side matrix B. + * It updates the internal state of the solver with the computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -224,6 +449,17 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for diagonal matrices. + * + * This function computes the solution of the linear system represented by + * the diagonal matrix A and the diagonal matrix B. It updates the internal + * state of the solver with the computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size M). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -235,6 +471,17 @@ class LinalgSolver { return Matrix(std::move(result)); } + /** + * @brief Solves the linear system Ax = B for sparse matrices. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the dense matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -251,6 +498,17 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for sparse matrices. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the sparse matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -262,6 +520,17 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for sparse matrices. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the diagonal matrix B. It updates the internal + * state of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size M). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -276,6 +545,17 @@ class LinalgSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for sparse matrices. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the sparse matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -291,6 +571,17 @@ class LinalgSolver { } /* Inv function */ + + /** + * @brief Computes the inverse of a dense matrix. + * + * This function computes the inverse of a dense square matrix A using a + * custom GMRES-based solver. It updates the internal state with the computed + * inverse and returns it as a new matrix object. + * + * @param A The input square matrix to invert (of size MxM). + * @return Matrix The computed inverse matrix. + */ inline auto inv(const Matrix &A) -> Matrix { @@ -300,6 +591,16 @@ class LinalgSolver { this->rep_num, this->X_1); } + /** + * @brief Computes the inverse of a sparse matrix. + * + * This function computes the inverse of a sparse square matrix A using a + * custom GMRES-based solver. It updates the internal state with the computed + * inverse and returns it as a new matrix object. + * + * @param A The input square sparse matrix to invert (of size MxM). + * @return Matrix The computed inverse matrix. + */ inline auto inv(const Matrix &A) -> Matrix { @@ -309,14 +610,39 @@ class LinalgSolver { this->rep_num, X_1); } + /** + * @brief Computes the inverse of a diagonal matrix. + * + * This function computes the inverse of a diagonal matrix A using a custom + * method that handles both real and complex numbers. It updates the internal + * state with the computed inverse and returns it as a new matrix object. + * + * @param A The input diagonal matrix to invert (of size MxM). + * @return Matrix The computed inverse diagonal matrix. + */ inline auto get_answer(void) -> Matrix { return Matrix(this->X_1); } + /** + * @brief Sets the decay rate for the GMRES solver. + * + * This function sets the decay rate parameter used in the GMRES solver. + * + * @param decay_rate_in The decay rate value to set. + */ inline void set_decay_rate(const Value_Type &decay_rate_in) { this->decay_rate = decay_rate_in; } + /** + * @brief Sets the minimum division threshold for numerical stability. + * + * This function sets the minimum division threshold used in the solver to + * prevent numerical instability during matrix inversion. + * + * @param division_min_in The minimum division value to set. + */ inline void set_division_min(const Value_Type &division_min_in) { this->division_min = division_min_in; } @@ -338,6 +664,14 @@ class LinalgSolver { std::array rep_num; }; +/** * @brief Linalg solver for inverse diagonal matrices. + * + * This class provides methods to compute the inverse of diagonal matrices. + * It supports both real and complex number types (float or double). + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of rows and columns in the square matrix. + */ template class LinalgInvDiag { public: /* Type */ @@ -379,16 +713,43 @@ template class LinalgInvDiag { public: /* Function */ + + /** + * @brief Computes the inverse of a diagonal matrix. + * + * This function computes the inverse of a diagonal matrix A using a custom + * method that handles both real and complex numbers. It updates the internal + * state with the computed inverse and returns it as a new matrix object. + * + * @param A The input diagonal matrix to invert (of size MxM). + * @return Matrix The computed inverse diagonal matrix. + */ inline auto inv(const Matrix &A) -> Matrix { return InverseOperation::InverseDiag::compute( A, this->division_min, X_1); } + /** + * @brief Returns the computed inverse diagonal matrix. + * + * This function returns the internal state of the solver, which contains the + * computed inverse diagonal matrix. + * + * @return Matrix The computed inverse diagonal matrix. + */ inline auto get_answer(void) -> Matrix { return Matrix(this->X_1); } + /** + * @brief Sets the minimum division threshold for numerical stability. + * + * This function sets the minimum division threshold used in the solver to + * prevent numerical instability during matrix inversion. + * + * @param division_min_in The minimum division value to set. + */ inline void set_division_min(const T &division_min_in) { this->division_min = division_min_in; } @@ -408,6 +769,19 @@ template class LinalgInvDiag { }; /* make LinalgSolver for inv */ + +/** + * @brief Creates a LinalgSolver for matrix inversion. + * + * This function template creates an instance of LinalgSolver specialized for + * matrix inversion based on the type of the input matrix A. It supports dense, + * diagonal, and sparse matrices. + * + * @tparam A_Type The type of the input matrix (e.g., DenseMatrix, DiagMatrix, + * SparseMatrix). + * @return LinalgSolverInv_Type An instance of LinalgSolver for matrix + * inversion. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> @@ -421,6 +795,16 @@ inline auto make_LinalgSolverInv(void) typename A_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for diagonal matrix inversion. + * + * This function template creates an instance of LinalgInvDiag specialized for + * diagonal matrices. It supports both real and complex number types. + * + * @tparam A_Type The type of the input diagonal matrix (e.g., DiagMatrix). + * @return LinalgInvDiag An instance of LinalgInvDiag for diagonal + * matrix inversion. + */ template ::value>::type * = nullptr> inline auto make_LinalgSolverInv(void) @@ -429,6 +813,16 @@ inline auto make_LinalgSolverInv(void) return LinalgInvDiag(); } +/** + * @brief Creates a LinalgSolver for sparse matrix inversion. + * + * This function template creates an instance of LinalgSolver specialized for + * sparse matrix inversion. It supports both real and complex number types. + * + * @tparam A_Type The type of the input sparse matrix (e.g., SparseMatrix). + * @return LinalgSolverInv_Type An instance of LinalgSolver for sparse + * matrix inversion. + */ template < typename A_Type, typename std::enable_if::value>::type * = nullptr> @@ -447,6 +841,21 @@ template using LinalgSolverInv_Type = decltype(make_LinalgSolverInv()); /* make LinalgSolver */ + +/** + * @brief Creates a LinalgSolver for solving linear systems. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems based on the types of the input matrices A and B. It + * supports dense, diagonal, and sparse matrices. + * + * @tparam A_Type The type of the coefficient matrix (e.g., DenseMatrix, + * DiagMatrix, SparseMatrix). + * @tparam B_Type The type of the right-hand side matrix (e.g., DenseMatrix, + * DiagMatrix, SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -465,6 +874,21 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with diagonal + * matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where the coefficient matrix is diagonal. It supports + * both real and complex number types. + * + * @tparam A_Type The type of the coefficient diagonal matrix (e.g., + * DiagMatrix). + * @tparam B_Type The type of the right-hand side matrix (e.g., DenseMatrix, + * DiagMatrix, SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with diagonal matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -483,6 +907,21 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with sparse + * matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where the coefficient matrix is sparse. It supports + * both real and complex number types. + * + * @tparam A_Type The type of the coefficient sparse matrix (e.g., + * SparseMatrix). + * @tparam B_Type The type of the right-hand side matrix (e.g., DenseMatrix, + * DiagMatrix, SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with sparse matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -501,6 +940,21 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with diagonal + * matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where the coefficient matrix is diagonal. It supports + * both real and complex number types. + * + * @tparam A_Type The type of the coefficient diagonal matrix (e.g., + * DiagMatrix). + * @tparam B_Type The type of the right-hand side matrix (e.g., DenseMatrix, + * DiagMatrix, SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with diagonal matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -519,6 +973,21 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with diagonal + * matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where both the coefficient matrix and the right-hand + * side matrix are diagonal. It supports both real and complex number types. + * + * @tparam A_Type The type of the coefficient diagonal matrix (e.g., + * DiagMatrix). + * @tparam B_Type The type of the right-hand side diagonal matrix (e.g., + * DiagMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with diagonal matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -537,6 +1006,22 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with diagonal and + * sparse matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where the coefficient matrix is diagonal and the + * right-hand side matrix is sparse. It supports both real and complex number + * types. + * + * @tparam A_Type The type of the coefficient diagonal matrix (e.g., + * DiagMatrix). + * @tparam B_Type The type of the right-hand side sparse matrix (e.g., + * SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with diagonal and sparse matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -555,6 +1040,22 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with sparse + * matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where the coefficient matrix is sparse and the + * right-hand side matrix can be dense, diagonal, or sparse. It supports both + * real and complex number types. + * + * @tparam A_Type The type of the coefficient sparse matrix (e.g., + * SparseMatrix). + * @tparam B_Type The type of the right-hand side matrix (e.g., DenseMatrix, + * DiagMatrix, SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with sparse matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -573,6 +1074,22 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with sparse and + * diagonal matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where the coefficient matrix is sparse and the + * right-hand side matrix is diagonal. It supports both real and complex number + * types. + * + * @tparam A_Type The type of the coefficient sparse matrix (e.g., + * SparseMatrix). + * @tparam B_Type The type of the right-hand side diagonal matrix (e.g., + * DiagMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with sparse and diagonal matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -591,6 +1108,21 @@ inline auto make_LinalgSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Creates a LinalgSolver for solving linear systems with sparse + * matrices. + * + * This function template creates an instance of LinalgSolver specialized for + * solving linear systems where both the coefficient matrix and the right-hand + * side matrix are sparse. It supports both real and complex number types. + * + * @tparam A_Type The type of the coefficient sparse matrix (e.g., + * SparseMatrix). + * @tparam B_Type The type of the right-hand side sparse matrix (e.g., + * SparseMatrix). + * @return LinalgSolver An instance of LinalgSolver for + * solving linear systems with sparse matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -614,6 +1146,20 @@ template using LinalgSolver_Type = decltype(make_LinalgSolver()); /* Linalg Partition Solver */ + +/** @brief Linalg solver for partitioned matrices. + * + * This class provides methods to solve linear systems using partitioned + * matrices. It supports both dense and sparse matrices, and allows for + * partitioning based on the number of rows and columns. + * + * @tparam T The data type of the matrix elements (e.g., float, double). + * @tparam M The number of columns in the square matrix. + * @tparam K The number of rows in the right-hand side matrix. + * @tparam SparseAvailable_A Indicates if the coefficient matrix A is sparse. + * @tparam SparseAvailable_B Indicates if the right-hand side matrix B is + * sparse. + */ template class LinalgPartitionSolver { @@ -674,6 +1220,19 @@ class LinalgPartitionSolver { } /* Solve function */ + + /** + * @brief Solves the linear system Ax = B for dense matrices. + * + * This function computes the solution of the linear system represented by + * the dense matrix A and the dense matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient dense matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -689,6 +1248,19 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for dense matrices with cold start. + * + * This function computes the solution of the linear system represented by + * the dense matrix A and the dense matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient dense matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -706,6 +1278,18 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Solves the linear system Ax = B for dense matrices with sparse B. + * + * This function computes the solution of the linear system represented by + * the dense matrix A and the sparse matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient dense matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -721,6 +1305,20 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for dense matrices with sparse B + * and cold start. + * + * This function computes the solution of the linear system represented by + * the dense matrix A and the sparse matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient dense matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -738,6 +1336,18 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Solves the linear system Ax = B for dense matrices with sparse B. + * + * This function computes the solution of the linear system represented by + * the dense matrix A and the sparse matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient dense matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -756,6 +1366,20 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for dense matrices with sparse B + * and cold start. + * + * This function computes the solution of the linear system represented by + * the dense matrix A and the sparse matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient dense matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -776,6 +1400,18 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Solves the linear system Ax = B for diagonal matrices. + * + * This function computes the solution of the linear system represented by + * the diagonal matrix A and the dense matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -790,6 +1426,20 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for diagonal matrices with cold + * start. + * + * This function computes the solution of the linear system represented by + * the diagonal matrix A and the dense matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -805,6 +1455,18 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Solves the linear system Ax = B for diagonal matrices. + * + * This function computes the solution of the linear system represented by + * the diagonal matrix A and the diagonal matrix B. It updates the internal + * state of the solver with the computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size MxM). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -820,6 +1482,20 @@ class LinalgPartitionSolver { return Matrix(std::move(result)); } + /** + * @brief Solves the linear system Ax = B for diagonal matrices with cold + * start. + * + * This function computes the solution of the linear system represented by + * the diagonal matrix A and the diagonal matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size MxM). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -835,6 +1511,20 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for diagonal matrices with cold + * start. + * + * This function computes the solution of the linear system represented by + * the diagonal matrix A and the sparse matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient diagonal matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -854,6 +1544,18 @@ class LinalgPartitionSolver { Base::Matrix::output_dense_matrix(X_1_temporary)); } + /** + * @brief Solves the linear system Ax = B for sparse matrices. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the dense matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -869,6 +1571,19 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for sparse matrices with cold start. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the dense matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side dense matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -886,6 +1601,18 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Solves the linear system Ax = B for sparse matrices with diagonal B. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the diagonal matrix B. It updates the internal + * state of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size MxM). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -904,6 +1631,20 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for sparse matrices with diagonal B + * and cold start. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the diagonal matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side diagonal matrix (of size MxM). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -924,6 +1665,18 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Solves the linear system Ax = B for sparse matrices with sparse B. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the sparse matrix B. It updates the internal state + * of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -942,6 +1695,20 @@ class LinalgPartitionSolver { return Matrix(this->X_1); } + /** + * @brief Solves the linear system Ax = B for sparse matrices with sparse B + * and cold start. + * + * This function computes the solution of the linear system represented by + * the sparse matrix A and the sparse matrix B without using any previous + * solution as a starting point. It returns a new matrix object with the + * computed solution. + * + * @param A The coefficient sparse matrix (of size MxM). + * @param B The right-hand side sparse matrix (of size MxK). + * @param matrix_size The size of the matrices to be processed. + * @return Matrix The computed solution matrix. + */ inline auto cold_solve(const Matrix &A, const Matrix &B, std::size_t matrix_size) -> Matrix { @@ -962,14 +1729,39 @@ class LinalgPartitionSolver { return Matrix(X_1_temporary); } + /** + * @brief Returns the computed solution matrix. + * + * This function returns the computed solution matrix stored in the solver. + * It is typically called after a successful solve operation. + * + * @return Matrix The computed solution matrix. + */ inline auto get_answer(void) -> Matrix { return Matrix(this->X_1); } + /** + * @brief Sets the decay rate for the solver. + * + * This function sets the decay rate used in the solver's computations. + * The decay rate influences the convergence behavior of the solver. + * + * @param decay_rate_in The decay rate to be set. + */ inline void set_decay_rate(const Value_Type &decay_rate_in) { this->decay_rate = decay_rate_in; } + /** + * @brief Sets the minimum division value for the solver. + * + * This function sets the minimum division value used in the solver's + * computations. It prevents division by zero and controls numerical + * stability. + * + * @param division_min_in The minimum division value to be set. + */ inline void set_division_min(const Value_Type &division_min_in) { this->division_min = division_min_in; } @@ -992,6 +1784,19 @@ class LinalgPartitionSolver { }; /* make LinalgPartitionSolver */ + +/** + * @brief Factory function to create a LinalgPartitionSolver instance. + * + * This function creates an instance of LinalgPartitionSolver based on the + * types of the input matrices A and B. It ensures that the value types of A and + * B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient matrix A. + * @tparam B_Type The type of the right-hand side matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1012,6 +1817,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * diagonal matrices. + * + * This function creates an instance of LinalgPartitionSolver configured for + * diagonal matrices A and B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side diagonal matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1032,6 +1850,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * sparse matrices. + * + * This function creates an instance of LinalgPartitionSolver configured for + * sparse matrices A and B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient sparse matrix A. + * @tparam B_Type The type of the right-hand side sparse matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1052,6 +1883,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * diagonal matrices with sparse B. + * + * This function creates an instance of LinalgPartitionSolver configured for + * diagonal matrix A and sparse matrix B. It ensures that the value types of A + * and B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side sparse matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1072,6 +1916,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * diagonal matrices with diagonal B. + * + * This function creates an instance of LinalgPartitionSolver configured for + * diagonal matrices A and B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side diagonal matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1092,6 +1949,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * diagonal matrices with sparse B. + * + * This function creates an instance of LinalgPartitionSolver configured for + * diagonal matrix A and sparse matrix B. It ensures that the value types of A + * and B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side sparse matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1112,6 +1982,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * sparse matrices with dense B. + * + * This function creates an instance of LinalgPartitionSolver configured for + * sparse matrix A and dense matrix B. It ensures that the value types of A and + * B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient sparse matrix A. + * @tparam B_Type The type of the right-hand side dense matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1132,6 +2015,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * sparse matrices with diagonal B. + * + * This function creates an instance of LinalgPartitionSolver configured for + * sparse matrix A and diagonal matrix B. It ensures that the value types of A + * and B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient sparse matrix A. + * @tparam B_Type The type of the right-hand side diagonal matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1152,6 +2048,19 @@ inline auto make_LinalgPartitionSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgPartitionSolver instance for + * sparse matrices with diagonal B. + * + * This function creates an instance of LinalgPartitionSolver configured for + * sparse matrix A and diagonal matrix B. It ensures that the value types of A + * and B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient sparse matrix A. + * @tparam B_Type The type of the right-hand side diagonal matrix B. + * @return LinalgPartitionSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1178,6 +2087,22 @@ using LinalgPartitionSolver_Type = decltype(make_LinalgPartitionSolver()); /* least-squares solution to a linear matrix equation */ + +/** + * @brief Class for solving least-squares problems using GMRES. + * + * This class provides methods to solve least-squares problems of the form + * Ax = B, where A is a matrix and B is a matrix or vector. It supports dense, + * diagonal, and sparse matrices, and can handle both cold starts and warm + * starts. + * + * @tparam T The value type of the matrices (float or double). + * @tparam M The number of columns in matrix A. + * @tparam N The number of rows in matrix A (and rows in matrix B). + * @tparam K The number of rows in matrix B. + * @tparam SparseAvailable_A Indicates if sparse matrices are available for A. + * @tparam SparseAvailable_B Indicates if sparse matrices are available for B. + */ template class LinalgLstsqSolver { @@ -1239,7 +2164,20 @@ class LinalgLstsqSolver { return *this; } +public: /* Solve method */ + + /** + * @brief Solves the least-squares problem Ax = B for dense matrices. + * + * This function computes the least-squares solution of the linear system + * represented by the dense matrix A and the dense matrix B. It updates the + * internal state of the solver with the computed solution. + * + * @param A The coefficient dense matrix (of size MxN). + * @param B The right-hand side dense matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -1253,6 +2191,18 @@ class LinalgLstsqSolver { return Matrix(X_1); } + /** + * @brief Solves the least-squares problem Ax = B for dense matrices with + * diagonal B. + * + * This function computes the least-squares solution of the linear system + * represented by the dense matrix A and the diagonal matrix B. It updates + * the internal state of the solver with the computed solution. + * + * @param A The coefficient dense matrix (of size MxN). + * @param B The right-hand side diagonal matrix (of size MxM). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -1266,6 +2216,18 @@ class LinalgLstsqSolver { return Matrix(X_1); } + /** + * @brief Solves the least-squares problem Ax = B for dense matrices with + * sparse B. + * + * This function computes the least-squares solution of the linear system + * represented by the dense matrix A and the sparse matrix B. It updates the + * internal state of the solver with the computed solution. + * + * @param A The coefficient dense matrix (of size MxN). + * @param B The right-hand side sparse matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -1282,6 +2244,19 @@ class LinalgLstsqSolver { return Matrix(X_1); } + /** + * @brief Solves the least-squares problem Ax = B for dense matrices with + * sparse B and cold start. + * + * This function computes the least-squares solution of the linear system + * represented by the dense matrix A and the sparse matrix B without using + * any previous solution as a starting point. It returns a new matrix object + * with the computed solution. + * + * @param A The coefficient dense matrix (of size MxN). + * @param B The right-hand side sparse matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -1295,6 +2270,18 @@ class LinalgLstsqSolver { return Matrix(X_1); } + /** + * @brief Solves the least-squares problem Ax = B for sparse matrices with + * diagonal B. + * + * This function computes the least-squares solution of the linear system + * represented by the sparse matrix A and the diagonal matrix B. It updates + * the internal state of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxN). + * @param B The right-hand side diagonal matrix (of size MxM). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -1308,6 +2295,18 @@ class LinalgLstsqSolver { return Matrix(X_1); } + /** + * @brief Solves the least-squares problem Ax = B for sparse matrices with + * sparse B. + * + * This function computes the least-squares solution of the linear system + * represented by the sparse matrix A and the sparse matrix B. It updates the + * internal state of the solver with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxN). + * @param B The right-hand side sparse matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto solve(const Matrix &A, const Matrix &B) -> Matrix { @@ -1324,14 +2323,44 @@ class LinalgLstsqSolver { return Matrix(X_1); } + /** + * @brief Solves the least-squares problem Ax = B for sparse matrices with + * sparse B and cold start. + * + * This function computes the least-squares solution of the linear system + * represented by the sparse matrix A and the sparse matrix B without using + * any previous solution as a starting point. It returns a new matrix object + * with the computed solution. + * + * @param A The coefficient sparse matrix (of size MxN). + * @param B The right-hand side sparse matrix (of size MxK). + * @return Matrix The computed solution matrix. + */ inline auto get_answer(void) -> Matrix { return Matrix(this->X_1); } + /** + * @brief Sets the decay rate for the solver. + * + * This function sets the decay rate used in the solver's computations. + * The decay rate influences the convergence behavior of the solver. + * + * @param decay_rate_in The decay rate to be set. + */ inline void set_decay_rate(const Value_Type &decay_rate_in) { this->decay_rate = decay_rate_in; } + /** + * @brief Sets the minimum division value for the solver. + * + * This function sets the minimum division value used in the solver's + * computations. It prevents division by zero and controls numerical + * stability. + * + * @param division_min_in The minimum division value to be set. + */ inline void set_division_min(const Value_Type &division_min_in) { this->division_min = division_min_in; } @@ -1355,6 +2384,19 @@ class LinalgLstsqSolver { }; /* make LinalgLstsqSolver */ + +/** + * @brief Factory function to create a LinalgLstsqSolver instance. + * + * This function creates an instance of LinalgLstsqSolver based on the types of + * the input matrices A and B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient matrix A. + * @tparam B_Type The type of the right-hand side matrix B. + * @return LinalgLstsqSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1375,6 +2417,19 @@ inline auto make_LinalgLstsqSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgLstsqSolver instance for diagonal + * matrices. + * + * This function creates an instance of LinalgLstsqSolver configured for + * diagonal matrices A and B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side diagonal matrix B. + * @return LinalgLstsqSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1395,6 +2450,19 @@ inline auto make_LinalgLstsqSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgLstsqSolver instance for sparse + * matrices. + * + * This function creates an instance of LinalgLstsqSolver configured for sparse + * matrices A and B. It ensures that the value types of A and B are compatible + * and returns a solver configured for the specific matrix dimensions and + * sparsity. + * + * @tparam A_Type The type of the coefficient sparse matrix A. + * @tparam B_Type The type of the right-hand side sparse matrix B. + * @return LinalgLstsqSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1415,6 +2483,19 @@ inline auto make_LinalgLstsqSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgLstsqSolver instance for diagonal + * matrices with sparse B. + * + * This function creates an instance of LinalgLstsqSolver configured for + * diagonal matrix A and sparse matrix B. It ensures that the value types of A + * and B are compatible and returns a solver configured for the specific matrix + * dimensions and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side sparse matrix B. + * @return LinalgLstsqSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1435,6 +2516,19 @@ inline auto make_LinalgLstsqSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgLstsqSolver instance for diagonal + * matrices with diagonal B. + * + * This function creates an instance of LinalgLstsqSolver configured for + * diagonal matrices A and B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient diagonal matrix A. + * @tparam B_Type The type of the right-hand side diagonal matrix B. + * @return LinalgLstsqSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && @@ -1455,6 +2549,19 @@ inline auto make_LinalgLstsqSolver(void) typename B_Type::SparseAvailable_Type>(); } +/** + * @brief Factory function to create a LinalgLstsqSolver instance for sparse + * matrices with dense B. + * + * This function creates an instance of LinalgLstsqSolver configured for sparse + * matrix A and dense matrix B. It ensures that the value types of A and B are + * compatible and returns a solver configured for the specific matrix dimensions + * and sparsity. + * + * @tparam A_Type The type of the coefficient sparse matrix A. + * @tparam B_Type The type of the right-hand side dense matrix B. + * @return LinalgLstsqSolver instance configured for the given matrices. + */ template < typename A_Type, typename B_Type, typename std::enable_if::value && From 1a9c2a2c51759d5374894f3b2618ed82120c55b4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 12:55:18 +0900 Subject: [PATCH 28/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python_numpy/python_numpy_templates.hpp | 33 +++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/python_numpy/python_numpy_templates.hpp b/python_numpy/python_numpy_templates.hpp index 97ba4b6..1e0fcb7 100644 --- a/python_numpy/python_numpy_templates.hpp +++ b/python_numpy/python_numpy_templates.hpp @@ -1,3 +1,36 @@ +/** + * @file python_numpy_templates.hpp + * @brief Template aliases for Python-like Numpy matrix and sparse matrix + * operations in C++. + * + * This header provides a collection of template aliases within the PythonNumpy + * namespace, designed to facilitate matrix and sparse matrix operations similar + * to those found in Python's Numpy library. The templates wrap and expose + * various matrix utilities and structures from the Base::Matrix namespace, + * enabling type-safe and efficient manipulation of dense, sparse, and + * triangular matrices at compile time. + * + * The provided template aliases cover: + * - Complex type detection. + * - Construction and manipulation of sparse matrix structures (row indices, row + * pointers, column availability). + * - Dense and diagonal matrix availability. + * - Creation and transformation of sparse matrix representations. + * - Concatenation, addition, subtraction, and multiplication of sparse + * matrices. + * - Extraction of matrix rows and transposition. + * - Validation and utility operations for matrix structures. + * + * All templates are intended to be used as building blocks for + * high-performance, type-safe matrix computations in C++ projects that require + * Numpy-like functionality. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_TEMPLATES_HPP__ #define __PYTHON_NUMPY_TEMPLATES_HPP__ From 8adf7938440ebfa7bf54b0d8df6930447ca82036 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 13:06:08 +0900 Subject: [PATCH 29/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../python_numpy_transpose_operation.hpp | 316 ++++++++++++++++++ 1 file changed, 316 insertions(+) diff --git a/python_numpy/python_numpy_transpose_operation.hpp b/python_numpy/python_numpy_transpose_operation.hpp index 4905498..03c11a2 100644 --- a/python_numpy/python_numpy_transpose_operation.hpp +++ b/python_numpy/python_numpy_transpose_operation.hpp @@ -1,3 +1,21 @@ +/** + * @file python_numpy_transpose_operation.hpp + * @brief Provides matrix multiplication operations involving transposed + * matrices, supporting dense, diagonal, and sparse matrix types. + * + * This header defines a set of template functions within the PythonNumpy + * namespace to perform efficient matrix multiplications where one of the + * operands is transposed. The operations are designed to work with various + * matrix representations, including dense, diagonal, and sparse matrices, and + * their combinations. The functions leverage template specialization to select + * the optimal multiplication strategy based on the matrix types involved. + * + * @note + * tparam M is the number of columns in the matrix. + * tparam N is the number of rows in the matrix. + * Somehow Programming custom is vice versa, + * but in this project, we use the mathematical custom. + */ #ifndef __PYTHON_NUMPY_TRANSPOSE_OPERATION_HPP__ #define __PYTHON_NUMPY_TRANSPOSE_OPERATION_HPP__ @@ -11,6 +29,23 @@ namespace PythonNumpy { /* (matrix) * (transposed matrix) */ + +/** + * @brief Multiplies matrix A by the transpose of matrix B. + * + * This function computes the matrix product of A and the transpose of B, + * i.e., it returns the result of A * B^T. Both input matrices must have the + * same number of columns (K). The resulting matrix will have the same number of + * rows as A (M) and the same number of rows as B (N). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @tparam N The number of columns in matrix B. + * @param A The left-hand side matrix of size M x K. + * @param B The right-hand side matrix of size N x K (to be transposed). + * @return Matrix The result of A multiplied by B transposed. + */ template inline auto A_mul_BTranspose(const Matrix &A, const Matrix &B) @@ -20,6 +55,22 @@ inline auto A_mul_BTranspose(const Matrix &A, Base::Matrix::matrix_multiply_A_mul_BTranspose(A.matrix, B.matrix)); } +/** + * @brief Multiplies matrix A by the transpose of diagonal matrix B. + * + * This function computes the matrix product of A and the transpose of a + * diagonal matrix B, i.e., it returns the result of A * B^T. The resulting + * matrix will have the same number of rows as A (M) and the same number of rows + * as B (K). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @param A The left-hand side matrix of size M x K. + * @param B The right-hand side diagonal matrix of size K. + * @return Matrix The result of + * A multiplied by B transposed. + */ template inline auto A_mul_BTranspose(const Matrix &A, const Matrix &B) @@ -28,6 +79,22 @@ inline auto A_mul_BTranspose(const Matrix &A, return Matrix(A.matrix * B.matrix); } +/** + * @brief Multiplies matrix A by the transpose of sparse matrix B. + * + * This function computes the matrix product of A and the transpose of a sparse + * matrix B, i.e., it returns the result of A * B^T. The resulting matrix will + * have the same number of rows as A (M) and the same number of rows as B (N). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @tparam N The number of columns in matrix B. + * @param A The left-hand side matrix of size M x K. + * @param B The right-hand side sparse matrix of size N x K (to be transposed). + * @return Matrix The result of + * A multiplied by B transposed. + */ template inline auto @@ -39,6 +106,20 @@ A_mul_BTranspose(const Matrix &A, Base::Matrix::matrix_multiply_A_mul_SparseBTranspose(A.matrix, B.matrix)); } +/** + * @brief Multiplies diagonal matrix A by the transpose of matrix B. + * + * This function computes the matrix product of a diagonal matrix A and the + * transpose of matrix B, i.e., it returns the result of A * B^T. The resulting + * matrix will have the same number of rows as A (M) and the same number of rows + * as B (N). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in matrix A and columns in B. + * @param A The left-hand side diagonal matrix of size M. + * @param B The right-hand side matrix of size N x M (to be transposed). + * @return Matrix The result of A multiplied by B transposed. + */ template inline auto A_mul_BTranspose(const Matrix &A, const Matrix &B) @@ -48,6 +129,19 @@ inline auto A_mul_BTranspose(const Matrix &A, A.matrix * Base::Matrix::output_matrix_transpose(B.matrix)); } +/** + * @brief Multiplies diagonal matrix A by the transpose of diagonal matrix B. + * + * This function computes the matrix product of two diagonal matrices A and B, + * i.e., it returns the result of A * B^T. The resulting matrix will be a + * diagonal matrix of size M. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in both diagonal matrices. + * @param A The left-hand side diagonal matrix of size M. + * @param B The right-hand side diagonal matrix of size M (to be transposed). + * @return Matrix The result of A multiplied by B transposed. + */ template inline auto A_mul_BTranspose(const Matrix &A, const Matrix &B) @@ -56,6 +150,21 @@ inline auto A_mul_BTranspose(const Matrix &A, return Matrix(A.matrix * B.matrix); } +/** + * @brief Multiplies diagonal matrix A by the transpose of sparse matrix B. + * + * This function computes the matrix product of a diagonal matrix A and the + * transpose of a sparse matrix B, i.e., it returns the result of A * B^T. The + * resulting matrix will have the same number of rows as A (M) and the same + * number of rows as B (N). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in matrix A and columns in B. + * @param A The left-hand side diagonal matrix of size M. + * @param B The right-hand side sparse matrix of size N x M (to be transposed). + * @return Matrix The result of + * A multiplied by B transposed. + */ template inline auto A_mul_BTranspose(const Matrix &A, @@ -66,6 +175,22 @@ A_mul_BTranspose(const Matrix &A, A.matrix * Base::Matrix::output_matrix_transpose(B.matrix)); } +/** + * @brief Multiplies sparse matrix A by the transpose of matrix B. + * + * This function computes the matrix product of a sparse matrix A and the + * transpose of a dense matrix B, i.e., it returns the result of A * B^T. The + * resulting matrix will have the same number of rows as A (M) and the same + * number of rows as B (N). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @tparam N The number of columns in matrix B. + * @param A The left-hand side sparse matrix of size M x K. + * @param B The right-hand side dense matrix of size N x K (to be transposed). + * @return Matrix The result of A multiplied by B transposed. + */ template inline auto @@ -77,6 +202,23 @@ A_mul_BTranspose(const Matrix &A, Base::Matrix::matrix_multiply_SparseA_mul_BTranspose(A.matrix, B.matrix)); } +/** + * @brief Multiplies sparse matrix A by the transpose of diagonal matrix B. + * + * This function computes the matrix product of a sparse matrix A and the + * transpose of a diagonal matrix B, i.e., it returns the result of A * B^T. + * The resulting matrix will have the same number of rows as A (M) and the same + * number of rows as B (K). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @param A The left-hand side sparse matrix of size M x K. + * @param B The right-hand side diagonal matrix + * of size K (to be transposed). + * @return Matrix The result of + * A multiplied by B transposed. + */ template inline auto A_mul_BTranspose(const Matrix &A, @@ -86,6 +228,22 @@ A_mul_BTranspose(const Matrix &A, return Matrix(A.matrix * B.matrix); } +/** + * @brief Multiplies sparse matrix A by the transpose of sparse matrix B. + * + * This function computes the matrix product of two sparse matrices A and B, + * i.e., it returns the result of A * B^T. The resulting matrix will have the + * same number of rows as A (M) and the same number of rows as B (N). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @tparam N The number of columns in matrix B. + * @param A The left-hand side sparse matrix of size M x K. + * @param B The right-hand side sparse matrix of size N x K (to be transposed). + * @return Matrix The result of + * A multiplied by B transposed. + */ template inline auto @@ -110,6 +268,22 @@ using A_mul_BTranspose_Type = decltype(A_mul_BTranspose(std::declval(), std::declval())); /* (transpose matrix) * (matrix) */ + +/** + * @brief Multiplies the transpose of matrix A by matrix B. + * + * This function computes the matrix product of the transpose of A and B, + * i.e., it returns the result of A^T * B. The resulting matrix will have the + * same number of rows as A (M) and the same number of columns as B (K). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in matrix A. + * @tparam K The number of rows in matrix A and B. + * @tparam N The number of columns in matrix B. + * @param A The left-hand side matrix of size N x M (to be transposed). + * @param B The right-hand side matrix of size N x K. + * @return Matrix The result of A transposed multiplied by B. + */ template inline auto ATranspose_mul_B(const Matrix &A, const Matrix &B) @@ -119,6 +293,22 @@ inline auto ATranspose_mul_B(const Matrix &A, Base::Matrix::matrix_multiply_AT_mul_B(A.matrix, B.matrix)); } +/** + * @brief Multiplies the transpose of diagonal matrix A by matrix B. + * + * This function computes the matrix product of the transpose of a diagonal + * matrix A and B, i.e., it returns the result of A^T * B. The resulting matrix + * will have the same number of rows as A (M) and the same number of columns as + * B (K). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in matrix A and columns in B. + * @param A The left-hand side diagonal matrix + * of size M (to be transposed). + * @param B The right-hand side matrix of size N x M. + * @return Matrix The result of + * A transposed multiplied by B. + */ template inline auto ATranspose_mul_B(const Matrix &A, const Matrix &B) @@ -128,6 +318,23 @@ inline auto ATranspose_mul_B(const Matrix &A, Base::Matrix::output_matrix_transpose(A.matrix) * B.matrix); } +/** + * @brief Multiplies the transpose of sparse matrix A by matrix B. + * + * This function computes the matrix product of the transpose of a sparse + * matrix A and B, i.e., it returns the result of A^T * B. The resulting matrix + * will have the same number of rows as A (M) and the same number of columns as + * B (K). + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in matrix A. + * @tparam K The number of rows in matrix B. + * @tparam N The number of columns in matrix B. + * @param A The left-hand side sparse matrix of size N x M (to be transposed). + * @param B The right-hand side dense matrix of size N x K. + * @return Matrix The result of + * A transposed multiplied by B. + */ template inline auto @@ -139,6 +346,21 @@ ATranspose_mul_B(const Matrix &A, Base::Matrix::matrix_multiply_ATranspose_mul_SparseB(A.matrix, B.matrix)); } +/** + * @brief Multiplies the transpose of a diagonal matrix A with a dense matrix B. + * + * This function computes the product of the transpose of matrix A (assumed to + * be diagonal) and matrix B (assumed to be dense), returning the result as a + * dense matrix. + * + * @tparam T The data type of the matrix elements. + * @tparam K The number of rows in matrix B and the result. + * @tparam N The number of columns in matrix A and the result. + * @param A The input diagonal matrix (DefDiag) of size NxN. + * @param B The input dense matrix (DefDense) of size NxK. + * @return A dense matrix (DefDense) of size NxK representing the product A^T + * * B. + */ template inline auto ATranspose_mul_B(const Matrix &A, const Matrix &B) @@ -147,6 +369,21 @@ inline auto ATranspose_mul_B(const Matrix &A, return Matrix(A.matrix * B.matrix); } +/** + * @brief Multiplies the transpose of a diagonal matrix A with a diagonal matrix + * B. + * + * This function computes the product of the transpose of matrix A (assumed to + * be diagonal) and matrix B (also assumed to be diagonal), returning the result + * as a diagonal matrix. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in both diagonal matrices. + * @param A The input diagonal matrix (DefDiag) of size M. + * @param B The input diagonal matrix (DefDiag) of size M. + * @return A diagonal matrix (DefDiag) of size M representing the product A^T + * * B. + */ template inline auto ATranspose_mul_B(const Matrix &A, const Matrix &B) @@ -155,6 +392,22 @@ inline auto ATranspose_mul_B(const Matrix &A, return Matrix(A.matrix * B.matrix); } +/** + * @brief Multiplies the transpose of a diagonal matrix A with a sparse matrix + * B. + * + * This function computes the product of the transpose of matrix A (assumed to + * be diagonal) and matrix B (assumed to be sparse), returning the result as a + * sparse matrix. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in matrix A and columns in B. + * @tparam N The number of columns in matrix B. + * @param A The input diagonal matrix (DefDiag) of size M. + * @param B The input sparse matrix (DefSparse) of size N x M. + * @return A sparse matrix (DefSparse) of size M x N representing the product + * A^T * B. + */ template inline auto ATranspose_mul_B(const Matrix &A, @@ -164,6 +417,21 @@ ATranspose_mul_B(const Matrix &A, return Matrix(A.matrix * B.matrix); } +/** + * @brief Multiplies the transpose of a sparse matrix A with a dense matrix B. + * + * This function computes the product of the transpose of a sparse matrix A and + * a dense matrix B, returning the result as a dense matrix. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in matrix A and columns in B. + * @tparam K The number of rows in matrix B. + * @param A The input sparse matrix (DefSparse) of + * size N x M (to be transposed). + * @param B The input dense matrix (DefDense) of size N x K. + * @return A dense matrix (DefDense) of size M x K representing the product + * A^T * B. + */ template inline auto @@ -175,6 +443,26 @@ ATranspose_mul_B(const Matrix &A, Base::Matrix::matrix_multiply_SparseAT_mul_B(A.matrix, B.matrix)); } +/** + * @brief Multiplies the transpose of a sparse matrix A by a diagonal matrix B. + * + * This function computes the matrix multiplication of the transpose of matrix A + * (which is a sparse matrix of size N x M) with matrix B (a diagonal matrix of + * size N x N). The result is a sparse matrix of size M x N, with the sparsity + * pattern determined by SparseAvailableTranspose. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of columns in the original matrix A. + * @tparam N The number of rows in the original matrix A (and the size of the + * diagonal matrix B). + * @tparam SparseAvailable The sparsity pattern or trait for the input matrix A. + * + * @param A The input sparse matrix of type Matrix. + * @param B The input diagonal matrix of type Matrix. + * @return Matrix> + * The result of (A^T) * B as a sparse matrix. + */ template inline auto ATranspose_mul_B(const Matrix &A, @@ -186,6 +474,34 @@ ATranspose_mul_B(const Matrix &A, A.matrix)); } +/** + * @brief Multiplies the transpose of a sparse matrix A with another sparse + * matrix B. + * + * This function computes the matrix multiplication of the transpose of a sparse + * matrix A (of size N x M) with another sparse matrix B (of size N x K). The + * result is a sparse matrix of size M x K, with the sparsity pattern determined + * by SparseAvailableMatrixMultiply, + * SparseAvailable_B>. + * + * @tparam T The data type of the matrix elements. + * @tparam M The number of rows in the original matrix A. + * @tparam K The number of rows in the original matrix B. + * @tparam N The number of columns in both matrices A and B. + * @tparam SparseAvailable_A The sparsity pattern or trait for the input matrix + * A. + * @tparam SparseAvailable_B The sparsity pattern or trait for the input matrix + * B. + * + * @param A The input sparse matrix of type Matrix. + * @param B The input sparse matrix of type Matrix. + * @return Matrix, + * SparseAvailable_B>> + * The result of (A^T) * B as a sparse matrix. + */ template inline auto From 91d981c850118d61cdae217c6e1e259e2d422719 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 13:10:58 +0900 Subject: [PATCH 30/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/arithmetic_operation.cpp | 12 ++++++++++++ sample/cholesky.cpp | 12 ++++++++++++ sample/concatenation.cpp | 12 ++++++++++++ sample/eigen_values_vectors_complex.cpp | 12 ++++++++++++ sample/eigen_values_vectors_real.cpp | 12 ++++++++++++ sample/inverse.cpp | 12 ++++++++++++ sample/left_divide.cpp | 9 +++++++++ sample/matrix_action.cpp | 12 ++++++++++++ sample/qr.cpp | 9 +++++++++ 9 files changed, 102 insertions(+) diff --git a/sample/arithmetic_operation.cpp b/sample/arithmetic_operation.cpp index 9f0acc0..8c28e16 100644 --- a/sample/arithmetic_operation.cpp +++ b/sample/arithmetic_operation.cpp @@ -1,3 +1,15 @@ +/** + * @file arithmetic_operation.cpp + * @brief Demonstrates various matrix operations using custom matrix types in + * C++. + * + * This file contains a sample program that showcases the creation and + * manipulation of different types of matrices, including dense, diagonal, and + * sparse matrices. It demonstrates matrix initialization, arithmetic + * operations, transposition, conversion between matrix types, and the creation + * of matrices filled with zeros, ones, or a specific value. The program also + * illustrates how to print the contents of these matrices. + */ #include #include "python_numpy.hpp" diff --git a/sample/cholesky.cpp b/sample/cholesky.cpp index eae744b..03e5821 100644 --- a/sample/cholesky.cpp +++ b/sample/cholesky.cpp @@ -1,3 +1,15 @@ +/** + * @file cholesky.cpp + * @brief Demonstrates the usage of Cholesky decomposition and linear algebra + * solvers on various matrix types. + * + * This program creates several types of matrices (dense, diagonal, and sparse) + * and applies a Cholesky-based linear algebra solver. It showcases the process + * of solving a matrix using Cholesky decomposition, converting the result to a + * dense matrix, and performing further matrix operations such as multiplying + * the transpose of the solution with itself. The results are printed to the + * standard output. + */ #include #include "python_numpy.hpp" diff --git a/sample/concatenation.cpp b/sample/concatenation.cpp index ce3cbe0..0613194 100644 --- a/sample/concatenation.cpp +++ b/sample/concatenation.cpp @@ -1,3 +1,15 @@ +/** + * @file concatenation.cpp + * @brief Demonstrates matrix creation, concatenation, and update operations + * using various matrix types. + * + * This file contains a sample main function that showcases the creation and + * manipulation of different matrix types, including dense, diagonal, and sparse + * matrices. It demonstrates vertical and horizontal concatenation, block and + * tile concatenation, as well as updating concatenated matrices. The code also + * illustrates how to convert concatenated matrices to dense format for output + * and how to use type aliases for concatenated matrix types. + */ #include #include "python_numpy.hpp" diff --git a/sample/eigen_values_vectors_complex.cpp b/sample/eigen_values_vectors_complex.cpp index 2941058..1a36351 100644 --- a/sample/eigen_values_vectors_complex.cpp +++ b/sample/eigen_values_vectors_complex.cpp @@ -1,3 +1,15 @@ +/** + * @file eigen_values_vectors_complex.cpp + * @brief Demonstrates the computation of eigenvalues and eigenvectors for a 3x3 + * real matrix using custom linear algebra classes. + * + * This program constructs a 3x3 dense matrix, computes its eigenvalues and + * eigenvectors using a linear algebra solver, and prints the results in a + * format that displays both real and imaginary parts. It also verifies the + * eigen decomposition by checking the equation A * V = V * D, where A is the + * original matrix, V is the matrix of eigenvectors, and D is the diagonal + * matrix of eigenvalues. + */ #include #include "python_numpy.hpp" diff --git a/sample/eigen_values_vectors_real.cpp b/sample/eigen_values_vectors_real.cpp index a48c30c..d4a603f 100644 --- a/sample/eigen_values_vectors_real.cpp +++ b/sample/eigen_values_vectors_real.cpp @@ -1,3 +1,15 @@ +/** + * @file eigen_values_vectors_real.cpp + * @brief Example demonstrating the computation of eigenvalues and eigenvectors + * of a real matrix using the PythonNumpy library. + * + * This code constructs a 3x3 real matrix, computes its eigenvalues and + * eigenvectors using the LinalgSolverEigReal class from the PythonNumpy + * library, and prints the results. It also verifies the eigen decomposition by + * evaluating the difference between A*V and V*D, where A is the original + * matrix, V is the matrix of eigenvectors, and D is the diagonal matrix of + * eigenvalues. + */ #include #include "python_numpy.hpp" diff --git a/sample/inverse.cpp b/sample/inverse.cpp index 24e1f9c..f472814 100644 --- a/sample/inverse.cpp +++ b/sample/inverse.cpp @@ -1,3 +1,15 @@ +/** + * @file inverse.cpp + * @brief Demonstrates the usage of various matrix types and a linear algebra + * solver for matrix inversion. + * + * This file contains a sample C++ program that showcases the creation and + * manipulation of different matrix types, including dense, diagonal, and sparse + * matrices, using factory functions such as make_DenseMatrix, make_DiagMatrix, + * and make_SparseMatrix. It also demonstrates how to use a linear algebra + * solver (LinalgSolverInv) to compute the inverse of a dense matrix and output + * the result. + */ #include #include "python_numpy.hpp" diff --git a/sample/left_divide.cpp b/sample/left_divide.cpp index 354d22e..b2337e7 100644 --- a/sample/left_divide.cpp +++ b/sample/left_divide.cpp @@ -1,3 +1,12 @@ +/** + * @file left_divide.cpp + * @brief Demonstrates solving a linear system using custom matrix and solver + * classes. + * + * This example creates several types of matrices (dense, diagonal, sparse) + * using the PythonNumpy library, and solves a linear system of equations using + * a linear algebra solver. The result is printed to the console. + */ #include #include "python_numpy.hpp" diff --git a/sample/matrix_action.cpp b/sample/matrix_action.cpp index 7aef844..1597d25 100644 --- a/sample/matrix_action.cpp +++ b/sample/matrix_action.cpp @@ -1,3 +1,15 @@ +/** + * @file matrix_action.cpp + * @brief Demonstrates various matrix operations including reshaping, updating, + * and substitution using dense and sparse matrices. + * + * This program showcases the following functionalities: + * - Creation and reshaping of dense matrices. + * - Updating a reshaped matrix with new values. + * - Substituting values from a dense matrix into a sparse matrix. + * - Creating a dense matrix from a sparse matrix. + * - Substituting a smaller matrix into a larger matrix at a specified position. + */ #include #include "python_numpy.hpp" diff --git a/sample/qr.cpp b/sample/qr.cpp index 0b1ca9f..31693d4 100644 --- a/sample/qr.cpp +++ b/sample/qr.cpp @@ -1,3 +1,12 @@ +/** + * @file qr.cpp + * @brief Demonstrates QR decomposition using custom matrix and solver classes. + * + * This example constructs several types of matrices (dense, diagonal, and + * sparse) and performs QR decomposition on a dense matrix using a linear + * algebra solver. The code prints the resulting Q and R matrices, as well as + * their product, to verify the decomposition. + */ #include #include "python_numpy.hpp" From de2278b921badaaee5131ecf4875b2946c5dfc75 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Jun 2025 13:14:57 +0900 Subject: [PATCH 31/31] =?UTF-8?q?=E4=BD=9C=E6=88=90=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/arithmetic_operation.py | 6 ++++++ sample/cholesky.py | 7 +++++++ sample/concatenation.py | 9 +++++++++ sample/eigen_values_vectors_complex.py | 6 ++++++ sample/eigen_values_vectors_real.py | 7 +++++++ sample/inverse.py | 5 +++++ sample/left_divide.py | 6 ++++++ sample/matrix_action.py | 6 ++++++ sample/qr.py | 7 +++++++ 9 files changed, 59 insertions(+) diff --git a/sample/arithmetic_operation.py b/sample/arithmetic_operation.py index 414c5f8..451046c 100644 --- a/sample/arithmetic_operation.py +++ b/sample/arithmetic_operation.py @@ -1,4 +1,10 @@ +""" +This script demonstrates basic matrix operations using NumPy, including matrix multiplication, transposition, and zero matrix creation. +It also utilizes the `NumpyDeploy` class from the `python_numpy.numpy_deploy` module to generate C++ header files that define the matrices in C++ code. +The script prints the results of various matrix operations and outputs the filenames of the generated C++ code for each matrix. +""" import os + import sys sys.path.append(os.getcwd()) diff --git a/sample/cholesky.py b/sample/cholesky.py index 2661fee..f1190b7 100644 --- a/sample/cholesky.py +++ b/sample/cholesky.py @@ -1,5 +1,12 @@ +""" +This script demonstrates the use of NumPy to perform Cholesky decomposition on a symmetric positive-definite matrix. +It defines three matrices (A, B, and C), computes the Cholesky factorization of matrix A, and verifies the result by +multiplying the Cholesky factor with its transpose to reconstruct the original matrix. The script prints both the +Cholesky factor and the reconstructed matrix for validation. +""" import numpy as np + A = np.array([[2., 1., 3.], [1., 4., 2.], [3., 2., 6.]]) B = np.diag([1., 2., 3.]) C = np.array([[1., 0., 0.], [0., 8., 3.], [0., 3., 4.]]) diff --git a/sample/concatenation.py b/sample/concatenation.py index b022466..7153c96 100644 --- a/sample/concatenation.py +++ b/sample/concatenation.py @@ -1,5 +1,14 @@ +""" +This script demonstrates various NumPy array concatenation and tiling operations. +It creates several 3x3 matrices and performs the following operations: +- Vertical concatenation of two matrices using `np.concatenate` along axis 0. +- Horizontal concatenation of two matrices using `np.concatenate` along axis 1. +- Block matrix construction using `np.block` to combine four matrices into a larger matrix. +- Tiling of a matrix using `np.tile` to repeat the matrix in a grid pattern. +""" import numpy as np + A = np.array([[1., 2., 3.], [5., 4., 6.], [9., 8., 7.]]) B = np.diag([1., 2., 3.]) C = np.array([[1., 0., 0.], [3., 0., 8.], [0., 2., 4.]]) diff --git a/sample/eigen_values_vectors_complex.py b/sample/eigen_values_vectors_complex.py index 95bd318..e12f657 100644 --- a/sample/eigen_values_vectors_complex.py +++ b/sample/eigen_values_vectors_complex.py @@ -1,3 +1,9 @@ +""" +This script demonstrates how to compute the eigenvalues and eigenvectors of a real square matrix using NumPy. +It calculates the eigenvalues and eigenvectors of a predefined 3x3 matrix, prints them, and verifies the result +by checking the equation A * v = v * D, where A is the original matrix, v is the matrix of eigenvectors, and D +is the diagonal matrix of eigenvalues. +""" import numpy as np A_c = np.array([[1., 2., 3.], [3., 1., 2.], [2., 3., 1.]]) diff --git a/sample/eigen_values_vectors_real.py b/sample/eigen_values_vectors_real.py index 9415b78..fbe8dc6 100644 --- a/sample/eigen_values_vectors_real.py +++ b/sample/eigen_values_vectors_real.py @@ -1,5 +1,12 @@ +""" +This script demonstrates how to compute the eigenvalues and eigenvectors of a real square matrix using NumPy. +It defines a 3x3 real matrix, calculates its eigenvalues and eigenvectors, and prints them. +Additionally, it verifies the eigen decomposition by computing the difference between A @ V and V @ D, +where A is the original matrix, V is the matrix of eigenvectors, and D is the diagonal matrix of eigenvalues. +""" import numpy as np + A_r = np.array([[6., -3., 5.], [-1., 4., -5.], [-3., 3., -4.]]) eigen_values_r, eigen_vectors_r = np.linalg.eig(A_r) diff --git a/sample/inverse.py b/sample/inverse.py index 1a56526..c8b4ffb 100644 --- a/sample/inverse.py +++ b/sample/inverse.py @@ -1,5 +1,10 @@ +""" +This script demonstrates the calculation of the inverse of a square matrix using NumPy. +It defines several sample matrices, computes the inverse of one of them, and prints the result. +""" import numpy as np + A = np.array([[1., 2., 3.], [5., 4., 6.], [9., 8., 7.]]) A_s = np.array([[1., 2., 3.], [5., 4., 6.], [5., 4., 6.]]) B = np.diag([1., 2., 3.]) diff --git a/sample/left_divide.py b/sample/left_divide.py index ac59b93..491536b 100644 --- a/sample/left_divide.py +++ b/sample/left_divide.py @@ -1,5 +1,11 @@ +""" +This script demonstrates solving a system of linear equations using NumPy. +It defines three matrices: A, B, and C. The script then solves the matrix equation A * X = C for X using numpy.linalg.solve, +and prints the resulting matrix. The script also initializes a zero matrix for demonstration purposes. +""" import numpy as np + Zero = np.zeros((3, 3)) A = np.array([[1., 2., 3.], [5., 4., 6.], [9., 8., 7.]]) diff --git a/sample/matrix_action.py b/sample/matrix_action.py index 2b2bf86..f1d10d2 100644 --- a/sample/matrix_action.py +++ b/sample/matrix_action.py @@ -1,5 +1,11 @@ +""" +This script demonstrates basic matrix manipulations using NumPy. It includes: +- Reshaping a 4x4 matrix into an 8x2 matrix using column-major (Fortran-style) order. +- Substituting a 3x3 matrix into a submatrix of a 4x4 zero matrix. +""" import numpy as np + # reshape A = np.array([[16, 2, 3, 13], [5, 11, 10, 8], diff --git a/sample/qr.py b/sample/qr.py index 110d3ea..065ecda 100644 --- a/sample/qr.py +++ b/sample/qr.py @@ -1,5 +1,12 @@ +""" +This script demonstrates the use of NumPy to perform QR decomposition on a given matrix. +It defines three matrices (A, B, and C), computes the QR decomposition of matrix A, +and prints the resulting orthogonal matrix Q and upper triangular matrix R. +Finally, it verifies the decomposition by multiplying Q and R to reconstruct the original matrix A. +""" import numpy as np + A = np.array([[1., 2., 3.], [5., 4., 6.], [9., 8., 7.]]) B = np.diag([1., 2., 3.]) C = np.array([[1., 0., 0.], [3., 0., 8.], [0., 2., 4.]])