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

Order units #2086

Open
SpazzNon opened this issue Dec 5, 2024 · 3 comments
Open

Order units #2086

SpazzNon opened this issue Dec 5, 2024 · 3 comments

Comments

@SpazzNon
Copy link

SpazzNon commented Dec 5, 2024

Hi, when I multiply units of force with length or its simplification results in force times length the result always gives in order length times force, I would like that to change because I am using handcalcs and generate reports so it is unusual to read length times force, when I am used to it being force times length. Attached is the configuration.

from IPython.display import Latex

import handcalcs.render
import pandas as pd
pd.options.display.float_format = "{:.2f}".format
from math import sqrt, sin, tan, pi,cos
import pint
un = pint.UnitRegistry(auto_reduce_dimensions=True)
preferred_units = [
    un.m,
    un.kg,
    un.s, 
    un.K,
    un.kN,
    un.W,
    un.kip,
]

un.default_preferred_units = preferred_units
un.define('tonf = 1000 * kg * 9.80665 * m / s**2')
un.define('kgf = 1 * kg * 9.80665 * m / s**2')
#un.define('ksi = 1 * kg * 1* m / s**2*6894757.29316836/m**2')
un.define('ksi = 1 * kip / inch**2')

m= un.m
mm= un.mm
inch = un.inch
MPa = un.MPa
tonf= un.tonf
kgf= un.kgf
cm = un.cm
ksi = un.ksi
kip = un.kip
lb= un.lb
ft= un.ft
N= un.N

un.default_preferred_units = preferred_units
handcalcs.set_option("preferred_string_formatter", "~L")```
@andrewgsavage
Copy link
Collaborator

this is possible, here's an example. it needs adding to the documentation.

>>> import pint
>>> ur = pint.UnitRegistry()
>>> ur.formatter.dim_order
('[substance]', '[mass]', '[current]', '[luminosity]', '[length]', '[]', '[time]', '[temperature]')
>>>
>>> from pint.delegates.formatter._compound_unit_helpers import sort_by_dimensionality
>>> ur.formatter.default_sort_func = sort_by_dimensionality
>>>
>>> Q_=ur.Quantity
>>> res=Q_(1,"m")*Q_(1,"N")
>>> str(res)
'1 newton * meter'
>>>
>>> ur.formatter.dim_order = ('[temperature]', '[time]', '[]', '[length]', '[luminosity]', '[current]', '[mass]', '[substance]')
>>>
>>> Q_=ur.Quantity
>>> res=Q_(1,"m")*Q_(1,"N")
>>> str(res)
'1 meter * newton'
>>>

@SpazzNon
Copy link
Author

SpazzNon commented Dec 6, 2024

this is possible, here's an example. it needs adding to the documentation.

>>> import pint
>>> ur = pint.UnitRegistry()
>>> ur.formatter.dim_order
('[substance]', '[mass]', '[current]', '[luminosity]', '[length]', '[]', '[time]', '[temperature]')
>>>
>>> from pint.delegates.formatter._compound_unit_helpers import sort_by_dimensionality
>>> ur.formatter.default_sort_func = sort_by_dimensionality
>>>
>>> Q_=ur.Quantity
>>> res=Q_(1,"m")*Q_(1,"N")
>>> str(res)
'1 newton * meter'
>>>
>>> ur.formatter.dim_order = ('[temperature]', '[time]', '[]', '[length]', '[luminosity]', '[current]', '[mass]', '[substance]')
>>>
>>> Q_=ur.Quantity
>>> res=Q_(1,"m")*Q_(1,"N")
>>> str(res)
'1 meter * newton'
>>>

Thank you very much for your prompt reply; I tried it with your proposal and it worked perfectly, but when I tried it with ‘inch’ and ‘kip’ and it stopped working, the ‘inch’ is always put in front and I don't understand why.

@andrewgsavage
Copy link
Collaborator

the function sort_by_dimensionality ordered based on the index of the dimensionality of each unit in the dim_order tuple, see

def sort_by_dimensionality(

you could write your own function to sort in the way you desire. see the sort_by_dimensionality for an example

this would be something good to add to pint; the functionality to provide a list of units and a function that sorts based on that list, analagous to the dim_order

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

No branches or pull requests

2 participants