Suppose I have a numpy matrix like this:
[[ 1 2 3]
[ 10 100 1000]]
I would like to compute the inner product of each column with itself, so the result would be:
[1*1 + 10*10 2*2 + 100*100 3*3 + 1000*1000] == [101, 10004, 1000009]
I would like to know if this is possible using the einsum
function (and to better understand it).
So far, the closest result I could have is:
import numpy as np
arr = np.array([[1, 2, 3], [10, 100, 1000]])
res = np.einsum('ij,ik->jk', arr, arr)
# [[ 101 1002 10003]
# [ 1002 10004 100006]
# [ 10003 100006 1000009]]
The diagonal contains the expected result, but I would like to know if I can avoid edge calculations.