def test_blocked(self): # test alignments offsets for simd instructions # alignments for vz + 2 * (vs - 1) + 1 for dt, sz in [(np.float32, 11), (np.float64, 7), (np.int32, 11)]: for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt, type='binary', max_size=sz): exp1 = np.ones_like(inp1) inp1[...] = np.ones_like(inp1) inp2[...] = np.zeros_like(inp2) assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg) assert_almost_equal(np.add(inp1, 2), exp1 + 2, err_msg=msg) assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg) np.add(inp1, inp2, out=out) assert_almost_equal(out, exp1, err_msg=msg) inp2[...] += np.arange(inp2.size, dtype=dt) + 1 assert_almost_equal(np.square(inp2), np.multiply(inp2, inp2), err_msg=msg) # skip true divide for ints if dt != np.int32 or (sys.version_info.major < 3 and not sys.py3kwarning): assert_almost_equal(np.reciprocal(inp2), np.divide(1, inp2), err_msg=msg) inp1[...] = np.ones_like(inp1) np.add(inp1, 2, out=out) assert_almost_equal(out, exp1 + 2, err_msg=msg) inp2[...] = np.ones_like(inp2) np.add(2, inp2, out=out) assert_almost_equal(out, exp1 + 2, err_msg=msg)
def test_blocked(self): # test alignments offsets for simd instructions # alignments for vz + 2 * (vs - 1) + 1 for dt, sz in [(np.float32, 11), (np.float64, 7)]: for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt, type='binary', max_size=sz): exp1 = np.ones_like(inp1) inp1[...] = np.ones_like(inp1) inp2[...] = np.zeros_like(inp2) assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg) assert_almost_equal(np.add(inp1, 1), exp1 + 1, err_msg=msg) assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg) np.add(inp1, inp2, out=out) assert_almost_equal(out, exp1, err_msg=msg) inp2[...] += np.arange(inp2.size, dtype=dt) + 1 assert_almost_equal(np.square(inp2), np.multiply(inp2, inp2), err_msg=msg) assert_almost_equal(np.reciprocal(inp2), np.divide(1, inp2), err_msg=msg) inp1[...] = np.ones_like(inp1) inp2[...] = np.zeros_like(inp2) np.add(inp1, 1, out=out) assert_almost_equal(out, exp1 + 1, err_msg=msg) np.add(1, inp2, out=out) assert_almost_equal(out, exp1, err_msg=msg)
def test_abs_blocked(self): "simd tests on abs" for dt in [np.float32, np.float64]: for out, inp, msg in _gen_alignment_data(dtype=dt, type='unary', max_size=17): tgt = [ncu.absolute(i) for i in inp] np.absolute(inp, out=out) assert_equal(out, tgt, err_msg=msg) self.assertTrue((out >= 0).all()) # will throw invalid flag depending on compiler optimizations with np.errstate(invalid='ignore'): for v in [np.nan, -np.inf, np.inf]: for i in range(inp.size): d = np.arange(inp.size, dtype=dt) inp[:] = -d inp[i] = v d[i] = -v if v == -np.inf else v assert_array_equal(np.abs(inp), d, err_msg=msg) np.abs(inp, out=out) assert_array_equal(out, d, err_msg=msg) assert_array_equal(-inp, -1 * inp, err_msg=msg) np.negative(inp, out=out) assert_array_equal(out, -1 * inp, err_msg=msg)
def test_power_float(self): x = np.array([1.0, 2.0, 3.0]) assert_equal(x ** 0, [1.0, 1.0, 1.0]) assert_equal(x ** 1, x) assert_equal(x ** 2, [1.0, 4.0, 9.0]) y = x.copy() y **= 2 assert_equal(y, [1.0, 4.0, 9.0]) assert_almost_equal(x ** (-1), [1.0, 0.5, 1.0 / 3]) assert_almost_equal(x ** (0.5), [1.0, ncu.sqrt(2), ncu.sqrt(3)]) for out, inp, msg in _gen_alignment_data(dtype=np.float32, type="unary"): exp = [ncu.sqrt(i) for i in inp] assert_almost_equal(inp ** (0.5), exp, err_msg=msg) np.sqrt(inp, out=out) assert_equal(out, exp, err_msg=msg) for out, inp, msg in _gen_alignment_data(dtype=np.float64, type="unary"): exp = [ncu.sqrt(i) for i in inp] assert_almost_equal(inp ** (0.5), exp, err_msg=msg) np.sqrt(inp, out=out) assert_equal(out, exp, err_msg=msg)
def test_minmax_blocked(self): "simd tests on max/min" for dt in [np.float32, np.float64]: for out, inp, msg in _gen_alignment_data(dtype=dt, type="unary", max_size=17): for i in range(inp.size): inp[:] = np.arange(inp.size, dtype=dt) inp[i] = np.nan self.assertTrue(np.isnan(inp.max()), msg=repr(inp) + "\n" + msg) self.assertTrue(np.isnan(inp.min()), msg=repr(inp) + "\n" + msg) inp[i] = 1e10 assert_equal(inp.max(), 1e10, err_msg=msg) inp[i] = -1e10 assert_equal(inp.min(), -1e10, err_msg=msg)
def test_power_float(self): x = np.array([1., 2., 3.]) assert_equal(x**0, [1., 1., 1.]) assert_equal(x**1, x) assert_equal(x**2, [1., 4., 9.]) y = x.copy() y **= 2 assert_equal(y, [1., 4., 9.]) assert_almost_equal(x**(-1), [1., 0.5, 1. / 3]) assert_almost_equal(x**(0.5), [1., ncu.sqrt(2), ncu.sqrt(3)]) for out, inp, msg in _gen_alignment_data(dtype=np.float32, type='unary'): exp = [ncu.sqrt(i) for i in inp] assert_almost_equal(inp**(0.5), exp, err_msg=msg) np.sqrt(inp, out=out) assert_equal(out, exp, err_msg=msg) for out, inp, msg in _gen_alignment_data(dtype=np.float64, type='unary'): exp = [ncu.sqrt(i) for i in inp] assert_almost_equal(inp**(0.5), exp, err_msg=msg) np.sqrt(inp, out=out) assert_equal(out, exp, err_msg=msg)
def test_minmax_blocked(self): "simd tests on max/min" for dt in [np.float32, np.float64]: for out, inp, msg in _gen_alignment_data(dtype=dt, type='unary', max_size=17): for i in range(inp.size): inp[:] = np.arange(inp.size, dtype=dt) inp[i] = np.nan self.assertTrue(np.isnan(inp.max()), msg=repr(inp) + '\n' + msg) self.assertTrue(np.isnan(inp.min()), msg=repr(inp) + '\n' + msg) inp[i] = 1e10 assert_equal(inp.max(), 1e10, err_msg=msg) inp[i] = -1e10 assert_equal(inp.min(), -1e10, err_msg=msg)
def test_abs_blocked(self): "simd tests on abs" for dt in [np.float32, np.float64]: for out, inp, msg in _gen_alignment_data(dtype=dt, type="unary", max_size=17): tgt = [ncu.absolute(i) for i in inp] np.absolute(inp, out=out) assert_equal(out, tgt, err_msg=msg) self.assertTrue((out >= 0).all()) # will throw invalid flag depending on compiler optimizations with np.errstate(invalid="ignore"): for v in [np.nan, -np.inf, np.inf]: for i in range(inp.size): d = np.arange(inp.size, dtype=dt) inp[:] = -d inp[i] = v d[i] = -v if v == -np.inf else v assert_array_equal(np.abs(inp), d, err_msg=msg) np.abs(inp, out=out) assert_array_equal(out, d, err_msg=msg)