Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
2
Task/Vector-products/00-META.yaml
Normal file
2
Task/Vector-products/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Vector_products
|
||||
46
Task/Vector-products/00-TASK.txt
Normal file
46
Task/Vector-products/00-TASK.txt
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
A vector is defined as 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 the vectors:
|
||||
<big> A = (a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>) </big>
|
||||
<big> B = (b<sub>1</sub>, b<sub>2</sub>, b<sub>3</sub>) </big>
|
||||
<big> C = (c<sub>1</sub>, c<sub>2</sub>, c<sub>3</sub>) </big>
|
||||
then the following common vector products are defined:
|
||||
* '''The dot product''' (a scalar quantity)
|
||||
:::: <big> A • B = a<sub>1</sub>b<sub>1</sub> + a<sub>2</sub>b<sub>2</sub> + a<sub>3</sub>b<sub>3</sub> </big>
|
||||
* '''The cross product''' (a vector quantity)
|
||||
:::: <big> A x B = (a<sub>2</sub>b<sub>3</sub> - a<sub>3</sub>b<sub>2</sub>, a<sub>3</sub>b<sub>1</sub> - a<sub>1</sub>b<sub>3</sub>, a<sub>1</sub>b<sub>2</sub> - a<sub>2</sub>b<sub>1</sub>) </big>
|
||||
* '''The scalar triple product''' (a scalar quantity)
|
||||
:::: <big> A • (B x C) </big>
|
||||
* '''The vector triple product''' (a vector quantity)
|
||||
:::: <big> A x (B x C) </big>
|
||||
|
||||
|
||||
;Task:
|
||||
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: <code>a • b</code>
|
||||
# Compute and display: <code>a x b</code>
|
||||
# Compute and display: <code>a • (b x c)</code>, the scalar triple product.
|
||||
# Compute and display: <code>a x (b x c)</code>, the vector triple product.
|
||||
|
||||
|
||||
;References:
|
||||
* A starting page on Wolfram MathWorld is {{Wolfram|Vector|Multiplication}}.
|
||||
* Wikipedia [[wp:Dot product|dot product]].
|
||||
* Wikipedia [[wp:Cross product|cross product]].
|
||||
* Wikipedia [[wp:Triple product|triple product]].
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Dot product]]
|
||||
* [[Quaternion type]]
|
||||
<br><br>
|
||||
|
||||
15
Task/Vector-products/11l/vector-products.11l
Normal file
15
Task/Vector-products/11l/vector-products.11l
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
F scalartriplep(a, b, c)
|
||||
return dot(a, cross(b, c))
|
||||
|
||||
F vectortriplep(a, b, c)
|
||||
return cross(a, cross(b, c))
|
||||
|
||||
V a = (3, 4, 5)
|
||||
V b = (4, 3, 5)
|
||||
V c = (-5, -12, -13)
|
||||
|
||||
print(‘a = #.; b = #.; c = #.’.format(a, b, c))
|
||||
print(‘a . b = #.’.format(dot(a, b)))
|
||||
print(‘a x b = #.’.format(cross(a,b)))
|
||||
print(‘a . (b x c) = #.’.format(scalartriplep(a, b, c)))
|
||||
print(‘a x (b x c) = #.’.format(vectortriplep(a, b, c)))
|
||||
53
Task/Vector-products/ALGOL-68/vector-products.alg
Normal file
53
Task/Vector-products/ALGOL-68/vector-products.alg
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
MODE FIELD = INT;
|
||||
FORMAT field fmt = $g(-0)$;
|
||||
|
||||
MODE VEC = [3]FIELD;
|
||||
FORMAT vec fmt = $"("f(field fmt)", "f(field fmt)", "f(field fmt)")"$;
|
||||
|
||||
PROC crossp = (VEC a, b)VEC:(
|
||||
#Cross product of two 3D vectors#
|
||||
CO ASSERT(LWB a = LWB b AND UPB a = UPB b AND UPB b = 3 # "For 3D vectors only" #); CO
|
||||
(a[2]*b[3] - a[3]*b[2], a[3]*b[1] - a[1]*b[3], a[1]*b[2] - a[2]*b[1])
|
||||
);
|
||||
|
||||
PRIO MAXLWB = 8, MINUPB=8;
|
||||
|
||||
OP MAXLWB = (VEC a, b)INT: (LWB a<LWB b|LWB a|LWB b);
|
||||
OP MINUPB = (VEC a, b)INT: (UPB a>UPB b|UPB a|UPB b);
|
||||
|
||||
PROC dotp = (VEC a, b)FIELD:(
|
||||
#Dot product of two vectors#
|
||||
FIELD sum := 0;
|
||||
FOR i FROM a MAXLWB b TO a MINUPB b DO sum +:= a[i]*b[i] OD;
|
||||
sum
|
||||
);
|
||||
|
||||
PROC scalartriplep = (VEC a, b, c)VEC:(
|
||||
#Scalar triple product of three vectors: "a . (b x c)"#
|
||||
dotp(a, crossp(b, c))
|
||||
);
|
||||
|
||||
PROC vectortriplep = (VEC a, b, c)VEC:(
|
||||
#Vector triple product of three vectors: "a x (b x c)"#
|
||||
crossp(a, crossp(b, c))
|
||||
);
|
||||
|
||||
# Declare some useful operators #
|
||||
PRIO DOT = 5, X = 5;
|
||||
OP (VEC, VEC)FIELD DOT = dotp;
|
||||
OP (VEC, VEC)VEC X = crossp;
|
||||
|
||||
main:(
|
||||
VEC a=(3, 4, 5), b=(4, 3, 5), c=(-5, -12, -13);
|
||||
printf(($"a = "f(vec fmt)"; b = "f(vec fmt)"; c = "f(vec fmt)l$ , a, b, c));
|
||||
printf($"Using PROCedures:"l$);
|
||||
printf(($"a . b = "f(field fmt)l$, dotp(a,b)));
|
||||
printf(($"a x b = "f(vec fmt)l$, crossp(a,b)));
|
||||
printf(($"a . (b x c) = "f(field fmt)l$, scalartriplep(a, b, c)));
|
||||
printf(($"a x (b x c) = "f(vec fmt)l$, vectortriplep(a, b, c)));
|
||||
printf($"Using OPerators:"l$);
|
||||
printf(($"a . b = "f(field fmt)l$, a DOT b));
|
||||
printf(($"a x b = "f(vec fmt)l$, a X b));
|
||||
printf(($"a . (b x c) = "f(field fmt)l$, a DOT (b X c)));
|
||||
printf(($"a x (b x c) = "f(vec fmt)l$, a X (b X c)))
|
||||
)
|
||||
45
Task/Vector-products/ALGOL-W/vector-products.alg
Normal file
45
Task/Vector-products/ALGOL-W/vector-products.alg
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
begin
|
||||
% define the Vector record type %
|
||||
record Vector( integer X, Y, Z );
|
||||
|
||||
% calculates the dot product of two Vectors %
|
||||
integer procedure dotProduct( reference(Vector) value A, B ) ;
|
||||
( X(A) * X(B) ) + ( Y(A) * Y(B) ) + ( Z(A) * Z(B) );
|
||||
|
||||
% calculates the cross product or two Vectors %
|
||||
reference(Vector) procedure crossProduct( reference(Vector) value A, B ) ;
|
||||
Vector( ( Y(A) * Z(B) ) - ( Z(A) * Y(B) )
|
||||
, ( Z(A) * X(B) ) - ( X(A) * Z(B) )
|
||||
, ( X(A) * Y(B) ) - ( Y(A) * X(B) )
|
||||
);
|
||||
|
||||
% calculates the scaler triple product of two vectors %
|
||||
integer procedure scalerTripleProduct( reference(Vector) value A, B, C ) ;
|
||||
dotProduct( A, crossProduct( B, C ) );
|
||||
|
||||
% calculates the vector triple product of two vectors %
|
||||
reference(Vector) procedure vectorTripleProduct( reference(Vector) value A, B, C ) ;
|
||||
crossProduct( A, crossProduct( B, C ) );
|
||||
|
||||
% test the Vector routines %
|
||||
begin
|
||||
procedure writeonVector( reference(Vector) value v ) ;
|
||||
writeon( "(", X(v), ", ", Y(v), ", ", Z(v), ")" );
|
||||
|
||||
Reference(Vector) a, b, c;
|
||||
|
||||
a := Vector( 3, 4, 5 );
|
||||
b := Vector( 4, 3, 5 );
|
||||
c := Vector( -5, -12, -13 );
|
||||
|
||||
i_w := 1; s_w := 0; % set output formatting %
|
||||
|
||||
write( " a: " ); writeonVector( a );
|
||||
write( " b: " ); writeonVector( b );
|
||||
write( " c: " ); writeonVector( c );
|
||||
write( " a . b: ", dotProduct( a, b ) );
|
||||
write( " a x b: " ); writeonVector( crossProduct( a, b ) );
|
||||
write( "a . ( b x c ): ", scalerTripleProduct( a, b, c ) );
|
||||
write( "a x ( b x c ): " ); writeonVector( vectorTripleProduct( a, b, c ) )
|
||||
end
|
||||
end.
|
||||
2
Task/Vector-products/APL/vector-products-1.apl
Normal file
2
Task/Vector-products/APL/vector-products-1.apl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
dot ← +.×
|
||||
cross ← 1⌽(⊣×1⌽⊢)-⊢×1⌽⊣
|
||||
11
Task/Vector-products/APL/vector-products-2.apl
Normal file
11
Task/Vector-products/APL/vector-products-2.apl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
a←3 4 5
|
||||
b←4 3 5
|
||||
c←¯5 ¯12 ¯13
|
||||
a dot b
|
||||
49
|
||||
a cross b
|
||||
5 5 ¯7
|
||||
a dot b cross c
|
||||
6
|
||||
a cross b cross c
|
||||
¯267 204 ¯3
|
||||
29
Task/Vector-products/AWK/vector-products.awk
Normal file
29
Task/Vector-products/AWK/vector-products.awk
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
a[1] = 3; a[2]= 4; a[3] = 5;
|
||||
b[1] = 4; b[2]= 3; b[3] = 5;
|
||||
c[1] = -5; c[2]= -12; c[3] = -13;
|
||||
|
||||
print "a = ",printVec(a);
|
||||
print "b = ",printVec(b);
|
||||
print "c = ",printVec(c);
|
||||
print "a.b = ",dot(a,b);
|
||||
## upper case variables are used as temporary or intermediate results
|
||||
cross(a,b,D);print "a.b = ",printVec(D);
|
||||
cross(b,c,D);print "a.(b x c) = ",dot(a,D);
|
||||
cross(b,c,D);cross(a,D,E); print "a x (b x c) = ",printVec(E);
|
||||
}
|
||||
|
||||
function dot(A,B) {
|
||||
return A[1]*B[1]+A[2]*B[2]+A[3]*B[3];
|
||||
}
|
||||
|
||||
function cross(A,B,C) {
|
||||
C[1] = A[2]*B[3]-A[3]*B[2];
|
||||
C[2] = A[3]*B[1]-A[1]*B[3];
|
||||
C[3] = A[1]*B[2]-A[2]*B[1];
|
||||
}
|
||||
|
||||
function printVec(C) {
|
||||
return "[ "C[1]" "C[2]" "C[3]" ]";
|
||||
}
|
||||
54
Task/Vector-products/Action-/vector-products.action
Normal file
54
Task/Vector-products/Action-/vector-products.action
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
TYPE Vector=[INT x,y,z]
|
||||
|
||||
PROC CreateVector(INT vx,vy,vz Vector POINTER v)
|
||||
v.x=vx v.y=vy v.z=vz
|
||||
RETURN
|
||||
|
||||
PROC PrintVector(Vector POINTER v)
|
||||
PrintF("(%I,%I,%I)",v.x,v.y,v.z)
|
||||
RETURN
|
||||
|
||||
INT FUNC DotProduct(Vector POINTER v1,v2)
|
||||
INT res
|
||||
|
||||
res=v1.x*v2.x ;calculation split into parts
|
||||
res==+v1.y*v2.y ;otherwise incorrect result
|
||||
res==+v1.z*v2.z ;is returned
|
||||
RETURN (res)
|
||||
|
||||
PROC CrossProduct(Vector POINTER v1,v2,res)
|
||||
res.x=v1.y*v2.z ;calculation split into parts
|
||||
res.x==-v1.z*v2.y ;otherwise incorrect result
|
||||
res.y=v1.z*v2.x ;is returned
|
||||
res.y==-v1.x*v2.z
|
||||
res.z=v1.x*v2.y
|
||||
res.z==-v1.y*v2.x
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
Vector a,b,c,d,e
|
||||
INT res
|
||||
|
||||
CreateVector(3,4,5,a)
|
||||
CreateVector(4,3,5,b)
|
||||
CreateVector(-5,-12,-13,c)
|
||||
|
||||
Print("a=") PrintVector(a) PutE()
|
||||
Print("b=") PrintVector(b) PutE()
|
||||
Print("c=") PrintVector(c) PutE()
|
||||
PutE()
|
||||
|
||||
res=DotProduct(a,b)
|
||||
PrintF("a.b=%I%E",res)
|
||||
|
||||
CrossProduct(a,b,d)
|
||||
Print("axb=") PrintVector(d) PutE()
|
||||
|
||||
CrossProduct(b,c,d)
|
||||
res=DotProduct(a,d)
|
||||
PrintF("a.(bxc)=%I%E",res)
|
||||
|
||||
CrossProduct(b,c,d)
|
||||
CrossProduct(a,d,e)
|
||||
Print("ax(bxc)=") PrintVector(e) PutE()
|
||||
RETURN
|
||||
78
Task/Vector-products/Ada/vector-products.ada
Normal file
78
Task/Vector-products/Ada/vector-products.ada
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Vector is
|
||||
type Float_Vector is array (Positive range <>) of Float;
|
||||
package Float_IO is new Ada.Text_IO.Float_IO (Float);
|
||||
|
||||
procedure Vector_Put (X : Float_Vector) is
|
||||
begin
|
||||
Ada.Text_IO.Put ("(");
|
||||
for I in X'Range loop
|
||||
Float_IO.Put (X (I), Aft => 1, Exp => 0);
|
||||
if I /= X'Last then
|
||||
Ada.Text_IO.Put (", ");
|
||||
end if;
|
||||
end loop;
|
||||
Ada.Text_IO.Put (")");
|
||||
end Vector_Put;
|
||||
|
||||
-- cross product
|
||||
function "*" (Left, Right : Float_Vector) return Float_Vector is
|
||||
begin
|
||||
if Left'Length /= Right'Length then
|
||||
raise Constraint_Error with "vectors of different size in dot product";
|
||||
end if;
|
||||
if Left'Length /= 3 then
|
||||
raise Constraint_Error with "dot product only implemented for R**3";
|
||||
end if;
|
||||
return Float_Vector'(Left (Left'First + 1) * Right (Right'First + 2) -
|
||||
Left (Left'First + 2) * Right (Right'First + 1),
|
||||
Left (Left'First + 2) * Right (Right'First) -
|
||||
Left (Left'First) * Right (Right'First + 2),
|
||||
Left (Left'First) * Right (Right'First + 1) -
|
||||
Left (Left'First + 1) * Right (Right'First));
|
||||
end "*";
|
||||
|
||||
-- scalar product
|
||||
function "*" (Left, Right : Float_Vector) return Float is
|
||||
Result : Float := 0.0;
|
||||
I, J : Positive;
|
||||
begin
|
||||
if Left'Length /= Right'Length then
|
||||
raise Constraint_Error with "vectors of different size in scalar product";
|
||||
end if;
|
||||
I := Left'First; J := Right'First;
|
||||
while I <= Left'Last and then J <= Right'Last loop
|
||||
Result := Result + Left (I) * Right (J);
|
||||
I := I + 1; J := J + 1;
|
||||
end loop;
|
||||
return Result;
|
||||
end "*";
|
||||
|
||||
-- stretching
|
||||
function "*" (Left : Float_Vector; Right : Float) return Float_Vector is
|
||||
Result : Float_Vector (Left'Range);
|
||||
begin
|
||||
for I in Left'Range loop
|
||||
Result (I) := Left (I) * Right;
|
||||
end loop;
|
||||
return Result;
|
||||
end "*";
|
||||
|
||||
A : constant Float_Vector := (3.0, 4.0, 5.0);
|
||||
B : constant Float_Vector := (4.0, 3.0, 5.0);
|
||||
C : constant Float_Vector := (-5.0, -12.0, -13.0);
|
||||
begin
|
||||
Ada.Text_IO.Put ("A: "); Vector_Put (A); Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("B: "); Vector_Put (B); Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("C: "); Vector_Put (C); Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A dot B = "); Float_IO.Put (A * B, Aft => 1, Exp => 0);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A x B = "); Vector_Put (A * B);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A dot (B x C) = "); Float_IO.Put (A * (B * C), Aft => 1, Exp => 0);
|
||||
Ada.Text_IO.New_Line;
|
||||
Ada.Text_IO.Put ("A x (B x C) = "); Vector_Put (A * Float_Vector'(B * C));
|
||||
Ada.Text_IO.New_Line;
|
||||
end Vector;
|
||||
256
Task/Vector-products/AppleScript/vector-products.applescript
Normal file
256
Task/Vector-products/AppleScript/vector-products.applescript
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
--------------------- VECTOR PRODUCTS ---------------------
|
||||
|
||||
-- dotProduct :: Num a => [a] -> [a] -> Either String a
|
||||
on dotProduct(xs, ys)
|
||||
-- Dot product of two vectors of equal dimension.
|
||||
|
||||
if length of xs = length of ys then
|
||||
|Right|(sum(zipWith(my mul, xs, ys)))
|
||||
else
|
||||
|Left|("Dot product not defined for vectors of differing dimension.")
|
||||
end if
|
||||
end dotProduct
|
||||
|
||||
|
||||
-- crossProduct :: Num a => (a, a, a) -> (a, a, a)
|
||||
-- Either String -> (a, a, a)
|
||||
on crossProduct(xs, ys)
|
||||
-- The cross product of two 3D vectors.
|
||||
|
||||
if 3 ≠ length of xs or 3 ≠ length of ys then
|
||||
|Left|("Cross product is defined only for 3d vectors.")
|
||||
else
|
||||
set {x1, x2, x3} to xs
|
||||
set {y1, y2, y3} to ys
|
||||
|Right|({¬
|
||||
x2 * y3 - x3 * y2, ¬
|
||||
x3 * y1 - x1 * y3, ¬
|
||||
x1 * y2 - x2 * y1})
|
||||
end if
|
||||
end crossProduct
|
||||
|
||||
|
||||
-- scalarTriple :: Num a => (a, a, a) -> (a, a, a) -> (a, a a) ->
|
||||
-- Either String -> a
|
||||
on scalarTriple(q, r, s)
|
||||
-- The scalar triple product.
|
||||
|
||||
script go
|
||||
on |λ|(ys)
|
||||
dotProduct(q, ys)
|
||||
end |λ|
|
||||
end script
|
||||
bindLR(crossProduct(r, s), go)
|
||||
end scalarTriple
|
||||
|
||||
|
||||
-- vectorTriple :: Num a => (a, a, a) -> (a, a, a) -> (a, a a) ->
|
||||
-- Either String -> (a, a, a)
|
||||
on vectorTriple(q, r, s)
|
||||
-- The vector triple product.
|
||||
|
||||
script go
|
||||
on |λ|(ys)
|
||||
crossProduct(q, ys)
|
||||
end |λ|
|
||||
end script
|
||||
bindLR(crossProduct(r, s), go)
|
||||
end vectorTriple
|
||||
|
||||
|
||||
-------------------------- TEST ---------------------------
|
||||
on run
|
||||
set a to {3, 4, 5}
|
||||
set b to {4, 3, 5}
|
||||
set c to {-5, -12, -13}
|
||||
set d to {3, 4, 5, 6}
|
||||
|
||||
|
||||
script test
|
||||
on |λ|(f)
|
||||
either(my identity, my show, ¬
|
||||
mReturn(f)'s |λ|(a, b, c, d))
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
|
||||
tell test
|
||||
unlines({¬
|
||||
"a . b = " & |λ|(dotProduct), ¬
|
||||
"a x b = " & |λ|(crossProduct), ¬
|
||||
"a . (b x c) = " & |λ|(scalarTriple), ¬
|
||||
"a x (b x c) = " & |λ|(vectorTriple), ¬
|
||||
"a x d = " & either(my identity, my show, ¬
|
||||
dotProduct(a, d)), ¬
|
||||
"a . (b x d) = " & either(my identity, my show, ¬
|
||||
scalarTriple(a, b, d)) ¬
|
||||
})
|
||||
end tell
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS --------------------
|
||||
|
||||
-- Left :: a -> Either a b
|
||||
on |Left|(x)
|
||||
{type:"Either", |Left|:x, |Right|:missing value}
|
||||
end |Left|
|
||||
|
||||
|
||||
-- Right :: b -> Either a b
|
||||
on |Right|(x)
|
||||
{type:"Either", |Left|:missing value, |Right|:x}
|
||||
end |Right|
|
||||
|
||||
|
||||
-- bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
|
||||
on bindLR(m, mf)
|
||||
if missing value is not |Left| of m then
|
||||
m
|
||||
else
|
||||
mReturn(mf)'s |λ|(|Right| of m)
|
||||
end if
|
||||
end bindLR
|
||||
|
||||
|
||||
-- either :: (a -> c) -> (b -> c) -> Either a b -> c
|
||||
on either(lf, rf, e)
|
||||
if missing value is |Left| of e then
|
||||
tell mReturn(rf) to |λ|(|Right| of e)
|
||||
else
|
||||
tell mReturn(lf) to |λ|(|Left| of e)
|
||||
end if
|
||||
end either
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- identity :: a -> a
|
||||
on identity(x)
|
||||
-- The argument unchanged.
|
||||
x
|
||||
end identity
|
||||
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(delim, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, delim}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end intercalate
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
-- The list obtained by applying f
|
||||
-- to each element of xs.
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- min :: Ord a => a -> a -> a
|
||||
on min(x, y)
|
||||
if y < x then
|
||||
y
|
||||
else
|
||||
x
|
||||
end if
|
||||
end min
|
||||
|
||||
|
||||
-- mul :: Num a :: a -> a -> a
|
||||
on mul(x, y)
|
||||
x * y
|
||||
end mul
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- show :: a -> String
|
||||
on show(x)
|
||||
if list is class of x then
|
||||
showList(x)
|
||||
else
|
||||
str(x)
|
||||
end if
|
||||
end show
|
||||
|
||||
|
||||
-- showList :: [a] -> String
|
||||
on showList(xs)
|
||||
"[" & intercalate(", ", map(my str, xs)) & "]"
|
||||
end showList
|
||||
|
||||
|
||||
-- str :: a -> String
|
||||
on str(x)
|
||||
x as string
|
||||
end str
|
||||
|
||||
|
||||
-- sum :: [Number] -> Number
|
||||
on sum(xs)
|
||||
script add
|
||||
on |λ|(a, b)
|
||||
a + b
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(add, 0, xs)
|
||||
end sum
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
-- A single string formed by the intercalation
|
||||
-- of a list of strings with the newline character.
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end unlines
|
||||
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to min(length of xs, length of ys)
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, item i of ys)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith
|
||||
32
Task/Vector-products/Arturo/vector-products.arturo
Normal file
32
Task/Vector-products/Arturo/vector-products.arturo
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
; dot product
|
||||
dot: function [a b][
|
||||
sum map couple a b => product
|
||||
]
|
||||
|
||||
; cross product
|
||||
cross: function [a b][
|
||||
A: (a\1 * b\2) - a\2 * b\1
|
||||
B: (a\2 * b\0) - a\0 * b\2
|
||||
C: (a\0 * b\1) - a\1 * b\0
|
||||
@[A B C]
|
||||
]
|
||||
|
||||
; scalar triple product
|
||||
stp: function [a b c][
|
||||
dot a cross b c
|
||||
]
|
||||
|
||||
; vector triple product
|
||||
vtp: function [a b c][
|
||||
cross a cross b c
|
||||
]
|
||||
|
||||
; task
|
||||
a: [3 4 5]
|
||||
b: [4 3 5]
|
||||
c: @[neg 5 neg 12 neg 13]
|
||||
|
||||
print ["a • b =", dot a b]
|
||||
print ["a x b =", cross a b]
|
||||
print ["a • (b x c) =", stp a b c]
|
||||
print ["a x (b x c) =", vtp a b c]
|
||||
30
Task/Vector-products/AutoHotkey/vector-products.ahk
Normal file
30
Task/Vector-products/AutoHotkey/vector-products.ahk
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
V := {a: [3, 4, 5], b: [4, 3, 5], c: [-5, -12, -13]}
|
||||
|
||||
for key, val in V
|
||||
Out .= key " = (" val[1] ", " val[2] ", " val[3] ")`n"
|
||||
|
||||
CP := CrossProduct(V.a, V.b)
|
||||
VTP := VectorTripleProduct(V.a, V.b, V.c)
|
||||
|
||||
MsgBox, % Out "`na • b = " DotProduct(V.a, V.b) "`n"
|
||||
. "a x b = (" CP[1] ", " CP[2] ", " CP[3] ")`n"
|
||||
. "a • b x c = " ScalerTripleProduct(V.a, V.b, V.c) "`n"
|
||||
. "a x b x c = (" VTP[1] ", " VTP[2] ", " VTP[3] ")"
|
||||
|
||||
DotProduct(v1, v2) {
|
||||
return, v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3]
|
||||
}
|
||||
|
||||
CrossProduct(v1, v2) {
|
||||
return, [v1[2] * v2[3] - v1[3] * v2[2]
|
||||
, v1[3] * v2[1] - v1[1] * v2[3]
|
||||
, v1[1] * v2[2] - v1[2] * v2[1]]
|
||||
}
|
||||
|
||||
ScalerTripleProduct(v1, v2, v3) {
|
||||
return, DotProduct(v1, CrossProduct(v2, v3))
|
||||
}
|
||||
|
||||
VectorTripleProduct(v1, v2, v3) {
|
||||
return, CrossProduct(v1, CrossProduct(v2, v3))
|
||||
}
|
||||
32
Task/Vector-products/BASIC256/vector-products.basic
Normal file
32
Task/Vector-products/BASIC256/vector-products.basic
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
a={3,4,5}:b={4,3,5}:c={-5,-12,-13}
|
||||
|
||||
print "A.B = "+dot_product(ref(a),ref(b))
|
||||
call cross_product(ref(a),ref(b),ref(y))
|
||||
Print "AxB = ("+y[0]+","+y[1]+","+y[2]+")"
|
||||
print "A.(BxC) = "+s_tri(ref(a),ref(b),ref(c))
|
||||
call v_tri(ref(a),ref(b),ref(c),ref(x),ref(y))
|
||||
Print "A x (BxC) = ("+y[0]+","+y[1]+","+y[2]+")"
|
||||
|
||||
function dot_product(ref(x1),ref(x2))
|
||||
dot_product= 0
|
||||
for t = 0 to 2
|
||||
dot_product += x1[t]*x2[t]
|
||||
next t
|
||||
end function
|
||||
|
||||
subroutine cross_product(ref(x1),ref(x2),ref(y1))
|
||||
y1={0,0,0}
|
||||
y1[0]=x1[1]*x2[2]-x1[2]*x2[1]
|
||||
y1[1]=x1[2]*x2[0]-x1[0]*x2[2]
|
||||
y1[2]=x1[0]*x2[1]-x1[1]*x2[0]
|
||||
end subroutine
|
||||
|
||||
function s_tri(ref(x1),ref(x2),ref(x3))
|
||||
call cross_product(ref(x2),ref(x3),ref(y1))
|
||||
s_tri=dot_product(ref(x1),ref(y1))
|
||||
end function
|
||||
|
||||
subroutine v_tri(ref(x1),ref(x2),ref(x3),ref(y1),ref(y2))
|
||||
call cross_product(ref(x2),ref(x3),ref(y1))
|
||||
call cross_product(ref(x1),ref(y1),ref(y2))
|
||||
end subroutine
|
||||
31
Task/Vector-products/BBC-BASIC/vector-products.basic
Normal file
31
Task/Vector-products/BBC-BASIC/vector-products.basic
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
DIM a(2), b(2), c(2), d(2)
|
||||
a() = 3, 4, 5
|
||||
b() = 4, 3, 5
|
||||
c() = -5, -12, -13
|
||||
|
||||
PRINT "a . b = "; FNdot(a(),b())
|
||||
PROCcross(a(),b(),d())
|
||||
PRINT "a x b = (";d(0)", ";d(1)", ";d(2)")"
|
||||
PRINT "a . (b x c) = "; FNscalartriple(a(),b(),c())
|
||||
PROCvectortriple(a(),b(),c(),d())
|
||||
PRINT "a x (b x c) = (";d(0)", ";d(1)", ";d(2)")"
|
||||
END
|
||||
|
||||
DEF FNdot(A(),B())
|
||||
LOCAL C() : DIM C(0,0)
|
||||
C() = A().B()
|
||||
= C(0,0)
|
||||
|
||||
DEF PROCcross(A(),B(),C())
|
||||
C() = A(1)*B(2)-A(2)*B(1), A(2)*B(0)-A(0)*B(2), A(0)*B(1)-A(1)*B(0)
|
||||
ENDPROC
|
||||
|
||||
DEF FNscalartriple(A(),B(),C())
|
||||
LOCAL D() : DIM D(2)
|
||||
PROCcross(B(),C(),D())
|
||||
= FNdot(A(),D())
|
||||
|
||||
DEF PROCvectortriple(A(),B(),C(),D())
|
||||
PROCcross(B(),C(),D())
|
||||
PROCcross(A(),D(),D())
|
||||
ENDPROC
|
||||
8
Task/Vector-products/BQN/vector-products-1.bqn
Normal file
8
Task/Vector-products/BQN/vector-products-1.bqn
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Dot ← +´∘×
|
||||
Cross ← 1⊸⌽⊸×{1⌽𝔽˜-𝔽}
|
||||
Triple ← {𝕊a‿b‿c: a Dot b Cross c}
|
||||
VTriple ← Cross´
|
||||
|
||||
a←3‿4‿5
|
||||
b←4‿3‿5
|
||||
c←¯5‿¯12‿¯13
|
||||
11
Task/Vector-products/BQN/vector-products-2.bqn
Normal file
11
Task/Vector-products/BQN/vector-products-2.bqn
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
a Dot b
|
||||
49
|
||||
|
||||
a Cross b
|
||||
⟨ 5 5 ¯7 ⟩
|
||||
|
||||
Triple a‿b‿c
|
||||
6
|
||||
|
||||
VTriple a‿b‿c
|
||||
⟨ ¯267 204 ¯3 ⟩
|
||||
54
Task/Vector-products/C++/vector-products.cpp
Normal file
54
Task/Vector-products/C++/vector-products.cpp
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include <iostream>
|
||||
|
||||
template< class T >
|
||||
class D3Vector {
|
||||
|
||||
template< class U >
|
||||
friend std::ostream & operator<<( std::ostream & , const D3Vector<U> & ) ;
|
||||
|
||||
public :
|
||||
D3Vector( T a , T b , T c ) {
|
||||
x = a ;
|
||||
y = b ;
|
||||
z = c ;
|
||||
}
|
||||
|
||||
T dotproduct ( const D3Vector & rhs ) {
|
||||
T scalar = x * rhs.x + y * rhs.y + z * rhs.z ;
|
||||
return scalar ;
|
||||
}
|
||||
|
||||
D3Vector crossproduct ( const D3Vector & rhs ) {
|
||||
T a = y * rhs.z - z * rhs.y ;
|
||||
T b = z * rhs.x - x * rhs.z ;
|
||||
T c = x * rhs.y - y * rhs.x ;
|
||||
D3Vector product( a , b , c ) ;
|
||||
return product ;
|
||||
}
|
||||
|
||||
D3Vector triplevec( D3Vector & a , D3Vector & b ) {
|
||||
return crossproduct ( a.crossproduct( b ) ) ;
|
||||
}
|
||||
|
||||
T triplescal( D3Vector & a, D3Vector & b ) {
|
||||
return dotproduct( a.crossproduct( b ) ) ;
|
||||
}
|
||||
|
||||
private :
|
||||
T x , y , z ;
|
||||
} ;
|
||||
|
||||
template< class T >
|
||||
std::ostream & operator<< ( std::ostream & os , const D3Vector<T> & vec ) {
|
||||
os << "( " << vec.x << " , " << vec.y << " , " << vec.z << " )" ;
|
||||
return os ;
|
||||
}
|
||||
|
||||
int main( ) {
|
||||
D3Vector<int> a( 3 , 4 , 5 ) , b ( 4 , 3 , 5 ) , c( -5 , -12 , -13 ) ;
|
||||
std::cout << "a . b : " << a.dotproduct( b ) << "\n" ;
|
||||
std::cout << "a x b : " << a.crossproduct( b ) << "\n" ;
|
||||
std::cout << "a . b x c : " << a.triplescal( b , c ) << "\n" ;
|
||||
std::cout << "a x b x c : " << a.triplevec( b , c ) << "\n" ;
|
||||
return 0 ;
|
||||
}
|
||||
27
Task/Vector-products/C-sharp/vector-products.cs
Normal file
27
Task/Vector-products/C-sharp/vector-products.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Windows.Media.Media3D;
|
||||
|
||||
class VectorProducts
|
||||
{
|
||||
static double ScalarTripleProduct(Vector3D a, Vector3D b, Vector3D c)
|
||||
{
|
||||
return Vector3D.DotProduct(a, Vector3D.CrossProduct(b, c));
|
||||
}
|
||||
|
||||
static Vector3D VectorTripleProduct(Vector3D a, Vector3D b, Vector3D c)
|
||||
{
|
||||
return Vector3D.CrossProduct(a, Vector3D.CrossProduct(b, c));
|
||||
}
|
||||
|
||||
static void Main()
|
||||
{
|
||||
var a = new Vector3D(3, 4, 5);
|
||||
var b = new Vector3D(4, 3, 5);
|
||||
var c = new Vector3D(-5, -12, -13);
|
||||
|
||||
Console.WriteLine(Vector3D.DotProduct(a, b));
|
||||
Console.WriteLine(Vector3D.CrossProduct(a, b));
|
||||
Console.WriteLine(ScalarTripleProduct(a, b, c));
|
||||
Console.WriteLine(VectorTripleProduct(a, b, c));
|
||||
}
|
||||
}
|
||||
47
Task/Vector-products/C/vector-products.c
Normal file
47
Task/Vector-products/C/vector-products.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include<stdio.h>
|
||||
|
||||
typedef struct{
|
||||
float i,j,k;
|
||||
}Vector;
|
||||
|
||||
Vector a = {3, 4, 5},b = {4, 3, 5},c = {-5, -12, -13};
|
||||
|
||||
float dotProduct(Vector a, Vector b)
|
||||
{
|
||||
return a.i*b.i+a.j*b.j+a.k*b.k;
|
||||
}
|
||||
|
||||
Vector crossProduct(Vector a,Vector b)
|
||||
{
|
||||
Vector c = {a.j*b.k - a.k*b.j, a.k*b.i - a.i*b.k, a.i*b.j - a.j*b.i};
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
float scalarTripleProduct(Vector a,Vector b,Vector c)
|
||||
{
|
||||
return dotProduct(a,crossProduct(b,c));
|
||||
}
|
||||
|
||||
Vector vectorTripleProduct(Vector a,Vector b,Vector c)
|
||||
{
|
||||
return crossProduct(a,crossProduct(b,c));
|
||||
}
|
||||
|
||||
void printVector(Vector a)
|
||||
{
|
||||
printf("( %f, %f, %f)",a.i,a.j,a.k);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("\n a = "); printVector(a);
|
||||
printf("\n b = "); printVector(b);
|
||||
printf("\n c = "); printVector(c);
|
||||
printf("\n a . b = %f",dotProduct(a,b));
|
||||
printf("\n a x b = "); printVector(crossProduct(a,b));
|
||||
printf("\n a . (b x c) = %f",scalarTripleProduct(a,b,c));
|
||||
printf("\n a x (b x c) = "); printVector(vectorTripleProduct(a,b,c));
|
||||
|
||||
return 0;
|
||||
}
|
||||
67
Task/Vector-products/CLU/vector-products.clu
Normal file
67
Task/Vector-products/CLU/vector-products.clu
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
vector = cluster [T: type] is make, dot_product, cross_product,
|
||||
equal, power, mul, unparse
|
||||
where T has add: proctype (T,T) returns (T) signals (overflow),
|
||||
sub: proctype (T,T) returns (T) signals (overflow),
|
||||
mul: proctype (T,T) returns (T) signals (overflow),
|
||||
equal: proctype (T,T) returns (bool),
|
||||
unparse: proctype (T) returns (string)
|
||||
|
||||
rep = struct[x, y, z: T]
|
||||
|
||||
make = proc (x, y, z: T) returns (cvt)
|
||||
return(rep${x:x, y:y, z:z})
|
||||
end make
|
||||
|
||||
dot_product = proc (a, b: cvt) returns (T) signals (overflow)
|
||||
return (a.x*b.x + a.y*b.y + a.z*b.z) resignal overflow
|
||||
end dot_product
|
||||
|
||||
cross_product = proc (a, b: cvt) returns (cvt) signals (overflow)
|
||||
begin
|
||||
x: T := a.y * b.z - a.z * b.y
|
||||
y: T := a.z * b.x - a.x * b.z
|
||||
z: T := a.x * b.y - a.y * b.x
|
||||
return(down(make(x,y,z)))
|
||||
end resignal overflow
|
||||
end cross_product
|
||||
|
||||
equal = proc (a, b: cvt) returns (bool)
|
||||
return (a.x = b.x & a.y = b.y & a.z = b.z)
|
||||
end equal
|
||||
|
||||
% Allow cross_product to be written as ** and dot_product to be written as *
|
||||
power = proc (a, b: cvt) returns (cvt) signals (overflow)
|
||||
return(down(cross_product(up(a),up(b)))) resignal overflow
|
||||
end power
|
||||
|
||||
mul = proc (a, b: cvt) returns (T) signals (overflow)
|
||||
return(dot_product(up(a),up(b))) resignal overflow
|
||||
end mul
|
||||
|
||||
% Standard to_string routine. Properly, `parse' should also be defined,
|
||||
% and x = parse(unparse(x)) forall x; but I'm not bothering here.
|
||||
unparse = proc (v: cvt) returns (string)
|
||||
return( "(" || T$unparse(v.x)
|
||||
|| ", " || T$unparse(v.y)
|
||||
|| ", " || T$unparse(v.z) || ")" )
|
||||
end unparse
|
||||
end vector
|
||||
|
||||
start_up = proc ()
|
||||
vi = vector[int] % integer math is good enough for the examples
|
||||
|
||||
po: stream := stream$primary_output()
|
||||
|
||||
a, b, c: vi
|
||||
a := vi$make(3, 4, 5)
|
||||
b := vi$make(4, 3, 5)
|
||||
c := vi$make(-5, -12, -13)
|
||||
|
||||
stream$putl(po, " a = " || vi$unparse(a))
|
||||
stream$putl(po, " b = " || vi$unparse(b))
|
||||
stream$putl(po, " c = " || vi$unparse(c))
|
||||
stream$putl(po, " a . b = " || int$unparse(a * b))
|
||||
stream$putl(po, " a x b = " || vi$unparse(a ** b))
|
||||
stream$putl(po, "a . (b x c) = " || int$unparse(a * b ** c))
|
||||
stream$putl(po, "a x (b x c) = " || vi$unparse(a ** b ** c))
|
||||
end start_up
|
||||
28
Task/Vector-products/Ceylon/vector-products.ceylon
Normal file
28
Task/Vector-products/Ceylon/vector-products.ceylon
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
shared void run() {
|
||||
|
||||
alias Vector => Float[3];
|
||||
|
||||
function dot(Vector a, Vector b) =>
|
||||
a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
||||
|
||||
function cross(Vector a, Vector b) => [
|
||||
a[1] * b[2] - a[2] * b[1],
|
||||
a[2] * b[0] - a[0] * b[2],
|
||||
a[0] * b[1] - a[1] * b[0]
|
||||
];
|
||||
|
||||
function scalarTriple(Vector a, Vector b, Vector c) =>
|
||||
dot(a, cross(b, c));
|
||||
|
||||
function vectorTriple(Vector a, Vector b, Vector c) =>
|
||||
cross(a, cross(b, c));
|
||||
|
||||
value a = [ 3.0, 4.0, 5.0 ];
|
||||
value b = [ 4.0, 3.0, 5.0 ];
|
||||
value c = [-5.0, -12.0, -13.0 ];
|
||||
|
||||
print("``a`` . ``b`` = ``dot(a, b)``");
|
||||
print("``a`` X ``b`` = ``cross(a, b)``");
|
||||
print("``a`` . ``b`` X ``c`` = ``scalarTriple(a, b, c)``");
|
||||
print("``a`` X ``b`` X ``c`` = ``vectorTriple(a, b, c)``");
|
||||
}
|
||||
25
Task/Vector-products/Clojure/vector-products.clj
Normal file
25
Task/Vector-products/Clojure/vector-products.clj
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(defrecord Vector [x y z])
|
||||
|
||||
(defn dot
|
||||
[U V]
|
||||
(+ (* (:x U) (:x V))
|
||||
(* (:y U) (:y V))
|
||||
(* (:z U) (:z V))))
|
||||
|
||||
(defn cross
|
||||
[U V]
|
||||
(new Vector
|
||||
(- (* (:y U) (:z V)) (* (:z U) (:y V)))
|
||||
(- (* (:z U) (:x V)) (* (:x U) (:z V)))
|
||||
(- (* (:x U) (:y V)) (* (:y U) (:x V)))))
|
||||
|
||||
(let [a (new Vector 3 4 5)
|
||||
b (new Vector 4 3 5)
|
||||
c (new Vector -5 -12 -13)]
|
||||
(doseq
|
||||
[prod (list
|
||||
(dot a b)
|
||||
(cross a b)
|
||||
(dot a (cross b c))
|
||||
(cross a (cross b c)))]
|
||||
(println prod)))
|
||||
45
Task/Vector-products/Common-Lisp/vector-products-1.lisp
Normal file
45
Task/Vector-products/Common-Lisp/vector-products-1.lisp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
(defclass 3d-vector ()
|
||||
((x :type number :initarg :x)
|
||||
(y :type number :initarg :y)
|
||||
(z :type number :initarg :z)))
|
||||
|
||||
(defmethod print-object ((object 3d-vector) stream)
|
||||
(print-unreadable-object (object stream :type t)
|
||||
(with-slots (x y z) object
|
||||
(format stream "~a ~a ~a" x y z))))
|
||||
|
||||
(defun make-3d-vector (x y z)
|
||||
(make-instance '3d-vector :x x :y y :z z))
|
||||
|
||||
(defmethod dot-product ((a 3d-vector) (b 3d-vector))
|
||||
(with-slots ((a1 x) (a2 y) (a3 z)) a
|
||||
(with-slots ((b1 x) (b2 y) (b3 z)) b
|
||||
(+ (* a1 b1) (* a2 b2) (* a3 b3)))))
|
||||
|
||||
(defmethod cross-product ((a 3d-vector)
|
||||
(b 3d-vector))
|
||||
(with-slots ((a1 x) (a2 y) (a3 z)) a
|
||||
(with-slots ((b1 x) (b2 y) (b3 z)) b
|
||||
(make-instance '3d-vector
|
||||
:x (- (* a2 b3) (* a3 b2))
|
||||
:y (- (* a3 b1) (* a1 b3))
|
||||
:z (- (* a1 b2) (* a2 b1))))))
|
||||
|
||||
(defmethod scalar-triple-product ((a 3d-vector)
|
||||
(b 3d-vector)
|
||||
(c 3d-vector))
|
||||
(dot-product a (cross-product b c)))
|
||||
|
||||
(defmethod vector-triple-product ((a 3d-vector)
|
||||
(b 3d-vector)
|
||||
(c 3d-vector))
|
||||
(cross-product a (cross-product b c)))
|
||||
|
||||
(defun vector-products-example ()
|
||||
(let ((a (make-3d-vector 3 4 5))
|
||||
(b (make-3d-vector 4 3 5))
|
||||
(c (make-3d-vector -5 -12 -13)))
|
||||
(values (dot-product a b)
|
||||
(cross-product a b)
|
||||
(scalar-triple-product a b c)
|
||||
(vector-triple-product a b c))))
|
||||
22
Task/Vector-products/Common-Lisp/vector-products-2.lisp
Normal file
22
Task/Vector-products/Common-Lisp/vector-products-2.lisp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(defun cross (a b)
|
||||
(when (and (equal (length a) 3) (equal (length b) 3))
|
||||
(vector
|
||||
(- (* (elt a 1) (elt b 2)) (* (elt a 2) (elt b 1)))
|
||||
(- (* (elt a 2) (elt b 0)) (* (elt a 0) (elt b 2)))
|
||||
(- (* (elt a 0) (elt b 1)) (* (elt a 1) (elt b 0))))))
|
||||
|
||||
(defun dot (a b)
|
||||
(when (equal (length a) (length b))
|
||||
(loop for ai across a for bi across b sum (* ai bi))))
|
||||
|
||||
(defun scalar-triple (a b c)
|
||||
(dot a (cross b c)))
|
||||
|
||||
(defun vector-triple (a b c)
|
||||
(cross a (cross b c)))
|
||||
|
||||
(defun task (a b c)
|
||||
(values (dot a b)
|
||||
(cross a b)
|
||||
(scalar-triple a b c)
|
||||
(vector-triple a b c)))
|
||||
65
Task/Vector-products/Cowgol/vector-products.cowgol
Normal file
65
Task/Vector-products/Cowgol/vector-products.cowgol
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
record Vector is
|
||||
x: int32; # Cowgol does not have floating point types,
|
||||
y: int32; # but for the examples it does not matter.
|
||||
z: int32;
|
||||
end record;
|
||||
|
||||
sub print_signed(n: int32) is
|
||||
if n < 0 then
|
||||
print_char('-');
|
||||
n := -n;
|
||||
end if;
|
||||
print_i32(n as uint32);
|
||||
end sub;
|
||||
|
||||
sub print_vector(v: [Vector]) is
|
||||
print_char('(');
|
||||
print_signed(v.x);
|
||||
print(", ");
|
||||
print_signed(v.y);
|
||||
print(", ");
|
||||
print_signed(v.z);
|
||||
print_char(')');
|
||||
print_nl();
|
||||
end sub;
|
||||
|
||||
sub dot(a: [Vector], b: [Vector]): (r: int32) is
|
||||
r := a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
end sub;
|
||||
|
||||
# Unfortunately it is impossible to return a complex type
|
||||
# from a function. We have to have the caller pass in a pointer
|
||||
# and have this function set its fields.
|
||||
sub cross(a: [Vector], b: [Vector], r: [Vector]) is
|
||||
r.x := a.y * b.z - a.z * b.y;
|
||||
r.y := a.z * b.x - a.x * b.z;
|
||||
r.z := a.x * b.y - a.y * b.x;
|
||||
end sub;
|
||||
|
||||
sub scalarTriple(a: [Vector], b: [Vector], c: [Vector]): (r: int32) is
|
||||
var v: Vector;
|
||||
cross(b, c, &v);
|
||||
r := dot(a, &v);
|
||||
end sub;
|
||||
|
||||
sub vectorTriple(a: [Vector], b: [Vector], c: [Vector], r: [Vector]) is
|
||||
var v: Vector;
|
||||
cross(b, c, &v);
|
||||
cross(a, &v, r);
|
||||
end sub;
|
||||
|
||||
var a: Vector := {3, 4, 5};
|
||||
var b: Vector := {4, 3, 5};
|
||||
var c: Vector := {-5, -12, -13};
|
||||
var scratch: Vector;
|
||||
|
||||
print(" a = "); print_vector(&a);
|
||||
print(" b = "); print_vector(&b);
|
||||
print(" c = "); print_vector(&c);
|
||||
print(" a . b = "); print_signed(dot(&a, &b)); print_nl();
|
||||
print(" a x b = "); cross(&a, &b, &scratch); print_vector(&scratch);
|
||||
print("a . b x c = "); print_signed(scalarTriple(&a, &b, &c)); print_nl();
|
||||
print("a x b x c = "); vectorTriple(&a, &b, &c, &scratch);
|
||||
print_vector(&scratch);
|
||||
39
Task/Vector-products/Crystal/vector-products.crystal
Normal file
39
Task/Vector-products/Crystal/vector-products.crystal
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
class Vector
|
||||
property x, y, z
|
||||
|
||||
def initialize(@x : Int64, @y : Int64, @z : Int64) end
|
||||
|
||||
def dot_product(other : Vector)
|
||||
(self.x * other.x) + (self.y * other.y) + (self.z * other.z)
|
||||
end
|
||||
|
||||
def cross_product(other : Vector)
|
||||
Vector.new(self.y * other.z - self.z * other.y,
|
||||
self.z * other.x - self.x * other.z,
|
||||
self.x * other.y - self.y * other.x)
|
||||
end
|
||||
|
||||
def scalar_triple_product(b : Vector, c : Vector)
|
||||
self.dot_product(b.cross_product(c))
|
||||
end
|
||||
|
||||
def vector_triple_product(b : Vector, c : Vector)
|
||||
self.cross_product(b.cross_product(c))
|
||||
end
|
||||
|
||||
def to_s
|
||||
"(#{self.x}, #{self.y}, #{self.z})\n"
|
||||
end
|
||||
end
|
||||
|
||||
a = Vector.new(3, 4, 5)
|
||||
b = Vector.new(4, 3, 5)
|
||||
c = Vector.new(-5, -12, -13)
|
||||
|
||||
puts "a = #{a.to_s}"
|
||||
puts "b = #{b.to_s}"
|
||||
puts "c = #{c.to_s}"
|
||||
puts "a dot b = #{a.dot_product b}"
|
||||
puts "a cross b = #{a.cross_product(b).to_s}"
|
||||
puts "a dot (b cross c) = #{a.scalar_triple_product b, c}"
|
||||
puts "a cross (b cross c) = #{a.vector_triple_product(b, c).to_s}"
|
||||
44
Task/Vector-products/D/vector-products.d
Normal file
44
Task/Vector-products/D/vector-products.d
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import std.stdio, std.conv, std.numeric;
|
||||
|
||||
struct V3 {
|
||||
union {
|
||||
immutable struct { double x, y, z; }
|
||||
immutable double[3] v;
|
||||
}
|
||||
|
||||
double dot(in V3 rhs) const pure nothrow /*@safe*/ @nogc {
|
||||
return dotProduct(v, rhs.v);
|
||||
}
|
||||
|
||||
V3 cross(in V3 rhs) const pure nothrow @safe @nogc {
|
||||
return V3(y * rhs.z - z * rhs.y,
|
||||
z * rhs.x - x * rhs.z,
|
||||
x * rhs.y - y * rhs.x);
|
||||
}
|
||||
|
||||
string toString() const { return v.text; }
|
||||
}
|
||||
|
||||
double scalarTriple(in V3 a, in V3 b, in V3 c) /*@safe*/ pure nothrow {
|
||||
return a.dot(b.cross(c));
|
||||
// function vector_products.V3.cross (const(V3) rhs) immutable
|
||||
// is not callable using argument types (const(V3)) const
|
||||
}
|
||||
|
||||
V3 vectorTriple(in V3 a, in V3 b, in V3 c) @safe pure nothrow @nogc {
|
||||
return a.cross(b.cross(c));
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable V3 a = {3, 4, 5},
|
||||
b = {4, 3, 5},
|
||||
c = {-5, -12, -13};
|
||||
|
||||
writeln("a = ", a);
|
||||
writeln("b = ", b);
|
||||
writeln("c = ", c);
|
||||
writeln("a . b = ", a.dot(b));
|
||||
writeln("a x b = ", a.cross(b));
|
||||
writeln("a . (b x c) = ", scalarTriple(a, b, c));
|
||||
writeln("a x (b x c) = ", vectorTriple(a, b, c));
|
||||
}
|
||||
57
Task/Vector-products/ERRE/vector-products.erre
Normal file
57
Task/Vector-products/ERRE/vector-products.erre
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
PROGRAM VECTORPRODUCT
|
||||
|
||||
!$DOUBLE
|
||||
|
||||
TYPE TVECTOR=(X,Y,Z)
|
||||
|
||||
DIM A:TVECTOR,B:TVECTOR,C:TVECTOR
|
||||
|
||||
DIM AA:TVECTOR,BB:TVECTOR,CC:TVECTOR
|
||||
DIM DD:TVECTOR,EE:TVECTOR,FF:TVECTOR
|
||||
|
||||
PROCEDURE DOTPRODUCT(DD.,EE.->DOTP)
|
||||
DOTP=DD.X*EE.X+DD.Y*EE.Y+DD.Z*EE.Z
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE CROSSPRODUCT(DD.,EE.->FF.)
|
||||
FF.X=DD.Y*EE.Z-DD.Z*EE.Y
|
||||
FF.Y=DD.Z*EE.X-DD.X*EE.Z
|
||||
FF.Z=DD.X*EE.Y-DD.Y*EE.X
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE SCALARTRIPLEPRODUCT(AA.,BB.,CC.->SCALARTP)
|
||||
CROSSPRODUCT(BB.,CC.->FF.)
|
||||
DOTPRODUCT(AA.,FF.->SCALARTP)
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE VECTORTRIPLEPRODUCT(AA.,BB.,CC.->FF.)
|
||||
CROSSPRODUCT(BB.,CC.->FF.)
|
||||
CROSSPRODUCT(AA.,FF.->FF.)
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE PRINTVECTOR(AA.)
|
||||
PRINT("(";AA.X;",";AA.Y;",";AA.Z;")")
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
A.X=3 A.Y=4 A.Z=5
|
||||
B.X=4 B.Y=3 B.Z=5
|
||||
C.X=-5 C.Y=-12 C.Z=-13
|
||||
|
||||
PRINT("A: ";) PRINTVECTOR(A.)
|
||||
PRINT("B: ";) PRINTVECTOR(B.)
|
||||
PRINT("C: ";) PRINTVECTOR(C.)
|
||||
|
||||
PRINT
|
||||
DOTPRODUCT(A.,B.->DOTP)
|
||||
PRINT("A.B =";DOTP)
|
||||
|
||||
CROSSPRODUCT(A.,B.->FF.)
|
||||
PRINT("AxB =";) PRINTVECTOR(FF.)
|
||||
|
||||
SCALARTRIPLEPRODUCT(A.,B.,C.->SCALARTP)
|
||||
PRINT("A.(BxC)=";SCALARTP)
|
||||
|
||||
VECTORTRIPLEPRODUCT(A.,B.,C.->FF.)
|
||||
PRINT("Ax(BxC)=";) PRINTVECTOR(FF.)
|
||||
END PROGRAM
|
||||
20
Task/Vector-products/EchoLisp/vector-products.l
Normal file
20
Task/Vector-products/EchoLisp/vector-products.l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(lib 'math)
|
||||
|
||||
(define (scalar-triple-product a b c)
|
||||
(dot-product a (cross-product b c)))
|
||||
|
||||
(define (vector-triple-product a b c)
|
||||
(cross-product a (cross-product b c)))
|
||||
|
||||
(define a #(3 4 5))
|
||||
(define b #(4 3 5))
|
||||
(define c #(-5 -12 -13))
|
||||
|
||||
(cross-product a b)
|
||||
→ #( 5 5 -7)
|
||||
(dot-product a b)
|
||||
→ 49
|
||||
(scalar-triple-product a b c)
|
||||
→ 6
|
||||
(vector-triple-product a b c)
|
||||
→ #( -267 204 -3)
|
||||
21
Task/Vector-products/Elixir/vector-products.elixir
Normal file
21
Task/Vector-products/Elixir/vector-products.elixir
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
defmodule Vector do
|
||||
def dot_product({a1,a2,a3}, {b1,b2,b3}), do: a1*b1 + a2*b2 + a3*b3
|
||||
|
||||
def cross_product({a1,a2,a3}, {b1,b2,b3}), do: {a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1}
|
||||
|
||||
def scalar_triple_product(a, b, c), do: dot_product(a, cross_product(b, c))
|
||||
|
||||
def vector_triple_product(a, b, c), do: cross_product(a, cross_product(b, c))
|
||||
end
|
||||
|
||||
a = {3, 4, 5}
|
||||
b = {4, 3, 5}
|
||||
c = {-5, -12, -13}
|
||||
|
||||
IO.puts "a = #{inspect a}"
|
||||
IO.puts "b = #{inspect b}"
|
||||
IO.puts "c = #{inspect c}"
|
||||
IO.puts "a . b = #{inspect Vector.dot_product(a, b)}"
|
||||
IO.puts "a x b = #{inspect Vector.cross_product(a, b)}"
|
||||
IO.puts "a . (b x c) = #{inspect Vector.scalar_triple_product(a, b, c)}"
|
||||
IO.puts "a x (b x c) = #{inspect Vector.vector_triple_product(a, b, c)}"
|
||||
21
Task/Vector-products/Erlang/vector-products.erl
Normal file
21
Task/Vector-products/Erlang/vector-products.erl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
-module(vector).
|
||||
-export([main/0]).
|
||||
vector_product(X,Y)->
|
||||
[X1,X2,X3]=X,
|
||||
[Y1,Y2,Y3]=Y,
|
||||
Ans=[X2*Y3-X3*Y2,X3*Y1-X1*Y3,X1*Y2-X2*Y1],
|
||||
Ans.
|
||||
dot_product(X,Y)->
|
||||
[X1,X2,X3]=X,
|
||||
[Y1,Y2,Y3]=Y,
|
||||
Ans=X1*Y1+X2*Y2+X3*Y3,
|
||||
io:fwrite("~p~n",[Ans]).
|
||||
main()->
|
||||
{ok, A} = io:fread("Enter vector A : ", "~d ~d ~d"),
|
||||
{ok, B} = io:fread("Enter vector B : ", "~d ~d ~d"),
|
||||
{ok, C} = io:fread("Enter vector C : ", "~d ~d ~d"),
|
||||
dot_product(A,B),
|
||||
Ans=vector_product(A,B),
|
||||
io:fwrite("~p,~p,~p~n",Ans),
|
||||
dot_product(C,vector_product(A,B)),
|
||||
io:fwrite("~p,~p,~p~n",vector_product(C,vector_product(A,B))).
|
||||
36
Task/Vector-products/Euphoria/vector-products.euphoria
Normal file
36
Task/Vector-products/Euphoria/vector-products.euphoria
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
constant X = 1, Y = 2, Z = 3
|
||||
|
||||
function dot_product(sequence a, sequence b)
|
||||
return a[X]*b[X] + a[Y]*b[Y] + a[Z]*b[Z]
|
||||
end function
|
||||
|
||||
function cross_product(sequence a, sequence b)
|
||||
return { a[Y]*b[Z] - a[Z]*b[Y],
|
||||
a[Z]*b[X] - a[X]*b[Z],
|
||||
a[X]*b[Y] - a[Y]*b[X] }
|
||||
end function
|
||||
|
||||
function scalar_triple(sequence a, sequence b, sequence c)
|
||||
return dot_product( a, cross_product( b, c ) )
|
||||
end function
|
||||
|
||||
function vector_triple( sequence a, sequence b, sequence c)
|
||||
return cross_product( a, cross_product( b, c ) )
|
||||
end function
|
||||
|
||||
constant a = { 3, 4, 5 }, b = { 4, 3, 5 }, c = { -5, -12, -13 }
|
||||
|
||||
puts(1,"a = ")
|
||||
? a
|
||||
puts(1,"b = ")
|
||||
? b
|
||||
puts(1,"c = ")
|
||||
? c
|
||||
puts(1,"a dot b = ")
|
||||
? dot_product( a, b )
|
||||
puts(1,"a x b = ")
|
||||
? cross_product( a, b )
|
||||
puts(1,"a dot (b x c) = ")
|
||||
? scalar_triple( a, b, c )
|
||||
puts(1,"a x (b x c) = ")
|
||||
? vector_triple( a, b, c )
|
||||
22
Task/Vector-products/F-Sharp/vector-products.fs
Normal file
22
Task/Vector-products/F-Sharp/vector-products.fs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
let dot (ax, ay, az) (bx, by, bz) =
|
||||
ax * bx + ay * by + az * bz
|
||||
|
||||
let cross (ax, ay, az) (bx, by, bz) =
|
||||
(ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx)
|
||||
|
||||
let scalTrip a b c =
|
||||
dot a (cross b c)
|
||||
|
||||
let vecTrip a b c =
|
||||
cross a (cross b c)
|
||||
|
||||
[<EntryPoint>]
|
||||
let main _ =
|
||||
let a = (3.0, 4.0, 5.0)
|
||||
let b = (4.0, 3.0, 5.0)
|
||||
let c = (-5.0, -12.0, -13.0)
|
||||
printfn "%A" (dot a b)
|
||||
printfn "%A" (cross a b)
|
||||
printfn "%A" (scalTrip a b c)
|
||||
printfn "%A" (vecTrip a b c)
|
||||
0 // return an integer exit code
|
||||
30
Task/Vector-products/Factor/vector-products.factor
Normal file
30
Task/Vector-products/Factor/vector-products.factor
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
USING: arrays io locals math prettyprint sequences ;
|
||||
|
||||
: dot-product ( a b -- dp ) [ * ] 2map sum ;
|
||||
|
||||
:: cross-product ( a b -- cp )
|
||||
a first :> a1 a second :> a2 a third :> a3
|
||||
b first :> b1 b second :> b2 b third :> b3
|
||||
a2 b3 * a3 b2 * - ! X
|
||||
a3 b1 * a1 b3 * - ! Y
|
||||
a1 b2 * a2 b1 * - ! Z
|
||||
3array ;
|
||||
|
||||
: scalar-triple-product ( a b c -- stp )
|
||||
cross-product dot-product ;
|
||||
|
||||
: vector-triple-product ( a b c -- vtp )
|
||||
cross-product cross-product ;
|
||||
|
||||
[let
|
||||
{ 3 4 5 } :> a
|
||||
{ 4 3 5 } :> b
|
||||
{ -5 -12 -13 } :> c
|
||||
"a: " write a .
|
||||
"b: " write b .
|
||||
"c: " write c . nl
|
||||
"a . b: " write a b dot-product .
|
||||
"a x b: " write a b cross-product .
|
||||
"a . (b x c): " write a b c scalar-triple-product .
|
||||
"a x (b x c): " write a b c vector-triple-product .
|
||||
]
|
||||
34
Task/Vector-products/Fantom/vector-products.fantom
Normal file
34
Task/Vector-products/Fantom/vector-products.fantom
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
class Main
|
||||
{
|
||||
Int dot_product (Int[] a, Int[] b)
|
||||
{
|
||||
a[0]*b[0] + a[1]*b[1] + a[2]*b[2]
|
||||
}
|
||||
|
||||
Int[] cross_product (Int[] a, Int[] b)
|
||||
{
|
||||
[a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1]-a[1]*b[0]]
|
||||
}
|
||||
|
||||
Int scalar_triple_product (Int[] a, Int[] b, Int[] c)
|
||||
{
|
||||
dot_product (a, cross_product (b, c))
|
||||
}
|
||||
|
||||
Int[] vector_triple_product (Int[] a, Int[] b, Int[] c)
|
||||
{
|
||||
cross_product (a, cross_product (b, c))
|
||||
}
|
||||
|
||||
Void main ()
|
||||
{
|
||||
a := [3, 4, 5]
|
||||
b := [4, 3, 5]
|
||||
c := [-5, -12, -13]
|
||||
|
||||
echo ("a . b = " + dot_product (a, b))
|
||||
echo ("a x b = [" + cross_product(a, b).join (", ") + "]")
|
||||
echo ("a . (b x c) = " + scalar_triple_product (a, b, c))
|
||||
echo ("a x (b x c) = [" + vector_triple_product(a, b, c).join (", ") + "]")
|
||||
}
|
||||
}
|
||||
39
Task/Vector-products/Forth/vector-products-1.fth
Normal file
39
Task/Vector-products/Forth/vector-products-1.fth
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
: 3f! ( &v - ) ( f: x y z - ) dup float+ dup float+ f! f! f! ;
|
||||
|
||||
: Vector \ Compiletime: ( f: x y z - ) ( <name> - )
|
||||
create here [ 3 floats ] literal allot 3f! ; \ Runtime: ( - &v )
|
||||
|
||||
: >fx@ ( &v - ) ( f: - n ) postpone f@ ; immediate
|
||||
: >fy@ ( &v - ) ( f: - n ) float+ f@ ;
|
||||
: >fz@ ( &v - ) ( f: - n ) float+ float+ f@ ;
|
||||
: .Vector ( &v - ) dup >fz@ dup >fy@ >fx@ f. f. f. ;
|
||||
|
||||
: Dot* ( &v1 &v2 - ) ( f - DotPrd )
|
||||
2dup >fx@ >fx@ f*
|
||||
2dup >fy@ >fy@ f* f+
|
||||
>fz@ >fz@ f* f+ ;
|
||||
|
||||
: Cross* ( &v1 &v2 &vResult - )
|
||||
>r 2dup >fz@ >fy@ f*
|
||||
2dup >fy@ >fz@ f* f-
|
||||
2dup >fx@ >fz@ f*
|
||||
2dup >fz@ >fx@ f* f-
|
||||
2dup >fy@ >fx@ f*
|
||||
>fx@ >fy@ f* f-
|
||||
r> 3f! ;
|
||||
|
||||
: ScalarTriple* ( &v1 &v2 &v3 - ) ( f: - ScalarTriple* )
|
||||
>r pad Cross* pad r> Dot* ;
|
||||
|
||||
: VectorTriple* ( &v1 &v2 &v3 &vDest - )
|
||||
>r swap r@ Cross* r> tuck Cross* ;
|
||||
|
||||
3e 4e 5e Vector A
|
||||
4e 3e 5e Vector B
|
||||
-5e -12e -13e Vector C
|
||||
|
||||
cr
|
||||
cr .( a . b = ) A B Dot* f.
|
||||
cr .( a x b = ) A B pad Cross* pad .Vector
|
||||
cr .( a . [b x c] = ) A B C ScalarTriple* f.
|
||||
cr .( a x [b x c] = ) A B C pad VectorTriple* pad .Vector
|
||||
13
Task/Vector-products/Forth/vector-products-2.fth
Normal file
13
Task/Vector-products/Forth/vector-products-2.fth
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
S" fsl-util.fs" REQUIRED
|
||||
: 3f! 3 SWAP }fput ;
|
||||
: vector
|
||||
CREATE
|
||||
HERE 3 DUP FLOAT DUP , * ALLOT SWAP CELL+ }fput
|
||||
DOES>
|
||||
CELL+ ;
|
||||
: >fx@ 0 } F@ ;
|
||||
: >fy@ 1 } F@ ;
|
||||
: >fz@ 2 } F@ ;
|
||||
: .Vector 3 SWAP }fprint ;
|
||||
0e 0e 0e vector pad \ NB: your system will be non-standard after this line
|
||||
\ From here on is identical to the above example
|
||||
39
Task/Vector-products/Fortran/vector-products.f
Normal file
39
Task/Vector-products/Fortran/vector-products.f
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
program VectorProducts
|
||||
|
||||
real, dimension(3) :: a, b, c
|
||||
|
||||
a = (/ 3, 4, 5 /)
|
||||
b = (/ 4, 3, 5 /)
|
||||
c = (/ -5, -12, -13 /)
|
||||
|
||||
print *, dot_product(a, b)
|
||||
print *, cross_product(a, b)
|
||||
print *, s3_product(a, b, c)
|
||||
print *, v3_product(a, b, c)
|
||||
|
||||
contains
|
||||
|
||||
function cross_product(a, b)
|
||||
real, dimension(3) :: cross_product
|
||||
real, dimension(3), intent(in) :: a, b
|
||||
|
||||
cross_product(1) = a(2)*b(3) - a(3)*b(2)
|
||||
cross_product(2) = a(3)*b(1) - a(1)*b(3)
|
||||
cross_product(3) = a(1)*b(2) - b(1)*a(2)
|
||||
end function cross_product
|
||||
|
||||
function s3_product(a, b, c)
|
||||
real :: s3_product
|
||||
real, dimension(3), intent(in) :: a, b, c
|
||||
|
||||
s3_product = dot_product(a, cross_product(b, c))
|
||||
end function s3_product
|
||||
|
||||
function v3_product(a, b, c)
|
||||
real, dimension(3) :: v3_product
|
||||
real, dimension(3), intent(in) :: a, b, c
|
||||
|
||||
v3_product = cross_product(a, cross_product(b, c))
|
||||
end function v3_product
|
||||
|
||||
end program VectorProducts
|
||||
32
Task/Vector-products/FreeBASIC/vector-products.basic
Normal file
32
Task/Vector-products/FreeBASIC/vector-products.basic
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
'Construct only required operators for this.
|
||||
Type V3
|
||||
As double x,y,z
|
||||
declare operator cast() as string
|
||||
End Type
|
||||
#define dot *
|
||||
#define cross ^
|
||||
#define Show(t1,t) ? #t1;tab(22);t
|
||||
|
||||
operator V3.cast() as string
|
||||
return "("+str(x)+","+str(y)+","+str(z)+")"
|
||||
end operator
|
||||
|
||||
Operator dot(v1 As v3,v2 As v3) As double
|
||||
Return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z
|
||||
End Operator
|
||||
|
||||
Operator cross(v1 As v3,v2 As v3) As v3
|
||||
Return type<v3>(v1.y*v2.z-v2.y*v1.z,-(v1.x*v2.z-v2.x*v1.z),v1.x*v2.y-v2.x*v1.y)
|
||||
End Operator
|
||||
|
||||
dim as V3 a = (3, 4, 5), b = (4, 3, 5), c = (-5, -12, -13)
|
||||
|
||||
Show(a,a)
|
||||
Show(b,b)
|
||||
Show(c,c)
|
||||
?
|
||||
Show(a . b,a dot b)
|
||||
Show(a X b,a cross b)
|
||||
Show(a . b X c,a dot b cross c)
|
||||
Show(a X (b X c),a cross (b cross c))
|
||||
sleep
|
||||
13
Task/Vector-products/FunL/vector-products.funl
Normal file
13
Task/Vector-products/FunL/vector-products.funl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
A = (3, 4, 5)
|
||||
B = (4, 3, 5)
|
||||
C = (-5, -12, -13)
|
||||
|
||||
def dot( u, v ) = sum( u(i)v(i) | i <- 0:u.>length() )
|
||||
def cross( u, v ) = (u(1)v(2) - u(2)v(1), u(2)v(0) - u(0)v(2), u(0)v(1) - u(1)v(0) )
|
||||
def scalarTriple( u, v, w ) = dot( u, cross(v, w) )
|
||||
def vectorTriple( u, v, w ) = cross( u, cross(v, w) )
|
||||
|
||||
println( "A\u00b7B = ${dot(A, B)}" )
|
||||
println( "A\u00d7B = ${cross(A, B)}" )
|
||||
println( "A\u00b7(B\u00d7C) = ${scalarTriple(A, B, C)}" )
|
||||
println( "A\u00d7(B\u00d7C) = ${vectorTriple(A, B, C)}" )
|
||||
38
Task/Vector-products/GAP/vector-products.gap
Normal file
38
Task/Vector-products/GAP/vector-products.gap
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
DotProduct := function(u, v)
|
||||
return u*v;
|
||||
end;
|
||||
|
||||
CrossProduct := function(u, v)
|
||||
return [
|
||||
u[2]*v[3] - u[3]*v[2],
|
||||
u[3]*v[1] - u[1]*v[3],
|
||||
u[1]*v[2] - u[2]*v[1] ];
|
||||
end;
|
||||
|
||||
ScalarTripleProduct := function(u, v, w)
|
||||
return DotProduct(u, CrossProduct(v, w));
|
||||
end;
|
||||
|
||||
VectorTripleProduct := function(u, v, w)
|
||||
return CrossProduct(u, CrossProduct(v, w));
|
||||
end;
|
||||
|
||||
a := [3, 4, 5];
|
||||
b := [4, 3, 5];
|
||||
c := [-5, -12, -13];
|
||||
|
||||
DotProduct(a, b);
|
||||
# 49
|
||||
|
||||
CrossProduct(a, b);
|
||||
# [ 5, 5, -7 ]
|
||||
|
||||
ScalarTripleProduct(a, b, c);
|
||||
# 6
|
||||
|
||||
# Another way to get it
|
||||
Determinant([a, b, c]);
|
||||
# 6
|
||||
|
||||
VectorTripleProduct(a, b, c);
|
||||
# [ -267, 204, -3 ]
|
||||
23
Task/Vector-products/GLSL/vector-products.glsl
Normal file
23
Task/Vector-products/GLSL/vector-products.glsl
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
vec3 a = vec3(3, 4, 5),b = vec3(4, 3, 5),c = vec3(-5, -12, -13);
|
||||
|
||||
float dotProduct(vec3 a, vec3 b)
|
||||
{
|
||||
return a.x*b.x+a.y*b.y+a.z*b.z;
|
||||
}
|
||||
|
||||
vec3 crossProduct(vec3 a,vec3 b)
|
||||
{
|
||||
vec3 c = vec3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y- a.y*b.x);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
float scalarTripleProduct(vec3 a,vec3 b,vec3 c)
|
||||
{
|
||||
return dotProduct(a,crossProduct(b,c));
|
||||
}
|
||||
|
||||
vec3 vectorTripleProduct(vec3 a,vec3 b,vec3 c)
|
||||
{
|
||||
return crossProduct(a,crossProduct(b,c));
|
||||
}
|
||||
36
Task/Vector-products/Go/vector-products.go
Normal file
36
Task/Vector-products/Go/vector-products.go
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type vector struct {
|
||||
x, y, z float64
|
||||
}
|
||||
|
||||
var (
|
||||
a = vector{3, 4, 5}
|
||||
b = vector{4, 3, 5}
|
||||
c = vector{-5, -12, -13}
|
||||
)
|
||||
|
||||
func dot(a, b vector) float64 {
|
||||
return a.x*b.x + a.y*b.y + a.z*b.z
|
||||
}
|
||||
|
||||
func cross(a, b vector) vector {
|
||||
return vector{a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x}
|
||||
}
|
||||
|
||||
func s3(a, b, c vector) float64 {
|
||||
return dot(a, cross(b, c))
|
||||
}
|
||||
|
||||
func v3(a, b, c vector) vector {
|
||||
return cross(a, cross(b, c))
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(dot(a, b))
|
||||
fmt.Println(cross(a, b))
|
||||
fmt.Println(s3(a, b, c))
|
||||
fmt.Println(v3(a, b, c))
|
||||
}
|
||||
11
Task/Vector-products/Groovy/vector-products-1.groovy
Normal file
11
Task/Vector-products/Groovy/vector-products-1.groovy
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def pairwiseOperation = { x, y, Closure binaryOp ->
|
||||
assert x && y && x.size() == y.size()
|
||||
[x, y].transpose().collect(binaryOp)
|
||||
}
|
||||
|
||||
def pwMult = pairwiseOperation.rcurry { it[0] * it[1] }
|
||||
|
||||
def dotProduct = { x, y ->
|
||||
assert x && y && x.size() == y.size()
|
||||
pwMult(x, y).sum()
|
||||
}
|
||||
4
Task/Vector-products/Groovy/vector-products-2.groovy
Normal file
4
Task/Vector-products/Groovy/vector-products-2.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def crossProductS = { x, y ->
|
||||
assert x && y && x.size() == 3 && y.size() == 3
|
||||
[x[1]*y[2] - x[2]*y[1], x[2]*y[0] - x[0]*y[2] , x[0]*y[1] - x[1]*y[0]]
|
||||
}
|
||||
16
Task/Vector-products/Groovy/vector-products-3.groovy
Normal file
16
Task/Vector-products/Groovy/vector-products-3.groovy
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def rotR = {
|
||||
assert it && it.size() > 2
|
||||
[it[-1]] + it[0..-2]
|
||||
}
|
||||
|
||||
def rotL = {
|
||||
assert it && it.size() > 2
|
||||
it[1..-1] + [it[0]]
|
||||
}
|
||||
|
||||
def pwSubtr = pairwiseOperation.rcurry { it[0] - it[1] }
|
||||
|
||||
def crossProductV = { x, y ->
|
||||
assert x && y && x.size() == 3 && y.size() == 3
|
||||
pwSubtr(pwMult(rotL(x), rotR(y)), pwMult(rotL(y), rotR(x)))
|
||||
}
|
||||
23
Task/Vector-products/Groovy/vector-products-4.groovy
Normal file
23
Task/Vector-products/Groovy/vector-products-4.groovy
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
def test = { crossProduct ->
|
||||
|
||||
def scalarTripleProduct = { x, y, z ->
|
||||
dotProduct(x, crossProduct(y, z))
|
||||
}
|
||||
|
||||
def vectorTripleProduct = { x, y, z ->
|
||||
crossProduct(x, crossProduct(y, z))
|
||||
}
|
||||
|
||||
def a = [3, 4, 5]
|
||||
def b = [4, 3, 5]
|
||||
def c = [-5, -12, -13]
|
||||
|
||||
println(" a . b = " + dotProduct(a,b))
|
||||
println(" a x b = " + crossProduct(a,b))
|
||||
println("a . (b x c) = " + scalarTripleProduct(a,b,c))
|
||||
println("a x (b x c) = " + vectorTripleProduct(a,b,c))
|
||||
println()
|
||||
}
|
||||
|
||||
test(crossProductS)
|
||||
test(crossProductV)
|
||||
53
Task/Vector-products/Haskell/vector-products-1.hs
Normal file
53
Task/Vector-products/Haskell/vector-products-1.hs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import Data.Monoid ((<>))
|
||||
|
||||
type Vector a = [a]
|
||||
|
||||
type Scalar a = a
|
||||
|
||||
a, b, c, d :: Vector Int
|
||||
a = [3, 4, 5]
|
||||
|
||||
b = [4, 3, 5]
|
||||
|
||||
c = [-5, -12, -13]
|
||||
|
||||
d = [3, 4, 5, 6]
|
||||
|
||||
dot
|
||||
:: (Num t)
|
||||
=> Vector t -> Vector t -> Scalar t
|
||||
dot u v
|
||||
| length u == length v = sum $ zipWith (*) u v
|
||||
| otherwise = error "Dotted Vectors must be of equal dimension."
|
||||
|
||||
cross
|
||||
:: (Num t)
|
||||
=> Vector t -> Vector t -> Vector t
|
||||
cross u v
|
||||
| length u == 3 && length v == 3 =
|
||||
[ u !! 1 * v !! 2 - u !! 2 * v !! 1
|
||||
, u !! 2 * head v - head u * v !! 2
|
||||
, head u * v !! 1 - u !! 1 * head v
|
||||
]
|
||||
| otherwise = error "Crossed Vectors must both be three dimensional."
|
||||
|
||||
scalarTriple
|
||||
:: (Num t)
|
||||
=> Vector t -> Vector t -> Vector t -> Scalar t
|
||||
scalarTriple q r s = dot q $ cross r s
|
||||
|
||||
vectorTriple
|
||||
:: (Num t)
|
||||
=> Vector t -> Vector t -> Vector t -> Vector t
|
||||
vectorTriple q r s = cross q $ cross r s
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
putStrLn
|
||||
[ "a . b = " <> show (dot a b)
|
||||
, "a x b = " <> show (cross a b)
|
||||
, "a . b x c = " <> show (scalarTriple a b c)
|
||||
, "a x b x c = " <> show (vectorTriple a b c)
|
||||
, "a . d = " <> show (dot a d)
|
||||
]
|
||||
56
Task/Vector-products/Haskell/vector-products-2.hs
Normal file
56
Task/Vector-products/Haskell/vector-products-2.hs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
dotProduct
|
||||
:: Num a
|
||||
=> [a] -> [a] -> Either String a
|
||||
dotProduct xs ys
|
||||
| length xs /= length ys =
|
||||
Left "Dot product not defined - vectors differ in dimension."
|
||||
| otherwise = Right (sum $ zipWith (*) xs ys)
|
||||
|
||||
crossProduct
|
||||
:: Num a
|
||||
=> [a] -> [a] -> Either String [a]
|
||||
crossProduct xs ys
|
||||
| 3 /= length xs || 3 /= length ys =
|
||||
Left "crossProduct is defined only for 3d vectors."
|
||||
| otherwise = Right [x2 * y3 - x3 * y2, x3 * y1 - x1 * y3, x1 * y2 - x2 * y1]
|
||||
where
|
||||
[x1, x2, x3] = xs
|
||||
[y1, y2, y3] = ys
|
||||
|
||||
scalarTriple
|
||||
:: Num a
|
||||
=> [a] -> [a] -> [a] -> Either String a
|
||||
scalarTriple q r s = crossProduct r s >>= dotProduct q
|
||||
|
||||
vectorTriple
|
||||
:: Num a
|
||||
=> [a] -> [a] -> [a] -> Either String [a]
|
||||
vectorTriple q r s = crossProduct r s >>= crossProduct q
|
||||
|
||||
-- TEST ---------------------------------------------------
|
||||
a = [3, 4, 5]
|
||||
|
||||
b = [4, 3, 5]
|
||||
|
||||
c = [-5, -12, -13]
|
||||
|
||||
d = [3, 4, 5, 6]
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_ putStrLn $
|
||||
zipWith
|
||||
(++)
|
||||
["a . b", "a x b", "a . b x c", "a x b x c", "a . d", "a . (b x d)"]
|
||||
[ sh $ dotProduct a b
|
||||
, sh $ crossProduct a b
|
||||
, sh $ scalarTriple a b c
|
||||
, sh $ vectorTriple a b c
|
||||
, sh $ dotProduct a d
|
||||
, sh $ scalarTriple a b d
|
||||
]
|
||||
|
||||
sh
|
||||
:: Show a
|
||||
=> Either String a -> String
|
||||
sh = either (" => " ++) ((" = " ++) . show)
|
||||
42
Task/Vector-products/Icon/vector-products.icon
Normal file
42
Task/Vector-products/Icon/vector-products.icon
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# record type to store a 3D vector
|
||||
record Vector3D(x, y, z)
|
||||
|
||||
# procedure to display vector as a string
|
||||
procedure toString (vector)
|
||||
return "(" || vector.x || ", " || vector.y || ", " || vector.z || ")"
|
||||
end
|
||||
|
||||
procedure dotProduct (a, b)
|
||||
return a.x * b.x + a.y * b.y + a.z * b.z
|
||||
end
|
||||
|
||||
procedure crossProduct (a, b)
|
||||
x := a.y * b.z - a.z * b.y
|
||||
y := a.z * b.x - a.x * b.z
|
||||
z := a.x * b.y - a.y * b.x
|
||||
return Vector3D(x, y, z)
|
||||
end
|
||||
|
||||
procedure scalarTriple (a, b, c)
|
||||
return dotProduct (a, crossProduct (b, c))
|
||||
end
|
||||
|
||||
procedure vectorTriple (a, b, c)
|
||||
return crossProduct (a, crossProduct (b, c))
|
||||
end
|
||||
|
||||
# main procedure, to run given test
|
||||
procedure main ()
|
||||
a := Vector3D(3, 4, 5)
|
||||
b := Vector3D(4, 3, 5)
|
||||
c := Vector3D(-5, -12, -13)
|
||||
|
||||
writes ("A.B : " || toString(a) || "." || toString(b) || " = ")
|
||||
write (dotProduct (a, b))
|
||||
writes ("AxB : " || toString(a) || "x" || toString(b) || " = ")
|
||||
write (toString(crossProduct (a, b)))
|
||||
writes ("A.(BxC) : " || toString(a) || ".(" || toString(b) || "x" || toString(c) || ") = ")
|
||||
write (scalarTriple (a, b, c))
|
||||
writes ("Ax(BxC) : " || toString(a) || "x(" || toString(b) || "x" || toString(c) || ") = ")
|
||||
write (toString(vectorTriple (a, b, c)))
|
||||
end
|
||||
1
Task/Vector-products/J/vector-products-1.j
Normal file
1
Task/Vector-products/J/vector-products-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
cross=: (1&|.@[ * 2&|.@]) - 2&|.@[ * 1&|.@]
|
||||
1
Task/Vector-products/J/vector-products-2.j
Normal file
1
Task/Vector-products/J/vector-products-2.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
cross=: {{ ((1|.x)*2|.y) - (2|.x)*1|.y }}
|
||||
3
Task/Vector-products/J/vector-products-3.j
Normal file
3
Task/Vector-products/J/vector-products-3.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
CT=: C.!.2 @ (#:i.) @ $~
|
||||
ip=: +/ .* NB. inner product
|
||||
cross=: ] ip CT@#@[ ip [
|
||||
1
Task/Vector-products/J/vector-products-4.j
Normal file
1
Task/Vector-products/J/vector-products-4.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
cross=: [: > [: -&.>/ .(*&.>) (<"1=i.3) , ,:&:(<"0)
|
||||
1
Task/Vector-products/J/vector-products-5.j
Normal file
1
Task/Vector-products/J/vector-products-5.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
cross=: {{ >-L:0/ .(*L:0) (<"1=i.3), x,:&:(<"0) y}}
|
||||
12
Task/Vector-products/J/vector-products-6.j
Normal file
12
Task/Vector-products/J/vector-products-6.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
a=: 3 4 5
|
||||
b=: 4 3 5
|
||||
c=: -5 12 13
|
||||
|
||||
A=: 0 {:: ] NB. contents of the first box on the right
|
||||
B=: 1 {:: ] NB. contents of the second box on the right
|
||||
C=: 2 {:: ] NB. contents of the third box on the right
|
||||
|
||||
dotP=: A ip B
|
||||
crossP=: A cross B
|
||||
scTriP=: A ip B cross C
|
||||
veTriP=: A cross B cross C
|
||||
8
Task/Vector-products/J/vector-products-7.j
Normal file
8
Task/Vector-products/J/vector-products-7.j
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
dotP a;b
|
||||
49
|
||||
crossP a;b
|
||||
5 5 _7
|
||||
scTriP a;b;c
|
||||
6
|
||||
veTriP a;b;c
|
||||
_267 204 _3
|
||||
48
Task/Vector-products/Java/vector-products-1.java
Normal file
48
Task/Vector-products/Java/vector-products-1.java
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
public class VectorProds{
|
||||
public static class Vector3D<T extends Number>{
|
||||
private T a, b, c;
|
||||
|
||||
public Vector3D(T a, T b, T c){
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
public double dot(Vector3D<?> vec){
|
||||
return (a.doubleValue() * vec.a.doubleValue() +
|
||||
b.doubleValue() * vec.b.doubleValue() +
|
||||
c.doubleValue() * vec.c.doubleValue());
|
||||
}
|
||||
|
||||
public Vector3D<Double> cross(Vector3D<?> vec){
|
||||
Double newA = b.doubleValue()*vec.c.doubleValue() - c.doubleValue()*vec.b.doubleValue();
|
||||
Double newB = c.doubleValue()*vec.a.doubleValue() - a.doubleValue()*vec.c.doubleValue();
|
||||
Double newC = a.doubleValue()*vec.b.doubleValue() - b.doubleValue()*vec.a.doubleValue();
|
||||
return new Vector3D<Double>(newA, newB, newC);
|
||||
}
|
||||
|
||||
public double scalTrip(Vector3D<?> vecB, Vector3D<?> vecC){
|
||||
return this.dot(vecB.cross(vecC));
|
||||
}
|
||||
|
||||
public Vector3D<Double> vecTrip(Vector3D<?> vecB, Vector3D<?> vecC){
|
||||
return this.cross(vecB.cross(vecC));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "<" + a.toString() + ", " + b.toString() + ", " + c.toString() + ">";
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Vector3D<Integer> a = new Vector3D<Integer>(3, 4, 5);
|
||||
Vector3D<Integer> b = new Vector3D<Integer>(4, 3, 5);
|
||||
Vector3D<Integer> c = new Vector3D<Integer>(-5, -12, -13);
|
||||
|
||||
System.out.println(a.dot(b));
|
||||
System.out.println(a.cross(b));
|
||||
System.out.println(a.scalTrip(b, c));
|
||||
System.out.println(a.vecTrip(b, c));
|
||||
}
|
||||
}
|
||||
62
Task/Vector-products/Java/vector-products-2.java
Normal file
62
Task/Vector-products/Java/vector-products-2.java
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class VectorsOp {
|
||||
// Vector dot product using Java SE 8 stream abilities
|
||||
// the method first create an array of size values,
|
||||
// and map the product of each vectors components in a new array (method map())
|
||||
// and transform the array to a scalr by summing all elements (method reduce)
|
||||
// the method parallel is there for optimization
|
||||
private static int dotProduct(int[] v1, int[] v2,int length) {
|
||||
|
||||
int result = IntStream.range(0, length)
|
||||
.parallel()
|
||||
.map( id -> v1[id] * v2[id])
|
||||
.reduce(0, Integer::sum);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Vector Cross product using Java SE 8 stream abilities
|
||||
// here we map in a new array where each element is equal to the cross product
|
||||
// With Stream is is easier to handle N dimensions vectors
|
||||
private static int[] crossProduct(int[] v1, int[] v2,int length) {
|
||||
|
||||
int result[] = new int[length] ;
|
||||
//result[0] = v1[1] * v2[2] - v1[2]*v2[1] ;
|
||||
//result[1] = v1[2] * v2[0] - v1[0]*v2[2] ;
|
||||
// result[2] = v1[0] * v2[1] - v1[1]*v2[0] ;
|
||||
|
||||
result = IntStream.range(0, length)
|
||||
.parallel()
|
||||
.map( i -> v1[(i+1)%length] * v2[(i+2)%length] - v1[(i+2)%length]*v2[(i+1)%length])
|
||||
.toArray();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
int[] vect1 = {3, 4, 5};
|
||||
int[] vect2 = {4, 3, 5};
|
||||
int[] vect3 = {-5, -12, -13};
|
||||
|
||||
System.out.println("dot product =:" + dotProduct(vect1,vect2,3));
|
||||
|
||||
int[] prodvect = new int[3];
|
||||
prodvect = crossProduct(vect1,vect2,3);
|
||||
System.out.println("cross product =:[" + prodvect[0] + ","
|
||||
+ prodvect[1] + ","
|
||||
+ prodvect[2] + "]");
|
||||
|
||||
prodvect = crossProduct(vect2,vect3,3);
|
||||
System.out.println("scalar product =:" + dotProduct(vect1,prodvect,3));
|
||||
|
||||
prodvect = crossProduct(vect1,prodvect,3);
|
||||
|
||||
System.out.println("triple product =:[" + prodvect[0] + ","
|
||||
+ prodvect[1] + ","
|
||||
+ prodvect[2] + "]");
|
||||
|
||||
}
|
||||
}
|
||||
70
Task/Vector-products/JavaScript/vector-products-1.js
Normal file
70
Task/Vector-products/JavaScript/vector-products-1.js
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
function dotProduct() {
|
||||
var len = arguments[0] && arguments[0].length;
|
||||
var argsLen = arguments.length;
|
||||
var i, j = len;
|
||||
var prod, sum = 0;
|
||||
|
||||
// If no arguments supplied, return undefined
|
||||
if (!len) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If all vectors not same length, return undefined
|
||||
i = argsLen;
|
||||
while (i--) {
|
||||
|
||||
if (arguments[i].length != len) {
|
||||
return; // return undefined
|
||||
}
|
||||
}
|
||||
|
||||
// Sum terms
|
||||
while (j--) {
|
||||
i = argsLen;
|
||||
prod = 1;
|
||||
|
||||
while (i--) {
|
||||
prod *= arguments[i][j];
|
||||
}
|
||||
sum += prod;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function crossProduct(a, b) {
|
||||
|
||||
// Check lengths
|
||||
if (a.length != 3 || b.length != 3) {
|
||||
return;
|
||||
}
|
||||
|
||||
return [a[1]*b[2] - a[2]*b[1],
|
||||
a[2]*b[0] - a[0]*b[2],
|
||||
a[0]*b[1] - a[1]*b[0]];
|
||||
|
||||
}
|
||||
|
||||
function scalarTripleProduct(a, b, c) {
|
||||
return dotProduct(a, crossProduct(b, c));
|
||||
}
|
||||
|
||||
function vectorTripleProduct(a, b, c) {
|
||||
return crossProduct(a, crossProduct(b, c));
|
||||
}
|
||||
|
||||
// Run tests
|
||||
(function () {
|
||||
var a = [3, 4, 5];
|
||||
var b = [4, 3, 5];
|
||||
var c = [-5, -12, -13];
|
||||
|
||||
alert(
|
||||
'A . B: ' + dotProduct(a, b) +
|
||||
'\n' +
|
||||
'A x B: ' + crossProduct(a, b) +
|
||||
'\n' +
|
||||
'A . (B x C): ' + scalarTripleProduct(a, b, c) +
|
||||
'\n' +
|
||||
'A x (B x C): ' + vectorTripleProduct(a, b, c)
|
||||
);
|
||||
}());
|
||||
140
Task/Vector-products/JavaScript/vector-products-2.js
Normal file
140
Task/Vector-products/JavaScript/vector-products-2.js
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// dotProduct :: [a] -> [a] -> Either String a
|
||||
const dotProduct = xs =>
|
||||
// Dot product of two vectors of equal dimension.
|
||||
ys => xs.length !== ys.length ? (
|
||||
Left('Dot product not defined - vectors differ in dimension.')
|
||||
) : Right(sum(
|
||||
zipWith(mul)(Array.from(xs))(Array.from(ys))
|
||||
));
|
||||
|
||||
// crossProduct :: Num a => (a, a, a) -> (a, a, a)
|
||||
// Either String -> (a, a, a)
|
||||
const crossProduct = xs =>
|
||||
// Cross product of two 3D vectors.
|
||||
ys => 3 !== xs.length || 3 !== ys.length ? (
|
||||
Left('crossProduct is defined only for 3d vectors.')
|
||||
) : Right((() => {
|
||||
const [x1, x2, x3] = Array.from(xs);
|
||||
const [y1, y2, y3] = Array.from(ys);
|
||||
return [
|
||||
x2 * y3 - x3 * y2,
|
||||
x3 * y1 - x1 * y3,
|
||||
x1 * y2 - x2 * y1
|
||||
];
|
||||
})());
|
||||
|
||||
// scalarTriple :: Num a => (a, a, a) -> (a, a, a) -> (a, a a) ->
|
||||
// Either String -> a
|
||||
const scalarTriple = q =>
|
||||
// The scalar triple product.
|
||||
r => s => bindLR(crossProduct(r)(s))(
|
||||
dotProduct(q)
|
||||
);
|
||||
|
||||
// vectorTriple :: Num a => (a, a, a) -> (a, a, a) -> (a, a a) ->
|
||||
// Either String -> (a, a, a)
|
||||
const vectorTriple = q =>
|
||||
// The vector triple product.
|
||||
r => s => bindLR(crossProduct(r)(s))(
|
||||
crossProduct(q)
|
||||
);
|
||||
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
// TEST -------------------------------------------
|
||||
const
|
||||
a = [3, 4, 5],
|
||||
b = [4, 3, 5],
|
||||
c = [-5, -12, -13],
|
||||
d = [3, 4, 5, 6];
|
||||
|
||||
console.log(unlines(
|
||||
zipWith(k => f => k + show(
|
||||
saturated(f)([a, b, c])
|
||||
))(['a . b', 'a x b', 'a . (b x c)', 'a x (b x c)'])(
|
||||
[dotProduct, crossProduct, scalarTriple, vectorTriple]
|
||||
)
|
||||
.concat([
|
||||
'a . d' + show(
|
||||
dotProduct(a)(d)
|
||||
),
|
||||
'a . (b x d)' + show(
|
||||
scalarTriple(a)(b)(d)
|
||||
)
|
||||
])
|
||||
));
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// Left :: a -> Either a b
|
||||
const Left = x => ({
|
||||
type: 'Either',
|
||||
Left: x
|
||||
});
|
||||
|
||||
// Right :: b -> Either a b
|
||||
const Right = x => ({
|
||||
type: 'Either',
|
||||
Right: x
|
||||
});
|
||||
|
||||
// bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
|
||||
const bindLR = m => mf =>
|
||||
undefined !== m.Left ? (
|
||||
m
|
||||
) : mf(m.Right);
|
||||
|
||||
// either :: (a -> c) -> (b -> c) -> Either a b -> c
|
||||
const either = fl => fr => e =>
|
||||
'Either' === e.type ? (
|
||||
undefined !== e.Left ? (
|
||||
fl(e.Left)
|
||||
) : fr(e.Right)
|
||||
) : undefined;
|
||||
|
||||
// identity :: a -> a
|
||||
const identity = x => x;
|
||||
|
||||
// mul (*) :: Num a => a -> a -> a
|
||||
const mul = a => b => a * b;
|
||||
|
||||
// Curried function -> [Argument] -> a more saturated value
|
||||
const saturated = f =>
|
||||
// A curried function applied successively to
|
||||
// a list of arguments up to, but not beyond,
|
||||
// the point of saturation.
|
||||
args => 0 < args.length ? (
|
||||
args.slice(1).reduce(
|
||||
(a, x) => 'function' !== typeof a ? (
|
||||
a
|
||||
) : a(x),
|
||||
f(args[0])
|
||||
)
|
||||
) : f;
|
||||
|
||||
// show :: Either String a -> String
|
||||
const show = x =>
|
||||
either(x => ' => ' + x)(
|
||||
x => ' = ' + JSON.stringify(x)
|
||||
)(x);
|
||||
|
||||
// sum :: [Num] -> Num
|
||||
const sum = xs => xs.reduce((a, x) => a + x, 0);
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
// zipWith:: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f => xs => ys =>
|
||||
xs.slice(
|
||||
0, Math.min(xs.length, ys.length)
|
||||
).map((x, i) => f(x)(ys[i]));
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
21
Task/Vector-products/Jq/vector-products-1.jq
Normal file
21
Task/Vector-products/Jq/vector-products-1.jq
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
def dot_product(a; b):
|
||||
reduce range(0;a|length) as $i (0; . + (a[$i] * b[$i]) );
|
||||
|
||||
# for 3d vectors
|
||||
def cross_product(a;b):
|
||||
[ a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1]-a[1]*b[0] ];
|
||||
|
||||
def scalar_triple_product(a;b;c):
|
||||
dot_product(a; cross_product(b; c));
|
||||
|
||||
def vector_triple_product(a;b;c):
|
||||
cross_product(a; cross_product(b; c));
|
||||
|
||||
def main:
|
||||
[3, 4, 5] as $a
|
||||
| [4, 3, 5] as $b
|
||||
| [-5, -12, -13] as $c
|
||||
| "a . b = \(dot_product($a; $b))",
|
||||
"a x b = [\( cross_product($a; $b) | map(tostring) | join (", ") )]" ,
|
||||
"a . (b x c) = \( scalar_triple_product ($a; $b; $c)) )",
|
||||
"a x (b x c) = [\( vector_triple_product($a; $b; $c)|map(tostring)|join (", ") )]" ;
|
||||
4
Task/Vector-products/Jq/vector-products-2.jq
Normal file
4
Task/Vector-products/Jq/vector-products-2.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
"a . b = 49"
|
||||
"a x b = [5, 5, -7]"
|
||||
"a . (b x c) = 6 )"
|
||||
"a x (b x c) = [-267, 204, -3]"
|
||||
14
Task/Vector-products/Julia/vector-products.julia
Normal file
14
Task/Vector-products/Julia/vector-products.julia
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
using LinearAlgebra
|
||||
|
||||
const a = [3, 4, 5]
|
||||
const b = [4, 3, 5]
|
||||
const c = [-5, -12, -13]
|
||||
|
||||
println("Test Vectors:")
|
||||
@show a b c
|
||||
|
||||
println("\nVector Products:")
|
||||
@show a ⋅ b
|
||||
@show a × b
|
||||
@show a ⋅ (b × c)
|
||||
@show a × (b × c)
|
||||
28
Task/Vector-products/Kotlin/vector-products.kotlin
Normal file
28
Task/Vector-products/Kotlin/vector-products.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// version 1.1.2
|
||||
|
||||
class Vector3D(val x: Double, val y: Double, val z: Double) {
|
||||
infix fun dot(v: Vector3D) = x * v.x + y * v.y + z * v.z
|
||||
|
||||
infix fun cross(v: Vector3D) =
|
||||
Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x)
|
||||
|
||||
fun scalarTriple(v: Vector3D, w: Vector3D) = this dot (v cross w)
|
||||
|
||||
fun vectorTriple(v: Vector3D, w: Vector3D) = this cross (v cross w)
|
||||
|
||||
override fun toString() = "($x, $y, $z)"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a = Vector3D(3.0, 4.0, 5.0)
|
||||
val b = Vector3D(4.0, 3.0, 5.0)
|
||||
val c = Vector3D(-5.0, -12.0, -13.0)
|
||||
println("a = $a")
|
||||
println("b = $b")
|
||||
println("c = $c")
|
||||
println()
|
||||
println("a . b = ${a dot b}")
|
||||
println("a x b = ${a cross b}")
|
||||
println("a . b x c = ${a.scalarTriple(b, c)}")
|
||||
println("a x b x c = ${a.vectorTriple(b, c)}")
|
||||
}
|
||||
84
Task/Vector-products/Ksh/vector-products.ksh
Normal file
84
Task/Vector-products/Ksh/vector-products.ksh
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
#!/bin/ksh
|
||||
|
||||
# Vector products
|
||||
# # dot product (a scalar quantity) A • B = a1b1 + a2b2 + a3b3 + ...
|
||||
# # cross product (a vector quantity) A x B = (a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1)
|
||||
# # scalar triple product (a scalar quantity) A • (B x C)
|
||||
# # vector triple product (a vector quantity) A x (B x C)
|
||||
|
||||
# # Variables:
|
||||
#
|
||||
typeset -a A=( 3 4 5 )
|
||||
typeset -a B=( 4 3 5 )
|
||||
typeset -a C=( -5 -12 -13 )
|
||||
|
||||
# # Functions:
|
||||
#
|
||||
|
||||
# # Function _dotprod(vec1, vec2) - Return the (scalar) dot product of 2 vectors
|
||||
#
|
||||
function _dotprod {
|
||||
typeset _vec1 ; nameref _vec1="$1" # Input vector 1
|
||||
typeset _vec2 ; nameref _vec2="$2" # Input vector 2
|
||||
typeset _i ; typeset -si _i
|
||||
typeset _dotp ; integer _dotp=0
|
||||
|
||||
for ((_i=0; _i<${#_vec1[*]}; _i++)); do
|
||||
(( _dotp+=(_vec1[_i] * _vec2[_i]) ))
|
||||
done
|
||||
echo ${_dotp}
|
||||
}
|
||||
|
||||
# # Function _crossprod(vec1, vec2, vec) - Return the (vector) cross product of 2 vectors
|
||||
#
|
||||
function _crossprod {
|
||||
typeset _vec1 ; nameref _vec1="$1" # Input vector 1
|
||||
typeset _vec2 ; nameref _vec2="$2" # Input vector 2
|
||||
typeset _vec3 ; nameref _vec3="$3" # Output vector
|
||||
|
||||
_vec3+=( $(( _vec1[1]*_vec2[2] - _vec1[2]*_vec2[1] )) )
|
||||
_vec3+=( $(( _vec1[2]*_vec2[0] - _vec1[0]*_vec2[2] )) )
|
||||
_vec3+=( $(( _vec1[0]*_vec2[1] - _vec1[1]*_vec2[0] )) )
|
||||
}
|
||||
|
||||
# # Function _scal3prod(vec1, vec2, vec3) - Return the (scalar) scalar triple product of 3 vectors
|
||||
#
|
||||
function _scal3prod {
|
||||
typeset _vec1 ; nameref _vec1="$1" # Input vector 1
|
||||
typeset _vec2 ; nameref _vec2="$2" # Input vector 2
|
||||
typeset _vec3 ; nameref _vec3="$3" # Input vector 3
|
||||
typeset _vect ; typeset -a _vect # temp vector
|
||||
|
||||
_crossprod _vec2 _vec3 _vect # (B x C)
|
||||
echo $(_dotprod _vec1 _vect) # A • (B x C)
|
||||
|
||||
}
|
||||
|
||||
# # Function _vect3prod(vec1, vec2, vec3, vec) - Return the (vector) vector triple product of 3 vectors
|
||||
#
|
||||
function _vect3prod {
|
||||
typeset _vec1 ; nameref _vec1="$1" # Input vector 1
|
||||
typeset _vec2 ; nameref _vec2="$2" # Input vector 2
|
||||
typeset _vec3 ; nameref _vec3="$3" # Input vector 3
|
||||
typeset _vec4 ; nameref _vec4="$4" # Output vector
|
||||
typeset _vect ; typeset -a _vect # temp vector
|
||||
|
||||
_crossprod _vec2 _vec3 _vect # (B x C)
|
||||
_crossprod _vec1 _vect _vec4 # A x (B x C)
|
||||
}
|
||||
|
||||
######
|
||||
# main #
|
||||
######
|
||||
|
||||
print "The dot product A • B = $(_dotprod A B)"
|
||||
|
||||
typeset -a arr
|
||||
_crossprod A B arr
|
||||
print "The cross product A x B = ( ${arr[@]} )"
|
||||
|
||||
print "The scalar triple product A • (B x C) = $(_scal3prod A B C)"
|
||||
|
||||
typeset -m crossprod=arr ; typeset -a arr
|
||||
_vect3prod A B C arr
|
||||
print "The vector triple product A x (B x C) = ( ${arr[@]} )"
|
||||
25
Task/Vector-products/Lambdatalk/vector-products.lambdatalk
Normal file
25
Task/Vector-products/Lambdatalk/vector-products.lambdatalk
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
{def dotProduct
|
||||
{lambda {:a :b}
|
||||
{+ {* {A.get 0 :a} {A.get 0 :b}}
|
||||
{* {A.get 1 :a} {A.get 1 :b}}
|
||||
{* {A.get 2 :a} {A.get 2 :b}}}}}
|
||||
-> dotProduct
|
||||
|
||||
{def crossProduct
|
||||
{lambda {:a :b}
|
||||
{A.new {- {* {A.get 1 :a} {A.get 2 :b}}
|
||||
{* {A.get 2 :a} {A.get 1 :b}}}
|
||||
{- {* {A.get 2 :a} {A.get 0 :b}}
|
||||
{* {A.get 0 :a} {A.get 2 :b}}}
|
||||
{- {* {A.get 0 :a} {A.get 1 :b}}
|
||||
{* {A.get 1 :a} {A.get 0 :b}}} }}}
|
||||
-> crossProduct
|
||||
|
||||
{def A {A.new 3 4 5}} -> A = [3,4,5]
|
||||
{def B {A.new 4 3 5}} -> B = [4,3,5]
|
||||
{def C {A.new -5 -12 -13}} -> C = [4,3,5]
|
||||
|
||||
A.B : {dotProduct {A} {B}} -> 49
|
||||
AxB : {crossProduct {A} {B}} -> [5,5,-7]
|
||||
A.(BxC) : {dotProduct {A} {crossProduct {B} {C}}} -> 6
|
||||
Ax(BxC) : {crossProduct {A} {crossProduct {B} {C}}} -> [-267,204,-3]
|
||||
45
Task/Vector-products/Liberty-BASIC/vector-products.basic
Normal file
45
Task/Vector-products/Liberty-BASIC/vector-products.basic
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
print "Vector products of 3-D vectors"
|
||||
|
||||
print "Dot product of 3,4,5 and 4,3,5 is "
|
||||
print DotProduct( "3,4,5", "4,3,5")
|
||||
print "Cross product of 3,4,5 and 4,3,5 is "
|
||||
print CrossProduct$( "3,4,5", "4,3,5")
|
||||
print "Scalar triple product of 3,4,5, 4,3,5 -5, -12, -13 is "
|
||||
print ScalarTripleProduct( "3,4,5", "4,3,5", "-5, -12, -13")
|
||||
print "Vector triple product of 3,4,5, 4,3,5 -5, -12, -13 is "
|
||||
print VectorTripleProduct$( "3,4,5", "4,3,5", "-5, -12, -13")
|
||||
|
||||
|
||||
end
|
||||
|
||||
function DotProduct( i$, j$)
|
||||
ix =val( word$( i$, 1, ","))
|
||||
iy =val( word$( i$, 2, ","))
|
||||
iz =val( word$( i$, 3, ","))
|
||||
jx =val( word$( j$, 1, ","))
|
||||
jy =val( word$( j$, 2, ","))
|
||||
jz =val( word$( j$, 3, ","))
|
||||
DotProduct = ix *jx +iy *jy + iz *jz
|
||||
end function
|
||||
|
||||
function CrossProduct$( i$, j$)
|
||||
ix =val( word$( i$, 1, ","))
|
||||
iy =val( word$( i$, 2, ","))
|
||||
iz =val( word$( i$, 3, ","))
|
||||
jx =val( word$( j$, 1, ","))
|
||||
jy =val( word$( j$, 2, ","))
|
||||
jz =val( word$( j$, 3, ","))
|
||||
cpx =iy *jz -iz *jy
|
||||
cpy =iz *jx -ix *jz
|
||||
cpz =ix *jy -iy *jx
|
||||
CrossProduct$ =str$( cpx); ","; str$( cpy); ","; str$( cpz)
|
||||
end function
|
||||
|
||||
function ScalarTripleProduct( i$, j$, k$))
|
||||
ScalarTripleProduct =DotProduct( i$, CrossProduct$( j$, k$))
|
||||
end function
|
||||
|
||||
function VectorTripleProduct$( i$, j$, k$))
|
||||
VectorTripleProduct$ =CrossProduct$( i$, CrossProduct$( j$, k$))
|
||||
end function
|
||||
END SUB
|
||||
11
Task/Vector-products/Lingo/vector-products.lingo
Normal file
11
Task/Vector-products/Lingo/vector-products.lingo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
a = vector(1,2,3)
|
||||
b = vector(4,5,6)
|
||||
|
||||
put a * b
|
||||
-- 32.0000
|
||||
|
||||
put a.dot(b)
|
||||
-- 32.0000
|
||||
|
||||
put a.cross(b)
|
||||
-- vector( -3.0000, 6.0000, -3.0000 )
|
||||
37
Task/Vector-products/Lua/vector-products.lua
Normal file
37
Task/Vector-products/Lua/vector-products.lua
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
Vector = {}
|
||||
function Vector.new( _x, _y, _z )
|
||||
return { x=_x, y=_y, z=_z }
|
||||
end
|
||||
|
||||
function Vector.dot( A, B )
|
||||
return A.x*B.x + A.y*B.y + A.z*B.z
|
||||
end
|
||||
|
||||
function Vector.cross( A, B )
|
||||
return { x = A.y*B.z - A.z*B.y,
|
||||
y = A.z*B.x - A.x*B.z,
|
||||
z = A.x*B.y - A.y*B.x }
|
||||
end
|
||||
|
||||
function Vector.scalar_triple( A, B, C )
|
||||
return Vector.dot( A, Vector.cross( B, C ) )
|
||||
end
|
||||
|
||||
function Vector.vector_triple( A, B, C )
|
||||
return Vector.cross( A, Vector.cross( B, C ) )
|
||||
end
|
||||
|
||||
|
||||
A = Vector.new( 3, 4, 5 )
|
||||
B = Vector.new( 4, 3, 5 )
|
||||
C = Vector.new( -5, -12, -13 )
|
||||
|
||||
print( Vector.dot( A, B ) )
|
||||
|
||||
r = Vector.cross(A, B )
|
||||
print( r.x, r.y, r.z )
|
||||
|
||||
print( Vector.scalar_triple( A, B, C ) )
|
||||
|
||||
r = Vector.vector_triple( A, B, C )
|
||||
print( r.x, r.y, r.z )
|
||||
69
Task/Vector-products/M2000-Interpreter/vector-products.m2000
Normal file
69
Task/Vector-products/M2000-Interpreter/vector-products.m2000
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
Module checkit {
|
||||
class Vector {
|
||||
\\ by default are double
|
||||
a,b,c
|
||||
Property ToString$ {
|
||||
Value {
|
||||
link parent a,b,c to a,b,c
|
||||
value$=format$("({0}, {1}, {2})",a,b,c)
|
||||
}
|
||||
}
|
||||
Operator "==" {
|
||||
read n
|
||||
push .a==n.a and .b==n.b and .c==n.c
|
||||
}
|
||||
Operator Unary {
|
||||
.a-! : .b-! : .c-!
|
||||
}
|
||||
Operator "+" {
|
||||
Read v2
|
||||
For this, v2 {
|
||||
.a+=..a :.b+=..b:.c+=..c:
|
||||
}
|
||||
}
|
||||
Function Mul(r) {
|
||||
vv=this
|
||||
for vv {
|
||||
.a*=r:.b*=r:.c*=r
|
||||
}
|
||||
=vv
|
||||
}
|
||||
Function Dot(v2) {
|
||||
def double sum
|
||||
for this, v2 {
|
||||
sum=.a*..a+.b*..b+.c*..c
|
||||
}
|
||||
=sum
|
||||
}
|
||||
Operator "*" {
|
||||
Read v2
|
||||
For This, v2 {
|
||||
Push .b*..c-.c*..b
|
||||
Push .c*..a-.a*..c
|
||||
.c<=.a*..b-.b*..a
|
||||
Read .b, .a
|
||||
}
|
||||
}
|
||||
class:
|
||||
module Vector {
|
||||
if match("NNN") then {
|
||||
Read .a,.b,.c
|
||||
}
|
||||
}
|
||||
}
|
||||
A=Vector(3,4,5)
|
||||
B=Vector(4,3,5)
|
||||
C=Vector(-5,-12,-13)
|
||||
Print "A=";A.toString$
|
||||
Print "B=";B.toString$
|
||||
Print "C=";C.toString$
|
||||
Print "A dot B="; A.dot(B)
|
||||
AxB=A*B
|
||||
Print "A x B="; AxB.toString$
|
||||
Print "A dot (B x C)=";A.dot(B*C)
|
||||
AxBxC=A*(B*C)
|
||||
Print "A x (B x C)=";AxBxC.toString$
|
||||
Def ToString$(a)=a.toString$
|
||||
Print "A x (B x C)=";ToString$(A*(B*C))
|
||||
}
|
||||
Checkit
|
||||
16
Task/Vector-products/MATLAB/vector-products.m
Normal file
16
Task/Vector-products/MATLAB/vector-products.m
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
% Create a named function/subroutine/method to compute the dot product of two vectors.
|
||||
dot(a,b)
|
||||
% Create a function to compute the cross product of two vectors.
|
||||
cross(a,b)
|
||||
% Optionally create a function to compute the scalar triple product of three vectors.
|
||||
dot(a,cross(b,c))
|
||||
% Optionally create a function to compute the vector triple product of three vectors.
|
||||
cross(a,cross(b,c))
|
||||
% Compute and display: a • b
|
||||
cross(a,b)
|
||||
% Compute and display: a x b
|
||||
cross(a,b)
|
||||
% Compute and display: a • b x c, the scaler triple product.
|
||||
dot(a,cross(b,c))
|
||||
% Compute and display: a x b x c, the vector triple product.
|
||||
cross(a,cross(b,c))
|
||||
12
Task/Vector-products/Maple/vector-products.maple
Normal file
12
Task/Vector-products/Maple/vector-products.maple
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
with(LinearAlgebra):
|
||||
A := Vector([3,4,5]):
|
||||
B := Vector([4,3,5]):
|
||||
C := Vector([-5,-12,-13]):
|
||||
>>>A.B;
|
||||
49
|
||||
>>>CrossProduct(A,B);
|
||||
Vector([5, 5, -7])
|
||||
>>>A.(CrossProduct(B,C));
|
||||
6
|
||||
>>>CrossProduct(A,CrossProduct(B,C));
|
||||
Vector([-267, 204, -3])
|
||||
7
Task/Vector-products/Mathematica/vector-products.math
Normal file
7
Task/Vector-products/Mathematica/vector-products.math
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
a={3,4,5};
|
||||
b={4,3,5};
|
||||
c={-5,-12,-13};
|
||||
a.b
|
||||
Cross[a,b]
|
||||
a.Cross[b,c]
|
||||
Cross[a,Cross[b,c]]
|
||||
42
Task/Vector-products/Mercury/vector-products.mercury
Normal file
42
Task/Vector-products/Mercury/vector-products.mercury
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
:- module vector_product.
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module int, list, string.
|
||||
|
||||
main(!IO) :-
|
||||
A = vector3d(3, 4, 5),
|
||||
B = vector3d(4, 3, 5),
|
||||
C = vector3d(-5, -12, -13),
|
||||
io.format("A . B = %d\n", [i(A `dot_product` B)], !IO),
|
||||
io.format("A x B = %s\n", [s(to_string(A `cross_product` B))], !IO),
|
||||
io.format("A . (B x C) = %d\n", [i(scalar_triple_product(A, B, C))], !IO),
|
||||
io.format("A x (B x C) = %s\n", [s(to_string(vector_triple_product(A, B, C)))], !IO).
|
||||
|
||||
:- type vector3d ---> vector3d(int, int, int).
|
||||
|
||||
:- func dot_product(vector3d, vector3d) = int.
|
||||
|
||||
dot_product(vector3d(A1, A2, A3), vector3d(B1, B2, B3)) =
|
||||
A1 * B1 + A2 * B2 + A3 * B3.
|
||||
|
||||
:- func cross_product(vector3d, vector3d) = vector3d.
|
||||
|
||||
cross_product(vector3d(A1, A2, A3), vector3d(B1, B2, B3)) =
|
||||
vector3d(A2 * B3 - A3 * B2, A3 * B1 - A1 * B3, A1 * B2 - A2 * B1).
|
||||
|
||||
:- func scalar_triple_product(vector3d, vector3d, vector3d) = int.
|
||||
|
||||
scalar_triple_product(A, B, C) = A `dot_product` (B `cross_product` C).
|
||||
|
||||
:- func vector_triple_product(vector3d, vector3d, vector3d) = vector3d.
|
||||
|
||||
vector_triple_product(A, B, C) = A `cross_product` (B `cross_product` C).
|
||||
|
||||
:- func to_string(vector3d) = string.
|
||||
|
||||
to_string(vector3d(X, Y, Z)) =
|
||||
string.format("(%d, %d, %d)", [i(X), i(Y), i(Z)]).
|
||||
16
Task/Vector-products/MiniScript/vector-products.mini
Normal file
16
Task/Vector-products/MiniScript/vector-products.mini
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
vectorA = [3, 4, 5]
|
||||
vectorB = [4, 3, 5]
|
||||
vectorC = [-5, -12, -13]
|
||||
|
||||
dotProduct = function(x, y)
|
||||
return x[0]*y[0] + x[1]*y[1] + x[2]*y[2]
|
||||
end function
|
||||
|
||||
crossProduct = function(x, y)
|
||||
return [x[1]*y[2] - x[2]*y[1], x[2]*y[0] - x[0]*y[2], x[0]*y[1] - x[1]*y[0]]
|
||||
end function
|
||||
|
||||
print "Dot Product = " + dotProduct(vectorA, vectorB)
|
||||
print "Cross Product = " + crossProduct(vectorA, vectorB)
|
||||
print "Scalar Triple Product = " + dotProduct(vectorA, crossProduct(vectorB,vectorC))
|
||||
print "Vector Triple Product = " + crossProduct(vectorA, crossProduct(vectorB,vectorC))
|
||||
83
Task/Vector-products/Modula-2/vector-products.mod2
Normal file
83
Task/Vector-products/Modula-2/vector-products.mod2
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
MODULE VectorProducts;
|
||||
FROM RealStr IMPORT RealToStr;
|
||||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
|
||||
|
||||
PROCEDURE WriteReal(r : REAL);
|
||||
VAR buf : ARRAY[0..31] OF CHAR;
|
||||
BEGIN
|
||||
RealToStr(r, buf);
|
||||
WriteString(buf)
|
||||
END WriteReal;
|
||||
|
||||
TYPE Vector = RECORD
|
||||
a,b,c : REAL;
|
||||
END;
|
||||
|
||||
PROCEDURE Dot(u,v : Vector) : REAL;
|
||||
BEGIN
|
||||
RETURN u.a * v.a
|
||||
+ u.b * v.b
|
||||
+ u.c * v.c
|
||||
END Dot;
|
||||
|
||||
PROCEDURE Cross(u,v : Vector) : Vector;
|
||||
BEGIN
|
||||
RETURN Vector{
|
||||
u.b*v.c - u.c*v.b,
|
||||
u.c*v.a - u.a*v.c,
|
||||
u.a*v.b - u.b*v.a
|
||||
}
|
||||
END Cross;
|
||||
|
||||
PROCEDURE ScalarTriple(u,v,w : Vector) : REAL;
|
||||
BEGIN
|
||||
RETURN Dot(u, Cross(v, w))
|
||||
END ScalarTriple;
|
||||
|
||||
PROCEDURE VectorTriple(u,v,w : Vector) : Vector;
|
||||
BEGIN
|
||||
RETURN Cross(u, Cross(v, w))
|
||||
END VectorTriple;
|
||||
|
||||
PROCEDURE WriteVector(v : Vector);
|
||||
BEGIN
|
||||
WriteString("<");
|
||||
WriteReal(v.a);
|
||||
WriteString(", ");
|
||||
WriteReal(v.b);
|
||||
WriteString(", ");
|
||||
WriteReal(v.c);
|
||||
WriteString(">")
|
||||
END WriteVector;
|
||||
|
||||
VAR a,b,c : Vector;
|
||||
BEGIN
|
||||
a := Vector{3.0, 4.0, 5.0};
|
||||
b := Vector{4.0, 3.0, 5.0};
|
||||
c := Vector{-5.0, -12.0, -13.0};
|
||||
|
||||
WriteVector(a);
|
||||
WriteString(" dot ");
|
||||
WriteVector(b);
|
||||
WriteString(" = ");
|
||||
WriteReal(Dot(a,b));
|
||||
WriteLn;
|
||||
|
||||
WriteVector(a);
|
||||
WriteString(" cross ");
|
||||
WriteVector(b);
|
||||
WriteString(" = ");
|
||||
WriteVector(Cross(a,b));
|
||||
WriteLn;
|
||||
|
||||
WriteVector(a);
|
||||
WriteString(" cross (");
|
||||
WriteVector(b);
|
||||
WriteString(" cross ");
|
||||
WriteVector(c);
|
||||
WriteString(") = ");
|
||||
WriteVector(VectorTriple(a,b,c));
|
||||
WriteLn;
|
||||
|
||||
ReadChar
|
||||
END VectorProducts.
|
||||
36
Task/Vector-products/Nemerle/vector-products.nemerle
Normal file
36
Task/Vector-products/Nemerle/vector-products.nemerle
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System.Console;
|
||||
|
||||
module VectorProducts3d
|
||||
{
|
||||
Dot(x : int * int * int, y : int * int * int) : int
|
||||
{
|
||||
def (x1, x2, x3) = x;
|
||||
def (y1, y2, y3) = y;
|
||||
(x1 * y1) + (x2 * y2) + (x3 * y3)
|
||||
}
|
||||
|
||||
Cross(x : int * int * int, y : int * int * int) : int * int * int
|
||||
{
|
||||
def (x1, x2, x3) = x;
|
||||
def (y1, y2, y3) = y;
|
||||
((x2 * y3 - x3 * y2), (x3 * y1 - x1 * y3), (x1 * y2 - x2 * y1))
|
||||
}
|
||||
|
||||
ScalarTriple(a : int * int * int, b : int * int * int, c : int * int * int) : int
|
||||
{
|
||||
Dot(a, Cross(b, c))
|
||||
}
|
||||
|
||||
VectorTriple(a : int * int * int, b : int * int * int, c : int * int * int) : int * int * int
|
||||
{
|
||||
Cross(a, Cross(b, c))
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def a = (3, 4, 5); def b = (4, 3, 5); def c = (-5, -12, -13);
|
||||
WriteLine(Dot(a, b)); WriteLine(Cross(a, b));
|
||||
WriteLine(ScalarTriple(a, b, c));
|
||||
WriteLine(VectorTriple(a, b, c));
|
||||
}
|
||||
}
|
||||
35
Task/Vector-products/Never/vector-products.never
Normal file
35
Task/Vector-products/Never/vector-products.never
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
func printv(a[d] : float) -> int {
|
||||
prints("[" + a[0] + ", " + a[1] + ", " + a[2] + "]\n");
|
||||
0
|
||||
}
|
||||
|
||||
func dot(a[d1] : float, b[d2] : float) -> float {
|
||||
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
|
||||
}
|
||||
|
||||
func cross(a[d1] : float, b[d2] : float) -> [_] : float {
|
||||
[ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ] : float
|
||||
}
|
||||
|
||||
func scalar_triple(a[d1] : float, b[d2] : float, c[d3] : float) -> float {
|
||||
dot(a, cross(b, c))
|
||||
}
|
||||
|
||||
func vector_triple(a[d1] : float, b[d2] : float, c[d3] : float) -> [_] : float {
|
||||
cross(a, cross(b, c))
|
||||
}
|
||||
|
||||
func main() -> int {
|
||||
var a = [ 3.0, 4.0, 5.0 ] : float;
|
||||
var b = [ 4.0, 3.0, 5.0 ] : float;
|
||||
var c = [ -5.0, -12.0, -13.0 ] : float;
|
||||
|
||||
printv(a);
|
||||
printv(b);
|
||||
printv(c);
|
||||
printf(dot(a, b));
|
||||
printv(cross(a, b));
|
||||
printf(scalar_triple(a, b, c));
|
||||
printv(vector_triple(a, b, c));
|
||||
0
|
||||
}
|
||||
31
Task/Vector-products/Nim/vector-products.nim
Normal file
31
Task/Vector-products/Nim/vector-products.nim
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import strformat, strutils
|
||||
|
||||
type Vector3 = array[1..3, float]
|
||||
|
||||
proc `$`(a: Vector3): string =
|
||||
result = "("
|
||||
for x in a:
|
||||
result.addSep(", ", 1)
|
||||
result.add &"{x}"
|
||||
result.add ')'
|
||||
|
||||
proc cross(a, b: Vector3): Vector3 =
|
||||
result = [a[2]*b[3] - a[3]*b[2], a[3]*b[1] - a[1]*b[3], a[1]*b[2] - a[2]*b[1]]
|
||||
|
||||
proc dot(a, b: Vector3): float =
|
||||
for i in a.low..a.high:
|
||||
result += a[i] * b[i]
|
||||
|
||||
proc scalarTriple(a, b, c: Vector3): float = a.dot(b.cross(c))
|
||||
|
||||
proc vectorTriple(a, b, c: Vector3): Vector3 = a.cross(b.cross(c))
|
||||
|
||||
let
|
||||
a = [3.0, 4.0, 5.0]
|
||||
b = [4.0, 3.0, 5.0]
|
||||
c = [-5.0, -12.0, -13.0]
|
||||
|
||||
echo &"a ⨯ b = {a.cross(b)}"
|
||||
echo &"a . b = {a.dot(b)}"
|
||||
echo &"a . (b ⨯ c) = {scalarTriple(a, b, c)}"
|
||||
echo &"a ⨯ (b ⨯ c) = {vectorTriple(a, b, c)}"
|
||||
30
Task/Vector-products/OCaml/vector-products.ocaml
Normal file
30
Task/Vector-products/OCaml/vector-products.ocaml
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
let a = (3.0, 4.0, 5.0)
|
||||
let b = (4.0, 3.0, 5.0)
|
||||
let c = (-5.0, -12.0, -13.0)
|
||||
|
||||
let string_of_vector (x,y,z) =
|
||||
Printf.sprintf "(%g, %g, %g)" x y z
|
||||
|
||||
let dot (a1, a2, a3) (b1, b2, b3) =
|
||||
(a1 *. b1) +. (a2 *. b2) +. (a3 *. b3)
|
||||
|
||||
let cross (a1, a2, a3) (b1, b2, b3) =
|
||||
(a2 *. b3 -. a3 *. b2,
|
||||
a3 *. b1 -. a1 *. b3,
|
||||
a1 *. b2 -. a2 *. b1)
|
||||
|
||||
let scalar_triple a b c =
|
||||
dot a (cross b c)
|
||||
|
||||
let vector_triple a b c =
|
||||
cross a (cross b c)
|
||||
|
||||
let () =
|
||||
Printf.printf "a: %s\n" (string_of_vector a);
|
||||
Printf.printf "b: %s\n" (string_of_vector b);
|
||||
Printf.printf "c: %s\n" (string_of_vector c);
|
||||
Printf.printf "a . b = %g\n" (dot a b);
|
||||
Printf.printf "a x b = %s\n" (string_of_vector (cross a b));
|
||||
Printf.printf "a . (b x c) = %g\n" (scalar_triple a b c);
|
||||
Printf.printf "a x (b x c) = %s\n" (string_of_vector (vector_triple a b c));
|
||||
;;
|
||||
63
Task/Vector-products/Objeck/vector-products.objeck
Normal file
63
Task/Vector-products/Objeck/vector-products.objeck
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
bundle Default {
|
||||
class VectorProduct {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
a := Vector3D->New(3.0, 4.0, 5.0);
|
||||
b := Vector3D->New(4.0, 3.0, 5.0);
|
||||
c := Vector3D->New(-5.0, -12.0, -13.0);
|
||||
|
||||
a->Dot(b)->Print();
|
||||
a->Cross(b)->Print();
|
||||
a->ScaleTrip(b, c)->Print();
|
||||
a->VectorTrip(b, c)->Print();
|
||||
}
|
||||
}
|
||||
|
||||
class Vector3D {
|
||||
@a : Float;
|
||||
@b : Float;
|
||||
@c : Float;
|
||||
|
||||
New(a : Float, b : Float, c : Float) {
|
||||
@a := a;
|
||||
@b := b;
|
||||
@c := c;
|
||||
}
|
||||
|
||||
method : GetA() ~ Float {
|
||||
return @a;
|
||||
}
|
||||
|
||||
method : GetB() ~ Float {
|
||||
return @b;
|
||||
}
|
||||
|
||||
method : GetC() ~ Float {
|
||||
return @c;
|
||||
}
|
||||
|
||||
method : public : Dot(vec : Vector3D) ~ Float {
|
||||
return @a * vec->GetA() + @b * vec->GetB() + @c * vec->GetC();
|
||||
}
|
||||
|
||||
method : public : Cross(vec : Vector3D) ~ Vector3D {
|
||||
newA := @b * vec->GetC() - @c * vec->GetB();
|
||||
newB := @c * vec->GetA() - @a * vec->GetC();
|
||||
newC := @a * vec->GetB() - @b * vec->GetA();
|
||||
|
||||
return Vector3D->New(newA, newB, newC);
|
||||
}
|
||||
|
||||
method : public : ScaleTrip(vec_b: Vector3D, vec_c : Vector3D) ~ Float {
|
||||
return Dot(vec_b->Cross(vec_c));
|
||||
}
|
||||
|
||||
method : public : Print() ~ Nil {
|
||||
IO.Console->Print('<')->Print(@a)->Print(" ,")
|
||||
->Print(@b)->Print(", ")->Print(@c)->PrintLine('>');
|
||||
}
|
||||
|
||||
method : public : VectorTrip(vec_b: Vector3D, vec_c : Vector3D) ~ Vector3D {
|
||||
return Cross(vec_b->Cross(vec_c));
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Task/Vector-products/Octave/vector-products.octave
Normal file
25
Task/Vector-products/Octave/vector-products.octave
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
a = [3, 4, 5];
|
||||
b = [4, 3, 5];
|
||||
c = [-5, -12, -13];
|
||||
|
||||
function r = s3prod(a, b, c)
|
||||
r = dot(a, cross(b, c));
|
||||
endfunction
|
||||
|
||||
function r = v3prod(a, b, c)
|
||||
r = cross(a, cross(b, c));
|
||||
endfunction
|
||||
|
||||
% 49
|
||||
dot(a, b)
|
||||
% or matrix-multiplication between row and column vectors
|
||||
a * b'
|
||||
|
||||
% 5 5 -7
|
||||
cross(a, b) % only for 3d-vectors
|
||||
|
||||
% 6
|
||||
s3prod(a, b, c)
|
||||
|
||||
% -267 204 -3
|
||||
v3prod(a, b, c)
|
||||
49
Task/Vector-products/OoRexx/vector-products.rexx
Normal file
49
Task/Vector-products/OoRexx/vector-products.rexx
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
a = .vector~new(3, 4, 5);
|
||||
b = .vector~new(4, 3, 5);
|
||||
c = .vector~new(-5, -12, -13);
|
||||
|
||||
say a~dot(b)
|
||||
say a~cross(b)
|
||||
say a~scalarTriple(b, c)
|
||||
say a~vectorTriple(b, c)
|
||||
|
||||
|
||||
::class vector
|
||||
::method init
|
||||
expose x y z
|
||||
use arg x, y, z
|
||||
|
||||
::attribute x get
|
||||
::attribute y get
|
||||
::attribute z get
|
||||
|
||||
-- dot product operation
|
||||
::method dot
|
||||
expose x y z
|
||||
use strict arg other
|
||||
|
||||
return x * other~x + y * other~y + z * other~z
|
||||
|
||||
-- cross product operation
|
||||
::method cross
|
||||
expose x y z
|
||||
use strict arg other
|
||||
|
||||
newX = y * other~z - z * other~y
|
||||
newY = z * other~x - x * other~z
|
||||
newZ = x * other~y - y * other~x
|
||||
return self~class~new(newX, newY, newZ)
|
||||
|
||||
-- scalar triple product
|
||||
::method scalarTriple
|
||||
use strict arg vectorB, vectorC
|
||||
return self~dot(vectorB~cross(vectorC))
|
||||
|
||||
-- vector triple product
|
||||
::method vectorTriple
|
||||
use strict arg vectorB, vectorC
|
||||
return self~cross(vectorB~cross(vectorC))
|
||||
|
||||
::method string
|
||||
expose x y z
|
||||
return "<"||x", "y", "z">"
|
||||
18
Task/Vector-products/PARI-GP/vector-products.parigp
Normal file
18
Task/Vector-products/PARI-GP/vector-products.parigp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
dot(u,v)={
|
||||
sum(i=1,#u,u[i]*v[i])
|
||||
};
|
||||
cross(u,v)={
|
||||
[u[2]*v[3] - u[3]*v[2], u[3]*v[1] - u[1]*v[3], u[1]*v[2] - u[2]*v[1]]
|
||||
};
|
||||
striple(a,b,c)={
|
||||
dot(a,cross(b,c))
|
||||
};
|
||||
vtriple(a,b,c)={
|
||||
cross(a,cross(b,c))
|
||||
};
|
||||
|
||||
a = [3,4,5]; b = [4,3,5]; c = [-5,-12,-13];
|
||||
dot(a,b)
|
||||
cross(a,b)
|
||||
striple(a,b,c)
|
||||
vtriple(a,b,c)
|
||||
104
Task/Vector-products/PHP/vector-products.php
Normal file
104
Task/Vector-products/PHP/vector-products.php
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
class Vector
|
||||
{
|
||||
private $values;
|
||||
|
||||
public function setValues(array $values)
|
||||
{
|
||||
if (count($values) != 3)
|
||||
throw new Exception('Values must contain exactly 3 values');
|
||||
foreach ($values as $value)
|
||||
if (!is_int($value) && !is_float($value))
|
||||
throw new Exception('Value "' . $value . '" has an invalid type');
|
||||
$this->values = $values;
|
||||
}
|
||||
|
||||
public function getValues()
|
||||
{
|
||||
if ($this->values == null)
|
||||
$this->setValues(array (
|
||||
0,
|
||||
0,
|
||||
0
|
||||
));
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
public function Vector(array $values)
|
||||
{
|
||||
$this->setValues($values);
|
||||
}
|
||||
|
||||
public static function dotProduct(Vector $va, Vector $vb)
|
||||
{
|
||||
$a = $va->getValues();
|
||||
$b = $vb->getValues();
|
||||
return ($a[0] * $b[0]) + ($a[1] * $b[1]) + ($a[2] * $b[2]);
|
||||
}
|
||||
|
||||
public static function crossProduct(Vector $va, Vector $vb)
|
||||
{
|
||||
$a = $va->getValues();
|
||||
$b = $vb->getValues();
|
||||
return new Vector(array (
|
||||
($a[1] * $b[2]) - ($a[2] * $b[1]),
|
||||
($a[2] * $b[0]) - ($a[0] * $b[2]),
|
||||
($a[0] * $b[1]) - ($a[1] * $b[0])
|
||||
));
|
||||
}
|
||||
|
||||
public static function scalarTripleProduct(Vector $va, Vector $vb, Vector $vc)
|
||||
{
|
||||
return self::dotProduct($va, self::crossProduct($vb, $vc));
|
||||
}
|
||||
|
||||
public static function vectorTrippleProduct(Vector $va, Vector $vb, Vector $vc)
|
||||
{
|
||||
return self::crossProduct($va, self::crossProduct($vb, $vc));
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
|
||||
public function Program()
|
||||
{
|
||||
$a = array (
|
||||
3,
|
||||
4,
|
||||
5
|
||||
);
|
||||
$b = array (
|
||||
4,
|
||||
3,
|
||||
5
|
||||
);
|
||||
$c = array (
|
||||
-5,
|
||||
-12,
|
||||
-13
|
||||
);
|
||||
$va = new Vector($a);
|
||||
$vb = new Vector($b);
|
||||
$vc = new Vector($c);
|
||||
|
||||
$result1 = Vector::dotProduct($va, $vb);
|
||||
$result2 = Vector::crossProduct($va, $vb)->getValues();
|
||||
$result3 = Vector::scalarTripleProduct($va, $vb, $vc);
|
||||
$result4 = Vector::vectorTrippleProduct($va, $vb, $vc)->getValues();
|
||||
|
||||
printf("\n");
|
||||
printf("A = (%0.2f, %0.2f, %0.2f)\n", $a[0], $a[1], $a[2]);
|
||||
printf("B = (%0.2f, %0.2f, %0.2f)\n", $b[0], $b[1], $b[2]);
|
||||
printf("C = (%0.2f, %0.2f, %0.2f)\n", $c[0], $c[1], $c[2]);
|
||||
printf("\n");
|
||||
printf("A · B = %0.2f\n", $result1);
|
||||
printf("A × B = (%0.2f, %0.2f, %0.2f)\n", $result2[0], $result2[1], $result2[2]);
|
||||
printf("A · (B × C) = %0.2f\n", $result3);
|
||||
printf("A × (B × C) =(%0.2f, %0.2f, %0.2f)\n", $result4[0], $result4[1], $result4[2]);
|
||||
}
|
||||
}
|
||||
|
||||
new Program();
|
||||
?>
|
||||
42
Task/Vector-products/PL-I/vector-products-1.pli
Normal file
42
Task/Vector-products/PL-I/vector-products-1.pli
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* dot product, cross product, etc. 4 June 2011 */
|
||||
|
||||
test_products: procedure options (main);
|
||||
|
||||
declare a(3) fixed initial (3, 4, 5);
|
||||
declare b(3) fixed initial (4, 3, 5);
|
||||
declare c(3) fixed initial (-5, -12, -13);
|
||||
declare e(3) fixed;
|
||||
|
||||
put skip list ('a . b =', dot_product(a, b));
|
||||
call cross_product(a, b, e); put skip list ('a x b =', e);
|
||||
put skip list ('a . (b x c) =', scalar_triple_product(a, b, c));
|
||||
call vector_triple_product(a, b, c, e); put skip list ('a x (b x c) =', e);
|
||||
|
||||
|
||||
dot_product: procedure (a, b) returns (fixed);
|
||||
declare (a, b) (*) fixed;
|
||||
return (sum(a*b));
|
||||
end dot_product;
|
||||
|
||||
cross_product: procedure (a, b, c);
|
||||
declare (a, b, c) (*) fixed;
|
||||
c(1) = a(2)*b(3) - a(3)*b(2);
|
||||
c(2) = a(3)*b(1) - a(1)*b(3);
|
||||
c(3) = a(1)*b(2) - a(2)*b(1);
|
||||
end cross_product;
|
||||
|
||||
scalar_triple_product: procedure (a, b, c) returns (fixed);
|
||||
declare (a, b, c)(*) fixed;
|
||||
declare t(hbound(a, 1)) fixed;
|
||||
call cross_product(b, c, t);
|
||||
return (dot_product(a, t));
|
||||
end scalar_triple_product;
|
||||
|
||||
vector_triple_product: procedure (a, b, c, e);
|
||||
declare (a, b, c, e)(*) fixed;
|
||||
declare t(hbound(a,1)) fixed;
|
||||
call cross_product(b, c, t);
|
||||
call cross_product(a, t, e);
|
||||
end vector_triple_product;
|
||||
|
||||
end test_products;
|
||||
48
Task/Vector-products/PL-I/vector-products-2.pli
Normal file
48
Task/Vector-products/PL-I/vector-products-2.pli
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
/* This version uses the ability of PL/I to return arrays. */
|
||||
|
||||
/* dot product, cross product, etc. 6 June 2011 */
|
||||
|
||||
test_products: procedure options (main);
|
||||
define structure 1 vector, 2 vec(3) fixed;
|
||||
declare (a, b, c) type(vector);
|
||||
|
||||
a.vec(1) = 3; a.vec(2) = 4; a.vec(3) = 5;
|
||||
b.vec(1) = 4; b.vec(2) = 3; b.vec(3) = 5;
|
||||
c.vec(1) = -5; c.vec(2) = -12; c.vec(3) = -13;
|
||||
|
||||
put skip list ('a . b =', dot_product (a, b) );
|
||||
put skip list ('a x b =', cross_product(a, b).vec);
|
||||
put skip list ('a . (b x c) =', scalar_triple_product(a, b, c) );
|
||||
put skip list ('a x (b x c) =', vector_triple_product(a, b, c).vec);
|
||||
|
||||
|
||||
dot_product: procedure (a, b) returns (fixed);
|
||||
declare (a, b) type(vector);
|
||||
return (sum(a.vec*b.vec));
|
||||
end dot_product;
|
||||
|
||||
cross_product: procedure (a, b) returns (type(vector));
|
||||
declare (a, b) type(vector);
|
||||
declare c type vector;
|
||||
c.vec(1) = a.vec(2)*b.vec(3) - a.vec(3)*b.vec(2);
|
||||
c.vec(2) = a.vec(3)*b.vec(1) - a.vec(1)*b.vec(3);
|
||||
c.vec(3) = a.vec(1)*b.vec(2) - a.vec(2)*b.vec(1);
|
||||
return (c);
|
||||
end cross_product;
|
||||
|
||||
scalar_triple_product: procedure (a, b, c) returns (fixed);
|
||||
declare (a, b, c) type(vector);
|
||||
declare t type (vector);
|
||||
t = cross_product(b, c);
|
||||
return (dot_product(a, t));
|
||||
end scalar_triple_product;
|
||||
|
||||
vector_triple_product: procedure (a, b, c) returns (type(vector));
|
||||
declare (a, b, c) type(vector);
|
||||
declare (t, e) type (vector);
|
||||
t = cross_product(b, c);
|
||||
e = cross_product(a, t);
|
||||
return (e);
|
||||
end vector_triple_product;
|
||||
|
||||
end test_products;
|
||||
48
Task/Vector-products/Pascal/vector-products.pas
Normal file
48
Task/Vector-products/Pascal/vector-products.pas
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
Program VectorProduct (output);
|
||||
|
||||
type
|
||||
Tvector = record
|
||||
x, y, z: double
|
||||
end;
|
||||
|
||||
function dotProduct(a, b: Tvector): double;
|
||||
begin
|
||||
dotProduct := a.x*b.x + a.y*b.y + a.z*b.z;
|
||||
end;
|
||||
|
||||
function crossProduct(a, b: Tvector): Tvector;
|
||||
begin
|
||||
crossProduct.x := a.y*b.z - a.z*b.y;
|
||||
crossProduct.y := a.z*b.x - a.x*b.z;
|
||||
crossProduct.z := a.x*b.y - a.y*b.x;
|
||||
end;
|
||||
|
||||
function scalarTripleProduct(a, b, c: Tvector): double;
|
||||
begin
|
||||
scalarTripleProduct := dotProduct(a, crossProduct(b, c));
|
||||
end;
|
||||
|
||||
function vectorTripleProduct(a, b, c: Tvector): Tvector;
|
||||
begin
|
||||
vectorTripleProduct := crossProduct(a, crossProduct(b, c));
|
||||
end;
|
||||
|
||||
procedure printVector(a: Tvector);
|
||||
begin
|
||||
writeln(a.x:15:8, a.y:15:8, a.z:15:8);
|
||||
end;
|
||||
|
||||
var
|
||||
a: Tvector = (x: 3; y: 4; z: 5);
|
||||
b: Tvector = (x: 4; y: 3; z: 5);
|
||||
c: Tvector = (x:-5; y:-12; z:-13);
|
||||
|
||||
begin
|
||||
write('a: '); printVector(a);
|
||||
write('b: '); printVector(b);
|
||||
write('c: '); printVector(c);
|
||||
writeln('a . b: ', dotProduct(a,b):15:8);
|
||||
write('a x b: '); printVector(crossProduct(a,b));
|
||||
writeln('a . (b x c): ', scalarTripleProduct(a,b,c):15:8);
|
||||
write('a x (b x c): '); printVector(vectorTripleProduct(a,b,c));
|
||||
end.
|
||||
28
Task/Vector-products/Perl/vector-products.pl
Normal file
28
Task/Vector-products/Perl/vector-products.pl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package Vector;
|
||||
use List::Util 'sum';
|
||||
use List::MoreUtils 'pairwise';
|
||||
|
||||
sub new { shift; bless [@_] }
|
||||
|
||||
use overload (
|
||||
'""' => sub { "(@{+shift})" },
|
||||
'&' => sub { sum pairwise { $a * $b } @{+shift}, @{+shift} },
|
||||
'^' => sub {
|
||||
my @a = @{+shift};
|
||||
my @b = @{+shift};
|
||||
bless [ $a[1]*$b[2] - $a[2]*$b[1],
|
||||
$a[2]*$b[0] - $a[0]*$b[2],
|
||||
$a[0]*$b[1] - $a[1]*$b[0] ]
|
||||
},
|
||||
);
|
||||
|
||||
package main;
|
||||
my $a = Vector->new(3, 4, 5);
|
||||
my $b = Vector->new(4, 3, 5);
|
||||
my $c = Vector->new(-5, -12, -13);
|
||||
|
||||
print "a = $a b = $b c = $c\n";
|
||||
print "$a . $b = ", $a & $b, "\n";
|
||||
print "$a x $b = ", $a ^ $b, "\n";
|
||||
print "$a . ($b x $c) = ", $a & ($b ^ $c), "\n";
|
||||
print "$a x ($b x $c) = ", $a ^ ($b ^ $c), "\n";
|
||||
25
Task/Vector-products/Phix/vector-products.phix
Normal file
25
Task/Vector-products/Phix/vector-products.phix
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">dot_product</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">cross_product</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a3</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">a1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">a2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b1</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">scalar_triple_product</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">dot_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cross_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">vector_triple_product</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">cross_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cross_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">a</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">},</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">13</span><span style="color: #0000FF;">}</span>
|
||||
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" a . b = %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">dot_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" a x b = %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">cross_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a . (b x c) = %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">scalar_triple_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a x (b x c) = %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">vector_triple_product</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)})</span>
|
||||
<!--
|
||||
29
Task/Vector-products/Phixmonti/vector-products.phixmonti
Normal file
29
Task/Vector-products/Phixmonti/vector-products.phixmonti
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
include ..\Utilitys.pmt
|
||||
|
||||
( 3 4 5 ) var vectorA
|
||||
( 4 3 5 ) var vectorB
|
||||
( -5 -12 -13 ) var vectorC
|
||||
|
||||
def dotProduct /# x y -- n #/
|
||||
0 >ps
|
||||
len for var i
|
||||
i get rot i get rot * ps> + >ps
|
||||
endfor
|
||||
drop drop
|
||||
ps>
|
||||
enddef
|
||||
|
||||
def crossProduct /# x y -- z #/
|
||||
1 get rot 2 get rot * >ps
|
||||
1 get rot 2 get rot * >ps
|
||||
3 get rot 1 get rot * >ps
|
||||
3 get rot 1 get rot * >ps
|
||||
2 get rot 3 get rot * >ps
|
||||
2 get rot 3 get rot * ps> - ps> ps> - ps> ps> - 3 tolist
|
||||
nip nip
|
||||
enddef
|
||||
|
||||
"Dot Product = " print vectorA vectorB dotProduct ?
|
||||
"Cross Product = " print vectorA vectorB crossProduct ?
|
||||
"Scalar Triple Product = " print vectorB vectorC crossProduct vectorA swap dotProduct ?
|
||||
"Vector Triple Product = " print vectorB vectorC crossProduct vectorA swap crossProduct ?
|
||||
19
Task/Vector-products/Picat/vector-products.picat
Normal file
19
Task/Vector-products/Picat/vector-products.picat
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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([ 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)).
|
||||
14
Task/Vector-products/PicoLisp/vector-products.l
Normal file
14
Task/Vector-products/PicoLisp/vector-products.l
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
(de dotProduct (A B)
|
||||
(sum * A B) )
|
||||
|
||||
(de crossProduct (A B)
|
||||
(list
|
||||
(- (* (cadr A) (caddr B)) (* (caddr A) (cadr B)))
|
||||
(- (* (caddr A) (car B)) (* (car A) (caddr B)))
|
||||
(- (* (car A) (cadr B)) (* (cadr A) (car B))) ) )
|
||||
|
||||
(de scalarTriple (A B C)
|
||||
(dotProduct A (crossProduct B C)) )
|
||||
|
||||
(de vectorTriple (A B C)
|
||||
(crossProduct A (crossProduct B C)) )
|
||||
68
Task/Vector-products/Plain-English/vector-products.plain
Normal file
68
Task/Vector-products/Plain-English/vector-products.plain
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
To run:
|
||||
Start up.
|
||||
Make a vector from 3 and 4 and 5.
|
||||
Make another vector from 4 and 3 and 5.
|
||||
Make a third vector from -5 and -12 and -13.
|
||||
Write "A vector: " then the vector on the console.
|
||||
Write "Another vector: " then the other vector on the console.
|
||||
Write "A third vector: " then the third vector on the console.
|
||||
Write "" on the console.
|
||||
Compute a dot product of the vector and the other vector.
|
||||
Write "Dot product between the vector and the other vector: " then the dot product on the console.
|
||||
Compute a cross product of the vector and the other vector.
|
||||
Write "Cross product between the vector and the other vector: " then the cross product on the console.
|
||||
Compute a scalar triple product of the vector and the other vector and the third vector.
|
||||
Write "Scalar triple product between the vector and the other vector and the third vector: " then the scalar triple product on the console.
|
||||
Compute a vector triple product of the vector and the other vector and the third vector.
|
||||
Write "Vector triple product between the vector and the other vector and the third vector: " then the vector triple product on the console.
|
||||
Wait for the escape key.
|
||||
Shut down.
|
||||
|
||||
A vector has a first number, a second number, and a third number.
|
||||
|
||||
To make a vector from a first number and a second number and a third number:
|
||||
Put the first into the vector's first.
|
||||
Put the second into the vector's second.
|
||||
Put the third into the vector's third.
|
||||
|
||||
To put a vector into another vector:
|
||||
Put the vector's first into the other vector's first.
|
||||
Put the vector's second into the other vector's second.
|
||||
Put the vector's third into the other vector's third.
|
||||
|
||||
To convert a vector into a string:
|
||||
Append "(" then the vector's first then ", " then the vector's second then ", " then the vector's third then ")" to the string.
|
||||
|
||||
A dot product is a number.
|
||||
|
||||
To compute a dot product of a vector and another vector:
|
||||
Put the vector's first times the other vector's first into a first number.
|
||||
Put the vector's second times the other vector's second into a second number.
|
||||
Put the vector's third times the other vector's third into a third number.
|
||||
Put the first plus the second plus the third into the dot product.
|
||||
|
||||
A cross product is a vector.
|
||||
|
||||
To compute a cross product of a vector and another vector:
|
||||
Put the vector's second times the other vector's third into a first number.
|
||||
Put the vector's third times the other vector's second into a second number.
|
||||
Put the vector's third times the other vector's first into a third number.
|
||||
Put the vector's first times the other vector's third into a fourth number.
|
||||
Put the vector's first times the other vector's second into a fifth number.
|
||||
Put the vector's second times the other vector's first into a sixth number.
|
||||
Make a result vector from the first minus the second and the third minus the fourth and the fifth minus the sixth.
|
||||
Put the result into the cross product.
|
||||
|
||||
A scalar triple product is a number.
|
||||
|
||||
To compute a scalar triple product of a vector and another vector and a third vector:
|
||||
Compute a cross product of the other vector and the third vector.
|
||||
Compute a dot product of the vector and the cross product.
|
||||
Put the dot product into the scalar triple product.
|
||||
|
||||
A vector triple product is a vector.
|
||||
|
||||
To compute a vector triple product of a vector and another vector and a third vector:
|
||||
Compute a cross product of the other vector and the third vector.
|
||||
Compute another cross product of the vector and the cross product.
|
||||
Put the other cross product into the vector triple product.
|
||||
27
Task/Vector-products/PowerShell/vector-products.psh
Normal file
27
Task/Vector-products/PowerShell/vector-products.psh
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function dot-product($a,$b) {
|
||||
$a[0]*$b[0] + $a[1]*$b[1] + $a[2]*$b[2]
|
||||
}
|
||||
|
||||
function cross-product($a,$b) {
|
||||
$v1 = $a[1]*$b[2] - $a[2]*$b[1]
|
||||
$v2 = $a[2]*$b[0] - $a[0]*$b[2]
|
||||
$v3 = $a[0]*$b[1] - $a[1]*$b[0]
|
||||
@($v1,$v2,$v3)
|
||||
}
|
||||
|
||||
function scalar-triple-product($a,$b,$c) {
|
||||
dot-product $a (cross-product $b $c)
|
||||
}
|
||||
|
||||
function vector-triple-product($a,$b) {
|
||||
cross-product $a (cross-product $b $c)
|
||||
}
|
||||
|
||||
$a = @(3, 4, 5)
|
||||
$b = @(4, 3, 5)
|
||||
$c = @(-5, -12, -13)
|
||||
|
||||
"a.b = $(dot-product $a $b)"
|
||||
"axb = $(cross-product $a $b)"
|
||||
"a.(bxc) = $(scalar-triple-product $a $b $c)"
|
||||
"ax(bxc) = $(vector-triple-product $a $b $c)"
|
||||
16
Task/Vector-products/Prolog/vector-products.pro
Normal file
16
Task/Vector-products/Prolog/vector-products.pro
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
dot_product([A1, A2, A3], [B1, B2, B3], Ans) :-
|
||||
Ans is A1 * B1 + A2 * B2 + A3 * B3.
|
||||
|
||||
cross_product([A1, A2, A3], [B1, B2, B3], Ans) :-
|
||||
T1 is A2 * B3 - A3 * B2,
|
||||
T2 is A3 * B1 - A1 * B3,
|
||||
T3 is A1 * B2 - A2 * B1,
|
||||
Ans = [T1, T2, T3].
|
||||
|
||||
scala_triple(A, B, C, Ans) :-
|
||||
cross_product(B, C, Temp),
|
||||
dot_product(A, Temp, Ans).
|
||||
|
||||
vector_triple(A, B, C, Ans) :-
|
||||
cross_product(B, C, Temp),
|
||||
cross_product(A, Temp, Ans).
|
||||
50
Task/Vector-products/PureBasic/vector-products.basic
Normal file
50
Task/Vector-products/PureBasic/vector-products.basic
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
Structure vector
|
||||
x.f
|
||||
y.f
|
||||
z.f
|
||||
EndStructure
|
||||
|
||||
;convert vector to a string for display
|
||||
Procedure.s toString(*v.vector)
|
||||
ProcedureReturn "[" + StrF(*v\x, 2) + ", " + StrF(*v\y, 2) + ", " + StrF(*v\z, 2) + "]"
|
||||
EndProcedure
|
||||
|
||||
Procedure.f dotProduct(*a.vector, *b.vector)
|
||||
ProcedureReturn *a\x * *b\x + *a\y * *b\y + *a\z * *b\z
|
||||
EndProcedure
|
||||
|
||||
Procedure crossProduct(*a.vector, *b.vector, *r.vector)
|
||||
*r\x = *a\y * *b\z - *a\z * *b\y
|
||||
*r\y = *a\z * *b\x - *a\x * *b\z
|
||||
*r\z = *a\x * *b\y - *a\y * *b\x
|
||||
EndProcedure
|
||||
|
||||
Procedure.f scalarTriple(*a.vector, *b.vector, *c.vector)
|
||||
Protected r.vector
|
||||
crossProduct(*b, *c, r)
|
||||
ProcedureReturn dotProduct(*a, r)
|
||||
EndProcedure
|
||||
|
||||
Procedure vectorTriple(*a.vector, *b.vector, *c.vector, *r.vector)
|
||||
Protected r.vector
|
||||
crossProduct(*b, *c, r)
|
||||
crossProduct(*a, r, *r)
|
||||
EndProcedure
|
||||
|
||||
If OpenConsole()
|
||||
Define.vector a, b, c, r
|
||||
a\x = 3: a\y = 4: a\z = 5
|
||||
b\x = 4: b\y = 3: b\z = 5
|
||||
c\x = -5: c\y = -12: c\z = -13
|
||||
|
||||
PrintN("a = " + toString(a) + ", b = " + toString(b) + ", c = " + toString(c))
|
||||
PrintN("a . b = " + StrF(dotProduct(a, b), 2))
|
||||
crossProduct(a, b, r)
|
||||
PrintN("a x b = " + toString(r))
|
||||
PrintN("a . b x c = " + StrF(scalarTriple(a, b, c), 2))
|
||||
vectorTriple(a, b, c, r)
|
||||
PrintN("a x b x c = " + toString(r))
|
||||
|
||||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
|
||||
CloseConsole()
|
||||
EndIf
|
||||
27
Task/Vector-products/Python/vector-products.py
Normal file
27
Task/Vector-products/Python/vector-products.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
def crossp(a, b):
|
||||
'''Cross product of two 3D vectors'''
|
||||
assert len(a) == len(b) == 3, 'For 3D vectors only'
|
||||
a1, a2, a3 = a
|
||||
b1, b2, b3 = b
|
||||
return (a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1)
|
||||
|
||||
def dotp(a,b):
|
||||
'''Dot product of two eqi-dimensioned vectors'''
|
||||
assert len(a) == len(b), 'Vector sizes must match'
|
||||
return sum(aterm * bterm for aterm,bterm in zip(a, b))
|
||||
|
||||
def scalartriplep(a, b, c):
|
||||
'''Scalar triple product of three vectors: "a . (b x c)"'''
|
||||
return dotp(a, crossp(b, c))
|
||||
|
||||
def vectortriplep(a, b, c):
|
||||
'''Vector triple product of three vectors: "a x (b x c)"'''
|
||||
return crossp(a, crossp(b, c))
|
||||
|
||||
if __name__ == '__main__':
|
||||
a, b, c = (3, 4, 5), (4, 3, 5), (-5, -12, -13)
|
||||
print("a = %r; b = %r; c = %r" % (a, b, c))
|
||||
print("a . b = %r" % dotp(a,b))
|
||||
print("a x b = %r" % (crossp(a,b),))
|
||||
print("a . (b x c) = %r" % scalartriplep(a, b, c))
|
||||
print("a x (b x c) = %r" % (vectortriplep(a, b, c),))
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue