Task

Create a function/use an in-built function, to compute the   dot product,   also known as the   scalar product   of two vectors.

If possible, make the vectors of arbitrary length.



As an example, compute the dot product of the vectors:

     [1,  3, -5]       and 
     [4, -2, -1]   


If implementing the dot product of two vectors directly:

   each vector must be the same length
   multiply corresponding terms from each vector
   sum the products   (to produce the answer)


Related task

   Vector products


#define NAN 0.0/0.0  'dot product of different-dimensioned vectors is no more defined than 0/0    
 
function dot( a() as double, b() as double ) as double
    if ubound(a)<>ubound(b) then return NAN
    dim as uinteger i
    dim as double dp = 0.0
    for i = 0 to ubound(a)
        dp += a(i)*b(i)
    next i
    return dp
end function
 
dim as double zero3d(0 to 2) = {0.0, 0.0, 0.0}     'some example vectors
dim as double zero5d(0 to 4) = {0.0, 0.0, 0.0, 0.0, 0.0}
dim as double x(0 to 2) = {1.0, 0.0, 0.0}
dim as double y(0 to 2) = {0.0, 1.0, 0.0}
dim as double z(0 to 2) = {0.0, 0.0, 1.0}
dim as double q(0 to 2) = {1.0, 1.0, 3.14159}
dim as double r(0 to 2) = {-1.0, 2.618033989, 3.0}
 
print " q dot r           = ", dot(q(), r())
print " zero3d dot zero5d = ", dot(zero3d(), zero5d())
print " zero3d dot x      = ", dot(zero3d(), x())
print " z dot z           = ", dot(z(), z())
print " y dot z           = ", dot(y(), z())