def test_imag_inplace(self, dtype): x = cupy.zeros((2, 3), dtype=dtype) # TODO(kmaehashi) The following line should raise error for real # dtypes, but currently ignored silently. x.imag[:] = 1 expected = cupy.zeros( (2, 3), dtype=dtype) + (1j if x.dtype.kind == 'c' else 0) assert cupy.all(x == expected)
def check_distribution(self, dist_func, mean_dtype, cov_dtype, dtype): mean = cupy.zeros(self.d, dtype=mean_dtype) cov = cupy.random.normal(size=(self.d, self.d), dtype=cov_dtype) cov = cov.T.dot(cov) out = dist_func(mean, cov, self.shape, dtype=dtype) self.assertEqual(self.shape + (self.d,), out.shape) self.assertEqual(out.dtype, dtype)
def test_logseries_for_invalid_p(self, p_dtype, dtype): with self.assertRaises(ValueError): cp_params = {'p': cupy.zeros(self.p_shape, dtype=p_dtype)} _distributions.logseries(size=self.shape, dtype=dtype, **cp_params) with self.assertRaises(ValueError): cp_params = {'p': cupy.ones(self.p_shape, dtype=p_dtype)} _distributions.logseries(size=self.shape, dtype=dtype, **cp_params)
def check_distribution(self, dist_func, mean_dtype, cov_dtype): mean = cupy.zeros(self.d, dtype=mean_dtype) cov = cupy.random.normal(size=(self.d, self.d)) # dpnp.dparray doesn't have .dot # TODO # no conversation to ndarray cov = numpy.array(cov) cov = cov.T.dot(cov) cov = cupy.array(cov) out = dist_func(mean, cov, self.shape) self.assertEqual(self.shape + (self.d, ), out.shape) # numpy and dpdp output dtype is float64 self.assertEqual(out.dtype, numpy.float64)
def test_svd(type, shape): a = numpy.arange(shape[0] * shape[1], dtype=type).reshape(shape) ia = inp.array(a) np_u, np_s, np_vt = numpy.linalg.svd(a) dpnp_u, dpnp_s, dpnp_vt = inp.linalg.svd(ia) assert (dpnp_u.dtype == np_u.dtype) assert (dpnp_s.dtype == np_s.dtype) assert (dpnp_vt.dtype == np_vt.dtype) assert (dpnp_u.shape == np_u.shape) assert (dpnp_s.shape == np_s.shape) assert (dpnp_vt.shape == np_vt.shape) if type == numpy.float32: tol = 1e-03 else: tol = 1e-12 # check decomposition dpnp_diag_s = inp.zeros(shape, dtype=dpnp_s.dtype) for i in range(dpnp_s.size): dpnp_diag_s[i, i] = dpnp_s[i] # check decomposition numpy.testing.assert_allclose(ia, inp.dot(dpnp_u, inp.dot(dpnp_diag_s, dpnp_vt)), rtol=tol, atol=tol) # compare singular values # numpy.testing.assert_allclose(dpnp_s, np_s, rtol=tol, atol=tol) # change sign of vectors for i in range(min(shape[0], shape[1])): if np_u[0, i] * dpnp_u[0, i] < 0: np_u[:, i] = -np_u[:, i] np_vt[i, :] = -np_vt[i, :] # compare vectors for non-zero values for i in range(numpy.count_nonzero(np_s > tol)): numpy.testing.assert_allclose(inp.asnumpy(dpnp_u)[:, i], np_u[:, i], rtol=tol, atol=tol) numpy.testing.assert_allclose(inp.asnumpy(dpnp_vt)[i, :], np_vt[i, :], rtol=tol, atol=tol)
def test_empty_like_reshape_contiguity3_cupy_only(self, dtype, order): a = testing.shaped_arange((2, 3, 4), cupy, dtype) # test strides that are both non-contiguous and non-descending a = a[:, ::2, :].swapaxes(0, 1) b = cupy.empty_like(a, order=order, shape=self.shape) b.fill(0) shape = self.shape if not numpy.isscalar(self.shape) else ( self.shape, ) if len(shape) == 1: self.assertTrue(b.flags.c_contiguous) self.assertTrue(b.flags.f_contiguous) elif order in ['k', 'K', None] and len(shape) == a.ndim: self.assertFalse(b.flags.c_contiguous) self.assertFalse(b.flags.f_contiguous) elif order in ['f', 'F']: self.assertFalse(b.flags.c_contiguous) self.assertTrue(b.flags.f_contiguous) else: self.assertTrue(b.flags.c_contiguous) self.assertFalse(b.flags.f_contiguous) c = cupy.zeros(self.shape) c.fill(0) testing.assert_array_equal(b, c)
b = T * mr z = T * sig_sig_two c = 0.25 * z y = 1./np.sqrt(z) w1 = (a - b + c) * y w2 = (a - b - c) * y d1 = 0.5 + 0.5 * np.erf(w1) d2 = 0.5 + 0.5 * np.erf(w2) Se = np.exp(b) * S r = P * d1 - Se * d2 call[:] = r # temporary `r` is necessary for faster `put` computation put[:] = r - P + Se np.random.seed(SEED) price = np.random.uniform(PL, PH, SIZE) strike = np.random.uniform(SL, SH, SIZE) t = np.random.uniform(TL, TH, SIZE) call = np.zeros(SIZE, dtype=DTYPE) put = -np.ones(SIZE, dtype=DTYPE) black_scholes(price, strike, t, RISK_FREE, VOLATILITY, call, put) print(call[:10]) print(put[:10])
def test_real_inplace(self, dtype): x = cupy.zeros((2, 3), dtype=dtype) x.real[:] = 1 expected = cupy.ones((2, 3), dtype=dtype) assert cupy.all(x == expected)
def test_zeros_like_reshape_cupy_only(self, dtype, order): a = testing.shaped_arange((2, 3, 4), cupy, dtype) b = cupy.zeros_like(a, shape=self.shape) c = cupy.zeros(self.shape, order=order, dtype=dtype) testing.assert_array_equal(b, c)
def test_zeros_strides(self, order): a = numpy.zeros((2, 3), dtype='d', order=order) b = cupy.zeros((2, 3), dtype='d', order=order) self.assertEqual(b.strides, a.strides)