New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implemented homogeneous option to Quaternion.to_rotation_matrix #24382
Implemented homogeneous option to Quaternion.to_rotation_matrix #24382
Conversation
Benchmark results from GitHub Actions Lower numbers are good, higher numbers are bad. A ratio less than 1 Significantly changed benchmark results (PR vs master) Significantly changed benchmark results (master vs previous release) before after ratio
[41d90958] [d505ea97]
<sympy-1.11.1^0>
- 984±3μs 630±1μs 0.64 solve.TimeSparseSystem.time_linear_eq_to_matrix(10)
- 2.79±0ms 1.18±0.01ms 0.42 solve.TimeSparseSystem.time_linear_eq_to_matrix(20)
- 5.58±0.01ms 1.72±0ms 0.31 solve.TimeSparseSystem.time_linear_eq_to_matrix(30)
Full benchmark results can be found as artifacts in GitHub Actions |
✅ Hi, I am the SymPy bot (v169). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.12. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
Either the parameter description or the docstring proper should probably have a description of what this option does. |
You're right, I completely forgot about is somehow. I added a line to explain this, but maybe it needs some more work! |
Can you help me find it on the page? I don't find the word "homogeneous" there. Are you using something that is common nomenclature when working with quaternions? Also, there is a table of conventions near the discussion of JPL convention. Which method does SymPy use? |
The formula I implemented can be found in Conversion to and from the matrix representation -> From a quaternion to an orthogonal matrix
I have seen the word homogeneous before but I can't find it again, maybe we could change the name. I might have read it in some journal article.
In this website (Derivation of Equations section) we can see how, during the derivation, first we get a matrix in which every element is the product between two quaternion elements, and then we get a matrix similar to the one defined originally in SymPy when supposing that This formula (the original form SymPy) makes sense for numerical purposes because it has less operations, but I don't think it makes much sense to use it for symbolic calculations! Maybe instead of the a parameter called |
@smichr As an extra reasoning for using the formula I presented here, declaring the following:
And then using the product to find the rotation (supposing
We can symbolically differentiate
Once again, this gives the same formula as the one I'm proposing (minus the normalization): So it would make the symbolic formula more consistent with the calculation of a rotation by the product with EDIT.: This can also be used to create a code generator for the rotation matrix, that can be copy-pasted to the rotation matrix function implementation:
Generates:
|
So this option is just giving an equivalent form of the expression: the rhs below. That would be what On the page you reference they say that the non-homogeneous form is "more efficient calculation in which the quaternion does not need to be unit normalized". Perhaps the flag could be called |
This is actually the same in general (except, of course, if the norm is zero)! By simply multiplying and dividing that lone I didn't really understand the |
Just changed the parameter from As a last step, I'd personally prefer that |
[21] simplifies to [22] and this is not the same as [23] (But for that matter, the normal of [21] is not [23], either.) But if there is no denominator on any of the arguments then the normal form is consistent with what I am not opposed to changing the default value since there is no guarantee of the format of the result, only the equivalency of it. And since it is the same for numbers and better for symbolic expressions, I don't think there would be any complaint. We can see if anyone else has an opinion. |
Ah alright, you meant in the context of sympy simplifications! I see what you mean! Alright! In the meantime, I think this is finished! So feel free to merge it or tell me to change the default value whenever you feel like it :) |
@smichr If you're curious, I found where I had read the word homogeneous, it was in a different page. |
Thanks for the ref to the use of "homogeneous". Given some precedence of the term use, I think it makes sense to change |
I'm not sure about the name anymore tbh, because it can be misinterpreted as a homogeneous 4x4 matrix... I agree with making it by default though! |
ok, if you want to change the function parameter and docstring we can see what fails (if anything) with the change. |
Switched it back to |
Brief description of what is fixed or changed
Added a bool parameter called
homogeneous
defaulting toFalse
.When
homogeneous == True
, a slightly different but equivalent definition of the rotation matrix is given.Other comments
This is another one in a series of small quality-of-life contributions I'm making to
Quaternion
.The "homogeneous" version of the rotation matrix can be slightly better for symbolic manipulation (its formula can be seen here).
To demonstrate this, the test function I implemented shows that the homogeneous version is equivalent to the original non-homogeneous version after being passed to
simplify
.As an interesting observation, the homogeneous version also has the property of being equivalent to a rotation + a scaling when the quaternion norm is ignored. If the norm is ignored in the non-homogeneous version, the rotation does not correctly represent anything.
Release Notes