PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
SubMatrix.h
1#ifndef PBAT_MATH_LINALG_MINI_SUBMATRIX_H
2#define PBAT_MATH_LINALG_MINI_SUBMATRIX_H
3
4#include "Assign.h"
5#include "Concepts.h"
6#include "Transpose.h"
7#include "pbat/HostDevice.h"
8
9#include <type_traits>
10#include <utility>
11
12namespace pbat {
13namespace math {
14namespace linalg {
15namespace mini {
16
17template <class /*CMatrix*/ TMatrix, int M, int N>
18class ConstSubMatrix
19{
20 public:
21 using NestedType = TMatrix;
22 using ScalarType = typename NestedType::ScalarType;
23 using SelfType = ConstSubMatrix<NestedType, M, N>;
24
25 static auto constexpr kRows = M;
26 static auto constexpr kCols = N;
27 static bool constexpr bRowMajor = NestedType::bRowMajor;
28
29 PBAT_HOST_DEVICE ConstSubMatrix(NestedType const& _A, int _ib, int _jb) : A(_A), ib(_ib), jb(_jb)
30 {
31 static_assert(
32 NestedType::kRows >= M and NestedType::kCols >= N and M > 0 and N > 0,
33 "Invalid submatrix dimensions");
34 }
35
36 PBAT_HOST_DEVICE constexpr auto Rows() const { return kRows; }
37 PBAT_HOST_DEVICE constexpr auto Cols() const { return kCols; }
38
39 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const { return A(ib + i, jb + j); }
40
41 // Vector(ized) access
42 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
43 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
44
45 template <auto S, auto T>
46 PBAT_HOST_DEVICE ConstSubMatrix<SelfType, S, T> Slice(auto i, auto j) const
47 {
48 return ConstSubMatrix<SelfType, S, T>(*this, i, j);
49 }
50 PBAT_HOST_DEVICE ConstSubMatrix<SelfType, kRows, 1> Col(auto j) const
51 {
52 return Slice<kRows, 1>(0, j);
53 }
54 PBAT_HOST_DEVICE ConstSubMatrix<SelfType, 1, kCols> Row(auto i) const
55 {
56 return Slice<1, kCols>(i, 0);
57 }
58 PBAT_HOST_DEVICE ConstTransposeView<SelfType const> Transpose() const
59 {
61 }
62
63 private:
64 NestedType const& A;
65 int ib, jb;
66};
67
68template <class /*CMatrix*/ TMatrix, int M, int N>
69class SubMatrix
70{
71 public:
72 using NestedType = TMatrix;
73 using ScalarType = typename NestedType::ScalarType;
74 using SelfType = SubMatrix<NestedType, M, N>;
75
76 static auto constexpr kRows = M;
77 static auto constexpr kCols = N;
78 static bool constexpr bRowMajor = NestedType::bRowMajor;
79
80 PBAT_HOST_DEVICE SubMatrix(NestedType& _A, int _ib, int _jb) : A(_A), ib(_ib), jb(_jb)
81 {
82 static_assert(
83 NestedType::kRows >= M and NestedType::kCols >= N and M > 0 and N > 0,
84 "Invalid submatrix dimensions");
85 }
86
87 template <class /*CMatrix*/ TOtherMatrix>
88 PBAT_HOST_DEVICE SelfType& operator=(TOtherMatrix&& B)
89 {
90 Assign(*this, std::forward<TOtherMatrix>(B));
91 return *this;
92 }
93
94 PBAT_HOST_DEVICE auto Rows() const { return kRows; }
95 PBAT_HOST_DEVICE auto Cols() const { return kCols; }
96
97 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const { return A(ib + i, jb + j); }
98 PBAT_HOST_DEVICE ScalarType& operator()(auto i, auto j) { return A(ib + i, jb + j); }
99
100 // Vector(ized) access
101 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
102 PBAT_HOST_DEVICE ScalarType& operator()(auto i) { return (*this)(i % kRows, i / kRows); }
103 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
104 PBAT_HOST_DEVICE ScalarType& operator[](auto i) { return (*this)(i); }
105
106 template <auto S, auto T>
107 PBAT_HOST_DEVICE auto Slice(auto i, auto j)
108 {
109 return SubMatrix<SelfType, S, T>(*this, i, j);
110 }
111 PBAT_HOST_DEVICE auto Col(auto j) { return Slice<kRows, 1>(0, j); }
112 PBAT_HOST_DEVICE auto Row(auto i) { return Slice<1, kCols>(i, 0); }
113
114 template <auto S, auto T>
115 PBAT_HOST_DEVICE auto Slice(auto i, auto j) const
116 {
117 return ConstSubMatrix<SelfType, S, T>(*this, i, j);
118 }
119 PBAT_HOST_DEVICE auto Col(auto j) const { return Slice<kRows, 1>(0, j); }
120 PBAT_HOST_DEVICE auto Row(auto i) const { return Slice<1, kCols>(i, 0); }
121
122 PBAT_HOST_DEVICE auto Transpose() { return TransposeView<SelfType>(*this); }
123 PBAT_HOST_DEVICE auto Transpose() const { return ConstTransposeView<SelfType>(*this); }
124
125 void SetConstant(auto k) { AssignScalar(*this, k); }
126
127 private:
128 NestedType& A;
129 int ib, jb;
130};
131
132#define PBAT_MINI_SUBMATRIX_API(SelfType) \
133 template <auto S, auto T> \
134 PBAT_HOST_DEVICE [[maybe_unused]] auto Slice(int i, int j) \
135 { \
136 return SubMatrix<SelfType, S, T>(*this, i, j); \
137 } \
138 PBAT_HOST_DEVICE [[maybe_unused]] auto Col(int j) \
139 { \
140 return Slice<kRows, 1>(0, j); \
141 } \
142 PBAT_HOST_DEVICE [[maybe_unused]] auto Row(int i) \
143 { \
144 return Slice<1, kCols>(i, 0); \
145 }
146
147#define PBAT_MINI_CONST_SUBMATRIX_API(SelfType) \
148 template <auto S, auto T> \
149 PBAT_HOST_DEVICE [[maybe_unused]] auto Slice(int i, int j) const \
150 { \
151 return ConstSubMatrix<SelfType, S, T>(*this, i, j); \
152 } \
153 PBAT_HOST_DEVICE [[maybe_unused]] auto Col(int j) const \
154 { \
155 return Slice<kRows, 1>(0, j); \
156 } \
157 PBAT_HOST_DEVICE [[maybe_unused]] auto Row(int i) const \
158 { \
159 return Slice<1, kCols>(i, 0); \
160 }
161
162} // namespace mini
163} // namespace linalg
164} // namespace math
165} // namespace pbat
166
167#endif // PBAT_MATH_LINALG_MINI_SUBMATRIX_H
Definition Transpose.h:139
Mini linear algebra related functionality.
Definition Assign.h:12
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
Math related functionality.
Definition Concepts.h:19
The main namespace of the library.
Definition Aliases.h:15