def test_frompyfunc_outerloop(self): import sys from numpy import frompyfunc, dtype, arange if '__pypy__' not in sys.builtin_module_names: skip('PyPy only frompyfunc extension') def int_times2(in_array, out_array): assert in_array.dtype == int in_flat = in_array.flat out_flat = out_array.flat for i in range(in_array.size): out_flat[i] = in_flat[i] * 2 def double_times2(in_array, out_array): assert in_array.dtype == float in_flat = in_array.flat out_flat = out_array.flat for i in range(in_array.size): out_flat[i] = in_flat[i] * 2 ufunc = frompyfunc([int_times2, double_times2], 1, 1, signature='()->()', dtypes=[dtype(int), dtype(int), dtype(float), dtype(float) ], stack_inputs=True, ) ai = arange(10, dtype=int) ai2 = ufunc(ai) assert all(ai2 == ai * 2) af = arange(10, dtype=float) af2 = ufunc(af) assert all(af2 == af * 2) ac = arange(10, dtype=complex) skip('casting not implemented yet') ac1 = ufunc(ac)
def test_frompyfunc_2d_sig(self): import sys from numpy import frompyfunc, dtype, arange if "__pypy__" not in sys.builtin_module_names: skip("PyPy only frompyfunc extension") def times_2(in_array, out_array): assert len(in_array.shape) == 2 assert in_array.shape == out_array.shape out_array[:] = in_array * 2 ufunc = frompyfunc( [times_2], 1, 1, signature="(m,n)->(n,m)", dtypes=[dtype(int), dtype(int)], stack_inputs=True ) ai = arange(18, dtype=int).reshape(2, 3, 3) ai3 = ufunc(ai[0, :, :]) ai2 = ufunc(ai) assert (ai2 == ai * 2).all() ufunc = frompyfunc( [times_2], 1, 1, signature="(m,m)->(m,m)", dtypes=[dtype(int), dtype(int)], stack_inputs=True ) ai = arange(12 * 3 * 3, dtype="int32").reshape(12, 3, 3) exc = raises(ValueError, ufunc, ai[:, :, 0]) assert "perand 0 has a mismatch in its core dimension 1" in exc.value.message ai3 = ufunc(ai[0, :, :]) ai2 = ufunc(ai) assert (ai2 == ai * 2).all() # view aiV = ai[::-2, :, :] assert aiV.strides == (-72, 12, 4) ai2 = ufunc(aiV) assert (ai2 == aiV * 2).all()
def test_comparisons(self): import operator from numpy import equal, not_equal, less, less_equal, greater, greater_equal, arange for ufunc, func in [ (equal, operator.eq), (not_equal, operator.ne), (less, operator.lt), (less_equal, operator.le), (greater, operator.gt), (greater_equal, operator.ge), ]: for a, b in [ (3, 3), (3, 4), (4, 3), (3.0, 3.0), (3.0, 3.5), (3.5, 3.0), (3.0, 3), (3, 3.0), (3.5, 3), (3, 3.5), ]: assert ufunc(a, b) == func(a, b) c = arange(10) val = c == "abcdefg" assert val == False
def test_comparisons(self): import operator from numpy import equal, not_equal, less, less_equal, greater, greater_equal for ufunc, func in [ (equal, operator.eq), (not_equal, operator.ne), (less, operator.lt), (less_equal, operator.le), (greater, operator.gt), (greater_equal, operator.ge), ]: for a, b in [ (3, 3), (3, 4), (4, 3), (3.0, 3.0), (3.0, 3.5), (3.5, 3.0), (3.0, 3), (3, 3.0), (3.5, 3), (3, 3.5), ]: assert ufunc(a, b) is func(a, b)
def test_comparisons(self): import operator from numpy import (equal, not_equal, less, less_equal, greater, greater_equal, arange) for ufunc, func in [ (equal, operator.eq), (not_equal, operator.ne), (less, operator.lt), (less_equal, operator.le), (greater, operator.gt), (greater_equal, operator.ge), ]: for a, b in [ (3, 3), (3, 4), (4, 3), (3.0, 3.0), (3.0, 3.5), (3.5, 3.0), (3.0, 3), (3, 3.0), (3.5, 3), (3, 3.5), ]: assert ufunc(a, b) == func(a, b) c = arange(10) val = c == 'abcdefg' assert val == False
def test_frompyfunc_needs_nditer(self): import sys from numpy import frompyfunc, dtype, arange if "__pypy__" not in sys.builtin_module_names: skip("PyPy only frompyfunc extension") def summer(in0): print "in summer, in0=", in0, "in0.shape=", in0.shape return in0.sum() ufunc = frompyfunc([summer], 1, 1, signature="(m,m)->()", dtypes=[dtype(int), dtype(int)], stack_inputs=False) ai = arange(12, dtype=int).reshape(3, 2, 2) ao = ufunc(ai) assert ao.size == 3
def test_frompyfunc_2d_sig(self): import sys from numpy import frompyfunc, dtype, arange if '__pypy__' not in sys.builtin_module_names: skip('PyPy only frompyfunc extension') def times_2(in_array, out_array): assert len(in_array.shape) == 2 assert in_array.shape == out_array.shape out_array[:] = in_array * 2 ufunc = frompyfunc([times_2], 1, 1, signature='(m,n)->(n,m)', dtypes=[dtype(int), dtype(int)], stack_inputs=True, ) ai = arange(18, dtype=int).reshape(2,3,3) ai3 = ufunc(ai[0,:,:]) ai2 = ufunc(ai) assert (ai2 == ai * 2).all() ufunc = frompyfunc([times_2], 1, 1, signature='(m,m)->(m,m)', dtypes=[dtype(int), dtype(int)], stack_inputs=True, ) ai = arange(12*3*3, dtype='int32').reshape(12,3,3) exc = raises(ValueError, ufunc, ai[:,:,0]) assert "perand 0 has a mismatch in its core dimension 1" in exc.value.message ai3 = ufunc(ai[0,:,:]) ai2 = ufunc(ai) assert (ai2 == ai * 2).all() # view aiV = ai[::-2, :, :] assert aiV.strides == (-72, 12, 4) ai2 = ufunc(aiV) assert (ai2 == aiV * 2).all()
def test_frompyfunc_needs_nditer(self): import sys from numpy import frompyfunc, dtype, arange if '__pypy__' not in sys.builtin_module_names: skip('PyPy only frompyfunc extension') def summer(in0): print 'in summer, in0=',in0,'in0.shape=',in0.shape return in0.sum() ufunc = frompyfunc([summer], 1, 1, signature='(m,m)->()', dtypes=[dtype(int), dtype(int)], stack_inputs=False, ) ai = arange(12, dtype=int).reshape(3, 2, 2) ao = ufunc(ai) assert ao.size == 3