def shape(self, newshape): """Set new lengths of axes. A tuple of numbers represents size of each dimention. It involves reshaping without copy. If the array cannot be reshaped without copy, it raises an exception. .. seealso: :attr:`numpy.ndarray.shape` """ dpnp.reshape(self, newshape)
def test_multi_dot(type): n = 16 a = inp.reshape(inp.arange(n, dtype=type), (4, 4)) b = inp.reshape(inp.arange(n, dtype=type), (4, 4)) c = inp.reshape(inp.arange(n, dtype=type), (4, 4)) d = inp.reshape(inp.arange(n, dtype=type), (4, 4)) a1 = numpy.arange(n, dtype=type).reshape((4, 4)) b1 = numpy.arange(n, dtype=type).reshape((4, 4)) c1 = numpy.arange(n, dtype=type).reshape((4, 4)) d1 = numpy.arange(n, dtype=type).reshape((4, 4)) result = inp.linalg.multi_dot([a, b, c, d]) expected = numpy.linalg.multi_dot([a1, b1, c1, d1]) numpy.testing.assert_array_equal(expected, result)
def reshape(self, d0, *dn, order=b'C'): """ Returns an array containing the same data with a new shape. Refer to `dpnp.reshape` for full documentation. .. seealso:: :meth:`numpy.ndarray.reshape` Notes ----- Unlike the free function `dpnp.reshape`, this method on `ndarray` allows the elements of the shape parameter to be passed in as separate arguments. For example, ``a.reshape(10, 11)`` is equivalent to ``a.reshape((10, 11))``. """ if dn: if not isinstance(d0, int): msg_tmpl = "'{}' object cannot be interpreted as an integer" raise TypeError(msg_tmpl.format(type(d0).__name__)) shape = [d0, *dn] else: shape = d0 shape_tup = dpnp.dpnp_utils._object_to_tuple(shape) return dpnp.reshape(self, shape_tup)
def test_matmul2(self): array_data1 = [1., 2., 3., 4., 5., 6.] array_data2 = [1., 2., 3., 4., 5., 6., 7., 8.] # DPNP array1 = inp.reshape(inp.array(array_data1, dtype=inp.float64), (3, 2)) array2 = inp.reshape(inp.array(array_data2, dtype=inp.float64), (2, 4)) result = inp.matmul(array1, array2) # print(result) # original array_1 = numpy.array(array_data1, dtype=numpy.float64).reshape((3, 2)) array_2 = numpy.array(array_data2, dtype=numpy.float64).reshape((2, 4)) expected = numpy.matmul(array_1, array_2) # print(expected) numpy.testing.assert_array_equal(expected, result)
def test_strides_true_devide(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a.T + 1 dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa.T + 1 result = dpnp.fmod(dpa, dpb) expected = numpy.fmod(a, b) numpy.testing.assert_allclose(result, expected)
def test_strides_copysign(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = -a.T dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpnp.negative(dpa.T) result = dpnp.copysign(dpa, dpb) expected = numpy.copysign(a, b) numpy.testing.assert_allclose(result, expected)
def test_strides_tan(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] result = dpnp.tan(dpb) expected = numpy.tan(b) numpy.testing.assert_allclose(result, expected, rtol=1e-06)
def test_concatenate(self): # Test concatenate function # One sequence returns unmodified (but as array) r4 = list(range(4)) numpy.testing.assert_array_equal(dpnp.concatenate((r4, )), r4) # Any sequence numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), )), r4) numpy.testing.assert_array_equal(dpnp.concatenate((dpnp.array(r4), )), r4) # 1D default concatenation r3 = list(range(3)) numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3)), r4 + r3) # Mixed sequence types numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), r3)), r4 + r3) numpy.testing.assert_array_equal( dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3) # Explicit axis specification numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), 0), r4 + r3) # Including negative numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), -1), r4 + r3) # 2D a23 = dpnp.array([[10, 11, 12], [13, 14, 15]]) a13 = dpnp.array([[0, 1, 2]]) res = dpnp.array([[10, 11, 12], [13, 14, 15], [0, 1, 2]]) numpy.testing.assert_array_equal(dpnp.concatenate((a23, a13)), res) numpy.testing.assert_array_equal(dpnp.concatenate((a23, a13), 0), res) numpy.testing.assert_array_equal(dpnp.concatenate((a23.T, a13.T), 1), res.T) numpy.testing.assert_array_equal(dpnp.concatenate((a23.T, a13.T), -1), res.T) # Arrays much match shape numpy.testing.assert_raises(ValueError, dpnp.concatenate, (a23.T, a13.T), 0) # 3D res = dpnp.reshape(dpnp.arange(2 * 3 * 7), (2, 3, 7)) a0 = res[..., :4] a1 = res[..., 4:6] a2 = res[..., 6:] numpy.testing.assert_array_equal(dpnp.concatenate((a0, a1, a2), 2), res) numpy.testing.assert_array_equal(dpnp.concatenate((a0, a1, a2), -1), res) numpy.testing.assert_array_equal( dpnp.concatenate((a0.T, a1.T, a2.T), 0), res.T) out = dpnp.copy(res) rout = dpnp.concatenate((a0, a1, a2), 2, out=out) numpy.testing.assert_(out is rout) numpy.testing.assert_equal(res, rout)
def test_matmul(self): array_data = [1., 2., 3., 4.] size = 2 # DPNP array1 = inp.reshape(inp.array(array_data, dtype=inp.float64), (size, size)) array2 = inp.reshape(inp.array(array_data, dtype=inp.float64), (size, size)) result = inp.matmul(array1, array2) # print(result) # original array_1 = numpy.array(array_data, dtype=numpy.float64).reshape( (size, size)) array_2 = numpy.array(array_data, dtype=numpy.float64).reshape( (size, size)) expected = numpy.matmul(array_1, array_2) # print(expected) # passed numpy.testing.assert_array_equal(expected, result)
def test_strides_reciprocal(dtype, shape): start, stop = 1, numpy.prod(shape) + 1 a = numpy.arange(start, stop, dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(start, stop, dtype=dtype), shape) dpb = dpa[::2] result = dpnp.reciprocal(dpb) expected = numpy.reciprocal(b) numpy.testing.assert_allclose(result, expected, rtol=1e-06)
def test_strides_erf(dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] result = dpnp.erf(dpb) expected = numpy.empty_like(b) for idx, val in enumerate(b): expected[idx] = math.erf(val) numpy.testing.assert_allclose(result, expected)
def test_strides_1arg(func_name, dtype, shape): a = numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape) b = a[::2] dpa = dpnp.reshape(dpnp.arange(numpy.prod(shape), dtype=dtype), shape) dpb = dpa[::2] dpnp_func = _getattr(dpnp, func_name) result = dpnp_func(dpb) numpy_func = _getattr(numpy, func_name) expected = numpy_func(b) numpy.testing.assert_allclose(result, expected)