Jump to content

Vector products: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
m (Adjust Mercury formatting.)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 1,733:
cross(a, b)
# [1] 5 5 -7</lang>
 
=={{header|REXX}}==
<lang rexx>
/*REXX program to compute the products: the dot product, */
/* the cross product, */
/* the scalar triple product, and*/
/* the vector triple product. */
 
a= 3 4 5 /*positive numbers don't need " */
b= 4 3 5
c="-5 -12 -13"
 
call tellV 'vector A =',a /*show the A vector, aligned #s*/
call tellV 'vector B =',b /*show the B vector, aligned #s*/
call tellV 'vector C =',c /*show the C vector, aligned #s*/
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
 
/*─────────────────────────────────────tellV subroutine─────────────────*/
tellV: procedure; parse arg name,x y z /*display the vector*/
w=max(4,length(x),length(y),length(z)) /*max width of nums.*/
say right(name,40) right(x,w) right(y,w) right(z,w)
return
 
/*─────────────────────────────────────dot subroutine───────────────────*/
dot: procedure; parse arg x1 x2 x3,y1 y2 y3 /*the DOT product.*/
return x1*y1 + x2*y2 + x3*y3 /*a scaler quantity.*/
 
/*─────────────────────────────────────cross subroutine─────────────────*/
cross: procedure; parse arg x1 x2 x3,y1 y2 y3 /*the CROSS product.*/
return x2*y3-x3*y2 x3*y1-x1*y3 x1*y2-x2*y1 /*a vector quantity.*/
</lang>
Output:
<pre style="height:25ex;overflow:scroll">
vector A = 3 4 5
vector B = 4 3 5
vector C = -5 -12 -13
 
dot product [A∙B] = 49
cross product [AxB] = 5 5 -7
scalar triple product [A∙(BxC)] = 6
vector triple product [Ax(BxC)] = -267 204 -3
</pre>
 
=={{header|Ruby}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.