Vector products: Difference between revisions

Added zkl
No edit summary
(Added zkl)
Line 2,686:
6
-267 204 -3
</pre>
 
=={{header|zkl}}==
Since the input vectors are all int, the output is int. For a float output, use float data (or convert) in the input vectors and change sum() to sum(0.0) (in dotp).
 
The [(a1,a2,a3)] parameter notation just means add a preamble to the function body to do list assignment: a1,a2,a3:=arglist[0]. Since we don't need the vector as such, don't bother to name it (in the parameter list)
<lang zkl>fcn dotp(a,b){ a.zipWith('*,b).sum() } //1 slow but concise
fcn crossp([(a1,a2,a3)],[(b1,b2,b3)]) //2
{ T(a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1) }
 
var A=T(3,4,5), B=T(4,3,5), C=T(-5,-12,-13);
dotp(A,B) //5
crossp(A,B) //6
 
dotp(A, crossp(B,C)) //7
crossp(A, crossp(B,C)) //8</lang>
{{out}}
<pre>
49
L(5,5,-7)
6
L(-267,204,-3)
</pre>
Anonymous user