Vector products: Difference between revisions

PascalABC.NET
(PascalABC.NET)
 
(One intermediate revision by one other user not shown)
Line 3,802:
a x (b x c): -267.00000000 204.00000000 -3.00000000
</pre>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
uses System.Numerics;
 
function DotProduct(v1,v2: Vector3): real
:= v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
 
function CrossProduct(v1,v2: Vector3): Vector3
:= new Vector3(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
 
function ScalarTripleProduct(a,b,c: Vector3): real
:= DotProduct(a, CrossProduct(b, c));
function VectorTripleProduct(a,b,c: Vector3): Vector3
:= CrossProduct(a, CrossProduct(b, c));
 
 
begin
var a := new Vector3(3,4,5);
var b := new Vector3(4,3,5);
var c := new Vector3(-5,-12,-13);
Writeln(DotProduct(a, b));
Writeln(CrossProduct(a, b));
Writeln(ScalarTripleProduct(a, b, c));
Writeln(VectorTripleProduct(a, b, c));
end.
</syntaxhighlight>
{{out}}
</pre>
49
<5. 5. -7>
6
<-267. 204. -3>
</pre>
 
 
=={{header|Perl}}==
Line 4,630 ⟶ 4,668:
 
=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program computes the products: dot, cross, scalar triple, and vector triple.*/
a= 3 4 5
b= 4 3 5 b= 4 3 5 /*(positive numbers don't need quotes.)*/
c= "'-5 -12 -13"'
callCall tellV 'vector A =', a /*show the A vector, aligned numbers.*/
callCall tellV '"vector B ='", b /* " " B " " " */
callCall tellV '"vector C ='", c /* " " C " " " */
Say ''
say
callCall tellV ' dot product [A∙BA·B] =', dot(a, b)
callCall tellV 'cross product [AxB] =', cross(a, b)
callCall tellV 'scalar triple product [A∙(BxC)] =', dot(a, cross(b, c) )
callCall tellV 'vector triple product [Ax(BxC)] =', cross(a, cross(b, c) )
exit 0 Exit /*stick a fork in it, we're all done. */
/*---------------------------------------------------------------------------*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
cross: Procedure
cross: procedure; arg $1 $2 $3,@1 @2 @3; return $2*@3 -$3*@2 $3*@1 -$1*@3 $1*@2 -$2*@1
Arg a b c, u v w
dot: procedure; arg $1 $2 $3,@1 @2 @3; return $1*@1 + $2*@2 + $3*@3
Return b*w-c*v c*u-a*w a*v-b*u
/*──────────────────────────────────────────────────────────────────────────────────────*/
dot: Procedure
tellV: procedure; parse arg name,x y z; w=max(4,length(x),length(y),length(z)) /*max W*/
Arg a b c, u v w
say right(name,40) right(x,w) right(y,w) right(z,w); /*show vector.*/ return</syntaxhighlight>
Return a*u + b*v + c*w
{{out|output|text=&nbsp; when using the default internal inputs:}}
/*---------------------------------------------------------------------------*/
tellV: Procedure
Parse Arg name,x y z
tellV: procedure; parse arg name,x y z; w=max(4,length(x),length(y),length(z)) /*max Wwidth */
saySay right(name,4033) right(x,w) right(y,w) right(z,w); /*show vector.*/ return< */syntaxhighlight>
Return</syntaxhighlight>
{{out|output|text=&nbsp; when using the default internal inputs:}}
<pre>
vector A = 3 4 5
vector B = 4 3 5
vector C = -5 -12 -13
 
dot product [A∙BAÀB] = 49
cross product [AxB] = 5 5 -7
scalar triple product [A∙(BxC)] = 6
vector triple product [Ax(BxC)] = -267 204 -3</pre>
</pre>
 
=={{header|Ring}}==
217

edits