Sparse: fix long int as index type in simplicial cholesky and other decompositions

This commit is contained in:
Gael Guennebaud 2011-06-06 10:17:28 +02:00
parent 7a61a564ef
commit 421ece38e1
7 changed files with 68 additions and 63 deletions

View File

@ -116,21 +116,21 @@ template<typename MatrixType, unsigned int UpLo> class SparseSelfAdjointView
SparseSelfAdjointView& rankUpdate(const SparseMatrixBase<DerivedU>& u, Scalar alpha = Scalar(1)); SparseSelfAdjointView& rankUpdate(const SparseMatrixBase<DerivedU>& u, Scalar alpha = Scalar(1));
/** \internal triggered by sparse_matrix = SparseSelfadjointView; */ /** \internal triggered by sparse_matrix = SparseSelfadjointView; */
template<typename DestScalar> void evalTo(SparseMatrix<DestScalar>& _dest) const template<typename DestScalar> void evalTo(SparseMatrix<DestScalar,ColMajor,Index>& _dest) const
{ {
internal::permute_symm_to_fullsymm<UpLo>(m_matrix, _dest); internal::permute_symm_to_fullsymm<UpLo>(m_matrix, _dest);
} }
template<typename DestScalar> void evalTo(DynamicSparseMatrix<DestScalar>& _dest) const template<typename DestScalar> void evalTo(DynamicSparseMatrix<DestScalar,ColMajor,Index>& _dest) const
{ {
// TODO directly evaluate into _dest; // TODO directly evaluate into _dest;
SparseMatrix<DestScalar> tmp(_dest.rows(),_dest.cols()); SparseMatrix<DestScalar,ColMajor,Index> tmp(_dest.rows(),_dest.cols());
internal::permute_symm_to_fullsymm<UpLo>(m_matrix, tmp); internal::permute_symm_to_fullsymm<UpLo>(m_matrix, tmp);
_dest = tmp; _dest = tmp;
} }
/** \returns an expression of P^-1 H P */ /** \returns an expression of P^-1 H P */
SparseSymmetricPermutationProduct<_MatrixTypeNested,UpLo> twistedBy(const PermutationMatrix<Dynamic>& perm) const SparseSymmetricPermutationProduct<_MatrixTypeNested,UpLo> twistedBy(const PermutationMatrix<Dynamic,Dynamic,Index>& perm) const
{ {
return SparseSymmetricPermutationProduct<_MatrixTypeNested,UpLo>(m_matrix, perm); return SparseSymmetricPermutationProduct<_MatrixTypeNested,UpLo>(m_matrix, perm);
} }
@ -419,10 +419,12 @@ template<typename MatrixType,int UpLo>
class SparseSymmetricPermutationProduct class SparseSymmetricPermutationProduct
: public EigenBase<SparseSymmetricPermutationProduct<MatrixType,UpLo> > : public EigenBase<SparseSymmetricPermutationProduct<MatrixType,UpLo> >
{ {
typedef PermutationMatrix<Dynamic> Perm;
public: public:
typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::Scalar Scalar;
typedef typename MatrixType::Index Index; typedef typename MatrixType::Index Index;
protected:
typedef PermutationMatrix<Dynamic,Dynamic,Index> Perm;
public:
typedef Matrix<Index,Dynamic,1> VectorI; typedef Matrix<Index,Dynamic,1> VectorI;
typedef typename MatrixType::Nested MatrixTypeNested; typedef typename MatrixType::Nested MatrixTypeNested;
typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested; typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;

View File

@ -58,15 +58,15 @@ enum {
* \param zeroCoords and nonzeroCoords allows to get the coordinate lists of the non zero, * \param zeroCoords and nonzeroCoords allows to get the coordinate lists of the non zero,
* and zero coefficients respectively. * and zero coefficients respectively.
*/ */
template<typename Scalar,int Opt1,int Opt2> void template<typename Scalar,int Opt1,int Opt2,typename Index> void
initSparse(double density, initSparse(double density,
Matrix<Scalar,Dynamic,Dynamic,Opt1>& refMat, Matrix<Scalar,Dynamic,Dynamic,Opt1>& refMat,
SparseMatrix<Scalar,Opt2>& sparseMat, SparseMatrix<Scalar,Opt2,Index>& sparseMat,
int flags = 0, int flags = 0,
std::vector<Vector2i>* zeroCoords = 0, std::vector<Vector2i>* zeroCoords = 0,
std::vector<Vector2i>* nonzeroCoords = 0) std::vector<Vector2i>* nonzeroCoords = 0)
{ {
enum { IsRowMajor = SparseMatrix<Scalar,Opt2>::IsRowMajor }; enum { IsRowMajor = SparseMatrix<Scalar,Opt2,Index>::IsRowMajor };
sparseMat.setZero(); sparseMat.setZero();
sparseMat.reserve(int(refMat.rows()*refMat.cols()*density)); sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
@ -108,15 +108,15 @@ initSparse(double density,
sparseMat.finalize(); sparseMat.finalize();
} }
template<typename Scalar,int Opt1,int Opt2> void template<typename Scalar,int Opt1,int Opt2,typename Index> void
initSparse(double density, initSparse(double density,
Matrix<Scalar,Dynamic,Dynamic, Opt1>& refMat, Matrix<Scalar,Dynamic,Dynamic, Opt1>& refMat,
DynamicSparseMatrix<Scalar, Opt2>& sparseMat, DynamicSparseMatrix<Scalar, Opt2, Index>& sparseMat,
int flags = 0, int flags = 0,
std::vector<Vector2i>* zeroCoords = 0, std::vector<Vector2i>* zeroCoords = 0,
std::vector<Vector2i>* nonzeroCoords = 0) std::vector<Vector2i>* nonzeroCoords = 0)
{ {
enum { IsRowMajor = DynamicSparseMatrix<Scalar,Opt2>::IsRowMajor }; enum { IsRowMajor = DynamicSparseMatrix<Scalar,Opt2,Index>::IsRowMajor };
sparseMat.setZero(); sparseMat.setZero();
sparseMat.reserve(int(refMat.rows()*refMat.cols()*density)); sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
for(int j=0; j<sparseMat.outerSize(); j++) for(int j=0; j<sparseMat.outerSize(); j++)

View File

@ -103,7 +103,7 @@ Index cs_tdfs(Index j, Index k, Index *head, const Index *next, Index *post, Ind
* The input matrix \a C must be a selfadjoint compressed column major SparseMatrix object. Both the upper and lower parts have to be stored, but the diagonal entries are optional. * The input matrix \a C must be a selfadjoint compressed column major SparseMatrix object. Both the upper and lower parts have to be stored, but the diagonal entries are optional.
* On exit the values of C are destroyed */ * On exit the values of C are destroyed */
template<typename Scalar, typename Index> template<typename Scalar, typename Index>
void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, PermutationMatrix<Dynamic>& perm) void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, PermutationMatrix<Dynamic,Dynamic,Index>& perm)
{ {
typedef SparseMatrix<Scalar,ColMajor,Index> CCS; typedef SparseMatrix<Scalar,ColMajor,Index> CCS;
@ -151,7 +151,7 @@ void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, Permutation
elen[i] = 0; // Ek of node i is empty elen[i] = 0; // Ek of node i is empty
degree[i] = len[i]; // degree of node i degree[i] = len[i]; // degree of node i
} }
mark = cs_wclear (0, 0, w, n); /* clear w */ mark = cs_wclear<Index>(0, 0, w, n); /* clear w */
elen[n] = -2; /* n is a dead element */ elen[n] = -2; /* n is a dead element */
Cp[n] = -1; /* n is a root of assembly tree */ Cp[n] = -1; /* n is a root of assembly tree */
w[n] = 0; /* n is a dead element */ w[n] = 0; /* n is a dead element */
@ -266,7 +266,7 @@ void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, Permutation
elen[k] = -2; /* k is now an element */ elen[k] = -2; /* k is now an element */
/* --- Find set differences ----------------------------------------- */ /* --- Find set differences ----------------------------------------- */
mark = cs_wclear (mark, lemax, w, n); /* clear w if necessary */ mark = cs_wclear<Index>(mark, lemax, w, n); /* clear w if necessary */
for(pk = pk1; pk < pk2; pk++) /* scan 1: find |Le\Lk| */ for(pk = pk1; pk < pk2; pk++) /* scan 1: find |Le\Lk| */
{ {
i = Ci[pk]; i = Ci[pk];
@ -349,7 +349,7 @@ void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, Permutation
} /* scan2 is done */ } /* scan2 is done */
degree[k] = dk; /* finalize |Lk| */ degree[k] = dk; /* finalize |Lk| */
lemax = std::max<Index>(lemax, dk); lemax = std::max<Index>(lemax, dk);
mark = cs_wclear (mark+lemax, lemax, w, n); /* clear w */ mark = cs_wclear<Index>(mark+lemax, lemax, w, n); /* clear w */
/* --- Supernode detection ------------------------------------------ */ /* --- Supernode detection ------------------------------------------ */
for(pk = pk1; pk < pk2; pk++) for(pk = pk1; pk < pk2; pk++)
@ -435,7 +435,7 @@ void minimum_degree_ordering(SparseMatrix<Scalar,ColMajor,Index>& C, Permutation
} }
for(k = 0, i = 0; i <= n; i++) /* postorder the assembly tree */ for(k = 0, i = 0; i <= n; i++) /* postorder the assembly tree */
{ {
if(Cp[i] == -1) k = cs_tdfs (i, k, head, next, perm.indices().data(), w); if(Cp[i] == -1) k = cs_tdfs<Index>(i, k, head, next, perm.indices().data(), w);
} }
perm.indices().conservativeResize(n); perm.indices().conservativeResize(n);

View File

@ -193,12 +193,12 @@ class SimplicialCholesky
/** \returns the permutation P /** \returns the permutation P
* \sa permutationPinv() */ * \sa permutationPinv() */
const PermutationMatrix<Dynamic>& permutationP() const const PermutationMatrix<Dynamic,Dynamic,Index>& permutationP() const
{ return m_P; } { return m_P; }
/** \returns the inverse P^-1 of the permutation P /** \returns the inverse P^-1 of the permutation P
* \sa permutationP() */ * \sa permutationP() */
const PermutationMatrix<Dynamic>& permutationPinv() const const PermutationMatrix<Dynamic,Dynamic,Index>& permutationPinv() const
{ return m_Pinv; } { return m_Pinv; }
#ifndef EIGEN_PARSED_BY_DOXYGEN #ifndef EIGEN_PARSED_BY_DOXYGEN
@ -282,8 +282,8 @@ class SimplicialCholesky
VectorType m_diag; // the diagonal coefficients in case of a LDLt decomposition VectorType m_diag; // the diagonal coefficients in case of a LDLt decomposition
VectorXi m_parent; // elimination tree VectorXi m_parent; // elimination tree
VectorXi m_nonZerosPerCol; VectorXi m_nonZerosPerCol;
PermutationMatrix<Dynamic> m_P; // the permutation PermutationMatrix<Dynamic,Dynamic,Index> m_P; // the permutation
PermutationMatrix<Dynamic> m_Pinv; // the inverse permutation PermutationMatrix<Dynamic,Dynamic,Index> m_Pinv; // the inverse permutation
}; };
template<typename _MatrixType, int _UpLo> template<typename _MatrixType, int _UpLo>

View File

@ -90,10 +90,9 @@ class SparseLDLT
}; };
public: public:
typedef SparseMatrix<Scalar> CholMatrixType;
typedef _MatrixType MatrixType; typedef _MatrixType MatrixType;
typedef typename MatrixType::Index Index; typedef typename MatrixType::Index Index;
typedef SparseMatrix<Scalar,ColMajor,Index> CholMatrixType;
/** Creates a dummy LDLT factorization object with flags \a flags. */ /** Creates a dummy LDLT factorization object with flags \a flags. */
SparseLDLT(int flags = 0) SparseLDLT(int flags = 0)
@ -187,8 +186,8 @@ class SparseLDLT
VectorXi m_parent; // elimination tree VectorXi m_parent; // elimination tree
VectorXi m_nonZerosPerCol; VectorXi m_nonZerosPerCol;
// VectorXi m_w; // workspace // VectorXi m_w; // workspace
PermutationMatrix<Dynamic> m_P; PermutationMatrix<Dynamic,Dynamic,Index> m_P;
PermutationMatrix<Dynamic> m_Pinv; PermutationMatrix<Dynamic,Dynamic,Index> m_Pinv;
RealScalar m_precision; RealScalar m_precision;
int m_flags; int m_flags;
mutable int m_status; mutable int m_status;
@ -257,7 +256,7 @@ void SparseLDLT<_MatrixType,Backend>::_symbolic(const _MatrixType& a)
if(P) if(P)
{ {
m_P.indices() = VectorXi::Map(P,size); m_P.indices() = Map<const Matrix<Index,Dynamic,1> >(P,size);
m_Pinv = m_P.inverse(); m_Pinv = m_P.inverse();
Pinv = m_Pinv.indices().data(); Pinv = m_Pinv.indices().data();
} }

