python操作实例_Python numpy.vdot() 使用实例

本文通过多个代码示例详细展示了如何使用Python的numpy库中的vdot()函数,涉及向量点乘、矩阵运算等多个场景,适用于理解该函数的用法。
摘要由CSDN通过智能技术生成

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def vdot(a, b):

"""Returns the dot product of two vectors.

The input arrays are flattened into 1-D vectors and then it performs inner

product of these vectors.

Args:

a (cupy.ndarray): The first argument.

b (cupy.ndarray): The second argument.

Returns:

cupy.ndarray: Zero-dimensional array of the dot product result.

.. seealso:: :func:`numpy.vdot`

"""

if a.size != b.size:

raise ValueError('Axis dimension mismatch')

if a.dtype.kind == 'c':

a = a.conj()

return core.tensordot_core(a, b, None, 1, 1, a.size, ())

Example 2

def test_sandwich(nr_sites, local_dim, rank, rgen, dtype):

mps = factory.random_mpa(nr_sites, local_dim, rank,

randstate=rgen, dtype=dtype, normalized=True)

mps2 = factory.random_mpa(nr_sites, local_dim, rank,

randstate=rgen, dtype=dtype, normalized=True)

mpo = factory.random_mpa(nr_sites, [local_dim] * 2, rank,

randstate=rgen, dtype=dtype)

mpo.canonicalize()

mpo /= mp.trace(mpo)

vec = mps.to_array().ravel()

op = mpo.to_array_global().reshape([local_dim**nr_sites] * 2)

res_arr = np.vdot(vec, np.dot(op, vec))

res_mpo = mp.inner(mps, mp.dot(mpo, mps))

res_sandwich = mp.sandwich(mpo, mps)

assert_almost_equal(res_mpo, res_arr)

assert_almost_equal(res_sandwich, res_arr)

vec2 = mps2.to_array().ravel()

res_arr = np.vdot(vec2, np.dot(op, vec))

res_mpo = mp.inner(mps2, mp.dot(mpo, mps))

res_sandwich = mp.sandwich(mpo, mps, mps2)

assert_almost_equal(res_mpo, res_arr)

assert_almost_equal(res_sandwich, res_arr)

Example 3

def test_adjoint(dtype, shearletSystem):

"""Validate the adjoint."""

shape = tuple(shearletSystem['size'])

# load data

X = np.random.randn(*shape).astype(dtype)

# decomposition

coeffs = pyshearlab.SLsheardec2D(X, shearletSystem)

# adjoint

Xadj = pyshearlab.SLshearadjoint2D(coeffs, shearletSystem)

assert Xadj.dtype == X.dtype

assert Xadj.shape == X.shape

# should equal

assert (pytest.approx(np.vdot(coeffs, coeffs), rel=1e-3, abs=0) ==

np.vdot(X, Xadj))

Example 4

def test_adjoint_of_inverse(dtype, shearletSystem):

"""Validate the adjoint of the inverse."""

X = np.random.randn(*shearletSystem['size']).astype(dtype)

# decomposition

coeffs = pyshearlab.SLsheardec2D(X, shearletSystem)

# reconstruction

Xrec = pyshearlab.SLshearrec2D(coeffs, shearletSystem)

Xrecadj = pyshearlab.SLshearrecadjoint2D(Xrec, shearletSystem)

assert Xrecadj.dtype == X.dtype

assert Xrecadj.shape == coeffs.shape

# = .

assert (pytest.approx(np.vdot(Xrec, Xrec), rel=1e-3, abs=0) ==

np.vdot(Xrecadj, coeffs))

Example 5

def test_basic(self):

dt_numeric = np.typecodes['AllFloat'] + np.typecodes['AllInteger']

dt_complex = np.typecodes['Complex']

# test real

a = np.eye(3)

for dt in dt_numeric + 'O':

b = a.astype(dt)

res = np.vdot(b, b)

assert_(np.isscalar(res))

assert_equal(np.vdot(b, b), 3)

# test complex

a = np.eye(3) * 1j

for dt in dt_complex + 'O':

b = a.astype(dt)

res = np.vdot(b, b)

assert_(np.isscalar(res))

assert_equal(np.vdot(b, b), 3)

# test boolean

b = np.eye(3, dtype=np.bool)

res = np.vdot(b, b)

assert_(np.isscalar(res))

assert_equal(np.vdot(b, b), True)

Example 6

def test_vdot_uncontiguous(self):

for size in [2, 1000]:

# Different sizes match different branches in vdot.

a = np.zeros((size, 2, 2))

b = np.zeros((size, 2, 2))

a[:, 0, 0] = np.arange(size)

b[:, 0, 0] = np.arange(size) + 1

# Make a and b uncontiguous:

a = a[..., 0]

b = b[..., 0]

assert_equal(np.vdot(a, b),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a, b.copy()),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a.copy(), b),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a.copy('F'), b),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a, b.copy('F')),

np.vdot(a.flatten(), b.flatten()))

Example 7

def test_basic(self):

dt_numeric = np.typecodes['AllFloat'] + np.typecodes['AllInteger']

dt_complex = np.typecodes['Complex']

# test real

a = np.eye(3)

for dt in dt_numeric + 'O':

b = a.astype(dt)

res = np.vdot(b, b)

assert_(np.isscalar(res))

assert_equal(np.vdot(b, b), 3)

# test complex

a = np.eye(3) * 1j

for dt in dt_complex + 'O':

b = a.astype(dt)

res = np.vdot(b, b)

assert_(np.isscalar(res))

assert_equal(np.vdot(b, b), 3)

# test boolean

b = np.eye(3, dtype=np.bool)

res = np.vdot(b, b)

assert_(np.isscalar(res))

assert_equal(np.vdot(b, b), True)

Example 8

def test_vdot_uncontiguous(self):

for size in [2, 1000]:

# Different sizes match different branches in vdot.

a = np.zeros((size, 2, 2))

b = np.zeros((size, 2, 2))

a[:, 0, 0] = np.arange(size)

b[:, 0, 0] = np.arange(size) + 1

# Make a and b uncontiguous:

a = a[..., 0]

b = b[..., 0]

assert_equal(np.vdot(a, b),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a, b.copy()),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a.copy(), b),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a.copy('F'), b),

np.vdot(a.flatten(), b.flatten()))

assert_equal(np.vdot(a, b.copy('F'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值