Vector products: Difference between revisions

Content added Content deleted
(→‎{{header|MATLAB}} / {{header|Octave}}: vector products in Matlab and Octave)
(→‎{{header|AWK}}: vector products)
Line 197: Line 197:
a x (b x c) = (-267, 204, -3)
a x (b x c) = (-267, 204, -3)
</pre>
</pre>
=={{header|AWK}}==

<lang awk>#!/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]" ]";
}</lang>

Output:

<pre>a = [ 3 4 5 ]
b = [ 4 3 5 ]
c = [ -5 -12 -13 ]
A.b = 49
a.b = [ 5 5 -7 ]
a.(b x c) = 6
a x (b x c) = [ -267 204 -3 ]
</pre>

=={{header|C|C}}==
=={{header|C|C}}==
<lang c>
<lang c>
Line 260: Line 303:
a x (b x c) = ( -267.000000, 204.000000, -3.000000)
a x (b x c) = ( -267.000000, 204.000000, -3.000000)
</lang>
</lang>

=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
<lang csharp>using System;
<lang csharp>using System;