openGPMP
Open Source Mathematics Package
Public Member Functions | List of all members
gpmp::linalg::Mtx Class Reference

Mtx class offers matrix arithmetic operations. Some methods are element-wise while others make use of grouping and unrolling. More...

#include <mtx.hpp>

Public Member Functions

template<typename T >
void std_mtx_add (const T *A, const T *B, T *C, int rows, int cols)
 Perform matrix addition on two matrices as flat arrays. More...
 
template<typename T >
void std_mtx_sub (const T *A, const T *B, T *C, int rows, int cols)
 Perform matrix subtraction on two matrices as flat arrays. More...
 
template<typename T , typename U >
void std_mtx_mult (const T *A, const T *B, U *C, int rows_a, int cols_a, int cols_b)
 
template<typename T >
void std_mtx_tpose (const T *A, T *At, int rows, int cols)
 
template<typename T >
void std_mtx_add (const std::vector< T > &A, const std::vector< T > &B, std::vector< T > &C)
 Perform matrix addition on two matrices as flat vectors. More...
 
template<typename T >
void std_mtx_add (const std::vector< std::vector< T >> &A, const std::vector< std::vector< T >> &B, std::vector< std::vector< T >> &C)
 Perform matrix addition on two matrices as 2D vectors. More...
 
template<typename T >
void std_mtx_sub (const std::vector< std::vector< T >> &A, const std::vector< std::vector< T >> &B, std::vector< std::vector< T >> &C)
 Perform matrix subtraction on two matrices as 2D vectors. More...
 
template<typename T >
void std_mtx_mult (const std::vector< std::vector< T >> &A, const std::vector< std::vector< T >> &B, std::vector< std::vector< T >> &C)
 Perform matrix multiplication on two matrices 2D vectors. More...
 

Detailed Description

Mtx class offers matrix arithmetic operations. Some methods are element-wise while others make use of grouping and unrolling.

Definition at line 57 of file mtx.hpp.

Member Function Documentation

◆ std_mtx_add() [1/3]

template<typename T >
void gpmp::linalg::Mtx::std_mtx_add ( const std::vector< std::vector< T >> &  A,
const std::vector< std::vector< T >> &  B,
std::vector< std::vector< T >> &  C 
)

Perform matrix addition on two matrices as 2D vectors.

Parameters
AInput matrix A
BInput matrix B
COutput matrix C This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 89 of file mtx_naive.cpp.

91  {
92  const int size = A.size();
93 
94  for (int i = 0; i < size; ++i) {
95  for (int j = 0; j < size; ++j) {
96  // perform matrix addition
97  C[i][j] = A[i][j] + B[i][j];
98  }
99  }
100 }
list C
Definition: linalg.py:24
list A
Definition: linalg.py:22
list B
Definition: linalg.py:23

References python.linalg::A, python.linalg::B, and python.linalg::C.

◆ std_mtx_add() [2/3]

template<typename T >
void gpmp::linalg::Mtx::std_mtx_add ( const std::vector< T > &  A,
const std::vector< T > &  B,
std::vector< T > &  C 
)

Perform matrix addition on two matrices as flat vectors.

Parameters
AInput matrix A
BInput matrix B
COutput matrix C This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 53 of file mtx_naive.cpp.

55  {
56  // MTX A AND B MUST BE SAME SIZE
57  int rows = A.size();
58  int cols = rows > 0 ? A.size() / rows : 0;
59 
60  for (int i = 0; i < rows; ++i) {
61  for (int j = 0; j < cols; ++j) {
62  // perform matrix addition
63  C[i * cols + j] = A[i * cols + j] + B[i * cols + j];
64  }
65  }
66 }

References python.linalg::A, python.linalg::B, python.linalg::C, test_linalg::cols, and test_linalg::rows.

◆ std_mtx_add() [3/3]

template<typename T >
void gpmp::linalg::Mtx::std_mtx_add ( const T *  A,
const T *  B,
T *  C,
int  rows,
int  cols 
)
inline

Perform matrix addition on two matrices as flat arrays.

Parameters
AInput matrix A
BInput matrix B
COutput matrix C
rowsNumber of rows
colsNumber of columns This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 510 of file mtx.hpp.

