Home > Net >  Multiplying 3d matrix and 3d matrix
Multiplying 3d matrix and 3d matrix

Time:07-01

I'm trying to do the multiplying of 3d matrix and 3d matrix, my matrix is as follows:

Z = np.array([
[[0,0,0.25],[0.25,0.5,0.75],[0,0,0.25],[0.75,1.0,1.0],[0.75,1.0,1.0]],
[[0,0,0.25],[0,0,0.25],[0.5,0.75,1.0],[0,0,0.25],[0,0,0.25]],
[[0,0,0.25],[0,0,0.25],[0,0,0.25],[0,0.25,0.5],[0,0,0.25]],
[[0,0,0.25],[0.25,0.5,0.75],[0,0,0.25],(0,0,0.25),[0,0,0.25]],
[[0,0,0.25],[0,0,0.25],[0,0,0.25],[0,0,0.25],[0,0,0.25]]
])
print(Z)
print(type(Z))
print("np.shape = ",np.shape(Z))

The shape is (5,5,3), I want to do the multiplying like np.dot(Z,Z) ,but it can't work in 3d matrix.

I've seen about using np.tensordot(Z,Z,axes=?), but I don't know how to set axes.

CodePudding user response:

I suggest you take a look at the documentation of the tensordot() function to actually understand what it is doing with the matrices:

import numpy as np

Z = np.array([
[[0,0,0.25],[0.25,0.5,0.75],[0,0,0.25],[0.75,1.0,1.0],[0.75,1.0,1.0]],
[[0,0,0.25],[0,0,0.25],[0.5,0.75,1.0],[0,0,0.25],[0,0,0.25]],
[[0,0,0.25],[0,0,0.25],[0,0,0.25],[0,0.25,0.5],[0,0,0.25]],
[[0,0,0.25],[0.25,0.5,0.75],[0,0,0.25],(0,0,0.25),[0,0,0.25]],
[[0,0,0.25],[0,0,0.25],[0,0,0.25],[0,0,0.25],[0,0,0.25]]
])

B = np.tensordot(Z, Z, axes=[1, 0])
print(B)

Output:

[[[[0.     0.     0.4375]
   [0.1875 0.375  0.8125]
   [0.125  0.1875 0.625 ]
   [0.     0.     0.4375]
   [0.     0.     0.4375]]

  [[0.     0.     0.625 ]
   [0.25   0.5    1.125 ]
   [0.25   0.375  1.    ]
   [0.     0.     0.625 ]
   [0.     0.     0.625 ]]

  [[0.     0.     0.8125]
   [0.3125 0.625  1.4375]
   [0.375  0.5625 1.375 ]
   [0.1875 0.3125 1.0625]
   [0.1875 0.25   1.    ]]]


 [[[0.     0.     0.125 ]
   [0.     0.     0.125 ]
   [0.     0.     0.125 ]
   [0.     0.125  0.25  ]
   [0.     0.     0.125 ]]

  [[0.     0.     0.1875]
   [0.     0.     0.1875]
   [0.     0.     0.1875]
   [0.     0.1875 0.375 ]
   [0.     0.     0.1875]]

  [[0.     0.     0.5   ]
   [0.125  0.25   0.75  ]
   [0.125  0.1875 0.6875]
   [0.1875 0.5    0.9375]
   [0.1875 0.25   0.6875]]]


 [[[0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]]

  [[0.     0.     0.0625]
   [0.0625 0.125  0.1875]
   [0.     0.     0.0625]
   [0.     0.     0.0625]
   [0.     0.     0.0625]]

  [[0.     0.     0.375 ]
   [0.1875 0.375  0.75  ]
   [0.125  0.1875 0.5625]
   [0.1875 0.3125 0.625 ]
   [0.1875 0.25   0.5625]]]


 [[[0.     0.     0.0625]
   [0.     0.     0.0625]
   [0.125  0.1875 0.25  ]
   [0.     0.     0.0625]
   [0.     0.     0.0625]]

  [[0.     0.     0.125 ]
   [0.     0.     0.125 ]
   [0.25   0.375  0.5   ]
   [0.     0.     0.125 ]
   [0.     0.     0.125 ]]

  [[0.     0.     0.4375]
   [0.125  0.25   0.6875]
   [0.375  0.5625 1.    ]
   [0.1875 0.3125 0.6875]
   [0.1875 0.25   0.625 ]]]


 [[[0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]]

  [[0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]
   [0.     0.     0.    ]]

  [[0.     0.     0.3125]
   [0.125  0.25   0.5625]
   [0.125  0.1875 0.5   ]
   [0.1875 0.3125 0.5625]
   [0.1875 0.25   0.5   ]]]]

CodePudding user response:

With a 3d array, there are various ways of doing a matrix product:

In [365]: Z.shape
Out[365]: (5, 5, 3)

You say np.dot isn't valid, but don't show why:

In [366]: np.dot(Z,Z).shape
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [366], in <cell line: 1>()
----> 1 np.dot(Z,Z).shape

File <__array_function__ internals>:5, in dot(*args, **kwargs)

ValueError: shapes (5,5,3) and (5,5,3) not aligned: 3 (dim 2) != 5 (dim 1)

If you take time to read the np.dot docs, you see that it trys to do the sum-of-products on the last axis of A and 2nd to the last of B, hence the mismatch between 3 and 5.

You'd get the same error is Z was (5,3) shape.

One way around this is to change the 2nd Z to (5,3,5) shape:

In [367]: np.dot(Z,Z.transpose(0,2,1)).shape
Out[367]: (5, 5, 5, 5)

But dot does a kind of outer-product on the leading dimensions.

I think the tensordot in the other answer does this as well. tensordot just reduces the calculation down to a dot call, with some reshape and transposes.

matmul/@ treats the leading dimensions as 'batch' and applies normal broadcasting rules:

In [368]: np.matmul(Z,Z.transpose(0,2,1)).shape
Out[368]: (5, 5, 5)

With einsum we can specify other combinations of axes.

In [369]: np.einsum('ijk,ijk->ij',Z,Z).shape
Out[369]: (5, 5)
In [370]: np.einsum('ijk,ijk->ik',Z,Z).shape
Out[370]: (5, 3)
In [371]: np.einsum('ijk,ilk->ijl',Z,Z).shape
Out[371]: (5, 5, 5)
In [373]: np.einsum('ijk,ijl->ijl',Z,Z).shape
Out[373]: (5, 5, 3)   
In [374]: np.einsum('ijk,jlk->ilk',Z,Z).shape
Out[374]: (5, 5, 3)
  • Related