Hacker News new | past | comments | ask | show | jobs | submit login

That library pisses me off on a visceral level.

The author "Enki Mute" is no doubt a brilliant mathematician, his library looks awesome, the online demos are unreal, but he's squandered his talent.

The code he writes is unreadable gibberish.

Zero useful comments. One-character variable names. Stringly-typed programming. Regex soup. It's psychotic.

I tried to reverse-engineer it in order to write a SIMD-optimised Rust version. But fuck me. It's like disassembling the machine code of an obfuscated anti-cheat library. Even with good tooling and industrial-strength refactoring, I couldn't make enough sense of it to make any useful headway.

Just try and read this. Seriously: https://github.com/enkimute/ganja.js/blob/master/ganja.js

What saddens me is that every other library I looked at was ancient and no longer maintained. Half wouldn't even compile. Most started with an explicit assumption that only 2D, 3D, or non-degenerate metrics are needed. Very few have efficient code-generation, and those that do are invariably C++ only, but I'm never going back there, even at gunpoint. Just shoot me now, I'd rather eat a bullet than face another STL compilation error that I have to decode from a linker error to do with __malloc() or some garbage I don't care about.

Enki Mute's Ganja.js is the only recent GA library I've seen that ticks all the checkboxes, but when I opened it up, I faced only the unspeakable horror of yet another dead end.

GA is going nowhere because everyone cooks up their own special version, the terminology is unique and special to each researcher, and most libraries stop just short of being useful.

What we need is standardisation around a well-documented and extensible library with polyglot code-gen. Something with expression simplification, SIMD, and both maths-centric and 3D graphics optimised capabilities.

Ganja.js is not it, unfortunately.




Are we looking at the same file?

It has masses of high quality comments.

I think calling this stringly typed is unfair. Basis names appear to be strings with some densly coded information in them, and can sometimes take the values "-1", "0" or "1". But they're not integers, and it matches the mathematical notation used very closely.

I see plenty of 1 character variable names, but the vast majority are either loop variables, or have a comment near the top of the file explaining their meaning, or are the only argument on a commented/named function.

I see plenty of regexes, but they are all extremely short matching patterns.

So it looks pretty good quality to me. Extremely densely written, typical for the "genius mathematician" programmer, but with some effort put into clarity.


Are we?

The comments are all of the style "now, this is the thing that is the thing". None explain anything at all other than to give names to variables or functions. Which you know... can already be named using identifiers in the language.

What are the matrices of constants? Where do they come from? What do they mean? What is their purpose? What are the random indexes into arrays? What do they do?

This is one of the first "comments":

// Documentation below is for implementors. I'll assume you know about Clifford Algebra's, grades, its products, etc .. // I'll also assume you are familiar with ES6. My style may feel a bith mathematical, advise is to read slow.

I don't know about you, but whenever I see a comment littered with typos that says "this is a bit hard to read", it's a sure sign that whatever is to follow is of low quality. Not reusable. Not suitable for widespread use. Not extensible. Not useful at all.

To reiterate: this makes me sad. I really wish it didn't, because GA is one of my favourite things, and this Enki Mute guy is clearly one of the few people that also "gets it" and wants to spread the good work. Clearly, his heart is in the right place.

I know people like this guy. I went to University with a chap who would casually litter his Haskell code with 5 dimensional arrays of functions. Now I'm sure that made sense in his head, and his code actually worked, but no human being on Earth other than him could read it. He couldn't explain to me even verbally, let alone in comments.

Unfortunately, while Enki Mute could have made a great contribution, his methodology has not helped GA become more approachable.


All the major arrays are named after mathematical constructs you can find defined elsewhere. There's examples of them in the docs, and the describe function dumps them out for you.

The hairiest parts of the code and where he deviates from mathematical convention have more in depth comments (e.g. simplify, simplify_bits, inline).

Much of meaning and purpose is explained by that top coment - go read about Clifford algebras elsewhere. That seems an entirely reasonable approach to me. Indeed, I read the wikipedia page briefly, and already the code made a great deal more sense to me. So I think his position is justified.

So I really cannot see how you see this falling short.

Perhaps this is about expectations. You obviously wanted code that teaches, as well as functions. Maybe you were also expecting more object orientation, more spread out code? Well, those were not part of the authors plan, but don't alone make it bad code.

Seeing as this functions as a code generator, frankly, the output is a better teaching tool - one can look at the rust generated for, say complex numbers and dual numbers, and immediately see which parts are in common, and which parts are different. That makes you think about what other things could fit the same template, which is basically what clifford algebra attempts to answer.


Right. uhm. glad to get the blood flowing ;)

