1#ifndef PBAT_MATH_LINALG_MINI_UNARYOPERATIONS_H
2#define PBAT_MATH_LINALG_MINI_UNARYOPERATIONS_H
8#include "pbat/HostDevice.h"
21template <
class TMatrix>
25 using NestedType = TMatrix;
26 using ScalarType =
typename NestedType::ScalarType;
27 using SelfType = Square<NestedType>;
29 static auto constexpr kRows = NestedType::kRows;
30 static auto constexpr kCols = NestedType::kCols;
31 static bool constexpr bRowMajor = NestedType::bRowMajor;
33 PBAT_HOST_DEVICE Square(NestedType
const& _A) : A(_A) {}
35 PBAT_HOST_DEVICE ScalarType operator()(
auto i,
auto j)
const {
return A(i, j) * A(i, j); }
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); }
41 PBAT_MINI_READ_API(SelfType)
47template <
class TLhsMatrix>
51 using LhsNestedType = TLhsMatrix;
52 using ScalarType =
typename LhsNestedType::ScalarType;
53 using SelfType = Reciprocal<LhsNestedType>;
55 static auto constexpr kRows = LhsNestedType::kRows;
56 static auto constexpr kCols = LhsNestedType::kCols;
57 static bool constexpr bRowMajor = LhsNestedType::bRowMajor;
59 PBAT_HOST_DEVICE Reciprocal(LhsNestedType
const& A) : mA(A) {}
61 PBAT_HOST_DEVICE ScalarType operator()(
auto i,
auto j)
const
63 return ScalarType(1) / mA(i, j);
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); }
70 PBAT_MINI_READ_API(SelfType)
73 LhsNestedType
const& mA;
76template <
class TMatrix>
80 using NestedType = TMatrix;
81 using ScalarType =
typename NestedType::ScalarType;
82 using SelfType = Absolute<NestedType>;
84 static auto constexpr kRows = NestedType::kRows;
85 static auto constexpr kCols = NestedType::kCols;
86 static bool constexpr bRowMajor = NestedType::bRowMajor;
88 PBAT_HOST_DEVICE Absolute(NestedType
const& _A) : A(_A) {}
90 PBAT_HOST_DEVICE ScalarType operator()(
auto i,
auto j)
const
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); }
100 PBAT_MINI_READ_API(SelfType)
106template <
class TMatrix>
107PBAT_HOST_DEVICE
auto Squared(TMatrix&& A)
109 using MatrixType = std::remove_cvref_t<TMatrix>;
110 PBAT_MINI_CHECK_CMATRIX(MatrixType);
114template <
class TMatrix>
115PBAT_HOST_DEVICE
auto Abs(TMatrix&& A)
117 using MatrixType = std::remove_cvref_t<TMatrix>;
118 PBAT_MINI_CHECK_CMATRIX(MatrixType);
119 return Absolute<MatrixType>(std::forward<TMatrix>(A));
122template <
class TMatrix>
123PBAT_HOST_DEVICE
auto Normalized(TMatrix&& A)
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));
130template <CReadableVectorizedMatrix TMatrix>
131PBAT_HOST_DEVICE
auto Min(TMatrix
const& A)
133 using IntegerType = std::remove_const_t<
decltype(TMatrix::kRows)>;
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))), ...);
142 auto minimum = [&]<IntegerType... K>(std::integer_sequence<IntegerType, K...>) {
143 return std::min({A(K)...});
146 return minimum(std::make_integer_sequence<IntegerType, TMatrix::kRows * TMatrix::kCols>());
149template <CReadableVectorizedMatrix TMatrix>
150PBAT_HOST_DEVICE
auto Max(TMatrix
const& A)
152 using IntegerType = std::remove_const_t<
decltype(TMatrix::kRows)>;
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))), ...);
161 auto maximum = [&]<IntegerType... K>(std::integer_sequence<IntegerType, K...>) {
162 return std::max({A(K)...});
165 return maximum(std::make_integer_sequence<IntegerType, TMatrix::kRows * TMatrix::kCols>());
168template <
class TMatrix>
169PBAT_HOST_DEVICE
auto operator/(
typename std::remove_cvref_t<TMatrix>::ScalarType k, TMatrix&& A)
171 using MatrixType = std::remove_cvref_t<TMatrix>;
172 PBAT_MINI_CHECK_CMATRIX(MatrixType);
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