all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,30 @@
Define a vector 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 vectors <code>A = (a<sub>1</sub>, a<sub>2</sub>, a<sub>3</sub>); B = (b<sub>1</sub>, b<sub>2</sub>, b<sub>3</sub>);</code> and <code>C = (c<sub>1</sub>, c<sub>2</sub>, c<sub>3</sub>);</code> then the following common vector products are defined:
* '''The dot product'''
: A • B = <code>a<sub>1</sub>b<sub>1</sub> + a<sub>2</sub>b<sub>2</sub> + a<sub>3</sub>b<sub>3</sub>;</code> a scalar quantity
* '''The cross product'''
: A x B = <code>(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>);</code> a vector quantity
* '''The scalar triple product'''
: A • (B x C); a scalar quantity
* '''The vector triple product'''
: A x (B x C); a vector quantity
;Task description
Given the three vectors: <code>a = (3, 4, 5); b = (4, 3, 5); c = (-5, -12, -13)</code>:
# 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 scaler triple product.
# Compute and display: <code>a x b x c</code>, the vector triple product.
;References:
* [[Dot product]] here on RC.
* [http://mathworld.wolfram.com/VectorMultiplication.html A starting page] to the Wolfram Mathworld information on vector multiplication.
* Wikipedias [[wp:Dot product|dot product]], [[wp:Cross product|cross product]] and [[wp:Triple product|triple product]] entries.
;C.f.
* [[Quaternion type]]

View 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)))
)

View 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]" ]";
}

View 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;

View 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

View 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

View 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 ;
}

View 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;
}

View 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)))

View 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))))

View file

@ -0,0 +1,44 @@
import std.stdio, std.conv, std.numeric;
struct V3 {
union {
immutable static struct { double x, y, z; }
immutable double[3] v;
}
double dot(in V3 rhs) /*@safe*/ const pure nothrow {
return dotProduct(v, rhs.v);
}
V3 cross(in V3 rhs) @safe const pure nothrow {
return V3(y * rhs.z - z * rhs.y,
z * rhs.x - x * rhs.z,
x * rhs.y - y * rhs.x);
}
string toString() /*@safe*/ const { return text(v); }
}
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 {
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));
}

View 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 )

View 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 (", ") + "]")
}
}

View 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

View 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 ]

View 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))
}

View 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()
}

View 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]]
}

View 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)))
}

View 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)

View file

@ -0,0 +1,32 @@
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 * v !! 0 - u !! 0 * v !! 2,
u !! 0 * v !! 1 - u !! 1 * v !! 0]
| 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 = do
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) ]

View 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

View file

@ -0,0 +1,3 @@
CT=: C.!.2 @ (#:i.) @ $~
ip=: +/ .* NB. inner product
cross=: ] ip CT@#@[ ip [

View file

@ -0,0 +1 @@
cross=: [: > [: -&.>/ .(*&.>) (<"1=i.3) , ,:&:(<"0)

View 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

View 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

View 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));
}
}

View 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)
);
}());

View 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

View 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 )

View 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))

View 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]]

View 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)]).

View 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));
}
}

View 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));
;;

View 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));
}
}
}

View 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)

View 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)

View 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;

View 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;

View 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.

View file

@ -0,0 +1,20 @@
sub infix:<> { [+] @^a »*« @^b }
sub infix:<>([$a1, $a2, $a3], [$b1, $b2, $b3]) {
[ $a2*$b3 - $a3*$b2,
$a3*$b1 - $a1*$b3,
$a1*$b2 - $a2*$b1 ];
}
sub scalar-triple-product { @^a(@^b @^c) }
sub vector-triple-product { @^a (@^b @^c) }
my @a = <3 4 5>;
my @b = <4 3 5>;
my @c = <-5 -12 -13>;
say (:@a, :@b, :@c).perl;
say "a b = { @a@b }";
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) }>";

View 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";

View 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)) )

View 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

View 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 =", dotp(a,b))
print("a x b =", crossp(a,b))
print("a . (b x c) =", scalartriplep(a, b, c))
print("a x (b x c) =", vectortriplep(a, b, c))

View file

@ -0,0 +1,10 @@
a <- c( 3.0, 4.0, 5.0)
b <- c( 4.0, 3.0, 5.0)
cross <- function(a, b)
c(a[2]*b[3] - a[3]*b[2],
a[3]*b[1] - a[1]*b[3],
a[1]*b[2] - a[2]*b[1])
cross(a, b)
# [1] 5 5 -7

View file

