# Dot product

**URL:** https://discourse.julialang.org/t/dot-product/128704
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [May 5, 2025, 6:40am UTC](https://discourse.julialang.org/t/dot-product/128704 "2025-05-05T06:40:56Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)
#### Post date: [May 5, 2025, 6:40am UTC](https://discourse.julialang.org/t/dot-product/128704/1 "2025-05-05T06:40:56Z")

</div>

Hi, this is probably dumb, but I want to know if there is some standard way of doing the dot product. I know that

```julia
a'b

```

will give a correct answer, though I do not know if there is some standard when extending it to matrix multiplication (I mean if there is a used operation besides using LinearAlgebra). Some sources cite \cdot but apparently that is no longer defined on the general library.

---

<div class="post-metadata">

### Author: ![JADekker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jadekker/32/210281_2.png) [@JADekker](https://discourse.julialang.org/u/JADekker)
#### Post date: [May 5, 2025, 6:46am UTC](https://discourse.julialang.org/t/dot-product/128704/2 "2025-05-05T06:46:56Z")

</div>

If you import LinearAlgebra you should have \cdot. I wasn’t aware that a’b works without that package? (But that may be because I import LinearAlgebra whenever I need linear algebra stuff such as dot products)

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [May 5, 2025, 6:56am UTC](https://discourse.julialang.org/t/dot-product/128704/3 "2025-05-05T06:56:19Z")

</div>

The standard way is to use the `dot` function from the `LinearAlgebra` standard library.  
Julia 1.11 is somewhat helpful about finding it.

```julia
julia> dot
ERROR: UndefVarError: `dot` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Hint: a global variable of this name may be made accessible by importing LinearAlgebra in the current active module Main

julia> using LinearAlgebra

help?> dot
search: dot do cot det stdout ldlt acot sort @doc edit cotd coth

  dot(x, y)
  x ⋅ y

  Compute the dot product between two vectors. For complex vectors, the first vector is conjugated.

  dot also works on arbitrary iterable objects, including arrays of any dimension, as long as dot is defined
  on the elements.

[...]

```

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [May 5, 2025, 11:06am UTC](https://discourse.julialang.org/t/dot-product/128704/4 "2025-05-05T11:06:47Z")

</div>

I find this definition in wiki

```julia
Algebraically, the dot product is the sum of the products of the corresponding entries of the two sequences of numbers.

```

Translating into julia, it is

```julia
function dot_product(x, y) return sum(x .* y) end

```

But due to the fact that we haven’t restricted the type of args, this function may have some other (pleasantly) unexpected usage. Therefore, I would prefer a more appropriate name for it, see [this](https://discourse.julialang.org/t/inner-product-grammar-is-neater-than-sum-index-in-jump-modeling-but-triggers-warning/124528/28).

My larger point is: **there is no foolproof method**. And we should not hope for this.  
As an example, if sparsity exists, we may sometimes need to write our own code, see [this](https://discourse.julialang.org/t/i-added-1-million-constraints-to-an-lp-unwittingly/127668/9).

In general, the `adjoint` (`'`) or `transpose` are versatile, not restricted to only offering a scalar output (which in math sense, inner product). I think these are already enough.

If there is anyone who knows more about the underlying performance issue, just correct me 🙂

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 5, 2025, 12:17pm UTC](https://discourse.julialang.org/t/dot-product/128704/5 "2025-05-05T12:17:44Z")

</div>

> [@WalterMadelim](#):
>
> My larger point is: **there is no foolproof method**.

I think you’re just spreading confusion here needlessly.

The `dot(x,y)` function from the LinearAlgebra package (and its synonym `x ⋅ y`) is the standard interface for inner products in Julia. It has methods for inner products of matrices as well. And it is also extended to methods for other types of objects (e.g. sparse vectors) in other packages like SparseArrays.

> [@martin\_sanchez](#):
>
> I want to know if there is some standard way of doing the dot product. I know that
> 
> ```julia
> a'b
> 
> ```
> 
> will give a correct answer, though I do not know if there is some standard when extending it to matrix multiplication

`a'b` or `a' * b` is [_equivalent_ to calling](https://github.com/JuliaLang/LinearAlgebra.jl/blob/6e0a996e092e94b4d0ef87a9ac4d135a41f1a958/src/adjtrans.jl#L517) `dot(a, b)` if these are two “column vectors” (1d arrays) of scalars [\*see below].

It’s not clear what “extension” to matrix multiplication you are asking for. `A'B` for two matrices computes the matrix product \overline{A^T} B. `dot(A, B)` computes the [Frobenius inner product](https://en.wikipedia.org/wiki/Frobenius_inner_product). These are two different things, both of which are useful depending on what you are doing.

Numpy muddied the waters even further because [`numpy.dot`](https://numpy.org/doc/stable/reference/generated/numpy.dot.html) is ordinary matrix multiplication (`A * B` in Julia) for two 2d arrays (and is a type of tensor contraction more generally). [Naming things is hard](https://joncalder.co.za/2017-12-04-naming-things-is-hard/).

* * *

PS. Note that `sum(x .* y)`, besides being inefficient, is also not an inner product if the elements aren’t real: a true inner product needs a complex conjugation, which is what `dot(x, y)` does. (There was reasonable disagreement about whether `dot` in Julia should be unconjugated, and we should have some other name like `inner(x,y)` for the true inner product, but nowadays Julia is past that bikeshedding.)

[\*] There’s some subtlety here because linear-algebra notation sometimes glosses over the isomorphism between a 1-row matrix and a [linear form or “dual” vector](https://en.wikipedia.org/wiki/Linear_form), or between a 1 \times 1 matrix and a scalar. As a programming language, Julia had to define this more explicitly; see also the talk [Taking Vector Transposes Seriously](https://www.youtube.com/watch?v=C2RO34b_oPM) by @jiahao.

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [May 6, 2025, 12:37am UTC](https://discourse.julialang.org/t/dot-product/128704/6 "2025-05-06T00:37:04Z")

</div>

I watched that video. I prefer the current (julia’s) behavior, comparing to other languages.

Yes, `dot(x, y)` calls `BLAS`, which is specialized.  
Looks like `sum(x .* y)` will call `mapreduce` and therefore resolves into standard `+` and `*` operations (perhaps calling standard C functions?).

Moreover, it appears that `dot(x, A, y)` also outperforms `x' * A * y`, at least in terms of allocations, sometimes also in time (can someone do a test?).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 6, 2025, 12:46am UTC](https://discourse.julialang.org/t/dot-product/128704/7 "2025-05-06T00:46:22Z")

</div>

> [@WalterMadelim](#):
>
> Looks like `sum(x .* y)` will call `mapreduce`

It first computes `x .* y`, allocating a new array, and _then_ calls `sum` on this new array. This is much less efficient than doing a single pass over the arrays and accumulating the sum without allocating anything, as `dot` does.

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [May 6, 2025, 10:28am UTC](https://discourse.julialang.org/t/dot-product/128704/8 "2025-05-06T10:28:47Z")

</div>

Is this dispatch as expected? I don’t see the special code owing to SparseArrays.jl.

```julia
julia> using LinearAlgebra, SparseArrays

julia> x = sprand(100, .1);

julia> A = rand(100, 100);

julia> @which dot(x, A, x)
dot(x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
     @ LinearAlgebra K:\julia-1.11.4\share\julia\stdlib\v1.11\LinearAlgebra\src\generic.jl:931

julia> x = Vector(x); # as a comparison

julia> @which dot(x, A, x) # the same method
dot(x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
     @ LinearAlgebra K:\julia-1.11.4\share\julia\stdlib\v1.11\LinearAlgebra\src\generic.jl:931

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 6, 2025, 11:02am UTC](https://discourse.julialang.org/t/dot-product/128704/9 "2025-05-06T11:02:28Z")

</div>

> [@WalterMadelim](#):
>
> Is this dispatch as expected? I don’t see the special code owing to [SparseArrays.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/SparseArrays).

Looks like the 3-arg `dot(x, A, y)` doesn’t have an optimized method for sparse vectors yet.

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [May 7, 2025, 2:32am UTC](https://discourse.julialang.org/t/dot-product/128704/10 "2025-05-07T02:32:54Z")

</div>

> [@stevengj](#):
>
> PS. Note that `sum(x .* y)`, besides being inefficient

Not related to OP’s question, but I am wondering what is the best way to do this. Is `transpose(x)y` the best?

---

<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: [May 7, 2025, 3:14am UTC](https://discourse.julialang.org/t/dot-product/128704/11 "2025-05-07T03:14:02Z")

</div>

> [@linwaytin](#):
>
> Not related to OP’s question, but I am wondering what is the best way to do this

what is “this”? just `dot(x, y)` ?

---

<div class="post-metadata">

### Author: ![dqeeq](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@dqeeq](https://discourse.julialang.org/u/dqeeq)
#### Post date: [May 7, 2025, 3:56am UTC](https://discourse.julialang.org/t/dot-product/128704/12 "2025-05-07T03:56:20Z")

</div>

“this” is the dot product without conjugating the first vector, I guess.

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [May 7, 2025, 4:02am UTC](https://discourse.julialang.org/t/dot-product/128704/13 "2025-05-07T04:02:34Z")

</div>

What is “best” depends on to what extent the corresponding method is optimized.  
For regular numerical operations, `dot` will call BLAS, and is therefore “the best”.  
You can write `transpose(x) * y` or `x' * y` resembling the textbook, but you don’t have to.  
`dot(x, y)` is considered “standard”.

On the other hand, `transpose` and `'` are more versatile so that they can reach more abundant results. e.g., you can do `[rand(3, 3) for _ in 1:4]' * rand(4)`, which is not an inner product but is also meaningful itself. If we look into this operation, we find that it essentially calls

```julia
sum(uu*vv for (uu, vv) in zip(u, v))

```

inside `*(u::AdjOrTransAbsVec, v::AbstractVector) = _dot_nonrecursive(u, v)`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [May 7, 2025, 5:51am UTC](https://discourse.julialang.org/t/dot-product/128704/14 "2025-05-07T05:51:36Z")

</div>

> [@dqeeq](#):
>
> dot product without conjugating the first vector, I guess.

Do you have a particular case with complex vectors where you specifically need to _avoid_ conjugation?

If the vectors are real-valued, you should use `x' * y`, not `transpose(x) * y`.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 7, 2025, 3:32pm UTC](https://discourse.julialang.org/t/dot-product/128704/15 "2025-05-07T15:32:44Z")

</div>

> [@DNF](#):
>
> If the vectors are real-valued, you should use `x' * y`, not `transpose(x) * y`.

For vectors with a real element type, both `x' * y` and `transpose(x) * y` are equivalent: both call `dot(x, y)`.

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [May 7, 2025, 4:29pm UTC](https://discourse.julialang.org/t/dot-product/128704/16 "2025-05-07T16:29:05Z")

</div>

Sorry for the confusion. Yes, I want to multiply two vector element-wise and sum the result, like the following

```julia
res = 0.0im
for i = eachindex(x)
   res += x[i]*y[i]
end

```

When `x` is real, I can use dot, but sometimes I need this for complex vectors.  
The above code is fine, but a little lengthy, so I’m wondering if there is a better solution.

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [May 7, 2025, 4:43pm UTC](https://discourse.julialang.org/t/dot-product/128704/17 "2025-05-07T16:43:39Z")

</div>

Surprisingly, I found the functions `cdotu` and `zdotu` exist in LAPACK, which perform this operation.

I really hope these functions can be included, maybe through extending the current `dot` function. (I had trouble with the `dot` function before because the name for me suggests only element-wise product. I feel `inner` implies the conjugation involved.)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [May 7, 2025, 4:46pm UTC](https://discourse.julialang.org/t/dot-product/128704/18 "2025-05-07T16:46:23Z")

</div>

> [@linwaytin](#):
>
> Surprisingly, I found the functions `cdotu` and `zdotu` exist in LAPACK, which perform this operation.
> 
> I really hope these functions can be included, maybe through extending the current `dot` function.

If you use `transpose(x) * y` (as suggested above) it will do what you want. It calls `dot(x, y)` for real arrays, calls `BLAS.dotu(x, y)` for BLAS-compatible complex types, and otherwise uses a `sum(xx*yy for (xx, yy) in zip(x, y))` fallback.

---

<div class="post-metadata">

### Author: ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)
#### Post date: [May 7, 2025, 5:55pm UTC](https://discourse.julialang.org/t/dot-product/128704/19 "2025-05-07T17:55:53Z")

</div>

Great! Thanks for the information. Now I know what to do when I need dot product without conjugation.
