Line data Source code
1 : #ifndef D49F29DA_4806_421C_89F4_8462894DFBAA
2 : #define D49F29DA_4806_421C_89F4_8462894DFBAA
3 :
4 : #include "math/linalg/matrix_view.h"
5 :
6 : namespace tracking
7 : {
8 : namespace math
9 : {
10 :
11 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
12 6 : inline MatrixView<ValueType_, Rows_, Cols_>::MatrixView(const Matrix<ValueType_, Rows_, Cols_>& matrix,
13 : const sint32 rowBegin,
14 : const sint32 colBegin,
15 : const sint32 rowEnd,
16 : const sint32 colEnd)
17 6 : : _matrix{matrix}
18 6 : , _rowBegin{rowBegin}
19 6 : , _colBegin{colBegin}
20 6 : , _rowCount{rowEnd - rowBegin + 1}
21 6 : , _colCount{colEnd - colBegin + 1}
22 :
23 : {
24 6 : assert(_rowBegin >= 0);
25 6 : assert(_colBegin >= 0);
26 6 : assert(_rowCount <= Rows_);
27 6 : assert(_colCount <= Cols_);
28 6 : }
29 :
30 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
31 32 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator()(sint32 row, sint32 col) const -> ValueType_
32 : {
33 32 : assert(row < _rowCount);
34 32 : assert(col < _colCount);
35 32 : return _matrix.at_unsafe(_rowBegin + row, _colBegin + col);
36 : }
37 :
38 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
39 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator+(const Matrix<ValueType_, Rows_, Cols_>& other) const
40 : -> Matrix<ValueType_, Rows_, Cols_>
41 : {
42 : auto result{other};
43 : for (auto row = 0; row < Rows_; ++row)
44 : {
45 : for (auto col = 0; col < Cols_; ++col)
46 : {
47 : result.at_unsafe(row, col) += this->operator()(row, col);
48 : }
49 : }
50 : return result;
51 : }
52 :
53 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
54 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator-(const Matrix<ValueType_, Rows_, Cols_>& other) const
55 : -> Matrix<ValueType_, Rows_, Cols_>
56 : {
57 : auto result{other};
58 : for (auto row = 0; row < Rows_; ++row)
59 : {
60 : for (auto col = 0; col < Cols_; ++col)
61 : {
62 : result.at_unsafe(row, col) -= this->operator()(row, col);
63 : }
64 : }
65 : return result;
66 : }
67 :
68 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
69 : template <sint32 Cols2_>
70 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator*(const Matrix<ValueType_, Cols_, Cols2_>& other) const
71 : -> Matrix<ValueType_, Rows_, Cols2_>
72 : {
73 : Matrix<ValueType_, Rows_, Cols_> result;
74 : for (auto i = 0; i < Rows_; ++i)
75 : {
76 : for (auto k = 0; k < Cols_; ++k)
77 : {
78 : for (auto j = 0; j < Cols2_; ++j)
79 : {
80 : result.at_unsafe(i, j) += this->operator()(i, k) * other.at_unsafe(k, j);
81 : }
82 : }
83 : }
84 : return result;
85 : }
86 :
87 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
88 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator+(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>
89 : {
90 : Matrix<ValueType_, Rows_, Cols_> result;
91 : for (auto row = 0; row < _rowCount; ++row)
92 : {
93 : for (auto col = 0; col < _colCount; ++col)
94 : {
95 : result.at_unsafe(row, col) = this->operator()(row, col) + other;
96 : }
97 : }
98 : return result;
99 : }
100 :
101 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
102 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator-(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>
103 : {
104 : Matrix<ValueType_, Rows_, Cols_> result;
105 : for (auto row = 0; row < _rowCount; ++row)
106 : {
107 : for (auto col = 0; col < _colCount; ++col)
108 : {
109 : result.at_unsafe(row, col) = this->operator()(row, col) - other;
110 : }
111 : }
112 : return result;
113 : }
114 :
115 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
116 1 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator*(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>
117 : {
118 1 : Matrix<ValueType_, Rows_, Cols_> result;
119 3 : for (auto row = 0; row < _rowCount; ++row)
120 : {
121 6 : for (auto col = 0; col < _colCount; ++col)
122 : {
123 4 : result.at_unsafe(row, col) = this->operator()(row, col) * other;
124 : }
125 : }
126 1 : return result;
127 : }
128 :
129 : template <typename ValueType_, sint32 Rows_, sint32 Cols_>
130 : inline auto MatrixView<ValueType_, Rows_, Cols_>::operator/(const ValueType_& other) const -> Matrix<ValueType_, Rows_, Cols_>
131 : {
132 : Matrix<ValueType_, Rows_, Cols_> result;
133 : for (auto row = 0; row < _rowCount; ++row)
134 : {
135 : for (auto col = 0; col < _colCount; ++col)
136 : {
137 : result.at_unsafe(row, col) = this->operator()(row, col) / other;
138 : }
139 : }
140 : return result;
141 : }
142 :
143 : template <typename ValueType_, sint32 RowsA_, sint32 ColsA_, sint32 RowsB_, sint32 ColsB_>
144 1 : auto operator+(const MatrixView<ValueType_, RowsA_, ColsA_>& a, const MatrixView<ValueType_, RowsB_, ColsB_>& b)
145 : -> Matrix<ValueType_, std::min(RowsA_, RowsB_), std::min(ColsA_, ColsB_)>
146 : {
147 1 : assert(a.getRowCount() == b.getRowCount());
148 1 : assert(a.getColCount() == b.getColCount());
149 1 : Matrix<ValueType_, std::min(RowsA_, RowsB_), std::min(ColsA_, ColsB_)> result{};
150 :
151 3 : for (auto row = 0; row < a.getRowCount(); ++row)
152 : {
153 6 : for (auto col = 0; col < a.getColCount(); ++col)
154 : {
155 4 : result.at_unsafe(row, col) = a(row, col) + b(row, col);
156 : }
157 : }
158 1 : return result;
159 : }
160 :
161 : template <typename ValueType_, sint32 RowsA_, sint32 ColsA_, sint32 RowsB_, sint32 ColsB_>
162 : auto operator-(const MatrixView<ValueType_, RowsA_, ColsA_>& a, const MatrixView<ValueType_, RowsB_, ColsB_>& b)
163 : -> Matrix<ValueType_, std::min(RowsA_, RowsB_), std::min(ColsA_, ColsB_)>
164 : {
165 : assert(a.getRowCount() == b.getRowCount());
166 : assert(a.getColCount() == b.getColCount());
167 : Matrix<ValueType_, std::min(RowsA_, RowsB_), std::min(ColsA_, ColsB_)> result{};
168 :
169 : for (auto row = 0; row < a.getRowCount(); ++row)
170 : {
171 : for (auto col = 0; col < a.getColCount(); ++col)
172 : {
173 : result.at_unsafe(row, col) = a(row, col) - b(row, col);
174 : }
175 : }
176 : return result;
177 : }
178 :
179 : template <typename ValueType_, sint32 RowsA_, sint32 ColsA_, sint32 RowsB_, sint32 ColsB_>
180 1 : auto operator*(const MatrixView<ValueType_, RowsA_, ColsA_>& a,
181 : const MatrixView<ValueType_, RowsB_, ColsB_>& b) -> Matrix<ValueType_, RowsA_, ColsB_>
182 : {
183 1 : assert(a.getColCount() == b.getRowCount());
184 1 : Matrix<ValueType_, RowsA_, ColsB_> result{};
185 :
186 3 : for (auto i = 0; i < a.getRowCount(); ++i)
187 : {
188 6 : for (auto k = 0; k < a.getColCount(); ++k)
189 : {
190 12 : for (auto j = 0; j < b.getColCount(); ++j)
191 : {
192 8 : result.at_unsafe(i, j) += a(i, k) * b(k, j);
193 : }
194 : }
195 : }
196 1 : return result;
197 : }
198 :
199 : } // namespace math
200 : } // namespace tracking
201 :
202 : #endif // D49F29DA_4806_421C_89F4_8462894DFBAA
|