我正在尝试创建一个三维数组,Tusing numpy定义如下:
T_ {i,j,k} = \ delta_ {i,k} - \ delta {j,k}
其中\ delta_ {i,j}是Kronecker delta函数(当i = j时为1,否则为0)。 我想知道用numpy做这个最有效的方法。 我可以使用for循环创建两个三维数组并将其减去。 但我怀疑有一种更快更自然的方法。 非常感激任何的帮助。
I am trying to create a three dimensional array, Tusing numpy defined as follows:
T_{i, j, k} = \delta_{i, k} - \delta{j, k}
where \delta_{i, j} is the Kronecker delta function (1 when i=j and 0 otherwise). I am wondering what the most efficient way to do this using numpy. I can create two three dimensional arrays using for loops and subtract them. But I suspect there is a quicker and more idiomatic method. Any help would be most appreciated.
最满意答案
相当于三角洲的eye是裸eye :
delta = numpy.eye(5) T = delta[:,None,:] - delta[None,:,:]None创建了一个用于numpy广播的<virtual>维度(不需要额外的内存)。
The equivalent to delta is eye in numpy:
delta = numpy.eye(5) T = delta[:,None,:] - delta[None,:,:]The None creates a ‹virtual› dimension (doesn't take any additional memory) used for broadcasting in numpy.
更多推荐
发布评论