Dot product of two row vectors

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

Example:

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> 

1 Like
vecdot(a,b)

https://docs.julialang.org/en/stable/stdlib/linalg/#Base.LinAlg.vecdot

2 Likes

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

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

1 Like

I think a dot method should be added for two RowVectors. 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

4 Likes

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.

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?

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).

3 Likes

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

4 Likes