2011-10-11 17:32:26 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
// for linear algebra.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2011 Gael Guennebaud <g.gael@free.fr>
|
|
|
|
//
|
|
|
|
// Eigen is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 3 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Alternatively, you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License and a copy of the GNU General Public License along with
|
|
|
|
// Eigen. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "sparse.h"
|
2011-11-12 21:11:27 +08:00
|
|
|
#include <Eigen/SparseCore>
|
2011-10-11 17:32:26 +08:00
|
|
|
|
|
|
|
template<typename Solver, typename Rhs, typename DenseMat, typename DenseRhs>
|
|
|
|
void check_sparse_solving(Solver& solver, const typename Solver::MatrixType& A, const Rhs& b, const DenseMat& dA, const DenseRhs& db)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
|
|
|
|
DenseRhs refX = dA.lu().solve(db);
|
|
|
|
|
|
|
|
Rhs x(b.rows(), b.cols());
|
|
|
|
Rhs oldb = b;
|
|
|
|
|
|
|
|
solver.compute(A);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
2011-11-12 21:11:27 +08:00
|
|
|
std::cerr << "sparse solver testing: factorization failed (check_sparse_solving)\n";
|
2011-10-11 17:32:26 +08:00
|
|
|
exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = solver.solve(b);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
2012-02-27 20:22:38 +08:00
|
|
|
std::cerr << "sparse solver testing: solving failed\n";
|
2011-10-11 17:32:26 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-02-27 20:22:38 +08:00
|
|
|
VERIFY(oldb.isApprox(b) && "sparse solver testing: the rhs should not be modified!");
|
|
|
|
|
|
|
|
VERIFY(x.isApprox(refX,test_precision<Scalar>()));
|
|
|
|
|
|
|
|
x.setZero();
|
|
|
|
// test the analyze/factorize API
|
|
|
|
solver.analyzePattern(A);
|
|
|
|
solver.factorize(A);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
|
|
|
std::cerr << "sparse solver testing: factorization failed (check_sparse_solving)\n";
|
|
|
|
exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = solver.solve(b);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
|
|
|
std::cerr << "sparse solver testing: solving failed\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VERIFY(oldb.isApprox(b) && "sparse solver testing: the rhs should not be modified!");
|
2011-10-11 17:32:26 +08:00
|
|
|
|
|
|
|
VERIFY(x.isApprox(refX,test_precision<Scalar>()));
|
|
|
|
}
|
|
|
|
|
2012-03-29 20:32:54 +08:00
|
|
|
template<typename Solver, typename Rhs>
|
|
|
|
void check_sparse_solving_real_cases(Solver& solver, const typename Solver::MatrixType& A, const Rhs& b, const Rhs& refX)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef typename Mat::RealScalar RealScalar;
|
|
|
|
|
|
|
|
Rhs x(b.rows(), b.cols());
|
|
|
|
|
|
|
|
solver.compute(A);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
|
|
|
std::cerr << "sparse solver testing: factorization failed (check_sparse_solving_real_cases)\n";
|
|
|
|
exit(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = solver.solve(b);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
|
|
|
std::cerr << "sparse solver testing: solving failed\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RealScalar res_error;
|
|
|
|
// Compute the norm of the relative error
|
|
|
|
if(refX.size() != 0)
|
|
|
|
res_error = (refX - x).norm()/refX.norm();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Compute the relative residual norm
|
|
|
|
res_error = (b - A * x).norm()/b.norm();
|
|
|
|
}
|
|
|
|
if (res_error > test_precision<Scalar>() ){
|
|
|
|
std::cerr << "Test " << g_test_stack.back() << " failed in "EI_PP_MAKE_STRING(__FILE__)
|
|
|
|
<< " (" << EI_PP_MAKE_STRING(__LINE__) << ")" << std::endl << std::endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-10-11 17:32:26 +08:00
|
|
|
template<typename Solver, typename DenseMat>
|
|
|
|
void check_sparse_determinant(Solver& solver, const typename Solver::MatrixType& A, const DenseMat& dA)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef typename Mat::RealScalar RealScalar;
|
|
|
|
|
|
|
|
solver.compute(A);
|
|
|
|
if (solver.info() != Success)
|
|
|
|
{
|
2011-11-12 21:11:27 +08:00
|
|
|
std::cerr << "sparse solver testing: factorization failed (check_sparse_determinant)\n";
|
2011-10-11 17:32:26 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Scalar refDet = dA.determinant();
|
|
|
|
VERIFY_IS_APPROX(refDet,solver.determinant());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename Solver, typename DenseMat>
|
|
|
|
int generate_sparse_spd_problem(Solver& , typename Solver::MatrixType& A, typename Solver::MatrixType& halfA, DenseMat& dA, int maxSize = 300)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
|
|
|
|
int size = internal::random<int>(1,maxSize);
|
|
|
|
double density = (std::max)(8./(size*size), 0.01);
|
|
|
|
|
|
|
|
Mat M(size, size);
|
|
|
|
DenseMatrix dM(size, size);
|
|
|
|
|
|
|
|
initSparse<Scalar>(density, dM, M, ForceNonZeroDiag);
|
|
|
|
|
|
|
|
A = M * M.adjoint();
|
|
|
|
dA = dM * dM.adjoint();
|
|
|
|
|
|
|
|
halfA.resize(size,size);
|
|
|
|
halfA.template selfadjointView<Solver::UpLo>().rankUpdate(M);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2012-03-29 20:53:42 +08:00
|
|
|
|
|
|
|
#ifdef TEST_REAL_CASES
|
|
|
|
template<typename Scalar>
|
|
|
|
inline std::string get_matrixfolder()
|
|
|
|
{
|
|
|
|
std::string mat_folder = TEST_REAL_CASES;
|
|
|
|
if( internal::is_same<Scalar, std::complex<float> >::value || internal::is_same<Scalar, std::complex<double> >::value )
|
|
|
|
mat_folder = mat_folder + static_cast<string>("/complex/");
|
|
|
|
else
|
|
|
|
mat_folder = mat_folder + static_cast<string>("/real/");
|
|
|
|
return mat_folder;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 17:32:26 +08:00
|
|
|
template<typename Solver> void check_sparse_spd_solving(Solver& solver)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
2012-03-29 20:32:54 +08:00
|
|
|
typedef typename Mat::Index Index;
|
2012-02-04 21:20:56 +08:00
|
|
|
typedef SparseMatrix<Scalar,ColMajor> SpMat;
|
2011-10-11 17:32:26 +08:00
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
typedef Matrix<Scalar,Dynamic,1> DenseVector;
|
|
|
|
|
|
|
|
// generate the problem
|
|
|
|
Mat A, halfA;
|
|
|
|
DenseMatrix dA;
|
|
|
|
int size = generate_sparse_spd_problem(solver, A, halfA, dA);
|
|
|
|
|
|
|
|
// generate the right hand sides
|
|
|
|
int rhsCols = internal::random<int>(1,16);
|
|
|
|
double density = (std::max)(8./(size*rhsCols), 0.1);
|
2012-02-04 21:20:56 +08:00
|
|
|
SpMat B(size,rhsCols);
|
2011-10-11 17:32:26 +08:00
|
|
|
DenseVector b = DenseVector::Random(size);
|
|
|
|
DenseMatrix dB(size,rhsCols);
|
|
|
|
initSparse<Scalar>(density, dB, B);
|
2012-03-29 20:32:54 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < g_repeat; i++) {
|
|
|
|
check_sparse_solving(solver, A, b, dA, b);
|
|
|
|
check_sparse_solving(solver, halfA, b, dA, b);
|
|
|
|
check_sparse_solving(solver, A, dB, dA, dB);
|
|
|
|
check_sparse_solving(solver, halfA, dB, dA, dB);
|
|
|
|
check_sparse_solving(solver, A, B, dA, dB);
|
|
|
|
check_sparse_solving(solver, halfA, B, dA, dB);
|
|
|
|
}
|
2011-10-11 17:32:26 +08:00
|
|
|
|
2012-03-29 20:32:54 +08:00
|
|
|
// First, get the folder
|
2012-03-29 20:53:42 +08:00
|
|
|
#ifdef TEST_REAL_CASES
|
2012-03-29 20:32:54 +08:00
|
|
|
if (internal::is_same<Scalar, float>::value
|
|
|
|
|| internal::is_same<Scalar, std::complex<float> >::value)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
std::string mat_folder = get_matrixfolder<Scalar>();
|
|
|
|
MatrixMarketIterator<Scalar> it(mat_folder);
|
|
|
|
for (; it; ++it)
|
|
|
|
{
|
|
|
|
if (it.sym() == SPD){
|
|
|
|
Mat halfA;
|
|
|
|
PermutationMatrix<Dynamic, Dynamic, Index> pnull;
|
|
|
|
halfA.template selfadjointView<Solver::UpLo>() = it.matrix().template triangularView<Eigen::Lower>().twistedBy(pnull);
|
|
|
|
|
|
|
|
std::cout<< " ==== SOLVING WITH MATRIX " << it.matname() << " ==== \n";
|
|
|
|
check_sparse_solving_real_cases(solver, it.matrix(), it.rhs(), it.refX());
|
|
|
|
check_sparse_solving_real_cases(solver, halfA, it.rhs(), it.refX());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-10-11 17:32:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Solver> void check_sparse_spd_determinant(Solver& solver)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
|
|
|
|
// generate the problem
|
|
|
|
Mat A, halfA;
|
|
|
|
DenseMatrix dA;
|
|
|
|
generate_sparse_spd_problem(solver, A, halfA, dA, 30);
|
2012-03-29 20:32:54 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < g_repeat; i++) {
|
|
|
|
check_sparse_determinant(solver, A, dA);
|
|
|
|
check_sparse_determinant(solver, halfA, dA );
|
|
|
|
}
|
2011-10-11 17:32:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Solver, typename DenseMat>
|
|
|
|
int generate_sparse_square_problem(Solver&, typename Solver::MatrixType& A, DenseMat& dA, int maxSize = 300)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
|
|
|
|
int size = internal::random<int>(1,maxSize);
|
|
|
|
double density = (std::max)(8./(size*size), 0.01);
|
|
|
|
|
|
|
|
A.resize(size,size);
|
|
|
|
dA.resize(size,size);
|
|
|
|
|
|
|
|
initSparse<Scalar>(density, dA, A, ForceNonZeroDiag);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Solver> void check_sparse_square_solving(Solver& solver)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
typedef Matrix<Scalar,Dynamic,1> DenseVector;
|
|
|
|
|
|
|
|
int rhsCols = internal::random<int>(1,16);
|
|
|
|
|
|
|
|
Mat A;
|
|
|
|
DenseMatrix dA;
|
|
|
|
int size = generate_sparse_square_problem(solver, A, dA);
|
|
|
|
|
|
|
|
DenseVector b = DenseVector::Random(size);
|
|
|
|
DenseMatrix dB = DenseMatrix::Random(size,rhsCols);
|
2012-03-29 20:32:54 +08:00
|
|
|
A.makeCompressed();
|
|
|
|
for (int i = 0; i < g_repeat; i++) {
|
|
|
|
check_sparse_solving(solver, A, b, dA, b);
|
|
|
|
check_sparse_solving(solver, A, dB, dA, dB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, get the folder
|
2012-03-29 20:53:42 +08:00
|
|
|
#ifdef TEST_REAL_CASES
|
2012-03-29 20:32:54 +08:00
|
|
|
if (internal::is_same<Scalar, float>::value
|
|
|
|
|| internal::is_same<Scalar, std::complex<float> >::value)
|
|
|
|
return ;
|
|
|
|
|
|
|
|
std::string mat_folder = get_matrixfolder<Scalar>();
|
|
|
|
MatrixMarketIterator<Scalar> it(mat_folder);
|
|
|
|
for (; it; ++it)
|
|
|
|
{
|
|
|
|
std::cout<< " ==== SOLVING WITH MATRIX " << it.matname() << " ==== \n";
|
|
|
|
check_sparse_solving_real_cases(solver, it.matrix(), it.rhs(), it.refX());
|
|
|
|
}
|
|
|
|
#endif
|
2011-10-11 17:32:26 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Solver> void check_sparse_square_determinant(Solver& solver)
|
|
|
|
{
|
|
|
|
typedef typename Solver::MatrixType Mat;
|
|
|
|
typedef typename Mat::Scalar Scalar;
|
|
|
|
typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
|
|
|
|
|
|
|
|
// generate the problem
|
|
|
|
Mat A;
|
|
|
|
DenseMatrix dA;
|
|
|
|
generate_sparse_square_problem(solver, A, dA, 30);
|
2012-03-29 20:32:54 +08:00
|
|
|
A.makeCompressed();
|
|
|
|
for (int i = 0; i < g_repeat; i++) {
|
|
|
|
check_sparse_determinant(solver, A, dA);
|
|
|
|
}
|
2011-10-11 17:32:26 +08:00
|
|
|
}
|