def test_ifft_2(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) a = xp.asarray(a, order=self.data_order) if xp == np and self.axis > a.ndim - 1: raise ValueError out = xp.fft.ifft(a, norm=self.norm, n=self.n, axis=self.axis) if xp == np and dtype in [np.float32, np.complex64]: out = out.astype(np.complex64) return out
def test_irfft2(self, xp, dtype, order): a = testing.shaped_random(self.shape, xp, dtype) a = xp.asarray(a, order=order) out = xp.fft.irfft2(a, s=self.s, norm=self.norm) if xp == np and dtype is np.float32: out = out.astype(np.float64) if xp == np and dtype is np.complex64: out = out.astype(np.float32) return out
def test_view(self, xp): data = testing.shaped_random([4], xp, dtype=numpy.float32) mask = [1, 1, 0, 1] fill_value = xp.arange(4) a = xp.ma.array(data, mask=mask, fill_value=fill_value) b = a.view(dtype=numpy.int32) b.data[0] = 100 b.mask[0] = 1 b.fill_value = 10 return a
def test_rfft_move(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) ma = xp.moveaxis(a, self.move_1, self.move_2) out = xp.fft.rfft(ma) if xp == np and dtype is np.float32: out = out.astype(np.complex64) elif xp == np and dtype is not np.float32: out = out.astype(np.complex128) return out
def test_copyto_other_dst1(self, xp, src_dtype, dst_dtype, dst_order, src_order): if numpy.can_cast(src_dtype, dst_dtype): dst = xp.empty( (2, 4, 3), dtype=dst_dtype, order=dst_order).transpose(0, 2, 1) src = xp.asarray( testing.shaped_random((2, 3, 4), xp, src_dtype), order=src_order) xp.copyto(dst, src) return dst else: return -1
def test_irfft_move_slice(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) ma = xp.moveaxis(a, self.move_1, self.move_2) out = xp.fft.irfft(ma[1:, 1:]) if xp == np and dtype is np.complex64: out = out.astype(np.float32) elif xp == np and dtype is not np.complex64: out = out.astype(np.float64) return out
def test_cholesky_single(self, xp, dtype, order): a = testing.shaped_random(self.shape, xp, dtype) m = a.shape[-1] a = a.reshape(-1, m, m) for i in range(a.shape[0]): a[i] = numpy.dot(a[i], xp.conjugate(a[i].T)) a[i] += numpy.eye(a[i].shape[0], a[i].shape[1], dtype=a.dtype) * 100 a = xp.asarray(a.reshape(self.shape), order=order) return xp.linalg.cholesky(a)
def test_stack_err_out_dtype(self, xp, in_dtype, out_dtype): if not numpy.can_cast(in_dtype, out_dtype, casting='same_kind'): arrays = [ testing.shaped_random((2, 3, 4), xp, in_dtype) for i in range(5) ] out = xp.empty((5, 2, 3, 4), dtype=out_dtype) return xp.stack(arrays, out=out) else: raise DummyError()
def test_rfft(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) out = xp.fft.rfft(a, n=self.n, norm=self.norm) if xp == np and dtype is np.float32: out = out.astype(np.complex64) elif xp == np and dtype is not np.float32: out = out.astype(np.complex128) return out
def test_ifft2(self, xp, dtype, order, enable_nd): global enable_nd_planning assert enable_nd_planning == enable_nd a = testing.shaped_random(self.shape, xp, dtype) a = xp.asarray(a, order=order) a = _numpy_fftn_correct_dtype(xp, a) out = xp.fft.ifft2(a, s=self.s, norm=self.norm) if xp == np and dtype in [np.float16, np.float32, np.complex64]: out = out.astype(np.complex64) return out
def test_astype_type_f_contiguous_no_copy(self, dtype, order): data = testing.shaped_arange((2, 3, 4), nlcpy) mask = testing.shaped_random((2, 3, 4), nlcpy, dtype=numpy.bool_) fill_value = testing.shaped_arange((2, 3, 4), nlcpy) + 1 a = nlcpy.ma.array(data, mask=mask, fill_value=fill_value, dtype=dtype, order='F') b = a.astype(dtype, order=order, copy=False) self.assertTrue(b is a)
def test_inv(self, xp, dtype, order): if dtype != numpy.bool_: a = xp.asarray(testing.shaped_random(self.shape, xp, dtype), order=order) else: n = self.shape[-1] a = xp.empty(self.shape) a[..., :, :] = xp.eye(n) args = dict() args["a"] = a return xp.linalg.inv(**args)
def test_copyto_ndarray_broadcast(self, xp, dst_dtype, src_dtype, dst_order, src_order): if numpy.can_cast(src_dtype, dst_dtype): src_shape, dst_shape = self.pat_shapes dst = xp.empty(dst_shape, dtype=dst_dtype, order=dst_order) src = xp.asarray( testing.shaped_random(src_shape, xp, src_dtype), order=src_order) xp.copyto(dst, src) return dst else: return -1
def test_take_with_masked_array_out(self, xp): data = testing.shaped_random([2, 4, 3], xp) mask = testing.shaped_arange([2, 4, 3], xp) % 2 a = xp.ma.array(data, mask=mask) idx = xp.array([[1, 3], [2, 0]]) data = xp.empty([2, 2, 2, 3]) mask = xp.zeros([2, 2, 2, 3]) mask[0] = 1 out = xp.ma.array(data, mask=mask, dtype=numpy.float32) a.take(idx, axis=1, out=out) return out
def test_stack_out2(self, xp, in_dtype, out_dtype): if numpy.can_cast(in_dtype, out_dtype, casting='same_kind'): arrays = [ testing.shaped_random((2, 3, 4), xp, in_dtype) for i in range(5) ] out = xp.empty((2, 5, 3, 4), dtype=out_dtype) axis = 1 return xp.stack(arrays, axis=axis, out=out) else: return 0
def test_norm_vector(self, xp, dtype, order): shape, axis = self.pat x = xp.asarray(testing.shaped_random(shape, xp, dtype), order=order) args = dict() args["x"] = x if self.ord is not None: args["ord"] = self.ord if axis is not None: args["axis"] = axis if self.keepdims: args["keepdims"] = self.keepdims return xp.linalg.norm(**args)
def test_irfft(self, xp, dtype): if self.n is None and self.axis == -1: raise Exception("ignore case") a = testing.shaped_random(self.shape, xp, dtype) out = xp.rfft.irfft(a, n=self.n, axis=self.axis) if xp == np and dtype is np.complex64: out = out.astype(np.float32) elif xp == np and dtype is not np.complex64: out = out.astype(np.float64) return out
def test_irfft_1(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) a = xp.asarray(a, order=self.data_order) if xp == np and self.axis > a.ndim - 1: raise ValueError out = xp.fft.irfft(a, axis=self.axis, n=self.n, norm=self.norm) if xp == np and dtype is np.complex64: out = out.astype(np.float32) elif xp == np and dtype is not np.complex64: out = out.astype(np.float64) return out
def test_ihfft(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) out = xp.fft.ihfft(a, n=self.n, axis=self.axis, norm=self.norm) # if xp == np and dtype in [np.float16, np.float32, np.complex64]: # out = out.astype(np.complex64) if out.dtype in [np.float16, np.float32]: out = out.astype(np.float64) if out.dtype in [np.complex64]: out = out.astype(np.complex128) return out
def test_rfft2(self, xp, dtype, order): # the scaling of old Numpy is incorrect if np.__version__ < np.lib.NumpyVersion('1.13.0'): if self.s is not None: return xp.empty(0) a = testing.shaped_random(self.shape, xp, dtype) a = xp.asarray(a, order=order) out = xp.fft.rfft2(a, s=self.s, norm=self.norm) if xp == np and dtype is np.float32: out = out.astype(np.complex64) return out
def test_unique(self, xp, dtype, order): if len(self.shape) == 1 and self.axis == 1: return 0 shape = list(self.shape) idx = 0 if self.axis is None else self.axis shape[idx] = int(shape[idx] / 2) a = xp.asarray(testing.shaped_random(shape, xp, dtype), order=order) a = xp.tile(a, 2) return xp.unique(a, axis=self.axis, return_index=self.index, return_inverse=self.inverse, return_counts=self.count)
def test_qr_double(self, xp, dtype, order): a = xp.asarray(testing.shaped_random(self.shape, xp, dtype), order=order) if self.mode == 'complete': q, r = xp.linalg.qr(a, self.mode) m, n = a.shape k = min(m, n) q[:, k:m] = 0 r[k:m] = 0 return q, r elif self.mode is not None: return xp.linalg.qr(a, mode=self.mode) else: return xp.linalg.qr(a)
def test_rfft_2(self, xp, dtype): a = testing.shaped_random(self.shape, xp, dtype) a = xp.asarray(a, order=self.data_order) if xp == np and self.axis >= len(self.shape) and self.norm == 'ortho': raise ValueError if xp == np and self.axis > a.ndim - 1: raise ValueError out = xp.fft.rfft(a, norm=self.norm, n=self.n, axis=self.axis) if xp == np and dtype is np.float32: out = out.astype(np.complex64) elif xp == np and dtype is not np.float32: out = out.astype(np.complex128) return out
def test_4d_hypervolumetric(self): nlcpy.random.seed(0) xin = testing.shaped_random(self.shape, nlcpy).astype(self.dtype) rtol = TOL_SINGLE if self.dtype == numpy.float32 else TOL_DOUBLE sca_res, sca_out = compute_with_sca( self.type, xin, self.stencil_scale, is_out=self.is_out, optimize=self.optimize, ) naive_res = compute_with_naive(self.type, xin, self.stencil_scale) if self.is_out: assert id(sca_res) == id(sca_out) testing.assert_allclose(sca_res, naive_res, rtol=rtol)
def test_copy(self, xp, dtype, order): data = numpy.arange(12, dtype=dtype).reshape(3, 4, order=self.src_order) mask = testing.shaped_random((3, 4), numpy, dtype=numpy.bool_) fill_value = numpy.arange(1, 13, dtype=dtype).reshape(3, 4, order=self.src_order) a = xp.ma.array(data, mask=mask, fill_value=fill_value, order=self.src_order, hard_mask=True) b = a.copy(order=order) return b
def test_qr_single(self, xp, dtype, order): a = xp.asarray(testing.shaped_random(self.shape, xp, dtype), order=order) args = dict() args["a"] = a if self.mode is not None: args["mode"] = self.mode if self.mode == 'complete': q, r = xp.linalg.qr(**args) m, n = a.shape k = min(m, n) q[:, k:m] = 0 r[k:m] = 0 return q, r else: return xp.linalg.qr(**args)
def test_isinstance_numpy_view_copy_f(self, xp, dtype, order): data = numpy.arange(100, dtype=dtype).reshape(10, 10, order=self.src_order) mask = testing.shaped_random((10, 10), numpy, dtype=numpy.bool_) fill_value = numpy.arange(1, 101, dtype=dtype).reshape(10, 10, order=self.src_order) a = numpy.ma.array(data, mask=mask, fill_value=fill_value, order=self.src_order) a = a[2:5, 1:8] b = xp.ma.array(xp.empty(a.shape), dtype=dtype, order=order) b[:] = a return b
def test_eig(self, xp, dtype, order): a = xp.asarray(testing.shaped_random(self.shape, xp, dtype), order=order) args = dict() args["a"] = a w, v = xp.linalg.eig(**args) ret = [w.shape, v.shape, v.dtype, v.flags.c_contiguous, v.flags.f_contiguous] m = a.shape[-1] a = a.reshape([-1, m, m]) w = w.reshape([-1, m]) v = v.reshape([-1, m, m]) if type(a) is nlcpy.ndarray: a = a.get() w = w.get() v = v.get() x = numpy.array([numpy.dot(a[i], v[i]) for i in range(a.shape[0])]) y = numpy.array([w[i] * v[i] for i in range(a.shape[0])]) tol = 1e-5 if a.dtype.char in 'fF' else 1e-12 numpy.testing.assert_allclose(x, y, atol=tol, rtol=tol) return ret
def test_svd_general_compute_uv(self, xp, dtype, order): a = xp.asarray(testing.shaped_random(self.shape, xp, dtype), order=order) args = dict() args["a"] = a if not self.full_matrices: args["full_matrices"] = self.full_matrices u, s, vh = xp.linalg.svd(**args) ret = [(i.shape, i.dtype, i.flags.c_contiguous, i.flags.f_contiguous) for i in (u, s, vh)] if a.size == 0: return ret if self.full_matrices: min_mn = min(self.shape[-1], self.shape[-2]) u = u[..., :self.shape[-2], :min_mn] vh = vh[..., :min_mn, :self.shape[-1]] if type(u) is nlcpy.ndarray: u = u.get() s = s.get() vh = vh.get() x = numpy.matmul((u * s[..., None, :]), vh) tol = 1e-5 if a.dtype.char in 'fF' else 1e-12 numpy.testing.assert_allclose(a, x, atol=tol, rtol=tol) return ret
def test_count_nonzero(self, xp, dtype, order): a = xp.ones(self.shape, dtype=dtype, order=order) mask = testing.shaped_random(self.shape, xp, dtype='?') return xp.count_nonzero(a * mask, axis=self.axis)