~ubuntu-branches/ubuntu/feisty/python-numpy/feisty

« back to all changes in this revision

Viewing changes to numpy/core/tests/test_umath.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-07-12 10:00:24 UTC
  • Revision ID: james.westby@ubuntu.com-20060712100024-5lw9q2yczlisqcrt
Tags: upstream-0.9.8
ImportĀ upstreamĀ versionĀ 0.9.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from numpy.testing import *
 
3
set_package_path()
 
4
from numpy.core.umath import minimum, maximum, exp
 
5
import numpy.core.umath as ncu
 
6
from numpy import zeros, ndarray, array, choose
 
7
restore_path()
 
8
 
 
9
class test_power(ScipyTestCase):
 
10
    def check_power_float(self):
 
11
        x = array([1., 2., 3.])
 
12
        assert_equal(x**0, [1., 1., 1.])
 
13
        assert_equal(x**1, x)
 
14
        assert_equal(x**2, [1., 4., 9.])
 
15
        assert_almost_equal(x**(-1), [1., 0.5, 1./3])
 
16
        assert_almost_equal(x**(0.5), [1., ncu.sqrt(2), ncu.sqrt(3)])
 
17
 
 
18
    def check_power_complex(self):
 
19
        x = array([1+2j, 2+3j, 3+4j])
 
20
        assert_equal(x**0, [1., 1., 1.])
 
21
        assert_equal(x**1, x)
 
22
        assert_equal(x**2, [-3+4j, -5+12j, -7+24j])
 
23
        assert_almost_equal(x**(-1), [1/(1+2j), 1/(2+3j), 1/(3+4j)])
 
24
        assert_almost_equal(x**(-3), [(-11+2j)/125, (-46-9j)/2197,
 
25
                                      (-117-44j)/15625])
 
26
        assert_almost_equal(x**(0.5), [ncu.sqrt(1+2j), ncu.sqrt(2+3j),
 
27
                                       ncu.sqrt(3+4j)])
 
28
        assert_almost_equal(x**14, [-76443+16124j, 23161315+58317492j,
 
29
                                    5583548873 +  2465133864j])
 
30
 
 
31
class test_log1p(ScipyTestCase):
 
32
    def check_log1p(self):
 
33
        assert_almost_equal(ncu.log1p(0.2), ncu.log(1.2))
 
34
        assert_almost_equal(ncu.log1p(1e-6), ncu.log(1+1e-6))
 
35
 
 
36
class test_expm1(ScipyTestCase):
 
37
    def check_expm1(self):
 
38
        assert_almost_equal(ncu.expm1(0.2), ncu.exp(0.2)-1)
 
39
        assert_almost_equal(ncu.expm1(1e-6), ncu.exp(1e-6)-1)
 
40
 
 
41
class test_maximum(ScipyTestCase):
 
42
    def check_reduce_complex(self):
 
43
        assert_equal(maximum.reduce([1,2j]),1)
 
44
        assert_equal(maximum.reduce([1+3j,2j]),1+3j)
 
45
 
 
46
class test_minimum(ScipyTestCase):
 
47
    def check_reduce_complex(self):
 
48
        assert_equal(minimum.reduce([1,2j]),2j)
 
49
 
 
50
class test_floating_point(ScipyTestCase):
 
51
    def check_floating_point(self):
 
52
        assert_equal(ncu.FLOATING_POINT_SUPPORT, 1)
 
53
 
 
54
class test_special_methods(ScipyTestCase):
 
55
    def test_wrap(self):
 
56
        class with_wrap(object):
 
57
            def __array__(self):
 
58
                return zeros(1)
 
59
            def __array_wrap__(self, arr, context):
 
60
                r = with_wrap()
 
61
                r.arr = arr
 
62
                r.context = context
 
63
                return r
 
64
        a = with_wrap()
 
65
        x = minimum(a, a)
 
66
        assert_equal(x.arr, zeros(1))
 
67
        func, args, i = x.context
 
