Re: [SciPy-user] Vectorization question
Anand Patil wrote:
/Hi all, />>/ />>/I want to make array A from array B like so: />>/ />>/A[t, j, k] = \sum_i B[t, j, i] B[t, i, k] />>/ />>/That is, for each t />>/ />>/A[t,] = dot(B[t,], B[t,]) />>/ />>/There's no loopless way to do this in numpy, right? />>/ />>/ />You should be able to do this just using
A = dot(B,B)
Because the dot function returns the sum of products over the last dimension of the first argument and the second-to-last dimension of the second argument.
-Travis
The output array I'm looking for is rank-3, but as I understand them dot and tensordot can only ever return even-rank arrays: In [1]: from numpy import zeros, dot In [2]: B=zeros((4,3,3)) In [3]: dot(B,B).shape Out[3]: (4, 3, 4, 3) In [4]: Thanks, Anand
Anand Patil wrote:
Anand Patil wrote:
/Hi all,
/>>/ />>/I want to make array A from array B like so: />>/ />>/A[t, j, k] = \sum_i B[t, j, i] B[t, i, k] />>/ />>/That is, for each t />>/ />>/A[t,] = dot(B[t,], B[t,]) />>/ />>/There's no loopless way to do this in numpy, right? />>/ />>/ />You should be able to do this just using
A = dot(B,B)
Because the dot function returns the sum of products over the last dimension of the first argument and the second-to-last dimension of the second argument.
-Travis
The output array I'm looking for is rank-3, but as I understand them dot and tensordot can only ever return even-rank arrays:
I see the difference now. Yes, you are right, you are wanting to do a sum of products without an outer-product. You would have to extract the "diagonal" of the result. -Travis
participants (2)
-
Anand Patil -
Travis Oliphant