openGPMP
Open Source Mathematics Package
Public Member Functions | Public Attributes | List of all members
gpmp::linalg::Matrix< Type > Class Template Reference

Matrix and Scalar operations. More...

#include <mtx_tmpl.hpp>

Public Member Functions

 Matrix (size_t mtx_rows, size_t mtx_cols)
 Matrix Class constructor initializing empty vector. More...
 
 Matrix ()
 
Type & operator() (size_t row, size_t col)
 Overload operator. More...
 
Matrix mult (Matrix &target)
 Matrix Multiplication. More...
 
Matrix scalar_mult (Type scalar)
 
Matrix hadamard (Matrix &target)
 
Matrix sqr_err ()
 
Matrix add (Matrix &target)
 
Matrix operator+ (Matrix &target)
 
Matrix scalar_add (Type scalar)
 
Matrix operator- ()
 
Matrix sub (Matrix &target)
 
Matrix operator- (Matrix &target)
 
Matrix< unsigned short > operator== (Matrix &target)
 
bool all ()
 
Matrix transpose ()
 
Matrix T ()
 
Matrix sum ()
 
Matrix sum (size_t dimension)
 
Matrix mean ()
 
Matrix mean (size_t dimension)
 
Matrix concatenate (Matrix target, size_t dimension)
 
Matrix diag ()
 
Matrix apply_func (const std::function< Type(const Type &)> &function)
 
void print_shape ()
 
void print_mtx ()
 
void fill_index (Type val)
 

Public Attributes

size_t cols
 
size_t rows
 
std::vector< Type > data
 
std::tuple< size_t, size_t > dim
 
int64_t num_elements = rows * cols
 

Detailed Description

template<typename Type>
class gpmp::linalg::Matrix< Type >

Matrix and Scalar operations.

Definition at line 61 of file mtx_tmpl.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/2]

template<typename Type >
gpmp::linalg::Matrix< Type >::Matrix ( size_t  mtx_rows,
size_t  mtx_cols 
)
inline

Matrix Class constructor initializing empty vector.

Parameters
[in]rows: size of rows
[in]cols: size of columns

Definition at line 77 of file mtx_tmpl.hpp.

78  : cols(cols), rows(rows), data({}) {
79  // initialize an empty vector for storage
80  data.resize(cols * rows, Type());
81  dim = std::make_tuple(rows, cols);
82  }
std::vector< Type > data
Definition: mtx_tmpl.hpp:66
std::tuple< size_t, size_t > dim
Definition: mtx_tmpl.hpp:67

◆ Matrix() [2/2]

template<typename Type >
gpmp::linalg::Matrix< Type >::Matrix ( )
inline

Definition at line 83 of file mtx_tmpl.hpp.

83  : cols(0), rows(0), data({}) {
84  dim = {rows, cols};
85  };

Member Function Documentation

◆ add()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::add ( Matrix< Type > &  target)
inline

Definition at line 165 of file mtx_tmpl.hpp.

165  {
166  assert(dim == target.dim);
167  Matrix res(rows, target.cols);
168 
169  for (size_t r = 0; r < res.rows; ++r) {
170  for (size_t c = 0; c < res.cols; ++c) {
171  res(r, c) = (*this)(r, c) + target(r, c);
172  }
173  }
174  return res;
175  }

References gpmp::linalg::Matrix< Type >::cols, gpmp::linalg::Matrix< Type >::dim, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

Referenced by gpmp::linalg::Matrix< Type >::operator+(), and gpmp::linalg::Matrix< Type >::sub().

◆ all()

template<typename Type >
bool gpmp::linalg::Matrix< Type >::all ( )
inline

Definition at line 235 of file mtx_tmpl.hpp.

235  {
236  int64_t counter{0};
237 
238  for (int64_t r = 0; r < rows; ++r) {
239  for (int64_t c = 0; c < cols; ++c) {
240  if ((*this)(r, c))
241  counter++;
242  }
243  }
244  return (counter == num_elements);
245  }

References gpmp::linalg::Matrix< Type >::cols, gpmp::linalg::Matrix< Type >::num_elements, and gpmp::linalg::Matrix< Type >::rows.

◆ apply_func()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::apply_func ( const std::function< Type(const Type &)> &  function)
inline

