June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
31
Task/Vector-products/Factor/vector-products.factor
Normal file
31
Task/Vector-products/Factor/vector-products.factor
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
USING: arrays io locals math prettyprint sequences ;
|
||||
IN: rosetta-code.vector-products
|
||||
|
||||
: dot-product ( a b -- dp ) [ * ] 2map sum ;
|
||||
|
||||
:: cross-product ( a b -- cp )
|
||||
a first :> a1 a second :> a2 a third :> a3
|
||||
b first :> b1 b second :> b2 b third :> b3
|
||||
a2 b3 * a3 b2 * - ! X
|
||||
a3 b1 * a1 b3 * - ! Y
|
||||
a1 b2 * a2 b1 * - ! Z
|
||||
3array ;
|
||||
|
||||
: scalar-triple-product ( a b c -- stp )
|
||||
cross-product dot-product ;
|
||||
|
||||
: vector-triple-product ( a b c -- vtp )
|
||||
cross-product cross-product ;
|
||||
|
||||
[let
|
||||
{ 3 4 5 } :> a
|
||||
{ 4 3 5 } :> b
|
||||
{ -5 -12 -13 } :> c
|
||||
"a: " write a .
|
||||
"b: " write b .
|
||||
"c: " write c . nl
|
||||
"a . b: " write a b dot-product .
|
||||
"a x b: " write a b cross-product .
|
||||
"a . (b x c): " write a b c scalar-triple-product .
|
||||
"a x (b x c): " write a b c vector-triple-product .
|
||||
]
|
||||
|
|
@ -1,26 +1,20 @@
|
|||
function scltrip{T<:Number}(a::AbstractArray{T,1},
|
||||
b::AbstractArray{T,1},
|
||||
c::AbstractArray{T,1})
|
||||
dot(a, cross(b, c))
|
||||
function scalarproduct(a::AbstractVector{T}, b::AbstractVector{T}, c::AbstractVector{T}) where {T<:Number}
|
||||
return 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))
|
||||
function vectorproduct(a::AbstractVector{T}, b::AbstractVector{T}, c::AbstractVector{T}) where {T<:Number}
|
||||
return cross(a, cross(b, c))
|
||||
end
|
||||
|
||||
a = [3, 4, 5]
|
||||
b = [4, 3, 5]
|
||||
c = [-5, -12, -13]
|
||||
const a = [3, 4, 5]
|
||||
const b = [4, 3, 5]
|
||||
const c = [-5, -12, -13]
|
||||
|
||||
println("Test Vectors:")
|
||||
println(" a = ", a)
|
||||
println(" b = ", a)
|
||||
println(" c = ", a)
|
||||
@show a b c
|
||||
|
||||
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))
|
||||
@show dot(a, b)
|
||||
@show cross(a, b)
|
||||
@show scalarproduct(a, b, c)
|
||||
@show vectorproduct(a, b, c)
|
||||
|
|
|
|||
83
Task/Vector-products/Modula-2/vector-products.mod2
Normal file
83
Task/Vector-products/Modula-2/vector-products.mod2
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
MODULE VectorProducts;
|
||||
FROM RealStr IMPORT RealToStr;
|
||||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
|
||||
|
||||
PROCEDURE WriteReal(r : REAL);
|
||||
VAR buf : ARRAY[0..31] OF CHAR;
|
||||
BEGIN
|
||||
RealToStr(r, buf);
|
||||
WriteString(buf)
|
||||
END WriteReal;
|
||||
|
||||
TYPE Vector = RECORD
|
||||
a,b,c : REAL;
|
||||
END;
|
||||
|
||||
PROCEDURE Dot(u,v : Vector) : REAL;
|
||||
BEGIN
|
||||
RETURN u.a * v.a
|
||||
+ u.b * v.b
|
||||
+ u.c * v.c
|
||||
END Dot;
|
||||
|
||||
PROCEDURE Cross(u,v : Vector) : Vector;
|
||||
BEGIN
|
||||
RETURN Vector{
|
||||
u.b*v.c - u.c*v.b,
|
||||
u.c*v.a - u.a*v.c,
|
||||
u.a*v.b - u.b*v.a
|
||||
}
|
||||
END Cross;
|
||||
|
||||
PROCEDURE ScalarTriple(u,v,w : Vector) : REAL;
|
||||
BEGIN
|
||||
RETURN Dot(u, Cross(v, w))
|
||||
END ScalarTriple;
|
||||
|
||||
PROCEDURE VectorTriple(u,v,w : Vector) : Vector;
|
||||
BEGIN
|
||||
RETURN Cross(u, Cross(v, w))
|
||||
END VectorTriple;
|
||||
|
||||
PROCEDURE WriteVector(v : Vector);
|
||||
BEGIN
|
||||
WriteString("<");
|
||||
WriteReal(v.a);
|
||||
WriteString(", ");
|
||||
WriteReal(v.b);
|
||||
WriteString(", ");
|
||||
WriteReal(v.c);
|
||||
WriteString(">")
|
||||
END WriteVector;
|
||||
|
||||
VAR a,b,c : Vector;
|
||||
BEGIN
|
||||
a := Vector{3.0, 4.0, 5.0};
|
||||
b := Vector{4.0, 3.0, 5.0};
|
||||
c := Vector{-5.0, -12.0, -13.0};
|
||||
|
||||
WriteVector(a);
|
||||
WriteString(" dot ");
|
||||
WriteVector(b);
|
||||
WriteString(" = ");
|
||||
WriteReal(Dot(a,b));
|
||||
WriteLn;
|
||||
|
||||
WriteVector(a);
|
||||
WriteString(" cross ");
|
||||
WriteVector(b);
|
||||
WriteString(" = ");
|
||||
WriteVector(Cross(a,b));
|
||||
WriteLn;
|
||||
|
||||
WriteVector(a);
|
||||
WriteString(" cross (");
|
||||
WriteVector(b);
|
||||
WriteString(" cross ");
|
||||
WriteVector(c);
|
||||
WriteString(") = ");
|
||||
WriteVector(VectorTriple(a,b,c));
|
||||
WriteLn;
|
||||
|
||||
ReadChar
|
||||
END VectorProducts.
|
||||
Loading…
Add table
Add a link
Reference in a new issue