编辑:如果您使用的是numpy> = 1.8.0的版本,那么np.linalg.eigvals会在您处理的任何数组的最后两个维度上运行,因此如果您将输入重新整形为(n_subarrays,nrows, ncols)数组你只需要调用一次eigvals:
import numpy as np
A = np.array([[0, 1, 0],
[0, 2, 0],
[0, 0, 3]])
# the input needs to be an array, since matrices can only be 2D.
B = np.repeat(A[np.newaxis,...], 4, 0)
# for arbitrary input arrays you could do something like:
# B = np.vstack(a[np.newaxis,...] for a in input_arrays)
# but for this to work it will be necessary for each element in
# 'input_arrays' to have the same shape
# eigvals will operate over the last two dimensions of the array and return
# a (4, 3) array of eigenvalues
C = np.linalg.eigvals(B)
# reshape this output so that it matches your original example
C.shape = (2, 2, 3)
如果您的输入数组并非都具有相同的尺寸,例如input_arrays [0] .shape ==(2,2),input_arrays [1] .shape ==(3,3)等等,那么你只能在具有匹配尺寸的子集上对该计算进行矢量化.
如果您使用的是较旧版本的numpy,那么遗憾的是我认为没有任何方法可以在多个输入数组上对特征值的计算进行矢量化 – 您只需要在Python中循环输入.