-
Notifications
You must be signed in to change notification settings - Fork 473
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
QuantityKinds #1967
base: master
Are you sure you want to change the base?
QuantityKinds #1967
Conversation
I got this behaviour working as I wanted: from pint import UnitRegistry
ureg = UnitRegistry()
Q_ = ureg.Quantity
moment_arm = Q_(1, "m").to_kind("[moment_arm]")
force = Q_(1, "lbf").to_kind("[force]")
# to_kind converts to the preferred_unit of the kind
assert force.units == ureg.N
# both force and moment_arm have kind defined.
# Torque is defined in default_en:
# [torque] = [force] * [moment_arm]
# the result is a quantity with kind [torque]
torque = force * moment_arm
assert torque.units == ureg.N * ureg.m
assert torque.kind == "[torque]"
# Energy is defined in default_en:
# [energy] = [force] * [length] = J
distance = Q_(1, "m").to_kind("[length]")
energy = force * distance
assert energy.kind == "[energy]"
assert energy.units == ureg.J
# Torque is not energy so cannot be added
with pytest.raises(ValueError):
energy + torque Here I've used an attribute One constraint is the multiplication of I imagine there's a better way to do the lookup when multiplying. What other examples would be good to test? |
Will need a Another thought is the order of operations could matter eg if specific energy is defined,
Would need to define shc as |
Added a I am thinking a QuantityKind would be worth adding, mainly so |
functionality to address #676 force = ureg.Kind("[force]")
# Valid units based on dimensionality
force.compatible_units()
# Kinds that when multiplied together with the specified exponents give ['force']
force.kind_relations()
newton = ureg.N
# Kinds that have the same dimenionality as the unit
newton.compatible_kinds()
joule = ureg.J
# A better example, (only force is defined with dimensions of kg m s-2)
joule.compatible_kinds()
|
@andrewgsavage Adding support for qunatity kinds would be great. Thanks a lot for starting this. Above you wrote
Then you added the The name I was expecting that QuantityKind instances support quantity methods, but when I tried I got
Other examples that I tried worked fine. Playing with this confirmed how useful quantity kinds are for correct handling of quantities in even more cases. |
There were a lot of I am liking the seperationso far;
Ah I hadn't realised that |
I found a c++ library thats being written with kinds. It has some good documentation on how it's dealing with kinds: And also points, vectors, tensors ... which they are calling character interestingly they are using a single class for quantity/quantitykind. They are also not automatically converting force*moment_arm into torque, but are allowing the result to be compared with torque, if I'm reading it correctly. |
Thanks for the great reference! It is the best in-depth text I ever read. They also mentioned the lack of kind handling in pint. |
Wouldn't it be an option to initialize Quantities directly with their kind? So instead of converting
initialize directly as
This would mean adding a new argument |
You can do Wha lt would you expect the result of a quantity* quantitykind to be? Should it error, or drop the kind? I saw that too. The thought that some units can only be of one kind was not obvious to me |
Another thing to note is the information they need for their definitions (
point/vector/tensor) isn't included in qudt. Adding kinds adds any more
definitions and relations which will take some work to set up. they dont
have a definition file parse like pint, units are defined in code.
…On Fri, 17 May 2024, 00:24 David Linke, ***@***.***> wrote:
Wouldn't it be an option to initialize Quantities directly with their kind?
So instead of converting
moment_arm = Q_(1, "m").to_kind("[moment_arm]")
initialize directly as
moment_arm = Q_(1, "m", "[moment_arm]")
This would mean adding a new argument kinds: Optional[KindLike] = None.
What disadvantage would this API have? It seems simpler but may have other
implications e.g. for performance.
—
Reply to this email directly, view it on GitHub
<#1967 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ADEMLEENFZEDYWCFLEOORQTZCUWZPAVCNFSM6AAAAABGBCOXUKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJWGMYDGMBYGE>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
pre-commit run --all-files
with no errorsReleveant issues: #1388 #551 #676 #1073