~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to Lib/linalg/tests/test_matfuncs.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#
3
 
# Created by: Pearu Peterson, March 2002
4
 
#
5
 
""" Test functions for linalg.matfuncs module
6
 
 
7
 
"""
8
 
__usage__ = """
9
 
Build linalg:
10
 
  python setup_linalg.py build
11
 
Run tests if scipy is installed:
12
 
  python -c 'import scipy;scipy.linalg.test(<level>)'
13
 
Run tests if linalg is not installed:
14
 
  python tests/test_matfuncs.py [<level>]
15
 
"""
16
 
 
17
 
from numpy import array, identity
18
 
 
19
 
import sys
20
 
from numpy.testing import *
21
 
set_package_path()
22
 
import numpy
23
 
from numpy import dot,sqrt
24
 
import linalg
25
 
from linalg import signm,logm,funm, sqrtm, expm, expm2, expm3
26
 
restore_path()
27
 
 
28
 
class test_signm(ScipyTestCase):
29
 
 
30
 
    def check_nils(self):
31
 
        a = array([[ 29.2, -24.2,  69.5,  49.8,   7. ],
32
 
                   [ -9.2,   5.2, -18. , -16.8,  -2. ],
33
 
                   [-10. ,   6. , -20. , -18. ,  -2. ],
34
 
                   [ -9.6,   9.6, -25.5, -15.4,  -2. ],
35
 
                   [  9.8,  -4.8,  18. ,  18.2,   2. ]])
36
 
        cr = array([[ 11.94933333,-2.24533333,15.31733333,21.65333333,-2.24533333],
37
 
                    [ -3.84266667,0.49866667,-4.59066667,-7.18666667,0.49866667],
38
 
                    [ -4.08,0.56,-4.92,-7.6 ,0.56],
39
 
                    [ -4.03466667,1.04266667,-5.59866667,-7.02666667,1.04266667],
40
 
                    [4.15733333,-0.50133333,4.90933333,7.81333333,-0.50133333]])
41
 
        r = signm(a)
42
 
        assert_array_almost_equal(r,cr)
43
 
 
44
 
    def check_defective1(self):
45
 
        a = array([[0.0,1,0,0],[1,0,1,0],[0,0,0,1],[0,0,1,0]])
46
 
        r = signm(a)
47
 
        #XXX: what would be the correct result?
48
 
 
49
 
    def check_defective2(self):
50
 
        a = array((
51
 
            [29.2,-24.2,69.5,49.8,7.0],
52
 
            [-9.2,5.2,-18.0,-16.8,-2.0],
53
 
            [-10.0,6.0,-20.0,-18.0,-2.0],
54
 
            [-9.6,9.6,-25.5,-15.4,-2.0],
55
 
            [9.8,-4.8,18.0,18.2,2.0]))
56
 
        r = signm(a)
57
 
        #XXX: what would be the correct result?
58
 
 
59
 
    def check_defective3(self):
60
 
        a = array([[ -2.,  25.,   0.,   0.,   0.,   0.,   0.],
61
 
                   [  0.,  -3.,  10.,   3.,   3.,   3.,   0.],
62
 
                   [  0.,   0.,   2.,  15.,   3.,   3.,   0.],
63
 
                   [  0.,   0.,   0.,   0.,  15.,   3.,   0.],
64
 
                   [  0.,   0.,   0.,   0.,   3.,  10.,   0.],
65
 
                   [  0.,   0.,   0.,   0.,   0.,  -2.,  25.],
66
 
                   [  0.,   0.,   0.,   0.,   0.,   0.,  -3.]])
67
 
        r = signm(a)
68
 
        #XXX: what would be the correct result?
69
 
 
70
 
class test_logm(ScipyTestCase):
71
 
 
72
 
    def check_nils(self):
73
 
        a = array([[ -2.,  25.,   0.,   0.,   0.,   0.,   0.],
74
 
                   [  0.,  -3.,  10.,   3.,   3.,   3.,   0.],
75
 
                   [  0.,   0.,   2.,  15.,   3.,   3.,   0.],
76
 
                   [  0.,   0.,   0.,   0.,  15.,   3.,   0.],
77
 
                   [  0.,   0.,   0.,   0.,   3.,  10.,   0.],
78
 
                   [  0.,   0.,   0.,   0.,   0.,  -2.,  25.],
79
 
                   [  0.,   0.,   0.,   0.,   0.,   0.,  -3.]])
80
 
        m = (identity(7)*3.1+0j)-a
81
 
        logm(m)
82
 
 
83
 
 
84
 
class test_sqrtm(ScipyTestCase):
85
 
    def check_bad(self):
86
 
        # See http://www.maths.man.ac.uk/~nareports/narep336.ps.gz
87
 
        e = 2**-5
88
 
        se = sqrt(e)
89
 
        a = array([[1.0,0,0,1],
90
 
                   [0,e,0,0],
91
 
                   [0,0,e,0],
92
 
                   [0,0,0,1]])
93
 
        sa = array([[1,0,0,0.5],
94
 
                    [0,se,0,0],
95
 
                    [0,0,se,0],
96
 
                    [0,0,0,1]])
97
 
        assert_array_almost_equal(dot(sa,sa),a)
98
 
        esa = sqrtm(a)
99
 
        assert_array_almost_equal(dot(esa,esa),a)
100
 
 
101
 
class test_expm(ScipyTestCase):
102
 
    def check_zero(self):
103
 
        a = array([[0.,0],[0,0]])
104
 
        assert_array_almost_equal(expm(a),[[1,0],[0,1]])
105
 
        assert_array_almost_equal(expm2(a),[[1,0],[0,1]])
106
 
        assert_array_almost_equal(expm3(a),[[1,0],[0,1]])
107
 
 
108
 
if __name__ == "__main__":
109
 
    ScipyTest().run()