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
Scale.h
1#ifndef PBAT_MATH_LINALG_MINI_SCALE_H
2#define PBAT_MATH_LINALG_MINI_SCALE_H
3
4#include "Api.h"
5#include "Assign.h"
6#include "Concepts.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>
18class Scale
19{
20 public:
21 using NestedType = TMatrix;
22 using ScalarType = typename NestedType::ScalarType;
23 using SelfType = Scale<NestedType>;
24
25 static auto constexpr kRows = NestedType::kRows;
26 static auto constexpr kCols = NestedType::kCols;
27 static bool constexpr bRowMajor = NestedType::bRowMajor;
28
29 PBAT_HOST_DEVICE Scale(ScalarType _k, NestedType const& _A) : k(_k), A(_A) {}
30
31 PBAT_HOST_DEVICE ScalarType operator()(auto i, auto j) const { return k * A(i, j); }
32
33 // Vector(ized) access
34 PBAT_HOST_DEVICE ScalarType operator()(auto i) const { return (*this)(i % kRows, i / kRows); }
35 PBAT_HOST_DEVICE ScalarType operator[](auto i) const { return (*this)(i); }
36
37 PBAT_MINI_READ_API(SelfType)
38
39 private:
40 ScalarType k;
41 NestedType const& A;
42};
43
44template <class /*CMatrix*/ TMatrix>
45PBAT_HOST_DEVICE auto operator*(typename std::remove_cvref_t<TMatrix>::ScalarType k, TMatrix&& A)
46{
47 using MatrixType = std::remove_cvref_t<TMatrix>;
48 PBAT_MINI_CHECK_CMATRIX(MatrixType);
49 return Scale<MatrixType>(k, std::forward<TMatrix>(A));
50}
51
52template <class /*CMatrix*/ TMatrix>
53PBAT_HOST_DEVICE auto operator*(TMatrix&& A, typename std::remove_cvref_t<TMatrix>::ScalarType k)
54{
55 return k * std::forward<TMatrix>(A);
56}
57
58template <class /*CMatrix*/ TMatrix>
59PBAT_HOST_DEVICE auto operator*=(TMatrix&& A, typename std::remove_cvref_t<TMatrix>::ScalarType k)
60{
61 MultiplyAssign(std::forward<TMatrix>(A), k);
62 return A;
63}
64
65template <class /*CMatrix*/ TMatrix>
66PBAT_HOST_DEVICE auto operator/(TMatrix&& A, typename std::remove_cvref_t<TMatrix>::ScalarType k)
67{
68 using MatrixType = std::remove_cvref_t<TMatrix>;
69 PBAT_MINI_CHECK_CMATRIX(MatrixType);
70 using ScalarType = typename MatrixType::ScalarType;
71 return Scale<MatrixType>(ScalarType(1. / k), std::forward<TMatrix>(A));
72}
73
74template <class /*CMatrix*/ TMatrix>
75PBAT_HOST_DEVICE auto operator/=(TMatrix&& A, typename std::remove_cvref_t<TMatrix>::ScalarType k)
76{
77 DivideAssign(std::forward<TMatrix>(A), k);
78 return A;
79}
80
81template <class /*CMatrix*/ TMatrix>
82PBAT_HOST_DEVICE auto operator-(TMatrix&& A)
83{
84 using MatrixType = std::remove_cvref_t<TMatrix>;
85 PBAT_MINI_CHECK_CMATRIX(MatrixType);
86 using ScalarType = typename MatrixType::ScalarType;
87 return Scale<MatrixType>(ScalarType(-1.), std::forward<TMatrix>(A));
88}
89
90} // namespace mini
91} // namespace linalg
92} // namespace math
93} // namespace pbat
94
95#endif // PBAT_MATH_LINALG_MINI_SCALE_H
Definition Scale.h:19
Mini linear algebra related functionality.
Definition Assign.h:12
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
Math related functionality.
Definition Concepts.h:19
Rational operator*(Integer a, Rational const &b)
Multiplication operation between Rational and integral type.
Definition Rational.h:164
The main namespace of the library.
Definition Aliases.h:15