| 1 |
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest |
| 2 |
|
| 3 |
|
| 4 |
class AppTestNumSupport(BaseNumpyAppTest): |
| 5 |
def test_zeros(self): |
| 6 |
from numpy import zeros |
| 7 |
a = zeros(3) |
| 8 |
assert len(a) == 3 |
| 9 |
assert a[0] == a[1] == a[2] == 0 |
| 10 |
|
| 11 |
def test_empty(self): |
| 12 |
from numpy import empty |
| 13 |
import gc |
| 14 |
for i in range(1000): |
| 15 |
a = empty(3) |
| 16 |
assert len(a) == 3 |
| 17 |
if not (a[0] == a[1] == a[2] == 0): |
| 18 |
break |
| 19 |
a[0] = 1.23 |
| 20 |
a[1] = 4.56 |
| 21 |
a[2] = 7.89 |
| 22 |
del a |
| 23 |
gc.collect() |
| 24 |
else: |
| 25 |
raise AssertionError( |
| 26 |
"empty() returned a zeroed out array every time") |
| 27 |
|
| 28 |
def test_where(self): |
| 29 |
from numpy import where, ones, zeros, array |
| 30 |
a = [1, 2, 3, 0, -3] |
| 31 |
a = where(array(a) > 0, ones(5), zeros(5)) |
| 32 |
assert (a == [1, 1, 1, 0, 0]).all() |
| 33 |
|
| 34 |
def test_where_differing_dtypes(self): |
| 35 |
from numpy import array, ones, zeros, where |
| 36 |
a = [1, 2, 3, 0, -3] |
| 37 |
a = where(array(a) > 0, ones(5, dtype=int), zeros(5, dtype=float)) |
| 38 |
assert (a == [1, 1, 1, 0, 0]).all() |
| 39 |
|
| 40 |
def test_where_broadcast(self): |
| 41 |
from numpy import array, where |
| 42 |
a = where(array([[1, 2, 3], [4, 5, 6]]) > 3, [1, 1, 1], 2) |
| 43 |
assert (a == [[2, 2, 2], [1, 1, 1]]).all() |
| 44 |
a = where(True, [1, 1, 1], 2) |
| 45 |
assert (a == [1, 1, 1]).all() |
| 46 |
|
| 47 |
def test_where_errors(self): |
| 48 |
from numpy import where, array |
| 49 |
raises(ValueError, "where([1, 2, 3], [3, 4, 5])") |
| 50 |
raises(ValueError, "where([1, 2, 3], [3, 4, 5], [6, 7])") |
| 51 |
assert where(True, 1, 2) == array(1) |
| 52 |
assert where(False, 1, 2) == array(2) |
| 53 |
assert (where(True, [1, 2, 3], 2) == [1, 2, 3]).all() |
| 54 |
assert (where(False, 1, [1, 2, 3]) == [1, 2, 3]).all() |
| 55 |
assert (where([1, 2, 3], True, False) == [True, True, True]).all() |
| 56 |
|
| 57 |
def test_where_1_arg(self): |
| 58 |
from numpy import where, array |
| 59 |
|
| 60 |
result = where([1,0,1]) |
| 61 |
|
| 62 |
assert isinstance(result, tuple) |
| 63 |
assert len(result) == 1 |
| 64 |
assert (result[0] == array([0, 2])).all() |
| 65 |
|
| 66 |
def test_where_1_arg_2d(self): |
| 67 |
from numpy import where, array |
| 68 |
|
| 69 |
result = where([[1,0,1],[2,-1,-1]]) |
| 70 |
|
| 71 |
assert isinstance(result, tuple) |
| 72 |
assert len(result) == 2 |
| 73 |
assert (result[0] == array([0, 0, 1, 1, 1])).all() |
| 74 |
assert (result[1] == array([0, 2, 0, 1, 2])).all() |
| 75 |
|
| 76 |
def test_where_invalidates(self): |
| 77 |
from numpy import where, ones, zeros, array |
| 78 |
a = array([1, 2, 3, 0, -3]) |
| 79 |
b = where(a > 0, ones(5), zeros(5)) |
| 80 |
a[0] = 0 |
| 81 |
assert (b == [1, 1, 1, 0, 0]).all() |
| 82 |
|
| 83 |
def test_dot_basic(self): |
| 84 |
from numpy import array, dot, arange |
| 85 |
a = array(range(5)) |
| 86 |
assert dot(a, a) == 30.0 |
| 87 |
|
| 88 |
a = array(range(5)) |
| 89 |
assert a.dot(range(5)) == 30 |
| 90 |
assert dot(range(5), range(5)) == 30 |
| 91 |
assert (dot(5, [1, 2, 3]) == [5, 10, 15]).all() |
| 92 |
|
| 93 |
a = arange(12).reshape(3, 4) |
| 94 |
b = arange(12).reshape(4, 3) |
| 95 |
c = a.dot(b) |
| 96 |
assert (c == [[ 42, 48, 54], [114, 136, 158], [186, 224, 262]]).all() |
| 97 |
c = a.dot(b.astype(float)) |
| 98 |
assert (c == [[ 42, 48, 54], [114, 136, 158], [186, 224, 262]]).all() |
| 99 |
c = a.astype(float).dot(b) |
| 100 |
assert (c == [[ 42, 48, 54], [114, 136, 158], [186, 224, 262]]).all() |
| 101 |
|
| 102 |
a = arange(24).reshape(2, 3, 4) |
| 103 |
raises(ValueError, "a.dot(a)") |
| 104 |
b = a[0, :, :].T |
| 105 |
|
| 106 |
assert a.shape == (2, 3, 4) |
| 107 |
assert b.shape == (4, 3) |
| 108 |
c = dot(a, b) |
| 109 |
assert (c == [[[14, 38, 62], [38, 126, 214], [62, 214, 366]], |
| 110 |
[[86, 302, 518], [110, 390, 670], [134, 478, 822]]]).all() |
| 111 |
c = dot(a, b[:, 2]) |
| 112 |
assert (c == [[62, 214, 366], [518, 670, 822]]).all() |
| 113 |
a = arange(3*2*6).reshape((3,2,6)) |
| 114 |
b = arange(3*2*6)[::-1].reshape((2,6,3)) |
| 115 |
assert dot(a, b)[2,0,1,2] == 1140 |
| 116 |
assert (dot([[1,2],[3,4]],[5,6]) == [17, 39]).all() |
| 117 |
|
| 118 |
def test_dot_constant(self): |
| 119 |
from numpy import array, dot |
| 120 |
a = array(range(5)) |
| 121 |
b = a.dot(2.5) |
| 122 |
for i in xrange(5): |
| 123 |
assert b[i] == 2.5 * a[i] |
| 124 |
c = dot(4, 3.0) |
| 125 |
assert c == 12.0 |
| 126 |
c = array(3.0).dot(array(4)) |
| 127 |
assert c == 12.0 |
| 128 |
|
| 129 |
def test_dot_out(self): |
| 130 |
from numpy import arange, dot |
| 131 |
a = arange(12).reshape(3, 4) |
| 132 |
b = arange(12).reshape(4, 3) |
| 133 |
out = arange(9).reshape(3, 3) |
| 134 |
c = dot(a, b, out=out) |
| 135 |
assert (c == out).all() |
| 136 |
assert (c == [[42, 48, 54], [114, 136, 158], [186, 224, 262]]).all() |
| 137 |
out = arange(9, dtype=float).reshape(3, 3) |
| 138 |
exc = raises(ValueError, dot, a, b, out) |
| 139 |
assert exc.value[0] == ('output array is not acceptable (must have the ' |
| 140 |
'right type, nr dimensions, and be a C-Array)') |
| 141 |
|
| 142 |
def test_choose_basic(self): |
| 143 |
from numpy import array |
| 144 |
a, b, c = array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]) |
| 145 |
r = array([2, 1, 0]).choose([a, b, c]) |
| 146 |
assert (r == [7, 5, 3]).all() |
| 147 |
|
| 148 |
def test_choose_broadcast(self): |
| 149 |
from numpy import array |
| 150 |
a, b, c = array([1, 2, 3]), [4, 5, 6], 13 |
| 151 |
r = array([2, 1, 0]).choose([a, b, c]) |
| 152 |
assert (r == [13, 5, 3]).all() |
| 153 |
|
| 154 |
def test_choose_out(self): |
| 155 |
from numpy import array |
| 156 |
a, b, c = array([1, 2, 3]), [4, 5, 6], 13 |
| 157 |
r = array([2, 1, 0]).choose([a, b, c], out=None) |
| 158 |
assert (r == [13, 5, 3]).all() |
| 159 |
assert (a == [1, 2, 3]).all() |
| 160 |
r = array([2, 1, 0]).choose([a, b, c], out=a) |
| 161 |
assert (r == [13, 5, 3]).all() |
| 162 |
assert (a == [13, 5, 3]).all() |
| 163 |
|
| 164 |
def test_choose_modes(self): |
| 165 |
from numpy import array |
| 166 |
a, b, c = array([1, 2, 3]), [4, 5, 6], 13 |
| 167 |
raises(ValueError, "array([3, 1, 0]).choose([a, b, c])") |
| 168 |
raises(ValueError, "array([3, 1, 0]).choose([a, b, c], mode='raises')") |
| 169 |
raises(ValueError, "array([3, 1, 0]).choose([])") |
| 170 |
raises(ValueError, "array([-1, -2, -3]).choose([a, b, c])") |
| 171 |
r = array([4, 1, 0]).choose([a, b, c], mode='clip') |
| 172 |
assert (r == [13, 5, 3]).all() |
| 173 |
r = array([4, 1, 0]).choose([a, b, c], mode='wrap') |
| 174 |
assert (r == [4, 5, 3]).all() |
| 175 |
|
| 176 |
def test_choose_dtype(self): |
| 177 |
from numpy import array |
| 178 |
a, b, c = array([1.2, 2, 3]), [4, 5, 6], 13 |
| 179 |
r = array([2, 1, 0]).choose([a, b, c]) |
| 180 |
assert r.dtype == float |
| 181 |
|
| 182 |
def test_choose_dtype_out(self): |
| 183 |
from numpy import array |
| 184 |
a, b, c = array([1, 2, 3]), [4, 5, 6], 13 |
| 185 |
x = array([0, 0, 0], dtype='i2') |
| 186 |
r = array([2, 1, 0]).choose([a, b, c], out=x) |
| 187 |
assert r.dtype == 'i2' |
| 188 |
|
| 189 |
def test_put_basic(self): |
| 190 |
from numpy import arange, array |
| 191 |
a = arange(5) |
| 192 |
a.put([0, 2], [-44, -55]) |
| 193 |
assert (a == array([-44, 1, -55, 3, 4])).all() |
| 194 |
a = arange(5) |
| 195 |
a.put([3, 4], 9) |
| 196 |
assert (a == array([0, 1, 2, 9, 9])).all() |
| 197 |
a = arange(5) |
| 198 |
a.put(1, [7, 8]) |
| 199 |
assert (a == array([0, 7, 2, 3, 4])).all() |
| 200 |
|
| 201 |
def test_put_modes(self): |
| 202 |
from numpy import array, arange |
| 203 |
a = arange(5) |
| 204 |
a.put(22, -5, mode='clip') |
| 205 |
assert (a == array([0, 1, 2, 3, -5])).all() |
| 206 |
a = arange(5) |
| 207 |
a.put(22, -5, mode='wrap') |
| 208 |
assert (a == array([0, 1, -5, 3, 4])).all() |
| 209 |
raises(IndexError, "arange(5).put(22, -5, mode='raise')") |
| 210 |
raises(IndexError, "arange(5).put(22, -5, mode=2)") |
| 211 |
a.put(22, -10, mode='wrongmode_starts_with_w_so_wrap') |
| 212 |
assert (a == array([0, 1, -10, 3, 4])).all() |
| 213 |
a.put(22, -15, mode='cccccccc') |
| 214 |
assert (a == array([0, 1, -10, 3, -15])).all() |
| 215 |
a.put(23, -1, mode=1) |
| 216 |
assert (a == array([0, 1, -10, -1, -15])).all() |
| 217 |
raises(TypeError, "arange(5).put(22, -5, mode='zzzz')") |