diff --git a/include/config/omp.hpp b/include/config/omp.hpp index 6fbf8b6..a688522 100644 --- a/include/config/omp.hpp +++ b/include/config/omp.hpp @@ -168,7 +168,7 @@ // // That means the same code still works on microcontrollers and non-OpenMP builds. #define PANIC_OMP_PARALLEL_FOR - #define PANIC_OMP_PARALLEL_FOR_IF(condition) + #define PANIC_OMP_PARALLEL_FOR_IF(condition) static_cast(sizeof(condition)); #endif diff --git a/include/io/print_tensor.hpp b/include/io/print_tensor.hpp index 1be4c39..c8e6228 100644 --- a/include/io/print_tensor.hpp +++ b/include/io/print_tensor.hpp @@ -31,74 +31,52 @@ * Functions to print out tensors with std::cout << x std::endl; * *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -#pragma one -//--------------------------------------------------------------------------------------------------------------------------- -// INCLUDE DESCRIPTION -//----------------------------------------------------------------------------------------------------- +#pragma once -//--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- +#include // for panic::vector +#include // for panic::matrix -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- -//--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE -//--------------------------------------------------------------------------------------------------------------------------- namespace panic{ namespace io{ -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::io::print_vector -// -// Description: -// Prints a vector out in the terminal -// -// Inputs: -// v const panic::tensor::uint_vector& -// const panic::tensor::int_vector& -// const panic::tensor::real_vector& -// vector to print -// -// Outputs: -// None. -// -// Returns: -// void -// -// Notes: -// The vector is printed out in square brackets. -//-------------------------------------------------------------------------------------------------------------------------- -void print_vector(const panic::tensor::uint_vector& v); -void print_vector(const panic::tensor::int_vector& v); -void print_vector(const panic::tensor::real_vector& v); +/** + * @brief prints out vector to iostream. + * + * Computes: + * @code + * print_vector(a) + * @endcode + * + * @tparam T Numeric element type. + * @param a Input vector. + * + * @return true if @p a was successfully printed. + * @return false if printing of @p c failed. + * + * @note N/A + */ +template +bool print_vector(const panic::tensor::vector& v); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::io::print_matrix -// -// Description: -// Prints a matrix out in the terminal -// -// Inputs: -// v const panic::tensor::uint_matrix& -// const panic::tensor::int_matrix& -// const panic::tensor::real_matrix& -// matrix to print -// -// Outputs: -// None. -// -// Returns: -// void -// -// Notes: -// The matrix is printed out in square brackets. -//-------------------------------------------------------------------------------------------------------------------------- -void print_matrix(const panic::tensor::uint_matrix& v); -void print_matrix(const panic::tensor::int_matrix& v); -void print_matrix(const panic::tensor::real_matrix& v); +/** + * @brief prints out matrix to iostream. + * + * Computes: + * @code + * print_matrix(a) + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * + * @return true if @p A was successfully printed. + * @return false if printing of @p C failed. + * + * @note N/A + */ +template +bool print_matrix(const panic::tensor::matrix& A); diff --git a/include/math/add.hpp b/include/math/add.hpp index 31f7387..f60beb0 100644 --- a/include/math/add.hpp +++ b/include/math/add.hpp @@ -1,4 +1,5 @@ /**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * * * PANIC * Portable Algorithms and Numerics In C++ @@ -30,333 +31,266 @@ * Description: * Functions to add panic::tensor togther; * - *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -#pragma one -//--------------------------------------------------------------------------------------------------------------------------- -// INCLUDE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- -#include // for panic::real_vector (uint_vector, int_vector) -#include // for panic::real_matrix (uint_matrix, int_matrix) -//--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- + *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * @file add.hpp + * @brief Public API for element-wise addition operations on PANIC vectors and matrices. + * + * This header contains the declarations that users of the math module should call. + * The comments here describe how each function is used, what dimensions are required, + * and what is returned on failure. + * + * Implementation details, OpenMP thresholds, and explicit template instantiations are + * kept in add.cpp. + * + *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +#pragma once -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- +#include // for panic::vector +#include // for panic::matrix -//--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE -//--------------------------------------------------------------------------------------------------------------------------- namespace panic{ namespace math{ -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds a scaler to a panic::tensor::vector -// c(2) = a(2) + k -// -// Inputs: -// -// a const panic::tensor::vector& -// -// k const -// -// Outputs: -// c panic::tensor::vector& -// -// Returns: -// bool -// -// Notes: -// The c vector is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- + +/** + * @brief Adds a scalar to every element of a vector. + * + * Computes: + * @code + * c[i] = a[i] + k + * @endcode + * + * @tparam T Numeric element type. + * @param a Input vector. + * @param k Scalar value added to each element of @p a. + * @param c Output vector. Resized to match @p a. + * + * @return true if @p c was resized and filled successfully. + * @return false if resizing @p c failed. + * + * @note This overload writes the result into an existing vector to avoid + * unnecessary temporary allocations. + */ template bool add(const panic::tensor::vector& a, const T k, panic::tensor::vector& c); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds a scaler to a panic::tensor::vector -// c(2) = a(2) + k -// -// Inputs: -// -// a const panic::tensor::vector& -// -// k const -// -// Outputs: -// None. -// -// Returns: -// c panic::tensor::vector -// -// Notes: -// Makes new vector and returns the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Returns a new vector containing a scalar added to every element. + * + * Computes: + * @code + * result[i] = a[i] + k + * @endcode + * + * @tparam T Numeric element type. + * @param a Input vector. + * @param k Scalar value added to each element of @p a. + * + * @return A new vector containing the result. + * @return An empty vector if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ template panic::tensor::vector add(const panic::tensor::vector& a, const T k); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds two panic::tensor::vector -// c(2) = a(2) + b(2) -// -// Inputs: -// -// a const panic::tensor::vector& -// -// b const panic::tensor::vector& -// -// Outputs: -// c panic::tensor::vector& -// -// Returns: -// bool -// -// Notes: -// If input vector dimentions are not correct, output is a empty vector and returns false. -// The c vector is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a vector elementwise too a vector. + * + * Computes: + * @code + * c[i] = a[i] + b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param a First vector. + * @param b Second vector. Must have size of @p a + * @param c Output vector. Resized to match @p a. + * + * @return true if @p c was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ template bool add(const panic::tensor::vector& a, const panic::tensor::vector& b, panic::tensor::vector& c); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds two panic::tensor::vector -// c(2) = a(2) + b(2) -// -// Inputs: -// -// a const panic::tensor::vector& -// -// b const panic::tensor::vector& -// -// Outputs: -// None. -// -// Returns: -// c panic::tensor::vector -// -// Notes: -// If input vector dimentions are not correct, return is an empty vector. -// Makes new vector and returns the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Returns a new vector containing a vector added elementwise. + * + * Computes: + * @code + * result[i] = a[i] + b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param a First vector. + * @param b Second vector. Must have size of @p a + * @param k Scalar value added to each element of @p a. + * + * @return A new vector containing the result. + * @return An empty vector if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ template panic::tensor::vector add(const panic::tensor::vector& a, const panic::tensor::vector& b); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds a scaler to a panic::tensor::matrix -// C(2,2) = A(2,2) + k -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// k const T -// -// Outputs: -// C panic::tensor::matrix& -// -// Returns: -// bool -// -// Notes: -// The C matrix is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a scalar to every element of a matix. + * + * Computes: + * @code + * C(i,j) = A(i,j) + k + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * @param k Scalar value added to each element of @p A. + * @param C Output Matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if resizing @p C failed. + * + * @note This overload writes the result into an existing vector to avoid + * unnecessary temporary allocations. + */ template bool add(const panic::tensor::matrix& A, const T k, panic::tensor::matrix& C); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds a scaler to a panic::tensor::matrix -// C(2,2) = A(2,2) + k -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// k const T -// -// Outputs: -// None. -// -// Returns: -// C panic::tensor::matrix -// -// Notes: -// Creates new matrix, C, for the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Returns a new matrix containing a scalar added to every element. + * + * Computes: + * @code + * result(i,j) = A(i,j) + k + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * @param k Scalar value added to each element of @p A. + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ template panic::tensor::matrix add(const panic::tensor::matrix& A, const T k); - -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds two panic::tensor::matrix -// C(2,2) = A(2,2) + B(2,2) -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// B const panic::tensor::matrix& -// -// Outputs: -// C panic::tensor::matrix& -// -// Returns: -// bool -// -// Notes: -// If input matrix dimentions are not correct, outputs an a empty matrix and returns false. -// The C matrix is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a matrix elementwise too a matrix. + * + * Computes: + * @code + * C(i,j) = A(i,j) + B(i,j) + * @endcode + * + * @tparam T Numeric element type. + * @param A First matrix. + * @param B Second matrix. Must have size of @p A. + * @param C Output matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ template bool add(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& C); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add -// -// Description: -// Adds two panic::tensor::matrix -// C(2,2) = A(2,2) + B(2,2) -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// B const panic::tensor::matrix& -// -// Outputs: -// None. -// -// Returns: -// C panic::tensor::matrix -// -// Notes: -// If input matrix dimentions are not correct, returns an a empty matrix. -// Creates new matrix, C, for the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Returns a new matrix containing a matrix added elementwise. + * + * Computes: + * @code + * result(i,j) = A(i,j) + B(i,j) + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * @param B Second matrix. Must have size of @p A. + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ template panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::tensor::matrix& B); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add_rowwise -// -// Description: -// Adds two panic::tensor::matrix -// C(2,3) = A(2,3) + b(2) -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// b const panic::tensor::vector& -// -// Outputs: -// C panic::tensor::matrix& -// -// Returns: -// bool -// -// Notes: -// If input vector dimentions are not correct, outputs an a empty matrix and returns false. -// The C matrix is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a vector rowwise too a matrix. + * + * Computes: + * @code + * C(i,j) = A(i,j) + b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.cols(). + * @param C Output matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ template bool add_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add_rowwise -// -// Description: -// Adds two panic::tensor::matrix -// C(2,3) = A(2,3) + b(2) -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// b const panic::tensor::vector& -// -// Outputs: -// None. -// -// Returns: -// C panic::tensor::matrix& -// -// Notes: -// If input vector dimentions are not correct, returns an a empty matrix. -// Creates new matrix, C, for the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a vector rowwise too a matrix. + * + * Computes: + * @code + * result(i,j) = A(i,j) + b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.cols(). + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ template panic::tensor::matrix add_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add_colwise -// -// Description: -// Adds two panic::tensor::matrix -// C(2,3) = A(2,3) + b(3) -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// b const panic::tensor::vector& -// -// Outputs: -// C panic::tensor::matrix& -// -// Returns: -// bool -// -// Notes: -// If input vector dimentions are not correct, outputs an a empty matrix and returns false. -// The C matrix is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a vector colwise too a matrix. + * + * Computes: + * @code + * C(i,j) = A(i,j) + b[j] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.rows(). + * @param C Output matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ template bool add_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::add_colwise -// -// Description: -// Adds two panic::tensor::matrix -// C(2,3) = A(2,3) + b(3) -// -// Inputs: -// -// A const panic::tensor::matrix& -// -// b const panic::tensor::vector& -// -// Outputs: -// None. -// -// Returns: -// C panic::tensor::matrix& -// -// Notes: -// If input vector dimentions are not correct, returns an a empty matrix. -// Creates new matrix, C, for the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Adds a vector colwise too a matrix. + * + * Computes: + * @code + * result(i,j) = A(i,j) + b[j] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.rows(). + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ template panic::tensor::matrix add_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b); diff --git a/include/math/matmul.hpp b/include/math/matmul.hpp index 3520e83..b44f01a 100644 --- a/include/math/matmul.hpp +++ b/include/math/matmul.hpp @@ -31,7 +31,7 @@ * Functions to print out tensors with std::cout << x std::endl; * *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ -#pragma one +#pragma once //--------------------------------------------------------------------------------------------------------------------------- // INCLUDE DESCRIPTION //----------------------------------------------------------------------------------------------------- @@ -50,55 +50,45 @@ namespace panic{ namespace math{ -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::matmul -// -// Description: -// Multiplies to panic::tensor::real_matrix -// C(2,4) = A(2,3) * B(3,4) -// -// Inputs: -// -// A const panic::tensor::real_matrix& -// -// B const panic::tensor::real_matrix& -// -// Outputs: -// C panic::tensor::real_matrix& -// -// Returns: -// bool -// -// Notes: -// If input matrix dimentions are not correct, output is a empty matrix and returns false. -// The C matrix is resized if nessessary. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Calculates matrix multiplication of two matrices. + * + * Computes: + * @code + * C(n, p) = A(n,m) + B(m,p) + * @endcode + * + * @tparam T Numeric element type. + * @param A First matrix. + * @param B Second matrix. @ p B.rows needs to be the size of @p A.cols. + * @param C Output matrix. Resized to C(A.rows, B.cols). + * + * @return true if @p C was resized and filled successfully. + * @return false if resizing @p C failed or A==B. + * + * @note This overload writes the result into an existing matrix to avoid + * unnecessary temporary allocations. + */ template bool matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& C); -//-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::matmul -// -// Description: -// Multiplies to panic::tensor::real_matrix -// C(2,4) = A(2,3) * B(3,4) -// -// Inputs: -// -// A const panic::tensor::real_matrix& -// -// B const panic::tensor::real_matrix& -// -// Outputs: -// None. -// -// Returns: -// C panic::tensor::real_matrix& -// -// Notes: -// If input matrix dimentions are not correct, returns an a empty matrix. -// Creates new matrix, C, for the result. -//-------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Returns a new matrix of multiplication of two matrices. + * + * Computes: + * @code + * result(n, p) = A(n,m) + B(m,p) + * @endcode + * + * @tparam T Numeric element type. + * @param A First matrix. + * @param B Second matrix. @ p B.rows needs to be the size of @p A.cols. + * + * @return A new matrix containing the result. + * @return An empty vector if the operation fails. + * + * @note This overload is convenient, but may allocate a new matrix. + */ template panic::tensor::matrix matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B); diff --git a/include/tensor/matrix.hpp b/include/tensor/matrix.hpp index 7022d44..6b9f5df 100644 --- a/include/tensor/matrix.hpp +++ b/include/tensor/matrix.hpp @@ -37,88 +37,154 @@ //--------------------------------------------------------------------------------------------------------------------------- #include // panic::uint_t, panic::int_t, and panic::real_t -//--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- -//--------------------------------------------------------------------------------------------------------------------------- -// TYPE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// Type Name : panic::tensor::matrix -// -// Description: -// Dynamic matrix storing T values. -// The matrix owns its memory and releases it in the destructor. -// -// Member Variables: -// n panic::uint_t -// Number of rows in the matrix. -// m panic::uint_t -// Number of columns in the matrix. -// -// data T* -// Pointer to the allocated matrix data. -// -// Notes: -// This matrix uses dynamic allocation with new[] and delete[]. -// Copying performs a deep copy. -//--------------------------------------------------------------------------------------------------------------------------- namespace panic{ namespace tensor{ +/** + * @brief struct for matrix object + * + * The struct is used for all PANIC libraries + * It uses dynamic allocation with new[] nad delete[]. + * Copying performs a deep copy. + */ template struct matrix{ + // Variable for number of rows panic::types::uint_t n; + // Variable for number of cols panic::types::uint_t m; + // Variable for length of data array panic::types::uint_t length; + // Pointer to data array T* data; - // empty contructor + /** + * @brief Empthy constructor + * + */ matrix(); - // contructor with size allocation + /** + * @brief Contructor with size allocation. + * + * @param rows Number of rows in the matrix + * @param cols Number of columns in the matrix + */ matrix(panic::types::uint_t rows, panic::types::uint_t cols); - // contructor with size allocation and sets it all to a value + /** + * @brief Contructor with size allocation and sets it all to a value. + * + * @param rows Number of rows in the matrix + * @param cols Number of columns in the matrix + * @param value all the values in the matrix. + */ matrix(panic::types::uint_t rows, panic::types::uint_t cols, T value); - // copy-contructor - // matrix A(3,3); - // matrix B = A; + /** + * @brief Copy-contructor. + * + * Computes: + * @code + * matrix A(3,3); + * matrix B = A; + * @endcode + * + */ matrix(const matrix& other); - // de-contructor, releases memory + /** + * @brief De-contructor, releases memory. + * + * @note Releases memory. + */ ~matrix(); - // copy-assignment - //matrix A(5); - //matrix B(3); - //B = A; + /** + * @brief Copy-assignment. + * + * Computes: + * @code + * matrix A(3,3); + * matrix B(5,8); + * B = A; + * @endcode + * + */ matrix& operator=(const matrix& other); - // function returns rows. - // const tells compiler that the fuction don't edit the objert. + /** + * @brief Returns number of rows. + * + * Computes: + * @code + * n = A.rows(); + * @endcode + * + * @note The const tells compiler that the fuction don't edit the objert. + */ panic::types::uint_t rows() const; - // function returns columns. - // const tells compiler that the fuction don't edit the objert. + /** + * @brief Returns number of columns. + * + * Computes: + * @code + * m = A.cols(); + * @endcode + * + * @note The const tells compiler that the fuction don't edit the objert. + */ panic::types::uint_t cols() const; - // function to resize data vector + /** + * @brief Function to resize data matrix. + * + * Computes: + * @code + * A.resize(3,4); + * @endcode + * + * @note It hold all the privious functions if avaliable. + */ bool resize(panic::types::uint_t new_n, panic::types::uint_t new_m); - // fill data with value + /** + * @brief Fill data with value. + * + * Computes: + * @code + * A.fill(3.1415); + * @endcode + * + * @note Sets all values in the matrix. + */ bool fill(T value); - // lets you read and write matrix data using row and column - // Example: - // A(1, 2) - // This is unchecked and fast. + /** + * @brief Read and write matrix data using row and column + * + * Computes: + * @code + * n = A(1,2); + * @endcode + * + * @note This is unchecked and fast. + */ T& operator()(panic::types::uint_t index_n, panic::types::uint_t index_m); - // lets you read matrix data using row and column from a const matrix + + /** + * @brief Read from const matrix data using row and column + * + * Computes: + * @code + * n = const A(1,2); + * @endcode + * + * @note This is unchecked and fast. + */ const T& operator()(panic::types::uint_t index_n, panic::types::uint_t index_m) const; }; @@ -144,12 +210,4 @@ extern template struct matrix; } // namespace tensor } // namespace panic -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE -//--------------------------------------------------------------------------------------------------------------------------- - diff --git a/include/tensor/vector.hpp b/include/tensor/vector.hpp index e2fe6f1..e6d45fb 100644 --- a/include/tensor/vector.hpp +++ b/include/tensor/vector.hpp @@ -32,18 +32,9 @@ * *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ #pragma once -//--------------------------------------------------------------------------------------------------------------------------- -// INCLUDE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- + #include // panic::uint_t, panic::int_t, and panic::real_t -//--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// TYPE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- // Type Name : panic::tensor::vector diff --git a/src/io/print_tensor.cpp b/src/io/print_tensor.cpp index 5026c04..6e45b83 100644 --- a/src/io/print_tensor.cpp +++ b/src/io/print_tensor.cpp @@ -35,10 +35,11 @@ //--------------------------------------------------------------------------------------------------------------------------- // INCLUDE DESCRIPTION //----------------------------------------------------------------------------------------------------- +#include #include // for std::cout, std::endl -//#include -#include // for panic::tensor::vector -#include + +#include // for panic::vector +#include // for panic::matrix //--------------------------------------------------------------------------------------------------------------------------- // DEFINE DESCRIPTION @@ -61,32 +62,32 @@ namespace panic { // // Description: // The vector is printed out in square brackets. -// Overloaded for panic::uint, panic::int and panic::real //-------------------------------------------------------------------------------------------------------------------------- -void print_vector(const panic::tensor::uint_vector& v){ +template +bool print_vector(const panic::tensor::vector& v){ std::cout << "["; for (panic::types::uint_t i = 0; i < v.size()-1; ++i){ std::cout << v[i] << ", "; } std::cout << v[v.size()-1]<< "]" << std::endl; -} -void print_vector(const panic::tensor::int_vector& v){ - std::cout << "["; - for (panic::types::uint_t i = 0; i < v.size()-1; ++i){ - std::cout << v[i] << ", "; - } - std::cout << v[v.size()-1]<< "]" << std::endl; + return true; } -void print_vector(const panic::tensor::real_vector& v){ - std::cout << "["; - for (panic::types::uint_t i = 0; i < v.size()-1; ++i){ - std::cout << v[i] << ", "; - } - std::cout << v[v.size()-1]<< "]" << std::endl; +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template bool print_vector(const panic::tensor::vector& v +); +template bool print_vector(const panic::tensor::vector& v +); +template bool print_vector(const panic::tensor::vector& v +); + -} @@ -95,9 +96,9 @@ void print_vector(const panic::tensor::real_vector& v){ // // Description: // The matrix is printed out in square brackets. -// Overloaded for panic::uint, panic::int and panic::real //-------------------------------------------------------------------------------------------------------------------------- -void print_matrix(const panic::tensor::uint_matrix& A){ +template +bool print_matrix(const panic::tensor::matrix& A){ std::cout << "["; for (panic::types::uint_t i = 0; i < A.rows(); ++i){ std::cout << "["; @@ -110,37 +111,24 @@ void print_matrix(const panic::tensor::uint_matrix& A){ std::cout << A(A.rows()-1,A.cols()-1) << "]]" << std::endl; } } + + return true; } -void print_matrix(const panic::tensor::int_matrix& A){ - std::cout << "["; - for (panic::types::uint_t i = 0; i < A.rows(); ++i){ - std::cout << "["; - for (panic::types::uint_t j = 0; j < A.cols(); ++j){ - std::cout << A(i,j) << ", "; - } - if (i < A.rows()-1){ - std::cout << A(A.rows()-1,A.cols()-1) << "]" << std::endl; - }else{ - std::cout << A(A.rows()-1,A.cols()-1) << "]]" << std::endl; - } - } -} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template bool print_matrix(const panic::tensor::matrix& A +); +template bool print_matrix(const panic::tensor::matrix& A +); +template bool print_matrix(const panic::tensor::matrix& A +); + -void print_matrix(const panic::tensor::real_matrix& A){ - std::cout << "["; - for (panic::types::uint_t i = 0; i < A.rows(); ++i){ - std::cout << "["; - for (panic::types::uint_t j = 0; j < A.cols(); ++j){ - std::cout << A(i,j) << ", "; - } - if (i < A.rows()-1){ - std::cout << A(A.rows()-1,A.cols()-1) << "]" << std::endl; - }else{ - std::cout << A(A.rows()-1,A.cols()-1) << "]]" << std::endl; - } - } -} } // namespace io diff --git a/src/math/add.cpp b/src/math/add.cpp index 107f9d0..a72fd84 100644 --- a/src/math/add.cpp +++ b/src/math/add.cpp @@ -38,19 +38,23 @@ #include #include -//#include // for panic::tensor::real_matrix + +#include // for panic::vector +#include // for panic::matrix //--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION +// PRIVATE CONSTANTS //--------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Minimum number of element operations before using the OpenMP-enabled loop. + * + * Small vectors and matrices are kept serial because the overhead of starting + * worker threads can be larger than the work itself. + */ static const panic::types::uint_t add_omp_min_work = 10000; //--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE +// INPLEMENTATION //--------------------------------------------------------------------------------------------------------------------------- namespace panic { @@ -77,9 +81,12 @@ bool add(const panic::tensor::vector& a, const T k, panic::tensor::vector& return true; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template bool add(const panic::tensor::vector& a, const panic::types::uint_t k, panic::tensor::vector& c @@ -111,9 +118,12 @@ panic::tensor::vector add(const panic::tensor::vector& a, const T k){ return c; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::vector add(const panic::tensor::vector& a, const panic::types::uint_t k @@ -128,10 +138,6 @@ template panic::tensor::vector ); - - - - //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::add // @@ -156,9 +162,12 @@ bool add(const panic::tensor::vector& a, const panic::tensor::vector& b, p return true; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template bool add(const panic::tensor::vector& a, const panic::tensor::vector& b, panic::tensor::vector& c @@ -173,12 +182,6 @@ template bool add(const panic::tensor::vector& a, ); - - - - - - //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::add // @@ -195,9 +198,12 @@ panic::tensor::vector add(const panic::tensor::vector& a, const panic::ten return c; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::vector add(const panic::tensor::vector& a, const panic::tensor::vector& b @@ -212,8 +218,6 @@ template panic::tensor::vector ); - - //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::add // @@ -242,9 +246,12 @@ bool add(const panic::tensor::matrix& A, const T k, panic::tensor::matrix& return true; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template bool add(const panic::tensor::matrix& A, const panic::types::uint_t k, panic::tensor::matrix& C @@ -277,9 +284,12 @@ panic::tensor::matrix add(const panic::tensor::matrix& A, const T k){ return C; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::types::uint_t k @@ -326,9 +336,12 @@ bool add(const panic::tensor::matrix& A, const panic::tensor::matrix& B, p return true; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template bool add(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& C @@ -344,9 +357,6 @@ template bool add(const panic::tensor::matrix& A, - - - //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::add // @@ -363,9 +373,12 @@ panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::ten return C; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::tensor::matrix& B @@ -413,9 +426,12 @@ bool add_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector< return true; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template bool add_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C @@ -448,9 +464,12 @@ panic::tensor::matrix add_rowwise(const panic::tensor::matrix& A, const pa return C; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::matrix add_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b @@ -467,9 +486,6 @@ template panic::tensor::matrix - - - //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::add_colwise // @@ -504,9 +520,12 @@ bool add_colwise(const panic::tensor::matrix& A, const panic::tensor::vector< return true; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template bool add_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C @@ -539,9 +558,12 @@ panic::tensor::matrix add_colwise(const panic::tensor::matrix& A, const pa return C; } -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::matrix add_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b @@ -558,6 +580,5 @@ template panic::tensor::matrix - } // namespace math } // namespace panic diff --git a/src/math/matmul.cpp b/src/math/matmul.cpp index 5a695bc..81dd4b1 100644 --- a/src/math/matmul.cpp +++ b/src/math/matmul.cpp @@ -36,20 +36,24 @@ // INCLUDE DESCRIPTION //----------------------------------------------------------------------------------------------------- #include -#include // for panic::tensor::real_matrix #include -//--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- +#include // for panic::tensor::matrix + //--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION +// PRIVATE CONSTANTS //--------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Minimum number of element operations before using the OpenMP-enabled loop. + * + * Small vectors and matrices are kept serial because the overhead of starting + * worker threads can be larger than the work itself. + */ static const panic::types::uint_t matmul_omp_min_work = 10000; //--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE +// INPLEMENTATION //--------------------------------------------------------------------------------------------------------------------------- namespace panic { @@ -103,32 +107,12 @@ bool matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B } return true; } - //-------------------------------------------------------------------------------------------------------------------------- -// Function Name : panic::math::matmul +// EXPLICIT TEMPLATE INSTANTIATION // -// Description: -// Multiply two matrices and returns the result. +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. //-------------------------------------------------------------------------------------------------------------------------- -template -panic::tensor::matrix matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B){ - - panic::tensor::matrix C; - - if (!matmul(A,B,C)){ - return panic::tensor::matrix(); - } - - return C; -} - - -//--------------------------------------------------------------------------------------------------------------------------- -// EXPLICIT TEMPLATE DECLARATION -//--------------------------------------------------------------------------------------------------------------------------- - -//-------------------------------------------------------------------------------------------------------------------------- -// Function : bool matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& C) template bool matmul( const panic::tensor::matrix& A, const panic::tensor::matrix& B, @@ -147,8 +131,31 @@ template bool matmul( panic::tensor::matrix& C ); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::matmul +// +// Description: +// Multiply two matrices and returns the result. +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B){ + + panic::tensor::matrix C; + + if (!matmul(A,B,C)){ + return panic::tensor::matrix(); + } + + return C; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. //-------------------------------------------------------------------------------------------------------------------------- -// Function : panic::tensor::matrix matmul(const panic::tensor::matrix& A, const panic::tensor::matrix& B) template panic::tensor::matrix matmul( const panic::tensor::matrix& A, const panic::tensor::matrix& B diff --git a/src/tensor/matrix.cpp b/src/tensor/matrix.cpp index 548495d..ef1b214 100644 --- a/src/tensor/matrix.cpp +++ b/src/tensor/matrix.cpp @@ -36,24 +36,20 @@ // INCLUDE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- #include +#include // panic::uint_t, panic::int_t, and panic::real_t #include //--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// TYPE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION +// PRIVATE CONSTANTS //--------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Minimum number of element operations before using the OpenMP-enabled loop. + * + * Small vectors and matrices are kept serial because the overhead of starting + * worker threads can be larger than the work itself. + */ static const panic::types::uint_t matrix_omp_min_size = 10000; -//--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE -//--------------------------------------------------------------------------------------------------------------------------- namespace panic{ namespace tensor{ @@ -127,9 +123,7 @@ matrix::matrix(panic::types::uint_t rows, panic::types::uint_t cols, T value) // Constructor Name : panic::tensor::matrix::matrix // // Description: -// Copy-contructor, makes a deep copy of another matrix like this: -// matrix A(3); -// matrix B = A; +// Copy-contructor, makes a deep copy of another matrix //-------------------------------------------------------------------------------------------------------------------------- template matrix::matrix(const matrix& other){ @@ -170,10 +164,7 @@ matrix::~matrix(){ // Function Name : panic::tensor::matrix::operator= // // Description: -// Copy-assignment. Copies from another matrix like this: -// matrix A(5); -// matrix B(3); -// B = A; +// Copy-assignment. Copies from another matrix. //-------------------------------------------------------------------------------------------------------------------------- template matrix& matrix::operator=(const matrix& other){