# Dot product of 2D matrices

**URL:** <https://discourse.julialang.org/t/dot-product-of-2d-matrices/66147>\
**Category:** New to Julia\
**Tags:** linearalgebra\
**Created:** [August 10, 2021, 7:35pm UTC](https://discourse.julialang.org/t/dot-product-of-2d-matrices/66147 "2021-08-10T19:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpmorr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpmorr/32/26819_2.png) [@jpmorr](https://discourse.julialang.org/u/jpmorr)\
**Post date:** [August 10, 2021, 7:35pm UTC](https://discourse.julialang.org/t/dot-product-of-2d-matrices/66147/1 "2021-08-10T19:35:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 10, 2021, 7:41pm UTC](https://discourse.julialang.org/t/dot-product-of-2d-matrices/66147/2 "2021-08-10T19:41:52Z")

</div>

> [@jpmorr](#):
>
> dot(transpose(x), y)

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

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

```

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

> [@Why matrix multiplication is much slower than PyTorch](https://discourse.julialang.org/t/why-matrix-multiplication-is-much-slower-than-pytorch/63661/4):
>
> also, eh, pytorch default type is float32… In [8]: torch.set\_num\_threads(1) ...: A = torch.randn(1000, 1000, dtype=torch.float64) ...: B = torch.randn(1000, 1000, dtype=torch.float64) ...: %timeit -n 5 torch.matmul(A, B) 43.1 ms ± 810 µs per loop (mean ± std. dev. of 7 runs, 5 loops each) In [9]: torch.set\_num\_threads(1) ...: A = torch.randn(1000, 1000) ...: B = torch.randn(1000, 1000) ...: %timeit -n 5 torch.matmul(A, B) 22.1 ms ± 245 µs per loop (mean ± std. dev. of 7 runs,…

* * *

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)

---

<div class="post-metadata">

**Author:** ![jpmorr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpmorr/32/26819_2.png) [@jpmorr](https://discourse.julialang.org/u/jpmorr)\
**Post date:** [August 10, 2021, 7:57pm UTC](https://discourse.julialang.org/t/dot-product-of-2d-matrices/66147/3 "2021-08-10T19:57:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 10, 2021, 7:59pm UTC](https://discourse.julialang.org/t/dot-product-of-2d-matrices/66147/4 "2021-08-10T19:59:39Z")

</div>

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