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
Stack.h
1#ifndef PBAT_MATH_LINALG_MINI_STACK_H
2#define PBAT_MATH_LINALG_MINI_STACK_H
3
4#include "Api.h"
5#include "Concepts.h"
6#include "pbat/HostDevice.h"
7
8#include <type_traits>
9#include <utility>
10
11namespace pbat {
12namespace math {
13namespace linalg {
14namespace mini {
15
16template <class /*CMatrix*/ TLhsMatrix, class /*CMatrix*/ TRhsMatrix>
17class HorizontalStack
18{
19 public:
20 using LhsNestedType = TLhsMatrix;
21 using RhsNestedType = TRhsMatrix;
22 using ScalarType = typename LhsNestedType::ScalarType;
23 using SelfType = HorizontalStack<LhsNestedType, RhsNestedType>;
24
25 static auto constexpr kRows = LhsNestedType::kRows;
26 static auto constexpr kCols = LhsNestedType::kCols + RhsNestedType::kCols;
27 static bool constexpr bRowMajor = LhsNestedType::bRowMajor;
28
29 PBAT_HOST_DEVICE HorizontalStack(LhsNestedType const& lhs, RhsNestedType const& rhs)
30 : mLhs(lhs), mRhs(rhs)
31 {
32 static_assert(
33 LhsNestedType::kRows == RhsNestedType::kRows,
34 "lhs and rhs must have same rows");
35 }
36
37 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const
38 {
39 return (j < LhsNestedType::kCols) ? mLhs(i, j) : mRhs(i, j - LhsNestedType::kCols);
40 }
41
42 // Vector(ized) access
43 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
44 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
45
46 PBAT_MINI_READ_API(SelfType)
47
48 private:
49 LhsNestedType const& mLhs;
50 RhsNestedType const& mRhs;
51};
52
53template <class /*CMatrix*/ TLhsMatrix, class /*CMatrix*/ TRhsMatrix>
54class VerticalStack
55{
56 public:
57 using LhsNestedType = TLhsMatrix;
58 using RhsNestedType = TRhsMatrix;
59 using ScalarType = typename LhsNestedType::ScalarType;
60 using SelfType = VerticalStack<LhsNestedType, RhsNestedType>;
61
62 static auto constexpr kRows = LhsNestedType::kRows + RhsNestedType::kRows;
63 static auto constexpr kCols = LhsNestedType::kCols;
64 static bool constexpr bRowMajor = LhsNestedType::bRowMajor;
65
66 PBAT_HOST_DEVICE VerticalStack(LhsNestedType const& lhs, RhsNestedType const& rhs)
67 : mLhs(lhs), mRhs(rhs)
68 {
69 static_assert(
70 LhsNestedType::kCols == RhsNestedType::kCols,
71 "lhs and rhs must have same columns");
72 }
73
74 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const
75 {
76 return (i < LhsNestedType::kRows) ? mLhs(i, j) : mRhs(i - LhsNestedType::kRows, j);
77 }
78
79 // Vector(ized) access
80 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
81 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
82
83 PBAT_MINI_READ_API(SelfType)
84
85 private:
86 LhsNestedType const& mLhs;
87 RhsNestedType const& mRhs;
88};
89
90template <class /*CMatrix*/ TLhsMatrix, class /*CMatrix*/ TRhsMatrix>
91PBAT_HOST_DEVICE auto HStack(TLhsMatrix&& A, TRhsMatrix&& B)
92{
93 using LhsMatrixType = std::remove_cvref_t<TLhsMatrix>;
94 using RhsMatrixType = std::remove_cvref_t<TRhsMatrix>;
95 PBAT_MINI_CHECK_CMATRIX(LhsMatrixType);
96 PBAT_MINI_CHECK_CMATRIX(RhsMatrixType);
98 std::forward<TLhsMatrix>(A),
99 std::forward<TRhsMatrix>(B));
100}
101
102template <class /*CMatrix*/ TLhsMatrix, class /*CMatrix*/ TRhsMatrix>
103PBAT_HOST_DEVICE auto VStack(TLhsMatrix&& A, TRhsMatrix&& B)
104{
105 using LhsMatrixType = std::remove_cvref_t<TLhsMatrix>;
106 using RhsMatrixType = std::remove_cvref_t<TRhsMatrix>;
107 PBAT_MINI_CHECK_CMATRIX(LhsMatrixType);
108 PBAT_MINI_CHECK_CMATRIX(RhsMatrixType);
109 return VerticalStack<LhsMatrixType, RhsMatrixType>(
110 std::forward<TLhsMatrix>(A),
111 std::forward<TRhsMatrix>(B));
112}
113
114} // namespace mini
115} // namespace linalg
116} // namespace math
117} // namespace pbat
118
119#endif // PBAT_MATH_LINALG_MINI_STACK_H
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