def test_where(self, xp, cond_type, x_type, y_type): m = testing.shaped_random(self.cond_shape, xp, xp.bool_) # Almost all values of a matrix `shaped_random` makes are not zero. # To make a sparse matrix, we need multiply `m`. cond = testing.shaped_random(self.cond_shape, xp, cond_type) * m x = testing.shaped_random(self.x_shape, xp, x_type) y = testing.shaped_random(self.y_shape, xp, y_type) return xp.where(cond, x, y)
def check_int8_sum(self, shape, xp, axis=None, keepdims=False): a = testing.shaped_random(shape, xp, 'b') if xp == cupy: return self.my_sum( a, axis=axis, keepdims=keepdims) else: return a.sum(axis=axis, keepdims=keepdims, dtype='b')
def test_get_and_add_callback(self): N = 100 cupy_arrays = [testing.shaped_random((2, 3)) for _ in range(N)] stream = cuda.Stream(null=True) out = [] for i in range(N): numpy_array = cupy_arrays[i].get(stream=stream) stream.add_callback( lambda _, __, t: out.append(t[0]), (i, numpy_array)) stream.synchronize() self.assertEqual(out, list(range(N)))
def test_nanmin_all(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) return xp.nanmin(a)
def test_argsort_none_axis(self, xp): a = testing.shaped_random((2, 3, 3), xp) return self.argsort(a, axis=None)
def test_count_nonzero(self, xp, dtype): m = testing.shaped_random((2, 3), xp, xp.bool_) a = testing.shaped_random((2, 3), xp, dtype) * m c = xp.count_nonzero(a) self.assertIsInstance(c, int) return c
def test_irfft_overwrite(self, xp, scp, dtype): x = testing.shaped_random(self.shape, xp, dtype) return scp.fftpack.irfft(x, n=self.n, axis=self.axis, overwrite_x=True)
def test_argsort_one_dim(self, xp, dtype): a = testing.shaped_random((10, ), xp, dtype) return self.argsort(a)
def _make_normalized_vector(self, dtype, xp): b = testing.shaped_random((self.n,), xp, dtype=dtype, scale=1) return b / xp.linalg.norm(b)
def test_max_axis1(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return a.max(axis=1)
def test_sort_negative_axis(self, xp): a = testing.shaped_random((2, 3, 3), xp) a.sort(axis=-2) return a
def test_external_sort_none_axis(self, xp): a = testing.shaped_random((2, 3, 3), xp) return xp.sort(a, axis=None)
def test_mul(self, xp, sp): linop = self._generate_linear_operator(xp, sp) x0 = testing.shaped_random((self.N,), xp, self.dtype) x1 = testing.shaped_random((self.N, 1), xp, self.dtype) x2 = testing.shaped_random((self.N, 8), xp, self.dtype) return linop * x0, linop * x1, linop * x2
def _make_random_matrix(self, shape, xp): a = testing.shaped_random(shape, xp, dtype=self.r_dtype, scale=1) if self.dtype.char in 'FD': a = a + 1j * testing.shaped_random( shape, xp, dtype=self.r_dtype, scale=1) return a
def test_dot(self, xp, sp): linop = self._generate_linear_operator(xp, sp) x0 = testing.shaped_random((self.N,), xp, self.dtype) x1 = testing.shaped_random((self.N, 1), xp, self.dtype) x2 = testing.shaped_random((self.N, 8), xp, self.dtype) return linop.dot(x0), linop.dot(x1), linop.dot(x2)
def test_rmatmat(self, xp, sp): linop = self._generate_linear_operator(xp, sp) x = testing.shaped_random((self.M, 8), xp, self.dtype) return linop.rmatmat(x)
def test_rmatvec(self, xp, sp): linop = self._generate_linear_operator(xp, sp) x_1dim = testing.shaped_random((self.M,), xp, self.dtype) x_2dim = testing.shaped_random((self.M, 1), xp, self.dtype) return linop.rmatvec(x_1dim), linop.rmatvec(x_2dim)
def test_nanmin_axis2(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return xp.nanmin(a, axis=2)
def test_sort_invalid_axis2(self): a = testing.shaped_random((2, 3, 3), cupy) with self.assertRaises(numpy.AxisError): a.sort(axis=3)
def test_where_cond(self, xp, dtype): m = testing.shaped_random(self.cond_shape, xp, xp.bool_) cond = testing.shaped_random(self.cond_shape, xp, dtype) * m return xp.where(cond)
def test_external_sort_invalid_negative_axis1(self): for xp in (numpy, cupy): a = testing.shaped_random((2, 3, 3), xp) with pytest.raises(numpy.AxisError): xp.sort(a, axis=-4)
def test_argmin_axis1(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return a.argmin(axis=1)
def test_external_sort_invalid_negative_axis2(self): a = testing.shaped_random((2, 3, 3), cupy) with self.assertRaises(numpy.AxisError): cupy.sort(a, axis=-4)
def test_min_axis_large(self, xp, dtype): a = testing.shaped_random((3, 1000), xp, dtype) return a.min(axis=0)
def test_nan1(self, xp, dtype): a = testing.shaped_random((10, ), xp, dtype) a[2] = a[6] = xp.nan out = xp.sort(a) return out
def test_argsort_zero_dim(self, xp, dtype): a = testing.shaped_random((), xp, dtype) return self.argsort(a)
def test_nan4(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) a[0, 2, 1] = a[1, 0, 3] = xp.nan out = xp.sort(a, axis=2) return out
def test_argsort_multi_dim(self, xp, dtype): a = testing.shaped_random((2, 3, 3), xp, dtype) return self.argsort(a)
def test_external_sort_zero_dim(self): for xp in (numpy, cupy): a = testing.shaped_random((), xp) with pytest.raises(numpy.AxisError): xp.sort(a)
def test_argsort_invalid_axis1(self): for xp in (numpy, cupy): a = testing.shaped_random((2, 3, 3), xp) with pytest.raises(numpy.AxisError): self.argsort(a, axis=3)
def test_lexsort_zero_dim(self): for xp in (numpy, cupy): a = testing.shaped_random((), xp) with pytest.raises(numpy.AxisError): return xp.lexsort(a)
def test_nanmax_axis1(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return xp.nanmax(a, axis=1)
def test_one_argument(self, xp): cond = testing.shaped_random((3, 4), xp, dtype=xp.bool_) x = testing.shaped_random((2, 3, 4), xp, xp.int32) xp.where(cond, x)
def test_nanmin_axis_large(self, xp, dtype): a = testing.shaped_random((3, 1000), xp, dtype) return xp.nanmin(a, axis=0)
def test_argmax_axis_large(self, xp, dtype): a = testing.shaped_random((3, 1000), xp, dtype) return a.argmax(axis=0)
def test_ifft(self, xp, scp, dtype): x = testing.shaped_random(self.shape, xp, dtype) x_orig = x.copy() out = scp.fftpack.ifft(x, n=self.n, axis=self.axis) testing.assert_array_equal(x, x_orig) return out
def test_external_argmin_all(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) return xp.argmin(a)
def test_lexsort_one_dim(self, xp): a = testing.shaped_random((2, ), xp) return xp.lexsort(a)
def _make_matrix(self, dtype, xp): a = testing.shaped_random(self.shape, xp, dtype=dtype) mask = testing.shaped_random(self.shape, xp, dtype='f', scale=1) a[mask > self.density] = 0 return a
def test_argmax_all(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) return a.argmax()
def test_lexsort_three_or_more_dim(self): a = testing.shaped_random((2, 10, 10), cupy) with self.assertRaises(NotImplementedError): return cupy.lexsort(a)
def test_argmax_axis2(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return a.argmax(axis=2)
def test_lexsort_dtype(self, xp, dtype): a = testing.shaped_random((2, 10), xp, dtype) return xp.lexsort(a)
def test_external_argmin_axis_large(self, xp, dtype): a = testing.shaped_random((3, 1000), xp, dtype) return xp.argmin(a, axis=0)
def test_nan3(self, xp, dtype): a = testing.shaped_random((2, 10), xp, dtype) a[1, 2] = a[1, 6] = xp.nan return xp.lexsort(a)
def test_view(self, xp): # from #3232 a = testing.shaped_random((4, 8), xp, dtype=xp.float64) a = a.T[::-1] return xp.lexsort(a)
def test_F_order(self, xp): a = testing.shaped_random((4, 8), xp, dtype=xp.float64) a = xp.asfortranarray(a) assert a.flags.f_contiguous assert not a.flags.c_contiguous return xp.lexsort(a)
def test_min_all(self, xp, dtype): a = testing.shaped_random((2, 3), xp, dtype) return a.min()
def test_sort_two_or_more_dim(self, xp): a = testing.shaped_random((2, 3, 3), xp) a.sort() return a
def test_min_axis2(self, xp, dtype): a = testing.shaped_random((2, 3, 4), xp, dtype) return a.min(axis=2)
def func(xp): m = testing.shaped_random((2, 3, 4), xp, xp.bool_) a = testing.shaped_random((2, 3, 4), xp, dtype) * m return xp.count_nonzero(a, axis=(ax, ay))