How about a double for? Saves the allocation of the intermediate vector. For completeness, in code:
vMv{T}(v::Vector{T},M::Matrix{T}) = begin
size(M) == (size(v,1),size(v,1)) || throw("Vector/Matrix size mismatch")
res = zero(T)
for i = 1:size(v,1)
for j = 1:size(v,1)
res += v[i]*v[j]*M[i,j]
end
end
res
end
Adding some @inboundsand maybe @simdwill be optimized.
source
share