MCPcopy Create free account
hub / github.com/numpy/numpy / assert_same_as_ufunc

Function assert_same_as_ufunc

numpy/lib/tests/test_stride_tricks.py:33–52  ·  view source on GitHub ↗
(shape0, shape1, transposed=False, flipped=False)

Source from the content-addressed store, hash-verified

31
32
33def assert_same_as_ufunc(shape0, shape1, transposed=False, flipped=False):
34 # Broadcast two shapes against each other and check that the data layout
35 # is the same as if a ufunc did the broadcasting.
36
37 x0 = np.zeros(shape0, dtype=int)
38 # Note that multiply.reduce's identity element is 1.0, so when shape1==(),
39 # this gives the desired n==1.
40 n = int(np.multiply.reduce(shape1))
41 x1 = np.arange(n).reshape(shape1)
42 if transposed:
43 x0 = x0.T
44 x1 = x1.T
45 if flipped:
46 x0 = x0[::-1]
47 x1 = x1[::-1]
48 # Use the add ufunc to do the broadcasting. Since we're adding 0s to x1, the
49 # result should be exactly the same as the broadcasted view of x1.
50 y = x0 + x1
51 b0, b1 = broadcast_arrays(x0, x1)
52 assert_array_equal(y, b1)
53
54
55def test_same():

Callers 1

test_same_as_ufuncFunction · 0.85

Calls 4

broadcast_arraysFunction · 0.90
assert_array_equalFunction · 0.90
reshapeMethod · 0.80
reduceMethod · 0.45

Tested by

no test coverage detected