Dot product of two tuples

Any particular reason for the dot product of two tuples to be undefined (v0.6)? Similarly for norm, or cross.

julia> dot((1, 3, 2), (1, 3, 2))
ERROR: MethodError: no method matching dot(::Tuple{Int64,Int64}, ::Tuple{Int64,Int64})

This is potential useful in many applications.

Use StaticArrays. “Naked” tuples are not really meant for linear algebra stuff, but they do provide a good datastructure to build static arrays on top of, that support linear algebra.

2 Likes

I do not want to pull this dependency for a single dot product in my code.

I am currently using

a = (1, 2)
b = (3, 4)
sum(a .* b)

which does not incur in any allocation.

You mentioned cross and norm so it seemed that you were interested in more things than a single dot. Your implementation is efficient for this use case though.

Yeah, apologies. That was a general comment.

FWIW, as a “general” solution you “should” use StaticArrays. It’s a pretty solid package that is backed by the community and excellent developers. I know what you mean by adding dependencies, but really, you’re already depending on huge dependencies by even running Julia, and in this case StaticArrays does exactly what you want. I get what you mean in general, but this is not a random package on some random account. If the philosophy wasn’t to get stuff out of base, I’m pretty sure you’d find this in base.

4 Likes

StaticArrays is also pretty self-contained: it only depends on Compat – which almost every package depends on. So this is not like depending on some huge package with a bajillion transitive dependencies.

1 Like