Given two numpy.array
s a
and b
,
鉴于两个numpy.arrays a和b,
c = numpy.outer(a, b)
returns an two-dimensional array where c[i, j] == a[i] * b[j]
. Now, imagine a
having k
dimensions.
返回二维数组,其中c [i,j] == a [i] * b [j]。现在,想象一下有k个维度。
- Which operation returns an array
c
of dimensionk+1
wherec[..., j] == a * b[j]
?
哪个操作返回维度为k + 1的数组c,其中c [...,j] == a * b [j]?
Additionally, let b
have l
dimensions.
另外,让b具有l维度。
- Which operation returns an array
c
of dimensionk+1
wherec[..., i1, i2, i3] == a * b[i1, i2, i3]
?
哪个操作返回维度为k + 1的数组c,其中c [...,i1,i2,i3] == a * b [i1,i2,i3]?
5 个解决方案
#1
5
The outer
method of NumPy ufuncs treats multidimensional input the way you want, so you could do
NumPy ufuncs的外部方法以您想要的方式处理多维输入,因此您可以这样做
numpy.multiply.outer(a, b)
rather than using numpy.outer
.
而不是使用numpy.outer。
All solutions suggested here are equally fast; for small arrays, multiply.outer
has a slight edge
这里建议的所有解决方案同样快速;对于小数组,multiply.outer有一个轻微的边缘
Code for generating the image:
生成图像的代码:
import numpy
import perfplot
def multiply_outer(data):
a, b = data
return numpy.multiply.outer(a, b)
def outer_reshape(data):
a, b = data
return numpy.outer(a, b).reshape((a.shape + b.shape))
def tensor_dot(data):
a, b = data
return numpy.tensordot(a, b, 0)
perfplot.show(
setup=lambda n: (numpy.random.rand(n, n), numpy.random.rand(n, n)),
kernels=[multiply_outer, outer_reshape, tensor_dot],
n_range=[2**k for k in range(7)],
logx=True,
logy=True,
)
#2
2
One approach would be using np.outer
and then reshape
-
一种方法是使用np.outer然后重塑 -
np.outer(a,b).reshape((a.shape + b.shape))
#3
2
I think np.tensordot
also works
我认为np.tensordot也有效
c = np.tensordot(a, b, 0)
inds = np.reshape(np.indices(b.shape), (b.ndim, -1))
for ind in inds.T:
ind = tuple(ind)
assert np.allclose(a * b[ind], c[(...,) + ind])
else:
print('no error')
# no error
#4
1
I think you're looking for kroneker product
我想你正在寻找kroneker产品
for example
> np.kron(np.eye(2), np.ones((2,2)))
array([[ 1., 1., 0., 0.],
[ 1., 1., 0., 0.],
[ 0., 0., 1., 1.],
[ 0., 0., 1., 1.]])
#5
1
np.einsum is what you are looking for.
np.einsum正是你要找的。
c[..., j] == a * b[j]
c [...,j] == a * b [j]
should be
c = np.einsum('...i,j -> ...ij', a, b)
c = np.einsum('... i,j - > ... ij',a,b)
and c[..., i1, i2, i3] == a * b[i1, i2, i3]
should be
和c [...,i1,i2,i3] == a * b [i1,i2,i3]应该是
c = np.einsum('i,...jkl -> ...ijkl', a, b)
c = np.einsum('i,... jkl - > ... ijkl',a,b)
#1
5
The outer
method of NumPy ufuncs treats multidimensional input the way you want, so you could do
NumPy ufuncs的外部方法以您想要的方式处理多维输入,因此您可以这样做
numpy.multiply.outer(a, b)
rather than using numpy.outer
.
而不是使用numpy.outer。
All solutions suggested here are equally fast; for small arrays, multiply.outer
has a slight edge
这里建议的所有解决方案同样快速;对于小数组,multiply.outer有一个轻微的边缘
Code for generating the image:
生成图像的代码:
import numpy
import perfplot
def multiply_outer(data):
a, b = data
return numpy.multiply.outer(a, b)
def outer_reshape(data):
a, b = data
return numpy.outer(a, b).reshape((a.shape + b.shape))
def tensor_dot(data):
a, b = data
return numpy.tensordot(a, b, 0)
perfplot.show(
setup=lambda n: (numpy.random.rand(n, n), numpy.random.rand(n, n)),
kernels=[multiply_outer, outer_reshape, tensor_dot],
n_range=[2**k for k in range(7)],
logx=True,
logy=True,
)
#2
2
One approach would be using np.outer
and then reshape
-
一种方法是使用np.outer然后重塑 -
np.outer(a,b).reshape((a.shape + b.shape))
#3
2
I think np.tensordot
also works
我认为np.tensordot也有效
c = np.tensordot(a, b, 0)
inds = np.reshape(np.indices(b.shape), (b.ndim, -1))
for ind in inds.T:
ind = tuple(ind)
assert np.allclose(a * b[ind], c[(...,) + ind])
else:
print('no error')
# no error
#4
1
I think you're looking for kroneker product
我想你正在寻找kroneker产品
for example
> np.kron(np.eye(2), np.ones((2,2)))
array([[ 1., 1., 0., 0.],
[ 1., 1., 0., 0.],
[ 0., 0., 1., 1.],
[ 0., 0., 1., 1.]])
#5
1
np.einsum is what you are looking for.
np.einsum正是你要找的。
c[..., j] == a * b[j]
c [...,j] == a * b [j]
should be
c = np.einsum('...i,j -> ...ij', a, b)
c = np.einsum('... i,j - > ... ij',a,b)
and c[..., i1, i2, i3] == a * b[i1, i2, i3]
should be
和c [...,i1,i2,i3] == a * b [i1,i2,i3]应该是
c = np.einsum('i,...jkl -> ...ijkl', a, b)
c = np.einsum('i,... jkl - > ... ijkl',a,b)