510  {
511  // MTX A AND B MUST BE SAME SIZE
512  for (int i = 0; i < rows; ++i) {
513  for (int j = 0; j < cols; ++j) {
514  // perform matrix addition
515  C[i * cols + j] = A[i * cols + j] + B[i * cols + j];
516  }
517  }
518  }

References python.linalg::A, python.linalg::B, python.linalg::C, test_linalg::cols, and test_linalg::rows.

◆ std_mtx_mult() [1/2]

template<typename T >
void gpmp::linalg::Mtx::std_mtx_mult ( const std::vector< std::vector< T >> &  A,
const std::vector< std::vector< T >> &  B,
std::vector< std::vector< T >> &  C 
)

Perform matrix multiplication on two matrices 2D vectors.

Parameters
AInput matrix A
BInput matrix B
COutput matrix C This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 151 of file mtx_naive.cpp.

153  {
154  assert(A.size() == B.size());
155  assert(A[0].size() == B[0].size());
156 
157  int64_t nrows = A.size();
158  int64_t ncols = A[0].size();
159 
160  for (int64_t i = 0; i < nrows; ++i) {
161  for (int64_t j = 0; j < ncols; ++j) {
162  C[i][j] = 0.0;
163  for (int64_t k = 0; k < ncols; ++k) {
164  C[i][j] += A[i][k] * B[k][j];
165  }
166  }
167  }
168 }

References python.linalg::A, python.linalg::B, and python.linalg::C.

◆ std_mtx_mult() [2/2]

template<typename T , typename U >
void gpmp::linalg::Mtx::std_mtx_mult ( const T *  A,
const T *  B,
U *  C,
int  rows_a,
int  cols_a,
int  cols_b 
)
inline

Definition at line 540 of file mtx.hpp.

545  {
546  for (int i = 0; i < rows_a; ++i) {
547  for (int j = 0; j < cols_b; ++j) {
548  U sum = 0; // Use T type for sum
549  for (int k = 0; k < cols_a; ++k) {
550  sum += A[i * cols_a + k] * B[k * cols_b + j];
551  }
552  C[i * cols_b + j] = sum;
553  }
554  }
555  }

References python.linalg::A, python.linalg::B, and python.linalg::C.

◆ std_mtx_sub() [1/2]

template<typename T >
void gpmp::linalg::Mtx::std_mtx_sub ( const std::vector< std::vector< T >> &  A,
const std::vector< std::vector< T >> &  B,
std::vector< std::vector< T >> &  C 
)

Perform matrix subtraction on two matrices as 2D vectors.

Parameters
AInput matrix A
BInput matrix B
COutput matrix C This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 120 of file mtx_naive.cpp.

122  {
123  const int size = A.size();
124 
125  for (int i = 0; i < size; ++i) {
126  for (int j = 0; j < size; ++j) {
127  // Perform matrix subtraction
128  C[i][j] = A[i][j] - B[i][j];
129  }
130  }
131 }

References python.linalg::A, python.linalg::B, and python.linalg::C.

◆ std_mtx_sub() [2/2]

template<typename T >
void gpmp::linalg::Mtx::std_mtx_sub ( const T *  A,
const T *  B,
T *  C,
int  rows,
int  cols 
)
inline

Perform matrix subtraction on two matrices as flat arrays.

Parameters
AInput matrix A
BInput matrix B
COutput matrix C
rowsNumber of rows
colsNumber of columns This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 529 of file mtx.hpp.

529  {
530  // MTX A AND B MUST BE SAME SIZE
531  for (int i = 0; i < rows; ++i) {
532  for (int j = 0; j < cols; ++j) {
533  // perform matrix addition
534  C[i * cols + j] = A[i * cols + j] - B[i * cols + j];
535  }
536  }
537  }

References python.linalg::A, python.linalg::B, python.linalg::C, test_linalg::cols, and test_linalg::rows.

◆ std_mtx_tpose()

template<typename T >
void gpmp::linalg::Mtx::std_mtx_tpose ( const T *  A,
T *  At,
int  rows,
int  cols 
)
inline

Definition at line 558 of file mtx.hpp.

558  {
559  for (int i = 0; i < rows; ++i) {
560  for (int j = 0; j < cols; ++j) {
561  At[j * rows + i] = A[i * cols + j];
562  }
563  }
564  }

References python.linalg::A, test_linalg::cols, and test_linalg::rows.


The documentation for this class was generated from the following files: