I am testing something very simple, a dot product, and I want to ensure, it gets two arrays (or vectors) of the same length. This attempt does not work:
function dot_product{N::UInt64}(vec1::Array{Float64, N}, vec2::Array{Float64, N})
return sum((x,y) -> x * y, zip(vec1, vec2))
end
and neither this one:
function dot_product(vec1::Array{Float64, N}, vec2::Array{Float64, N}) where N :: UInt64
return sum((x,y) -> x * y, zip(vec1, vec2))
end
(problems with the where clause).
Is Julia able to infer and work with types like that? How to do it?
Thanks.
EDIT:
As you pointed out, I accidentally wrote “length” but I meant “dimensional length” (dimensions) in my head. Sorry.
Oh, thank you for the clarification. I replaced it with AbstractVector.
But concerning this question, how can I guarantee, that parameter 1 and parameter 2 both share an Array with same number of dimensions?
Okay, so Julia does not officially support generic variables for concrete type parameters, right?
(For example, in languages with template parameterization, they often allow for template parameters which are constants or concrete values, not just types.)
EDIT:
The proper functionality in my original post can be written as
function dot_product(x::AbstractVector, y::AbstractVector)
return sum(pair -> pair[1] * pair[2], zip(x,y))
end
I don’t know, why Julia does not destructure a single tuple argument into multiple arguments, the pair thing is needed, but it gives me the same results like · from LinearAlgebra package.
I don’t know at the moment, if conventional C++ compilers would infer the N automatically, but in D for example, it is able to infer it for some cases.
Compile-time variables for concrete values make a type system more flexible and better usable because you get earlier compiler warnings and don’t need to write explicit asserts or similar. (Type annotations are basically just compile-time-evaluated assertion shortcuts.)
My question only was how to do that in Julia, when supported.