1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2012 Désiré Nuentsa-Wakam <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2012-2014 Gael Guennebaud <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li
11*bf2c3715SXin Li
12*bf2c3715SXin Li #ifndef EIGEN_SPARSE_LU_H
13*bf2c3715SXin Li #define EIGEN_SPARSE_LU_H
14*bf2c3715SXin Li
15*bf2c3715SXin Li namespace Eigen {
16*bf2c3715SXin Li
17*bf2c3715SXin Li template <typename _MatrixType, typename _OrderingType = COLAMDOrdering<typename _MatrixType::StorageIndex> > class SparseLU;
18*bf2c3715SXin Li template <typename MappedSparseMatrixType> struct SparseLUMatrixLReturnType;
19*bf2c3715SXin Li template <typename MatrixLType, typename MatrixUType> struct SparseLUMatrixUReturnType;
20*bf2c3715SXin Li
21*bf2c3715SXin Li template <bool Conjugate,class SparseLUType>
22*bf2c3715SXin Li class SparseLUTransposeView : public SparseSolverBase<SparseLUTransposeView<Conjugate,SparseLUType> >
23*bf2c3715SXin Li {
24*bf2c3715SXin Li protected:
25*bf2c3715SXin Li typedef SparseSolverBase<SparseLUTransposeView<Conjugate,SparseLUType> > APIBase;
26*bf2c3715SXin Li using APIBase::m_isInitialized;
27*bf2c3715SXin Li public:
28*bf2c3715SXin Li typedef typename SparseLUType::Scalar Scalar;
29*bf2c3715SXin Li typedef typename SparseLUType::StorageIndex StorageIndex;
30*bf2c3715SXin Li typedef typename SparseLUType::MatrixType MatrixType;
31*bf2c3715SXin Li typedef typename SparseLUType::OrderingType OrderingType;
32*bf2c3715SXin Li
33*bf2c3715SXin Li enum {
34*bf2c3715SXin Li ColsAtCompileTime = MatrixType::ColsAtCompileTime,
35*bf2c3715SXin Li MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
36*bf2c3715SXin Li };
37*bf2c3715SXin Li
SparseLUTransposeView()38*bf2c3715SXin Li SparseLUTransposeView() : m_sparseLU(NULL) {}
SparseLUTransposeView(const SparseLUTransposeView & view)39*bf2c3715SXin Li SparseLUTransposeView(const SparseLUTransposeView& view) {
40*bf2c3715SXin Li this->m_sparseLU = view.m_sparseLU;
41*bf2c3715SXin Li }
setIsInitialized(const bool isInitialized)42*bf2c3715SXin Li void setIsInitialized(const bool isInitialized) {this->m_isInitialized = isInitialized;}
setSparseLU(SparseLUType * sparseLU)43*bf2c3715SXin Li void setSparseLU(SparseLUType* sparseLU) {m_sparseLU = sparseLU;}
44*bf2c3715SXin Li using APIBase::_solve_impl;
45*bf2c3715SXin Li template<typename Rhs, typename Dest>
_solve_impl(const MatrixBase<Rhs> & B,MatrixBase<Dest> & X_base)46*bf2c3715SXin Li bool _solve_impl(const MatrixBase<Rhs> &B, MatrixBase<Dest> &X_base) const
47*bf2c3715SXin Li {
48*bf2c3715SXin Li Dest& X(X_base.derived());
49*bf2c3715SXin Li eigen_assert(m_sparseLU->info() == Success && "The matrix should be factorized first");
50*bf2c3715SXin Li EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,
51*bf2c3715SXin Li THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
52*bf2c3715SXin Li
53*bf2c3715SXin Li
54*bf2c3715SXin Li // this ugly const_cast_derived() helps to detect aliasing when applying the permutations
55*bf2c3715SXin Li for(Index j = 0; j < B.cols(); ++j){
56*bf2c3715SXin Li X.col(j) = m_sparseLU->colsPermutation() * B.const_cast_derived().col(j);
57*bf2c3715SXin Li }
58*bf2c3715SXin Li //Forward substitution with transposed or adjoint of U
59*bf2c3715SXin Li m_sparseLU->matrixU().template solveTransposedInPlace<Conjugate>(X);
60*bf2c3715SXin Li
61*bf2c3715SXin Li //Backward substitution with transposed or adjoint of L
62*bf2c3715SXin Li m_sparseLU->matrixL().template solveTransposedInPlace<Conjugate>(X);
63*bf2c3715SXin Li
64*bf2c3715SXin Li // Permute back the solution
65*bf2c3715SXin Li for (Index j = 0; j < B.cols(); ++j)
66*bf2c3715SXin Li X.col(j) = m_sparseLU->rowsPermutation().transpose() * X.col(j);
67*bf2c3715SXin Li return true;
68*bf2c3715SXin Li }
rows()69*bf2c3715SXin Li inline Index rows() const { return m_sparseLU->rows(); }
cols()70*bf2c3715SXin Li inline Index cols() const { return m_sparseLU->cols(); }
71*bf2c3715SXin Li
72*bf2c3715SXin Li private:
73*bf2c3715SXin Li SparseLUType *m_sparseLU;
74*bf2c3715SXin Li SparseLUTransposeView& operator=(const SparseLUTransposeView&);
75*bf2c3715SXin Li };
76*bf2c3715SXin Li
77*bf2c3715SXin Li
78*bf2c3715SXin Li /** \ingroup SparseLU_Module
79*bf2c3715SXin Li * \class SparseLU
80*bf2c3715SXin Li *
81*bf2c3715SXin Li * \brief Sparse supernodal LU factorization for general matrices
82*bf2c3715SXin Li *
83*bf2c3715SXin Li * This class implements the supernodal LU factorization for general matrices.
84*bf2c3715SXin Li * It uses the main techniques from the sequential SuperLU package
85*bf2c3715SXin Li * (http://crd-legacy.lbl.gov/~xiaoye/SuperLU/). It handles transparently real
86*bf2c3715SXin Li * and complex arithmetic with single and double precision, depending on the
87*bf2c3715SXin Li * scalar type of your input matrix.
88*bf2c3715SXin Li * The code has been optimized to provide BLAS-3 operations during supernode-panel updates.
89*bf2c3715SXin Li * It benefits directly from the built-in high-performant Eigen BLAS routines.
90*bf2c3715SXin Li * Moreover, when the size of a supernode is very small, the BLAS calls are avoided to
91*bf2c3715SXin Li * enable a better optimization from the compiler. For best performance,
92*bf2c3715SXin Li * you should compile it with NDEBUG flag to avoid the numerous bounds checking on vectors.
93*bf2c3715SXin Li *
94*bf2c3715SXin Li * An important parameter of this class is the ordering method. It is used to reorder the columns
95*bf2c3715SXin Li * (and eventually the rows) of the matrix to reduce the number of new elements that are created during
96*bf2c3715SXin Li * numerical factorization. The cheapest method available is COLAMD.
97*bf2c3715SXin Li * See \link OrderingMethods_Module the OrderingMethods module \endlink for the list of
98*bf2c3715SXin Li * built-in and external ordering methods.
99*bf2c3715SXin Li *
100*bf2c3715SXin Li * Simple example with key steps
101*bf2c3715SXin Li * \code
102*bf2c3715SXin Li * VectorXd x(n), b(n);
103*bf2c3715SXin Li * SparseMatrix<double> A;
104*bf2c3715SXin Li * SparseLU<SparseMatrix<double>, COLAMDOrdering<int> > solver;
105*bf2c3715SXin Li * // fill A and b;
106*bf2c3715SXin Li * // Compute the ordering permutation vector from the structural pattern of A
107*bf2c3715SXin Li * solver.analyzePattern(A);
108*bf2c3715SXin Li * // Compute the numerical factorization
109*bf2c3715SXin Li * solver.factorize(A);
110*bf2c3715SXin Li * //Use the factors to solve the linear system
111*bf2c3715SXin Li * x = solver.solve(b);
112*bf2c3715SXin Li * \endcode
113*bf2c3715SXin Li *
114*bf2c3715SXin Li * \warning The input matrix A should be in a \b compressed and \b column-major form.
115*bf2c3715SXin Li * Otherwise an expensive copy will be made. You can call the inexpensive makeCompressed() to get a compressed matrix.
116*bf2c3715SXin Li *
117*bf2c3715SXin Li * \note Unlike the initial SuperLU implementation, there is no step to equilibrate the matrix.
118*bf2c3715SXin Li * For badly scaled matrices, this step can be useful to reduce the pivoting during factorization.
119*bf2c3715SXin Li * If this is the case for your matrices, you can try the basic scaling method at
120*bf2c3715SXin Li * "unsupported/Eigen/src/IterativeSolvers/Scaling.h"
121*bf2c3715SXin Li *
122*bf2c3715SXin Li * \tparam _MatrixType The type of the sparse matrix. It must be a column-major SparseMatrix<>
123*bf2c3715SXin Li * \tparam _OrderingType The ordering method to use, either AMD, COLAMD or METIS. Default is COLMAD
124*bf2c3715SXin Li *
125*bf2c3715SXin Li * \implsparsesolverconcept
126*bf2c3715SXin Li *
127*bf2c3715SXin Li * \sa \ref TutorialSparseSolverConcept
128*bf2c3715SXin Li * \sa \ref OrderingMethods_Module
129*bf2c3715SXin Li */
130*bf2c3715SXin Li template <typename _MatrixType, typename _OrderingType>
131*bf2c3715SXin Li class SparseLU : public SparseSolverBase<SparseLU<_MatrixType,_OrderingType> >, public internal::SparseLUImpl<typename _MatrixType::Scalar, typename _MatrixType::StorageIndex>
132*bf2c3715SXin Li {
133*bf2c3715SXin Li protected:
134*bf2c3715SXin Li typedef SparseSolverBase<SparseLU<_MatrixType,_OrderingType> > APIBase;
135*bf2c3715SXin Li using APIBase::m_isInitialized;
136*bf2c3715SXin Li public:
137*bf2c3715SXin Li using APIBase::_solve_impl;
138*bf2c3715SXin Li
139*bf2c3715SXin Li typedef _MatrixType MatrixType;
140*bf2c3715SXin Li typedef _OrderingType OrderingType;
141*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
142*bf2c3715SXin Li typedef typename MatrixType::RealScalar RealScalar;
143*bf2c3715SXin Li typedef typename MatrixType::StorageIndex StorageIndex;
144*bf2c3715SXin Li typedef SparseMatrix<Scalar,ColMajor,StorageIndex> NCMatrix;
145*bf2c3715SXin Li typedef internal::MappedSuperNodalMatrix<Scalar, StorageIndex> SCMatrix;
146*bf2c3715SXin Li typedef Matrix<Scalar,Dynamic,1> ScalarVector;
147*bf2c3715SXin Li typedef Matrix<StorageIndex,Dynamic,1> IndexVector;
148*bf2c3715SXin Li typedef PermutationMatrix<Dynamic, Dynamic, StorageIndex> PermutationType;
149*bf2c3715SXin Li typedef internal::SparseLUImpl<Scalar, StorageIndex> Base;
150*bf2c3715SXin Li
151*bf2c3715SXin Li enum {
152*bf2c3715SXin Li ColsAtCompileTime = MatrixType::ColsAtCompileTime,
153*bf2c3715SXin Li MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
154*bf2c3715SXin Li };
155*bf2c3715SXin Li
156*bf2c3715SXin Li public:
157*bf2c3715SXin Li
SparseLU()158*bf2c3715SXin Li SparseLU():m_lastError(""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
159*bf2c3715SXin Li {
160*bf2c3715SXin Li initperfvalues();
161*bf2c3715SXin Li }
SparseLU(const MatrixType & matrix)162*bf2c3715SXin Li explicit SparseLU(const MatrixType& matrix)
163*bf2c3715SXin Li : m_lastError(""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
164*bf2c3715SXin Li {
165*bf2c3715SXin Li initperfvalues();
166*bf2c3715SXin Li compute(matrix);
167*bf2c3715SXin Li }
168*bf2c3715SXin Li
~SparseLU()169*bf2c3715SXin Li ~SparseLU()
170*bf2c3715SXin Li {
171*bf2c3715SXin Li // Free all explicit dynamic pointers
172*bf2c3715SXin Li }
173*bf2c3715SXin Li
174*bf2c3715SXin Li void analyzePattern (const MatrixType& matrix);
175*bf2c3715SXin Li void factorize (const MatrixType& matrix);
176*bf2c3715SXin Li void simplicialfactorize(const MatrixType& matrix);
177*bf2c3715SXin Li
178*bf2c3715SXin Li /**
179*bf2c3715SXin Li * Compute the symbolic and numeric factorization of the input sparse matrix.
180*bf2c3715SXin Li * The input matrix should be in column-major storage.
181*bf2c3715SXin Li */
compute(const MatrixType & matrix)182*bf2c3715SXin Li void compute (const MatrixType& matrix)
183*bf2c3715SXin Li {
184*bf2c3715SXin Li // Analyze
185*bf2c3715SXin Li analyzePattern(matrix);
186*bf2c3715SXin Li //Factorize
187*bf2c3715SXin Li factorize(matrix);
188*bf2c3715SXin Li }
189*bf2c3715SXin Li
190*bf2c3715SXin Li /** \returns an expression of the transposed of the factored matrix.
191*bf2c3715SXin Li *
192*bf2c3715SXin Li * A typical usage is to solve for the transposed problem A^T x = b:
193*bf2c3715SXin Li * \code
194*bf2c3715SXin Li * solver.compute(A);
195*bf2c3715SXin Li * x = solver.transpose().solve(b);
196*bf2c3715SXin Li * \endcode
197*bf2c3715SXin Li *
198*bf2c3715SXin Li * \sa adjoint(), solve()
199*bf2c3715SXin Li */
transpose()200*bf2c3715SXin Li const SparseLUTransposeView<false,SparseLU<_MatrixType,_OrderingType> > transpose()
201*bf2c3715SXin Li {
202*bf2c3715SXin Li SparseLUTransposeView<false, SparseLU<_MatrixType,_OrderingType> > transposeView;
203*bf2c3715SXin Li transposeView.setSparseLU(this);
204*bf2c3715SXin Li transposeView.setIsInitialized(this->m_isInitialized);
205*bf2c3715SXin Li return transposeView;
206*bf2c3715SXin Li }
207*bf2c3715SXin Li
208*bf2c3715SXin Li
209*bf2c3715SXin Li /** \returns an expression of the adjoint of the factored matrix
210*bf2c3715SXin Li *
211*bf2c3715SXin Li * A typical usage is to solve for the adjoint problem A' x = b:
212*bf2c3715SXin Li * \code
213*bf2c3715SXin Li * solver.compute(A);
214*bf2c3715SXin Li * x = solver.adjoint().solve(b);
215*bf2c3715SXin Li * \endcode
216*bf2c3715SXin Li *
217*bf2c3715SXin Li * For real scalar types, this function is equivalent to transpose().
218*bf2c3715SXin Li *
219*bf2c3715SXin Li * \sa transpose(), solve()
220*bf2c3715SXin Li */
adjoint()221*bf2c3715SXin Li const SparseLUTransposeView<true, SparseLU<_MatrixType,_OrderingType> > adjoint()
222*bf2c3715SXin Li {
223*bf2c3715SXin Li SparseLUTransposeView<true, SparseLU<_MatrixType,_OrderingType> > adjointView;
224*bf2c3715SXin Li adjointView.setSparseLU(this);
225*bf2c3715SXin Li adjointView.setIsInitialized(this->m_isInitialized);
226*bf2c3715SXin Li return adjointView;
227*bf2c3715SXin Li }
228*bf2c3715SXin Li
rows()229*bf2c3715SXin Li inline Index rows() const { return m_mat.rows(); }
cols()230*bf2c3715SXin Li inline Index cols() const { return m_mat.cols(); }
231*bf2c3715SXin Li /** Indicate that the pattern of the input matrix is symmetric */
isSymmetric(bool sym)232*bf2c3715SXin Li void isSymmetric(bool sym)
233*bf2c3715SXin Li {
234*bf2c3715SXin Li m_symmetricmode = sym;
235*bf2c3715SXin Li }
236*bf2c3715SXin Li
237*bf2c3715SXin Li /** \returns an expression of the matrix L, internally stored as supernodes
238*bf2c3715SXin Li * The only operation available with this expression is the triangular solve
239*bf2c3715SXin Li * \code
240*bf2c3715SXin Li * y = b; matrixL().solveInPlace(y);
241*bf2c3715SXin Li * \endcode
242*bf2c3715SXin Li */
matrixL()243*bf2c3715SXin Li SparseLUMatrixLReturnType<SCMatrix> matrixL() const
244*bf2c3715SXin Li {
245*bf2c3715SXin Li return SparseLUMatrixLReturnType<SCMatrix>(m_Lstore);
246*bf2c3715SXin Li }
247*bf2c3715SXin Li /** \returns an expression of the matrix U,
248*bf2c3715SXin Li * The only operation available with this expression is the triangular solve
249*bf2c3715SXin Li * \code
250*bf2c3715SXin Li * y = b; matrixU().solveInPlace(y);
251*bf2c3715SXin Li * \endcode
252*bf2c3715SXin Li */
matrixU()253*bf2c3715SXin Li SparseLUMatrixUReturnType<SCMatrix,MappedSparseMatrix<Scalar,ColMajor,StorageIndex> > matrixU() const
254*bf2c3715SXin Li {
255*bf2c3715SXin Li return SparseLUMatrixUReturnType<SCMatrix, MappedSparseMatrix<Scalar,ColMajor,StorageIndex> >(m_Lstore, m_Ustore);
256*bf2c3715SXin Li }
257*bf2c3715SXin Li
258*bf2c3715SXin Li /**
259*bf2c3715SXin Li * \returns a reference to the row matrix permutation \f$ P_r \f$ such that \f$P_r A P_c^T = L U\f$
260*bf2c3715SXin Li * \sa colsPermutation()
261*bf2c3715SXin Li */
rowsPermutation()262*bf2c3715SXin Li inline const PermutationType& rowsPermutation() const
263*bf2c3715SXin Li {
264*bf2c3715SXin Li return m_perm_r;
265*bf2c3715SXin Li }
266*bf2c3715SXin Li /**
267*bf2c3715SXin Li * \returns a reference to the column matrix permutation\f$ P_c^T \f$ such that \f$P_r A P_c^T = L U\f$
268*bf2c3715SXin Li * \sa rowsPermutation()
269*bf2c3715SXin Li */
colsPermutation()270*bf2c3715SXin Li inline const PermutationType& colsPermutation() const
271*bf2c3715SXin Li {
272*bf2c3715SXin Li return m_perm_c;
273*bf2c3715SXin Li }
274*bf2c3715SXin Li /** Set the threshold used for a diagonal entry to be an acceptable pivot. */
setPivotThreshold(const RealScalar & thresh)275*bf2c3715SXin Li void setPivotThreshold(const RealScalar& thresh)
276*bf2c3715SXin Li {
277*bf2c3715SXin Li m_diagpivotthresh = thresh;
278*bf2c3715SXin Li }
279*bf2c3715SXin Li
280*bf2c3715SXin Li #ifdef EIGEN_PARSED_BY_DOXYGEN
281*bf2c3715SXin Li /** \returns the solution X of \f$ A X = B \f$ using the current decomposition of A.
282*bf2c3715SXin Li *
283*bf2c3715SXin Li * \warning the destination matrix X in X = this->solve(B) must be colmun-major.
284*bf2c3715SXin Li *
285*bf2c3715SXin Li * \sa compute()
286*bf2c3715SXin Li */
287*bf2c3715SXin Li template<typename Rhs>
288*bf2c3715SXin Li inline const Solve<SparseLU, Rhs> solve(const MatrixBase<Rhs>& B) const;
289*bf2c3715SXin Li #endif // EIGEN_PARSED_BY_DOXYGEN
290*bf2c3715SXin Li
291*bf2c3715SXin Li /** \brief Reports whether previous computation was successful.
292*bf2c3715SXin Li *
293*bf2c3715SXin Li * \returns \c Success if computation was successful,
294*bf2c3715SXin Li * \c NumericalIssue if the LU factorization reports a problem, zero diagonal for instance
295*bf2c3715SXin Li * \c InvalidInput if the input matrix is invalid
296*bf2c3715SXin Li *
297*bf2c3715SXin Li * \sa iparm()
298*bf2c3715SXin Li */
info()299*bf2c3715SXin Li ComputationInfo info() const
300*bf2c3715SXin Li {
301*bf2c3715SXin Li eigen_assert(m_isInitialized && "Decomposition is not initialized.");
302*bf2c3715SXin Li return m_info;
303*bf2c3715SXin Li }
304*bf2c3715SXin Li
305*bf2c3715SXin Li /**
306*bf2c3715SXin Li * \returns A string describing the type of error
307*bf2c3715SXin Li */
lastErrorMessage()308*bf2c3715SXin Li std::string lastErrorMessage() const
309*bf2c3715SXin Li {
310*bf2c3715SXin Li return m_lastError;
311*bf2c3715SXin Li }
312*bf2c3715SXin Li
313*bf2c3715SXin Li template<typename Rhs, typename Dest>
_solve_impl(const MatrixBase<Rhs> & B,MatrixBase<Dest> & X_base)314*bf2c3715SXin Li bool _solve_impl(const MatrixBase<Rhs> &B, MatrixBase<Dest> &X_base) const
315*bf2c3715SXin Li {
316*bf2c3715SXin Li Dest& X(X_base.derived());
317*bf2c3715SXin Li eigen_assert(m_factorizationIsOk && "The matrix should be factorized first");
318*bf2c3715SXin Li EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,
319*bf2c3715SXin Li THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
320*bf2c3715SXin Li
321*bf2c3715SXin Li // Permute the right hand side to form X = Pr*B
322*bf2c3715SXin Li // on return, X is overwritten by the computed solution
323*bf2c3715SXin Li X.resize(B.rows(),B.cols());
324*bf2c3715SXin Li
325*bf2c3715SXin Li // this ugly const_cast_derived() helps to detect aliasing when applying the permutations
326*bf2c3715SXin Li for(Index j = 0; j < B.cols(); ++j)
327*bf2c3715SXin Li X.col(j) = rowsPermutation() * B.const_cast_derived().col(j);
328*bf2c3715SXin Li
329*bf2c3715SXin Li //Forward substitution with L
330*bf2c3715SXin Li this->matrixL().solveInPlace(X);
331*bf2c3715SXin Li this->matrixU().solveInPlace(X);
332*bf2c3715SXin Li
333*bf2c3715SXin Li // Permute back the solution
334*bf2c3715SXin Li for (Index j = 0; j < B.cols(); ++j)
335*bf2c3715SXin Li X.col(j) = colsPermutation().inverse() * X.col(j);
336*bf2c3715SXin Li
337*bf2c3715SXin Li return true;
338*bf2c3715SXin Li }
339*bf2c3715SXin Li
340*bf2c3715SXin Li /**
341*bf2c3715SXin Li * \returns the absolute value of the determinant of the matrix of which
342*bf2c3715SXin Li * *this is the QR decomposition.
343*bf2c3715SXin Li *
344*bf2c3715SXin Li * \warning a determinant can be very big or small, so for matrices
345*bf2c3715SXin Li * of large enough dimension, there is a risk of overflow/underflow.
346*bf2c3715SXin Li * One way to work around that is to use logAbsDeterminant() instead.
347*bf2c3715SXin Li *
348*bf2c3715SXin Li * \sa logAbsDeterminant(), signDeterminant()
349*bf2c3715SXin Li */
absDeterminant()350*bf2c3715SXin Li Scalar absDeterminant()
351*bf2c3715SXin Li {
352*bf2c3715SXin Li using std::abs;
353*bf2c3715SXin Li eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
354*bf2c3715SXin Li // Initialize with the determinant of the row matrix
355*bf2c3715SXin Li Scalar det = Scalar(1.);
356*bf2c3715SXin Li // Note that the diagonal blocks of U are stored in supernodes,
357*bf2c3715SXin Li // which are available in the L part :)
358*bf2c3715SXin Li for (Index j = 0; j < this->cols(); ++j)
359*bf2c3715SXin Li {
360*bf2c3715SXin Li for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
361*bf2c3715SXin Li {
362*bf2c3715SXin Li if(it.index() == j)
363*bf2c3715SXin Li {
364*bf2c3715SXin Li det *= abs(it.value());
365*bf2c3715SXin Li break;
366*bf2c3715SXin Li }
367*bf2c3715SXin Li }
368*bf2c3715SXin Li }
369*bf2c3715SXin Li return det;
370*bf2c3715SXin Li }
371*bf2c3715SXin Li
372*bf2c3715SXin Li /** \returns the natural log of the absolute value of the determinant of the matrix
373*bf2c3715SXin Li * of which **this is the QR decomposition
374*bf2c3715SXin Li *
375*bf2c3715SXin Li * \note This method is useful to work around the risk of overflow/underflow that's
376*bf2c3715SXin Li * inherent to the determinant computation.
377*bf2c3715SXin Li *
378*bf2c3715SXin Li * \sa absDeterminant(), signDeterminant()
379*bf2c3715SXin Li */
logAbsDeterminant()380*bf2c3715SXin Li Scalar logAbsDeterminant() const
381*bf2c3715SXin Li {
382*bf2c3715SXin Li using std::log;
383*bf2c3715SXin Li using std::abs;
384*bf2c3715SXin Li
385*bf2c3715SXin Li eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
386*bf2c3715SXin Li Scalar det = Scalar(0.);
387*bf2c3715SXin Li for (Index j = 0; j < this->cols(); ++j)
388*bf2c3715SXin Li {
389*bf2c3715SXin Li for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
390*bf2c3715SXin Li {
391*bf2c3715SXin Li if(it.row() < j) continue;
392*bf2c3715SXin Li if(it.row() == j)
393*bf2c3715SXin Li {
394*bf2c3715SXin Li det += log(abs(it.value()));
395*bf2c3715SXin Li break;
396*bf2c3715SXin Li }
397*bf2c3715SXin Li }
398*bf2c3715SXin Li }
399*bf2c3715SXin Li return det;
400*bf2c3715SXin Li }
401*bf2c3715SXin Li
402*bf2c3715SXin Li /** \returns A number representing the sign of the determinant
403*bf2c3715SXin Li *
404*bf2c3715SXin Li * \sa absDeterminant(), logAbsDeterminant()
405*bf2c3715SXin Li */
signDeterminant()406*bf2c3715SXin Li Scalar signDeterminant()
407*bf2c3715SXin Li {
408*bf2c3715SXin Li eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
409*bf2c3715SXin Li // Initialize with the determinant of the row matrix
410*bf2c3715SXin Li Index det = 1;
411*bf2c3715SXin Li // Note that the diagonal blocks of U are stored in supernodes,
412*bf2c3715SXin Li // which are available in the L part :)
413*bf2c3715SXin Li for (Index j = 0; j < this->cols(); ++j)
414*bf2c3715SXin Li {
415*bf2c3715SXin Li for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
416*bf2c3715SXin Li {
417*bf2c3715SXin Li if(it.index() == j)
418*bf2c3715SXin Li {
419*bf2c3715SXin Li if(it.value()<0)
420*bf2c3715SXin Li det = -det;
421*bf2c3715SXin Li else if(it.value()==0)
422*bf2c3715SXin Li return 0;
423*bf2c3715SXin Li break;
424*bf2c3715SXin Li }
425*bf2c3715SXin Li }
426*bf2c3715SXin Li }
427*bf2c3715SXin Li return det * m_detPermR * m_detPermC;
428*bf2c3715SXin Li }
429*bf2c3715SXin Li
430*bf2c3715SXin Li /** \returns The determinant of the matrix.
431*bf2c3715SXin Li *
432*bf2c3715SXin Li * \sa absDeterminant(), logAbsDeterminant()
433*bf2c3715SXin Li */
determinant()434*bf2c3715SXin Li Scalar determinant()
435*bf2c3715SXin Li {
436*bf2c3715SXin Li eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
437*bf2c3715SXin Li // Initialize with the determinant of the row matrix
438*bf2c3715SXin Li Scalar det = Scalar(1.);
439*bf2c3715SXin Li // Note that the diagonal blocks of U are stored in supernodes,
440*bf2c3715SXin Li // which are available in the L part :)
441*bf2c3715SXin Li for (Index j = 0; j < this->cols(); ++j)
442*bf2c3715SXin Li {
443*bf2c3715SXin Li for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
444*bf2c3715SXin Li {
445*bf2c3715SXin Li if(it.index() == j)
446*bf2c3715SXin Li {
447*bf2c3715SXin Li det *= it.value();
448*bf2c3715SXin Li break;
449*bf2c3715SXin Li }
450*bf2c3715SXin Li }
451*bf2c3715SXin Li }
452*bf2c3715SXin Li return (m_detPermR * m_detPermC) > 0 ? det : -det;
453*bf2c3715SXin Li }
454*bf2c3715SXin Li
nnzL()455*bf2c3715SXin Li Index nnzL() const { return m_nnzL; };
nnzU()456*bf2c3715SXin Li Index nnzU() const { return m_nnzU; };
457*bf2c3715SXin Li
458*bf2c3715SXin Li protected:
459*bf2c3715SXin Li // Functions
initperfvalues()460*bf2c3715SXin Li void initperfvalues()
461*bf2c3715SXin Li {
462*bf2c3715SXin Li m_perfv.panel_size = 16;
463*bf2c3715SXin Li m_perfv.relax = 1;
464*bf2c3715SXin Li m_perfv.maxsuper = 128;
465*bf2c3715SXin Li m_perfv.rowblk = 16;
466*bf2c3715SXin Li m_perfv.colblk = 8;
467*bf2c3715SXin Li m_perfv.fillfactor = 20;
468*bf2c3715SXin Li }
469*bf2c3715SXin Li
470*bf2c3715SXin Li // Variables
471*bf2c3715SXin Li mutable ComputationInfo m_info;
472*bf2c3715SXin Li bool m_factorizationIsOk;
473*bf2c3715SXin Li bool m_analysisIsOk;
474*bf2c3715SXin Li std::string m_lastError;
475*bf2c3715SXin Li NCMatrix m_mat; // The input (permuted ) matrix
476*bf2c3715SXin Li SCMatrix m_Lstore; // The lower triangular matrix (supernodal)
477*bf2c3715SXin Li MappedSparseMatrix<Scalar,ColMajor,StorageIndex> m_Ustore; // The upper triangular matrix
478*bf2c3715SXin Li PermutationType m_perm_c; // Column permutation
479*bf2c3715SXin Li PermutationType m_perm_r ; // Row permutation
480*bf2c3715SXin Li IndexVector m_etree; // Column elimination tree
481*bf2c3715SXin Li
482*bf2c3715SXin Li typename Base::GlobalLU_t m_glu;
483*bf2c3715SXin Li
484*bf2c3715SXin Li // SparseLU options
485*bf2c3715SXin Li bool m_symmetricmode;
486*bf2c3715SXin Li // values for performance
487*bf2c3715SXin Li internal::perfvalues m_perfv;
488*bf2c3715SXin Li RealScalar m_diagpivotthresh; // Specifies the threshold used for a diagonal entry to be an acceptable pivot
489*bf2c3715SXin Li Index m_nnzL, m_nnzU; // Nonzeros in L and U factors
490*bf2c3715SXin Li Index m_detPermR, m_detPermC; // Determinants of the permutation matrices
491*bf2c3715SXin Li private:
492*bf2c3715SXin Li // Disable copy constructor
493*bf2c3715SXin Li SparseLU (const SparseLU& );
494*bf2c3715SXin Li }; // End class SparseLU
495*bf2c3715SXin Li
496*bf2c3715SXin Li
497*bf2c3715SXin Li
498*bf2c3715SXin Li // Functions needed by the anaysis phase
499*bf2c3715SXin Li /**
500*bf2c3715SXin Li * Compute the column permutation to minimize the fill-in
501*bf2c3715SXin Li *
502*bf2c3715SXin Li * - Apply this permutation to the input matrix -
503*bf2c3715SXin Li *
504*bf2c3715SXin Li * - Compute the column elimination tree on the permuted matrix
505*bf2c3715SXin Li *
506*bf2c3715SXin Li * - Postorder the elimination tree and the column permutation
507*bf2c3715SXin Li *
508*bf2c3715SXin Li */
509*bf2c3715SXin Li template <typename MatrixType, typename OrderingType>
analyzePattern(const MatrixType & mat)510*bf2c3715SXin Li void SparseLU<MatrixType, OrderingType>::analyzePattern(const MatrixType& mat)
511*bf2c3715SXin Li {
512*bf2c3715SXin Li
513*bf2c3715SXin Li //TODO It is possible as in SuperLU to compute row and columns scaling vectors to equilibrate the matrix mat.
514*bf2c3715SXin Li
515*bf2c3715SXin Li // Firstly, copy the whole input matrix.
516*bf2c3715SXin Li m_mat = mat;
517*bf2c3715SXin Li
518*bf2c3715SXin Li // Compute fill-in ordering
519*bf2c3715SXin Li OrderingType ord;
520*bf2c3715SXin Li ord(m_mat,m_perm_c);
521*bf2c3715SXin Li
522*bf2c3715SXin Li // Apply the permutation to the column of the input matrix
523*bf2c3715SXin Li if (m_perm_c.size())
524*bf2c3715SXin Li {
525*bf2c3715SXin Li m_mat.uncompress(); //NOTE: The effect of this command is only to create the InnerNonzeros pointers. FIXME : This vector is filled but not subsequently used.
526*bf2c3715SXin Li // Then, permute only the column pointers
527*bf2c3715SXin Li ei_declare_aligned_stack_constructed_variable(StorageIndex,outerIndexPtr,mat.cols()+1,mat.isCompressed()?const_cast<StorageIndex*>(mat.outerIndexPtr()):0);
528*bf2c3715SXin Li
529*bf2c3715SXin Li // If the input matrix 'mat' is uncompressed, then the outer-indices do not match the ones of m_mat, and a copy is thus needed.
530*bf2c3715SXin Li if(!mat.isCompressed())
531*bf2c3715SXin Li IndexVector::Map(outerIndexPtr, mat.cols()+1) = IndexVector::Map(m_mat.outerIndexPtr(),mat.cols()+1);
532*bf2c3715SXin Li
533*bf2c3715SXin Li // Apply the permutation and compute the nnz per column.
534*bf2c3715SXin Li for (Index i = 0; i < mat.cols(); i++)
535*bf2c3715SXin Li {
536*bf2c3715SXin Li m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
537*bf2c3715SXin Li m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
538*bf2c3715SXin Li }
539*bf2c3715SXin Li }
540*bf2c3715SXin Li
541*bf2c3715SXin Li // Compute the column elimination tree of the permuted matrix
542*bf2c3715SXin Li IndexVector firstRowElt;
543*bf2c3715SXin Li internal::coletree(m_mat, m_etree,firstRowElt);
544*bf2c3715SXin Li
545*bf2c3715SXin Li // In symmetric mode, do not do postorder here
546*bf2c3715SXin Li if (!m_symmetricmode) {
547*bf2c3715SXin Li IndexVector post, iwork;
548*bf2c3715SXin Li // Post order etree
549*bf2c3715SXin Li internal::treePostorder(StorageIndex(m_mat.cols()), m_etree, post);
550*bf2c3715SXin Li
551*bf2c3715SXin Li
552*bf2c3715SXin Li // Renumber etree in postorder
553*bf2c3715SXin Li Index m = m_mat.cols();
554*bf2c3715SXin Li iwork.resize(m+1);
555*bf2c3715SXin Li for (Index i = 0; i < m; ++i) iwork(post(i)) = post(m_etree(i));
556*bf2c3715SXin Li m_etree = iwork;
557*bf2c3715SXin Li
558*bf2c3715SXin Li // Postmultiply A*Pc by post, i.e reorder the matrix according to the postorder of the etree
559*bf2c3715SXin Li PermutationType post_perm(m);
560*bf2c3715SXin Li for (Index i = 0; i < m; i++)
561*bf2c3715SXin Li post_perm.indices()(i) = post(i);
562*bf2c3715SXin Li
563*bf2c3715SXin Li // Combine the two permutations : postorder the permutation for future use
564*bf2c3715SXin Li if(m_perm_c.size()) {
565*bf2c3715SXin Li m_perm_c = post_perm * m_perm_c;
566*bf2c3715SXin Li }
567*bf2c3715SXin Li
568*bf2c3715SXin Li } // end postordering
569*bf2c3715SXin Li
570*bf2c3715SXin Li m_analysisIsOk = true;
571*bf2c3715SXin Li }
572*bf2c3715SXin Li
573*bf2c3715SXin Li // Functions needed by the numerical factorization phase
574*bf2c3715SXin Li
575*bf2c3715SXin Li
576*bf2c3715SXin Li /**
577*bf2c3715SXin Li * - Numerical factorization
578*bf2c3715SXin Li * - Interleaved with the symbolic factorization
579*bf2c3715SXin Li * On exit, info is
580*bf2c3715SXin Li *
581*bf2c3715SXin Li * = 0: successful factorization
582*bf2c3715SXin Li *
583*bf2c3715SXin Li * > 0: if info = i, and i is
584*bf2c3715SXin Li *
585*bf2c3715SXin Li * <= A->ncol: U(i,i) is exactly zero. The factorization has
586*bf2c3715SXin Li * been completed, but the factor U is exactly singular,
587*bf2c3715SXin Li * and division by zero will occur if it is used to solve a
588*bf2c3715SXin Li * system of equations.
589*bf2c3715SXin Li *
590*bf2c3715SXin Li * > A->ncol: number of bytes allocated when memory allocation
591*bf2c3715SXin Li * failure occurred, plus A->ncol. If lwork = -1, it is
592*bf2c3715SXin Li * the estimated amount of space needed, plus A->ncol.
593*bf2c3715SXin Li */
594*bf2c3715SXin Li template <typename MatrixType, typename OrderingType>
factorize(const MatrixType & matrix)595*bf2c3715SXin Li void SparseLU<MatrixType, OrderingType>::factorize(const MatrixType& matrix)
596*bf2c3715SXin Li {
597*bf2c3715SXin Li using internal::emptyIdxLU;
598*bf2c3715SXin Li eigen_assert(m_analysisIsOk && "analyzePattern() should be called first");
599*bf2c3715SXin Li eigen_assert((matrix.rows() == matrix.cols()) && "Only for squared matrices");
600*bf2c3715SXin Li
601*bf2c3715SXin Li m_isInitialized = true;
602*bf2c3715SXin Li
603*bf2c3715SXin Li // Apply the column permutation computed in analyzepattern()
604*bf2c3715SXin Li // m_mat = matrix * m_perm_c.inverse();
605*bf2c3715SXin Li m_mat = matrix;
606*bf2c3715SXin Li if (m_perm_c.size())
607*bf2c3715SXin Li {
608*bf2c3715SXin Li m_mat.uncompress(); //NOTE: The effect of this command is only to create the InnerNonzeros pointers.
609*bf2c3715SXin Li //Then, permute only the column pointers
610*bf2c3715SXin Li const StorageIndex * outerIndexPtr;
611*bf2c3715SXin Li if (matrix.isCompressed()) outerIndexPtr = matrix.outerIndexPtr();
612*bf2c3715SXin Li else
613*bf2c3715SXin Li {
614*bf2c3715SXin Li StorageIndex* outerIndexPtr_t = new StorageIndex[matrix.cols()+1];
615*bf2c3715SXin Li for(Index i = 0; i <= matrix.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
616*bf2c3715SXin Li outerIndexPtr = outerIndexPtr_t;
617*bf2c3715SXin Li }
618*bf2c3715SXin Li for (Index i = 0; i < matrix.cols(); i++)
619*bf2c3715SXin Li {
620*bf2c3715SXin Li m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
621*bf2c3715SXin Li m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
622*bf2c3715SXin Li }
623*bf2c3715SXin Li if(!matrix.isCompressed()) delete[] outerIndexPtr;
624*bf2c3715SXin Li }
625*bf2c3715SXin Li else
626*bf2c3715SXin Li { //FIXME This should not be needed if the empty permutation is handled transparently
627*bf2c3715SXin Li m_perm_c.resize(matrix.cols());
628*bf2c3715SXin Li for(StorageIndex i = 0; i < matrix.cols(); ++i) m_perm_c.indices()(i) = i;
629*bf2c3715SXin Li }
630*bf2c3715SXin Li
631*bf2c3715SXin Li Index m = m_mat.rows();
632*bf2c3715SXin Li Index n = m_mat.cols();
633*bf2c3715SXin Li Index nnz = m_mat.nonZeros();
634*bf2c3715SXin Li Index maxpanel = m_perfv.panel_size * m;
635*bf2c3715SXin Li // Allocate working storage common to the factor routines
636*bf2c3715SXin Li Index lwork = 0;
637*bf2c3715SXin Li Index info = Base::memInit(m, n, nnz, lwork, m_perfv.fillfactor, m_perfv.panel_size, m_glu);
638*bf2c3715SXin Li if (info)
639*bf2c3715SXin Li {
640*bf2c3715SXin Li m_lastError = "UNABLE TO ALLOCATE WORKING MEMORY\n\n" ;
641*bf2c3715SXin Li m_factorizationIsOk = false;
642*bf2c3715SXin Li return ;
643*bf2c3715SXin Li }
644*bf2c3715SXin Li
645*bf2c3715SXin Li // Set up pointers for integer working arrays
646*bf2c3715SXin Li IndexVector segrep(m); segrep.setZero();
647*bf2c3715SXin Li IndexVector parent(m); parent.setZero();
648*bf2c3715SXin Li IndexVector xplore(m); xplore.setZero();
649*bf2c3715SXin Li IndexVector repfnz(maxpanel);
650*bf2c3715SXin Li IndexVector panel_lsub(maxpanel);
651*bf2c3715SXin Li IndexVector xprune(n); xprune.setZero();
652*bf2c3715SXin Li IndexVector marker(m*internal::LUNoMarker); marker.setZero();
653*bf2c3715SXin Li
654*bf2c3715SXin Li repfnz.setConstant(-1);
655*bf2c3715SXin Li panel_lsub.setConstant(-1);
656*bf2c3715SXin Li
657*bf2c3715SXin Li // Set up pointers for scalar working arrays
658*bf2c3715SXin Li ScalarVector dense;
659*bf2c3715SXin Li dense.setZero(maxpanel);
660*bf2c3715SXin Li ScalarVector tempv;
661*bf2c3715SXin Li tempv.setZero(internal::LUnumTempV(m, m_perfv.panel_size, m_perfv.maxsuper, /*m_perfv.rowblk*/m) );
662*bf2c3715SXin Li
663*bf2c3715SXin Li // Compute the inverse of perm_c
664*bf2c3715SXin Li PermutationType iperm_c(m_perm_c.inverse());
665*bf2c3715SXin Li
666*bf2c3715SXin Li // Identify initial relaxed snodes
667*bf2c3715SXin Li IndexVector relax_end(n);
668*bf2c3715SXin Li if ( m_symmetricmode == true )
669*bf2c3715SXin Li Base::heap_relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
670*bf2c3715SXin Li else
671*bf2c3715SXin Li Base::relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
672*bf2c3715SXin Li
673*bf2c3715SXin Li
674*bf2c3715SXin Li m_perm_r.resize(m);
675*bf2c3715SXin Li m_perm_r.indices().setConstant(-1);
676*bf2c3715SXin Li marker.setConstant(-1);
677*bf2c3715SXin Li m_detPermR = 1; // Record the determinant of the row permutation
678*bf2c3715SXin Li
679*bf2c3715SXin Li m_glu.supno(0) = emptyIdxLU; m_glu.xsup.setConstant(0);
680*bf2c3715SXin Li m_glu.xsup(0) = m_glu.xlsub(0) = m_glu.xusub(0) = m_glu.xlusup(0) = Index(0);
681*bf2c3715SXin Li
682*bf2c3715SXin Li // Work on one 'panel' at a time. A panel is one of the following :
683*bf2c3715SXin Li // (a) a relaxed supernode at the bottom of the etree, or
684*bf2c3715SXin Li // (b) panel_size contiguous columns, <panel_size> defined by the user
685*bf2c3715SXin Li Index jcol;
686*bf2c3715SXin Li Index pivrow; // Pivotal row number in the original row matrix
687*bf2c3715SXin Li Index nseg1; // Number of segments in U-column above panel row jcol
688*bf2c3715SXin Li Index nseg; // Number of segments in each U-column
689*bf2c3715SXin Li Index irep;
690*bf2c3715SXin Li Index i, k, jj;
691*bf2c3715SXin Li for (jcol = 0; jcol < n; )
692*bf2c3715SXin Li {
693*bf2c3715SXin Li // Adjust panel size so that a panel won't overlap with the next relaxed snode.
694*bf2c3715SXin Li Index panel_size = m_perfv.panel_size; // upper bound on panel width
695*bf2c3715SXin Li for (k = jcol + 1; k < (std::min)(jcol+panel_size, n); k++)
696*bf2c3715SXin Li {
697*bf2c3715SXin Li if (relax_end(k) != emptyIdxLU)
698*bf2c3715SXin Li {
699*bf2c3715SXin Li panel_size = k - jcol;
700*bf2c3715SXin Li break;
701*bf2c3715SXin Li }
702*bf2c3715SXin Li }
703*bf2c3715SXin Li if (k == n)
704*bf2c3715SXin Li panel_size = n - jcol;
705*bf2c3715SXin Li
706*bf2c3715SXin Li // Symbolic outer factorization on a panel of columns
707*bf2c3715SXin Li Base::panel_dfs(m, panel_size, jcol, m_mat, m_perm_r.indices(), nseg1, dense, panel_lsub, segrep, repfnz, xprune, marker, parent, xplore, m_glu);
708*bf2c3715SXin Li
709*bf2c3715SXin Li // Numeric sup-panel updates in topological order
710*bf2c3715SXin Li Base::panel_bmod(m, panel_size, jcol, nseg1, dense, tempv, segrep, repfnz, m_glu);
711*bf2c3715SXin Li
712*bf2c3715SXin Li // Sparse LU within the panel, and below the panel diagonal
713*bf2c3715SXin Li for ( jj = jcol; jj< jcol + panel_size; jj++)
714*bf2c3715SXin Li {
715*bf2c3715SXin Li k = (jj - jcol) * m; // Column index for w-wide arrays
716*bf2c3715SXin Li
717*bf2c3715SXin Li nseg = nseg1; // begin after all the panel segments
718*bf2c3715SXin Li //Depth-first-search for the current column
719*bf2c3715SXin Li VectorBlock<IndexVector> panel_lsubk(panel_lsub, k, m);
720*bf2c3715SXin Li VectorBlock<IndexVector> repfnz_k(repfnz, k, m);
721*bf2c3715SXin Li info = Base::column_dfs(m, jj, m_perm_r.indices(), m_perfv.maxsuper, nseg, panel_lsubk, segrep, repfnz_k, xprune, marker, parent, xplore, m_glu);
722*bf2c3715SXin Li if ( info )
723*bf2c3715SXin Li {
724*bf2c3715SXin Li m_lastError = "UNABLE TO EXPAND MEMORY IN COLUMN_DFS() ";
725*bf2c3715SXin Li m_info = NumericalIssue;
726*bf2c3715SXin Li m_factorizationIsOk = false;
727*bf2c3715SXin Li return;
728*bf2c3715SXin Li }
729*bf2c3715SXin Li // Numeric updates to this column
730*bf2c3715SXin Li VectorBlock<ScalarVector> dense_k(dense, k, m);
731*bf2c3715SXin Li VectorBlock<IndexVector> segrep_k(segrep, nseg1, m-nseg1);
732*bf2c3715SXin Li info = Base::column_bmod(jj, (nseg - nseg1), dense_k, tempv, segrep_k, repfnz_k, jcol, m_glu);
733*bf2c3715SXin Li if ( info )
734*bf2c3715SXin Li {
735*bf2c3715SXin Li m_lastError = "UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
736*bf2c3715SXin Li m_info = NumericalIssue;
737*bf2c3715SXin Li m_factorizationIsOk = false;
738*bf2c3715SXin Li return;
739*bf2c3715SXin Li }
740*bf2c3715SXin Li
741*bf2c3715SXin Li // Copy the U-segments to ucol(*)
742*bf2c3715SXin Li info = Base::copy_to_ucol(jj, nseg, segrep, repfnz_k ,m_perm_r.indices(), dense_k, m_glu);
743*bf2c3715SXin Li if ( info )
744*bf2c3715SXin Li {
745*bf2c3715SXin Li m_lastError = "UNABLE TO EXPAND MEMORY IN COPY_TO_UCOL() ";
746*bf2c3715SXin Li m_info = NumericalIssue;
747*bf2c3715SXin Li m_factorizationIsOk = false;
748*bf2c3715SXin Li return;
749*bf2c3715SXin Li }
750*bf2c3715SXin Li
751*bf2c3715SXin Li // Form the L-segment
752*bf2c3715SXin Li info = Base::pivotL(jj, m_diagpivotthresh, m_perm_r.indices(), iperm_c.indices(), pivrow, m_glu);
753*bf2c3715SXin Li if ( info )
754*bf2c3715SXin Li {
755*bf2c3715SXin Li m_lastError = "THE MATRIX IS STRUCTURALLY SINGULAR ... ZERO COLUMN AT ";
756*bf2c3715SXin Li std::ostringstream returnInfo;
757*bf2c3715SXin Li returnInfo << info;
758*bf2c3715SXin Li m_lastError += returnInfo.str();
759*bf2c3715SXin Li m_info = NumericalIssue;
760*bf2c3715SXin Li m_factorizationIsOk = false;
761*bf2c3715SXin Li return;
762*bf2c3715SXin Li }
763*bf2c3715SXin Li
764*bf2c3715SXin Li // Update the determinant of the row permutation matrix
765*bf2c3715SXin Li // FIXME: the following test is not correct, we should probably take iperm_c into account and pivrow is not directly the row pivot.
766*bf2c3715SXin Li if (pivrow != jj) m_detPermR = -m_detPermR;
767*bf2c3715SXin Li
768*bf2c3715SXin Li // Prune columns (0:jj-1) using column jj
769*bf2c3715SXin Li Base::pruneL(jj, m_perm_r.indices(), pivrow, nseg, segrep, repfnz_k, xprune, m_glu);
770*bf2c3715SXin Li
771*bf2c3715SXin Li // Reset repfnz for this column
772*bf2c3715SXin Li for (i = 0; i < nseg; i++)
773*bf2c3715SXin Li {
774*bf2c3715SXin Li irep = segrep(i);
775*bf2c3715SXin Li repfnz_k(irep) = emptyIdxLU;
776*bf2c3715SXin Li }
777*bf2c3715SXin Li } // end SparseLU within the panel
778*bf2c3715SXin Li jcol += panel_size; // Move to the next panel
779*bf2c3715SXin Li } // end for -- end elimination
780*bf2c3715SXin Li
781*bf2c3715SXin Li m_detPermR = m_perm_r.determinant();
782*bf2c3715SXin Li m_detPermC = m_perm_c.determinant();
783*bf2c3715SXin Li
784*bf2c3715SXin Li // Count the number of nonzeros in factors
785*bf2c3715SXin Li Base::countnz(n, m_nnzL, m_nnzU, m_glu);
786*bf2c3715SXin Li // Apply permutation to the L subscripts
787*bf2c3715SXin Li Base::fixupL(n, m_perm_r.indices(), m_glu);
788*bf2c3715SXin Li
789*bf2c3715SXin Li // Create supernode matrix L
790*bf2c3715SXin Li m_Lstore.setInfos(m, n, m_glu.lusup, m_glu.xlusup, m_glu.lsub, m_glu.xlsub, m_glu.supno, m_glu.xsup);
791*bf2c3715SXin Li // Create the column major upper sparse matrix U;
792*bf2c3715SXin Li new (&m_Ustore) MappedSparseMatrix<Scalar, ColMajor, StorageIndex> ( m, n, m_nnzU, m_glu.xusub.data(), m_glu.usub.data(), m_glu.ucol.data() );
793*bf2c3715SXin Li
794*bf2c3715SXin Li m_info = Success;
795*bf2c3715SXin Li m_factorizationIsOk = true;
796*bf2c3715SXin Li }
797*bf2c3715SXin Li
798*bf2c3715SXin Li template<typename MappedSupernodalType>
799*bf2c3715SXin Li struct SparseLUMatrixLReturnType : internal::no_assignment_operator
800*bf2c3715SXin Li {
801*bf2c3715SXin Li typedef typename MappedSupernodalType::Scalar Scalar;
SparseLUMatrixLReturnTypeSparseLUMatrixLReturnType802*bf2c3715SXin Li explicit SparseLUMatrixLReturnType(const MappedSupernodalType& mapL) : m_mapL(mapL)
803*bf2c3715SXin Li { }
rowsSparseLUMatrixLReturnType804*bf2c3715SXin Li Index rows() const { return m_mapL.rows(); }
colsSparseLUMatrixLReturnType805*bf2c3715SXin Li Index cols() const { return m_mapL.cols(); }
806*bf2c3715SXin Li template<typename Dest>
solveInPlaceSparseLUMatrixLReturnType807*bf2c3715SXin Li void solveInPlace( MatrixBase<Dest> &X) const
808*bf2c3715SXin Li {
809*bf2c3715SXin Li m_mapL.solveInPlace(X);
810*bf2c3715SXin Li }
811*bf2c3715SXin Li template<bool Conjugate, typename Dest>
solveTransposedInPlaceSparseLUMatrixLReturnType812*bf2c3715SXin Li void solveTransposedInPlace( MatrixBase<Dest> &X) const
813*bf2c3715SXin Li {
814*bf2c3715SXin Li m_mapL.template solveTransposedInPlace<Conjugate>(X);
815*bf2c3715SXin Li }
816*bf2c3715SXin Li
817*bf2c3715SXin Li const MappedSupernodalType& m_mapL;
818*bf2c3715SXin Li };
819*bf2c3715SXin Li
820*bf2c3715SXin Li template<typename MatrixLType, typename MatrixUType>
821*bf2c3715SXin Li struct SparseLUMatrixUReturnType : internal::no_assignment_operator
822*bf2c3715SXin Li {
823*bf2c3715SXin Li typedef typename MatrixLType::Scalar Scalar;
SparseLUMatrixUReturnTypeSparseLUMatrixUReturnType824*bf2c3715SXin Li SparseLUMatrixUReturnType(const MatrixLType& mapL, const MatrixUType& mapU)
825*bf2c3715SXin Li : m_mapL(mapL),m_mapU(mapU)
826*bf2c3715SXin Li { }
rowsSparseLUMatrixUReturnType827*bf2c3715SXin Li Index rows() const { return m_mapL.rows(); }
colsSparseLUMatrixUReturnType828*bf2c3715SXin Li Index cols() const { return m_mapL.cols(); }
829*bf2c3715SXin Li
solveInPlaceSparseLUMatrixUReturnType830*bf2c3715SXin Li template<typename Dest> void solveInPlace(MatrixBase<Dest> &X) const
831*bf2c3715SXin Li {
832*bf2c3715SXin Li Index nrhs = X.cols();
833*bf2c3715SXin Li Index n = X.rows();
834*bf2c3715SXin Li // Backward solve with U
835*bf2c3715SXin Li for (Index k = m_mapL.nsuper(); k >= 0; k--)
836*bf2c3715SXin Li {
837*bf2c3715SXin Li Index fsupc = m_mapL.supToCol()[k];
838*bf2c3715SXin Li Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc]; // leading dimension
839*bf2c3715SXin Li Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
840*bf2c3715SXin Li Index luptr = m_mapL.colIndexPtr()[fsupc];
841*bf2c3715SXin Li
842*bf2c3715SXin Li if (nsupc == 1)
843*bf2c3715SXin Li {
844*bf2c3715SXin Li for (Index j = 0; j < nrhs; j++)
845*bf2c3715SXin Li {
846*bf2c3715SXin Li X(fsupc, j) /= m_mapL.valuePtr()[luptr];
847*bf2c3715SXin Li }
848*bf2c3715SXin Li }
849*bf2c3715SXin Li else
850*bf2c3715SXin Li {
851*bf2c3715SXin Li // FIXME: the following lines should use Block expressions and not Map!
852*bf2c3715SXin Li Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
853*bf2c3715SXin Li Map< Matrix<Scalar,Dynamic,Dest::ColsAtCompileTime, ColMajor>, 0, OuterStride<> > U (&(X.coeffRef(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
854*bf2c3715SXin Li U = A.template triangularView<Upper>().solve(U);
855*bf2c3715SXin Li }
856*bf2c3715SXin Li
857*bf2c3715SXin Li for (Index j = 0; j < nrhs; ++j)
858*bf2c3715SXin Li {
859*bf2c3715SXin Li for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
860*bf2c3715SXin Li {
861*bf2c3715SXin Li typename MatrixUType::InnerIterator it(m_mapU, jcol);
862*bf2c3715SXin Li for ( ; it; ++it)
863*bf2c3715SXin Li {
864*bf2c3715SXin Li Index irow = it.index();
865*bf2c3715SXin Li X(irow, j) -= X(jcol, j) * it.value();
866*bf2c3715SXin Li }
867*bf2c3715SXin Li }
868*bf2c3715SXin Li }
869*bf2c3715SXin Li } // End For U-solve
870*bf2c3715SXin Li }
871*bf2c3715SXin Li
solveTransposedInPlaceSparseLUMatrixUReturnType872*bf2c3715SXin Li template<bool Conjugate, typename Dest> void solveTransposedInPlace(MatrixBase<Dest> &X) const
873*bf2c3715SXin Li {
874*bf2c3715SXin Li using numext::conj;
875*bf2c3715SXin Li Index nrhs = X.cols();
876*bf2c3715SXin Li Index n = X.rows();
877*bf2c3715SXin Li // Forward solve with U
878*bf2c3715SXin Li for (Index k = 0; k <= m_mapL.nsuper(); k++)
879*bf2c3715SXin Li {
880*bf2c3715SXin Li Index fsupc = m_mapL.supToCol()[k];
881*bf2c3715SXin Li Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc]; // leading dimension
882*bf2c3715SXin Li Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
883*bf2c3715SXin Li Index luptr = m_mapL.colIndexPtr()[fsupc];
884*bf2c3715SXin Li
885*bf2c3715SXin Li for (Index j = 0; j < nrhs; ++j)
886*bf2c3715SXin Li {
887*bf2c3715SXin Li for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
888*bf2c3715SXin Li {
889*bf2c3715SXin Li typename MatrixUType::InnerIterator it(m_mapU, jcol);
890*bf2c3715SXin Li for ( ; it; ++it)
891*bf2c3715SXin Li {
892*bf2c3715SXin Li Index irow = it.index();
893*bf2c3715SXin Li X(jcol, j) -= X(irow, j) * (Conjugate? conj(it.value()): it.value());
894*bf2c3715SXin Li }
895*bf2c3715SXin Li }
896*bf2c3715SXin Li }
897*bf2c3715SXin Li if (nsupc == 1)
898*bf2c3715SXin Li {
899*bf2c3715SXin Li for (Index j = 0; j < nrhs; j++)
900*bf2c3715SXin Li {
901*bf2c3715SXin Li X(fsupc, j) /= (Conjugate? conj(m_mapL.valuePtr()[luptr]) : m_mapL.valuePtr()[luptr]);
902*bf2c3715SXin Li }
903*bf2c3715SXin Li }
904*bf2c3715SXin Li else
905*bf2c3715SXin Li {
906*bf2c3715SXin Li Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
907*bf2c3715SXin Li Map< Matrix<Scalar,Dynamic,Dest::ColsAtCompileTime, ColMajor>, 0, OuterStride<> > U (&(X(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
908*bf2c3715SXin Li if(Conjugate)
909*bf2c3715SXin Li U = A.adjoint().template triangularView<Lower>().solve(U);
910*bf2c3715SXin Li else
911*bf2c3715SXin Li U = A.transpose().template triangularView<Lower>().solve(U);
912*bf2c3715SXin Li }
913*bf2c3715SXin Li }// End For U-solve
914*bf2c3715SXin Li }
915*bf2c3715SXin Li
916*bf2c3715SXin Li
917*bf2c3715SXin Li const MatrixLType& m_mapL;
918*bf2c3715SXin Li const MatrixUType& m_mapU;
919*bf2c3715SXin Li };
920*bf2c3715SXin Li
921*bf2c3715SXin Li } // End namespace Eigen
922*bf2c3715SXin Li
923*bf2c3715SXin Li #endif
924