Dot product: Difference between revisions

Content added Content deleted
Line 1,911: Line 1,911:


=={{header|M2000 Interpreter}}==
=={{header|M2000 Interpreter}}==
Version 12 can use types for arrays (earlier versions use variant type by default).

So we can adjust the return value of Dot() to be the same as the first item of first array. All functions of M2000 return variant type (including objects) or array of variant values, for multiple values (which is an object too).

<syntaxhighlight lang="m2000 interpreter">
<syntaxhighlight lang="m2000 interpreter">
Module dot_product {
Module dot_product {
Line 1,918: Line 1,922:
if len(a)<>len(b) Then Error "not same length"
if len(a)<>len(b) Then Error "not same length"
if len(a)=0 then Error "empty vectors"
if len(a)=0 then Error "empty vectors"
Let a1=each(a), b1=each(b), sum=0
object a1=each(a), b1=each(b)
// take type by first item in a()
long lowbound=dimension(a,1, 0)
sum=a#val(lowbound)-a#val(lowbound)
While a1, b1 {sum+=array(a1)*array(b1)}
While a1, b1 {sum+=array(a1)*array(b1)}
=sum
=sum
}
}
Print Dot(A, B)
Print Dot(A, B)=3
Print Dot((1,3,-5), (4,-2,-1))
Print Dot((1,3,-5), (4,-2,-1), 0)=3
dim k(2 to 4) as long long, z(3) as long long
k(2)=1,3,-5
z(0)=4,-2,-1
result=Dot(k(), z())
Print result=3, type$(result)="Long Long"
}
}
Module dot_product
Module dot_product