// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2011 Gael Guennebaud // // 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 . #include "sparse.h" #include template void cg(int size) { double density = (std::max)(8./(size*size), 0.01); typedef Matrix DenseMatrix; typedef Matrix DenseVector; typedef SparseMatrix SparseMatrixType; SparseMatrixType m2(size,size); DenseMatrix refMat2(size,size); DenseVector b = DenseVector::Random(size); DenseVector ref_x(size), x(size); initSparse(density, refMat2, m2, ForceNonZeroDiag, 0, 0); // for(int i=0; i().rankUpdate(m2,0); m3_up.template selfadjointView().rankUpdate(m2,0); ref_x = refMat3.template selfadjointView().llt().solve(b); x = ConjugateGradient().compute(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, full storage, lower"); x.setRandom(); x = ConjugateGradient().compute(m3).solveWithGuess(b,x); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solveWithGuess, full storage, lower"); x = ConjugateGradient().compute(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, full storage, upper, single dense rhs"); x = ConjugateGradient(m3_lo).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, lower only, single dense rhs"); x = ConjugateGradient(m3_up).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, upper only, single dense rhs"); x = ConjugateGradient().compute(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, full storage, lower"); x = ConjugateGradient().compute(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, full storage, upper, single dense rhs"); x = ConjugateGradient(m3_lo).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, lower only, single dense rhs"); x = ConjugateGradient(m3_up).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "ConjugateGradient: solve, upper only, single dense rhs"); ref_x = refMat2.lu().solve(b); x = BiCGSTAB(m2).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "BiCGSTAB: solve, I, single dense rhs"); x = BiCGSTAB(m2).solve(b); VERIFY(ref_x.isApprox(x,test_precision()) && "BiCGSTAB: solve, diag, single dense rhs"); } void test_cg() { for(int i = 0; i < g_repeat; i++) { CALL_SUBTEST_1( (cg(8)) ); CALL_SUBTEST_1( (cg(8)) ); CALL_SUBTEST_2( (cg,int>(internal::random(1,300))) ); CALL_SUBTEST_1( (cg(internal::random(1,300))) ); } }