Comment rewrite
This commit is contained in:
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user