Vector products: Difference between revisions
→{{header|EasyLang}}
(32 intermediate revisions by 24 users not shown) | |||
Line 46:
* [[Quaternion type]]
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">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)))</syntaxhighlight>
{{out}}
<pre>
a = (3, 4, 5); b = (4, 3, 5); c = (-5, -12, -13)
a . b = 49
a x b = (5, 5, -7)
a . (b x c) = 6
a x (b x c) = (-267, 204, -3)
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">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</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Vector_products.png Screenshot from Atari 8-bit computer]
<pre>
a=(3,4,5)
b=(4,3,5)
c=(-5,-12,-13)
a.b=49
axb=(5,5,-7)
a.(bxc)=6
ax(bxc)=(-267,204,-3)
</pre>
=={{header|Ada}}==
Line 53 ⟶ 147:
vector.adb:
<
procedure Vector is
Line 130 ⟶ 224:
Ada.Text_IO.Put ("A x (B x C) = "); Vector_Put (A * Float_Vector'(B * C));
Ada.Text_IO.New_Line;
end Vector;</
Output:
<pre>A: ( 3.0, 4.0, 5.0)
Line 146 ⟶ 240:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<
FORMAT field fmt = $g(-0)$;
Line 198 ⟶ 292:
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)))
)</
Output:
<pre>
Line 215 ⟶ 309:
=={{header|ALGOL W}}==
<
% define the Vector record type %
record Vector( integer X, Y, Z );
Line 259 ⟶ 353:
write( "a x ( b x c ): " ); writeonVector( vectorTripleProduct( a, b, c ) )
end
end.</
{{out}}
<pre>
Line 270 ⟶ 364:
a x ( b x c ): (-267, 204, -3)
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">dot ← +.×
cross ← 1⌽(⊣×1⌽⊢)-⊢×1⌽⊣</syntaxhighlight>
{{out}}
<syntaxhighlight lang="apl"> 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</syntaxhighlight>
=={{header|AppleScript}}==
<
-- dotProduct :: Num a => [a] -> [a] -> Either String a
Line 527 ⟶ 638:
return lst
end tell
end zipWith</
{{Out}}
<pre>a . b = 49
Line 535 ⟶ 646:
a x d = Dot product not defined for vectors of differing dimension.
a . (b x d) = Cross product is defined only for 3d vectors</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">; 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]</syntaxhighlight>
{{out}}
<pre>a • b = 49
a x b = [5 5 -7]
a • (b x c) = 6
a x (b x c) = [-267 204 -3]</pre>
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
<
for key, val in V
Line 567 ⟶ 720:
VectorTripleProduct(v1, v2, v3) {
return, CrossProduct(v1, CrossProduct(v2, v3))
}</
'''Output:'''
<pre>a = (3, 4, 5)
Line 579 ⟶ 732:
=={{header|AWK}}==
<
BEGIN {
a[1] = 3; a[2]= 4; a[3] = 5;
Line 607 ⟶ 760:
function printVec(C) {
return "[ "C[1]" "C[2]" "C[3]" ]";
}</
Output:
<pre>a = [ 3 4 5 ]
Line 620 ⟶ 773:
=={{header|BASIC256}}==
{{works with|BASIC256 }}
<
a={3,4,5}:b={4,3,5}:c={-5,-12,-13}
Line 654 ⟶ 807:
end subroutine
</
Output:
<pre>
Line 665 ⟶ 818:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<
a() = 3, 4, 5
b() = 4, 3, 5
Line 695 ⟶ 848:
PROCcross(B(),C(),D())
PROCcross(A(),D(),D())
ENDPROC</
Output:
<pre>
Line 703 ⟶ 856:
a x (b x c) = (-267, 204, -3)
</pre>
=={{header|BQN}}==
The cross product function here multiplies each vector pointwise by the other rotated by one. To align this result with the third index (the one not involved), it's rotated once more at the end. The APL solution <code>1⌽(⊣×1⌽⊢)-⊢×1⌽⊣</code> uses the same idea and works in BQN without modification.
<syntaxhighlight lang="bqn">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</syntaxhighlight>
Results:
<syntaxhighlight lang="bqn"> a Dot b
49
a Cross b
⟨ 5 5 ¯7 ⟩
Triple a‿b‿c
6
VTriple a‿b‿c
⟨ ¯267 204 ¯3 ⟩</syntaxhighlight>
=={{header|C}}==
<
typedef struct{
Line 751 ⟶ 928:
return 0;
}</
Output:
<pre>
Line 764 ⟶ 941:
=={{header|C sharp|C#}}==
<
using System.Windows.Media.Media3D;
Line 790 ⟶ 967:
Console.WriteLine(VectorTripleProduct(a, b, c));
}
}</
Output:
<pre>49
Line 798 ⟶ 975:
=={{header|C++}}==
<
template< class T >
Line 851 ⟶ 1,028:
std::cout << "a x b x c : " << a.triplevec( b , c ) << "\n" ;
return 0 ;
}</
Output:<PRE>a . b : 49
a x b : ( 5 , 5 , -7 )
Line 859 ⟶ 1,036:
=={{header|Ceylon}}==
<
alias Vector => Float[3];
Line 886 ⟶ 1,063:
print("``a`` . ``b`` X ``c`` = ``scalarTriple(a, b, c)``");
print("``a`` X ``b`` X ``c`` = ``vectorTriple(a, b, c)``");
}</
{{out}}
<pre>[3.0, 4.0, 5.0] . [4.0, 3.0, 5.0] = 49.0
Line 894 ⟶ 1,071:
=={{header|Clojure}}==
<
(defn dot
Line 918 ⟶ 1,095:
(dot a (cross b c))
(cross a (cross b c)))]
(println prod)))</
Output:<PRE>
49
Line 924 ⟶ 1,101:
6
#:user.Vector{:x -267, :y 204, :z -3}</PRE>
=={{header|CLU}}==
<syntaxhighlight lang="clu">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</syntaxhighlight>
{{out}}
<pre> a = (3, 4, 5)
b = (4, 3, 5)
c = (-5, -12, -13)
a . b = 49
a x b = (5, 5, -7)
a . (b x c) = 6
a x (b x c) = (-267, 204, -3)</pre>
=={{header|Common Lisp}}==
Line 929 ⟶ 1,183:
Using the Common Lisp Object System.
<
((x :type number :initarg :x)
(y :type number :initarg :y)
Line 973 ⟶ 1,227:
(cross-product a b)
(scalar-triple-product a b c)
(vector-triple-product a b c))))</
Output:
CL-USER> (vector-products-example)
Line 983 ⟶ 1,237:
Using vector type
<
(when (and (equal (length a) 3) (equal (length b) 3))
(vector
Line 1,005 ⟶ 1,259:
(scalar-triple a b c)
(vector-triple a b c)))
</syntaxhighlight>
Output:
Line 1,013 ⟶ 1,267:
6
#(-267 204 -3)
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">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);</syntaxhighlight>
{{out}}
<pre> a = (3, 4, 5)
b = (4, 3, 5)
c = (-5, -12, -13)
a . b = 49
a x b = (5, 5, -7)
a . b x c = 6
a x b x c = (-267, 204, -3)</pre>
=={{header|Crystal}}==
<
property x, y, z
Line 1,053 ⟶ 1,382:
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}"</
{{out}}
<pre>a = (3, 4, 5)
Line 1,064 ⟶ 1,393:
=={{header|D}}==
<
struct V3 {
Line 1,107 ⟶ 1,436:
writeln("a . (b x c) = ", scalarTriple(a, b, c));
writeln("a x (b x c) = ", vectorTriple(a, b, c));
}</
{{out}}
<pre>a = [3, 4, 5]
Line 1,116 ⟶ 1,445:
a . (b x c) = 6
a x (b x c) = [-267, 204, -3]</pre>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Vector_products#Pascal Pascal].
=={{header|EasyLang}}==
<syntaxhighlight>
func vdot a[] b[] .
for i to len a[]
r += a[i] * b[i]
.
return r
.
func[] vcross a[] b[] .
r[] &= a[2] * b[3] - a[3] * b[2]
r[] &= a[3] * b[1] - a[1] * b[3]
r[] &= a[1] * b[2] - a[2] * b[1]
return r[]
.
a[] = [ 3 4 5 ]
b[] = [ 4 3 5 ]
c[] = [ -5 -12 -13 ]
#
print vdot a[] b[]
print vcross a[] b[]
print vdot a[] vcross b[] c[]
print vcross a[] vcross b[] c[]
</syntaxhighlight>
{{out}}
<pre>
49
[ 5 5 -7 ]
6
[ -267 204 -3 ]
</pre>
=={{header|EchoLisp}}==
The '''math''' library includes the '''dot-product''' and '''cross-product''' functions. They work on complex or real vectors.
<
(lib 'math)
Line 1,140 ⟶ 1,504:
(vector-triple-product a b c)
→ #( -267 204 -3)
</syntaxhighlight>
=={{header|Elixir}}==
<
def dot_product({a1,a2,a3}, {b1,b2,b3}), do: a1*b1 + a2*b2 + a3*b3
Line 1,163 ⟶ 1,527:
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)}"</
{{out}}
Line 1,177 ⟶ 1,541:
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">
-module(vector).
-export([main/0]).
Line 1,199 ⟶ 1,563:
dot_product(C,vector_product(A,B)),
io:fwrite("~p,~p,~p~n",vector_product(C,vector_product(A,B))).
</syntaxhighlight>
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROGRAM VECTORPRODUCT
Line 1,260 ⟶ 1,624:
PRINT("Ax(BxC)=";) PRINTVECTOR(FF.)
END PROGRAM
</syntaxhighlight>
=={{header|Euphoria}}==
<
function dot_product(sequence a, sequence b)
Line 1,298 ⟶ 1,662:
? scalar_triple( a, b, c )
puts(1,"a x (b x c) = ")
? vector_triple( a, b, c )</
Output:
<pre>a = {3,4,5}
Line 1,310 ⟶ 1,674:
=={{header|F#|F sharp}}==
<
ax * bx + ay * by + az * bz
Line 1,331 ⟶ 1,695:
printfn "%A" (scalTrip a b c)
printfn "%A" (vecTrip a b c)
0 // return an integer exit code</
{{out}}
<pre>49.0
Line 1,340 ⟶ 1,704:
=={{header|Factor}}==
Factor has a fantastic <tt>math.vectors</tt> vocabulary, but in the spirit of the task, it is not used.
<
: dot-product ( a b -- dp ) [ * ] 2map sum ;
Line 1,369 ⟶ 1,733:
"a . (b x c): " write a b c scalar-triple-product .
"a x (b x c): " write a b c vector-triple-product .
]</
{{out}}
<pre>
Line 1,383 ⟶ 1,747:
=={{header|Fantom}}==
<
{
Int dot_product (Int[] a, Int[] b)
Line 1,416 ⟶ 1,780:
echo ("a x (b x c) = [" + vector_triple_product(a, b, c).join (", ") + "]")
}
}</
Output:
<pre>
Line 1,426 ⟶ 1,790:
=={{header|Forth}}==
{{works with|Forth|1994 ANSI with a separate floating point stack.}}<
: 3f! ( &v - ) ( f: x y z - ) dup float+ dup float+ f! f! f! ;
Line 1,465 ⟶ 1,829:
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 </
{{out}}
<pre>
Line 1,473 ⟶ 1,837:
a x [b x c] = -267.000 204.000 -3.00000
</pre>
{{libheader|Forth Scientific Library}}<
S" fsl-util.fs" REQUIRED
: 3f! 3 SWAP }fput ;
Line 1,486 ⟶ 1,850:
: .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</
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
Specialized for 3-dimensional vectors.
<
real, dimension(3) :: a, b, c
Line 1,529 ⟶ 1,893:
end function v3_product
end program VectorProducts</
Output
<pre> 49.0000
Line 1,538 ⟶ 1,902:
=={{header|FreeBASIC}}==
<
Type V3
As double x,y,z
Line 1,569 ⟶ 1,933:
Show(a . b X c,a dot b cross c)
Show(a X (b X c),a cross (b cross c))
sleep</
{{out}}
<pre>a (3,4,5)
Line 1,581 ⟶ 1,945:
=={{header|FunL}}==
<
B = (4, 3, 5)
C = (-5, -12, -13)
Line 1,593 ⟶ 1,957:
println( "A\u00d7B = ${cross(A, B)}" )
println( "A\u00b7(B\u00d7C) = ${scalarTriple(A, B, C)}" )
println( "A\u00d7(B\u00d7C) = ${vectorTriple(A, B, C)}" )</
{{out}}
Line 1,605 ⟶ 1,969:
=={{header|GAP}}==
<
return u*v;
end;
Line 1,642 ⟶ 2,006:
VectorTripleProduct(a, b, c);
# [ -267, 204, -3 ]</
=={{header|GLSL}}==
{{trans|C}}
<
vec3 a = vec3(3, 4, 5),b = vec3(4, 3, 5),c = vec3(-5, -12, -13);
Line 1,670 ⟶ 2,034:
return crossProduct(a,crossProduct(b,c));
}
</syntaxhighlight>
=={{header|Go}}==
<
import "fmt"
Line 1,708 ⟶ 2,072:
fmt.Println(s3(a, b, c))
fmt.Println(v3(a, b, c))
}</
Output:
<pre>
Line 1,719 ⟶ 2,083:
=={{header|Groovy}}==
Dot Product Solution:
<
assert x && y && x.size() == y.size()
[x, y].transpose().collect(binaryOp)
Line 1,729 ⟶ 2,093:
assert x && y && x.size() == y.size()
pwMult(x, y).sum()
}</
Cross Product Solution, using scalar operations:
<
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]]
}</
Cross Product Solution, using "vector" operations:
<
assert it && it.size() > 2
[it[-1]] + it[0..-2]
Line 1,751 ⟶ 2,115:
assert x && y && x.size() == 3 && y.size() == 3
pwSubtr(pwMult(rotL(x), rotR(y)), pwMult(rotL(y), rotR(x)))
}</
Test program (including triple products):
<
def scalarTripleProduct = { x, y, z ->
Line 1,775 ⟶ 2,139:
test(crossProductS)
test(crossProductV)</
Output:
<pre> a . b = 49
Line 1,788 ⟶ 2,152:
=={{header|Haskell}}==
<
type Vector a = [a]
Line 1,840 ⟶ 2,204:
, "a x b x c = " <> show (vectorTriple a b c)
, "a . d = " <> show (dot a d)
]</
Output:<pre>a . b = 49
a x b = [5,5,-7]
Line 1,851 ⟶ 2,215:
Or using '''Either''' and '''(>>=)''', rather than '''error''', to pass on intelligible messages:
<
:: Num a
=> [a] -> [a] -> Either String a
Line 1,906 ⟶ 2,270:
:: Show a
=> Either String a -> String
sh = either (" => " ++) ((" = " ++) . show)</
{{Out}}
<pre>a . b = 49
Line 1,916 ⟶ 2,280:
=={{header|Icon}} and {{header|Unicon}}==
<
record Vector3D(x, y, z)
Line 1,957 ⟶ 2,321:
writes ("Ax(BxC) : " || toString(a) || "x(" || toString(b) || "x" || toString(c) || ") = ")
write (toString(vectorTriple (a, b, c)))
end</
Output:
<pre>
Line 1,969 ⟶ 2,333:
Perhaps the most straightforward definition for cross product in J uses rotate multiply and subtract:
<
or
<syntaxhighlight lang="j">cross=: {{ ((1|.x)*2|.y) - (2|.x)*1|.y }}</syntaxhighlight>
However, there are other valid approaches. For example, a "generalized approach" based on [[j:Essays/Complete Tensor]]:
<
ip=: +/ .* NB. inner product
cross=: ] ip CT@#@[ ip [</
Note that there are a variety of other generalizations have cross products as a part of what they do. (For example, we could implement cross product using complex numbers in a Cayley Dickson implementation of quaternion product.)
An alternative definition for cross (based on finding the determinant of a 3 by 3 matrix where one row is unit vectors) could be:
<
or
<syntaxhighlight lang="j">cross=: {{ >-L:0/ .(*L:0) (<"1=i.3), x,:&:(<"0) y}}</syntaxhighlight>
With an implementation of cross product and inner product, the rest of the task becomes trivial:
<
b=: 4 3 5
c=: -5 12 13
Line 1,994 ⟶ 2,363:
crossP=: A cross B
scTriP=: A ip B cross C
veTriP=: A cross B cross C</
Required example:
<
49
crossP a;b
Line 2,003 ⟶ 2,372:
6
veTriP a;b;c
_267 204 _3</
=={{header|Java}}==
{{works with|Java|1.5+}}
All operations which return vectors give vectors containing <code>Double</code>s.
<
public static class Vector3D<T extends Number>{
private T a, b, c;
Line 2,055 ⟶ 2,424:
System.out.println(a.vecTrip(b, c));
}
}</
Output:
<pre>49.0
Line 2,063 ⟶ 2,432:
{{works with|Java|1.8+}}
This solution uses Java SE new Stream API
<
import java.util.stream.IntStream;
Line 2,124 ⟶ 2,493:
}
}</
result is the same as above , fortunately
<pre>dot product =:49
Line 2,135 ⟶ 2,504:
The <code>dotProduct()</code> function is generic and will create a dot product of any set of vectors provided they are all the same dimension.
The <code>crossProduct()</code> function expects two 3D vectors.
<
var len = arguments[0] && arguments[0].length;
var argsLen = arguments.length;
Line 2,204 ⟶ 2,573:
'A x (B x C): ' + vectorTripleProduct(a, b, c)
);
}());</
{{Out}}
<pre>A . B: 49
Line 2,212 ⟶ 2,581:
===ES6===
<
'use strict';
Line 2,351 ⟶ 2,720:
// MAIN ---
return main();
})();</
{{Out}}
<pre>a . b = 49
Line 2,361 ⟶ 2,730:
=={{header|jq}}==
The <code>dot_product()</code> function is generic and will create a dot product of any pair of vectors provided they are both the same dimension. The other functions expect 3D vectors.<
reduce range(0;a|length) as $i (0; . + (a[$i] * b[$i]) );
Line 2,381 ⟶ 2,750:
"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 (", ") )]" ;</
Output:
<
"a x b = [5, 5, -7]"
"a . (b x c) = 6 )"
"a x (b x c) = [-267, 204, -3]"</
=={{header|Julia}}==
Julia provides dot and cross products with LinearAlgebra. It's easy enough to use these to construct the triple products.
<
const a = [3, 4, 5]
Line 2,408 ⟶ 2,769:
println("\nVector Products:")
@show
@show
@show
@show
{{out}}
<pre>
Test Vectors:
a = [3, 4, 5]
b = [4, 3, 5]
Line 2,420 ⟶ 2,782:
Vector Products:
</pre>
=={{header|Kotlin}}==
<
class Vector3D(val x: Double, val y: Double, val z: Double) {
Line 2,453 ⟶ 2,816:
println("a . b x c = ${a.scalarTriple(b, c)}")
println("a x b x c = ${a.vectorTriple(b, c)}")
}</
{{out}}
Line 2,466 ⟶ 2,829:
a x b x c = (-267.0, 204.0, -3.0)
</pre>
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/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[@]} )"
</syntaxhighlight>
{{out}}<pre>
The dot product A • B = 49
The cross product A x B = ( 5 5 -7 )
The scalar triple product A • (B x C) = 6
The vector triple product A x (B x C) = ( -267 204 -3 )</pre>
=={{header|Lambdatalk}}==
<
{def dotProduct
{lambda {:a :b}
Line 2,494 ⟶ 2,950:
A.(BxC) : {dotProduct {A} {crossProduct {B} {C}}} -> 6
Ax(BxC) : {crossProduct {A} {crossProduct {B} {C}}} -> [-267,204,-3]
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<
print "Dot product of 3,4,5 and 4,3,5 is "
Line 2,541 ⟶ 2,997:
VectorTripleProduct$ =CrossProduct$( i$, CrossProduct$( j$, k$))
end function
END SUB</
=={{header|Lingo}}==
Lingo has a built-in vector data type that supports calculation of both dot and cross products:
<
b = vector(4,5,6)
Line 2,555 ⟶ 3,011:
put a.cross(b)
-- vector( -3.0000, 6.0000, -3.0000 )</
=={{header|Lua}}==
<
function Vector.new( _x, _y, _z )
return { x=_x, y=_y, z=_z }
Line 2,594 ⟶ 3,050:
r = Vector.vector_triple( A, B, C )
print( r.x, r.y, r.z )</
<pre>49
5 5 -7
Line 2,601 ⟶ 3,057:
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module checkit {
class Vector {
Line 2,671 ⟶ 3,127:
}
Checkit
</syntaxhighlight>
{{out}}
<pre >
Line 2,685 ⟶ 3,141:
=={{header|Maple}}==
<
A := Vector([3,4,5]):
B := Vector([4,3,5]):
Line 2,696 ⟶ 3,152:
6
>>>CrossProduct(A,CrossProduct(B,C));
Vector([-267, 204, -3])</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
b={4,3,5};
c={-5,-12,-13};
Line 2,705 ⟶ 3,161:
Cross[a,b]
a.Cross[b,c]
Cross[a,Cross[b,c]]</
{{out}}
<pre>49
{5,5,-7}
Line 2,714 ⟶ 3,170:
=={{header|MATLAB}} / {{header|Octave}}==
Matlab / Octave use double precesion numbers per default, and pi is a builtin constant value. Arbitrary precision is only implemented in some additional toolboxes (e.g. symbolic toolbox).
<
dot(a,b)
% Create a function to compute the cross product of two vectors.
Line 2,729 ⟶ 3,185:
dot(a,cross(b,c))
% Compute and display: a x b x c, the vector triple product.
cross(a,cross(b,c))</
Code for testing:
Line 2,761 ⟶ 3,217:
=={{header|Mercury}}==
<syntaxhighlight lang="text">:- module vector_product.
:- interface.
Line 2,802 ⟶ 3,258:
to_string(vector3d(X, Y, Z)) =
string.format("(%d, %d, %d)", [i(X), i(Y), i(Z)]).</
=={{header|MiniScript}}==
<
vectorB = [4, 3, 5]
vectorC = [-5, -12, -13]
Line 2,821 ⟶ 3,277:
print "Scalar Triple Product = " + dotProduct(vectorA, crossProduct(vectorB,vectorC))
print "Vector Triple Product = " + crossProduct(vectorA, crossProduct(vectorB,vectorC))
</syntaxhighlight>
{{out}}
<pre>
Line 2,831 ⟶ 3,287:
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">ПП 54 С/П ПП 66 С/П
ИП0 ИП3 ИП6 П3 -> П0 -> П6
ИП1 ИП4 ИП7 П4 -> П1 -> П7
Line 2,842 ⟶ 3,298:
ИП1 ИП5 * ИП2 ИП4 * - П9
ИП2 ИП3 * ИП0 ИП5 * - ПA
ИП0 ИП4 * ИП1 ИП3 * - ПB В/О</
''Instruction'': Р0 - a<sub>1</sub>, Р1 - a<sub>2</sub>, Р2 - a<sub>3</sub>, Р3 - b<sub>1</sub>, Р4 - b<sub>2</sub>, Р5 - b<sub>3</sub>, Р6 - c<sub>1</sub>, Р7 - c<sub>2</sub>, Р8 - c<sub>3</sub>; В/О С/П.
=={{header|Modula-2}}==
<
FROM RealStr IMPORT RealToStr;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 2,929 ⟶ 3,385:
ReadChar
END VectorProducts.</
=={{header|Nemerle}}==
<
module VectorProducts3d
Line 2,967 ⟶ 3,423:
WriteLine(VectorTriple(a, b, c));
}
}</
Outputs
<pre>49
Line 2,975 ⟶ 3,431:
=={{header|Never}}==
<
prints("[" + a[0] + ", " + a[1] + ", " + a[2] + "]\n");
0
Line 3,010 ⟶ 3,466:
0
}
</syntaxhighlight>
Output:
<pre>
Line 3,023 ⟶ 3,479:
=={{header|Nim}}==
<
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
for i in a.low..a.high:
result += a[i] * b[i]
proc
proc
let
Line 3,052 ⟶ 3,505:
b = [4.0, 3.0, 5.0]
c = [-5.0, -12.0, -13.0]
echo &"a
echo &"a .
echo &"a
echo &"a ⨯ (b ⨯ c) = {vectorTriple(a, b, c)}"</syntaxhighlight>
{{out}}
<pre>a
a . b = 49.0
a . (b ⨯ c) = 6.0
a ⨯ (b ⨯ c) = (-267.0, 204.0, -3.0)</pre>
=={{header|Objeck}}==
<
class VectorProduct {
function : Main(args : String[]) ~ Nil {
Line 3,126 ⟶ 3,581:
}
}
</syntaxhighlight>
Output:
Line 3,135 ⟶ 3,590:
=={{header|OCaml}}==
<
let b = (4.0, 3.0, 5.0)
let c = (-5.0, -12.0, -13.0)
Line 3,164 ⟶ 3,619:
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));
;;</
outputs:
Line 3,178 ⟶ 3,633:
=={{header|Octave}}==
Octave handles naturally vectors / matrices.
<
b = [4, 3, 5];
c = [-5, -12, -13];
Line 3,202 ⟶ 3,657:
% -267 204 -3
v3prod(a, b, c)</
=={{header|ooRexx}}==
<syntaxhighlight lang="oorexx">
a = .vector~new(3, 4, 5);
b = .vector~new(4, 3, 5);
Line 3,255 ⟶ 3,710:
expose x y z
return "<"||x", "y", "z">"
</syntaxhighlight>
Output:
<pre>
Line 3,265 ⟶ 3,720:
=={{header|PARI/GP}}==
<
sum(i=1,#u,u[i]*v[i])
};
Line 3,282 ⟶ 3,737:
cross(a,b)
striple(a,b,c)
vtriple(a,b,c)</
Output:
<pre>49
Line 3,290 ⟶ 3,745:
=={{header|Pascal}}==
<
type
Line 3,337 ⟶ 3,792:
writeln('a . (b x c): ', scalarTripleProduct(a,b,c):15:8);
write('a x (b x c): '); printVector(vectorTripleProduct(a,b,c));
end.</
Output:
<pre>a: 3.00000000 4.00000000 5.00000000
Line 3,349 ⟶ 3,804:
=={{header|Perl}}==
<
use List::Util 'sum';
use List::MoreUtils 'pairwise';
Line 3,376 ⟶ 3,831:
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";</
Output: <pre>a = (3 4 5) b = (4 3 5) c = (-5 -12 -13)
Line 3,385 ⟶ 3,840:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--<syntaxhighlight lang="phix">-->
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,417 ⟶ 3,875:
=={{header|Phixmonti}}==
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt
( 3 4 5 ) var vectorA
Line 3,434 ⟶ 3,891:
def crossProduct /# x y -- z #/
1 get rot 2 get rot * >ps
1
3 get rot 1 get rot * >ps
3 get rot 1 get rot * >ps
2 get rot 3
2 get rot 3 get rot * ps>
nip
enddef
Line 3,456 ⟶ 3,903:
"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 ?</
{{out}}
<pre>Dot Product = 49
Line 3,466 ⟶ 3,913:
=={{header|PHP}}==
<
class Vector
Line 3,570 ⟶ 4,017:
new Program();
?>
</syntaxhighlight>
Output:
Line 3,584 ⟶ 4,031:
A × (B × C) =(-267.00, 204.00, -3.00)
</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">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)).</syntaxhighlight>
{{out}}
<pre>a = [3,4,5]
b = [4,3,5]
c = [-5,-12,-13]
A . B = 49
A x B = [5,5,-7]
A . (B x C) = 6
A X (B X C) = [-267,204,-3]</pre>
=={{header|PicoLisp}}==
<
(sum * A B) )
Line 3,599 ⟶ 4,077:
(de vectorTriple (A B C)
(crossProduct A (crossProduct B C)) )</
Test:
<pre>(setq
Line 3,619 ⟶ 4,097:
=={{header|PL/I}}==
<
test_products: procedure options (main);
Line 3,660 ⟶ 4,138:
end vector_triple_product;
end test_products;</
Results:
<pre>
Line 3,669 ⟶ 4,147:
</pre>
<
/* dot product, cross product, etc. 6 June 2011 */
Line 3,716 ⟶ 4,194:
end vector_triple_product;
end test_products;</
The output is:
<pre>
Line 3,723 ⟶ 4,201:
a . (b x c) = 6
a x (b x c) = -267 204 -3
</pre>
=={{header|Plain English}}==
<syntaxhighlight lang="plain english">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.</syntaxhighlight>
{{out}}
<pre>
A vector: (3, 4, 5)
Another vector: (4, 3, 5)
A third vector: (-5, -12, -13)
Dot product between the vector and the other vector: 49
Cross product between the vector and the other vector: (5, 5, -7)
Scalar triple product between the vector and the other vector and the third vector: 6
Vector triple product between the vector and the other vector and the third vector: (-267, 204, -3)
</pre>
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function dot-product($a,$b) {
$a[0]*$b[0] + $a[1]*$b[1] + $a[2]*$b[2]
Line 3,754 ⟶ 4,313:
"a.(bxc) = $(scalar-triple-product $a $b $c)"
"ax(bxc) = $(vector-triple-product $a $b $c)"
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 3,765 ⟶ 4,324:
=={{header|Prolog}}==
Works with SWI-Prolog.
<
dot_product([A1, A2, A3], [B1, B2, B3], Ans) :-
Ans is A1 * B1 + A2 * B2 + A3 * B3.
Line 3,782 ⟶ 4,341:
cross_product(B, C, Temp),
cross_product(A, Temp, Ans).
</syntaxhighlight>
Output:
<pre>
Line 3,799 ⟶ 4,358:
=={{header|PureBasic}}==
<
x.f
y.f
Line 3,848 ⟶ 4,407:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</
Sample output:
<pre>a = [3.00, 4.00, 5.00], b = [4.00, 3.00, 5.00], c = [-5.00, -12.00, -13.00]
Line 3,858 ⟶ 4,417:
=={{header|Python}}==
The solution is in the form of an [[Executable library]].
<
'''Cross product of two 3D vectors'''
assert len(a) == len(b) == 3, 'For 3D vectors only'
Line 3,884 ⟶ 4,443:
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),))</
{{out}}
Line 3,895 ⟶ 4,454:
;Note:
The popular [http://numpy.scipy.org/ numpy] package has functions for dot and cross products.
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ 0 unrot witheach
[ over i^ peek *
rot + swap ]
drop ] is dotproduct ( [ [ --> n )
[ join
dup 1 peek over 5 peek *
swap
dup 2 peek over 4 peek *
swap dip -
dup 2 peek over 3 peek *
swap
dup 0 peek over 5 peek *
swap dip -
dup 0 peek over 4 peek *
swap
dup 1 peek swap 3 peek *
- join join ] is crossproduct ( [ [ --> [ )
[ crossproduct dotproduct ] is scalartriple ( [ [ [ --> n )
[ crossproduct crossproduct ] is vectortriple ( [ [ [ --> [ )
[ ' [ 3 4 5 ] ] is a ( --> [ )
[ ' [ 4 3 5 ] ] is b ( --> [ )
[ ' [ -5 -12 -13 ] ] is c ( --> [ )
a b dotproduct echo cr
a b crossproduct echo cr
a b c scalartriple echo cr
a b c vectortriple echo cr</syntaxhighlight>
{{out}}
<pre>49
[ 5 5 -7 ]
6
[ -267 204 -3 ]
</pre>
=={{header|R}}==
<
# Vector products
# R implementation
Line 3,953 ⟶ 4,554:
cat("a x b =", crossp(a, b))
cat("a . (b x c) =", scalartriplep(a, b, c))
cat("a x (b x c) =", vectortriplep(a, b, c))</
{{out}}
Line 3,965 ⟶ 4,566:
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
Line 3,996 ⟶ 4,597:
(printf "A . B x C = ~s\n" (scalar-triple-product A B C))
(printf "A x B x C = ~s\n" (vector-triple-product A B C))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2015-11-24}}
<syntaxhighlight lang="raku"
sub infix:<⨯>([$a1, $a2, $a3], [$b1, $b2, $b3]) {
Line 4,020 ⟶ 4,621:
say "a ⨯ b = <{ @a ⨯ @b }>";
say "a ⋅ (b ⨯ c) = { scalar-triple-product(@a, @b, @c) }";
say "a ⨯ (b ⨯ c) = <{ vector-triple-product(@a, @b, @c) }>";</
{{out}}
<pre>("a" => ["3", "4", "5"], "b" => ["4", "3", "5"], "c" => ["-5", "-12", "-13"])
Line 4,029 ⟶ 4,630:
=={{header|REXX}}==
<
a= 3 4 5
b= 4 3 5 /*(positive numbers don't need quotes.)*/
Line 4,036 ⟶ 4,637:
call tellV 'vector B =', b /* " " B " " " */
call tellV 'vector C =', c /* " " C " " " */
say
call tellV ' dot product [A∙B] =', dot(a, b)
call tellV 'cross product [AxB] =', cross(a, b)
call tellV 'scalar triple product [A∙(BxC)] =', dot(a, cross(b, c) )
call tellV 'vector triple product [Ax(BxC)] =', cross(a, cross(b, c) )
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
cross: procedure; arg $1 $2 $3,@1 @2 @3;
dot: procedure; arg $1 $2 $3,@1 @2 @3;
/*──────────────────────────────────────────────────────────────────────────────────────*/
tellV: procedure; parse arg name,x y z;
{{out|output|text= when using the default internal inputs:}}
<pre>
vector A = 3 4 5
Line 4,063 ⟶ 4,662:
=={{header|Ring}}==
<
# Project : Vector products
Line 4,096 ⟶ 4,695:
cross(a,d,e)
see "a x (b x c) = (" + e[1] + ", " +e[2] + ", " + e[3] + ")"
</syntaxhighlight>
Output:
<pre>
Line 4,103 ⟶ 4,702:
a . (b x c) = 6
a x (b x c) = (-267, 204, -3)
</pre>
=={{header|RPL}}==
Dot and cross products are built-in functions in RPL.
{{works with|Halcyon Calc|4.2.7}}
≪ → a b c
≪ a b DOT
a b CROSS
a b c CROSS DOT
a b c CROSS CROSS
≫ ≫
‘VPROD’ STO
[3 4 5] [4 3 5] [-5 -12 -13] VPROD
{{out}}
<pre>
4: 49
3: [ 5 5 -7 ]
2: 6
1: [ -267 204 -3 ]
</pre>
=={{header|Ruby}}==
Dot product is also known as ''inner product''. The standard library already defines Vector#inner_product and Vector# cross_product, so this program only defines the other two methods.
<
class Vector
Line 4,126 ⟶ 4,745:
puts "a cross b = #{a.cross_product b}"
puts "a dot (b cross c) = #{a.scalar_triple_product b, c}"
puts "a cross (b cross c) = #{a.vector_triple_product b, c}"</
Output: <pre>a dot b = 49
a cross b = Vector[5, 5, -7]
Line 4,133 ⟶ 4,752:
=={{header|Rust}}==
<
struct Vector {
x: f64,
Line 4,177 ⟶ 4,796:
println!("a . (b x c) = {}", a.scalar_triple_product(&b, &c));
println!("a x (b x c) = {:?}", a.vector_triple_product(&b, &c));
}</
Output:<pre>a . b = 49
Line 4,185 ⟶ 4,804:
=={{header|Scala}}==
<
def dot(v:Vector3D):Double=x*v.x + y*v.y + z*v.z;
def cross(v:Vector3D)=Vector3D(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x)
Line 4,203 ⟶ 4,822:
println("a x (b x c) : " + (a vectorTriple(b, c)))
}
}</
{{out}}
<pre> a . b : 49.0
Line 4,214 ⟶ 4,833:
{{works with|Gauche}}
Using modified dot-product function from the [[Dot product]] task.
<
(apply + (map * (vector->list A) (vector->list B))))
Line 4,248 ⟶ 4,867:
(display "A x B = ")(display (cross-product A B))(newline)
(display "A . B x C = ")(display (scalar-triple-product A B C))(newline)
(display "A x B x C = ") (display (vector-triple-product A B C))(newline)</
Output:<pre>A = #(3 4 5)
B = #(4 3 5)
Line 4,261 ⟶ 4,880:
The program below uses Seed7s capaibility to define operator symbols.
The operators ''dot'' and ''X'' are defined with with priority 6 and assiciativity left-to-right.
<
include "float.s7i";
Line 4,311 ⟶ 4,930:
writeln("a .(b x c) = " <& scalarTriple(a, b, c));
writeln("a x(b x c) = " <& vectorTriple(a, b, c));
end func;</
{{output}}
Line 4,323 ⟶ 4,942:
=={{header|Sidef}}==
<
method ∙(vec) {
[self{:x,:y,:z}] »*« [vec{:x,:y,:z}] «+»
Line 4,347 ⟶ 4,966:
say "a ⨉ b = #{a ⨉ b}"
say "a ∙ (b ⨉ c) = #{a ∙ (b ⨉ c)}"
say "a ⨉ (b ⨉ c) = #{a ⨉ (b ⨉ c)}"</
{{out}}
<pre>a=(3, 4, 5); b=(4, 3, 5); c=(-5, -12, -13);
Line 4,356 ⟶ 4,975:
=={{header|Simula}}==
{{Trans|C}}
<
CLASS VECTOR(I,J,K); REAL I,J,K;;
Line 4,406 ⟶ 5,025:
OUTIMAGE;
END;
END;</
{{out}}
<pre>A = (3.000000, 4.000000, 5.000000)
Line 4,418 ⟶ 5,037:
=={{header|Stata}}==
<
real scalar sprod(real colvector u, real colvector v) {
return(u[1]*v[1] + u[2]*v[2] + u[3]*v[3])
Line 4,460 ⟶ 5,079:
3 | -3 |
+--------+
end</
=={{header|Swift}}==
<
infix operator • : MultiplicationPrecedence
Line 4,502 ⟶ 5,121:
print("a × b = \(a × b)")
print("a • (b × c) = \(a • (b × c))")
print("a × (b × c) = \(a × (b × c))")</
{{out}}
Line 4,516 ⟶ 5,135:
=={{header|Tcl}}==
<
lassign $A a1 a2 a3
lassign $B b1 b2 b3
Line 4,533 ⟶ 5,152:
proc vectorTriple {A B C} {
cross $A [cross $B $C]
}</
Demonstrating:
<
set b {4 3 5}
set c {-5 -12 -13}
Line 4,541 ⟶ 5,160:
puts "a x b = [cross $a $b]"
puts "a • b x c = [scalarTriple $a $b $c]"
puts "a x b x c = [vectorTriple $a $b $c]"</
Output:<pre>a • b = 49
a x b = 5 5 -7
Line 4,551 ⟶ 5,170:
{{Trans|BBC BASIC}}
Since uBasic/4tH has only one single array, we use its variables to hold the offsets of the vectors. A similar problem arises when local vectors are required.
<syntaxhighlight lang="text">a = 0 ' use variables for vector addresses
b = a + 3
c = b + 3
Line 4,594 ⟶ 5,213:
Proc _Cross (b@, c@, e@)
Proc _Cross (a@, e@, d@)
Return</
{{Out}}
<pre>a . b = 49
Line 4,604 ⟶ 5,223:
=={{header|VBA}}==
{{trans|Phix}}<
Function dot_product(a As Variant, b As Variant) As Variant
dot_product = WorksheetFunction.SumProduct(a, b)
Line 4,629 ⟶ 5,248:
Debug.Print "a . (b x c) = "; scalar_triple_product(a, b, c)
Debug.Print "a x (b x c) = "; "("; Join(vector_triple_product(a, b, c), ", "); ")"
End Sub</
<pre> a . b = 49
a x b = (5, 5, -7)
Line 4,637 ⟶ 5,256:
=={{header|Visual Basic .NET}}==
Class: Vector3D
<
Private _x, _y, _z As Double
Line 4,694 ⟶ 5,313:
Return String.Format("({0}, {1}, {2})", _x, _y, _z)
End Function
End Class</
Module: Module1
<
Sub Main()
Line 4,714 ⟶ 5,333:
End Sub
End Module</
Output:
<pre>v1: (3, 4, 5)
Line 4,724 ⟶ 5,343:
v1 . (v2 x v3) = 6
v1 x (v2 x v3) = (-267, 204, -3)</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">struct Vector {
x f64
y f64
z f64
}
const (
a = Vector{3, 4, 5}
b = Vector{4, 3, 5}
c = Vector{-5, -12, -13}
)
fn dot(a Vector, b Vector) f64 {
return a.x*b.x + a.y*b.y + a.z*b.z
}
fn cross(a Vector, 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}
}
fn s3(a Vector, b Vector, c Vector) f64 {
return dot(a, cross(b, c))
}
fn v3(a Vector, b Vector, c Vector) Vector {
return cross(a, cross(b, c))
}
fn main() {
println(dot(a, b))
println(cross(a, b))
println(s3(a, b, c))
println(v3(a, b, c))
}</syntaxhighlight>
{{out}}
<pre>
49.
Vector{
x: 5
y: 5
z: -7
}
6.
Vector{
x: -267
y: 204
z: -3
}
</pre>
=={{header|Wortel}}==
<
dot &[a b] @sum @mapm ^* [a b]
cross &[a b] [[
Line 4,746 ⟶ 5,418:
@!vectorTripleProduct [a b c]
]]
}</
Returns:
<pre>[49 [5 5 -7] 6 [-267 204 -3]]</pre>
Line 4,752 ⟶ 5,424:
=={{header|Wren}}==
{{trans|Kotlin}}
<
construct new(x, y, z) {
if (x.type != Num || y.type != Num || z.type != Num) Fiber.abort("Arguments must be numbers.")
Line 4,797 ⟶ 5,469:
System.print("a x b = %(a.cross(b))")
System.print("a . b x c = %(a.scalarTriple(b, c))")
System.print("a x b x c = %(a.vectorTriple(b, c))")</
{{out}}
Line 4,809 ⟶ 5,481:
a . b x c = 6
a x b x c = [-267, 204, -3]
</pre>
{{libheader|Wren-vector}}
Alternatively, using the above module to produce exactly the same output as before:
<syntaxhighlight lang="wren">import "./vector" for Vector3
var a = Vector3.new(3, 4, 5)
var b = Vector3.new(4, 3, 5)
var c = Vector3.new(-5, -12, -13)
System.print("a = %(a)")
System.print("b = %(b)")
System.print("c = %(c)")
System.print()
System.print("a . b = %(a.dot(b))")
System.print("a x b = %(a.cross(b))")
System.print("a . b x c = %(a.scalarTripleProd(b, c))")
System.print("a x b x c = %(a.vectorTripleProd(b, c))")</syntaxhighlight>
=={{header|XBS}}==
<syntaxhighlight lang="xbs">#> Vector3 Class <#
class Vector3 {
construct=func(self,x,y,z){
self:x=x;
self:y=y;
self:z=z;
};
ToString=func(self){
send self.x+", "+self.y+", "+self.z;
};
Magnitude=func(self){
send math.sqrt((self.x^2)+(self.y^2)+(self.z^2));
};
Normalize=func(self){
set Mag = self::Magnitude();
send new Vector3 with [self.x/Mag,self.y/Mag,self.z/Mag];
};
Dot=func(self,v2){
send (self.x*v2.x)+(self.y*v2.y)+(self.z*v2.z);
};
__add=func(self,x){
if (type(x)=="object"){
send new Vector3 with [self.x+x.x,self.y+x.y,self.z+x.z];
} else {
send new Vector3 with [self.x+x,self.y+x,self.z+x];
}
};
__sub=func(self,x){
if (type(x)=="object"){
send new Vector3 with [self.x-x.x,self.y-x.y,self.z-x.z];
} else {
send new Vector3 with [self.x-x,self.y-x,self.z-x];
}
};
__mul=func(self,x){
if (type(x)=="object"){
send new Vector3 with [self.x*x.x,self.y*x.y,self.z*x.z];
} else {
send new Vector3 with [self.x*x,self.y*x,self.z*x];
}
};
__div=func(self,x){
if (type(x)=="object"){
send new Vector3 with [self.x/x.x,self.y/x.y,self.z/x.z];
} else {
send new Vector3 with [self.x/x,self.y/x,self.z/x];
}
};
__pow=func(self,x){
if (type(x)=="object"){
send new Vector3 with [self.x^x.x,self.y^x.y,self.z^x.z];
} else {
send new Vector3 with [self.x^x,self.y^x,self.z^x];
}
};
__mod=func(self,x){
if (type(x)=="object"){
send new Vector3 with [self.x%x.x,self.y%x.y,self.z%x.z];
} else {
send new Vector3 with [self.x%x,self.y%x,self.z%x];
}
};
Reflect=func(self,v2){
set Normal = self::Normalize();
set Direction = v2::Normalize();
send (Normal*(2*Normal::Dot(Direction)))-Direction;
};
Cross=func(self,v2){
send new Vector3 with [(self.y*v2.z)-(self.z*v2.y),(self.z*v2.x)-(self.x*v2.z),(self.x*v2.y)-(self.y*v2.x)];
};
}
math:deg=func(x){
send x*(180/math.PI);
}
math:rad=func(x){
send x*(math.PI/180);
}
set a = new Vector3 with [3,4,5];
set b = new Vector3 with [4,3,5];
set c = new Vector3 with [-5,-12,-13];
log("Dot: ",a::Dot(b));
log("Cross: ",a::Cross(b)::ToString());
log("Scalar Triple: ",a::Dot(b::Cross(c)));
log("Vector Triple: ",a::Cross(b::Cross(c))::ToString());</syntaxhighlight>
{{out}}
<pre>
Dot: 49
Cross: 5, 5, -7
Scalar Triple: 6
Vector Triple: -267, 204, -3
</pre>
=={{header|XPL0}}==
<
func DotProd(A, B); \Return the dot product of two 3D vectors
Line 4,856 ⟶ 5,641:
IntOut(0, D(1)); ChOut(0, 9\tab\);
IntOut(0, D(2)); CrLf(0);
]</
Output:
Line 4,870 ⟶ 5,655:
The [(a1,a2,a3)] parameter notation just means add a preamble to the function body to do list assignment: a1,a2,a3:=arglist[0]. Since we don't need the vector as such, don't bother to name it (in the parameter list)
<
fcn crossp([(a1,a2,a3)],[(b1,b2,b3)]) //2
{ return(a2*b3 - a3*b2, a3*b1 - a1*b3, a1*b2 - a2*b1) }</
<
dotp(a,b).println(); //5 --> 49
crossp(a,b).println(); //6 --> (5,5,-7)
dotp(a, crossp(b,c)).println(); //7 --> 6
crossp(a, crossp(b,c)).println(); //8 --> (-267,204,-3)</
{{out}}
<pre>
Line 4,886 ⟶ 5,671:
</pre>
Or, using the GNU Scientific Library:
<
a:=GSL.VectorFromData( 3, 4, 5);
b:=GSL.VectorFromData( 4, 3, 5);
Line 4,896 ⟶ 5,681:
(a*(b.copy().crossProduct(c))).println(); // 6 scalar triple product
(a.crossProduct(b.crossProduct(c))) // (-267,204,-3) vector triple product, in place
.format().println();</
{{out}}
<pre>
|