# Dot product of multi-dimensional arrays

**URL:** <https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258>\
**Category:** New to Julia\
**Tags:** linearalgebra\
**Created:** [February 11, 2022, 3:53pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258 "2022-02-11T15:53:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lln](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lln](https://discourse.julialang.org/u/lln)\
**Post date:** [February 11, 2022, 3:53pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258/1 "2022-02-11T15:53:29Z")

</div>

Hi guys,

I am trying to compute the dot product of matrices A & B but getting different results compared to when I use the `numpy.dot(A, B)`.  
`A*B` and `A.*B` also give different results.

According to numpy docs ([numpy.dot — NumPy v1.23 Manual](https://numpy.org/doc/stable/reference/generated/numpy.dot.html)), this is what’s happening in my case: `dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m])`. How do I do this in Julia? Thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 11, 2022, 3:55pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258/2 "2022-02-11T15:55:38Z")

</div>

> [@lln](#):
>
> `A*B` and `A.*B` also give different results.

Those are totally different operations in Julia. `A * B` is general matrix multiplication (or matrix-vector multiplication as appropriate). `A .* B` is the _elementwise_ product of A and B (equivalent to what `*` does for `numpy.ndarray`).

Can you give a minimal example of the data you have, the result you want, and what you get instead?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [February 11, 2022, 4:08pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258/3 "2022-02-11T16:08:01Z")

</div>

Welcome. I assume that you are new to Julia and I can’t easily check the numpy.dot function for some example result.  
But perhaps you are just looking for:

```julia
julia> using LinearAlgebra

julia> a=rand(1:10,(3,3,3));

julia> b=rand(1:10,(3,3,3));

julia> dot(a,b)
973

```

---

<div class="post-metadata">

**Author:** ![lln](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lln](https://discourse.julialang.org/u/lln)\
**Post date:** [February 11, 2022, 4:10pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258/4 "2022-02-11T16:10:27Z")

</div>

Here’s an example  
Julia

```julia
using LinearAlgebra
A = [1. 0. 0. 0. 2.; 0. 0. 3. 0. 0.; 0. 0. 0. 0. 0.; 0. 2. 0. 0. 0.; 0. 2. 0. 0. 0.]
F = svd(A)
u, s, v = F;
h = v'.*(diagm(s)*v)

```

Python

```julia
from scipy import linalg
import scipy as sp

A = sp.array(sp.mat('1. 0. 0. 0. 2.; 0. 0. 3. 0. 0.; 0. 0. 0. 0. 0.; 0. 2. 0. 0. 0.; 0. 2. 0. 0. 0.'))
u, s, v = linalg.svd(A)
h = sp.dot(v.T, sp.dot(sp.diag(s), v))

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 11, 2022, 4:16pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258/5 "2022-02-11T16:16:47Z")

</div>

Ok, thanks. You don’t want `.*` here at all–it’s not a “dot product” in the linear algebra sense, it’s an elementwise product.

Note also that the `v` you get from Julia is the adjoint of what you got in Python, but that’s easy to deal with. We can also use `Diagonal` to create a very cheap _reference_ to `s` that behaves like a diagonal matrix, without actually filling in all the pointless zeros:

```julia
julia> h = v * Diagonal(s) * v'
5×5 Matrix{Float64}:
 0.447214 0.0 0.0 0.0 0.894427
 0.0 2.82843 0.0 0.0 0.0
 0.0 0.0 3.0 0.0 0.0
 0.0 0.0 0.0 0.0 0.0
 0.894427 0.0 0.0 0.0 1.78885

```

---

<div class="post-metadata">

**Author:** ![lln](https://avatars.discourse-cdn.com/v4/letter/l/ecae2f/32.png) [@lln](https://discourse.julialang.org/u/lln)\
**Post date:** [February 11, 2022, 4:27pm UTC](https://discourse.julialang.org/t/dot-product-of-multi-dimensional-arrays/76258/6 "2022-02-11T16:27:19Z")

</div>

Ah, thanks!
