File: test_matfuncs.py

package info (click to toggle)
python-scipy 0.7.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,500 kB
  • ctags: 36,081
  • sloc: cpp: 216,880; fortran: 76,016; python: 71,576; ansic: 62,118; makefile: 243; sh: 17
file content (97 lines) | stat: -rw-r--r-- 3,474 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
#
# Created by: Pearu Peterson, March 2002
#
""" Test functions for linalg.matfuncs module

"""

from numpy import array, identity, dot, sqrt
from numpy.testing import *

import scipy.linalg
from scipy.linalg import signm, logm, sqrtm, expm, expm2, expm3


class TestSignM(TestCase):

    def test_nils(self):
        a = array([[ 29.2, -24.2,  69.5,  49.8,   7. ],
                   [ -9.2,   5.2, -18. , -16.8,  -2. ],
                   [-10. ,   6. , -20. , -18. ,  -2. ],
                   [ -9.6,   9.6, -25.5, -15.4,  -2. ],
                   [  9.8,  -4.8,  18. ,  18.2,   2. ]])
        cr = array([[ 11.94933333,-2.24533333,15.31733333,21.65333333,-2.24533333],
                    [ -3.84266667,0.49866667,-4.59066667,-7.18666667,0.49866667],
                    [ -4.08,0.56,-4.92,-7.6 ,0.56],
                    [ -4.03466667,1.04266667,-5.59866667,-7.02666667,1.04266667],
                    [4.15733333,-0.50133333,4.90933333,7.81333333,-0.50133333]])
        r = signm(a)
        assert_array_almost_equal(r,cr)

    def test_defective1(self):
        a = array([[0.0,1,0,0],[1,0,1,0],[0,0,0,1],[0,0,1,0]])
        r = signm(a)
        #XXX: what would be the correct result?

    def test_defective2(self):
        a = array((
            [29.2,-24.2,69.5,49.8,7.0],
            [-9.2,5.2,-18.0,-16.8,-2.0],
            [-10.0,6.0,-20.0,-18.0,-2.0],
            [-9.6,9.6,-25.5,-15.4,-2.0],
            [9.8,-4.8,18.0,18.2,2.0]))
        r = signm(a)
        #XXX: what would be the correct result?

    def test_defective3(self):
        a = array([[ -2.,  25.,   0.,   0.,   0.,   0.,   0.],
                   [  0.,  -3.,  10.,   3.,   3.,   3.,   0.],
                   [  0.,   0.,   2.,  15.,   3.,   3.,   0.],
                   [  0.,   0.,   0.,   0.,  15.,   3.,   0.],
                   [  0.,   0.,   0.,   0.,   3.,  10.,   0.],
                   [  0.,   0.,   0.,   0.,   0.,  -2.,  25.],
                   [  0.,   0.,   0.,   0.,   0.,   0.,  -3.]])
        r = signm(a)
        #XXX: what would be the correct result?

class TestLogM(TestCase):

    def test_nils(self):
        a = array([[ -2.,  25.,   0.,   0.,   0.,   0.,   0.],
                   [  0.,  -3.,  10.,   3.,   3.,   3.,   0.],
                   [  0.,   0.,   2.,  15.,   3.,   3.,   0.],
                   [  0.,   0.,   0.,   0.,  15.,   3.,   0.],
                   [  0.,   0.,   0.,   0.,   3.,  10.,   0.],
                   [  0.,   0.,   0.,   0.,   0.,  -2.,  25.],
                   [  0.,   0.,   0.,   0.,   0.,   0.,  -3.]])
        m = (identity(7)*3.1+0j)-a
        logm(m)


class TestSqrtM(TestCase):
    def test_bad(self):
        # See http://www.maths.man.ac.uk/~nareports/narep336.ps.gz
        e = 2**-5
        se = sqrt(e)
        a = array([[1.0,0,0,1],
                   [0,e,0,0],
                   [0,0,e,0],
                   [0,0,0,1]])
        sa = array([[1,0,0,0.5],
                    [0,se,0,0],
                    [0,0,se,0],
                    [0,0,0,1]])
        assert_array_almost_equal(dot(sa,sa),a)
        esa = sqrtm(a)
        assert_array_almost_equal(dot(esa,esa),a)

class TestExpM(TestCase):
    def test_zero(self):
        a = array([[0.,0],[0,0]])
        assert_array_almost_equal(expm(a),[[1,0],[0,1]])
        assert_array_almost_equal(expm2(a),[[1,0],[0,1]])
        assert_array_almost_equal(expm3(a),[[1,0],[0,1]])

if __name__ == "__main__":
    run_module_suite()