Comment rewrite

This commit is contained in:
2026-07-25 14:54:13 +02:00
parent 8fd1b7762e
commit a30b410f89
10 changed files with 583 additions and 625 deletions
+1 -1
View File
@@ -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<void>(sizeof(condition));
#endif
+39 -61
View File
@@ -31,74 +31,52 @@
* Functions to print out tensors with std::cout << x std::endl;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma one
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // 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 <typename T>
bool print_vector(const panic::tensor::vector<T>& 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 <typename T>
bool print_matrix(const panic::tensor::matrix<T>& A);
+223 -289
View File
@@ -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 <tensor/vector.hpp> // for panic::real_vector (uint_vector, int_vector)
#include <tensor/matrix.hpp> // 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 <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // 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<T>&
//
// k const <T>
//
// Outputs:
// c panic::tensor::vector<T>&
//
// 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 <typename T>
bool add(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& 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<T>&
//
// k const <T>
//
// Outputs:
// None.
//
// Returns:
// c panic::tensor::vector<T>
//
// 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 <typename T>
panic::tensor::vector<T> add(const panic::tensor::vector<T>& 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<T>&
//
// b const panic::tensor::vector<T>&
//
// Outputs:
// c panic::tensor::vector<T>&
//
// 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 <typename T>
bool add(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
// Description:
// Adds two panic::tensor::vector
// c(2) = a(2) + b(2)
//
// Inputs:
//
// a const panic::tensor::vector<T>&
//
// b const panic::tensor::vector<T>&
//
// Outputs:
// None.
//
// Returns:
// c panic::tensor::vector<T>
//
// 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 <typename T>
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& 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<T>&
//
// k const T
//
// Outputs:
// C panic::tensor::matrix<T>&
//
// 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 <typename T>
bool add(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& 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<T>&
//
// k const T
//
// Outputs:
// None.
//
// Returns:
// C panic::tensor::matrix<T>
//
// 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 <typename T>
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& 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<T>&
//
// B const panic::tensor::matrix<T>&
//
// Outputs:
// C panic::tensor::matrix<T>&
//
// 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 <typename T>
bool add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& 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<T>&
//
// B const panic::tensor::matrix<T>&
//
// Outputs:
// None.
//
// Returns:
// C panic::tensor::matrix<T>
//
// 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 <typename T>
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& 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<T>&
//
// b const panic::tensor::vector<T>&
//
// 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 <typename T>
bool add_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& 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<T>&
//
// b const panic::tensor::vector<T>&
//
// Outputs:
// None.
//
// Returns:
// C panic::tensor::matrix<T>&
//
// 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 <typename T>
panic::tensor::matrix<T> add_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& 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<T>&
//
// b const panic::tensor::vector<T>&
//
// 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 <typename T>
bool add_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& 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<T>&
//
// b const panic::tensor::vector<T>&
//
// Outputs:
// None.
//
// Returns:
// C panic::tensor::matrix<T>&
//
// 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 <typename T>
panic::tensor::matrix<T> add_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
+37 -47
View File
@@ -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 <typename T>
bool matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& 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 <typename T>
panic::tensor::matrix<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
+115 -57
View File
@@ -37,88 +37,154 @@
//---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // 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 <typename T>
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<panic::types::uint_t>;
} // namespace tensor
} // namespace panic
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
+1 -10
View File
@@ -32,18 +32,9 @@
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// TYPE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// Type Name : panic::tensor::vector
+37 -49
View File
@@ -35,10 +35,11 @@
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <io/print_tensor.hpp>
#include <iostream> // for std::cout, std::endl
//#include <io/print_tensor.hpp>
#include <tensor/vector.hpp> // for panic::tensor::vector
#include <tensor/matrix.hpp>
#include <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // 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 <typename T>
bool print_vector(const panic::tensor::vector<T>& 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<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& v
);
template bool print_vector<panic::types::int_t>(const panic::tensor::vector<panic::types::int_t>& v
);
template bool print_vector<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& 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 <typename T>
bool print_matrix(const panic::tensor::matrix<T>& 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;
}
}
}
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;
}
}
return true;
}
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;
}
}
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool print_matrix<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
);
template bool print_matrix<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
);
template bool print_matrix<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
);
} // namespace io
+83 -62
View File
@@ -38,19 +38,23 @@
#include <math/add.hpp>
#include <config/omp.hpp>
//#include <tensor/matrix.hpp> // for panic::tensor::real_matrix
#include <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // 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<T>& a, const T k, panic::tensor::vector<T>&
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<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::types::uint_t k,
panic::tensor::vector<panic::types::uint_t>& c
@@ -111,9 +118,12 @@ panic::tensor::vector<T> add(const panic::tensor::vector<T>& 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<panic::types::uint_t>
add(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::types::uint_t k
@@ -128,10 +138,6 @@ template panic::tensor::vector<panic::types::real_t>
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
@@ -156,9 +162,12 @@ bool add(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& 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<panic::types::uint_t>& a,
const panic::tensor::vector<panic::types::uint_t>& b,
panic::tensor::vector<panic::types::uint_t>& c
@@ -173,12 +182,6 @@ template bool add(const panic::tensor::vector<panic::types::real_t>& a,
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
@@ -195,9 +198,12 @@ panic::tensor::vector<T> add(const panic::tensor::vector<T>& 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<panic::types::uint_t>
add(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::tensor::vector<panic::types::uint_t>& b
@@ -212,8 +218,6 @@ template panic::tensor::vector<panic::types::real_t>
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
@@ -242,9 +246,12 @@ bool add(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>&
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<panic::types::uint_t>& A,
const panic::types::uint_t k,
panic::tensor::matrix<panic::types::uint_t>& C
@@ -277,9 +284,12 @@ panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& 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<panic::types::uint_t>
add(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::types::uint_t k
@@ -326,9 +336,12 @@ bool add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& 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<panic::types::uint_t>& A,
const panic::tensor::matrix<panic::types::uint_t>& B,
panic::tensor::matrix<panic::types::uint_t>& C
@@ -344,9 +357,6 @@ template bool add(const panic::tensor::matrix<panic::types::real_t>& A,
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
@@ -363,9 +373,12 @@ panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& 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<panic::types::uint_t>
add(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::matrix<panic::types::uint_t>& B
@@ -413,9 +426,12 @@ bool add_rowwise(const panic::tensor::matrix<T>& 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<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b,
panic::tensor::matrix<panic::types::uint_t>& C
@@ -448,9 +464,12 @@ panic::tensor::matrix<T> add_rowwise(const panic::tensor::matrix<T>& 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<panic::types::uint_t>
add_rowwise(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b
@@ -467,9 +486,6 @@ template panic::tensor::matrix<panic::types::real_t>
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add_colwise
//
@@ -504,9 +520,12 @@ bool add_colwise(const panic::tensor::matrix<T>& 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<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b,
panic::tensor::matrix<panic::types::uint_t>& C
@@ -539,9 +558,12 @@ panic::tensor::matrix<T> add_colwise(const panic::tensor::matrix<T>& 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<panic::types::uint_t>
add_colwise(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b
@@ -558,6 +580,5 @@ template panic::tensor::matrix<panic::types::real_t>
} // namespace math
} // namespace panic
+37 -30
View File
@@ -36,20 +36,24 @@
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <math/matmul.hpp>
#include <tensor/matrix.hpp> // for panic::tensor::real_matrix
#include <config/omp.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <tensor/matrix.hpp> // 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<T>& A, const panic::tensor::matrix<T>& 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 <typename T>
panic::tensor::matrix<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
panic::tensor::matrix<T> C;
if (!matmul(A,B,C)){
return panic::tensor::matrix<T>();
}
return C;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------
// Function : bool matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C)
template bool matmul<panic::types::uint_t>(
const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::matrix<panic::types::uint_t>& B,
@@ -147,8 +131,31 @@ template bool matmul<panic::types::real_t>(
panic::tensor::matrix<panic::types::real_t>& C
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::matmul
//
// Description:
// Multiply two matrices and returns the result.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
panic::tensor::matrix<T> C;
if (!matmul(A,B,C)){
return panic::tensor::matrix<T>();
}
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<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B)
template panic::tensor::matrix<panic::types::uint_t> matmul<panic::types::uint_t>(
const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::matrix<panic::types::uint_t>& B
+10 -19
View File
@@ -36,24 +36,20 @@
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <tensor/matrix.hpp>
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
#include <config/omp.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// 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<T>::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 <typename T>
matrix<T>::matrix(const matrix& other){
@@ -170,10 +164,7 @@ matrix<T>::~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 <typename T>
matrix<T>& matrix<T>::operator=(const matrix& other){