11#ifndef PBAT_MATH_LINEAR_OPERATOR_H
12#define PBAT_MATH_LINEAR_OPERATOR_H
38 } -> std::convertible_to<int>;
41 } -> std::convertible_to<int>;
42 {t.Apply(
VectorX{}, std::declval<VectorX&>())};
43 {t.Apply(
MatrixX{}, std::declval<MatrixX&>())};
46 } -> std::convertible_to<CSCMatrix>;
59struct traits<
pbat::math::LinearOperator<TLinearOperators...>>
62 using StorageIndex = pbat::CSCMatrix::StorageIndex;
63 using StorageKind = Sparse;
64 using XprKind = MatrixXpr;
66 RowsAtCompileTime = Dynamic,
67 ColsAtCompileTime = Dynamic,
68 MaxRowsAtCompileTime = Dynamic,
69 MaxColsAtCompileTime = Dynamic,
94class LinearOperator :
public Eigen::EigenBase<LinearOperator<TLinearOperators...>>
105 ColsAtCompileTime = Eigen::Dynamic,
106 MaxColsAtCompileTime = Eigen::Dynamic,
107 RowsAtCompileTime = Eigen::Dynamic,
108 MaxRowsAtCompileTime = Eigen::Dynamic,
133 template <
class TDerivedIn,
class TDerivedOut>
134 void Apply(Eigen::MatrixBase<TDerivedIn>
const& x, Eigen::DenseBase<TDerivedOut>& y)
const;
174 Eigen::Product<SelfType, Rhs, Eigen::AliasFreeProduct>
177 return Eigen::Product<SelfType, Rhs, Eigen::AliasFreeProduct>(*
this, x.derived());
181 std::tuple<TLinearOperators
const&...> ops;
193template <CLinearOperator... TLinearOperators>
199template <CLinearOperator... TLinearOperators>
201 : ops(std::make_tuple(std::cref(inOps)...))
203 bool const bInputDimensionsMatch = std::apply(
204 [
this](
auto... op) ->
bool {
208 bool const bOutputDimensionsMatch = std::apply(
209 [
this](
auto... op) ->
bool {
213 if (not(bInputDimensionsMatch and bOutputDimensionsMatch))
215 throw std::invalid_argument(
216 "Dimensionality mismatch found in CompositeLinearOperator's linear operators.");
221template <
class TDerivedIn,
class TDerivedOut>
223 Eigen::MatrixBase<TDerivedIn>
const& x,
224 Eigen::DenseBase<TDerivedOut>& y)
const
226 PBAT_PROFILE_NAMED_SCOPE(
"pbat.math.LinearOperator.Apply");
227 std::apply([&](
auto... op) { (op.Apply(x, y), ...); }, ops);
233 PBAT_PROFILE_NAMED_SCOPE(
"pbat.math.LinearOperator.ToMatrix");
235 std::apply([&](
auto... op) { ((M += op.ToMatrix()), ...); }, ops);
242 return std::get<0>(ops).OutputDimensions();
248 return std::get<0>(ops).InputDimensions();
280template <pbat::math::CLinearOperator Lhs,
typename Rhs,
int ProductType>
281struct generic_product_impl<Lhs, Rhs, SparseShape, DenseShape, ProductType>
282 : generic_product_impl_base<Lhs, Rhs, generic_product_impl<Lhs, Rhs>>
284 typedef typename Product<Lhs, Rhs>::Scalar Scalar;
286 template <
typename Dst>
287 static void scaleAndAddTo(Dst& dst, Lhs
const& lhs, Rhs
const& rhs, Scalar
const& alpha)
289 lhs.Apply(alpha * rhs, dst);
Zero-overhead composite type satisfying the CLinearOperator concept.
Definition LinearOperator.h:95
BaseType::Index rows() const
Number of rows (Eigen compatibility)
Definition LinearOperator.h:159
pbat::Index OutputDimensions() const
Number of rows.
Definition LinearOperator.h:240
SelfType NestedExpression
Definition LinearOperator.h:103
Eigen::EigenBase< SelfType > BaseType
Definition LinearOperator.h:98
LinearOperator(TLinearOperators const &... inOps)
Construct a new Linear Operator object from a list of linear operators.
Definition LinearOperator.h:200
pbat::Index InputDimensions() const
Number of columns.
Definition LinearOperator.h:246
pbat::Scalar RealScalar
Definition LinearOperator.h:101
BaseType::Index cols() const
Number of columns (Eigen compatibility)
Definition LinearOperator.h:164
void Apply(Eigen::MatrixBase< TDerivedIn > const &x, Eigen::DenseBase< TDerivedOut > &y) const
Applies all linear operators on x, adding result to y.
Definition LinearOperator.h:222
Eigen::Product< SelfType, Rhs, Eigen::AliasFreeProduct > operator*(Eigen::MatrixBase< Rhs > const &x) const
Lazily left-multiply x by this linear operator.
Definition LinearOperator.h:175
LinearOperator< TLinearOperators... > SelfType
Instance type.
Definition LinearOperator.h:97
CSCMatrix ToMatrix() const
Construct the matrix of all underlying matrices obtained by Lops.
Definition LinearOperator.h:231
pbat::Scalar Scalar
Definition LinearOperator.h:100
typename CSCMatrix::StorageIndex StorageIndex
Definition LinearOperator.h:102
Concept for operator that satisfies linearity in the mathematical sense.
Definition LinearOperator.h:34
Math related functionality.
Definition Concepts.h:19
LinearOperator< TLinearOperators... > ComposeLinearOperators(TLinearOperators const &... inOps)
Compose multiple linear operators into a single linear operator.
Definition LinearOperator.h:194
The main namespace of the library.
Definition Aliases.h:15
Eigen::SparseMatrix< Scalar, Eigen::ColMajor > CSCMatrix
Column-major sparse matrix type.
Definition Aliases.h:52
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic-size matrix type.
Definition Aliases.h:34
Eigen::Vector< Scalar, Eigen::Dynamic > VectorX
Dynamic-size vector type.
Definition Aliases.h:33
std::ptrdiff_t Index
Index type.
Definition Aliases.h:17
double Scalar
Scalar type.
Definition Aliases.h:18
Profiling utilities for the Physics-Based Animation Toolkit (PBAT)