113
113
assert_equal(x['f1'][1], x['f1'][0])
115
class TestAssignment(TestCase):
116
def test_assignment_broadcasting(self):
117
a = np.arange(6).reshape(2,3)
119
# Broadcasting the input to the output
120
a[...] = np.arange(3)
121
assert_equal(a, [[0,1,2],[0,1,2]])
122
a[...] = np.arange(2).reshape(2,1)
123
assert_equal(a, [[0,0,0],[1,1,1]])
125
# For compatibility with <= 1.5, a limited version of broadcasting
126
# the output to the input.
128
# This behavior is inconsistent with NumPy broadcasting
129
# in general, because it only uses one of the two broadcasting
130
# rules (adding a new "1" dimension to the left of the shape),
131
# applied to the output instead of an input. In NumPy 2.0, this kind
132
# of broadcasting assignment will likely be disallowed.
133
a[...] = np.arange(6)[::-1].reshape(1,2,3)
134
assert_equal(a, [[5,4,3],[2,1,0]])
135
# The other type of broadcasting would require a reduction operation.
138
assert_raises(ValueError, assign, a, np.arange(12).reshape(2,2,3))
116
140
class TestDtypedescr(TestCase):
117
141
def test_construction(self):
239
263
self.assertRaises(IndexError, subscript, a, (newaxis, 0))
240
264
self.assertRaises(IndexError, subscript, a, (newaxis,)*50)
266
def test_overlapping_assignment(self):
267
# With positive strides
270
assert_equal(a, [1,2,3,3])
274
assert_equal(a, [0,0,1,2])
276
# With positive and negative strides
279
assert_equal(a, [3,2,1,0])
281
a = np.arange(6).reshape(2,3)
282
a[::-1,:] = a[:,::-1]
283
assert_equal(a, [[5,4,3],[2,1,0]])
285
a = np.arange(6).reshape(2,3)
286
a[::-1,::-1] = a[:,::-1]
287
assert_equal(a, [[3,4,5],[0,1,2]])
289
# With just one element overlapping
292
assert_equal(a, [2,3,4,3,4])
296
assert_equal(a, [0,1,0,1,2])
300
assert_equal(a, [4,3,2,3,4])
304
assert_equal(a, [0,1,2,1,0])
308
assert_equal(a, [2,3,4,3,4])
312
assert_equal(a, [0,1,0,1,2])
243
314
class TestCreation(TestCase):
244
315
def test_from_attribute(self):
255
326
msg = 'String conversion for %s' % type
256
327
assert_equal(array(nstr, dtype=type), result, err_msg=msg)
329
def test_non_sequence_sequence(self):
330
"""Should not segfault.
332
Class Fail breaks the sequence protocol for new style classes, i.e.,
333
those derived from object. Class Map is a mapping type indicated by
334
raising a ValueError. At some point we may raise a warning instead
335
of an error in the Fail case.
342
def __getitem__(self, index):
349
def __getitem__(self, index):
352
a = np.array([Map()])
353
assert_(a.shape == (1,))
354
assert_(a.dtype == np.dtype(object))
355
assert_raises(ValueError, np.array, [Fail()])
358
class TestStructured(TestCase):
359
def test_subarray_field_access(self):
360
a = np.zeros((3, 5), dtype=[('a', ('i4', (2, 2)))])
361
a['a'] = np.arange(60).reshape(3, 5, 2, 2)
363
# Since the subarray is always in C-order, these aren't equal
364
assert_(np.any(a['a'].T != a.T['a']))
366
# In Fortran order, the subarray gets appended
367
# like in all other cases, not prepended as a special case
368
b = a.copy(order='F')
369
assert_equal(a['a'].shape, b['a'].shape)
370
assert_equal(a.T['a'].shape, a.T.copy()['a'].shape)
373
def test_subarray_comparison(self):
374
# Check that comparisons between record arrays with
375
# multi-dimensional field types work properly
376
a = np.rec.fromrecords(
377
[([1,2,3],'a', [[1,2],[3,4]]),([3,3,3],'b',[[0,0],[0,0]])],
378
dtype=[('a', ('f4',3)), ('b', np.object), ('c', ('i4',(2,2)))])
380
assert_equal(a==b, [True,True])
381
assert_equal(a!=b, [False,False])
383
assert_equal(a==b, [True,False])
384
assert_equal(a!=b, [False,True])
388
assert_equal(a==b, [False,False])
389
assert_equal(a!=b, [True,True])
394
assert_equal(a==b, [False,True])
395
assert_equal(a!=b, [True,False])
397
# Check that broadcasting with a subarray works
398
a = np.array([[(0,)],[(1,)]],dtype=[('a','f8')])
399
b = np.array([(0,),(0,),(1,)],dtype=[('a','f8')])
400
assert_equal(a==b, [[True, True, False], [False, False, True]])
401
assert_equal(b==a, [[True, True, False], [False, False, True]])
402
a = np.array([[(0,)],[(1,)]],dtype=[('a','f8',(1,))])
403
b = np.array([(0,),(0,),(1,)],dtype=[('a','f8',(1,))])
404
assert_equal(a==b, [[True, True, False], [False, False, True]])
405
assert_equal(b==a, [[True, True, False], [False, False, True]])
406
a = np.array([[([0,0],)],[([1,1],)]],dtype=[('a','f8',(2,))])
407
b = np.array([([0,0],),([0,1],),([1,1],)],dtype=[('a','f8',(2,))])
408
assert_equal(a==b, [[True, False, False], [False, False, True]])
409
assert_equal(b==a, [[True, False, False], [False, False, True]])
411
# Check that broadcasting Fortran-style arrays with a subarray work
412
a = np.array([[([0,0],)],[([1,1],)]],dtype=[('a','f8',(2,))], order='F')
413
b = np.array([([0,0],),([0,1],),([1,1],)],dtype=[('a','f8',(2,))])
414
assert_equal(a==b, [[True, False, False], [False, False, True]])
415
assert_equal(b==a, [[True, False, False], [False, False, True]])
417
# Check that incompatible sub-array shapes don't result to broadcasting
418
x = np.zeros((1,), dtype=[('a', ('f4', (1,2))), ('b', 'i1')])
419
y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
420
assert_equal(x == y, False)
422
x = np.zeros((1,), dtype=[('a', ('f4', (2,1))), ('b', 'i1')])
423
y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
424
assert_equal(x == y, False)
259
427
class TestBool(TestCase):
260
428
def test_test_interning(self):
549
725
assert_equal(np.dot(a, b), a.dot(b))
550
726
assert_equal(np.dot(np.dot(a, b), c), a.dot(b).dot(c))
728
def test_ravel(self):
729
a = np.array([[0,1],[2,3]])
730
assert_equal(a.ravel(), [0,1,2,3])
731
assert_(not a.ravel().flags.owndata)
732
assert_equal(a.ravel('F'), [0,2,1,3])
733
assert_equal(a.ravel(order='C'), [0,1,2,3])
734
assert_equal(a.ravel(order='F'), [0,2,1,3])
735
assert_equal(a.ravel(order='A'), [0,1,2,3])
736
assert_(not a.ravel(order='A').flags.owndata)
737
assert_equal(a.ravel(order='K'), [0,1,2,3])
738
assert_(not a.ravel(order='K').flags.owndata)
739
assert_equal(a.ravel(), a.reshape(-1))
741
a = np.array([[0,1],[2,3]], order='F')
742
assert_equal(a.ravel(), [0,1,2,3])
743
assert_equal(a.ravel(order='A'), [0,2,1,3])
744
assert_equal(a.ravel(order='K'), [0,2,1,3])
745
assert_(not a.ravel(order='A').flags.owndata)
746
assert_(not a.ravel(order='K').flags.owndata)
747
assert_equal(a.ravel(), a.reshape(-1))
748
assert_equal(a.ravel(order='A'), a.reshape(-1, order='A'))
750
a = np.array([[0,1],[2,3]])[::-1,:]
751
assert_equal(a.ravel(), [2,3,0,1])
752
assert_equal(a.ravel(order='C'), [2,3,0,1])
753
assert_equal(a.ravel(order='F'), [2,0,3,1])
754
assert_equal(a.ravel(order='A'), [2,3,0,1])
755
# 'K' doesn't reverse the axes of negative strides
756
assert_equal(a.ravel(order='K'), [2,3,0,1])
757
assert_(a.ravel(order='K').flags.owndata)
759
def test_setasflat(self):
760
# In this case, setasflat can treat a as a flat array,
761
# and must treat b in chunks of 3
762
a = np.arange(3*3*4).reshape(3,3,4)
763
b = np.arange(3*4*3, dtype='f4').reshape(3,4,3).T
765
assert_(not np.all(a.ravel() == b.ravel()))
767
assert_equal(a.ravel(), b.ravel())
769
# A case where the strides of neither a nor b can be collapsed
770
a = np.arange(3*2*4).reshape(3,2,4)[:,:,:-1]
771
b = np.arange(3*3*3, dtype='f4').reshape(3,3,3).T[:,:,:-1]
773
assert_(not np.all(a.ravel() == b.ravel()))
775
assert_equal(a.ravel(), b.ravel())
552
777
class TestSubscripting(TestCase):
553
778
def test_test_zero_rank(self):
554
779
x = array([1,2,3])
1188
1455
res = dat.var(1)
1189
1456
assert res.info == dat.info
1458
class TestDot(TestCase):
1459
def test_dot_2args(self):
1460
from numpy.core.multiarray import dot
1462
a = np.array([[1, 2], [3, 4]], dtype=float)
1463
b = np.array([[1, 0], [1, 1]], dtype=float)
1464
c = np.array([[3, 2], [7, 4]], dtype=float)
1467
assert_allclose(c, d)
1469
def test_dot_3args(self):
1470
from numpy.core.multiarray import dot
1473
f = np.random.random_sample((1024, 16))
1474
v = np.random.random_sample((16, 32))
1476
r = np.empty((1024, 32))
1477
for i in xrange(12):
1479
assert_equal(sys.getrefcount(r), 2)
1480
r2 = dot(f,v,out=None)
1481
assert_array_equal(r2, r)
1482
assert_(r is dot(f,v,out=r))
1484
v = v[:,0].copy() # v.shape == (16,)
1485
r = r[:,0].copy() # r.shape == (1024,)
1487
assert_(r is dot(f,v,r))
1488
assert_array_equal(r2, r)
1490
def test_dot_3args_errors(self):
1491
from numpy.core.multiarray import dot
1494
f = np.random.random_sample((1024, 16))
1495
v = np.random.random_sample((16, 32))
1497
r = np.empty((1024, 31))
1498
assert_raises(ValueError, dot, f, v, r)
1500
r = np.empty((1024,))
1501
assert_raises(ValueError, dot, f, v, r)
1504
assert_raises(ValueError, dot, f, v, r)
1506
r = np.empty((32, 1024))
1507
assert_raises(ValueError, dot, f, v, r)
1508
assert_raises(ValueError, dot, f, v, r.T)
1510
r = np.empty((1024, 64))
1511
assert_raises(ValueError, dot, f, v, r[:,::2])
1512
assert_raises(ValueError, dot, f, v, r[:,:32])
1514
r = np.empty((1024, 32), dtype=np.float32)
1515
assert_raises(ValueError, dot, f, v, r)
1517
r = np.empty((1024, 32), dtype=int)
1518
assert_raises(ValueError, dot, f, v, r)
1192
1521
class TestSummarization(TestCase):
1193
1522
def test_1d(self):
1243
1572
def _test_simple2d(self, dt):
1244
1573
# Test zero and one padding for simple data type
1245
1574
x = np.array([[0, 1], [2, 3]], dtype=dt)
1246
r = [np.array([[0, 0, 0], [0, 0, 1]], dtype=dt),
1247
np.array([[0, 0, 0], [0, 1, 0]], dtype=dt),
1248
np.array([[0, 0, 1], [0, 2, 3]], dtype=dt),
1575
r = [np.array([[0, 0, 0], [0, 0, 1]], dtype=dt),
1576
np.array([[0, 0, 0], [0, 1, 0]], dtype=dt),
1577
np.array([[0, 0, 1], [0, 2, 3]], dtype=dt),
1249
1578
np.array([[0, 1, 0], [2, 3, 0]], dtype=dt)]
1250
1579
l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], NEIGH_MODE['zero'])
1251
1580
assert_array_equal(l, r)
1253
r = [np.array([[1, 1, 1], [1, 0, 1]], dtype=dt),
1254
np.array([[1, 1, 1], [0, 1, 1]], dtype=dt),
1255
np.array([[1, 0, 1], [1, 2, 3]], dtype=dt),
1582
r = [np.array([[1, 1, 1], [1, 0, 1]], dtype=dt),
1583
np.array([[1, 1, 1], [0, 1, 1]], dtype=dt),
1584
np.array([[1, 0, 1], [1, 2, 3]], dtype=dt),
1256
1585
np.array([[0, 1, 1], [2, 3, 1]], dtype=dt)]
1257
1586
l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], NEIGH_MODE['one'])
1258
1587
assert_array_equal(l, r)
1260
r = [np.array([[4, 4, 4], [4, 0, 1]], dtype=dt),
1261
np.array([[4, 4, 4], [0, 1, 4]], dtype=dt),
1262
np.array([[4, 0, 1], [4, 2, 3]], dtype=dt),
1589
r = [np.array([[4, 4, 4], [4, 0, 1]], dtype=dt),
1590
np.array([[4, 4, 4], [0, 1, 4]], dtype=dt),
1591
np.array([[4, 0, 1], [4, 2, 3]], dtype=dt),
1263
1592
np.array([[0, 1, 4], [2, 3, 4]], dtype=dt)]
1264
1593
l = test_neighborhood_iterator(x, [-1, 0, -1, 1], 4, NEIGH_MODE['constant'])
1265
1594
assert_array_equal(l, r)
1277
1606
def _test_mirror2d(self, dt):
1278
1607
x = np.array([[0, 1], [2, 3]], dtype=dt)
1279
r = [np.array([[0, 0, 1], [0, 0, 1]], dtype=dt),
1280
np.array([[0, 1, 1], [0, 1, 1]], dtype=dt),
1281
np.array([[0, 0, 1], [2, 2, 3]], dtype=dt),
1608
r = [np.array([[0, 0, 1], [0, 0, 1]], dtype=dt),
1609
np.array([[0, 1, 1], [0, 1, 1]], dtype=dt),
1610
np.array([[0, 0, 1], [2, 2, 3]], dtype=dt),
1282
1611
np.array([[0, 1, 1], [2, 3, 3]], dtype=dt)]
1283
1612
l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], NEIGH_MODE['mirror'])
1284
1613
assert_array_equal(l, r)
1573
1902
self._check('^ixxxx', [('f0', 'i'), ('', 'V4')])
1574
1903
self._check('^i7x', [('f0', 'i'), ('', 'V7')])
1905
def test_native_padding_3(self):
1906
dt = np.dtype([('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')], align=True)
1907
self._check("T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxxi:c:}", dt)
1909
dt = np.dtype([('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'), ('e', 'b'), ('sub', np.dtype('b,i', align=True))])
1910
self._check("T{b:a:=i:b:b:c:b:d:b:e:T{b:f0:xxxi:f1:}:sub:}", dt)
1912
def test_padding_with_array_inside_struct(self):
1913
dt = np.dtype([('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), ('d', 'i')], align=True)
1914
self._check("T{b:a:xxxi:b:3b:c:xi:d:}", dt)
1576
1916
def test_byteorder_inside_struct(self):
1577
1917
# The byte order after @T{=i} should be '=', not '@'.
1578
1918
# Check this by noting the absence of native alignment.
1768
2131
count_2 = sys.getrefcount(np.core._internal)
1769
2132
assert_equal(count_1, count_2)
2134
def test_padded_struct_array(self):
2135
dt1 = np.dtype([('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')], align=True)
2136
x1 = np.arange(dt1.itemsize, dtype=np.int8).view(dt1)
2137
self._check_roundtrip(x1)
2139
dt2 = np.dtype([('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), ('d', 'i')], align=True)
2140
x2 = np.arange(dt2.itemsize, dtype=np.int8).view(dt2)
2141
self._check_roundtrip(x2)
2143
dt3 = np.dtype([('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'), ('e', 'b'), ('sub', np.dtype('b,i', align=True))])
2144
x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3)
2145
self._check_roundtrip(x3)
1772
2147
if __name__ == "__main__":
1773
2148
run_module_suite()