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 + torqueHere 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:
***@***.***>
|
|
The mp-units blog has a good series of articles on the international system of quantitites (ISQ) and quantitiy kinds. I did not yet look into how exactly they implement quantity kinds (it is in C++). |
pre-commit run --all-fileswith no errorsReleveant issues: #1388 #551 #676 #1073