/* Produce a quaternion from an axis-angle representation.*/
function quat_from_axis(theta: number, x: number, y: number, z: number) {
const halfcos = Math.cos(theta * 0.5);
const halfsin = Math.sin(theta * 0.5);
return new Quaternion(
halfcos,
axis[0] * halfsin,
axis[1] * halfsin,
axis[2] * halfsin,
);
}
Function is vibe coded. fn exp(q: Quat) -> Quat {
let ijk = sqrt(q.i*q.i + q.j*q.j + q.k*q.k);
Quat {
i: exp(q.l) * sin(ijk) * q.i / ijk,
j: exp(q.l) * sin(ijk) * q.j / ijk,
k: exp(q.l) * sin(ijk) * q.k / ijk,
l: exp(q.l) * cos(ijk),
}
}
fn log(q: Quat) -> Quat {
let ijk = sqrt(q.i*q.i + q.j*q.j + q.k*q.k);
Quat {
i: atan2(ijk, q.l) * q.i / ijk,
j: atan2(ijk, q.l) * q.j / ijk,
k: atan2(ijk, q.l) * q.k / ijk,
l: 0.5 * log(q.i*q.i + q.j*q.j + q.k*q.k + q.l*q.l),
}
}The code is correct aside from the obvious bug.
Bugs predate the existence of AI by decades. This particular bug doesn't look like one an AI would write.
Also it's a short function. It's impossible to miss unused parame if you read your own code at all. And if you somehow don't... then it's deserved anyway.
It could make that mistake if it writes the header at one point, and the body separately. Though that's unlikely.
But I suspect this is a genuine human error.
HN: Show HN Awesome New LLM can Code!! Also HN: TFA is vibe coded angry pitchfork or ugh
https://en.wikipedia.org/wiki/Invariant_extended_Kalman_filt...
https://trumpf.id.au/pubs/Hua_Zamani_Trumpf_Mahony_Hamel_CDC...
Personally, I think one is better off learning some Differential Geometry and Abstract Algebra to really appreciate the broader context in which these exist.
mci•5mo ago
A unit quaternion represents a rotation in R³.
A quaternion represents the quotient of two vectors in R³. That's what Hamilton had in mind.
xeonmc•5mo ago
xelxebar•5mo ago
Trying to think about this stuff can be mind-bending, since it's natural to try to visualize things. So to give a little intuition, consider the coordinates of a 4D vector (x,y,z,w) and notice that we can spin around in both the x-y and z-w planes at the same time without interfering with each other!
nextaccountic•5mo ago
How does it make sense to divide a vector by another? Can we at least multiply two vectors as well? (So that if the quaternion q is the vector a divided by the vector b, then q times b is a)
reikonomusha•5mo ago
In Hamilton's original view of quaternions, he defined a "geometric quotient" of two 3d directed lines (one kind of object) as being a quaternion (another kind of object), and gave all sorts of complicated geometric formulas for how to calculate it.
xelxebar•5mo ago
For example, we can take the "difference" of two points to get a vector, i.e. the grocery store is in that direction, while it doesn't make much sense to add two points---this location plus the grocery store? Similarly with temperature (of a given scale), today can be 5 degrees hotter than yesterday, but saying "the total temperature between yesterday and today is 57 degrees" doesn't have a clear meaning.
So, in that vein, what operation transforms one 3D vector into another?
Well a combination of rotation and scaling, of course! There is some freedom in how you want to represent this scaling+rotation operation; we could use 3x3 matrices, meaning that the quotient of 3D vectors is reasonably described by a matrix. However, 3x3 matrices have 9 degrees of freedom, which is way overkill, since we just need 4 numbers: an axis (2 params), an angle, and a scale factor. For this reason, quaternions are a particularly natural representation of 3D vector division.
The general concept of division that we're getting at here has the terrible name "g-torsor". If you're not familiar with abstract algebra, though, the definition is not very enlightening. However, John Baez has a really accessible article[0] that comes to the rescue!
Side note: notice that in general, the results of division are different objects than the things doing the division. As such, it's somewhat unfortunate that we use the same units for both in many cases, e.g. temperature and temperature differences are really different things. Talking about units quickly turns into type theory, though, so I'll stop there.
[0]:https://math.ucr.edu/home/baez/torsors.html
godelski•5mo ago
This is all about how you abstractly define things like addition and multiplication. We'll call these actions an "operator". So you got these basic algebraic structures:
There's a lot more structures, but what we often really want is this Field thing. Our normal 1D numbers work work like that, the "every day" type of math. But think for a second, how do you do this same math in 2D? How do we make ab consistent? Well, you can't. At least not with our standard +,. So we introduce imaginary numbers. These numbers aren't imaginary so much as we're just changing the rule on +,* (∘,▪) to work differently. So we use imaginary numbers in 2D because it creates a field! You literally could do this with tuples as long as you remember the i^2 rule.Now we extend this to 3D. Ops, we get a problem. Really no way to make ▪ () consistent here, especially around division. Bummer. We have a Ring.
But going up to 4D we can use a similar rule as to what we did in 2D and get a Field again! Now we can do consistent math. And this is actually why quaternions become useful for 3D rotation. But they can do a lot more, though come with some limitations as well.
This is super high level and just scratches the surface of all of this stuff but I hope it at least makes some sense here. Please ask questions and I'm also sure someone else will add some useful comments. But your question is completely normal and actually gets to the motivation of why kids these days are learning the difference between 3
9 and 93, despite having the same answer they do mean different things and understanding that earlier helps you when you get to abstract nonsense.[0] It is taught later because this is when you first start getting into math as being about abstractions. You basically learn that you only ever learned one very specific kind of math and that all that breaks down. Your question is absolutely on point due to this. Like how you probably learned matrix multiplication and learned that AB ≠ BA
[1] Whatever ∘,▪ these will usually be referred to as "addition" and "multiplication" but just note that they aren't the same thing you're probably thinking of
[2] Let's return to less abstraction for a second. We have + and
for (standard) addition and multiplication, right? 0 is the identity element for + and note that 0 doesn't have an inverse for multiplication. That is: we can't do a * (0^-1) = a/0. Note here that / is a shorthand for * with an inverse operator.