Skip to content
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

Mixed-unit vectors and matrices #1385

Closed
tobinjones opened this issue Sep 13, 2021 · 4 comments
Closed

Mixed-unit vectors and matrices #1385

tobinjones opened this issue Sep 13, 2021 · 4 comments

Comments

@tobinjones
Copy link

I'm working with some code that defines vectors and transformation matrices that operate on those vectors.

A simplified (2D) example is something like this:

# We have a particle with a position and angle
particle = array([1, 2]) # units are ['mm', 'mrad']

# What happens if we let the particle drift through free space for 2 meters?
transfer = array([[1, 2],[0, 1]]) # units are [['','mm/mrad'],['mrad/mm','']]

# We can represent this as a matrix equation
particle_out = transfer @ particle

The result is array([5, 2]), meaning the particle now has position "5 mm" and the same angle "2 mrad". But clearly if I got the units of the "particle" wrong and used units ['m', 'deg'] then my answer would be wrong.

In the 2D case, I might just explicitly write out the matrix math, and not use vectors/arrays. However in reality I'm interested in doing this for matrices that are up to 24x24.

Is there is any way to express this with Pint?

@jules-ch
Copy link
Collaborator

jules-ch commented Sep 13, 2021

No I don't think you can work with mixed units with quantities & keep numpy operations, just like you cannot work with mixed dtypes with numpy.
Best way is to go back to magnitude work with numpy arrays to combine & make computations on arrays & matrix where you ensure you have the right units with functions like to("mm") or m_as("mm").
Then you will construct back your Quantity.

Numpy have made some steps towards metadata inside DTypes (https://numpy.org/neps/nep-0041-improved-dtype-support.html) but even there you need to have one unit per arrays or matrix.

A custom function with @unit.wraps might be useful there.
Wraps is doing just that, converting quantities to magnitude, make computation & construct back your quantity.

@tobinjones
Copy link
Author

Ok thank you very much for the answer, I appreciate it! I will just keep Pint "at the edges" like you suggest.

Since it feels like from your response that this is solidly out-of-scope, I'm happy for this to be closed.

@jules-ch
Copy link
Collaborator

Closing this

@kbauer
Copy link

kbauer commented Nov 10, 2023

Related: I came across this trying to invert a matrix with mixed units. This appears e.g. with stiffness matrices for rigid bodies, which have a 3x3 submatrix with a unit pattern (using SI units).

The force equations read

f = K.q

where f, q are six-vectors of mixed dimensions (force and torque, distance and angle), resulting in an overall pattern (using SI units Newton, meters, seconds)

 N       N/m  N/m  N/m   N    N    N      m
 N       N/m  N/m  N/m   N    N    N      m
 N    =  N/m  N/m  N/m   N    N    N   .  m
N.m       N    N    N   N.m  N.m  N.m     1
N.m       N    N    N   N.m  N.m  N.m     1
N.m       N    N    N   N.m  N.m  N.m     1

For inverting, being unable to store units this way and perform operations on them, leads to significant added complexity or loss of the tracking of units of the properties. Mostly it means having to do the “units math” by hand.

(Side remark: Also ties in a bit to #1288, due to the question of what of these terms should have “rad” added, when being explicit about it, which when done badly could even make the matrix uninvertible due to the units, I think.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants