1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import unittest |
---|
4 | #from math import sqrt |
---|
5 | |
---|
6 | from Numeric import allclose, array, transpose |
---|
7 | |
---|
8 | from cg_solve import * |
---|
9 | |
---|
10 | from scipy import sparse |
---|
11 | |
---|
12 | class TestCase(unittest.TestCase): |
---|
13 | |
---|
14 | def setUp(self): |
---|
15 | pass |
---|
16 | |
---|
17 | def tearDown(self): |
---|
18 | pass |
---|
19 | |
---|
20 | |
---|
21 | def test_solve(self): |
---|
22 | A = [[2.0, -1.0, 0.0, 0.0 ], |
---|
23 | [-1.0, 2.0, -1.0, 0.0], |
---|
24 | [0.0, -1.0, 2.0, -1.0], |
---|
25 | [0.0,0.0, -1.0, 2.0]] |
---|
26 | |
---|
27 | A = sparse.dok_matrix(A) |
---|
28 | |
---|
29 | xe = [0.0, 1.0, 2.0, 3.0] |
---|
30 | b = A*xe |
---|
31 | x = [0.0, 0.0, 0.0, 0.0] |
---|
32 | |
---|
33 | x = conjugate_gradient(A,b,x) |
---|
34 | |
---|
35 | print "x",x |
---|
36 | |
---|
37 | assert allclose(x,xe) |
---|
38 | |
---|
39 | |
---|
40 | def test_solve_more(self): |
---|
41 | A = [[2.0, -1.0, 0.0, 0.0 ], |
---|
42 | [-1.0, 2.0, -1.0, 0.0], |
---|
43 | [0.0, -1.0, 2.0, -1.0], |
---|
44 | [0.0,0.0, -1.0, 2.0]] |
---|
45 | |
---|
46 | A = sparse.dok_matrix(A) |
---|
47 | |
---|
48 | xe = [[0.0, 1.0], [1.0, 3.0], [2.0, 5.0], [3.0, 9.0]] |
---|
49 | b = A*xe |
---|
50 | x = [[0.0, 0.0], [0.0, 0.0],[0.0, 0.0],[0.0, 0.0]] |
---|
51 | |
---|
52 | x = conjugate_gradient(A,b,x) |
---|
53 | |
---|
54 | print "x",x |
---|
55 | |
---|
56 | assert allclose(x,xe) |
---|
57 | |
---|
58 | |
---|
59 | #------------------------------------------------------------- |
---|
60 | if __name__ == "__main__": |
---|
61 | suite = unittest.makeSuite(TestCase,'test') |
---|
62 | runner = unittest.TextTestRunner() |
---|
63 | runner.run(suite) |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | |
---|