Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,45 @@
begin
% define the Vector record type %
record Vector( integer X, Y, Z );
% calculates the dot product of two Vectors %
integer procedure dotProduct( reference(Vector) value A, B ) ;
( X(A) * X(B) ) + ( Y(A) * Y(B) ) + ( Z(A) * Z(B) );
% calculates the cross product or two Vectors %
reference(Vector) procedure crossProduct( reference(Vector) value A, B ) ;
Vector( ( Y(A) * Z(B) ) - ( Z(A) * Y(B) )
, ( Z(A) * X(B) ) - ( X(A) * Z(B) )
, ( X(A) * Y(B) ) - ( Y(A) * X(B) )
);
% calculates the scaler triple product of two vectors %
integer procedure scalerTripleProduct( reference(Vector) value A, B, C ) ;
dotProduct( A, crossProduct( B, C ) );
% calculates the vector triple product of two vectors %
reference(Vector) procedure vectorTripleProduct( reference(Vector) value A, B, C ) ;
crossProduct( A, crossProduct( B, C ) );
% test the Vector routines %
begin
procedure writeonVector( reference(Vector) value v ) ;
writeon( "(", X(v), ", ", Y(v), ", ", Z(v), ")" );
Reference(Vector) a, b, c;
a := Vector( 3, 4, 5 );
b := Vector( 4, 3, 5 );
c := Vector( -5, -12, -13 );
i_w := 1; s_w := 0; % set output formatting %
write( " a: " ); writeonVector( a );
write( " b: " ); writeonVector( b );
write( " c: " ); writeonVector( c );
write( " a . b: ", dotProduct( a, b ) );
write( " a x b: " ); writeonVector( crossProduct( a, b ) );
write( "a . ( b x c ): ", scalerTripleProduct( a, b, c ) );
write( "a x ( b x c ): " ); writeonVector( vectorTripleProduct( a, b, c ) )
end
end.

View file

@ -0,0 +1,22 @@
(defun cross (a b)
(when (and (equal (length a) 3) (equal (length b) 3))
(vector
(- (* (elt a 1) (elt b 2)) (* (elt a 2) (elt b 1)))
(- (* (elt a 2) (elt b 0)) (* (elt a 0) (elt b 2)))
(- (* (elt a 0) (elt b 1)) (* (elt a 1) (elt b 0))))))
(defun dot (a b)
(when (equal (length a) (length b))
(loop for ai across a for bi across b sum (* ai bi))))
(defun scalar-triple (a b c)
(dot a (cross b c)))
(defun vector-triple (a b c)
(cross a (cross b c)))
(defun task (a b c)
(values (dot a b)
(cross a b)
(scalar-triple a b c)
(vector-triple a b c)))

View file

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

View file

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

View file

@ -1,12 +1 @@
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
cross=: [: > [: -&.>/ .(*&.>) (<"1=i.3) , ,:&:(<"0)

View file

@ -1,8 +1,12 @@
dotP a;b
49
crossP a;b
5 5 _7
scTriP a;b;c
6
veTriP a;b;c
_267 204 _3
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,26 @@
function scltrip{T<:Number}(a::AbstractArray{T,1},
b::AbstractArray{T,1},
c::AbstractArray{T,1})
dot(a, cross(b, c))
end
function vectrip{T<:Number}(a::AbstractArray{T,1},
b::AbstractArray{T,1},
c::AbstractArray{T,1})
cross(a, cross(b, c))
end
a = [3, 4, 5]
b = [4, 3, 5]
c = [-5, -12, -13]
println("Test Vectors:")
println(" a = ", a)
println(" b = ", a)
println(" c = ", a)
println("\nVector Products:")
println(" a dot b = ", dot(a, b))
println(" a cross b = ", cross(a, b))
println(" a dot b cross c = ", scltrip(a, b, c))
println(" a cross b cross c = ", vectrip(a, b, c))

View file

@ -0,0 +1,27 @@
function dot-product($a,$b) {
$a[0]*$b[0] + $a[1]*$b[1] + $a[2]*$b[2]
}
function cross-product($a,$b) {
$v1 = $a[1]*$b[2] - $a[2]*$b[1]
$v2 = $a[2]*$b[0] - $a[0]*$b[2]
$v3 = $a[0]*$b[1] - $a[1]*$b[0]
@($v1,$v2,$v3)
}
function scalar-triple-product($a,$b,$c) {
dot-product $a (cross-product $b $c)
}
function vector-triple-product($a,$b) {
cross-product $a (cross-product $b $c)
}
$a = @(3, 4, 5)
$b = @(4, 3, 5)
$c = @(-5, -12, -13)
"a.b = $(dot-product $a $b)"
"axb = $(cross-product $a $b)"
"a.(bxc) = $(scalar-triple-product $a $b $c)"
"ax(bxc) = $(vector-triple-product $a $b $c)"