Dot product of 2D matrices

I’m trying to convert some python code to julia to see how much time it will save me but I’m stuck trying to find the correct syntax in julia to match something trivial with numpy. In python I typically have some code that can be simplified like this:

x = np.random.rand(10,3)
y = np.random.rand(10,3)
tensor_prod = x.T.dot(y)

It’s also possible to use einsum :

np.einsum(‘ki,kj->ij’, x, y)

and both of these return the required 3x3 matrix.

I assumed this would be trivial to do in julia with:

dot(transpose(x), y)

but this returns a single value as it seems dot() is only for operating on vectors. How do I convert the simple python numpy code to julia?

Thanks in advance.

Numpy incorrectly calls a bunch of things “dot”, in Julia, matrix multiplication is just *:

x' * y #or transpose(x) * y

notice you’re unlikely to get speed up for this because everyone is just calling OpenBLAS routine anyway.


Also checkout the very fast einsum pkg Tullio.jl (still, not gonna be faster for dense CPU matrix, OpenBLAS is super optimized, partly by hand written architecture specific assembly)

Right, so maybe just try nested for-loop, or try Tullio.jl but it may not be any faster. Will see how things go!

linear algebra operation on dense CPU arrays are “figured out”, try something with more interesting things going on.

1 Like