Hello Stack Overflow,
I am able to get desired results through a loop but wondering how I can apply a dot product per row of a 2D array. I'm trying to take each row from A and apply a series of dot products with B. It's a bit wonky right now with my intermediate results but I do get my desired results. Looking to vectorize and eliminate the loop.
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
B = np.array([[9,-1,8],[-12,20,-5],[6,1,4]])
int_result = np.ones((len(A),3))
for r in np.arange(0,len(A)):
int_result[r] = A[r].T.dot(B).dot(A[r])
print(int_result)
desired_result = int_result[:,0]
print(desired_result)
Intermediate Results
[[ 117. 117. 117.]
[ 744. 744. 744.]
[1911. 1911. 1911.]
[3618. 3618. 3618.]]
Desired Results
[ 117. 744. 1911. 3618.]
np.einsum('ij,kj,ik->i',A,B,A)