Debian Codegraph @

pypy/module/micronumpy/test/test_broadcast.py

raw symbols SCIP
1 # -*- encoding: utf-8 -*-
2
3 from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest
4
5
6 class AppTestArrayBroadcast(BaseNumpyAppTest):
7 def test_broadcast_for_row_and_column(self):
8 import numpy as np
9 x = np.array([[1], [2], [3]])
10 y = np.array([4, 5])
11 b = list(np.broadcast(x, y))
12 assert b == [(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]
13
14 def test_broadcast_properties(self):
15 import numpy as np
16 x = np.array([[1], [2], [3]])
17 y = np.array([4, 5])
18 b = np.broadcast(x, y)
19
20 assert b.shape == (3, 2)
21 assert b.size == 6
22 assert b.index == 0
23
24 b.next()
25 b.next()
26
27 assert b.shape == (3, 2)
28 assert b.size == 6
29 assert b.index == 2
30
31 def test_broadcast_from_doctest(self):
32 """
33 Test from numpy.broadcast doctest.
34 """
35 import numpy as np
36 x = np.array([[1], [2], [3]])
37 y = np.array([4, 5, 6])
38 reference = np.array([[5., 6., 7.],
39 [6., 7., 8.],
40 [7., 8., 9.]])
41
42 b = np.broadcast(x, y)
43 out = np.empty(b.shape)
44 out.flat = [u + v for (u, v) in b]
45
46 assert (reference == out).all()
47 assert out.dtype == reference.dtype
48 assert b.shape == reference.shape
49
50 def test_broadcast_linear(self):
51 import numpy as np
52 x = np.array([1, 2, 3])
53 y = np.array([4, 5, 6])
54 b = list(np.broadcast(x, y))
55 assert b == [(1, 4), (2, 5), (3, 6)]
56 assert b[0][0].dtype == x.dtype
57
58 def test_broadcast_failures(self):
59 import numpy as np
60 import sys
61 x = np.array([1, 2, 3])
62 y = np.array([4, 5])
63 raises(ValueError, np.broadcast, x, y)
64 a = np.empty(2**16,dtype='int8')
65 a = a.reshape(-1, 1, 1, 1)
66 b = a.reshape(1, -1, 1, 1)
67 c = a.reshape(1, 1, -1, 1)
68 d = a.reshape(1, 1, 1, -1)
69 exc = raises(ValueError, np.broadcast, a, b, c, d)
70 assert exc.value[0] == ('broadcast dimensions too large.')
71
72 def test_broadcast_3_args(self):
73 import numpy as np
74 x = np.array([[[1]], [[2]], [[3]]])
75 y = np.array([[[40], [50]]])
76 z = np.array([[[700, 800]]])
77
78 b = list(np.broadcast(x, y, z))
79
80 assert b == [(1, 40, 700), (1, 40, 800), (1, 50, 700), (1, 50, 800),
81 (2, 40, 700), (2, 40, 800), (2, 50, 700), (2, 50, 800),
82 (3, 40, 700), (3, 40, 800), (3, 50, 700), (3, 50, 800)]
83
84 def test_number_of_arguments(self):
85 """
86 Test from numpy unit tests.
87 """
88 import numpy as np
89 arr = np.empty((5,))
90 for j in range(35):
91 arrs = [arr] * j
92 if j < 2 or j > 32:
93 exc = raises(ValueError, np.broadcast, *arrs)
94 assert exc.value[0] == ('Need at least two and fewer than (32) array objects.')
95 else:
96 mit = np.broadcast(*arrs)
97 assert mit.numiter == j