March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,72 @@
q := [1, 2, 3, 4]
q1 := [2, 3, 4, 5]
q2 := [3, 4, 5, 6]
r := 7
MsgBox, % "q = " PrintQ(q)
. "`nq1 = " PrintQ(q1)
. "`nq2 = " PrintQ(q2)
. "`nr = " r
. "`nNorm(q) = " Norm(q)
. "`nNegative(q) = " PrintQ(Negative(q))
. "`nConjugate(q) = " PrintQ(Conjugate(q))
. "`nq + r = " PrintQ(AddR(q, r))
. "`nq1 + q2 = " PrintQ(AddQ(q1, q2))
. "`nq2 + q1 = " PrintQ(AddQ(q2, q1))
. "`nqr = " PrintQ(MulR(q, r))
. "`nq1q2 = " PrintQ(MulQ(q1, q2))
. "`nq2q1 = " PrintQ(MulQ(q2, q1))
Norm(q) {
return sqrt(q[1]**2 + q[2]**2 + q[3]**2 + q[4]**2)
}
Negative(q) {
a := []
for k, v in q
a[A_Index] := v * -1
return a
}
Conjugate(q) {
a := []
for k, v in q
a[A_Index] := v * (A_Index = 1 ? 1 : -1)
return a
}
AddR(q, r) {
a := []
for k, v in q
a[A_Index] := v + (A_Index = 1 ? r : 0)
return a
}
AddQ(q1, q2) {
a := []
for k, v in q1
a[A_Index] := v + q2[A_Index]
return a
}
MulR(q, r) {
a := []
for k, v in q
a[A_Index] := v * r
return a
}
MulQ(q, u) {
a := []
, a[1] := q[1]*u[1] - q[2]*u[2] - q[3]*u[3] - q[4]*u[4]
, a[2] := q[1]*u[2] + q[2]*u[1] + q[3]*u[4] - q[4]*u[3]
, a[3] := q[1]*u[3] - q[2]*u[4] + q[3]*u[1] + q[4]*u[2]
, a[4] := q[1]*u[4] + q[2]*u[3] - q[3]*u[2] + q[4]*u[1]
return a
}
PrintQ(q, b="(") {
for k, v in q
b .= v (A_Index = q.MaxIndex() ? ")" : ", ")
return b
}

View file

@ -0,0 +1,58 @@
var Quaternion = (function() {
// The Q() function takes an array argument and changes it
// prototype so that it becomes a Quaternion instance. This is
// scoped only for prototype member access.
function Q(a) {
a.__proto__ = proto;
return a;
}
// Actual constructor. This constructor converts its arguments to
// an array, then that array to a Quaternion instance, then
// returns that instance. (using "new" with this constructor is
// optional)
function Quaternion() {
return Q(Array.prototype.slice.call(arguments, 0, 4));
}
// Prototype for all Quaternions
const proto = {
// Inherits from a 4-element Array
__proto__ : [0,0,0,0],
// Properties -- In addition to Array[0..3] access, we
// also define matching a, b, c, and d properties
get a() this[0],
get b() this[1],
get c() this[2],
get d() this[3],
// Methods
norm : function() Math.sqrt(this.map(function(x) x*x).reduce(function(x,y) x+y)),
negate : function() Q(this.map(function(x) -x)),
conjugate : function() Q([ this[0] ].concat(this.slice(1).map(function(x) -x))),
add : function(x) {
if ("number" === typeof x) {
return Q([ this[0] + x ].concat(this.slice(1)));
} else {
return Q(this.map(function(v,i) v+x[i]));
}
},
mul : function(r) {
var q = this;
if ("number" === typeof r) {
return Q(q.map(function(e) e*r));
} else {
return Q([ q[0] * r[0] - q[1] * r[1] - q[2] * r[2] - q[3] * r[3],
q[0] * r[1] + q[1] * r[0] + q[2] * r[3] - q[3] * r[2],
q[0] * r[2] - q[1] * r[3] + q[2] * r[0] + q[3] * r[1],
q[0] * r[3] + q[1] * r[2] - q[2] * r[1] + q[3] * r[0] ]);
}
},
equals : function(q) this.every(function(v,i) v === q[i]),
toString : function() (this[0] + " + " + this[1] + "i + "+this[2] + "j + " + this[3] + "k").replace(/\+ -/g, '- ')
};
Quaternion.prototype = proto;
return Quaternion;
})();

View file

@ -0,0 +1,18 @@
var q = Quaternion(1,2,3,4);
var q1 = Quaternion(2,3,4,5);
var q2 = Quaternion(3,4,5,6);
var r = 7;
console.log("q = "+q);
console.log("q1 = "+q1);
console.log("q2 = "+q2);
console.log("r = "+r);
console.log("1. q.norm() = "+q.norm());
console.log("2. q.negate() = "+q.negate());
console.log("3. q.conjugate() = "+q.conjugate());
console.log("4. q.add(r) = "+q.add(r));
console.log("5. q1.add(q2) = "+q1.add(q2));
console.log("6. q.mul(r) = "+q.mul(r));
console.log("7.a. q1.mul(q2) = "+q1.mul(q2));
console.log("7.b. q2.mul(q1) = "+q2.mul(q1));
console.log("8. q1.mul(q2) " + (q1.mul(q2).equals(q2.mul(q1)) ? "==" : "!=") + " q2.mul(q1)");

View file

@ -0,0 +1 @@
pkg install -forge quaternion

View file

@ -0,0 +1,24 @@
> q = quaternion (1, 2, 3, 4)
q = 1 + 2i + 3j + 4k
> q1 = quaternion (2, 3, 4, 5)
q1 = 2 + 3i + 4j + 5k
> q2 = quaternion (3, 4, 5, 6)
q2 = 3 + 4i + 5j + 6k
> r = 7
r = 7
> norm(q)
ans = 5.4772
> -q
ans = -1 - 2i - 3j - 4k
> conj(q)
ans = 1 - 2i - 3j - 4k
> q + r
ans = 8 + 2i + 3j + 4k
> q1 + q2
ans = 5 + 7i + 9j + 11k
> q * r
ans = 7 + 14i + 21j + 28k
> q1 * q2
ans = -56 + 16i + 24j + 26k
> q1 == q2
ans = 0