View File

@ -29,15 +29,16 @@
#include <Eigen/CholmodSupport> #include <Eigen/CholmodSupport>
#endif #endif
template<typename Scalar> void sparse_ldlt(int rows, int cols) template<typename Scalar,typename Index> void sparse_ldlt(int rows, int cols)
{ {
static bool odd = true; static bool odd = true;
odd = !odd; odd = !odd;
double density = std::max(8./(rows*cols), 0.01); double density = std::max(8./(rows*cols), 0.01);
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
typedef Matrix<Scalar,Dynamic,1> DenseVector; typedef Matrix<Scalar,Dynamic,1> DenseVector;
typedef SparseMatrix<Scalar,ColMajor,Index> SparseMatrixType;
SparseMatrix<Scalar> m2(rows, cols); SparseMatrixType m2(rows, cols);
DenseMatrix refMat2(rows, cols); DenseMatrix refMat2(rows, cols);
DenseVector b = DenseVector::Random(cols); DenseVector b = DenseVector::Random(cols);
@ -45,11 +46,11 @@ template<typename Scalar> void sparse_ldlt(int rows, int cols)
initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeUpperTriangular, 0, 0); initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeUpperTriangular, 0, 0);
SparseMatrix<Scalar> m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows); SparseMatrixType m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows);
DenseMatrix refMat3 = refMat2 * refMat2.adjoint(); DenseMatrix refMat3 = refMat2 * refMat2.adjoint();
refX = refMat3.template selfadjointView<Upper>().ldlt().solve(b); refX = refMat3.template selfadjointView<Upper>().ldlt().solve(b);
typedef SparseMatrix<Scalar,Upper|SelfAdjoint> SparseSelfAdjointMatrix; typedef SparseMatrix<Scalar,Upper|SelfAdjoint,Index> SparseSelfAdjointMatrix;
x = b; x = b;
SparseLDLT<SparseSelfAdjointMatrix> ldlt(m3); SparseLDLT<SparseSelfAdjointMatrix> ldlt(m3);
if (ldlt.succeeded()) if (ldlt.succeeded())
@ -84,7 +85,7 @@ template<typename Scalar> void sparse_ldlt(int rows, int cols)
// new API // new API
{ {
SparseMatrix<Scalar> m2(rows, cols); SparseMatrixType m2(rows, cols);
DenseMatrix refMat2(rows, cols); DenseMatrix refMat2(rows, cols);
DenseVector b = DenseVector::Random(cols); DenseVector b = DenseVector::Random(cols);
@ -98,7 +99,7 @@ template<typename Scalar> void sparse_ldlt(int rows, int cols)
m2.coeffRef(i,i) = refMat2(i,i) = internal::abs(internal::real(refMat2(i,i))); m2.coeffRef(i,i) = refMat2(i,i) = internal::abs(internal::real(refMat2(i,i)));
SparseMatrix<Scalar> m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows); SparseMatrixType m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows);
DenseMatrix refMat3 = refMat2 * refMat2.adjoint(); DenseMatrix refMat3 = refMat2 * refMat2.adjoint();
m3_lo.template selfadjointView<Lower>().rankUpdate(m2,0); m3_lo.template selfadjointView<Lower>().rankUpdate(m2,0);
@ -107,40 +108,40 @@ template<typename Scalar> void sparse_ldlt(int rows, int cols)
// with a single vector as the rhs // with a single vector as the rhs
ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b); ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b);
x = SimplicialCholesky<SparseMatrix<Scalar>, Lower>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(b); x = SimplicialCholesky<SparseMatrixType, Lower>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, lower, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, lower, single dense rhs");
x = SimplicialCholesky<SparseMatrix<Scalar>, Upper>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(b); x = SimplicialCholesky<SparseMatrixType, Upper>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, upper, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, upper, single dense rhs");
x = SimplicialCholesky<SparseMatrix<Scalar>, Lower>(m3_lo).solve(b); x = SimplicialCholesky<SparseMatrixType, Lower>(m3_lo).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, lower only, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, lower only, single dense rhs");
x = SimplicialCholesky<SparseMatrix<Scalar>, Upper>(m3_up).solve(b); x = SimplicialCholesky<SparseMatrixType, Upper>(m3_up).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, upper only, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, upper only, single dense rhs");
// with multiple rhs // with multiple rhs
ref_X = refMat3.template selfadjointView<Lower>().llt().solve(B); ref_X = refMat3.template selfadjointView<Lower>().llt().solve(B);
X = SimplicialCholesky<SparseMatrix<Scalar>, Lower>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(B); X = SimplicialCholesky<SparseMatrixType, Lower>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(B);
VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, lower, multiple dense rhs"); VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, lower, multiple dense rhs");
X = SimplicialCholesky<SparseMatrix<Scalar>, Upper>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(B); X = SimplicialCholesky<SparseMatrixType, Upper>().setMode(odd ? SimplicialCholeskyLLt : SimplicialCholeskyLDLt).compute(m3).solve(B);
VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, upper, multiple dense rhs"); VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "SimplicialCholesky: solve, full storage, upper, multiple dense rhs");
// with a sparse rhs // with a sparse rhs
// SparseMatrix<Scalar> spB(rows,cols), spX(rows,cols); // SparseMatrixType spB(rows,cols), spX(rows,cols);
// B.diagonal().array() += 1; // B.diagonal().array() += 1;
// spB = B.sparseView(0.5,1); // spB = B.sparseView(0.5,1);
// //
// ref_X = refMat3.template selfadjointView<Lower>().llt().solve(DenseMatrix(spB)); // ref_X = refMat3.template selfadjointView<Lower>().llt().solve(DenseMatrix(spB));
// //
// spX = SimplicialCholesky<SparseMatrix<Scalar>, Lower>(m3).solve(spB); // spX = SimplicialCholesky<SparseMatrixType, Lower>(m3).solve(spB);
// VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs"); // VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs");
// //
// spX = SimplicialCholesky<SparseMatrix<Scalar>, Upper>(m3).solve(spB); // spX = SimplicialCholesky<SparseMatrixType, Upper>(m3).solve(spB);
// VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs"); // VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs");
} }
@ -167,9 +168,10 @@ template<typename Scalar> void sparse_ldlt(int rows, int cols)
void test_sparse_ldlt() void test_sparse_ldlt()
{ {
for(int i = 0; i < g_repeat; i++) { for(int i = 0; i < g_repeat; i++) {
CALL_SUBTEST_1(sparse_ldlt<double>(8, 8) ); CALL_SUBTEST_1( (sparse_ldlt<double,int>(8, 8)) );
CALL_SUBTEST_1( (sparse_ldlt<double,long int>(8, 8)) );
int s = internal::random<int>(1,300); int s = internal::random<int>(1,300);
CALL_SUBTEST_2(sparse_ldlt<std::complex<double> >(s,s) ); CALL_SUBTEST_2( (sparse_ldlt<std::complex<double>,int>(s,s)) );
CALL_SUBTEST_1(sparse_ldlt<double>(s,s) ); CALL_SUBTEST_1( (sparse_ldlt<double,int>(s,s)) );
} }
} }

