更新时间:2021-08-08 18:24:21
简答:
[a[:,:j] for j in i]
您要做的是不是可矢量化的操作.***将矢量化定义为对单个数组的批处理操作,而不是对单个标量进行的操作:
What you are trying to do is not a vectorizable operation. Wikipedia defines vectorization as a batch operation on a single array, instead of on individual scalars:
在计算机科学中,数组编程语言(也称为向量或多维语言)将标量运算泛化为透明地应用于向量、矩阵和高维数组.
In computer science, array programming languages (also known as vector or multidimensional languages) generalize operations on scalars to apply transparently to vectors, matrices, and higher-dimensional arrays.
...
...对整个数组进行操作的操作可以称为向量化操作...
... an operation that operates on entire arrays can be called a vectorized operation...
在CPU级优化方面,矢量化的定义是:
In terms of CPU-level optimization, the definition of vectorization is:
向量化"(简化)是重写循环的过程,这样它不是处理数组的单个元素 N 次,而是同时处理(比如说)数组的 4 个元素 N/4 次.
"Vectorization" (simplified) is the process of rewriting a loop so that instead of processing a single element of an array N times, it processes (say) 4 elements of the array simultaneously N/4 times.
你的情况的问题是每个单独操作的结果具有不同的形状:(3, 1)
, (3, 2)
和 (3, 3)
.它们不能形成单个向量化操作的输出,因为输出必须是一个连续的数组.当然,它内部可以包含 (3, 1)
, (3, 2)
和 (3, 3)
数组(如视图),但这就是您的原始数组 a
已经做到的.
The problem with your case is that the result of each individual operation has a different shape: (3, 1)
, (3, 2)
and (3, 3)
. They can not form the output of a single vectorized operation, because the output has to be one contiguous array. Of course, it can contain (3, 1)
, (3, 2)
and (3, 3)
arrays inside of it (as views), but that's what your original array a
already does.
您真正要寻找的只是一个计算所有这些的表达式:
What you're really looking for is just a single expression that computes all of them:
[a[:,:j] for j in i]
...但它在性能优化的意义上不是矢量化的.在引擎盖下,它是一个简单的旧 for
循环,它一个一个地计算每个项目.
... but it's not vectorized in a sense of performance optimization. Under the hood it's plain old for
loop that computes each item one by one.