68
        self.failUnless(func is minimum)
 
69
        self.failUnlessEqual(len(args), 2)
 
70
        assert_equal(args[0], a)
 
71
        assert_equal(args[1], a)
 
72
        self.failUnlessEqual(i, 0)
 
73
 
 
74
    def test_old_wrap(self):
 
75
        class with_wrap(object):
 
76
            def __array__(self):
 
77
                return zeros(1)
 
78
            def __array_wrap__(self, arr):
 
79
                r = with_wrap()
 
80
                r.arr = arr
 
81
                return r
 
82
        a = with_wrap()
 
83
        x = minimum(a, a)
 
84
        assert_equal(x.arr, zeros(1))
 
85
 
 
86
    def test_priority(self):
 
87
        class A(object):
 
88
            def __array__(self):
 
89
                return zeros(1)
 
90
            def __array_wrap__(self, arr, context):
 
91
                r = type(self)()
 
92
                r.arr = arr
 
93
                r.context = context
 
94
                return r
 
95
        class B(A):
 
96
            __array_priority__ = 20.
 
97
        class C(A):
 
98
            __array_priority__ = 40.
 
99
        x = zeros(1)
 
100
        a = A()
 
101
        b = B()
 
102
        c = C()
 
103
        f = minimum
 
104
        self.failUnless(type(f(x,x)) is ndarray)
 
105
        self.failUnless(type(f(x,a)) is A)
 
106
        self.failUnless(type(f(x,b)) is B)
 
107
        self.failUnless(type(f(x,c)) is C)
 
108
        self.failUnless(type(f(a,x)) is A)
 
109
        self.failUnless(type(f(b,x)) is B)
 
110
        self.failUnless(type(f(c,x)) is C)
 
111
 
 
112
        self.failUnless(type(f(a,a)) is A)
 
113
        self.failUnless(type(f(a,b)) is B)
 
114
        self.failUnless(type(f(b,a)) is B)
 
115
        self.failUnless(type(f(b,b)) is B)
 
116
        self.failUnless(type(f(b,c)) is C)
 
117
        self.failUnless(type(f(c,b)) is C)
 
118
        self.failUnless(type(f(c,c)) is C)
 
119
 
 
120
        self.failUnless(type(exp(a) is A))
 
121
        self.failUnless(type(exp(b) is B))
 
122
        self.failUnless(type(exp(c) is C))
 
123
 
 
124
    def test_failing_wrap(self):
 
125
        class A(object):
 
126
            def __array__(self):
 
127
                return zeros(1)
 
128
            def __array_wrap__(self, arr, context):
 
129
                raise RuntimeError
 
130
        a = A()
 
131
        self.failUnlessRaises(RuntimeError, maximum, a, a)
 
132
 
 
133
    def test_array_with_context(self):
 
134
        class A(object):
 
135
            def __array__(self, dtype=None, context=None):
 
136
                func, args, i = context
 
137
                self.func = func
 
138
                self.args = args
 
139
                self.i = i
 
140
                return zeros(1)
 
141
        class B(object):
 
142
            def __array__(self, dtype=None):
 
143
                return zeros(1, dtype)
 
144
        class C(object):
 
145
            def __array__(self):
 
146
                return zeros(1)
 
147
        a = A()
 
148
        maximum(zeros(1), a)
 
149
        self.failUnless(a.func is maximum)
 
150
        assert_equal(a.args[0], 0)
 
151
        self.failUnless(a.args[1] is a)
 
152
        self.failUnless(a.i == 1)
 
153
        assert_equal(maximum(a, B()), 0)
 
154
        assert_equal(maximum(a, C()), 0)
 
155
 
 
156
class test_choose(ScipyTestCase):
 
157
    def test_mixed(self):
 
158
        c = array([True,True])
 
159
        a = array([True,True])
 
160
        assert_equal(choose(c, (a, 1)), array([1,1]))
 
161
 
 
162
if __name__ == "__main__":
 
163
    ScipyTest().run()