11 #ifndef EIGEN_SPARSE_QR_H
12 #define EIGEN_SPARSE_QR_H
19 template <
typename MatrixType,
typename OrderingType>
21 template <
typename SparseQRType>
22 struct SparseQRMatrixQReturnType;
23 template <
typename SparseQRType>
24 struct SparseQRMatrixQTransposeReturnType;
25 template <
typename SparseQRType,
typename Derived>
26 struct SparseQR_QProduct;
28 template <
typename SparseQRType>
35 template <
typename SparseQRType>
39 template <
typename SparseQRType,
typename Derived>
87 template <
typename MatrixType_,
typename OrderingType_>
201 template <
typename Rhs,
typename Dest>
205 "SparseQR::solve() : invalid number of rows in the right hand side matrix");
210 typename Dest::PlainObject
y,
b;
215 y.resize((std::max<Index>)(
cols(),
y.rows()),
y.cols());
216 y.topRows(
rank) = this->
matrixR().topLeftCorner(rank,
rank).template triangularView<Upper>().solve(
b.topRows(
rank));
217 y.bottomRows(
y.rows() -
rank).setZero();
223 dest =
y.topRows(
cols());
243 template <
typename Rhs>
247 "SparseQR::solve() : invalid number of rows in the right hand side matrix");
250 template <
typename Rhs>
254 "SparseQR::solve() : invalid number of rows in the right hand side matrix");
300 template <
typename,
typename>
313 template <
typename MatrixType,
typename OrderingType>
317 "SparseQR requires a sparse matrix in compressed mode. Call .makeCompressed() before passing it to SparseQR");
319 std::conditional_t<MatrixType::IsRowMajor, QRMatrixType, const MatrixType&> matCpy(
mat);
322 ord(matCpy, m_perm_c);
327 if (!m_perm_c.size()) {
333 m_outputPerm_c = m_perm_c.inverse();
338 m_Q.resize(
m, diagSize);
344 m_hcoeffs.resize(diagSize);
345 m_analysisIsok =
true;
355 template <
typename MatrixType,
typename OrderingType>
359 eigen_assert(m_analysisIsok &&
"analyzePattern() should be called before this step");
366 Index nzcolR, nzcolQ;
373 m_outputPerm_c = m_perm_c.inverse();
387 if (MatrixType::IsRowMajor) {
388 originalOuterIndicesCpy = IndexVector::Map(m_pmat.outerIndexPtr(),
n + 1);
389 originalOuterIndices = originalOuterIndicesCpy.
data();
392 for (
int i = 0;
i <
n;
i++) {
393 Index p = m_perm_c.size() ? m_perm_c.indices()(
i) :
i;
394 m_pmat.outerIndexPtr()[
p] = originalOuterIndices[
i];
395 m_pmat.innerNonZeroPtr()[
p] = originalOuterIndices[
i + 1] - originalOuterIndices[
i];
404 if (m_useDefaultThreshold) {
406 for (
int j = 0;
j <
n;
j++) max2Norm =
numext::maxi(max2Norm, m_pmat.col(
j).norm());
410 pivotThreshold = m_threshold;
414 m_pivotperm.setIdentity(
n);
423 mark(nonzeroCol) =
col;
424 Qidx(0) = nonzeroCol;
427 bool found_diag = nonzeroCol >=
m;
437 if (curIdx == nonzeroCol) found_diag =
true;
442 m_lastError =
"Empty row found during numerical factorization";
449 for (; mark(st) !=
col; st = m_etree(st)) {
456 Index nt = nzcolR - bi;
461 tval(curIdx) = itp.value();
466 if (curIdx > nonzeroCol && mark(curIdx) !=
col) {
467 Qidx(nzcolQ) = curIdx;
474 for (
Index i = nzcolR - 1;
i >= 0;
i--) {
481 tdot = m_Q.col(curIdx).dot(tval);
483 tdot *= m_hcoeffs(curIdx);
486 tval -= tdot * m_Q.col(curIdx);
489 if (m_etree(Ridx(
i)) == nonzeroCol) {
492 if (mark(iQ) !=
col) {
503 if (nonzeroCol < diagSize) {
510 for (
Index itq = 1; itq < nzcolQ; ++itq) sqrNorm +=
numext::abs2(tval(Qidx(itq)));
519 for (
Index itq = 1; itq < nzcolQ; ++itq) tval(Qidx(itq)) /= (c0 -
beta);
525 for (
Index i = nzcolR - 1;
i >= 0;
i--) {
527 if (curIdx < nonzeroCol) {
528 m_R.insertBackByOuterInnerUnordered(
col, curIdx) = tval(curIdx);
529 tval(curIdx) =
Scalar(0.);
533 if (nonzeroCol < diagSize &&
abs(
beta) >= pivotThreshold) {
534 m_R.insertBackByOuterInner(
col, nonzeroCol) =
beta;
536 m_hcoeffs(nonzeroCol) = tau;
538 for (
Index itq = 0; itq < nzcolQ; ++itq) {
539 Index iQ = Qidx(itq);
540 m_Q.insertBackByOuterInnerUnordered(nonzeroCol, iQ) = tval(iQ);
544 if (nonzeroCol < diagSize) m_Q.startVec(nonzeroCol);
547 for (
Index j = nonzeroCol;
j <
n - 1;
j++)
std::swap(m_pivotperm.indices()(
j), m_pivotperm.indices()[
j + 1]);
555 m_hcoeffs.tail(diagSize - nonzeroCol).setZero();
559 m_Q.makeCompressed();
561 m_R.makeCompressed();
564 m_nonzeropivots = nonzeroCol;
566 if (nonzeroCol <
n) {
569 m_R = tempR * m_pivotperm;
572 m_outputPerm_c = m_outputPerm_c * m_pivotperm;
575 m_isInitialized =
true;
576 m_factorizationIsok =
true;
580 template <
typename SparseQRType,
typename Derived>
591 template <
typename DesType>
601 for (
Index k = 0;
k < diagSize;
k++) {
604 if (tau ==
Scalar(0))
continue;
605 tau = tau *
m_qr.m_hcoeffs(
k);
617 for (
Index k = start_k;
k >= 0;
k--) {
620 if (tau ==
Scalar(0))
continue;
633 template <
typename SparseQRType>
639 template <
typename Derived>
657 template <
typename SparseQRType>
660 template <
typename Derived>
669 template <
typename SparseQRType>
676 template <
typename DstXprType,
typename SparseQRType>
683 typename DstXprType::PlainObject idMat(src.
rows(), src.
cols());
686 const_cast<SparseQRType*
>(&src.
m_qr)->_sort_matrix_Q();
691 template <
typename DstXprType,
typename SparseQRType>
698 dst = src.
m_qr.matrixQ() * DstXprType::Identity(src.
m_qr.rows(), src.
m_qr.rows());
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:133
AnnoyingScalar imag(const AnnoyingScalar &)
Definition: AnnoyingScalar.h:132
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Eigen::SparseMatrix< double > mat
Definition: EigenUnitTest.cpp:10
HouseholderQR< MatrixXf > qr(A)
#define eigen_assert(x)
Definition: Macros.h:910
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
float * p
Definition: Tutorial_Map_using.cpp:9
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:48
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
EIGEN_DEVICE_FUNC Index size() const
Definition: PermutationMatrix.h:96
constexpr EIGEN_DEVICE_FUNC const Scalar * data() const
Definition: PlainObjectBase.h:273
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:569
EIGEN_DEVICE_FUNC Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:365
Definition: ReturnByValue.h:50
Pseudo expression representing a solving operation.
Definition: Solve.h:62
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
Index rows() const
Definition: SparseMatrixBase.h:182
Index nonZeros() const
Definition: SparseCompressedBase.h:64
Index cols() const
Definition: SparseMatrix.h:161
bool isCompressed() const
Definition: SparseCompressedBase.h:114
Index rows() const
Definition: SparseMatrix.h:159
Base::InnerIterator InnerIterator
Definition: SparseMatrix.h:138
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:189
Sparse left-looking QR factorization with numerical column pivoting.
Definition: SparseQR.h:88
void setPivotThreshold(const RealScalar &threshold)
Definition: SparseQR.h:234
SparseSolverBase< SparseQR< MatrixType_, OrderingType_ > > Base
Definition: SparseQR.h:90
PermutationType m_perm_c
Definition: SparseQR.h:289
SparseMatrix< Scalar, ColMajor, StorageIndex > QRMatrixType
Definition: SparseQR.h:100
PermutationType m_outputPerm_c
Definition: SparseQR.h:291
void analyzePattern(const MatrixType &mat)
Preprocessing step of a QR factorization.
Definition: SparseQR.h:314
bool m_isQSorted
Definition: SparseQR.h:297
void _sort_matrix_Q()
Definition: SparseQR.h:272
IndexVector m_firstRowElt
Definition: SparseQR.h:296
const Solve< SparseQR, Rhs > solve(const SparseMatrixBase< Rhs > &B) const
Definition: SparseQR.h:251
MatrixType::Scalar Scalar
Definition: SparseQR.h:97
void factorize(const MatrixType &mat)
Performs the numerical QR factorization of the input matrix.
Definition: SparseQR.h:356
SparseQR(const MatrixType &mat)
Definition: SparseQR.h:117
@ ColsAtCompileTime
Definition: SparseQR.h:105
@ MaxColsAtCompileTime
Definition: SparseQR.h:105
RealScalar m_threshold
Definition: SparseQR.h:292
ScalarVector m_hcoeffs
Definition: SparseQR.h:288
Index cols() const
Definition: SparseQR.h:141
const QRMatrixType & matrixR() const
Definition: SparseQR.h:156
bool m_factorizationIsok
Definition: SparseQR.h:282
Matrix< StorageIndex, Dynamic, 1 > IndexVector
Definition: SparseQR.h:101
QRMatrixType m_Q
Definition: SparseQR.h:287
bool m_isEtreeOk
Definition: SparseQR.h:298
ComputationInfo m_info
Definition: SparseQR.h:283
std::string m_lastError
Definition: SparseQR.h:284
Matrix< Scalar, Dynamic, 1 > ScalarVector
Definition: SparseQR.h:102
Index rows() const
Definition: SparseQR.h:137
OrderingType_ OrderingType
Definition: SparseQR.h:96
QRMatrixType m_R
Definition: SparseQR.h:286
const PermutationType & colsPermutation() const
Definition: SparseQR.h:190
SparseQRMatrixQReturnType< SparseQR > matrixQ() const
Definition: SparseQR.h:185
IndexVector m_etree
Definition: SparseQR.h:295
bool _solve_impl(const MatrixBase< Rhs > &B, MatrixBase< Dest > &dest) const
Definition: SparseQR.h:202
const Solve< SparseQR, Rhs > solve(const MatrixBase< Rhs > &B) const
Definition: SparseQR.h:244
PermutationType m_pivotperm
Definition: SparseQR.h:290
std::string lastErrorMessage() const
Definition: SparseQR.h:198
void compute(const MatrixType &mat)
Definition: SparseQR.h:128
SparseQR()
Definition: SparseQR.h:108
MatrixType::RealScalar RealScalar
Definition: SparseQR.h:98
MatrixType::StorageIndex StorageIndex
Definition: SparseQR.h:99
MatrixType_ MatrixType
Definition: SparseQR.h:95
bool m_useDefaultThreshold
Definition: SparseQR.h:293
bool m_analysisIsok
Definition: SparseQR.h:281
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SparseQR.h:266
Index m_nonzeropivots
Definition: SparseQR.h:294
PermutationMatrix< Dynamic, Dynamic, StorageIndex > PermutationType
Definition: SparseQR.h:103
Index rank() const
Definition: SparseQR.h:162
QRMatrixType m_pmat
Definition: SparseQR.h:285
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
void _solve_impl(const SparseMatrixBase< Rhs > &b, SparseMatrixBase< Dest > &dest) const
Definition: SparseSolverBase.h:104
bool m_isInitialized
Definition: SparseSolverBase.h:110
Definition: matrices.h:74
float real
Definition: datatypes.h:10
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
ComputationInfo
Definition: Constants.h:438
@ InvalidInput
Definition: Constants.h:447
@ Success
Definition: Constants.h:440
Scalar * y
Definition: level1_cplx_impl.h:128
EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:117
int * m
Definition: level2_cplx_impl.h:294
Scalar beta
Definition: level2_cplx_impl.h:36
char char char int int * k
Definition: level2_impl.h:374
int coletree(const MatrixType &mat, IndexVector &parent, IndexVector &firstRowElt, typename MatrixType::StorageIndex *perm=0)
Definition: SparseColEtree.h:61
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:926
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:920
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1102
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
void transpose()
Definition: skew_symmetric_matrix3.cpp:135
Definition: Eigen_Colamd.h:49
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
Definition: EigenBase.h:33
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:43
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: SparseQR.h:634
SparseQRMatrixQTransposeReturnType< SparseQRType > adjoint() const
Definition: SparseQR.h:644
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: SparseQR.h:636
SparseQR_QProduct< SparseQRType, Derived > operator*(const MatrixBase< Derived > &other)
Definition: SparseQR.h:640
SparseQRMatrixQTransposeReturnType< SparseQRType > transpose() const
Definition: SparseQR.h:650
Index rows() const
Definition: SparseQR.h:647
SparseQRMatrixQReturnType(const SparseQRType &qr)
Definition: SparseQR.h:638
@ ColsAtCompileTime
Definition: SparseQR.h:637
@ RowsAtCompileTime
Definition: SparseQR.h:637
const SparseQRType & m_qr
Definition: SparseQR.h:653
Index cols() const
Definition: SparseQR.h:648
SparseQRType::Scalar Scalar
Definition: SparseQR.h:635
Definition: SparseQR.h:658
const SparseQRType & m_qr
Definition: SparseQR.h:664
SparseQR_QProduct< SparseQRType, Derived > operator*(const MatrixBase< Derived > &other)
Definition: SparseQR.h:661
SparseQRMatrixQTransposeReturnType(const SparseQRType &qr)
Definition: SparseQR.h:659
Definition: SparseQR.h:581
Index cols() const
Definition: SparseQR.h:588
const SparseQRType & m_qr
Definition: SparseQR.h:628
void evalTo(DesType &res) const
Definition: SparseQR.h:592
SparseQRType::QRMatrixType MatrixType
Definition: SparseQR.h:582
SparseQR_QProduct(const SparseQRType &qr, const Derived &other, bool transpose)
Definition: SparseQR.h:585
bool m_transpose
Definition: SparseQR.h:630
SparseQRType::Scalar Scalar
Definition: SparseQR.h:583
const Derived & m_other
Definition: SparseQR.h:629
Index rows() const
Definition: SparseQR.h:587
Definition: Constants.h:570
SparseQRMatrixQReturnType< SparseQRType > SrcXprType
Definition: SparseQR.h:694
DstXprType::Scalar Scalar
Definition: SparseQR.h:695
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseQR.h:697
DstXprType::StorageIndex StorageIndex
Definition: SparseQR.h:696
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseQR.h:682
DstXprType::Scalar Scalar
Definition: SparseQR.h:680
DstXprType::StorageIndex StorageIndex
Definition: SparseQR.h:681
SparseQRMatrixQReturnType< SparseQRType > SrcXprType
Definition: SparseQR.h:679
Definition: AssignEvaluator.h:773
Definition: Constants.h:577
Definition: SparseAssign.h:61
Definition: SparseAssign.h:60
Template functor for scalar/packet assignment.
Definition: AssignmentFunctors.h:25
SparseShape Shape
Definition: SparseQR.h:673
storage_kind_to_evaluator_kind< typename MatrixType::StorageKind >::Kind Kind
Definition: SparseQR.h:672
SparseQRType::MatrixType MatrixType
Definition: SparseQR.h:671
Definition: CoreEvaluators.h:95
Definition: XprHelper.h:844
ReturnType::StorageIndex StorageIndex
Definition: SparseQR.h:31
SparseQRType::MatrixType ReturnType
Definition: SparseQR.h:30
ReturnType::StorageKind StorageKind
Definition: SparseQR.h:32
SparseQRType::MatrixType ReturnType
Definition: SparseQR.h:37
Derived::PlainObject ReturnType
Definition: SparseQR.h:41
Definition: ForwardDeclarations.h:21
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2