September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,53 @@
class Quat{
fcn init(real=0,i1=0,i2=0,i3=0){
var [const] vector= // Quat(r,i,j,k) or Quat( (r,i,j,k) )
(if(List.isType(real)) real else vm.arglist).apply("toFloat");
var r,i,j,k; r,i,j,k=vector; // duplicate data for ease of coding
var [const] // properties: This is one way to do it
norm2=vector.apply("pow",2).sum(0.0), // Norm squared
abs=norm2.sqrt(), // Norm
arg=(r/abs()).acos(), // Theta !!!this may be incorrect...
;
}
fcn toString { String("[",vector.concat(","),"]") }
var [const proxy] // properties that need calculation (or are recursive)
conj =fcn{ Quat(r,-i,-j,-k) }, // Conjugate
recip =fcn{ n2:=norm2; Quat(r/n2,-i/n2,-j/n2,-k/n2) },// Reciprocal
pureim =fcn{ Quat(0, i, j, k) }, // Pure imagery
versor =fcn{ self / abs; }, // Unit versor
iversor=fcn{ pureim / pureim.abs; }, // Unit versor of imagery part
;
fcn __opEQ(z) { r == z.r and i == z.i and j == z.j and k == z.k }
fcn __opNEQ(z){ (not (self==z)) }
fcn __opNegate{ Quat(-r, -i, -j, -k) }
fcn __opAdd(z){
if (Quat.isInstanceOf(z)) Quat(vector.zipWith('+,z.vector));
else Quat(r+z,i,j,k);
}
fcn __opSub(z){
if (Quat.isInstanceOf(z)) Quat(vector.zipWith('-,z.vector));
else Quat(r-z,vector.xplode(1)); // same as above
}
fcn __opMul(z){
if (Quat.isInstanceOf(z)){
Quat(r*z.r - i*z.i - j*z.j - k*z.k,
r*z.i + i*z.r + j*z.k - k*z.j,
r*z.j - i*z.k + j*z.r + k*z.i,
r*z.k + i*z.j - j*z.i + k*z.r);
}
else Quat(vector.apply('*(z)));
}
fcn __opDiv(z){
if (Quat.isInstanceOf(z)) self*z.recip;
else Quat(r/z,i/z,j/z,k/z);
}
fcn pow(r){ exp(r*iversor*arg)*abs.pow(r) } // Power function
fcn log{ iversor*(r / abs).acos() + abs.log() }
fcn exp{ // e^q
inorm:=pureim.abs;
(iversor*inorm.sin() + inorm.cos()) * r.exp();
}
}

View file

@ -0,0 +1,39 @@
// Demo code
r:=7;
q:=Quat(2,3,4,5); q1:=Quat(2,3,4,5); q2:=Quat(3,4,5,6);
println("1. norm: q.abs: ", q.abs);
println("2. -q: ", -q);
println("3. conjugate: q.conj: ", q.conj);
println("4. Quat(r) + q: ", Quat(r) + q);
println(" q + r: ", q + r);
println("5. q1 + q2: ", q1 + q2);
println("6. Quat(r) * q: ", Quat(r) * q);
println(" q * r: ", q * r);
println("7. q1 * q2: ", q1 * q2);
println(" q2 * q1: ", q2 * q1);
println("8. q1 * q2 == q2 * q1 ? ", q1 * q2 == q2 * q1);
i:=Quat(0,1); j:=Quat(0,0,1); k:=Quat(0,0,0,1);
println("9.1 i * i: ", i * i);
println(" J * j: ", j * j);
println(" k * k: ", k * k);
println(" i * j * k: ", i * j * k);
println("9.2 q1 / q2: ", q1 / q2);
println("9.3 q1 / q2 * q2: ", q1 / q2 * q2);
println(" q2 * q1 / q2: ", q2 * q1 / q2);
println("9.4 (i * pi).exp(): ", (i * (0.0).pi).exp());
println(" exp(j * pi): ", (j * (0.0).pi).exp());
println(" exp(k * pi): ", (k * (0.0).pi).exp());
println(" q.exp(): ", q.exp());
println(" q.log(): ", q.log());
println(" q.log().exp(): ", q.log().exp());
println(" q.exp().log(): ", q.exp().log());
s:=q.exp().log();
println("9.5 let s=q.exp().log(): ", s);
println(" s.exp(): ", s.exp());
println(" s.log(): ", s.log());
println(" s.log().exp(): ", s.log().exp());
println(" s.exp().log(): ", s.exp().log());