News for hackers. Stuff that matters.
Re:How Quaternions encode rotations: derivation and sample code by jacobolus on Wednesday September 07, 2016 @05:30AM (#12441871)
Anyone interested in such topics should check out geometric algebra, a.k.a. Clifford algebra. Start with http://geocalc.clas.asu.edu/pdf/OerstedMedalLecture.pdf

Matrices and trigonometry are in my opinion a pedagogically, conceptually, and practically poor way of understanding and using quaternions; furthermore, cross products (and the associated confusion about a right hand rule and polar vs. axial vectors and so on, not to mention a total failure to generalize to higher dimensions) need to vanish from the earth.

More generally, this is a topic that should be explained first using pictures, ideally interactive ones. The algebraic manipulations to formally prove what the pictures explain visually should just fill in the bookkeeping details.

Finally, perhaps the most important reason geometric (Clifford) algebras should be used for pedagogy, even if you plan to write code which only uses quaternions per se, is that they make the “sandwich” multiplication of versors as operators on vectors completely obvious as composition of reflections.

Re:How Quaternions encode rotations: derivation and sample code by grondilu on Wednesday September 07, 2016 @05:32AM (#12441886)
Say what you want about geometric algebra, if it has a least one merit, it's that it makes rotations in 3D space and their link with quaternions very simple to understand.

Also check out my implementation in Perl 6:

https://github.com/grondilu/clifford

With it, quaternions become:

    use Clifford;

    constant I = @e[1]*@e[2];
    constant J = @e[2]*@e[3];
    constant K = @e[1]*@e[3];
    
    say I² == J² == K² == I*J*K == -1;
Re:How Quaternions encode rotations: derivation and sample code by logfromblammo on Wednesday September 07, 2016 @10:26AM (#12443632)
The subalgebra of 3D Clifford using the scalar and the 2-blades represents quaternions.

The subalgebra using the pseudoscalar and the 1-blades can also represent quaternions.

You have to be careful using other people's implementations, because certain operations are anticommutative, so sign conventions and operand ordering matters a lot more. For instance, choosing whether K is defined as e3 * e1 rather than e1 * e3 depends on whether your base vectors square to 1 or -1.

Geometric Algebra is cool. It lets you say things like "the intersection of two spheres that don't touch is a circle with negative radius." You can't even represent a circle with negative radius in regular geometry.

Re:How Quaternions encode rotations: derivation and sample code by grondilu on Wednesday September 07, 2016 @10:47AM (#12443821)
> You have to be careful using other people's implementations, because certain operations are anticommutative, so sign conventions and operand ordering matters a lot more. For instance, choosing whether K is defined as e3 * e1 rather than e1 * e3 depends on whether your base vectors square to 1 or -1.

Sure. For my implementation I thought a lot about how to deal with signatures and stuff. It's technically easy but making it simple and elegant is not obvious. I first thought about defining a @signature array, but then it occurred to me that it's much simpler to create two infinite spaces : one euclidean and one anti-euclidean. Later I've learned that Hestenes and others have considered this as well, they called it the mother of all geometric algebras, or universal geometric algebra[1]. Conveniently, Perl 6 deals with infinite lists very well, so it was very appropriate.

So in my implementation, @e[$i]² is +1 for all $i. If you want vectors with negative squares, use the @ē basis.

With geometric algebra, there are indeed several ways to represent quaternions. In fact there are several ways to represent complex numbers as well. I don't think it's a pitfall. As long as notations are clear and simple, it should be fine, I think.

1. https://en.wikipedia.org/wiki/Universal_geometric_algebra

Re:How Quaternions encode rotations: derivation and sample code by logfromblammo on Wednesday September 07, 2016 @11:50AM (#12444369)
No, not a pitfall. it is more like a small pothole in the road: the problem is easily avoided if you already know that it exists.
Re:How Quaternions encode rotations: derivation and sample code by stephencanon on Wednesday September 07, 2016 @11:27AM (#12444169)
> Geometric Algebra is cool. It lets you say things like "the intersection of two spheres that don't touch is a circle with negative radius." You can't even represent a circle with negative radius in regular geometry.

For some notion of "regular geometry". Desargues could produce this result in the early 17th century by lifting the circles into right cones in three dimensions, though he would probably tell you that the circle with negative radius was meaningless.

Re:How Quaternions encode rotations: derivation and sample code by jacobolus on Wednesday September 07, 2016 @08:47PM (#12449337)
Re:How Quaternions encode rotations: derivation and sample code by riboflava on Wednesday September 07, 2016 @01:10PM (#12445153)
I was going to ask how your implementation compares to Versor[0] but I see you link from it in your readme. My next question was going to be if there are any license issues, since Versor was for a long time only free for non-commercial use, but I see their github is a bit more recently updated than their home page and mentions it's now under the BSD (yay!). Lastly, are you concerned about the patent on the conformal method? [1]

There are a lot of great resources linked in this thread. My memory of the last time I took a dive into GA was that there was a split in the resources for representation -- one side tried to do everything axiomatically and symbolically, the other side had some concrete numbers but it was mysterious how they all worked together. Have you seen a super-basic resource that goes through the same sorts of modeling problems middle school and high school students do when first learning trig and classic geometry?

[0] http://versor.mat.ucsb.edu/

[1] https://www.google.com/patents/US6853964

Re:How Quaternions encode rotations: derivation and sample code by grondilu on Wednesday September 07, 2016 @01:30PM (#12445340)
> Lastly, are you concerned about the patent on the conformal method?

I did not know it was patented. My module does not specifically implement the conformal method, though. I did create two nul vectors and I called them no and ni, as in the conformal method, but that's about it. It's very much possible to do conformal geometric algebra with my module, but it's just not nearly as optimized as Versor is.

I've tried many times to implement Versor in Perl 6, but for various reasons I never succeeded. It's just kind of hard I guess. Also I'm not completely sure the optimization method I implemented is much worse, so it's hard for me to convince myself into doing the effort to improve it.

Also, I'm slightly skeptical the method used by Versor is the best that can be done, so again that dissuades me to do the effort.

I would love to see it done, though.

Re:How Quaternions encode rotations: derivation and sample code by blux on Wednesday September 07, 2016 @08:01AM (#12442575)
Some years ago Wildberger gave some excellent lectures explaining quaternions using rational geometry, which, if I am not mistaken, is related to Clifford algebra in the way it is presented.

Lectures start out by explaining the relation between complex numbers and rotations in the 2D plane, then discuss rotations in 3D using quaternions.

https://www.youtube.com/watch?v=uRKZnFAR7yw

https://www.youtube.com/watch?v=0_XoZc-A1HU

https://www.youtube.com/watch?v=g22jAtg3QAk

https://www.youtube.com/watch?v=MkNfQtINEjo

Re:How Quaternions encode rotations: derivation and sample code by imglorp on Wednesday September 07, 2016 @08:44AM (#12442880)
I've noticed Wildberger getting some grief about some of his unconventional ideas. I very much enjoy his lecture style, but as a layperson I'm never sure when he's straying away from accepted areas into controversial ones. Any feedback there?
Re:How Quaternions encode rotations: derivation and sample code by llamaz on Wednesday September 07, 2016 @10:18AM (#12443574)
The general consensus is that his videos are very good. It's just his insistence that the real numbers shouldn't be used that is not accepted. Some mathematicians (1% of 1% of mathematicians) agree with him, most don't.

edit: Doing math without the real numbers is less elegant and harder, but it's philosophically more sound. It essentially means restricting yourself to math that can be described as a (possibly infinite) algorithm.

The lay person is more likely to agree with what Wildberger has to say than a mathematician. In fact, most people think that infinity in math is a process (potential infinity). But in math, we use absolute infinity. A completed infinity. 0.999... does not tend to 1, it _is_ 1. Infinity is completed.

Re:How Quaternions encode rotations: derivation and sample code by blux on Wednesday September 07, 2016 @10:34AM (#12443708)
> It essentially means restricting yourself to math that can be described as a (possibly infinite) algorithm.

I'm confused. My impression is that it is the other way around where irrational numbers and operations like sine, square root etc. are infinite 'algorithms'?

Re:How Quaternions encode rotations: derivation and sample code by empath75 on Wednesday September 07, 2016 @01:14PM (#12445188)
Yeah, but there's a difference between saying there exists in reality some exact value for the square root of 2, and saying that it's possible to calculate the square root of 2 to arbitrary precision.
Re:How Quaternions encode rotations: derivation and sample code by blux on Wednesday September 07, 2016 @09:44AM (#12443286)
Yes, he sure is controversial. He does not acknowledge the existence of irrational numbers for example. In his lectures he avoids the use of any transcendental functions for the same reason, so no square roots, sine, or cosine.

I'm no mathematician, so I'm not qualified to comment on this either. Would love to hear other opinions on this too.

My personal opinion on this is that his rational approach does produce beautiful math, in the sense that it is really simple and intuitive.

Re:How Quaternions encode rotations: derivation and sample code by nabla9 on Wednesday September 07, 2016 @10:25AM (#12443626)
>the existence of irrational numbers for example.

I think most of cases of "non acknowledging" are deeply personal aesthetic considerations. One likes to play with one set of objects and only with them.

In the widest sense existence in mathematics means that mathematical object is well-defined and the system used is consistent (you can't derive contradictions).

Of course mathematicians are free do limit themselves into any subset of axioms or concepts they feel is "natural" or "real" and work only with them. But if they make philosophical arguments against other using irrational numbers, axiom of choice etc. I think they should argue that they are not consistent or well defined.

Re:How Quaternions encode rotations: derivation and sample code by jcranmer on Wednesday September 07, 2016 @11:51AM (#12444382)
> In his lectures he avoids the use of any transcendental functions for the same reason, so no square roots, sine, or cosine.

Square roots are algebraic, not transcendental.

Re:How Quaternions encode rotations: derivation and sample code by ColinWright on Wednesday September 07, 2016 @05:39AM (#12441924)
I am told that in some modules in the Cambridge undergraduate mathematics degree there are always questions that are trivial to solve using Clifford Algebras, but a complete pig in the usual formulations. I didn't take such a module so I can't comment from first-hand experience, but knowing some of the lecturers I can well believe it. The take-away is that they are a powerful tool in the right circumstances.

Whether they can replace the usual formulations in toto is another question, but they are certainly interesting.

Re:How Quaternions encode rotations: derivation and sample code by eggy on Wednesday September 07, 2016 @06:48AM (#12442201)
GA notation is compact, and it allows for more expressiveness when dealing with geometry. Rotors are pretty intuitive. The software libraries and hardware need to be put in place for it to find more practical use. There has been work on custom hardware, or implementing them in PROLOG [1 PDF].

Hestenes book "New Foundations in Classical Mechanics" is a great start, and there are books on GA in computer graphics. It is only a matter of time until there are solid software libraries and dedicated hardware for GA for it to really take off.

The current method is fine, and has done its job, however, when you read the history, and study GA, you realize the turn mathematics took. It was not based upon the better road, but on historical incidents.

[1 PDF] http://www.gaalop.de/dhilden_data/fccm09.pdf

Re:How Quaternions encode rotations: derivation and sample code by jacobolus on Wednesday September 07, 2016 @05:44AM (#12441946)
Anyone trying to study rotating objects (precession, fictitious forces in rotating reference frames, complicated problems about angular momentum, collisions of rigid objects etc.) will find that the geometric algebra approach cuts the work and conceptual overhead by an order of magnitude.

Here’s the book to work through for anyone who wants to deeply understand rotation in classical mechanics problems: http://geocalc.clas.asu.edu/html/NFCM.html

Re:How Quaternions encode rotations: derivation and sample code by jules on Wednesday September 07, 2016 @06:07AM (#12442035)
Do you have a link to a worked out example that is made much easier by this?
Re:How Quaternions encode rotations: derivation and sample code by jacobolus on Wednesday September 07, 2016 @06:21AM (#12442081)
Unfortunately Hestenes’s book is not online. I don’t know of any comparative examples online to link to.

I do still have vivid memories of working such problems out painfully using matrix/tensor methods in a college mechanics course 12 years ago though. :-)

Re:How Quaternions encode rotations: derivation and sample code by tostitos1979 on Wednesday September 07, 2016 @06:35AM (#12442144)
Is there a video lecture one can watch? What background is required to understand this?
Re:How Quaternions encode rotations: derivation and sample code by haskellgewurz on Wednesday September 07, 2016 @11:04AM (#12443975)
Re:How Quaternions encode rotations: derivation and sample code by edwintorok on Wednesday September 07, 2016 @08:07AM (#12442629)
Re:How Quaternions encode rotations: derivation and sample code by drostie on Wednesday September 07, 2016 @11:04AM (#12443971)
Thanks for that first link, it looks very promising. My problem is that I already idiosyncratically try to understand spacetime in terms of spin-vectors,* so this is going to take a bunch of trying to map out a Rosetta stone to translate between them!

* In brief, you can convert a world vector v^μ into a Hermitian 2D complex matrix V by adjoining σ_0 = I to the standard Pauli matrices σ_{1,2,3} and then V = σ_μ v^μ, when you do this you find that actually det V = v_μ v^μ under the (+ − − −) signature. This means that for nullvectors det V = 0 and therefore V is a projection V = φ φ^†, else V is merely Hermitian with eigenvalues v_0 ± √(v_1² + v_2² + v_3²) (because it has trace 2v_0) and there's not much more you can say. Lorentz transforms are linear and preserve Hermitianness and the determinant, with the parity-reversing stemming from V → -V and V → (det V) V^{-1} and the rest being V → L V L^† for some L such that det(L) = 1. The special case where L^† L = 1 (which is SU(2)) of course corresponds to 3D rotations because it preserves trace hence v_0, so if it's also preserving det V then it must be preserving the eigenvalues and hence the Euclidean metric √(v_1² + v_2² + v_3²). In turn L can be seen to act directly on the φ of the null vectors, φ → L φ, so call those "spin vectors" and confirm that they are spinors by finding out that L = -I for any 360-degree rotation. This is the most direct way I know of to see that the Lorentz transforms are the group SL(2, C) with this nice SU(2) group embedded in there. Then you can square-root the metric into an orientation tensor on the C^2 vector space as [0, -1; 1, 0] and build up the rest of the algebra in reverse from that starting point.

Re:How Quaternions encode rotations: derivation and sample code by jacobolus on Wednesday September 07, 2016 @08:50PM (#12449349)
You might find this clearer than other sources: http://arxiv.org/abs/1205.5935

Also check out http://geocalc.clas.asu.edu/pdf/LGasSG.pdf

Re:How Quaternions encode rotations: derivation and sample code by happy4crazy on Wednesday September 07, 2016 @08:29AM (#12442777)
For another resource, Alan Macdonald has some nice videos that accompany his books, "Linear and Geometric Algebra" and "Vector and Geometric Calculus": https://www.youtube.com/channel/UCymE67THrWoeTABxzJm1wdg/pla...
Re:How Quaternions encode rotations: derivation and sample code by contravariant on Wednesday September 07, 2016 @09:19AM (#12443117)
Cross products likely won't vanish, precisely because they're heavily linked to the Clifford algebra, and are essentially all you need for the most common case.

By the way, it is possible to generalize the cross product to higher dimensions, it just won't be a nice binary product, in n dimensions you need n-1 vectors to uniquely determine one orthogonal to all of them. Of course you can achieve pretty much the same thing with the Clifford algebra, or with wedge products.

Re:How Quaternions encode rotations: derivation and sample code by ittekimasu on Wednesday September 07, 2016 @12:03PM (#12444484)
This is a question of what it "means". It's origins can be traced to problems dealing with SO(n), and in this sense it's uniquely defined by the Lie-algebra of \mathfrak{so}(n), but in the Clifford-algebra world they become blades (more appropriate for CG).

I consider the cross-product to be one of the most abused objects in Physics; my first course in Newtonian mechanics was a nightmare, filled as it were, with the ghosts of all the characters killed so as to make the plot ostensibly "simple".