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
UnaryOperations.h
1#ifndef PBAT_MATH_LINALG_MINI_UNARYOPERATIONS_H
2#define PBAT_MATH_LINALG_MINI_UNARYOPERATIONS_H
3
4#include "Api.h"
5#include "Concepts.h"
6#include "Norm.h"
7#include "Scale.h"
8#include "pbat/HostDevice.h"
9
10#include <algorithm>
11#include <cmath>
12#include <limits>
13#include <type_traits>
14#include <utility>
15
16namespace pbat {
17namespace math {
18namespace linalg {
19namespace mini {
20
21template <class /*CMatrix*/ TMatrix>
22class Square
23{
24 public:
25 using NestedType = TMatrix;
26 using ScalarType = typename NestedType::ScalarType;
27 using SelfType = Square<NestedType>;
28
29 static auto constexpr kRows = NestedType::kRows;
30 static auto constexpr kCols = NestedType::kCols;
31 static bool constexpr bRowMajor = NestedType::bRowMajor;
32
33 PBAT_HOST_DEVICE Square(NestedType const& _A) : A(_A) {}
34
35 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const { return A(i, j) * A(i, j); }
36
37 // Vector(ized) access
38 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
39 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
40
41 PBAT_MINI_READ_API(SelfType)
42
43 private:
44 NestedType const& A;
45};
46
47template <class /*CMatrix*/ TLhsMatrix>
48class Reciprocal
49{
50 public:
51 using LhsNestedType = TLhsMatrix;
52 using ScalarType = typename LhsNestedType::ScalarType;
53 using SelfType = Reciprocal<LhsNestedType>;
54
55 static auto constexpr kRows = LhsNestedType::kRows;
56 static auto constexpr kCols = LhsNestedType::kCols;
57 static bool constexpr bRowMajor = LhsNestedType::bRowMajor;
58
59 PBAT_HOST_DEVICE Reciprocal(LhsNestedType const& A) : mA(A) {}
60
61 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const
62 {
63 return ScalarType(1) / mA(i, j);
64 }
65
66 // Vector(ized) access
67 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
68 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
69
70 PBAT_MINI_READ_API(SelfType)
71
72 private:
73 LhsNestedType const& mA;
74};
75
76template <class /*CMatrix*/ TMatrix>
77class Absolute
78{
79 public:
80 using NestedType = TMatrix;
81 using ScalarType = typename NestedType::ScalarType;
82 using SelfType = Absolute<NestedType>;
83
84 static auto constexpr kRows = NestedType::kRows;
85 static auto constexpr kCols = NestedType::kCols;
86 static bool constexpr bRowMajor = NestedType::bRowMajor;
87
88 PBAT_HOST_DEVICE Absolute(NestedType const& _A) : A(_A) {}
89
90 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const
91 {
92 using namespace std;
93 return abs(A(i, j));
94 }
95
96 // Vector(ized) access
97 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
98 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
99
100 PBAT_MINI_READ_API(SelfType)
101
102 private:
103 NestedType const& A;
104};
105
106template <class /*CMatrix*/ TMatrix>
107PBAT_HOST_DEVICE auto Squared(TMatrix&& A)
108{
109 using MatrixType = std::remove_cvref_t<TMatrix>;
110 PBAT_MINI_CHECK_CMATRIX(MatrixType);
111 return Square<MatrixType>(std::forward<TMatrix>(A));
112}
113
114template <class /*CMatrix*/ TMatrix>
115PBAT_HOST_DEVICE auto Abs(TMatrix&& A)
116{
117 using MatrixType = std::remove_cvref_t<TMatrix>;
118 PBAT_MINI_CHECK_CMATRIX(MatrixType);
119 return Absolute<MatrixType>(std::forward<TMatrix>(A));
120}
121
122template <class /*CMatrix*/ TMatrix>
123PBAT_HOST_DEVICE auto Normalized(TMatrix&& A)
124{
125 using MatrixType = std::remove_cvref_t<TMatrix>;
126 PBAT_MINI_CHECK_CMATRIX(MatrixType);
127 return std::forward<TMatrix>(A) / Norm(std::forward<TMatrix>(A));
128}
129
130template <CReadableVectorizedMatrix TMatrix>
131PBAT_HOST_DEVICE auto Min(TMatrix const& A)
132{
133 using IntegerType = std::remove_const_t<decltype(TMatrix::kRows)>;
134#ifdef __CUDACC__
135 using ScalarType = typename TMatrix::ScalarType;
136 auto minimum = [&]<IntegerType... K>(std::integer_sequence<IntegerType, K...>) {
137 auto m = std::numeric_limits<ScalarType>::max();
138 ((m = min(m, A(K))), ...);
139 return m;
140 };
141#else
142 auto minimum = [&]<IntegerType... K>(std::integer_sequence<IntegerType, K...>) {
143 return std::min({A(K)...});
144 };
145#endif
146 return minimum(std::make_integer_sequence<IntegerType, TMatrix::kRows * TMatrix::kCols>());
147}
148
149template <CReadableVectorizedMatrix TMatrix>
150PBAT_HOST_DEVICE auto Max(TMatrix const& A)
151{
152 using IntegerType = std::remove_const_t<decltype(TMatrix::kRows)>;
153#ifdef __CUDACC__
154 using ScalarType = typename TMatrix::ScalarType;
155 auto maximum = [&]<IntegerType... K>(std::integer_sequence<IntegerType, K...>) {
156 auto m = std::numeric_limits<ScalarType>::lowest();
157 ((m = max(m, A(K))), ...);
158 return m;
159 };
160#else
161 auto maximum = [&]<IntegerType... K>(std::integer_sequence<IntegerType, K...>) {
162 return std::max({A(K)...});
163 };
164#endif
165 return maximum(std::make_integer_sequence<IntegerType, TMatrix::kRows * TMatrix::kCols>());
166}
167
168template <class /*CMatrix*/ TMatrix>
169PBAT_HOST_DEVICE auto operator/(typename std::remove_cvref_t<TMatrix>::ScalarType k, TMatrix&& A)
170{
171 using MatrixType = std::remove_cvref_t<TMatrix>;
172 PBAT_MINI_CHECK_CMATRIX(MatrixType);
173 return k * Reciprocal<MatrixType>(std::forward<TMatrix>(A));
174}
175
176} // namespace mini
177} // namespace linalg
178} // namespace math
179} // namespace pbat
180
181#endif // PBAT_MATH_LINALG_MINI_UNARYOPERATIONS_H
Definition UnaryOperations.h:49
Definition UnaryOperations.h:23
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