Line data Source code
1 : #ifndef DAD67FEF_0E47_455C_AC3E_CE31BDC7EE3F
2 : #define DAD67FEF_0E47_455C_AC3E_CE31BDC7EE3F
3 :
4 : #include "math/linalg/matrix_column_view.h"
5 :
6 : #include "math/linalg/matrix.hpp" // IWYU pragma: keep
7 :
8 : namespace tracking
9 : {
10 : namespace math
11 : {
12 :
13 : // Forward declaration to prevent cyclic includes
14 : template <typename ValueType_, sint32 Size_>
15 : class Vector;
16 :
17 : template <typename ValueType_, sint32 Rows_, sint32 Cols_, bool IsRowMajor_>
18 3112 : inline MatrixColumnView<ValueType_, Rows_, Cols_, IsRowMajor_>::MatrixColumnView(
19 : const Matrix<ValueType_, Rows_, Cols_, IsRowMajor_>& matrix, const sint32 col, const sint32 rowBegin, const sint32 rowEnd)
20 3112 : : _matrix{matrix}
21 3112 : , _col{col}
22 3112 : , _rowBegin{rowBegin}
23 3108 : , _rowCount{rowEnd - rowBegin + 1}
24 : {
25 3035 : assert(_rowBegin >= 0);
26 3035 : assert(_rowCount <= Rows_);
27 3035 : }
28 :
29 : template <typename ValueType_, sint32 Rows_, sint32 Cols_, bool IsRowMajor_>
30 45760 : inline auto MatrixColumnView<ValueType_, Rows_, Cols_, IsRowMajor_>::at_unsafe(const sint32 idx) const -> ValueType_
31 : {
32 45760 : assert(idx < _rowCount);
33 45760 : const auto row = _rowBegin + idx;
34 45760 : return _matrix.at_unsafe(row, _col);
35 : }
36 :
37 : template <typename ValueType_, sint32 Rows_, sint32 Cols_, bool IsRowMajor_>
38 : template <sint32 Rows2_>
39 72 : inline auto MatrixColumnView<ValueType_, Rows_, Cols_, IsRowMajor_>::operator*(const Vector<ValueType_, Rows2_>& other) const
40 : -> ValueType_
41 : {
42 : static_assert(Rows2_ <= Rows_);
43 72 : assert(Rows2_ == _rowCount);
44 : // calc dot product
45 : ValueType_ result{};
46 428 : for (auto row = 0; row < Rows2_; ++row)
47 : {
48 356 : result += this->at_unsafe(row) * other.at_unsafe(row);
49 : }
50 72 : return result;
51 : }
52 :
53 : template <typename ValueType_, sint32 Rows_, sint32 Cols_, bool IsRowMajor_>
54 : template <sint32 Rows2_, sint32 Cols2_, bool IsRowMajor2_>
55 1913 : inline auto MatrixColumnView<ValueType_, Rows_, Cols_, IsRowMajor_>::operator*(
56 : const MatrixColumnView<ValueType_, Rows2_, Cols2_, IsRowMajor2_>& other) const -> ValueType_
57 : {
58 1913 : assert(other.getRowCount() == _rowCount);
59 : // calc dot product
60 : ValueType_ result{};
61 9303 : for (auto row = 0; row < _rowCount; ++row)
62 : {
63 7390 : result += this->at_unsafe(row) * other.at_unsafe(row);
64 : }
65 1913 : return result;
66 : }
67 :
68 :
69 : template <typename ValueType_, sint32 Rows_, sint32 Cols_, bool IsRowMajor_, sint32 Rows2_, sint32 Cols2_, bool IsRowMajor2_>
70 72 : inline auto operator*(const Matrix<ValueType_, Rows_, Cols_, IsRowMajor_>& mat,
71 : const MatrixColumnView<ValueType_, Rows2_, Cols2_, IsRowMajor2_>& colView) -> Vector<ValueType_, Rows_>
72 : {
73 : static_assert(Cols_ <= Rows2_);
74 72 : assert(Cols_ == colView.getRowCount());
75 :
76 72 : Vector<ValueType_, Rows_> result{};
77 428 : for (auto row = 0; row < Rows_; ++row)
78 : {
79 2191 : for (auto col = 0; col < Cols_; ++col)
80 : {
81 1835 : result.at_unsafe(row) += mat.at_unsafe(row, col) * colView.at_unsafe(col);
82 : }
83 : }
84 72 : return result;
85 : }
86 :
87 : } // namespace math
88 : } // namespace tracking
89 :
90 : #endif // DAD67FEF_0E47_455C_AC3E_CE31BDC7EE3F
|