3

For a 4D array A with dimensions of (60,64,2,2), need to calculate the dot product with its transpose A_t.

A_t is of dimension(2,2,64,60). Below is what I do.

A_t = np.transpose(A)
A_At = A_t.dot(A)

The dot product throws an error

ValueError: shapes (2,2,64,60) and (60,64,2,2) not aligned: 60 (dim 3) != 2 (dim 2)

Am I taking the transpose incorrectly? I have also tried converting the individual arrays to numpy matrices(even though not recommended as per several posts) and then computing the dot product but I get a different error.

Have also researched numpy topics such as broadcasting but I could not find any useful example for 4D arrays.

Any inputs would be grateful. Thanks!

Note: I'm using python 2.7

vermachint
  • 93
  • 1
  • 9
  • 3
    What's the shape of expected output? Can you add a working loopy solution? – Divakar Oct 06 '18 at 13:47
  • I'm expecting the output to be a 2x2 – vermachint Oct 06 '18 at 14:28
  • 1
    Think of `dot` as sum of products. How you going to pair up the axes when multiplying, and which products will you sum. – hpaulj Oct 06 '18 at 16:09
  • Add a plain python tag to your question. – Mad Physicist Oct 06 '18 at 16:35
  • And keep in mind that dot reduces one axis per call. – Mad Physicist Oct 06 '18 at 16:35
  • From the [docstring of `numpy.dot`](https://docs.scipy.org/doc/numpy/reference/generated/numpy.dot.html): "If `a` is an N-D array and `b` is an M-D array (where M>=2), it is a sum product over the last axis of `a` and the second-to-last axis of `b`: `dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])`" – Warren Weckesser Oct 06 '18 at 16:39
  • What makes you think that what you want is a 2x2 array at the end? – keepAlive Oct 06 '18 at 18:08
  • `the individual arrays to numpy matrices` - what do you mean by this? Be precise. I might guess that you have 60*64 arrays, each of size (2,2). Or maybe 4 arrays of size (60,64). Dot or matrix product is well defined for 2 2d arrays, but there are all kinds of ways of extending the concept to 4d. – hpaulj Oct 06 '18 at 19:53
  • @Divakar Your answer to a similar question [here](https://stackoverflow.com/questions/47977238/intuition-and-idea-behind-reshaping-4d-array-to-2d-array-in-numpy) is great! Turns out it is not straight forward. – vermachint Oct 08 '18 at 07:25

1 Answers1

1

On your knowledge-driven wish of having a 2x2 array at the end, what about using xarray.dot for that kind of task. With your A in hand

>>> A.shape
(60, 64, 2, 2)

you would do

>>> xA   = xr.DataArray(A, dims=['d1','d2','d3','d4'])
>>> xA_t = xA.T
>>> xr.dot(xA_t, xA, dims=['d1','d2']).shape
(2, 2)
keepAlive
  • 6,369
  • 5
  • 24
  • 39
  • I'm not familiar with `xarray.dot`, but it looks like it's doing something similar to `np.tensordot`. – hpaulj Oct 06 '18 at 19:54
  • With `C = numpy.tensordot(A, A, axes=([0,1],[0,1]))` you would have to do `np.array([C[0,0,0,0], C[1,0,1,0], C[0,1,0,1], C[1,1,1,1]]).reshape((2,2))` to get the same result as `xr.dot`. – keepAlive Dec 15 '18 at 17:32