Ganja.js is a personal project that got quite out of hand. Its primary goal was to lower the threshold for people to discover Geometric Algebra through examples and see its coordinate-free approach to geometry in action on the web. (since there was, and is, literally nothing else out there.)

Javascript is not the ideal language for that. Without operator overloading, much of the charm is lost - because of this, ganja contains a minimal transpiler (which is what all the regular expressions are for). This is purely out of necessity, as in practice I feel one needs a language with operator overloading for a GA implementation to be useful.

Furthermore, to be able to provide demo's in a wide range of Algebras, ganja contains three different algebra generators. (each of which produces the code that actually implements the algebra). This is another choice that typically does not exist for any specific use case (one would pick an approach and stick to it - like e.g. the Klein library) - and that adds a lot of complexity.

To visualize results, it also contains a range of different visualizers (2D, 3D, projective, conformal, SVG, GL, implicit, ..), none of which would be needed in a generic implementation. This again adds complexity that has no place in a 'reference' implementation. (which ganja was never intended to be).

In short, a lot of trickery was needed for ganja to reach its primary goal: clean examples that showcase how GA can simplify a wide range of applications. It certainly is not intended as a 'how-to' or reference for an Algebra generator, and when preparing for the Siggraph course, I saw no opportunity to use it as such. Instead, I opted to create separate reference implementations that are available at https://bivector.net/tools.html. (for c++, rust, python, c#)

With the increased interest, I did start a rewrite of ganja.js, and one that is not for my brain only.

Cheers.


I agree, this code is horrendous! It is like trying to read one of those code golf answers. Author seems to pride themselves on a proficiency for packing as much as possible in to one line, which is not a good thing in my opinion. I do think it was reasonably well commented though compared to code that I am used to working on (in games). At least they expressed basically what they were trying to do before each block of code, with references to specific algorithm names etc. The single letter variables didn't bother me as much in this context since a lot of those seemed to be loop indices which are only used in a small scope, or mathematical quantities that you would be familiar with from reading some algorithms standard description but they don't have an easy way to express in English. Definitely agree with you about all the regex nonsense though. To me that has no place in a math related library


As an aside: I knew a maths major student who would keep his entire year of handwritten lecture notes on a single A4 page of paper. It always fit! The Ganja.js coding style reminds me of that guy.

On a more serious note, GA computing has several orthogonal aspects to it, some of which have very different programming requirements:

* Converting GA expressions, e.g.: "x ^ e_o times y ^ e_1" into a single expression.

* The above requires either an expression parser, or an AST accessible from code, or both.

* A general multivector type at compile time to compute the results of those expressions. This one doesn't require any special optimisation.

* An expression simplification library, like a mini computer algebra system (CAS) to simplify the resulting elements after all of the high-level geometric products are carried out. This is important to eliminate a bunch of multiplications by zero, double negation, etc...

* A code-generation module to produce the final, optimised library for the runtime. This has to have a bunch of features to be competitive with Vector algebra 3D libraries. Many of these features are output language dependent. For example, it makes sense to have packed multi-vectors, sparse multivectors, a native array-of-multivectors type, etc...

In my mind, there are two main types of code in such a library: The first is the pure functional maths stuff, which is mostly the computation of some lookup tables and lists of lists of things. This is mostly static and needs standardisation. The second type of code is typically impure and needs to have a clean API to make it extensible/pluggable: parsing, code-gen, SIMD, optimisation, etc... E.g.: SIMD changes over time, there may need to be multiple optimisation passes, you may want to interact with an imperative compiler interface like LLVM or Roslyn, etc...

One of the issues I have with Ganja.js (that isn't about syntax formatting) is that is interleaves these largely orthogonal concepts. The strings from the parser flow through much of the code, making the entire thing a stringly-typed mess.

Enki Mute is clearly a mathematician, not a software developer. I can tell he's never had to write code intended to be used by others.

While these disciplines have a lot of overlap, CS is mostly about cooperating successfully with other humans. Mathematics is mostly about correctness... and that's about it. You can publish whatever you want, it doesn't have to have doc-comments, it doesn't need to be modular, or reusable, or anything other than not wrong and maybe interesting.


Check out Versor. If you need an easier-to-understand version, Versor_1_0 (in another repo, by the same Author) is basically just plain old C.


Jeebus, I agree. There’s a lot in there that I’d never pass in a code review.

There’s no tests, either. As in, if those were in place it would be an arduous but straightforward task to deobfuscate the mess and make sure the tests pass.


The code and the comments are fine. Maybe you should learn a bit more about the domain before trying to port Ganja.js to Rust.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: