diff options
author | 2011-07-26 09:04:10 +0200 | |
---|---|---|
committer | 2011-07-26 09:04:10 +0200 | |
commit | 80b1d1371db8c2d5da21b3570bc866655263894e (patch) | |
tree | a966d95e9fe17516b6696279557bcac1b9c6593d /unsupported/test | |
parent | 8fa7e92e77f550efb6d67635f19dfd7672d3259c (diff) |
add a conjugate gradient solver
Diffstat (limited to 'unsupported/test')
-rw-r--r-- | unsupported/test/CMakeLists.txt | 1 | ||||
-rw-r--r-- | unsupported/test/cg.cpp | 74 |
2 files changed, 75 insertions, 0 deletions
diff --git a/unsupported/test/CMakeLists.txt b/unsupported/test/CMakeLists.txt index e0df35177..a14b57cb9 100644 --- a/unsupported/test/CMakeLists.txt +++ b/unsupported/test/CMakeLists.txt @@ -139,3 +139,4 @@ endif(GSL_FOUND) ei_add_test(polynomialsolver " " "${GSL_LIBRARIES}" ) ei_add_test(polynomialutils) ei_add_test(kronecker_product) +ei_add_test(cg) diff --git a/unsupported/test/cg.cpp b/unsupported/test/cg.cpp new file mode 100644 index 000000000..aede84d58 --- /dev/null +++ b/unsupported/test/cg.cpp @@ -0,0 +1,74 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.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" +#include <Eigen/IterativeSolvers> + +template<typename Scalar,typename Index> void cg(int size) +{ + double density = std::max(8./(size*size), 0.01); + typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; + typedef Matrix<Scalar,Dynamic,1> DenseVector; + typedef SparseMatrix<Scalar,ColMajor,Index> SparseMatrixType; + + SparseMatrixType m2(size,size); + DenseMatrix refMat2(size,size); + + DenseVector b = DenseVector::Random(size); + DenseVector ref_x(size), x(size); + + initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeLowerTriangular, 0, 0); +// for(int i=0; i<rows; ++i) +// m2.coeffRef(i,i) = refMat2(i,i) = internal::abs(internal::real(refMat2(i,i))); + + SparseMatrixType m3 = m2 * m2.adjoint(), m3_lo(size,size), m3_up(size,size); + DenseMatrix refMat3 = refMat2 * refMat2.adjoint(); + + m3_lo.template selfadjointView<Lower>().rankUpdate(m2,0); + m3_up.template selfadjointView<Upper>().rankUpdate(m2,0); + + ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b); + + x = ConjugateGradient<SparseMatrixType, Lower>().compute(m3).solve(b); + VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "ConjugateGradient: solve, full storage, lower"); + + x = ConjugateGradient<SparseMatrixType, Upper>().compute(m3).solve(b); + VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "ConjugateGradient: solve, full storage, upper, single dense rhs"); + + x = ConjugateGradient<SparseMatrixType, Lower>(m3_lo).solve(b); + VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, lower only, single dense rhs"); + + x = ConjugateGradient<SparseMatrixType, Upper>(m3_up).solve(b); + VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "SimplicialCholesky: solve, upper only, single dense rhs"); +} + +void test_cg() +{ + for(int i = 0; i < g_repeat; i++) { + CALL_SUBTEST_1( (cg<double,int>(8)) ); + CALL_SUBTEST_1( (cg<double,long int>(8)) ); + CALL_SUBTEST_2( (cg<std::complex<double>,int>(internal::random<int>(1,300))) ); + CALL_SUBTEST_1( (cg<double,int>(internal::random<int>(1,300))) ); + } +} |