~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

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 Numeric import array, identity
 
18
 
 
19
import sys
 
20
from scipy_test.testing import *
 
21
set_package_path()
 
22
import scipy_base
 
23
from scipy_base import dot,sqrt
 
24
import linalg
 
25
from linalg import signm,logm,funm, sqrtm
 
26
del sys.path[0]
 
27
 
 
28
import unittest
 
29
 
 
30
class test_signm(ScipyTestCase):
 
31
 
 
32
    def check_nils(self):
 
33
        a = array([[ 29.2, -24.2,  69.5,  49.8,   7. ],
 
34
                   [ -9.2,   5.2, -18. , -16.8,  -2. ],
 
35
                   [-10. ,   6. , -20. , -18. ,  -2. ],
 
36
                   [ -9.6,   9.6, -25.5, -15.4,  -2. ],
 
37
                   [  9.8,  -4.8,  18. ,  18.2,   2. ]])
 
38
        cr = array([[ 11.94933333,-2.24533333,15.31733333,21.65333333,-2.24533333],
 
39
                    [ -3.84266667,0.49866667,-4.59066667,-7.18666667,0.49866667],
 
40
                    [ -4.08,0.56,-4.92,-7.6 ,0.56],
 
41
                    [ -4.03466667,1.04266667,-5.59866667,-7.02666667,1.04266667],
 
42
                    [4.15733333,-0.50133333,4.90933333,7.81333333,-0.50133333]])
 
43
        r = signm(a)
 
44
        assert_array_almost_equal(r,cr)
 
45
 
 
46
    def check_defective1(self):
 
47
        a = array([[0.0,1,0,0],[1,0,1,0],[0,0,0,1],[0,0,1,0]])
 
48
        r = signm(a)
 
49
        #XXX: what would be the correct result?
 
50
 
 
51
    def check_defective2(self):
 
52
        a = array((
 
53
            [29.2,-24.2,69.5,49.8,7.0],
 
54
            [-9.2,5.2,-18.0,-16.8,-2.0],
 
55
            [-10.0,6.0,-20.0,-18.0,-2.0],
 
56
            [-9.6,9.6,-25.5,-15.4,-2.0],
 
57
            [9.8,-4.8,18.0,18.2,2.0]))
 
58
        r = signm(a)
 
59
        #XXX: what would be the correct result?
 
60
 
 
61
    def check_defective3(self):
 
62
        a = array([[ -2.,  25.,   0.,   0.,   0.,   0.,   0.],
 
63
                   [  0.,  -3.,  10.,   3.,   3.,   3.,   0.],
 
64
                   [  0.,   0.,   2.,  15.,   3.,   3.,   0.],
 
65
                   [  0.,   0.,   0.,   0.,  15.,   3.,   0.],
 
66
                   [  0.,   0.,   0.,   0.,   3.,  10.,   0.],
 
67
                   [  0.,   0.,   0.,   0.,   0.,  -2.,  25.],
 
68
                   [  0.,   0.,   0.,   0.,   0.,   0.,  -3.]])
 
69
        r = signm(a)
 
70
        #XXX: what would be the correct result?
 
71
 
 
72
class test_logm(ScipyTestCase):
 
73
 
 
74
    def check_nils(self):
 
75
        a = array([[ -2.,  25.,   0.,   0.,   0.,   0.,   0.],
 
76
                   [  0.,  -3.,  10.,   3.,   3.,   3.,   0.],
 
77
                   [  0.,   0.,   2.,  15.,   3.,   3.,   0.],
 
78
                   [  0.,   0.,   0.,   0.,  15.,   3.,   0.],
 
79
                   [  0.,   0.,   0.,   0.,   3.,  10.,   0.],
 
80
                   [  0.,   0.,   0.,   0.,   0.,  -2.,  25.],
 
81
                   [  0.,   0.,   0.,   0.,   0.,   0.,  -3.]])
 
82
        logm((identity(7)*3.1+0j)-a)
 
83
 
 
84
 
 
85
class test_sqrtm(ScipyTestCase):
 
86
    def check_bad(self):
 
87
        # See http://www.maths.man.ac.uk/~nareports/narep336.ps.gz
 
88
        e = 2**-5
 
89
        se = sqrt(e)
 
90
        a = array([[1.0,0,0,1],
 
91
                   [0,e,0,0],
 
92
                   [0,0,e,0],
 
93
                   [0,0,0,1]])
 
94
        sa = array([[1,0,0,0.5],
 
95
                    [0,se,0,0],
 
96
                    [0,0,se,0],
 
97
                    [0,0,0,1]])
 
98
        assert_array_almost_equal(dot(sa,sa),a)
 
99
        esa = sqrtm(a)
 
100
        assert_array_almost_equal(dot(esa,esa),a)
 
101
 
 
102
 
 
103
if __name__ == "__main__":
 
104
    ScipyTest('linalg.matfuncs').run()