March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
58
Task/Quaternion-type/JavaScript/quaternion-type-1.js
Normal file
58
Task/Quaternion-type/JavaScript/quaternion-type-1.js
Normal 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;
|
||||
})();
|
||||
18
Task/Quaternion-type/JavaScript/quaternion-type-2.js
Normal file
18
Task/Quaternion-type/JavaScript/quaternion-type-2.js
Normal 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)");
|
||||
Loading…
Add table
Add a link
Reference in a new issue