View File

@ -29,14 +29,15 @@
#include <Eigen/CholmodSupport> #include <Eigen/CholmodSupport>
#endif #endif
template<typename Scalar> void sparse_llt(int rows, int cols) template<typename Scalar,typename Index> void sparse_llt(int rows, int cols)
{ {
double density = std::max(8./(rows*cols), 0.01); double density = std::max(8./(rows*cols), 0.01);
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
typedef Matrix<Scalar,Dynamic,1> DenseVector; typedef Matrix<Scalar,Dynamic,1> DenseVector;
typedef SparseMatrix<Scalar,ColMajor,Index> SparseMatrixType;
// TODO fix the issue with complex (see SparseLLT::solveInPlace) // TODO fix the issue with complex (see SparseLLT::solveInPlace)
SparseMatrix<Scalar> m2(rows, cols); SparseMatrixType m2(rows, cols);
DenseMatrix refMat2(rows, cols); DenseMatrix refMat2(rows, cols);
DenseVector b = DenseVector::Random(cols); DenseVector b = DenseVector::Random(cols);
@ -53,7 +54,7 @@ template<typename Scalar> void sparse_llt(int rows, int cols)
if (!NumTraits<Scalar>::IsComplex) if (!NumTraits<Scalar>::IsComplex)
{ {
x = b; x = b;
SparseLLT<SparseMatrix<Scalar> > (m2).solveInPlace(x); SparseLLT<SparseMatrixType > (m2).solveInPlace(x);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: default"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: default");
} }
@ -61,23 +62,23 @@ template<typename Scalar> void sparse_llt(int rows, int cols)
// legacy API // legacy API
{ {
// Cholmod, as configured in CholmodSupport.h, only supports self-adjoint matrices // Cholmod, as configured in CholmodSupport.h, only supports self-adjoint matrices
SparseMatrix<Scalar> m3 = m2.adjoint()*m2; SparseMatrixType m3 = m2.adjoint()*m2;
DenseMatrix refMat3 = refMat2.adjoint()*refMat2; DenseMatrix refMat3 = refMat2.adjoint()*refMat2;
ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b); ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b);
x = b; x = b;
SparseLLT<SparseMatrix<Scalar>, Cholmod>(m3).solveInPlace(x); SparseLLT<SparseMatrixType, Cholmod>(m3).solveInPlace(x);
VERIFY((m3*x).isApprox(b,test_precision<Scalar>()) && "LLT legacy: cholmod solveInPlace"); VERIFY((m3*x).isApprox(b,test_precision<Scalar>()) && "LLT legacy: cholmod solveInPlace");
x = SparseLLT<SparseMatrix<Scalar>, Cholmod>(m3).solve(b); x = SparseLLT<SparseMatrixType, Cholmod>(m3).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT legacy: cholmod solve"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT legacy: cholmod solve");
} }
// new API // new API
{ {
// Cholmod, as configured in CholmodSupport.h, only supports self-adjoint matrices // Cholmod, as configured in CholmodSupport.h, only supports self-adjoint matrices
SparseMatrix<Scalar> m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows); SparseMatrixType m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows);
DenseMatrix refMat3 = refMat2 * refMat2.adjoint(); DenseMatrix refMat3 = refMat2 * refMat2.adjoint();
m3_lo.template selfadjointView<Lower>().rankUpdate(m2,0); m3_lo.template selfadjointView<Lower>().rankUpdate(m2,0);
@ -86,16 +87,16 @@ template<typename Scalar> void sparse_llt(int rows, int cols)
// with a single vector as the rhs // with a single vector as the rhs
ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b); ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b);
x = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3).solve(b); x = CholmodDecomposition<SparseMatrixType, Lower>(m3).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs");
x = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3).solve(b); x = CholmodDecomposition<SparseMatrixType, Upper>(m3).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs");
x = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3_lo).solve(b); x = CholmodDecomposition<SparseMatrixType, Lower>(m3_lo).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs");
x = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3_up).solve(b); x = CholmodDecomposition<SparseMatrixType, Upper>(m3_up).solve(b);
VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs");
@ -104,25 +105,25 @@ template<typename Scalar> void sparse_llt(int rows, int cols)
#ifndef EIGEN_DEFAULT_TO_ROW_MAJOR #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
// TODO make sure the API is properly documented about this fact // TODO make sure the API is properly documented about this fact
X = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3).solve(B); X = CholmodDecomposition<SparseMatrixType, Lower>(m3).solve(B);
VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "LLT: cholmod solve, multiple dense rhs"); VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "LLT: cholmod solve, multiple dense rhs");
X = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3).solve(B); X = CholmodDecomposition<SparseMatrixType, Upper>(m3).solve(B);
VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "LLT: cholmod solve, multiple dense rhs"); VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "LLT: cholmod solve, multiple dense rhs");
#endif #endif
// with a sparse rhs // with a sparse rhs
SparseMatrix<Scalar> spB(rows,cols), spX(rows,cols); SparseMatrixType spB(rows,cols), spX(rows,cols);
B.diagonal().array() += 1; B.diagonal().array() += 1;
spB = B.sparseView(0.5,1); spB = B.sparseView(0.5,1);
ref_X = refMat3.template selfadjointView<Lower>().llt().solve(DenseMatrix(spB)); ref_X = refMat3.template selfadjointView<Lower>().llt().solve(DenseMatrix(spB));
spX = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3).solve(spB); spX = CholmodDecomposition<SparseMatrixType, Lower>(m3).solve(spB);
VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs"); VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs");
spX = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3).solve(spB); spX = CholmodDecomposition<SparseMatrixType, Upper>(m3).solve(spB);
VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs"); VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs");
} }
#endif #endif
@ -132,9 +133,10 @@ template<typename Scalar> void sparse_llt(int rows, int cols)
void test_sparse_llt() void test_sparse_llt()
{ {
for(int i = 0; i < g_repeat; i++) { for(int i = 0; i < g_repeat; i++) {
CALL_SUBTEST_1(sparse_llt<double>(8, 8) ); CALL_SUBTEST_1( (sparse_llt<double,int>(8, 8)) );
int s = internal::random<int>(1,300); int s = internal::random<int>(1,300);
CALL_SUBTEST_2(sparse_llt<std::complex<double> >(s,s) ); CALL_SUBTEST_2( (sparse_llt<std::complex<double>,int>(s,s)) );
CALL_SUBTEST_1(sparse_llt<double>(s,s) ); CALL_SUBTEST_1( (sparse_llt<double,int>(s,s)) );
CALL_SUBTEST_1( (sparse_llt<double,long int>(s,s)) );
} }
} }