This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Iterative solve doesn't work

Tags: None
(comma "," separated)
dchen
Registered Member
Posts
13
Karma
0

Iterative solve doesn't work

Wed Aug 05, 2015 4:39 pm
Hi:

I am using the iterative solver for dense matrix. I have a matrix A with type MatrixXd, and a vector b with type VectorXd. I simply type

x = ConjugateGradient<MatrixXd>(A).solve(b);

It just returns a vector of all entires -1.#IND. When I use the direct solver, I would get the correct result. Could anyone tell what is the problem here? Thank you!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Iterative solve doesn't work

Wed Aug 05, 2015 5:19 pm
Is A symmetric positive definite?
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Wed Aug 05, 2015 6:14 pm
Yes. I also tried BICGSTAB, has the same issue. Hence I don't the matrix A cause the problem.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Iterative solve doesn't work

Wed Aug 05, 2015 7:39 pm
Which eigen version? What about A.diagonal().cwiseAbs().minCoeff() ?
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Wed Aug 05, 2015 7:48 pm
I believe I am using the latest version Eigen 3.2.5, and A.diagonal().cwiseAbs.minCoeff() works well.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Iterative solve doesn't work

Wed Aug 05, 2015 7:51 pm
What does is returns ? something really positive or close to zero??

Could you try with the devel branch? (http://bitbucket.org/eigen/eigen/get/default.zip)
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Wed Aug 05, 2015 7:52 pm
It returns 0.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Iterative solve doesn't work

Wed Aug 05, 2015 7:53 pm
ok, that's the problem. I see that a fix of the devel branch did not go to the 3.2 branch. So it should definitely work with the devel branch. I'll backport the fix for the next 3.2.6 release.
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Wed Aug 05, 2015 7:54 pm
Thank you!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Iterative solve doesn't work

Wed Aug 05, 2015 8:03 pm
Backport done: https://bitbucket.org/eigen/eigen/commits/b32d157299b3/
If you don't feel too adventurous better use the 3.2 branch: https://bitbucket.org/eigen/eigen/get/3.2.zip
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Wed Aug 05, 2015 8:23 pm
I tried this version, still got the invalid return. What is the problem here? Did I do something wrong here, since I didn't see other people ask this question. Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Iterative solve doesn't work

Thu Aug 06, 2015 7:34 am
could you share your matrix and right hand side vector?
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Thu Aug 06, 2015 2:46 pm
I am calculating the boundary integral equation in a circle, for discretization, I am using the corrected trapezoidal rule. Now I restrict my number of nodes to 10, and here is my matrix:

A =
0 -0.287157 -0.245947 1.29213 -1.36476 0.757617 -1.36476 1.29213
-0.245947 -0.287157
-0.287157 0 -0.287157 -0.245947 1.29213 -1.36476 0.757617 -1.36476
1.29213 -0.245947
-0.245947 -0.287157 0 -0.287157 -0.245947 1.29213 -1.36476 0.757617
-1.36476 1.29213
1.29213 -0.245947 -0.287157 0 -0.287157 -0.245947 1.29213 -1.36476
0.757617 -1.36476
-1.36476 1.29213 -0.245947 -0.287157 0 -0.287157 -0.245947 1.29213
-1.36476 0.757617
0.757617 -1.36476 1.29213 -0.245947 -0.287157 0 -0.287157 -0.245947
1.29213 -1.36476
-1.36476 0.757617 -1.36476 1.29213 -0.245947 -0.287157 0 -0.287157
-0.245947 1.29213
1.29213 -1.36476 0.757617 -1.36476 1.29213 -0.245947 -0.287157 0
-0.287157 -0.245947
-0.245947 1.29213 -1.36476 0.757617 -1.36476 1.29213 -0.245947 -0.287157
0 -0.287157
-0.287157 -0.245947 1.29213 -1.36476 0.757617 -1.36476 1.29213 -0.245947
-0.287157 0

b =
-0.330006
-0.357292
-0.119652
0.0582414
0.163355
0.213277
0.216202
0.172588
0.0753447
-0.0920579

I can also attach code if it helps. Thank you.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Works for me (see below). Make sure you are using the latest 3.2 branch or, since the diagonal is zero, you can also use the IdentityPreconditioner.

btw, what is the motivation for using a conjugate gradient on such problem? It is small and dense, so better use LDLT that will be much faster.

Code: Select all
#include <Eigen/Sparse>
#include <Eigen/Dense>
#include <iostream>

using namespace std;
using namespace Eigen;

int main() {
    int N = 10;
    Eigen::MatrixXd A(N,N);
    VectorXd b(N);
    A <<
      0,-0.287157,-0.245947,1.29213,-1.36476,0.757617,-1.36476,1.29213,-0.245947,-0.287157,
      -0.287157,0,-0.287157,-0.245947,1.29213,-1.36476,0.757617,-1.36476,1.29213,-0.245947,
      -0.245947,-0.287157,0,-0.287157,-0.245947,1.29213,-1.36476,0.757617,-1.36476,1.29213,
      1.29213,-0.245947,-0.287157,0,-0.287157,-0.245947,1.29213,-1.36476,0.757617,-1.36476,
      -1.36476,1.29213,-0.245947,-0.287157,0,-0.287157,-0.245947,1.29213,-1.36476,0.757617,
      0.757617,-1.36476,1.29213,-0.245947,-0.287157,0,-0.287157,-0.245947,1.29213,-1.36476,
      -1.36476,0.757617,-1.36476,1.29213,-0.245947,-0.287157,0,-0.287157,-0.245947,1.29213,
      1.29213,-1.36476,0.757617,-1.36476,1.29213,-0.245947,-0.287157,0,-0.287157,-0.245947,
      -0.245947,1.29213,-1.36476,0.757617,-1.36476,1.29213,-0.245947,-0.287157,0,-0.287157,
      -0.287157,-0.245947,1.29213,-1.36476,0.757617,-1.36476,1.29213,-0.245947,-0.287157,0;

    b << -0.330006,-0.357292,-0.119652,0.0582414,0.163355,0.213277,0.216202,0.172588,0.0753447,-0.0920579;
   
    VectorXd x = VectorXd::Zero(N);
   
    std::cout << "Eigenvalues: " << SelfAdjointEigenSolver<MatrixXd>(A).eigenvalues().transpose() << "\n";

    {
      ConjugateGradient< MatrixXd, Upper|Lower > cg;
      cg.compute(A);
      x = cg.solve(b);
      std::cout << x.transpose() << " -> " << (A*x-b).norm() / b.norm() << " in " << cg.iterations() << " iterations" << std::endl;
    }
   
    {
      ConjugateGradient< MatrixXd, Upper|Lower,  IdentityPreconditioner> cg;
      cg.compute(A);
      x = cg.solve(b);
      std::cout << x.transpose() << " -> " << (A*x-b).norm() / b.norm() << " in " << cg.iterations() << " iterations" << std::endl;
    }   
}
dchen
Registered Member
Posts
13
Karma
0

Re: Iterative solve doesn't work

Thu Aug 06, 2015 4:06 pm
I use the Eigen 3.2.5. I copy your code, and I find out that if I add IdentityPreconditioner, then it works. But if I don't add IdentityPreconditioner, I still get the invalid answer.

For the later question, the real problem can be huge, I set the N to 10 is just for debugging.

Thank you!


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]