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 multi_dot(arrays, out=None): """ Compute the dot product of two or more arrays in a single function call Parameters ---------- arrays : sequence of array_like If the first argument is 1-D it is treated as row vector. If the last argument is 1-D it is treated as column vector. The other arguments must be 2-D. out : ndarray, optional unsupported Returns ------- output : ndarray Returns the dot product of the supplied arrays. See Also -------- :obj:`numpy.multi_dot` """ n = len(arrays) if n < 2: checker_throw_value_error("multi_dot", "arrays", n, ">1") result = arrays[0] for id in range(1, n): result = dpnp.dot(result, arrays[id]) return result
def test_dot_ones(type): n = 10**5 a = numpy.ones(n, dtype=type) b = numpy.ones(n, dtype=type) ia = inp.array(a) ib = inp.array(b) result = inp.dot(ia, ib) expected = numpy.dot(a, b) numpy.testing.assert_array_equal(expected, result)
def test_dot_arange(type): n = 10**2 m = 10**3 a = numpy.hstack((numpy.arange(n, dtype=type),) * m) b = numpy.flipud(a) ia = inp.array(a) ib = inp.array(b) result = inp.dot(ia, ib) expected = numpy.dot(a, b) numpy.testing.assert_allclose(expected, result)
def vdot(*args, **kwargs): """ Return the dot product of two vectors. See Also -------- :meth:`numpy.vdot` """ return dpnp.dot(*args, **kwargs)
def vdot(*args, **kwargs): """ Return the dot product of two vectors. For full documentation refer to :obj:`numpy.vdot`. See Also -------- :obj:`dpnp.dot` : Returns the dot product. Notes ----- This function works the same as :obj:`dpnp.dot`. """ return dpnp.dot(*args, **kwargs)