Eigen  5.0.1
 
Loading...
Searching...
No Matches
SparseSolverBase.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPARSESOLVERBASE_H
12#define EIGEN_SPARSESOLVERBASE_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
25template <typename Decomposition, typename Rhs, typename Dest>
26std::enable_if_t<Rhs::ColsAtCompileTime != 1 && Dest::ColsAtCompileTime != 1> solve_sparse_through_dense_panels(
27 const Decomposition& dec, const Rhs& rhs, Dest& dest) {
28 EIGEN_STATIC_ASSERT((Dest::Flags & RowMajorBit) == 0, THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
29 using DestScalar = typename Dest::Scalar;
30 // we process the sparse rhs per block of NbColsAtOnce columns temporarily stored into a dense matrix.
31 static const Index NbColsAtOnce = 4;
32 Index rhsCols = rhs.cols();
33 Index size = rhs.rows();
34 // the temporary matrices do not need more columns than NbColsAtOnce:
35 Index tmpCols = (std::min)(rhsCols, NbColsAtOnce);
36 Eigen::Matrix<DestScalar, Dynamic, Dynamic> tmp(size, tmpCols);
37 Eigen::Matrix<DestScalar, Dynamic, Dynamic> tmpX(size, tmpCols);
38 for (Index k = 0; k < rhsCols; k += NbColsAtOnce) {
39 Index actualCols = std::min<Index>(rhsCols - k, NbColsAtOnce);
40 tmp.leftCols(actualCols) = rhs.middleCols(k, actualCols);
41 tmpX.leftCols(actualCols) = dec.solve(tmp.leftCols(actualCols));
42 dest.middleCols(k, actualCols) = tmpX.leftCols(actualCols).sparseView();
43 }
44}
45
46// Overload for vector as rhs
47template <typename Decomposition, typename Rhs, typename Dest>
48std::enable_if_t<Rhs::ColsAtCompileTime == 1 || Dest::ColsAtCompileTime == 1> solve_sparse_through_dense_panels(
49 const Decomposition& dec, const Rhs& rhs, Dest& dest) {
50 using DestScalar = typename Dest::Scalar;
51 Index size = rhs.rows();
52 Eigen::Matrix<DestScalar, Dynamic, 1> rhs_dense(rhs);
53 Eigen::Matrix<DestScalar, Dynamic, 1> dest_dense(size);
54 dest_dense = dec.solve(rhs_dense);
55 dest = dest_dense.sparseView();
56}
57
58} // end namespace internal
59
67template <typename Derived>
69 public:
71 SparseSolverBase() = default;
72
73 SparseSolverBase(const SparseSolverBase&) = delete;
74 SparseSolverBase& operator=(const SparseSolverBase&) = delete;
75
76 SparseSolverBase(SparseSolverBase&& other) : m_isInitialized{other.m_isInitialized} {}
77
78 ~SparseSolverBase() = default;
79
80 Derived& derived() { return *static_cast<Derived*>(this); }
81 const Derived& derived() const { return *static_cast<const Derived*>(this); }
82
87 template <typename Rhs>
88 inline Solve<Derived, Rhs> solve(const MatrixBase<Rhs>& b) const {
89 eigen_assert(m_isInitialized && "Solver is not initialized.");
90 eigen_assert(derived().rows() == b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
91 return Solve<Derived, Rhs>(derived(), b.derived());
92 }
93
98 template <typename Rhs>
100 eigen_assert(m_isInitialized && "Solver is not initialized.");
101 eigen_assert(derived().rows() == b.rows() && "solve(): invalid number of rows of the right hand side matrix b");
102 return Solve<Derived, Rhs>(derived(), b.derived());
103 }
104
105#ifndef EIGEN_PARSED_BY_DOXYGEN
107 template <typename Rhs, typename Dest>
108 void _solve_impl(const SparseMatrixBase<Rhs>& b, SparseMatrixBase<Dest>& dest) const {
109 internal::solve_sparse_through_dense_panels(derived(), b.derived(), dest.derived());
110 }
111#endif // EIGEN_PARSED_BY_DOXYGEN
112
113 protected:
114 mutable bool m_isInitialized = false;
115};
116
117} // end namespace Eigen
118
119#endif // EIGEN_SPARSESOLVERBASE_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
Pseudo expression representing a solving operation.
Definition Solve.h:63
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
Index rows() const
Definition SparseMatrixBase.h:182
A base class for sparse solvers.
Definition SparseSolverBase.h:68
Solve< Derived, Rhs > solve(const SparseMatrixBase< Rhs > &b) const
Definition SparseSolverBase.h:99
Solve< Derived, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition SparseSolverBase.h:88
constexpr unsigned int RowMajorBit
Definition Constants.h:71
constexpr Derived & derived()
Definition EigenBase.h:50