def test_binary_cast_scalar_scalar_with_out_with_where( self, xp, op, in1, in2, out, where): out = xp.array(out) where = xp.array(where) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): with numpy.warnings.catch_warnings(): numpy.warnings.simplefilter('ignore', numpy.ComplexWarning) func = getattr(xp, op) y = func(in1, in2, out=out, where=where) if xp is numpy and op in float16_op_set: if isinstance(in1, bool) and \ isinstance(in2, bool): y = func(in1, in2, out=out, where=where, dtype='f4') return y if xp is nlcpy: nlcpy.request.flush() return y
def test_binary_cast_array_array_with_out_with_where_broadcast( self, xp, op, in1, in2, out, where): in1 = xp.array(in1) in2 = xp.array(in2) out = xp.array(out) where = xp.array(where) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): with numpy.warnings.catch_warnings(): numpy.warnings.simplefilter('ignore', numpy.ComplexWarning) func = getattr(xp, op) y = func(in1, in2, out=out, where=where) if xp is numpy and op in float16_op_set: if in1.dtype.kind == 'b' and in2.dtype.kind == 'b': y = func(in1, in2, out=out, where=where, dtype='f4') return y if xp is nlcpy: nlcpy.request.flush() return y
def test_unary_cast_scalar_with_dtype(self, xp, op, in1, dtype): with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, dtype=dtype) if xp is nlcpy: nlcpy.request.flush() return y
def check_typecast(self, val, dtype): operators = [ operator.add, operator.sub, operator.mul, operator.truediv] for op in operators: with testing.NumpyError(divide='ignore', invalid='ignore'): a = op(val, (testing.shaped_arange((5,), numpy, dtype) - 2)) b = op(val, (testing.shaped_arange((5,), nlcpy, dtype) - 2)) self.assertEqual(a.dtype, b.dtype)
def test_unary_cast_array(self, xp, op, in1): in1 = xp.array(in1) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1) if xp is nlcpy: nlcpy.request.flush() return y
def test_unary_cast_array_with_out_with_dtype(self, xp, op, in1, out, dtype): in1 = xp.array(in1) out = xp.array(out) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, out=out, dtype=dtype) if xp is nlcpy: nlcpy.request.flush() return y
def test_binary_cast_scalar_scalar_with_out_with_dtype( self, xp, op, in1, in2, out): out = xp.array(out) dtype = out.dtype with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, in2, out=out, dtype=dtype) if xp is nlcpy: nlcpy.request.flush() return y
def test_unary_cast_scalar_with_out(self, xp, op, in1, out): out = xp.array(out) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, out=out) if xp is numpy and op in float16_op_set: if isinstance(in1, bool): y = func(in1, out=out, dtype='f4') return y if xp is nlcpy: nlcpy.request.flush() return y
def test_unary_cast_scalar_with_out_with_where_with_dtype_broadcast( self, xp, op, in1, out, where, dtype): out = xp.array(out) where = xp.array(where) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): with numpy.warnings.catch_warnings(): numpy.warnings.simplefilter('ignore', numpy.ComplexWarning) func = getattr(xp, op) y = func(in1, out=out, where=where, dtype=dtype) if xp is nlcpy: nlcpy.request.flush() return y
def test_unary_cast_array_with_out_broadcast(self, xp, op, in1, out): in1 = xp.array(in1) out = xp.array(out) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, out=out) if xp is numpy and op in float16_op_set: if in1.dtype == numpy.dtype('bool'): y = func(in1, out=out, dtype='f4') return y if xp is nlcpy: nlcpy.request.flush() return y
def check_typecast(self, val, dtype): operators = [ operator.add, operator.sub, operator.mul, operator.truediv ] mask = [0, 1, 0, 1, 0] nval = testing.shaped_arange((5, ), numpy, dtype) - 2 vval = testing.shaped_arange((5, ), nlcpy, dtype) - 2 nval = numpy.ma.array(nval, mask=mask) vval = nlcpy.ma.array(vval, mask=mask) for op in operators: with testing.NumpyError(divide='ignore', invalid='ignore'): a = op(val, nval) b = op(val, vval) self.assertEqual(a.dtype, b.dtype)
def test_binary_broadcast_array_array_with_where(self, xp, op, in1, in2, out, where): in1 = xp.array(in1) in2 = xp.array(in2) out = xp.array(out) where = xp.array(where) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, in2, out=out, where=where) if xp is nlcpy: nlcpy.request.flush() return y
def test_binary(self, xp): arg1 = self.arg1 arg2 = self.arg2 np1 = numpy.asarray(arg1) np2 = numpy.asarray(arg2) dtype1 = np1.dtype dtype2 = np2.dtype if self.name == 'power': # TODO: Fix this: xp.power(0j, 0) # numpy => 1+0j # nlcpy => nan + nanj c_arg1 = dtype1 in complex_types if c_arg1 and (np1 == 0j).any() and (np2 == 0).any(): return xp.array(True) if isinstance(arg1, numpy.ndarray): arg1 = xp.asarray(arg1) if isinstance(arg2, numpy.ndarray): arg2 = xp.asarray(arg2) # NumPy>=1.13.0 does not support subtraction between booleans # TODO: Write a separate test to check both NumPy and nlcpy # raise TypeError. if testing.numpy_satisfies('>=1.13.0') and self.name == 'subtract': if dtype1 == numpy.bool_ and dtype2 == numpy.bool_: return xp.array(True) func = getattr(xp, self.name) with testing.NumpyError(divide='ignore'): with numpy.warnings.catch_warnings(): numpy.warnings.filterwarnings('ignore') y = func(arg1, arg2) # NumPy returns different values (nan/inf) on division by zero # depending on the architecture. # As it is not possible for nlcpy to replicate this behavior, we ignore # the difference here. if self.name in ('floor_divide', 'remainder'): if y.dtype in (float_types + complex_types) and (np2 == 0).any(): y = xp.asarray(y) y[y == numpy.inf] = numpy.nan y[y == -numpy.inf] = numpy.nan return y
def test_binary_cast_array_array_with_out(self, xp, op, in1, in2, out): in1 = xp.array(in1) in2 = xp.array(in2) out = xp.array(out) with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): func = getattr(xp, op) y = func(in1, in2, out=out) if xp is numpy and op in float16_op_set: if in1.dtype.kind == 'b' and in2.dtype.kind == 'b': y = func(in1, in2, out=out, dtype='f4') return y if xp is nlcpy: nlcpy.request.flush() return y
def test_binary_cast_array_array_with_out_with_where_with_dtype( self, xp, op, in1, in2, out, where): in1 = xp.array(in1) in2 = xp.array(in2) out = xp.array(out) where = xp.array(where) dtype = out.dtype with testing.NumpyError(divide='ignore'): with testing.NlcpyError(divide='ignore', over='ignore', invalid='ignore'): with numpy.warnings.catch_warnings(): numpy.warnings.simplefilter('ignore', numpy.ComplexWarning) func = getattr(xp, op) y = func(in1, in2, out=out, where=where, dtype=dtype) if xp is nlcpy: nlcpy.request.flush() return y
def test_imod_array(self): with testing.NumpyError(divide='ignore', invalid='ignore'): self.check_array_array_op(operator.imod)
def test_ifloordiv_scalar(self): with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.ifloordiv, no_complex=True)
def test_itruediv_array(self): with testing.NumpyError(divide='ignore'): self.check_array_array_op(operator.itruediv)
def test_floordiv_array(self): with testing.NumpyError(divide='ignore'): self.check_array_array_op(operator.floordiv, no_complex=True)
def test_broadcasted_itruediv(self): with testing.NumpyError(divide='ignore'): self.check_array_broadcasted_op(operator.itruediv)
def test_broadcasted_ifloordiv(self): if '1.16.1' <= numpy.lib.NumpyVersion(numpy.__version__) < '1.18.0': self.skipTest("NumPy Issue #12927") with testing.NumpyError(divide='ignore'): self.check_array_broadcasted_op(operator.ifloordiv, no_complex=True)
def test_doubly_broadcasted_floordiv(self): with testing.NumpyError(divide='ignore'): self.check_array_doubly_broadcasted_op(operator.floordiv, no_complex=True)
def test_log2(self): with testing.NumpyError(divide='ignore'): self.check_unary('log2', no_complex=True)
def test_itruediv_array(self): with testing.NumpyError(divide='ignore'): self.check_ma_ndarray_op('__itruediv__')
def test_mod_scalar(self): with testing.NumpyError(divide='ignore', invalid='ignore'): self.check_array_scalar_op(operator.mod)
def test_rmod_scalarzero(self): with testing.NumpyError(divide='ignore', invalid='ignore'): self.check_array_scalarzero_op(operator.mod, swap=True)
def test_truediv_ma(self): with testing.NumpyError(divide='ignore'): self.check_ma_ma_op('__truediv__')
def test_doubly_broadcasted_mod(self): with testing.NumpyError(divide='ignore', invalid='ignore'): self.check_array_doubly_broadcasted_op(operator.mod)
def test_truediv_scalar(self): with testing.NumpyError(divide='ignore'): self.check_array_scalar_op(operator.truediv)
def test_log(self): with testing.NumpyError(divide='ignore'): self.check_unary('log')