@ -0,0 +1,29 @@
/*REXX program computes 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 [AB] =',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 /*stick a fork in it, we're done.*/
/*─────────────────────────────────────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.*/
/*─────────────────────────────────────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.*/
/*─────────────────────────────────────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

View file

@ -0,0 +1,29 @@
require 'matrix'
class Vector
def cross_product(v)
unless size == 3 && v.size == 3
raise ArgumentError, "Vectors must have size 3"
end
Vector[self[1] * v[2] - self[2] * v[1],
self[2] * v[0] - self[0] * v[2],
self[0] * v[1] - self[1] * v[0]]
end
def scalar_triple_product(b, c)
self.inner_product(b.cross_product c)
end
def vector_triple_product(b, c)
self.cross_product(b.cross_product c)
end
end
a = Vector[3, 4, 5]
b = Vector[4, 3, 5]
c = Vector[-5, -12, -13]
puts "a dot b = #{a.inner_product b}"
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}"

View file

@ -0,0 +1,19 @@
case class Vector3D(x:Double, y:Double, z:Double) {
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)
def scalarTriple(v1:Vector3D, v2:Vector3D)=this dot (v1 cross v2)
def vectorTriple(v1:Vector3D, v2:Vector3D)=this cross (v1 cross v2)
}
object VectorTest {
def main(args:Array[String])={
val a=Vector3D(3,4,5)
val b=Vector3D(4,3,5)
val c=Vector3D(-5,-12,-13)
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)))
}
}

View file

@ -0,0 +1,35 @@
(define (dot-product A B)
(apply + (map * (vector->list A) (vector->list B))))
(define (cross-product A B)
(define len (vector-length A))
(define xp (make-vector (vector-length A) #f))
(let loop ((n 0))
(vector-set! xp n (-
(* (vector-ref A (modulo (+ n 1) len))
(vector-ref B (modulo (+ n 2) len)))
(* (vector-ref A (modulo (+ n 2) len))
(vector-ref B (modulo (+ n 1) len)))))
(if (eqv? len (+ n 1))
xp
(loop (+ n 1)))))
(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))
(display "A = ")(display A)(newline)
(display "B = ")(display B)(newline)
(display "C = ")(display C)(newline)
(newline)
(display "A . B = ")(display (dot-product A B))(newline)
(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)

View file

@ -0,0 +1,51 @@
$ include "seed7_05.s7i";
include "float.s7i";
const type: vec3 is new struct
var float: x is 0.0;
var float: y is 0.0;
var float: z is 0.0;
end struct;
const func vec3: vec3 (in float: x, in float: y, in float: z) is func
result
var vec3: aVector is vec3.value;
begin
aVector.x := x;
aVector.y := y;
aVector.z := z;
end func;
$ syntax expr: .(). dot .() is -> 6;
const func float: (in vec3: a) dot (in vec3: b) is
return a.x*b.x + a.y*b.y + a.z*b.z;
$ syntax expr: .(). X .() is -> 6;
const func vec3: (in vec3: a) X (in vec3: b) is
return 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);
const func string: str (in vec3: v) is
return "(" <& v.x <& ", " <& v.y <& ", " <& v.z <& ")";
enable_output(vec3);
const func float: scalarTriple (in vec3: a, in vec3: b, in vec3: c) is
return a dot (b X c);
const func vec3: vectorTriple (in vec3: a, in vec3: b, in vec3: c) is
return a X (b X c);
const proc: main is func
local
const vec3: a is vec3(3.0, 4.0, 5.0);
const vec3: b is vec3(4.0, 3.0, 5.0);
const vec3: c is vec3(-5.0, -12.0, -13.0);
begin
writeln("a = " <& a <& ", b = " <& b <& ", c = " <& c);
writeln("a . b = " <& a dot b);
writeln("a x b = " <& a X b);
writeln("a .(b x c) = " <& scalarTriple(a, b, c));
writeln("a x(b x c) = " <& vectorTriple(a, b, c));
end func;

View file

@ -0,0 +1,18 @@
proc dot {A B} {
lassign $A a1 a2 a3
lassign $B b1 b2 b3
expr {$a1*$b1 + $a2*$b2 + $a3*$b3}
}
proc cross {A B} {
lassign $A a1 a2 a3
lassign $B b1 b2 b3
list [expr {$a2*$b3 - $a3*$b2}] \
[expr {$a3*$b1 - $a1*$b3}] \
[expr {$a1*$b2 - $a2*$b1}]
}
proc scalarTriple {A B C} {
dot $A [cross $B $C]
}
proc vectorTriple {A B C} {
cross $A [cross $B $C]
}

View file

@ -0,0 +1,7 @@
set a {3 4 5}
set b {4 3 5}
set c {-5 -12 -13}
puts "a b = [dot $a $b]"
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]"

View file

@ -0,0 +1,58 @@
Public Class Vector3D
Private _x, _y, _z As Double
Public Sub New(ByVal X As Double, ByVal Y As Double, ByVal Z As Double)
_x = X
_y = Y
_z = Z
End Sub
Public Property X() As Double
Get
Return _x
End Get
Set(ByVal value As Double)
_x = value
End Set
End Property
Public Property Y() As Double
Get
Return _y
End Get
Set(ByVal value As Double)
_y = value
End Set
End Property
Public Property Z() As Double
Get
Return _z
End Get
Set(ByVal value As Double)
_z = value
End Set
End Property
Public Function Dot(ByVal v2 As Vector3D) As Double
Return (X * v2.X) + (Y * v2.Y) + (Z * v2.Z)
End Function
Public Function Cross(ByVal v2 As Vector3D) As Vector3D
Return New Vector3D((Y * v2.Z) - (Z * v2.Y), _
(Z * v2.X) - (X * v2.Z), _
(X * v2.Y) - (Y * v2.X))
End Function
Public Function ScalarTriple(ByVal v2 As Vector3D, ByVal v3 As Vector3D) As Double
Return Me.Dot(v2.Cross(v3))
End Function
Public Function VectorTriple(ByRef v2 As Vector3D, ByVal v3 As Vector3D) As Vector3D
Return Me.Cross(v2.Cross(v3))
End Function
Public Overrides Function ToString() As String
Return String.Format("({0}, {1}, {2})", _x, _y, _z)
End Function
End Class

View file

@ -0,0 +1,19 @@
Module Module1
Sub Main()
Dim v1 As New Vector3D(3, 4, 5)
Dim v2 As New Vector3D(4, 3, 5)
Dim v3 As New Vector3D(-5, -12, -13)
Console.WriteLine("v1: {0}", v1.ToString())
Console.WriteLine("v2: {0}", v2.ToString())
Console.WriteLine("v3: {0}", v3.ToString())
Console.WriteLine()
Console.WriteLine("v1 . v2 = {0}", v1.Dot(v2))
Console.WriteLine("v1 x v2 = {0}", v1.Cross(v2).ToString())
Console.WriteLine("v1 . (v2 x v3) = {0}", v1.ScalarTriple(v2, v3))
Console.WriteLine("v1 x (v2 x v3) = {0}", v1.VectorTriple(v2, v3))
End Sub
End Module

View file

@ -0,0 +1,45 @@
include c:\cxpl\codes; \intrinsic 'code' declarations
func DotProd(A, B); \Return the dot product of two 3D vectors
int A, B; \A ù B
return A(0)*B(0) + A(1)*B(1) + A(2)*B(2);
proc CrossProd(A, B, C); \Calculate the cross product of two 3D vectors
int A, B, C; \C:= A x B
[C(0):= A(1)*B(2) - A(2)*B(1);
C(1):= A(2)*B(0) - A(0)*B(2);
C(2):= A(0)*B(1) - A(1)*B(0);
]; \CrossProd
func ScalarTriProd(A, B, C); \Return the scalar triple product
int A, B, C; \A ù (B x C)
int D(3);
[CrossProd(B, C, D);
return DotProd(A, D);
]; \ScalarTriProd
proc VectTriProd(A, B, C, D); \Calculate the vector triple product
int A, B, C, D; \D:= A x (B x C)
int E(3);
[CrossProd(B, C, E);
CrossProd(A, E, D);
]; \CrossProd
int A, B, C, D(3);
[A:= [3, 4, 5]; B:= [4, 3, 5]; C:= [-5, -12, -13];
IntOut(0, DotProd(A,B)); CrLf(0);
CrossProd(A, B, D);
IntOut(0, D(0)); ChOut(0, 9\tab\);
IntOut(0, D(1)); ChOut(0, 9\tab\);
IntOut(0, D(2)); CrLf(0);
IntOut(0, ScalarTriProd(A,B,C)); CrLf(0);
VectTriProd(A, B, C, D);
IntOut(0, D(0)); ChOut(0, 9\tab\);
IntOut(0, D(1)); ChOut(0, 9\tab\);
IntOut(0, D(2)); CrLf(0);
]