Python 中的单位转换
SymPy 是一个在 Python 中进行单位转换的好工具:
>>> from sympy.physics import units
>>> 12. * units.inch / units.m
0.304800000000000
您可以轻松地推出自己的:
>>> units.BTU = 1055.05585 * units.J
>>> units.BTU
1055.05585*m**2*kg/s**2
但是,我无法将其实现到我的应用程序中,除非我可以将 C 度(绝对)转换为 K 度、F 度到 R 度,或其任何组合。
我想也许这样的事情会起作用:
units.degC = <<somefunc of units.K>>
但显然这是错误的道路。 对于在 SymPy 中干净地实现“偏移”类型单位转换有什么建议吗?
注意:我愿意尝试其他单位转换模块,但除了 Unum< /a>,发现很麻烦。
编辑:好的,现在很清楚我想做的是首先确定要比较的两个量是否在同一坐标系中。 (例如时间单位参考不同的纪元或时区或 dB 到直线幅度),进行适当的转换,然后进行转换。 有没有通用的坐标系管理工具? 那太好了。
我会假设 °F 和 °C 在表达式中始终指 Δ°F Δ°C,但单独使用时指绝对值。 我只是想知道是否有一种方法可以使 units.degF
成为一个函数,并在其上添加一个装饰器 property()
来处理这两个条件。
但现在,我将设置 units.C ==units.K
并尝试在文档中明确说明如何使用函数 convertCtoK(...)
和convertFtoR(...)
处理绝对单位时。 (开玩笑。不,我不会。)
SymPy is a great tool for doing units conversions in Python:
>>> from sympy.physics import units
>>> 12. * units.inch / units.m
0.304800000000000
You can easily roll your own:
>>> units.BTU = 1055.05585 * units.J
>>> units.BTU
1055.05585*m**2*kg/s**2
However, I cannot implement this into my application unless I can convert degrees C (absolute) to K to degrees F to degrees R, or any combo thereof.
I thought maybe something like this would work:
units.degC = <<somefunc of units.K>>
But clearly that is the wrong path to go down. Any suggestions for cleanly implementing "offset"-type units conversions in SymPy?
Note: I'm open to trying other units conversion modules, but don't know of any besides Unum, and found it to be cumbersome.
Edit: OK, it is now clear that what I want to do is first determine if the two quantities to be compared are in the same coordinate system. (like time units reference to different epochs or time zones or dB to straight amplitude), make the appropriate transformation, then make the conversion. Are there any general coordinate system management tools? That would be great.
I would make the assumption that °F and °C always refer to Δ°F Δ°C within an expression but refer to absolute when standing alone. I was just wondering if there was a way to make units.degF
a function and slap a decorator property()
on it to deal with those two conditions.
But for now, I'll set units.C == units.K
and try to make it very clear in the documentation to use functions convertCtoK(...)
and convertFtoR(...)
when dealing with absolute units. (Just kidding. No I won't.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Unum 文档有一篇很好的文章解释了为什么这很难:
从概念上很容易看出尝试用符号表示绝对温度转换的问题。 对于任何正常的相对单位,
(xunit) * 2 == (x * 2)unit
——单位数学是可交换的。 对于绝对温度,这种情况就不成立了——很难做比没有其他单位量纲的直接温度转换更复杂的事情。 您可能最好将所有计算保留为开尔文,并且仅在代码的入口和出口点与其他温度单位进行相互转换。The Unum documentation has a pretty good writeup on why this is hard:
It's pretty easy to conceptually see the problems with trying to represent absolute temperature conversion symbolically. With any normal relative unit,
(x unit) * 2 == (x * 2) unit
—unit math is commutative. With absolute temperatures, that breaks down—it's difficult to do anything more complex than straight temperature conversions with no other unit dimensions. You're probably best off keeping all calculations in Kelvin, and converting to and from other temperature units only at the entry and exit points of your code.我个人喜欢 Quantities,因为它的 NumPy 集成,但是它只处理相对温度,而不是绝对温度。
I personally like Quantities thanks to its NumPy integration, however it only does relative temperatures, not absolute.
例如,它是如何工作的:
Example, how it could work:
natu 包处理温度单位。 例如,您可以这样做:
也支持前缀:
natu 还可以处理非线性单位,例如 < a href="http://en.wikipedia.org/wiki/Decibel" rel="nofollow">分贝,不仅仅是那些带有偏移量的 摄氏度 和华氏度。
关于您给出的第一个示例,您可以这样做:
BTU 已经内置。您可以将其显示单位更改为 m**2*kg/s**2,但默认情况下 natu 简化单位为J:
The natu package handles units of temperature. For instance, you can do this:
Prefixes are supported too:
natu also handles nonlinear units such as the decibel, not just those with offsets like degree Celsius and degree Fahrenheit.
Relating to the first example you gave, you can do this:
BTU is already built in. You can change its display unit to m**2*kg/s**2, but by default natu simplifies the unit to J: