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;
83template<
typename _MatrixType,
typename _OrderingType>
93 typedef typename MatrixType::Scalar
Scalar;
204 template<
typename Rhs,
typename Dest>
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());
219 y.topRows(
rank) = this->
matrixR().topLeftCorner(rank,
rank).template triangularView<Upper>().solve(
b.topRows(
rank));
220 y.bottomRows(
y.rows()-
rank).setZero();
224 else dest =
y.topRows(
cols());
245 template<
typename Rhs>
249 eigen_assert(this->
rows() == B.rows() &&
"SparseQR::solve() : invalid number of rows in the right hand side matrix");
252 template<
typename Rhs>
256 eigen_assert(this->
rows() == B.
rows() &&
"SparseQR::solve() : invalid number of rows in the right hand side matrix");
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");
327 ord(matCpy, m_perm_c);
328 Index n = mat.cols();
329 Index m = mat.rows();
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();
344 m_Q.resize(m, diagSize);
347 m_R.reserve(2*mat.nonZeros());
348 m_Q.reserve(2*mat.nonZeros());
349 m_hcoeffs.resize(diagSize);
350 m_analysisIsok =
true;
360template <
typename MatrixType,
typename OrderingType>
365 eigen_assert(m_analysisIsok &&
"analyzePattern() should be called before this step");
371 Index nzcolR, nzcolQ;
380 m_outputPerm_c = m_perm_c.inverse();
393 const StorageIndex *originalOuterIndices = mat.outerIndexPtr();
394 if(MatrixType::IsRowMajor)
396 originalOuterIndicesCpy = IndexVector::Map(m_pmat.outerIndexPtr(),n+1);
397 originalOuterIndices = originalOuterIndicesCpy.
data();
400 for (
int i = 0; i < n; i++)
402 Index p = m_perm_c.size() ? m_perm_c.indices()(i) : i;
403 m_pmat.outerIndexPtr()[p] = originalOuterIndices[i];
404 m_pmat.innerNonZeroPtr()[p] = originalOuterIndices[i+1] - originalOuterIndices[i];
412 if(m_useDefaultThreshold)
415 for (
int j = 0; j < n; j++) max2Norm =
numext::maxi(max2Norm, m_pmat.col(j).norm());
422 m_pivotperm.setIdentity(n);
432 mark(nonzeroCol) =
col;
433 Qidx(0) = nonzeroCol;
434 nzcolR = 0; nzcolQ = 1;
435 bool found_diag = nonzeroCol>=m;
446 if(curIdx == nonzeroCol) found_diag =
true;
452 m_lastError =
"Empty row found during numerical factorization";
459 for (; mark(st) !=
col; st = m_etree(st))
471 if(itp) tval(curIdx) = itp.value();
472 else tval(curIdx) =
Scalar(0);
475 if(curIdx > nonzeroCol && mark(curIdx) !=
col )
477 Qidx(nzcolQ) = curIdx;
484 for (
Index i = nzcolR-1; i >= 0; i--)
486 Index curIdx = Ridx(i);
492 tdot = m_Q.col(curIdx).dot(tval);
494 tdot *= m_hcoeffs(curIdx);
499 tval(itq.row()) -= itq.value() * tdot;
502 if(m_etree(Ridx(i)) == nonzeroCol)
519 if(nonzeroCol < diagSize)
527 for (
Index itq = 1; itq < nzcolQ; ++itq) sqrNorm +=
numext::abs2(tval(Qidx(itq)));
540 for (
Index itq = 1; itq < nzcolQ; ++itq)
541 tval(Qidx(itq)) /= (c0 -
beta);
542 tau = numext::conj((
beta-c0) /
beta);
548 for (
Index i = nzcolR-1; i >= 0; i--)
550 Index curIdx = Ridx(i);
551 if(curIdx < nonzeroCol)
553 m_R.insertBackByOuterInnerUnordered(
col, curIdx) = tval(curIdx);
554 tval(curIdx) =
Scalar(0.);
558 if(nonzeroCol < diagSize &&
abs(
beta) >= pivotThreshold)
560 m_R.insertBackByOuterInner(
col, nonzeroCol) =
beta;
562 m_hcoeffs(nonzeroCol) = tau;
564 for (
Index itq = 0; itq < nzcolQ; ++itq)
566 Index iQ = Qidx(itq);
567 m_Q.insertBackByOuterInnerUnordered(nonzeroCol,iQ) = tval(iQ);
571 if(nonzeroCol<diagSize)
572 m_Q.startVec(nonzeroCol);
577 for (
Index j = nonzeroCol; j < n-1; j++)
578 std::swap(m_pivotperm.indices()(j), m_pivotperm.indices()[j+1]);
586 m_hcoeffs.tail(diagSize-nonzeroCol).setZero();
590 m_Q.makeCompressed();
592 m_R.makeCompressed();
595 m_nonzeropivots = nonzeroCol;
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>
616 typedef typename SparseQRType::Scalar
Scalar;
624 template<
typename DesType>
635 for(
Index j = 0; j < res.cols(); j++){
636 for (
Index k = 0; k < diagSize; k++)
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);
650 res.conservativeResize(
rows(),
cols());
653 for(
Index j = 0; j < res.cols(); j++)
656 for (
Index k = start_k; k >=0; k--)
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);
673template<
typename SparseQRType>
676 typedef typename SparseQRType::Scalar
Scalar;
683 template<
typename Derived>
704template<
typename SparseQRType>
708 template<
typename Derived>
718template<
typename SparseQRType>
726template<
typename DstXprType,
typename SparseQRType>
730 typedef typename DstXprType::Scalar
Scalar;
734 typename DstXprType::PlainObject idMat(src.
rows(), src.
cols());
737 const_cast<SparseQRType *
>(&src.
m_qr)->_sort_matrix_Q();
742template<
typename DstXprType,
typename SparseQRType>
746 typedef typename DstXprType::Scalar
Scalar;
750 dst = src.
m_qr.matrixQ() * DstXprType::Identity(src.
m_qr.rows(), src.
m_qr.rows());
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE ColXpr col(Index i)
This is the const version of col().
Definition: BlockMethods.h:1097
EIGEN_DEVICE_FUNC RealReturnType real() const
Definition: CommonCwiseUnaryOps.h:100
EIGEN_DEVICE_FUNC const ImagReturnType imag() const
Definition: CommonCwiseUnaryOps.h:109
#define eigen_assert(x)
Definition: Macros.h:1047
constexpr common_return_t< T1, T2 > beta(const T1 a, const T2 b) noexcept
Compile-time beta function.
Definition: beta.hpp:36
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:50
EIGEN_DEVICE_FUNC Index size() const
Definition: PermutationMatrix.h:97
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: PlainObjectBase.h:247
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:562
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
Pseudo expression representing a solving operation.
Definition: Solve.h:63
Definition: SparseCompressedBase.h:159
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:28
const Derived & derived() const
Definition: SparseMatrixBase.h:143
Index rows() const
Definition: SparseMatrixBase.h:176
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
void _sort_matrix_Q()
Definition: SparseQR.h:276
bool m_factorizationIsok
Definition: SparseQR.h:288
std::string lastErrorMessage() const
Definition: SparseQR.h:201
MatrixType::Scalar Scalar
Definition: SparseQR.h:93
IndexVector m_firstRowElt
Definition: SparseQR.h:302
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SparseQR.h:268
ComputationInfo m_info
Definition: SparseQR.h:289
MatrixType::StorageIndex StorageIndex
Definition: SparseQR.h:95
MatrixType::RealScalar RealScalar
Definition: SparseQR.h:94
PermutationType m_perm_c
Definition: SparseQR.h:295
PermutationType m_pivotperm
Definition: SparseQR.h:296
QRMatrixType m_pmat
Definition: SparseQR.h:291
Index m_nonzeropivots
Definition: SparseQR.h:300
void analyzePattern(const MatrixType &mat)
Preprocessing step of a QR factorization.
Definition: SparseQR.h:320
IndexVector m_etree
Definition: SparseQR.h:301
std::string m_lastError
Definition: SparseQR.h:290
SparseSolverBase< SparseQR< _MatrixType, _OrderingType > > Base
Definition: SparseQR.h:87
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
SparseQR()
Definition: SparseQR.h:107
const Solve< SparseQR, Rhs > solve(const MatrixBase< Rhs > &B) const
Definition: SparseQR.h:246
PermutationType m_outputPerm_c
Definition: SparseQR.h:297
const PermutationType & colsPermutation() const
Definition: SparseQR.h:192
_OrderingType OrderingType
Definition: SparseQR.h:92
RealScalar m_threshold
Definition: SparseQR.h:298
Index rank() const
Definition: SparseQR.h:162
QRMatrixType m_R
Definition: SparseQR.h:292
PermutationMatrix< Dynamic, Dynamic, StorageIndex > PermutationType
Definition: SparseQR.h:99
ScalarVector m_hcoeffs
Definition: SparseQR.h:294
bool m_isEtreeOk
Definition: SparseQR.h:304
Matrix< Scalar, Dynamic, 1 > ScalarVector
Definition: SparseQR.h:98
@ MaxColsAtCompileTime
Definition: SparseQR.h:103
@ ColsAtCompileTime
Definition: SparseQR.h:102
SparseQRMatrixQReturnType< SparseQR > matrixQ() const
Definition: SparseQR.h:186
const QRMatrixType & matrixR() const
Definition: SparseQR.h:156
Index rows() const
Definition: SparseQR.h:137
bool m_useDefaultThreshold
Definition: SparseQR.h:299
SparseQR(const MatrixType &mat)
Construct a QR factorization of the matrix mat.
Definition: SparseQR.h:116
SparseMatrix< Scalar, ColMajor, StorageIndex > QRMatrixType
Definition: SparseQR.h:96
bool m_analysisIsok
Definition: SparseQR.h:287
const Solve< SparseQR, Rhs > solve(const SparseMatrixBase< Rhs > &B) const
Definition: SparseQR.h:253
Matrix< StorageIndex, Dynamic, 1 > IndexVector
Definition: SparseQR.h:97
_MatrixType MatrixType
Definition: SparseQR.h:91
bool _solve_impl(const MatrixBase< Rhs > &B, MatrixBase< Dest > &dest) const
Definition: SparseQR.h:205
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
bool m_isQSorted
Definition: SparseQR.h:303
QRMatrixType m_Q
Definition: SparseQR.h:293
A base class for sparse solvers.
Definition: SparseSolverBase.h:68
void _solve_impl(const SparseMatrixBase< Rhs > &b, SparseMatrixBase< Dest > &dest) const
Definition: SparseSolverBase.h:111
bool m_isInitialized
Definition: SparseSolverBase.h:119
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
auto sqrt(const UnitType &value) noexcept -> unit_t< square_root< typename units::traits::unit_t_traits< UnitType >::unit_type >, typename units::traits::unit_t_traits< UnitType >::underlying_type, linear_scale >
computes the square root of value
Definition: math.h:483
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
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
const Scalar & y
Definition: MathFunctions.h:821
int coletree(const MatrixType &mat, IndexVector &parent, IndexVector &firstRowElt, typename MatrixType::StorageIndex *perm=0)
Compute the column elimination tree of a sparse matrix.
Definition: SparseColEtree.h:61
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T maxi(const T &x, const T &y)
Definition: MathFunctions.h:1091
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T mini(const T &x, const T &y)
Definition: MathFunctions.h:1083
EIGEN_DEVICE_FUNC bool abs2(bool x)
Definition: MathFunctions.h:1292
Namespace containing all symbols from the Eigen library.
Definition: Core:141
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
Definition: Eigen_Colamd.h:50
NetworkTables (ntcore) namespace.
Definition: Topic.inc:15
void swap(wpi::SmallPtrSet< T, N > &LHS, wpi::SmallPtrSet< T, N > &RHS)
Implement std::swap in terms of SmallPtrSet swap.
Definition: SmallPtrSet.h:512
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:233
Definition: SparseQR.h:614
Index cols() const
Definition: SparseQR.h:621
const SparseQRType & m_qr
Definition: SparseQR.h:668
void evalTo(DesType &res) const
Definition: SparseQR.h:625
SparseQRType::QRMatrixType MatrixType
Definition: SparseQR.h:615
SparseQR_QProduct(const SparseQRType &qr, const Derived &other, bool transpose)
Definition: SparseQR.h:618
bool m_transpose
Definition: SparseQR.h:670
SparseQRType::Scalar Scalar
Definition: SparseQR.h:616
const Derived & m_other
Definition: SparseQR.h:669
Index rows() const
Definition: SparseQR.h:620
Definition: SparseQR.h:675
SparseQRMatrixQTransposeReturnType< SparseQRType > transpose() const
Definition: SparseQR.h:696
SparseQRMatrixQTransposeReturnType< SparseQRType > adjoint() const
Definition: SparseQR.h:689
SparseQR_QProduct< SparseQRType, Derived > operator*(const MatrixBase< Derived > &other)
Definition: SparseQR.h:684
Index rows() const
Definition: SparseQR.h:693
SparseQRMatrixQReturnType(const SparseQRType &qr)
Definition: SparseQR.h:682
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: SparseQR.h:677
const SparseQRType & m_qr
Definition: SparseQR.h:700
Index cols() const
Definition: SparseQR.h:694
SparseQRType::Scalar Scalar
Definition: SparseQR.h:676
@ ColsAtCompileTime
Definition: SparseQR.h:680
@ RowsAtCompileTime
Definition: SparseQR.h:679
Definition: SparseQR.h:706
const SparseQRType & m_qr
Definition: SparseQR.h:713
SparseQR_QProduct< SparseQRType, Derived > operator*(const MatrixBase< Derived > &other)
Definition: SparseQR.h:709
SparseQRMatrixQTransposeReturnType(const SparseQRType &qr)
Definition: SparseQR.h:707
Definition: Constants.h:537
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseQR.h:732
DstXprType::Scalar Scalar
Definition: SparseQR.h:730
DstXprType::StorageIndex StorageIndex
Definition: SparseQR.h:731
SparseQRMatrixQReturnType< SparseQRType > SrcXprType
Definition: SparseQR.h:729
SparseQRMatrixQReturnType< SparseQRType > SrcXprType
Definition: SparseQR.h:745
DstXprType::Scalar Scalar
Definition: SparseQR.h:746
static void run(DstXprType &dst, const SrcXprType &src, const internal::assign_op< Scalar, Scalar > &)
Definition: SparseQR.h:748
DstXprType::StorageIndex StorageIndex
Definition: SparseQR.h:747
Definition: AssignEvaluator.h:824
Definition: Constants.h:542
Definition: SparseAssign.h:62
Definition: SparseAssign.h:61
Definition: AssignmentFunctors.h:21
SparseShape Shape
Definition: SparseQR.h:723
storage_kind_to_evaluator_kind< typenameMatrixType::StorageKind >::Kind Kind
Definition: SparseQR.h:722
SparseQRType::MatrixType MatrixType
Definition: SparseQR.h:721
Definition: CoreEvaluators.h:80
Definition: XprHelper.h:679
Derived::PlainObject ReturnType
Definition: SparseQR.h:37
ReturnType::StorageIndex StorageIndex
Definition: SparseQR.h:24
SparseQRType::MatrixType ReturnType
Definition: SparseQR.h:23
ReturnType::StorageKind StorageKind
Definition: SparseQR.h:25
SparseQRType::MatrixType ReturnType
Definition: SparseQR.h:33
Definition: ForwardDeclarations.h:17