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
Assign.h
1#ifndef PBAT_MATH_LINALG_MINI_ASSIGN_H
2#define PBAT_MATH_LINALG_MINI_ASSIGN_H
3
4#include "Concepts.h"
5#include "pbat/HostDevice.h"
6
7#include <utility>
8
9namespace pbat {
10namespace math {
11namespace linalg {
12namespace mini {
13
14#define DefineMatrixMatrixAssign(FunctionName, Operator) \
15 template <class TLhsMatrix, class TRhsMatrix> \
16 PBAT_HOST_DEVICE void FunctionName(TLhsMatrix&& A, TRhsMatrix&& B) \
17 { \
18 using LhsMatrixType = std::remove_cvref_t<TLhsMatrix>; \
19 using RhsMatrixType = std::remove_cvref_t<TRhsMatrix>; \
20 static_assert(CMatrix<LhsMatrixType>, "Left input must satisfy concept CMatrix"); \
21 static_assert(CMatrix<RhsMatrixType>, "Right input must satisfy concept CMatrix"); \
22 static_assert( \
23 LhsMatrixType::kRows == RhsMatrixType::kRows and \
24 LhsMatrixType::kCols == RhsMatrixType::kCols, \
25 "A and B must have same dimensions"); \
26 using IntegerType = std::remove_const_t<decltype(LhsMatrixType::kRows)>; \
27 if constexpr (LhsMatrixType::bRowMajor and RhsMatrixType::bRowMajor) \
28 { \
29 auto fCols = [&]<IntegerType... J>( \
30 IntegerType i, \
31 std::integer_sequence<IntegerType, J...>) { \
32 ((std::forward<TLhsMatrix>(A)(i, J) Operator std::forward<TRhsMatrix>(B)(i, J)), \
33 ...); \
34 }; \
35 auto fRows = [&]<IntegerType... I>(std::integer_sequence<IntegerType, I...>) { \
36 (fCols(I, std::make_integer_sequence<IntegerType, LhsMatrixType::kCols>()), ...); \
37 }; \
38 fRows(std::make_integer_sequence<IntegerType, LhsMatrixType::kRows>()); \
39 } \
40 else \
41 { \
42 auto fRows = [&]<IntegerType... I>( \
43 IntegerType j, \
44 std::integer_sequence<IntegerType, I...>) { \
45 ((std::forward<TLhsMatrix>(A)(I, j) Operator std::forward<TRhsMatrix>(B)(I, j)), \
46 ...); \
47 }; \
48 auto fCols = [&]<IntegerType... J>(std::integer_sequence<IntegerType, J...>) { \
49 (fRows(J, std::make_integer_sequence<IntegerType, LhsMatrixType::kRows>()), ...); \
50 }; \
51 fCols(std::make_integer_sequence<IntegerType, LhsMatrixType::kCols>()); \
52 } \
53 }
54
55#define DefineMatrixScalarAssign(FunctionName, Operator) \
56 template <class TLhsMatrix> \
57 PBAT_HOST_DEVICE void FunctionName( \
58 TLhsMatrix&& A, \
59 typename std::remove_cvref_t<TLhsMatrix>::ScalarType k) \
60 { \
61 using LhsMatrixType = std::remove_cvref_t<TLhsMatrix>; \
62 static_assert(CMatrix<LhsMatrixType>, "Left input must satisfy concept CMatrix"); \
63 using IntegerType = std::remove_const_t<decltype(LhsMatrixType::kRows)>; \
64 if constexpr (LhsMatrixType::bRowMajor) \
65 { \
66 auto fCols = \
67 [&]<IntegerType... J>(IntegerType i, std::integer_sequence<IntegerType, J...>) { \
68 ((std::forward<TLhsMatrix>(A)(i, J) Operator k), ...); \
69 }; \
70 auto fRows = [&]<IntegerType... I>(std::integer_sequence<IntegerType, I...>) { \
71 (fCols(I, std::make_integer_sequence<IntegerType, LhsMatrixType::kCols>()), ...); \
72 }; \
73 fRows(std::make_integer_sequence<IntegerType, LhsMatrixType::kRows>()); \
74 } \
75 else \
76 { \
77 auto fRows = \
78 [&]<IntegerType... I>(IntegerType j, std::integer_sequence<IntegerType, I...>) { \
79 ((std::forward<TLhsMatrix>(A)(I, j) Operator k), ...); \
80 }; \
81 auto fCols = [&]<IntegerType... J>(std::integer_sequence<IntegerType, J...>) { \
82 (fRows(J, std::make_integer_sequence<IntegerType, LhsMatrixType::kRows>()), ...); \
83 }; \
84 fCols(std::make_integer_sequence<IntegerType, LhsMatrixType::kCols>()); \
85 } \
86 }
87
88DefineMatrixMatrixAssign(Assign, =);
89DefineMatrixMatrixAssign(AddAssign, +=);
90DefineMatrixMatrixAssign(SubtractAssign, -=);
91DefineMatrixScalarAssign(AssignScalar, =);
92DefineMatrixScalarAssign(AddAssignScalar, +=);
93DefineMatrixScalarAssign(SubtractAssignScalar, -=);
94DefineMatrixScalarAssign(MultiplyAssign, *=);
95DefineMatrixScalarAssign(DivideAssign, /=);
96
97#define PBAT_MINI_ASSIGN_API(SelfType) \
98 template <class TOtherMatrix> \
99 PBAT_HOST_DEVICE SelfType& operator=(TOtherMatrix&& B) \
100 { \
101 Assign(*this, std::forward<TOtherMatrix>(B)); \
102 return *this; \
103 }
104
105} // namespace mini
106} // namespace linalg
107} // namespace math
108} // namespace pbat
109
110#endif // PBAT_MATH_LINALG_MINI_ASSIGN_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