forked from tomstewart89/BasicLinearAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicLinearAlgebra.h
143 lines (99 loc) · 4.95 KB
/
BasicLinearAlgebra.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#pragma once
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "ElementStorage.h"
namespace BLA
{
template <int rows, int cols = 1, class MemT = Array<rows, cols, float>>
class Matrix
{
public:
typedef MemT mem_t;
const static int Rows = rows;
const static int Cols = cols;
MemT storage;
// Constructors
Matrix<rows, cols, MemT>() = default;
Matrix<rows, cols, MemT>(MemT &d);
template <class opMemT>
Matrix<rows, cols, MemT>(const Matrix<rows, cols, opMemT> &obj);
template <typename... TAIL>
Matrix(typename MemT::elem_t head, TAIL... args);
// Assignment
template <class opMemT>
Matrix<rows, cols, MemT> &operator=(const Matrix<rows, cols, opMemT> &obj);
Matrix<rows, cols, MemT> &operator=(typename MemT::elem_t arr[rows][cols]);
Matrix<rows, cols, MemT> &Fill(const typename MemT::elem_t &val);
template <typename... TAIL>
void FillRowMajor(int start_idx, typename MemT::elem_t head, TAIL... tail);
void FillRowMajor(int start_idx);
// Element Access
typename MemT::elem_t &operator()(int row, int col = 0);
typename MemT::elem_t operator()(int row, int col = 0) const;
template <int subRows, int subCols>
Matrix<subRows, subCols, Reference<MemT>> Submatrix(int top, int left);
template <int subRows, int subCols>
Matrix<subRows, subCols, ConstReference<MemT>> Submatrix(int top, int left) const;
Matrix<1, cols, Reference<MemT>> Row(int i);
Matrix<1, cols, ConstReference<MemT>> Row(int i) const;
Matrix<rows, 1, Reference<MemT>> Column(int j);
Matrix<rows, 1, ConstReference<MemT>> Column(int j) const;
// Concatenation
template <int operandCols, class opMemT>
Matrix<rows, cols + operandCols, HorzCat<cols, MemT, opMemT>> operator||(
const Matrix<rows, operandCols, opMemT> &obj) const;
template <int operandRows, class opMemT>
Matrix<rows + operandRows, cols, VertCat<rows, MemT, opMemT>> operator&&(
const Matrix<operandRows, cols, opMemT> &obj) const;
// Addition
template <class opMemT>
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator+(const Matrix<rows, cols, opMemT> &obj) const;
template <class opMemT>
Matrix<rows, cols, MemT> &operator+=(const Matrix<rows, cols, opMemT> &obj);
// Subtraction
template <class opMemT>
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator-(const Matrix<rows, cols, opMemT> &obj) const;
template <class opMemT>
Matrix<rows, cols, MemT> &operator-=(const Matrix<rows, cols, opMemT> &obj);
// Multiplication
template <int operandCols, class opMemT>
Matrix<rows, operandCols, Array<rows, operandCols, typename MemT::elem_t>> operator*(
const Matrix<cols, operandCols, opMemT> &operand) const;
template <class opMemT>
Matrix<rows, cols, MemT> &operator*=(const Matrix<rows, cols, opMemT> &operand);
// Negation
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator-() const;
// Transposition
Matrix<cols, rows, Trans<MemT>> operator~() const;
// Elementwise Operations
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator+(const typename MemT::elem_t k) const;
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator-(const typename MemT::elem_t k) const;
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator*(const typename MemT::elem_t k) const;
Matrix<rows, cols, Array<rows, cols, typename MemT::elem_t>> operator/(const typename MemT::elem_t k) const;
Matrix<rows, cols, MemT> &operator+=(const typename MemT::elem_t k);
Matrix<rows, cols, MemT> &operator-=(const typename MemT::elem_t k);
Matrix<rows, cols, MemT> &operator*=(const typename MemT::elem_t k);
Matrix<rows, cols, MemT> &operator/=(const typename MemT::elem_t k);
};
template <int rows, int cols = 1, class ElemT = float>
using ArrayMatrix = Matrix<rows, cols, Array<rows, cols, ElemT>>;
template <int rows, int cols, class ElemT = float>
using ArrayRef = Reference<Array<rows, cols, ElemT>>;
template <int rows, int cols, class ParentMemT>
using RefMatrix = Matrix<rows, cols, Reference<ParentMemT>>;
template <int rows, int cols = rows, class ElemT = float>
using Identity = Matrix<rows, cols, Eye<ElemT>>;
template <int rows, int cols = 1, class ElemT = float>
using Zeros = Matrix<rows, cols, Zero<ElemT>>;
template <int rows, int cols, int tableSize = cols, class ElemT = float>
using SparseMatrix = Matrix<rows, cols, Sparse<cols, tableSize, ElemT>>;
template <int dim, class ElemT = float>
using PermutationMatrix = Matrix<dim, dim, Permutation<dim, ElemT>>;
template <int rows, int cols, class MemT>
using LowerTriangularDiagonalOnesMatrix = Matrix<rows, cols, LowerTriangleOnesDiagonal<MemT>>;
template <int rows, int cols, class MemT>
using UpperTriangularMatrix = Matrix<rows, cols, UpperTriangle<MemT>>;
} // namespace BLA
#include "impl/BasicLinearAlgebra.h"
#include "impl/NotSoBasicLinearAlgebra.h"