JuliaLinearAlgebra / IterativeSolvers.jl

Iterative algorithms for solving linear systems, eigensystems, and singular value problems
MIT License
406 stars 106 forks source link

cg returns incorrect result? #112

Closed memoiry closed 7 years ago

memoiry commented 7 years ago

Hi all

I'm trying to use this package, and below is my experiments

using IterativeSolvers
julia> A = rand(10,10);

julia> b = rand(10);

julia> x = cg(A,b)[1];

julia> A*x-b
10-element Array{Float64,1}:
 -28.5586
 -86.5707
 -55.2936
 -85.9302
 -63.0426
 -26.0243
 -86.1318
 -26.6518
 -69.1788
  27.4126

Is there anything wrong? the result is a little confusing, it should be nearly zero I think. any explanation will be helpful, thanks!

KristofferC commented 7 years ago

What are the requirements on A for the conjugate gradient method to converge?

ChrisRackauckas commented 7 years ago

What are the requirements on A for the conjugate gradient method to converge?

Symmetric positive definite. That should probably be added to the docs. For random, you'll want something more general like gmres.

A = rand(10,10)
b = rand(10)
using IterativeSolvers
x = gmres(A,b)
A*x - b
KristofferC commented 7 years ago

... I didn't ask the question because I wondered about the answer. It was for the OP.

memoiry commented 7 years ago

@KristofferC @ChrisRackauckas Thanks for the kindly explanation.