/* Vector products (Rosetta code) in Picat. http://rosettacode.org/wiki/Vector_products """ Define a vector having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z). If you imagine a graph with the x and y axis being at right angles to each other and having a third, z axis coming out of the page, then a triplet of numbers, (X, Y, Z) would represent a point in the region, and a vector from the origin to the point. Given vectors A = (a1, a2, a3); B = (b1, b2, b3); and C = (c1, c2, c3); then the following common vector products are defined: - The dot product A • B = a1b1 + a2b2 + a3b3; a scalar quantity - The cross product A x B = (a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1); a vector quantity - The scalar triple product A • (B x C); a scalar quantity - The vector triple product A x (B x C); a vector quantity Task description Given the three vectors: a = (3, 4, 5); b = (4, 3, 5); c = (-5, -12, -13): Create a named function/subroutine/method to compute the dot product of two vectors. Create a function to compute the cross product of two vectors. Optionally create a function to compute the scalar triple product of three vectors. Optionally create a function to compute the vector triple product of three vectors. Compute and display: a • b Compute and display: a x b Compute and display: a • b x c, the scaler triple product. Compute and display: a x b x c, the vector triple product. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => A = [3, 4, 5], B = [4, 3, 5], C = [-5, -12, -13], println(a=A), println(b=B), println(c=C), println("A . B"=dot(A,B)), println("A x B"=cross(A,B)), println("A . (B x C)"=scalar_triple(A,B,C)), println("A X (B X C)"=vector_triple(A,B,C)), nl. % dot(A,B) = sum([A[I]*B[I] : I in 1..length(A)]). dot(A,B) = sum([ AA*BB : {AA,BB} in zip(A,B)]). cross(A,B) = [A[2]*B[3]-A[3]*B[2], A[3]*B[1]-A[1]*B[3], A[1]*B[2]-A[2]*B[1]]. scalar_triple(A,B,C) = dot(A,cross(B,C)). vector_triple(A,B,C) = cross(A,cross(B,C)).