Home:ALL Converter>Numpy einsum equivalent in scala breeze

Numpy einsum equivalent in scala breeze

Ask Time:2022-03-31T00:54:11         Author:amarchin

Json Formatter

I have a matrix x (dim = (2, 3) for instance) and I would like to calculate the outer product of each row of the matrix with itself, obtaining a tensor with the shape (2, 3, 3).

In python I can do this using numpy.einsum

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])

np.einsum('...j,...l', x, x)

#array([[[ 1,  2,  3],
#        [ 2,  4,  6],
#        [ 3,  6,  9]],
#
#       [[16, 20, 24],
#        [20, 25, 30],
#        [24, 30, 36]]])


Would it be possible to do the same in Scala using Breeze?

Author:amarchin,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71681203/numpy-einsum-equivalent-in-scala-breeze
yy