# Dot product of two row vectors

**URL:** <https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 13, 2017, 10:33am UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038 "2017-11-13T10:33:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 13, 2017, 10:33am UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/1 "2017-11-13T10:33:57Z")

</div>

Why is it not allowed to calculate the dot product of two row vectors?

Example:

```julia
julia> b=ones(3)'
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

julia> a=ones(3)'
1×3 RowVector{Float64,Array{Float64,1}}:
 1.0 1.0 1.0

julia> dot(a,b)
ERROR: MethodError: no method matching dot(::RowVector{Float64,Array{Float64,1}}, ::RowVector{Float64,Array{Float64,1}})

julia> 

```

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [November 13, 2017, 10:59am UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/2 "2017-11-13T10:59:06Z")

</div>

```nohighlight
vecdot(a,b)

```

[https://docs.julialang.org/en/stable/stdlib/linalg/#Base.LinAlg.vecdot](https://docs.julialang.org/en/stable/stdlib/linalg/#Base.LinAlg.vecdot)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 13, 2017, 12:06pm UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/3 "2017-11-13T12:06:03Z")

</div>

In addition to @Mattriks’s solution, a bit more context: think of `RowVector`s as matrices to a large extent. See `?RowVector`.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 13, 2017, 12:27pm UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/4 "2017-11-13T12:27:35Z")

</div>

I think the error message could be improved with a statement like “Did you mean vecdot?”

---

<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:** [November 13, 2017, 1:04pm UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/5 "2017-11-13T13:04:48Z")

</div>

I think a `dot` method should be added for two `RowVector`s. This is pretty unambiguous mathematically: if `x` is a vector and `x'` is a “row vector” (an element of the dual space), then the inner product on the former should define an inner product on the latter.

[https://github.com/JuliaLang/julia/issues/24590](https://github.com/JuliaLang/julia/issues/24590)

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [November 13, 2017, 5:40pm UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/6 "2017-11-13T17:40:35Z")

</div>

But if we define `dot(a,b) = a’*b` one would expect to get back a matrix (the outer product) when a and b are row vectors.

I’d say it should only be defined to return a Number if that’s also the case when a and b are matrices.

---

<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:** [November 13, 2017, 6:38pm UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/7 "2017-11-13T18:38:12Z")

</div>

> [@dlfivefifty](#):
>
> But if we define `dot(a,b) = a'*b` one would expect to get back a matrix (the outer product) when `a` and `b` are row vectors.

I’m skeptical that this should be the definition of `dot`; it seems like it should be defined to be an inner product, consistent with `norm(x) = sqrt(dot(x,x))`. Note that `norm(x')` is already defined to be `norm(x)` where `x` is a vector.

(I guess your `a'b` definition would work too if we defined `norm(x)` consistent with `sqrt(norm(dot(x,x)))`, but this seems unconventional to me.)

What is the point of defining `dot(a,b) = a'b` since you can just type `a'b` if that is what you want?

> [@dlfivefifty](#):
>
> I’d say it should only be defined to return a Number if that’s also the case when a and b are matrices.

The problem is that the choice of dot product with general matrices is more ambiguous. Worse, we define a default `norm(A)` for matrices by the induced norm, but that is not consistent with the most obvious matrix inner product (which would give the Frobenius norm).

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [November 13, 2017, 6:43pm UTC](https://discourse.julialang.org/t/dot-product-of-two-row-vectors/7038/8 "2017-11-13T18:43:51Z")

</div>

`dot` should always, always be an inner product and never ever an outer product.
