1 | import exceptions |
---|
2 | class VectorShapeError(exceptions.Exception): pass |
---|
3 | |
---|
4 | |
---|
5 | def conjugate_gradient(A,b,x0=None,imax=10000,tol=1.0e-8,iprint=0): |
---|
6 | """ |
---|
7 | Try to solve linear equation Ax = b using |
---|
8 | conjugate gradient method |
---|
9 | |
---|
10 | Input |
---|
11 | A: matrix or function which applies a matrix, assumed symmetric |
---|
12 | A can be either dense or sparse |
---|
13 | b: right hand side |
---|
14 | x0: inital guess (default the 0 vector) |
---|
15 | imax: max number of iterations |
---|
16 | tol: tolerance used for residual |
---|
17 | |
---|
18 | Output |
---|
19 | x: approximate solution |
---|
20 | """ |
---|
21 | |
---|
22 | from Numeric import dot, array, Float, zeros |
---|
23 | |
---|
24 | b = array(b, typecode=Float) |
---|
25 | if len(b.shape) != 1 : |
---|
26 | raise VectorShapeError, 'input vector should consist of only one column' |
---|
27 | |
---|
28 | if x0 is None: |
---|
29 | x0 = zeros(b.shape, typecode=Float) |
---|
30 | else: |
---|
31 | x0 = array(x0, typecode=Float) |
---|
32 | |
---|
33 | |
---|
34 | #FIXME: Should test using None |
---|
35 | if iprint == 0: |
---|
36 | iprint = imax |
---|
37 | |
---|
38 | i=1 |
---|
39 | x = x0 |
---|
40 | r = b - A*x |
---|
41 | d = r |
---|
42 | rTr = dot(r,r) |
---|
43 | rTr0 = rTr |
---|
44 | |
---|
45 | while (i<imax and rTr>tol**2*rTr0): |
---|
46 | q = A*d |
---|
47 | alpha = rTr/dot(d,q) |
---|
48 | x = x + alpha*d |
---|
49 | if i%50 : |
---|
50 | r = b - A*x |
---|
51 | else: |
---|
52 | r = r - alpha*q |
---|
53 | rTrOld = rTr |
---|
54 | rTr = dot(r,r) |
---|
55 | bt = rTr/rTrOld |
---|
56 | |
---|
57 | d = r + bt*d |
---|
58 | i = i+1 |
---|
59 | if i%iprint == 0 : |
---|
60 | pass |
---|
61 | #FIXME: Should depend on verbosity |
---|
62 | #print 'i = %g rTr = %20.15e'% (i,rTr) |
---|
63 | |
---|
64 | #FIXME: Should this raise an exception? |
---|
65 | if i==imax: |
---|
66 | print 'max number of iterations attained' |
---|
67 | |
---|
68 | return x |
---|
69 | |
---|
70 | |
---|