0

I'm using einsum, and I'm having an issue creating an inner product of two 5x2 matrices such that the resulting array has a size of (5,). I was able to use the code: print(np.einsum('ij,kj->ik', A, B)) to create the cross product of the two matrices resulting in a (5,5), and I used the code: print(np.einsum('ij,ik->ik', A, B)) to create a (5,2).

I'm not sure what else I can do to create the inner product of these two matrices to create a 1D array of size (5,). I think I'm on the right track, but I'm not sure what else to do.

My code and output are as follows:

import numpy as np

A = np.array([[4, 2],[-3, 3],[-3, -5],[1, -4], [1, 4]])
B = np.array([[-5, 0],[1, 5],[0, 1],[1, -2],[-1, -1]])

print(np.einsum('ij,kj->ik', A, B))
[[-20  14   2   0  -6]
 [ 15  12   3  -9   0]
 [ 15 -28  -5   7   8]
 [ -5 -19  -4   9   3]
 [ -5  21   4  -7  -5]]

print(np.einsum('ij,ik->ik', A, B))
[[-30   0]
 [  0   0]
 [  0  -8]
 [ -3   6]
 [ -5  -5]]
Pinka
  • 41
  • 3
  • 2
    `np.einsum('ij,ij->i', A, B)`? You want dot products of each row in A by the same row in B, right? – perl Mar 28 '21 at 18:05
  • 1
    @perl That's exactly what I was struggling with trying to figure out! I didn't realize inputting only one dimension was an option! I thought I had to input two in order to get an output, which was why I was so confused! Thank you so much! – Pinka Mar 28 '21 at 18:41

0 Answers0