11#ifndef EIGEN_SPARSE_QR_H
12#define EIGEN_SPARSE_QR_H
16template<
typename MatrixType,
typename OrderingType>
class SparseQR;
17template<
typename SparseQRType>
struct SparseQRMatrixQReturnType;
18template<
typename SparseQRType>
struct SparseQRMatrixQTransposeReturnType;
19template<
typename SparseQRType,
typename Derived>
struct SparseQR_QProduct;
23 typedef typename SparseQRType::MatrixType ReturnType;
24 typedef typename ReturnType::StorageIndex StorageIndex;
25 typedef typename ReturnType::StorageKind StorageKind;
33 typedef typename SparseQRType::MatrixType ReturnType;
37 typedef typename Derived::PlainObject ReturnType;
83template<
typename _MatrixType,
typename _OrderingType>
88 using Base::m_isInitialized;
90 using Base::_solve_impl;
91 typedef _MatrixType MatrixType;
93 typedef typename MatrixType::Scalar Scalar;
94 typedef typename MatrixType::RealScalar RealScalar;
95 typedef typename MatrixType::StorageIndex StorageIndex;
102 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
103 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
107 SparseQR () : m_analysisIsok(
false), m_lastError(
""), m_useDefaultThreshold(
true),m_isQSorted(
false),m_isEtreeOk(
false)
164 eigen_assert(m_isInitialized &&
"The factorization should be called first, use compute()");
165 return m_nonzeropivots;
194 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
195 return m_outputPerm_c;
204 template<
typename Rhs,
typename Dest>
207 eigen_assert(m_isInitialized &&
"The factorization should be called first, use compute()");
208 eigen_assert(this->
rows() == B.rows() &&
"SparseQR::solve() : invalid number of rows in the right hand side matrix");
213 typename Dest::PlainObject y, b;
218 y.
resize((std::max<Index>)(
cols(),y.rows()),y.cols());
224 else dest = y.topRows(
cols());
237 m_useDefaultThreshold =
false;
238 m_threshold = threshold;
245 template<
typename Rhs>
248 eigen_assert(m_isInitialized &&
"The factorization should be called first, use compute()");
249 eigen_assert(this->
rows() == B.rows() &&
"SparseQR::solve() : invalid number of rows in the right hand side matrix");
252 template<
typename Rhs>
255 eigen_assert(m_isInitialized &&
"The factorization should be called first, use compute()");
256 eigen_assert(this->
rows() == B.rows() &&
"SparseQR::solve() : invalid number of rows in the right hand side matrix");
270 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
276 inline void _sort_matrix_Q()
278 if(this->m_isQSorted)
return;
282 this->m_isQSorted =
true;
288 bool m_factorizationIsok;
290 std::string m_lastError;
294 ScalarVector m_hcoeffs;
295 PermutationType m_perm_c;
296 PermutationType m_pivotperm;
297 PermutationType m_outputPerm_c;
298 RealScalar m_threshold;
299 bool m_useDefaultThreshold;
300 Index m_nonzeropivots;
302 IndexVector m_firstRowElt;
306 template <
typename,
typename >
friend struct SparseQR_QProduct;
319template <
typename MatrixType,
typename OrderingType>
322 eigen_assert(
mat.isCompressed() &&
"SparseQR requires a sparse matrix in compressed mode. Call .makeCompressed() before passing it to SparseQR");
332 if (!m_perm_c.size())
335 m_perm_c.indices().
setLinSpaced(n, 0,StorageIndex(n-1));
339 m_outputPerm_c = m_perm_c.inverse();
340 internal::coletree(
matCpy, m_etree, m_firstRowElt, m_outputPerm_c.indices().data());
350 m_analysisIsok =
true;
360template <
typename MatrixType,
typename OrderingType>
365 eigen_assert(m_analysisIsok &&
"analyzePattern() should be called before this step");
366 StorageIndex m = StorageIndex(
mat.rows());
367 StorageIndex n = StorageIndex(
mat.cols());
368 StorageIndex
diagSize = (std::min)(m,n);
380 m_outputPerm_c = m_perm_c.
inverse();
381 internal::coletree(m_pmat, m_etree, m_firstRowElt, m_outputPerm_c.indices().data());
394 if(MatrixType::IsRowMajor)
400 for (
int i = 0; i < n; i++)
402 Index p = m_perm_c.size() ? m_perm_c.indices()(i) : i;
412 if(m_useDefaultThreshold)
422 m_pivotperm.setIdentity(n);
428 for (StorageIndex col = 0; col < n; ++col)
449 StorageIndex
st = m_firstRowElt(
curIdx);
452 m_lastError =
"Empty row found during numerical factorization";
459 for (; mark(
st) != col;
st = m_etree(
st))
506 StorageIndex
iQ = StorageIndex(
itq.row());
516 Scalar
tau = RealScalar(0);
528 if(
sqrNorm == RealScalar(0) && numext::imag(
c0) == RealScalar(0))
530 beta = numext::real(
c0);
537 if(numext::real(
c0) >= RealScalar(0))
542 tau = numext::conj((beta-
c0) / beta);
560 m_R.insertBackByOuterInner(col,
nonzeroCol) = beta;
578 std::swap(m_pivotperm.indices()(j), m_pivotperm.indices()[j+1]);
581 internal::coletree(m_pmat, m_etree, m_firstRowElt, m_pivotperm.indices().data());
590 m_Q.makeCompressed();
592 m_R.makeCompressed();
601 m_R =
tempR * m_pivotperm;
604 m_outputPerm_c = m_outputPerm_c * m_pivotperm;
607 m_isInitialized =
true;
608 m_factorizationIsok =
true;
612template <
typename SparseQRType,
typename Derived>
615 typedef typename SparseQRType::QRMatrixType MatrixType;
616 typedef typename SparseQRType::Scalar Scalar;
619 m_qr(
qr),m_other(other),m_transpose(transpose) {}
620 inline Index rows()
const {
return m_qr.matrixQ().rows(); }
621 inline Index cols()
const {
return m_other.cols(); }
624 template<
typename DesType>
625 void evalTo(
DesType& res)
const
627 Index m = m_qr.rows();
628 Index n = m_qr.cols();
633 eigen_assert(m_qr.m_Q.rows() == m_other.rows() &&
"Non conforming object sizes");
635 for(
Index j = 0; j < res.cols(); j++){
638 Scalar
tau = Scalar(0);
639 tau = m_qr.m_Q.col(k).
dot(res.col(j));
640 if(
tau==Scalar(0))
continue;
641 tau =
tau * m_qr.m_hcoeffs(k);
642 res.col(j) -=
tau * m_qr.m_Q.col(k);
648 eigen_assert(m_qr.matrixQ().cols() == m_other.rows() &&
"Non conforming object sizes");
650 res.conservativeResize(rows(), cols());
653 for(
Index j = 0; j < res.cols(); j++)
658 Scalar
tau = Scalar(0);
659 tau = m_qr.m_Q.col(k).
dot(res.col(j));
660 if(
tau==Scalar(0))
continue;
661 tau =
tau * numext::conj(m_qr.m_hcoeffs(k));
662 res.col(j) -=
tau * m_qr.m_Q.col(k);
669 const Derived& m_other;
673template<
typename SparseQRType>
676 typedef typename SparseQRType::Scalar Scalar;
683 template<
typename Derived>
693 inline Index rows()
const {
return m_qr.rows(); }
694 inline Index cols()
const {
return m_qr.rows(); }
704template<
typename SparseQRType>
708 template<
typename Derived>
718template<
typename SparseQRType>
721 typedef typename SparseQRType::MatrixType MatrixType;
726template<
typename DstXprType,
typename SparseQRType>
730 typedef typename DstXprType::Scalar Scalar;
731 typedef typename DstXprType::StorageIndex StorageIndex;
734 typename DstXprType::PlainObject
idMat(
src.rows(),
src.cols());
742template<
typename DstXprType,
typename SparseQRType>
746 typedef typename DstXprType::Scalar Scalar;
747 typedef typename DstXprType::StorageIndex StorageIndex;
750 dst =
src.m_qr.matrixQ() * DstXprType::Identity(
src.m_qr.rows(),
src.m_qr.rows());
EIGEN_DEVICE_FUNC void resize(Index newSize)
Only plain matrices/arrays, not expressions, may be resized; therefore the only useful resize methods...
Definition DenseBase.h:246
EIGEN_DEVICE_FUNC Derived & setLinSpaced(Index size, const Scalar &low, const Scalar &high)
Sets a linearly spaced vector.
Definition CwiseNullaryOp.h:430
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR Index nonZeros() const
Definition DenseBase.h:215
EIGEN_DEVICE_FUNC CoeffReturnType value() const
Definition DenseBase.h:526
EIGEN_DEVICE_FUNC Derived & setZero()
Sets all coefficients in this expression to zero.
Definition CwiseNullaryOp.h:546
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
EIGEN_DEVICE_FUNC ScalarBinaryOpTraits< typenameinternal::traits< Derived >::Scalar, typenameinternal::traits< OtherDerived >::Scalar >::ReturnType dot(const MatrixBase< OtherDerived > &other) const
EIGEN_DEVICE_FUNC const AdjointReturnType adjoint() const
Definition Transpose.h:221
EIGEN_DEVICE_FUNC RealScalar norm() const
Definition Dot.h:103
EIGEN_DEVICE_FUNC const Inverse< Derived > inverse() const
\lu_module
Definition InverseImpl.h:348
EIGEN_DEVICE_FUNC Derived & setIdentity()
Writes the identity expression (not necessarily square) into *this.
Definition CwiseNullaryOp.h:873
EIGEN_DEVICE_FUNC Index size() const
Definition PermutationMatrix.h:97
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Resizes to the given size, and sets all coefficients in this expression to the given value val.
Definition CwiseNullaryOp.h:361
Definition ReturnByValue.h:52
Index rows() const
Definition SparseMatrix.h:138
Index cols() const
Definition SparseMatrix.h:140
Sparse left-looking QR factorization with numerical column pivoting.
Definition SparseQR.h:85
std::string lastErrorMessage() const
Definition SparseQR.h:201
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SparseQR.h:268
void analyzePattern(const MatrixType &mat)
Preprocessing step of a QR factorization.
Definition SparseQR.h:320
void factorize(const MatrixType &mat)
Performs the numerical QR factorization of the input matrix.
Definition SparseQR.h:361
Index cols() const
Definition SparseQR.h:141
const Solve< SparseQR, Rhs > solve(const MatrixBase< Rhs > &B) const
Definition SparseQR.h:246
const PermutationType & colsPermutation() const
Definition SparseQR.h:192
Index rank() const
Definition SparseQR.h:162
SparseQRMatrixQReturnType< SparseQR > matrixQ() const
Definition SparseQR.h:186
const QRMatrixType & matrixR() const
Definition SparseQR.h:156
Index rows() const
Definition SparseQR.h:137
SparseQR(const MatrixType &mat)
Construct a QR factorization of the matrix mat.
Definition SparseQR.h:116
void setPivotThreshold(const RealScalar &threshold)
Sets the threshold that is used to determine linearly dependent columns during the factorization.
Definition SparseQR.h:235
void compute(const MatrixType &mat)
Computes the QR factorization of the sparse matrix mat.
Definition SparseQR.h:127
A base class for sparse solvers.
Definition SparseSolverBase.h:68
ComputationInfo
Enum for reporting the status of a computation.
Definition Constants.h:440
@ InvalidInput
The inputs are invalid, or the algorithm has been improperly called.
Definition Constants.h:449
@ Success
Computation was successful.
Definition Constants.h:442
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition Constants.h:22
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:24418
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition EigenBase.h:30
Eigen::Index Index
The interface type of indices.
Definition EigenBase.h:39
Holds information about the various numeric (i.e.
Definition NumTraits.h:236
Definition SparseQR.h:675
Definition SparseQR.h:706
Definition SparseQR.h:614
Definition Constants.h:537
Definition AssignEvaluator.h:824
Definition Constants.h:542
Definition SparseAssign.h:62
Definition SparseAssign.h:61
Definition AssignmentFunctors.h:21
Definition CoreEvaluators.h:80
Definition XprHelper.h:679
Definition ForwardDeclarations.h:17