Geometric algebra: Difference between revisions
Content added Content deleted
(rewriting. Back to verifying axioms.) |
(→{{header|javascript}}: update) |
||
Line 386: | Line 386: | ||
=={{header|javascript}}== |
=={{header|javascript}}== |
||
<lang javascript>var |
<lang javascript>var GA = function () { |
||
// parts of this comes from https://github.com/weshoke/versor.js/ |
|||
function e(n) { |
function e(n) { |
||
var result = []; |
var result = []; |
||
Line 393: | Line 392: | ||
return result; |
return result; |
||
} |
} |
||
function cdot(a, b) { return |
function cdot(a, b) { return mul([0.5], add(mul(a, b), mul(b, a))) } |
||
function neg(x) { return |
function neg(x) { return mul([-1], x) } |
||
function bitCount(i) { |
function bitCount(i) { |
||
// Note that unsigned shifting (>>>) is not required. |
// Note that unsigned shifting (>>>) is not required. |
||
Line 424: | Line 423: | ||
return result; |
return result; |
||
} |
} |
||
function |
function mul(a, b) |
||
{ |
{ |
||
var result = []; |
var result = []; |
||
Line 450: | Line 449: | ||
neg : neg, |
neg : neg, |
||
add : add, |
add : add, |
||
mul : |
mul : mul |
||
}; |
}; |
||
}();</lang> |
}();</lang> |
||
Line 456: | Line 455: | ||
And then, from the console: |
And then, from the console: |
||
<lang javascript>var e = |
<lang javascript>var e = GA.e, cdot = GA.cdot; |
||
⚫ | |||
function cdot(a, b) { return CGA.mul([0.5], CGA.add(CGA.mul(a, b), CGA.mul(b, a))) } |
|||
⚫ | |||
for (var |
for (var j = 0; j < 5; j++) { |
||
⚫ | |||
if (i < j) { |
if (i < j) { |
||
if (cdot(e(i), e(j))[0]) { console.log("unexpected non-nul scalar product"); } |
if (cdot(e(i), e(j))[0]) { console.log("unexpected non-nul scalar product"); } |
||
Line 469: | Line 467: | ||
} |
} |
||
function randomVector() { |
|||
⚫ | |||
var result = []; |
|||
v = CGA.add(v, CGA.mul([-1], e(1))); |
|||
for (var i = 0; i < 5; i++) { result = GA.add( result, GA.mul([Math.random()], e(i))); } |
|||
return result; |
|||
v = CGA.add(v, CGA.mul([3], e(3))); |
|||
} |
|||
v = CGA.add(v, CGA.mul([-2], e(4))); |
|||
function randomMultiVector() { |
|||
var result = []; |
|||
for (var i = 0; i < 32; i++) { result[i] = Math.random(); } |
|||
return result; |
|||
} |
|||
var a = randomMultiVector(), b = randomMultiVector(), c = randomMultiVector(); |
|||
⚫ | |||
⚫ | |||
// (ab)c == a(bc) |
|||
var i = CGA.mul(e(0), e(1)); |
|||
console.log(GA.mul(GA.mul(a, b), c)); |
|||
console.log(GA.mul(a, GA.mul(b, c))); |
|||
// a(b + c) == ab + ac |
|||
console.log(CGA.mul(i, i)); // [-1] |
|||
console.log( |
console.log(GA.mul(a, GA.add(b, c))); |
||
console.log( |
console.log(GA.add(GA.mul(a,b), GA.mul(a, c))); |
||
console.log(CGA.mul(CGA.mul(i, j), k)); // [-1] |
|||
// (a + b)c == ac + bc |
|||
console.log(GA.mul(GA.add(a, b), c)); |
|||
console.log(GA.add(GA.mul(a,c), GA.mul(b, c))); |
|||
var J = CGA.mul(e(2), e(3)); |
|||
var K = CGA.mul(e(1), e(3)); |
|||
// x² is real |
|||
⚫ | |||
console.log( |
console.log(GA.mul(x, x));</lang> |
||
{{out}} |
|||
console.log(CGA.mul(J, J)); // [-1] |
|||
<pre>[-7.834854130554672, -10.179405417124476, 5.696414143584243, -1.4014556169803851, 12.334288331422336, 11.690738709598888, -0.4279888274147221, 6.226618790084965, -10.904144874917206, -5.46919448234424, -5.647472225071031, -2.9801969751721744, -8.284532508545746, -3.3280413654836494, -2.2182526412098493, 0.4191036292473347, 3.0485450100607103, -0.20619687045226742, 2.1369938048939527, 3.730913391951158, 10.929856967963905, 8.301187183717643, -4.874133827873075, 0.7918650606624789, -8.520661635525103, -7.732342981599732, -6.494750491582618, -2.458749173402162, 3.573788336699224, 2.784339193089742, -1.6479372032388944, -0.35120747879544256] |
|||
console.log(CGA.mul(K, K)); // [-1] |
|||
[-7.83485413055467, -10.179405417124475, 5.696414143584248, -1.4014556169803827, 12.334288331422337, 11.690738709598893, -0.4279888274147213, 6.226618790084964, -10.90414487491721, -5.46919448234424, -5.647472225071032, -2.9801969751721726, -8.284532508545746, -3.3280413654836507, -2.218252641209847, 0.41910362924733874, 3.048545010060707, -0.20619687045226748, 2.136993804893955, 3.7309133919511575, 10.929856967963904, 8.301187183717648, -4.8741338278730755, 0.7918650606624811, -8.520661635525107, -7.732342981599734, -6.494750491582625, -2.45874917340216, 3.5737883366992262, 2.7843391930897443, -1.6479372032388935, -0.351207478795442] |
|||
console.log(CGA.mul(CGA.mul(I, J), K)); // [-1]</lang> |
|||
[-4.5157935996060425, -3.9762419076273514, -2.653425845411889, -1.2899302330562412, 6.161562884801266, 3.664812215240675, -0.4471521091019873, 2.39303455739218, -1.6486347268701103, 1.156714478904937, 4.5859158357958965, 6.879356425817299, 1.3341425863947358, 5.641350122882839, 6.378155334673649, 6.466962714879142, -3.645688408496504, -1.9659188980662032, 1.3062519818876646, 1.7973392350972788, 2.4770203476100843, 1.258017836002405, 1.3794942194985413, 3.993871627961031, -3.3620439843097127, -0.4228490927003264, 0.27245046364398495, 3.813642689561589, 2.6785051915908604, 5.409359105713415, 2.9578168177883555, 4.425426168284635] |
|||
[-4.515793599606042, -3.976241907627351, -2.653425845411889, -1.2899302330562417, 6.161562884801263, 3.664812215240676, -0.44715210910198766, 2.393034557392179, -1.6486347268701103, 1.156714478904937, 4.585915835795897, 6.8793564258172974, 1.3341425863947352, 5.641350122882839, 6.378155334673649, 6.466962714879143, -3.645688408496502, -1.9659188980662032, 1.3062519818876661, 1.7973392350972783, 2.4770203476100843, 1.258017836002407, 1.379494219498544, 3.99387162796103, -3.3620439843097127, -0.42284909270032545, 0.2724504636439853, 3.8136426895615894, 2.67850519159086, 5.409359105713415, 2.9578168177883555, 4.425426168284636] |
|||
[-5.8903316026132755, -6.619647679486295, -1.8140191326116537, -2.519531799741982, 6.604158362571294, 6.352401943423508, 0.9412086471616096, 3.719341486246096, -2.209111542028446, 1.9980997124233557, 5.717878641652222, 7.351597777237362, -2.9037939632499974, 1.497897713658653, 6.811544238648882, 5.861907187665564, -3.2638975880372363, -2.2659714695119115, 1.227221599808634, 0.8343365341022846, 2.72461491531054, 2.728833585944902, 2.226404227376565, 3.888097816250177, 0.35867175462798684, 2.3965356477571302, -1.7151608532791172, 1.403673323043394, -2.1441532262277607, 2.5435142440445646, 2.00110597707534, 1.9825972651495558] |
|||
[-5.8903316026132755, -6.6196476794862935, -1.8140191326116533, -2.5195317997419817, 6.604158362571292, 6.352401943423505, 0.9412086471616091, 3.719341486246094, -2.209111542028446, 1.9980997124233555, 5.71787864165222, 7.351597777237364, -2.9037939632499974, 1.4978977136586529, 6.81154423864888, 5.861907187665565, -3.263897588037235, -2.2659714695119124, 1.2272215998086353, 0.8343365341022843, 2.72461491531054, 2.7288335859449018, 2.226404227376565, 3.8880978162501783, 0.3586717546279864, 2.396535647757131, -1.715160853279117, 1.4036733230433938, -2.1441532262277603, 2.543514244044564, 2.0011059770753405, 1.9825972651495565] |
|||
⚫ | |||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |