Debian Codegraph @

pypy/module/micronumpy/test/test_outarg.py

raw symbols SCIP
1 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
2
3
4 class AppTestOutArg(BaseNumpyAppTest):
5 def test_reduce_out(self):
6 from numpy import arange, zeros, array
7 a = arange(15).reshape(5, 3)
8 b = arange(12).reshape(4,3)
9 c = a.sum(0, out=b[1])
10 assert (c == [30, 35, 40]).all()
11 assert (c == b[1]).all()
12 raises(ValueError, 'a.prod(0, out=arange(10))')
13 a=arange(12).reshape(3,2,2)
14 raises(ValueError, 'a.sum(0, out=arange(12).reshape(3,2,2))')
15 raises(ValueError, 'a.sum(0, out=arange(3))')
16 c = array([-1, 0, 1]).sum(out=zeros([], dtype=bool))
17 #You could argue that this should product False, but
18 # that would require an itermediate result. Cpython numpy
19 # gives True.
20 assert c == True
21 a = array([[-1, 0, 1], [1, 0, -1]])
22 c = a.sum(0, out=zeros((3,), dtype=bool))
23 assert (c == [True, False, True]).all()
24 c = a.sum(1, out=zeros((2,), dtype=bool))
25 assert (c == [True, True]).all()
26
27 def test_reduce_intermediary(self):
28 from numpy import arange, array
29 a = arange(15).reshape(5, 3)
30 b = array(range(3), dtype=bool)
31 c = a.prod(0, out=b)
32 assert(b == [False, True, True]).all()
33
34 def test_ufunc_out(self):
35 from numpy import array, negative, zeros, sin
36 from math import sin as msin
37 a = array([[1, 2], [3, 4]])
38 c = zeros((2,2,2))
39 b = negative(a + a, out=c[1])
40 #test for view, and also test that forcing out also forces b
41 assert (c[:, :, 1] == [[0, 0], [-4, -8]]).all()
42 assert (b == [[-2, -4], [-6, -8]]).all()
43 #Test broadcast, type promotion
44 b = negative(3, out=a)
45 assert (a == -3).all()
46 c = zeros((2, 2), dtype=float)
47 b = negative(3, out=c)
48 assert b.dtype.kind == c.dtype.kind
49 assert b.shape == c.shape
50 a = array([1, 2])
51 b = sin(a, out=c)
52 assert(c == [[msin(1), msin(2)]] * 2).all()
53 b = sin(a, out=c+c)
54 assert (c == b).all()
55
56 #Test shape agreement
57 a = zeros((3,4))
58 b = zeros((3,5))
59 raises(ValueError, 'negative(a, out=b)')
60 b = zeros((1,4))
61 raises(ValueError, 'negative(a, out=b)')
62
63 def test_binfunc_out(self):
64 from numpy import array, add
65 a = array([[1, 2], [3, 4]])
66 out = array([[1, 2], [3, 4]])
67 c = add(a, a, out=out)
68 assert (c == out).all()
69 assert c.shape == a.shape
70 assert c.dtype is a.dtype
71 c[0,0] = 100
72 assert out[0, 0] == 100
73 out[:] = 100
74 raises(ValueError, 'c = add(a, a, out=out[1])')
75 c = add(a[0], a[1], out=out[1])
76 assert (c == out[1]).all()
77 assert (c == [4, 6]).all()
78 assert (out[0] == 100).all()
79 c = add(a[0], a[1], out=out)
80 assert (c == out[1]).all()
81 assert (c == out[0]).all()
82 out = array(16, dtype=int)
83 b = add(10, 10, out=out)
84 assert b==out
85 assert b.dtype == out.dtype
86
87 def test_ufunc_cast(self):
88 from numpy import array, negative, add
89 a = array(16, dtype = int)
90 c = array(0, dtype = float)
91 b = negative(a, out=c)
92 assert b == c
93 b = add(a, a, out=c)
94 assert b == c
95 d = array([16, 16], dtype=int)
96 b = d.sum(out=c)
97 assert b == c
98 #cast_error = raises(TypeError, negative, c, a)
99 #assert str(cast_error.value) == \
100 # "Cannot cast ufunc negative output from dtype('float64') to dtype('int64') with casting rule 'same_kind'"