Definition at line 368 of file mtx_tmpl.hpp.

368  {
369  Matrix res((*this));
370 
371  for (size_t r = 0; r < rows; ++r) {
372  for (size_t c = 0; c < cols; ++c) {
373  res(r, c) = function((*this)(r, c));
374  }
375  }
376  return res;
377  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ concatenate()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::concatenate ( Matrix< Type >  target,
size_t  dimension 
)
inline

Definition at line 320 of file mtx_tmpl.hpp.

320  {
321  (dimension == 0) ? assert(rows == target.rows)
322  : assert(cols == target.cols);
323 
324  auto res = (dimension == 0) ? Matrix{rows + target.rows, cols}
325  : Matrix{rows, cols + target.cols};
326 
327  // copy self
328  for (size_t r = 0; r < rows; ++r) {
329  for (size_t c = 0; c < cols; ++c) {
330  res(r, c) = (*this)(r, c);
331  }
332  }
333 
334  // copy target
335  if (dimension == 0) {
336  for (size_t r = 0; r < target.rows; ++r) {
337  for (size_t c = 0; c < cols; ++c) {
338  res(r + rows, c) = target(r, c);
339  }
340  }
341  } else {
342  for (size_t r = 0; r < rows; ++r) {
343  for (size_t c = 0; c < target.cols; ++c) {
344  res(r, c + cols) = target(r, c);
345  }
346  }
347  }
348  return res;
349  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ diag()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::diag ( )
inline

Definition at line 351 of file mtx_tmpl.hpp.

351  {
352  assert((rows == 1 || cols == 1) || (rows == cols));
353 
354  if (rows == 1 || cols == 1) {
355  Matrix res{std::max(rows, cols), std::max(rows, cols)};
356  for (size_t i = 0; i < rows; ++i)
357  res(i, i) = (*this)(i, 0);
358  return res;
359  } else {
360  assert(rows == cols);
361  Matrix res{rows, 1};
362  for (size_t i = 0; i < rows; ++i)
363  res(i, 0) = (*this)(i, i);
364  return res;
365  }
366  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ fill_index()

template<typename Type >
void gpmp::linalg::Matrix< Type >::fill_index ( Type  val)
inline

Definition at line 394 of file mtx_tmpl.hpp.

394  {
395  for (size_t r = 0; r < rows; ++r) {
396  for (int64_t c = 0; c < cols; ++c) {
397  (*this)(r, c) = val;
398  }
399  }
400  }

References gpmp::linalg::Matrix< Type >::cols, and gpmp::linalg::Matrix< Type >::rows.

◆ hadamard()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::hadamard ( Matrix< Type > &  target)
inline

Definition at line 139 of file mtx_tmpl.hpp.

139  {
140  assert(dim == target.dim);
141  Matrix res((*this));
142 
143  for (size_t r = 0; r < res.rows; ++r) {
144  for (size_t c = 0; c < res.cols; ++c) {
145  res(r, c) = target(r, c) * (*this)(r, c);
146  }
147  }
148  return res;
149  }

References gpmp::linalg::Matrix< Type >::dim, and python.linalg::res.

Referenced by gpmp::linalg::Matrix< Type >::sqr_err().

◆ mean() [1/2]

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::mean ( )
inline

Definition at line 304 of file mtx_tmpl.hpp.

304  {
305  auto m = Type(num_elements);
306  return sum().scalar_mult(1 / m);
307  }
Matrix scalar_mult(Type scalar)
Definition: mtx_tmpl.hpp:124

References gpmp::linalg::Matrix< Type >::num_elements, gpmp::linalg::Matrix< Type >::scalar_mult(), and gpmp::linalg::Matrix< Type >::sum().

◆ mean() [2/2]

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::mean ( size_t  dimension)
inline

Definition at line 312 of file mtx_tmpl.hpp.

312  {
313  auto m = (dimension == 0) ? Type(rows) : Type(cols);
314  return sum().scalar_mult(1 / m);
315  }

References gpmp::linalg::Matrix< Type >::cols, gpmp::linalg::Matrix< Type >::rows, gpmp::linalg::Matrix< Type >::scalar_mult(), and gpmp::linalg::Matrix< Type >::sum().

◆ mult()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::mult ( Matrix< Type > &  target)
inline

Matrix Multiplication.

Parameters
[in]

Definition at line 107 of file mtx_tmpl.hpp.

107  {
108  assert(cols == target.rows);
109  Matrix res(rows, target.cols);
110 
111  for (size_t r = 0; res.rows; ++r) {
112  for (size_t c = 0; res.cols; ++c) {
113  for (size_t n = 0; n < target.rows; ++n) {
114  res(r, c) += (*this)(r, n) * target(n, c);
115  }
116  }
117  }
118  return res;
119  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ operator()()

template<typename Type >
Type& gpmp::linalg::Matrix< Type >::operator() ( size_t  row,
size_t  col 
)
inline

Overload operator.

Parameters
[in]row: size of rows
[in]col: size of columns

Definition at line 93 of file mtx_tmpl.hpp.

93  {
94  assert(0 <= row && row < rows);
95  assert(0 <= col && col < cols);
96 
97  return data[row * cols + col];
98  }

References gpmp::linalg::Matrix< Type >::cols, gpmp::linalg::Matrix< Type >::data, and gpmp::linalg::Matrix< Type >::rows.

◆ operator+()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::operator+ ( Matrix< Type > &  target)
inline

Definition at line 176 of file mtx_tmpl.hpp.

176  {
177  return add(target);
178  }
Matrix add(Matrix &target)
Definition: mtx_tmpl.hpp:165

References gpmp::linalg::Matrix< Type >::add().

◆ operator-() [1/2]

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::operator- ( )
inline

Definition at line 194 of file mtx_tmpl.hpp.

194  {
195  Matrix res(rows, cols);
196 
197  for (size_t r = 0; r < rows; ++r) {
198  for (size_t c = 0; c < cols; ++c) {
199  res(r, c) = -(*this)(r, c);
200  }
201  }
202  return res;
203  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ operator-() [2/2]

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::operator- ( Matrix< Type > &  target)
inline

Definition at line 212 of file mtx_tmpl.hpp.

212  {
213  return sub(target);
214  }
Matrix sub(Matrix &target)
Definition: mtx_tmpl.hpp:208

References gpmp::linalg::Matrix< Type >::sub().

◆ operator==()

template<typename Type >
Matrix<unsigned short> gpmp::linalg::Matrix< Type >::operator== ( Matrix< Type > &  target)
inline

Definition at line 216 of file mtx_tmpl.hpp.

216  {
217  assert(dim == target.dim);
218  Matrix<unsigned short> res(rows, cols);
219 
220  for (int64_t r = 0; r < rows; ++r) {
221  for (int64_t c = 0; c < cols; ++c) {
222  if ((*this)(r, c) - target(r, c) == 0.)
223  res(r, c) = 1;
224  else
225  res(r, c) = 0;
226  }
227  }
228  return res;
229  }

References gpmp::linalg::Matrix< Type >::cols, gpmp::linalg::Matrix< Type >::dim, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ print_mtx()

template<typename Type >
void gpmp::linalg::Matrix< Type >::print_mtx ( )
inline

Definition at line 384 of file mtx_tmpl.hpp.

384  {
385  for (size_t r = 0; r < rows; ++r) {
386  for (int64_t c = 0; c < cols; ++c) {
387  std::cout << (*this)(r, c) << " ";
388  }
389  std::cout << std::endl;
390  }
391  std::cout << std::endl;
392  }

References gpmp::linalg::Matrix< Type >::cols, and gpmp::linalg::Matrix< Type >::rows.

◆ print_shape()

template<typename Type >
void gpmp::linalg::Matrix< Type >::print_shape ( )
inline

Definition at line 379 of file mtx_tmpl.hpp.

379  {
380  std::cout << "Matrix Size([" << rows << ", " << cols << "])"
381  << std::endl;
382  }

References gpmp::linalg::Matrix< Type >::cols, and gpmp::linalg::Matrix< Type >::rows.

◆ scalar_add()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::scalar_add ( Type  scalar)
inline

Definition at line 183 of file mtx_tmpl.hpp.

183  {
184  Matrix res((*this));
185 
186  for (size_t r = 0; r < rows; ++r) {
187  for (size_t c = 0; c < cols; ++c) {
188  res(r, c) = (*this)(r, c) + scalar;
189  }
190  }
191  return res;
192  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ scalar_mult()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::scalar_mult ( Type  scalar)
inline

Definition at line 124 of file mtx_tmpl.hpp.

124  {
125  Matrix res((*this));
126 
127  for (size_t r = 0; r < res.rows; ++r) {
128  for (size_t c = 0; c < res.cols; ++c) {
129  res(r, c) = scalar * (*this)(r, c);
130  }
131  }
132  return res;
133  }

References python.linalg::res.

Referenced by gpmp::linalg::Matrix< Type >::mean().

◆ sqr_err()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::sqr_err ( )
inline

Definition at line 155 of file mtx_tmpl.hpp.

155  {
156  Matrix res((*this));
157 
158  res = hadamard(res);
159  return res;
160  }
Matrix hadamard(Matrix &target)
Definition: mtx_tmpl.hpp:139

References gpmp::linalg::Matrix< Type >::hadamard(), and python.linalg::res.

◆ sub()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::sub ( Matrix< Type > &  target)
inline

Definition at line 208 of file mtx_tmpl.hpp.

208  {
209  Matrix target_neg = -target;
210  return add(target_neg);
211  }

References gpmp::linalg::Matrix< Type >::add().

Referenced by gpmp::linalg::Matrix< Type >::operator-().

◆ sum() [1/2]

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::sum ( )
inline

Definition at line 269 of file mtx_tmpl.hpp.

269  {
270  Matrix res{1, 1};
271 
272  for (size_t r = 0; r < rows; ++r) {
273  for (size_t c = 0; c < cols; ++c) {
274  res(0, 0) += (*this)(r, c);
275  }
276  }
277  return res;
278  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

Referenced by gpmp::linalg::Matrix< Type >::mean().

◆ sum() [2/2]

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::sum ( size_t  dimension)
inline

Definition at line 283 of file mtx_tmpl.hpp.

283  {
284  assert(0 <= dimension && dimension < 2);
285  auto res = (dimension = 0) ? Matrix{1, cols} : Matrix{rows, 1};
286 
287  if (dimension == 0) {
288  for (size_t c = 0; c < cols; ++c) {
289  for (size_t r = 0; r < rows; ++r) {
290  res(0, c) += (*this)(r, c);
291  }
292  }
293  } else {
294  for (size_t r = 0; r < rows; ++r) {
295  for (size_t c = 0; c < cols; ++c) {
296  res(r, 0) += (*this)(r, c);
297  }
298  }
299  }
300  return res;
301  }

References gpmp::linalg::Matrix< Type >::cols, python.linalg::res, and gpmp::linalg::Matrix< Type >::rows.

◆ T()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::T ( )
inline

Definition at line 262 of file mtx_tmpl.hpp.

262  {
263  return (*this).transpose();
264  }

References gpmp::linalg::Matrix< Type >::transpose().

◆ transpose()

template<typename Type >
Matrix gpmp::linalg::Matrix< Type >::transpose ( )
inline

Definition at line 250 of file mtx_tmpl.hpp.

250  {
251  size_t new_rows{cols}, new_cols{rows};
252  Matrix transposed(new_rows, new_cols);
253 
254  for (size_t r = 0; r < new_rows; ++r) {
255  for (size_t c = 0; c < new_cols; ++c) {
256  transposed(r, c) = (*this)(c, r);
257  }
258  }
259  return transposed;
260  }

References gpmp::linalg::Matrix< Type >::cols, and gpmp::linalg::Matrix< Type >::rows.

Referenced by gpmp::linalg::Matrix< Type >::T().

Member Data Documentation

◆ cols

template<typename Type >
size_t gpmp::linalg::Matrix< Type >::cols

◆ data

template<typename Type >
std::vector<Type> gpmp::linalg::Matrix< Type >::data

Definition at line 66 of file mtx_tmpl.hpp.

Referenced by gpmp::linalg::Matrix< Type >::operator()().

◆ dim

template<typename Type >
std::tuple<size_t, size_t> gpmp::linalg::Matrix< Type >::dim

◆ num_elements

template<typename Type >
int64_t gpmp::linalg::Matrix< Type >::num_elements = rows * cols

◆ rows

template<typename Type >
size_t gpmp::linalg::Matrix< Type >::rows

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