Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -2,31 +2,30 @@ import std.stdio, std.conv, std.numeric;
|
|||
|
||||
struct V3 {
|
||||
union {
|
||||
immutable static struct { double x, y, z; }
|
||||
immutable struct { double x, y, z; }
|
||||
immutable double[3] v;
|
||||
}
|
||||
|
||||
double dot(in V3 rhs) /*@safe*/ const pure nothrow {
|
||||
double dot(in V3 rhs) const pure nothrow /*@safe*/ @nogc {
|
||||
return dotProduct(v, rhs.v);
|
||||
}
|
||||
|
||||
V3 cross(in V3 rhs) @safe const pure nothrow {
|
||||
V3 cross(in V3 rhs) const pure nothrow @safe @nogc {
|
||||
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); }
|
||||
string toString() const { return v.text; }
|
||||
}
|
||||
|
||||
double scalarTriple(in V3 a, in V3 b, in V3 c)
|
||||
/*@safe*/ pure nothrow {
|
||||
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 {
|
||||
V3 vectorTriple(in V3 a, in V3 b, in V3 c) @safe pure nothrow @nogc {
|
||||
return a.cross(b.cross(c));
|
||||
}
|
||||
|
||||
|
|
@ -34,6 +33,7 @@ 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);
|
||||
|
|
|
|||
39
Task/Vector-products/Forth/vector-products.fth
Normal file
39
Task/Vector-products/Forth/vector-products.fth
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
: 3f! ( &v - ) ( f: x y z - ) dup float+ dup float+ f! f! f! ;
|
||||
|
||||
: Vector \ Compiletime: ( f: x y z - ) ( <name> - )
|
||||
create here [ 3 floats ] literal allot 3f! ; \ Runtime: ( - &v )
|
||||
|
||||
: >fx@ ( &v - ) ( f: - n ) postpone f@ ; immediate
|
||||
: >fy@ ( &v - ) ( f: - n ) float+ f@ ;
|
||||
: >fz@ ( &v - ) ( f: - n ) float+ float+ f@ ;
|
||||
: .Vector ( &v - ) dup >fz@ dup >fy@ >fx@ f. f. f. ;
|
||||
|
||||
: Dot* ( &v1 &v2 - ) ( f - DotPrd )
|
||||
2dup >fx@ >fx@ f*
|
||||
2dup >fy@ >fy@ f* f+
|
||||
>fz@ >fz@ f* f+ ;
|
||||
|
||||
: Cross* ( &v1 &v2 &vResult - )
|
||||
>r 2dup >fz@ >fy@ f*
|
||||
2dup >fy@ >fz@ f* f-
|
||||
2dup >fx@ >fz@ f*
|
||||
2dup >fz@ >fx@ f* f-
|
||||
2dup >fy@ >fx@ f*
|
||||
>fx@ >fy@ f* f-
|
||||
r> 3f! ;
|
||||
|
||||
: ScalarTriple* ( &v1 &v2 &v3 - ) ( f: - ScalarTriple* )
|
||||
>r pad Cross* pad r> Dot* ;
|
||||
|
||||
: VectorTriple* ( &v1 &v2 &v3 &vDest - )
|
||||
>r swap r@ Cross* r> tuck Cross* ;
|
||||
|
||||
3e 4e 5e Vector A
|
||||
4e 3e 5e Vector B
|
||||
-5e -12e -13e Vector C
|
||||
|
||||
cr
|
||||
cr .( a . b = ) A B Dot* f.
|
||||
cr .( a x b = ) A B pad Cross* pad .Vector
|
||||
cr .( a . [b x c] = ) A B C ScalarTriple* f.
|
||||
cr .( a x [b x c] = ) A B C pad VectorTriple* pad .Vector
|
||||
16
Task/Vector-products/Prolog/vector-products.pro
Normal file
16
Task/Vector-products/Prolog/vector-products.pro
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
dot_product([A1, A2, A3], [B1, B2, B3], Ans) :-
|
||||
Ans is A1 * B1 + A2 * B2 + A3 * B3.
|
||||
|
||||
cross_product([A1, A2, A3], [B1, B2, B3], Ans) :-
|
||||
T1 is A2 * B3 - A3 * B2,
|
||||
T2 is A3 * B1 - A1 * B3,
|
||||
T3 is A1 * B2 - A2 * B1,
|
||||
Ans = [T1, T2, T3].
|
||||
|
||||
scala_triple(A, B, C, Ans) :-
|
||||
cross_product(B, C, Temp),
|
||||
dot_product(A, Temp, Ans).
|
||||
|
||||
vector_triple(A, B, C, Ans) :-
|
||||
cross_product(B, C, Temp),
|
||||
cross_product(A, Temp, Ans).
|
||||
|
|
@ -21,7 +21,7 @@ def vectortriplep(a, 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))
|
||||
print("a . b = %r" % dotp(a,b))
|
||||
print("a x b = %r" % (crossp(a,b),))
|
||||
print("a . (b x c) = %r" % scalartriplep(a, b, c))
|
||||
print("a x (b x c) = %r" % (vectortriplep(a, b, c),))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue