Bug in generator

On https://bivector.net/tools.html I selected 2, 0, 1 algebra, and downloaded the python file. It’s missing subs method which is used in sub method. I’d open an issue on github but I’m not sure which repo holds this generator - it would be nice to link to the github repo from https://bivector.net/tools.html.

The file that generates the python code is https://github.com/enkimute/ganja.js/blob/master/codegen/py.template.js.

Having said that, if you want to work with GA in python and not just tweak a reference implementation, I’d recommend using pip install clifford (disclaimer: I am one of the maintainers)

1 Like

While I should (and will) obviously fix that bug in the generator, I fully agree with @eric-wieser here - these reference implementations are intended to fill a void. They are for that phase where you’ve just discovered GA and need a minimal view that just shows you what gets multiplied where. They intentionally ignore all performance and other design considerations. For any actual applications, we have a list of actively maintained libraries that do attempt to be practically usable.

For python, that is clifford. I’m not a maintainer. Its good stuff.

1 Like

In defense of the code generator, the PyClifford library install can fail rather easily due to its dependencies (particularly numba, which fails for me on several new Win 10 64 installations with the current version of pip). Also, in my use case having a small amount of pure python code is important, since I am working inside my CAD software’s IDE which does not support external python libraries.

I wish there was a native python version of Ganga or some effort to allow for a dependency free version of PyClifford.

This bug has been fixed. Thanks for the heads-up.

Cheers,

Steven.

1 Like

If this happens to you again, can you open a github issue with the error log? It should just work out of the box.

1 Like

Definitely! It’s not a problem with PyClifford itself as far as I can tell though, but with numba when used with pip.

Just so you know, there is a bug in the C# PGA generated code:

BiVector.net, 2D Projective Geometric Algebra, pga2d.cs, line 336

public float inorm() { return this[1] != 0.0f ? this[1] : **this[15] != 0.0f ? this[15]** : (!this).norm(); }

Change to:

public float inorm() { return this[1] != 0.0f ? this[1] : **this[7] != 0.0f ? this[7]** : (!this).norm(); }

_mVec only has 8 floats in 2D PGA

I came here to report that the generator BiVector.net does not work for Rust. The code generated for 3D Proective Geometry Algebra fails to build with:

error[E0405]: cannot find trait `ssub` in this scope 

And similarly for subs.

Thinking that I would attempt to provide the missing traits I thought to try the generated Python so as to have some results to compare to. Sadly that fails with a division by zero:

 ✗ python3 pga3d.py 
Traceback (most recent call last):
  File "/Users/michaael/conveqs/pga32d_test/pga3d.py", line 473, in <module>
    ROT = rotor(math.pi / 2.0, E1 * E2)
  File "/Users/michaael/conveqs/pga32d_test/pga3d.py", line 432, in rotor
    return math.cos(angle / 2.0) + math.sin(angle / 2.0) * line.normalized()
  File "/Users/michaael/conveqs/pga32d_test/pga3d.py", line 427, in normalized
    return a * (1 / a.norm())
ZeroDivisionError: float division by zero

As a last resort I though to try the generated C++ which at least builds but produces gibberish results:

✗ gcc pga3d.cpp  
✗ ./a.out                 
a point       : 1e032 + 1e123
a line        : -1e23
a plane       : -3e0 + 2e1 + 1e3
a rotor       : nane0 + nane1 + nane2 + nane3 + nane01 + nane02 + nane03 + infe12 + nane31 + nane23 + nane021 + nane013 + nane032 + nane123 + nane0123
rotated line  : nane0 + nane1 + nane2 + nane3 + nane01 + nane02 + nane03 + nane12 + nane31 + nane23 + nane021 + nane013 + nane032 + nane123 + nane0123
rotated point : nane0 + nane1 + nane2 + nane3 + nane01 + nane02 + nane03 + nane12 + nane31 + nane23 + nane021 + nane013 + nane032 + nane123 + nane0123
rotated plane : nane0 + nane1 + nane2 + nane3 + nane01 + nane02 + nane03 + nane12 + nane31 + nane23 + nane021 + nane013 + nane032 + nane123 + nane0123
point on plane: nane0 + nane1 + nane2 + nane3 + nane01 + nane02 + nane03 + nane12 + nane31 + nane23 + infe021 + nane013 + infe032 + infe123 + nane0123
point on torus: nane0 + nane1 + nane2 + nane3 + nane01 + nane02 + nane03 + nane12 + nane31 + nane23 + nane021 + nane013 + nane032 + nane123 + nane0123
1e0
-1e0

I have only discovered there was such a thing as Geometric Algebra a few days ago and was hoping to use this code to play with as an aid to understanding.

OK. So I discovered GitHub - enkimute/ganja.js: 📐 Javascript Geometric Algebra Generator for Javascript, c++, c#, rust, python. (with operator overloading and algebraic literals) - and notice the generated code looks very similar to the online generator, all be it full with a lot of extra macros.

Sadly The generate Rust and C++ code produces different results:
Rust:

✗ ./pga3d              
a point       : 1e032 + 1e123
a line        : -1e23
a plane       : -3e0 + 2e1 + 1e3
a rotor       : 0.7071068 + 0.7071068e12
rotated line  : 1e31
rotated point : -1e013 + 1e123
rotated plane : -3e0 + -2e2 + 1e3
point on plane: 0.2e021 + 1.4e032 + 1e123
point on torus: 0.85e032 + 1e123
-1 + 1e0
1 + -1e0

C++:

✗ ./a.out      
a point       : 1e032 + 1e123
a line        : -1e23
a plane       : -3e0 + 2e1 + 1e3
a rotor       : 0.7071068 + 0.7071068e12
rotated line  : 1e31 + 7.671892e-08e23
rotated point : 2.503258e-08e3 + -1e013 + -7.671892e-08e032 + 1e123
rotated plane : -3e0 + -1.939635e-07e1 + -2e2 + 1e3 + 7.509773e-08e021 + 2.503258e-08e123
point on plane: 0.2e021 + 1.4e032 + 1e123
point on torus: 0.85e032 + 1e123
-1 + 1e0
1 + -1e0

The Python code agrees with the Rust.

✗ python3 pga3d.py 
a point       : 1e032 + 1e123
a line        : -1e23
a plane       : -3e0 + 2e1 + 1e3
a rotor       : 0.7071068 + 0.7071068e12
rotated line  : 1e31
rotated point : -1e013 + 1e123
rotated plane : -3e0 + -2e2 + 1e3
point on plane: 0.2e021 + 1.4e032 + 1e123
point on torus: 0.85e032 + 1e123
-1 + 1e0
1 + -1e0

So now I’m not sure what is what!