enkimute / ganja.js

:triangular_ruler: Javascript Geometric Algebra Generator for Javascript, c++, c#, rust, python. (with operator overloading and algebraic literals) -
MIT License
1.52k stars 107 forks source link

Dual of 1 returns false #170

Open deanrobertcook opened 6 months ago

deanrobertcook commented 6 months ago

Hi, thanks first off for this impressive library. It's been a joy playing around with it so far.

I found the gluLookAt example recently on observable and wanted to try it out on the actual camera used in a 3DPGA setup. I copied over the align function:

var align = (p,q)=>{
  for (var i=0, M=1, P=!1, Q=!1; i<p.length; ++i) {
    P = Q & (M >>> p[i]);
    Q = Q & q[i];
    M = (1 + Q.Normalized / P.Normalized).Normalized * M;
  }
  return M
}

But noticed I was getting NaNs in the output multivectors. After digging around a bit, I realised that !1 returns false, so P and Q aren't initialized to the pseudoscalar, as expected.

The line in question seems to be:

static Dual(a) { if (typeof a=='boolean' || typeof a=='number') return !a; return Element.toEl(a).Dual; };

So returning false seems to be the intended behaviour.

For now, I found a workaround by just using Element.toEl(1).Dual directly, but I wanted to raise this issue in case anyone else bumps into it. I'm not sure if it's possible to remove the typeof a =='number' check without unintended consequences, but it would be nice to have a consistent dual operation for 1, especially if we shouldn't use the pseudoscalar directly if it's degenerate?

Thanks in advance!