2
from numpy.testing import *
4
from numpy.core.umath import minimum, maximum, exp
5
import numpy.core.umath as ncu
6
from numpy import zeros, ndarray, array, choose
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.])
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)])
18
def check_power_complex(self):
19
x = array([1+2j, 2+3j, 3+4j])
20
assert_equal(x**0, [1., 1., 1.])
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,
26
assert_almost_equal(x**(0.5), [ncu.sqrt(1+2j), ncu.sqrt(2+3j),
28
assert_almost_equal(x**14, [-76443+16124j, 23161315+58317492j,
29
5583548873 + 2465133864j])
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))
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)
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)
46
class test_minimum(ScipyTestCase):
47
def check_reduce_complex(self):
48
assert_equal(minimum.reduce([1,2j]),2j)
50
class test_floating_point(ScipyTestCase):
51
def check_floating_point(self):
52
assert_equal(ncu.FLOATING_POINT_SUPPORT, 1)
54
class test_special_methods(ScipyTestCase):
56
class with_wrap(object):
59
def __array_wrap__(self, arr, context):
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)
74
def test_old_wrap(self):
75
class with_wrap(object):
78
def __array_wrap__(self, arr):
84
assert_equal(x.arr, zeros(1))
86
def test_priority(self):
90
def __array_wrap__(self, arr, context):
96
__array_priority__ = 20.
98
__array_priority__ = 40.
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)
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)
120
self.failUnless(type(exp(a) is A))
121
self.failUnless(type(exp(b) is B))
122
self.failUnless(type(exp(c) is C))
124
def test_failing_wrap(self):
128
def __array_wrap__(self, arr, context):
131
self.failUnlessRaises(RuntimeError, maximum, a, a)
133
def test_array_with_context(self):
135
def __array__(self, dtype=None, context=None):
136
func, args, i = context
142
def __array__(self, dtype=None):
143
return zeros(1, dtype)
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)
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]))
162
if __name__ == "__main__":