2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
18
19
20
53 template <
typename T,
unsigned int R,
unsigned int C>
60 Matrix<T, R, C>(T elements);
64 Matrix<T, R, C>(
const T(&initial)[R][C]);
68 Matrix<T, R, C>(
const Matrix<T, R, C> &initial);
71 Matrix<T, R, C>(
const std::array<std::array<T, C>, R> &initial);
81 std::string
str()
const;
91 int set(
const unsigned int &row,
const unsigned int &col,
const T &value);
98 int get(
const unsigned int &row,
const unsigned int &col, T &result)
const;
119 T
get(
const unsigned int &row,
const unsigned int &col)
const;
138 std::pair<
unsigned int,
unsigned int>
size() {
return std::pair<
unsigned int,
unsigned int>(R, C);}
142 void max(T &result, std::pair<
unsigned int,
unsigned int> &index)
const;
146 void min(T &result, std::pair<
unsigned int,
unsigned int> &index)
const;
155 int det(T &result)
const;
161 Matrix<T, R, C> inverse()
const {
Matrix<T, R, C> tmp; inverse(tmp);
return tmp;}
167 Matrix<T, C, R> transpose()
const;
172 int trace(T &result)
const;
188 std::array<std::array<T, C>, R>
values;
194 const unsigned int &start_c,
const unsigned int &end_c)
const;
207 template <
typename T,
unsigned int R,
unsigned int C>
208 Matrix<T, R, C>::Matrix() {
211 for(i = 0; i < R; i++) {
212 for(j = 0; j < C; j++) {
218 template <
typename T,
unsigned int R,
unsigned int C>
219 Matrix<T, R, C>::Matrix(T elements) {
222 for(i = 0; i < R; i++) {
223 for(j = 0; j < C; j++) {
229 template <
typename T,
unsigned int R,
unsigned int C>
230 Matrix<T, R, C>::Matrix(
const T(&initial)[R][C]) {
238 for(i = 0; i < R; i++) {
239 for(j = 0; j < C; j++) {
240 values[i][j] = initial[i][j];
245 template <
typename T,
unsigned int R,
unsigned int C>
246 Matrix<T, R, C>::Matrix(
const Matrix<T, R, C> &initial) {
249 for(i = 0; i < R; i++) {
250 for(j = 0; j < C; j++) {
251 values[i][j] = initial.values[i][j];
256 template <
typename T,
unsigned int R,
unsigned int C>
257 Matrix<T, R, C>::Matrix(
const std::array<std::array<T, C>, R> &initial) {
260 for(i = 0; i < R; i++) {
261 for(j = 0; j < C; j++) {
262 values[i][j] = initial[i][j];
267 template <
typename T,
unsigned int R,
unsigned int C>
277 template <
typename T,
unsigned int R,
unsigned int C>
281 for(i = 0; i < R; i++) {
283 for(j = 0; j < C; j++) {
289 std::cout<<
"]"<<std::endl;
293 template <
typename T,
unsigned int R,
unsigned int C>
294 std::string
Matrix<T, R, C>::
str()
const {
296 std::string out =
"[";
298 for(i = 0; i < R; i++) {
300 for(j = 0; j < C; j++) {
304 out += std::to_string(
values[i][j]);
313 template <
typename T,
unsigned int R,
unsigned int C>
314 int Matrix<T, R, C>::
set(
const unsigned int &row,
const unsigned int &col,
const T &value){
326 template <
typename T,
unsigned int R,
unsigned int C>
327 int Matrix<T, R, C>::
get(
const unsigned int &row,
const unsigned int &col, T &result)
const{
335 result =
values[row][col];
339 template <
typename T,
unsigned int R,
unsigned int C>
340 T
Matrix<T, R, C>::
get(
const unsigned int &row,
const unsigned int &col)
const{
344 template <
typename T,
unsigned int R,
unsigned int C>
346 const T* ptr = start_ptr;
348 for(i = 0; i < R; i++) {
349 for(j = 0; j < C; j++) {
356 template <
typename T,
unsigned int R,
unsigned int C>
360 for(i = 0; i < R; i++) {
361 for(j = 0; j < C; j++) {
368 template <
typename T,
unsigned int R,
unsigned int C>
375 for(i = 0; i < R; i++) {
376 for(j = 0; j < C; j++) {
377 result.values[i][j] =
values[i][j];
382 template <
typename T,
unsigned int R,
unsigned int C>
391 for(i = 0; i < R; i++) {
392 for(j = 0; j < C; j++) {
393 this->values[i][j] = other.values[i][j];
399 template <
typename T,
unsigned int R,
unsigned int C>
400 void Matrix<T, R, C>::
max(T &result, std::pair<
unsigned int,
unsigned int> &index)
const {
406 for(i = 0; i < R; i++) {
407 for(j = 0; j < C; j++) {
408 if(
values[i][j] > result) {
417 template <
typename T,
unsigned int R,
unsigned int C>
418 void Matrix<T, R, C>::
min(T &result, std::pair<
unsigned int,
unsigned int> &index)
const {
424 for(i = 0; i < R; i++) {
425 for(j = 0; j < C; j++) {
426 if(
values[i][j] < result) {
435 template <
typename T,
unsigned int R,
unsigned int C>
436 int Matrix<T, R, C>::
det(T &result)
const {
442 unsigned int i, j, err;
450 for(i = 0; i < R; i++) {
451 decomposed[i] = A[i];
452 for(j = 0; j < C; j++) {
476 result = decomposed[0][0];
477 for(i = 1; i < R; i++) {
478 result *= decomposed[i][i];
487 template <
typename T,
unsigned int R,
unsigned int C>
497 unsigned int i, j, k, idx, err;
505 for(i = 0; i < R; i++) {
506 decomposed[i] = A[i];
507 for(j = 0; j < C; j++) {
526 for (j = 0; j < R; j++) {
527 for (i = 0; i < R; i++) {
528 result.values[i][j] = P[i] == j ? 1.0 : 0.0;
529 for (k = 0; k < i; k++) {
530 result.values[i][j] -= decomposed[i][k]*result.values[k][j];
535 for(idx = 1; idx < R+1; idx++) {
537 for (k = i+1; k < R; k++) {
538 result.values[i][j] -= decomposed[i][k]*result.values[k][j];
540 err = safeDivide(result.values[i][j], decomposed[i][i], result.values[i][j]);
550 template <
typename T,
unsigned int R,
unsigned int C>
558 for(i = 0; i < R; i++) {
559 for(j = 0; j < C; j++) {
560 result.values[j][i] =
values[i][j];
564 template <
typename T,
unsigned int R,
unsigned int C>
574 template <
typename T,
unsigned int R,
unsigned int C>
585 for(i = 0; i < R; i++) {
591 template <
typename T,
unsigned int R,
unsigned int C>
594 for(i = 0; i < R; i++) {
595 for(j = 0; j < C; j++) {
601 template <
typename T,
unsigned int R,
unsigned int C>
605 for(i = 0; i < R; i++) {
606 for(j = 0; j < C; j++) {
618 template <
typename T,
unsigned int R,
unsigned int C>
620 const unsigned int &start_c,
const unsigned int &end_c)
const{
622 if(start_r > end_r || start_c > end_c) {
627 if(end_r >= R || end_c >= C) {
634 template<
typename T,
unsigned int R,
unsigned int C>
645 unsigned int i, j, k, imax;
646 T max_A, *ptr, abs_A;
649 for (k = 0; k <= R; k++) {
654 for (i = 0; i < R; i++) {
663 for (k = i; k < R; k++) {
664 abs_A = std::abs(A[k][i]);
698 for (j = i+1; j < R; j++) {
699 safeDivide(A[j][i], A[i][i], A[j][i]);
700 for (k = i+1; k < R; k++) {
701 A[j][k] -= A[j][i] * A[i][k];
Matrix math implementation.
Definition Matrix.hpp:54
void setToZeros()
Function to set all elements of the matrix to zero.
Definition Matrix.hpp:592
std::array< std::array< T, C >, R > values
The actual values held by the matrix – a two dimensional array of values indexed as (row,...
Definition Matrix.hpp:188
void getAsArray(T *start_ptr) const
Function to get the values of the matrix row-wise.
Definition Matrix.hpp:357
int inverse(Matrix< T, R, C > &result) const
Function to return the inverse of the matrix.
Definition Matrix.hpp:488
T * operator[](unsigned int idx)
Function to return a matrix row or vector value.
Definition Matrix.hpp:268
int set(const unsigned int &row, const unsigned int &col, const T &value)
Function to set a single value in the matrix.
Definition Matrix.hpp:314
std::string str() const
Function to dump information on matrix to string.
Definition Matrix.hpp:294
void setFromArray(const T *start_ptr)
Function to set the values of the matrix row-wise.
Definition Matrix.hpp:345
std::pair< unsigned int, unsigned int > size()
Function to get the size of the matrix.
Definition Matrix.hpp:138
T get(const unsigned int &row, const unsigned int &col) const
Function to get a single value in the matrix.
Definition Matrix.hpp:340
int trace(T &result) const
Function to return the trace of the matrix.
Definition Matrix.hpp:575
int identity()
Function to set matrix to identity, if it is a square matrix.
Definition Matrix.hpp:602
Matrix< T, R, C > & operator=(const Matrix< T, R, C > &other)
Equals operator overload for matrix.
Definition Matrix.hpp:383
int _LUPDecompose(T *A[R], unsigned int P[R+1]) const
Function to take a 2-d matrix represented by A and decompose it into LU form.
Definition Matrix.hpp:635
int det(T &result) const
Function to return the determinant of the matrix.
Definition Matrix.hpp:436
void transpose(Matrix< T, C, R > &result) const
Function to return the transpose of the matrix.
Definition Matrix.hpp:551
int get(const unsigned int &row, const unsigned int &col, T &result) const
Function to get a single value in the matrix.
Definition Matrix.hpp:327
void max(T &result, std::pair< unsigned int, unsigned int > &index) const
Function to return the maximum value in the matrix.
Definition Matrix.hpp:400
void getCopy(Matrix< T, R, C > &result) const
Function to get a copy of the matrix.
Definition Matrix.hpp:369
void min(T &result, std::pair< unsigned int, unsigned int > &index) const
Function to return the minimum value in the matrix.
Definition Matrix.hpp:418
void dump() const
Function to dump information on matrix.
Definition Matrix.hpp:278
int _checkLookupBoundaries(const unsigned int &start_r, const unsigned int &end_r, const unsigned int &start_c, const unsigned int &end_c) const
Function to check, given a set of submatrix boundaries, that those boundaries are valid for the curre...
Definition Matrix.hpp:619
#define NO_ERROR
Definition clockwerkerrors.h:31
#define TOLERANCE_CONDITIONING
Definition clockwerkerrors.h:43
#define ERROR_POORLY_CONDITIONED
Definition clockwerkerrors.h:42
#define ERROR_DIMENSIONS
Definition clockwerkerrors.h:38