简体   繁体   English

numpy einsum与'...'

[英]numpy einsum with '…'

The code below is meant to conduct a linear coordinate transformation on a set of 3d coordinates. 下面的代码用于在一组3d坐标上进行线性坐标变换。 The transformation matrix is A , and the array containing the coordinates is x . 变换矩阵是A ,包含坐标的数组是x The zeroth axis of x runs over the dimensions x, y, z. x的第0轴在尺寸x,y,z上运行。 It can have any arbitrary shape beyond that. 除此之外,它可以有任意形状。

Here's my attempt: 这是我的尝试:

A = np.random.random((3, 3))
x = np.random.random((3, 4, 2))

x_prime = np.einsum('ij,j...->i...', A, x)

The output is: 输出是:

    x_prime = np.einsum('ij,j...->i...', A, x)

ValueError: operand 0 did not have enough dimensions
to match the broadcasting, and couldn't be extended
because einstein sum subscripts were specified at both
the start and end

If I specify the additional subscripts in x explicitly, the error goes away. 如果我明确指定x的附加下标,则错误消失。 In other words, the following works: 换句话说,以下工作:

x_prime = np.einsum('ij,jkl->ikl', A, x)

I'd like x to be able to have any arbitrary number of axes after the zeroth axis, so the workaround I give about is not optimal. 我希望x能够在第0轴之后有任意数量的轴,所以我给出的解决方法并不是最佳的。 I'm actually not sure why the first einsum example is not working. 我实际上不确定为什么第一个einsum例子不起作用。 I'm using numpy 1.6.1. 我正在使用numpy 1.6.1。 Is this a bug, or am I misunderstanding the documentation ? 这是一个错误,还是我误解了文档

Yep, it's a bug. 是的,这是一个错误。 It was fixed in this pull request: https://github.com/numpy/numpy/pull/4099 它已在此拉取请求中修复: https//github.com/numpy/numpy/pull/4099

This was only merged a month ago, so it'll be a while before it makes it to a stable release. 这只是在一个月前合并,所以它还需要一段时间才能达到稳定版本。

EDIT : As @hpaulj mentions in the comment, you can work around this limitation by adding an ellipsis even when all indices are specified: 编辑 :正如@hpaulj在评论中提到的那样,即使指定了所有索引,也可以通过添加省略号来解决此限制:

np.einsum('...ij,j...->i...', A, x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM