如何根据其他表达式返回表达式

如何根据其他表达式返回表达式

本文介绍了SymPy:如何根据其他表达式返回表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 SymPy 还很陌生,我有一个可能是基本问题的问题.或者我可能只是误解了应该如何使用 SymPy.

有没有办法创建一个不是由原子表示的表达式,而是由其他表达式的组合来表示的?

示例:

>>>从 sympy.physics.units 导入 *>>>expr1 = 米/秒>>>expr2 = 英里/小时>>>表达式1多发性硬化症>>>表达式21397*米/(3125*秒)>>>expr1.in_terms_of([mile,hour]) #在我的梦里?3125*英里/(1397*小时)>>>

附带说明:我可以找到完整 SymPy 文档的官方"PDF(或其他可打印)版本吗?(我在工作中受到严格的互联网使用限制,并且厌倦了周末在家工作.)

更新:

这是我最终遵循 Prelude 的建议的结果,但是它不太可能像这样使用,因为它感觉很脏.欢迎评论和 WTF.

def in_terms_of(self, terms):expr2 = eval(str(self),physics.units.__dict__)转换器 = {}术语:term_expr =physics.units.__dict__[term]coeff = term_expr.as_coeff_terms()[0]unit =physics.units.Unit('my_'+term, term)转换器[term_expr/coeff] = 单位/系数返回 str(expr2.subs(converter))

用法:

>>>x = in_terms_of('J',['gram','mile','hour'])>>>X'9765625000*英里**2*克/(1951609*小时**2)'>>>in_terms_of(x,['J'])'J'
解决方案

检查 sympy.physics.units 你可以看到所有的单位都是用米、公斤、秒、安培、开尔文、摩尔和坎德拉定义的.这些是基本单位.

那么一英里定义为 5280 英尺,一英尺定义为 0.3048 米.

因此,所有使用非基本单位的表达式都会将非基本单位替换为基本单位.

您可以定义自己的单位,当您需要一个表达式来使用特定单位时,您可以将这些单位代入表达式中:

import sympy.physics.units 作为单位从 sympy 导入 Rationalmy_mile = units.Unit('my_mile', 'mile')my_hour = units.Unit('my_hour', '小时')

然后定义一个字典,用基本单位代替新单位.

converter = {units.m: my_mile/Rational('1609.344'),单位.s:my_hour/Rational('3600')}

使用基本单位执行所有计算.然后,如果您想要使用英里和小时的值,您可以将新单位代入表达式中.

v = 10*units.miles/units.hour打印 v # = 2794*m/(625*s)打印 v.subs(converter) # = 10*英里/小时

使用 ars 答案获取文档.Sympy 的存储库在这里:https://github.com/sympy/sympy

docs 文件夹中有一个 README 文件,描述了如何创建 html 文档.

I'm fairly new to SymPy and have what might be a basic question. Or I might simply be misinterpreting how SymPy is supposed to be used.

Is there a way to create an expression that is not represented by atoms, but by a combination of other expressions?

Example:

>>> from sympy.physics.units import *
>>> expr1 = m/s
>>> expr2 = mile/hour
>>> expr1
m/s
>>> expr2
1397*m/(3125*s)
>>> expr1.in_terms_of([mile,hour]) #in my dreams?
3125*mile/(1397*hour)
>>>

On a side note: Can I find an 'official' PDF (or other printable) version of the complete SymPy documentation? (I'm under draconian Internet usage restrictions at work and am tired of doing work at home on the weekend.)

Update:

This is what I ended up with following Prelude's suggestion, however it's not likely to get much use like this as it feels dirty. Comments and WTF's welcome.

def in_terms_of(self, terms):
    expr2 = eval(str(self), physics.units.__dict__)
    converter = {}
    for term in terms:
        term_expr = physics.units.__dict__[term]
        coeff = term_expr.as_coeff_terms()[0]
        unit = physics.units.Unit('my_'+term, term)
        converter[term_expr/coeff] = unit/coeff
    return str(expr2.subs(converter))

Usage:

>>> x = in_terms_of('J',['gram','mile','hour'])
>>> x
'9765625000*mile**2*gram/(1951609*hour**2)'
>>> in_terms_of(x,['J'])
'J'
解决方案

Checking the source for sympy.physics.units you can see that all units are defined in terms of meters, kilograms, seconds, amperes, kelvins, moles and candelas. These are the base units.

Then a mile is defined as 5280 feet, and a foot is defined as 0.3048 meters.

So all expressions using non-base units will have the non-base units replaced with the base units.

You can define your own units which you can subsitute into an expression when you need an expression to use particular units:

import sympy.physics.units as units
from sympy import Rational

my_mile = units.Unit('my_mile', 'mile')
my_hour = units.Unit('my_hour', 'hour')

Then define a dictionary which will substitute the base units for your new units.

converter = {units.m: my_mile/Rational('1609.344'),
             units.s: my_hour/Rational('3600')}

Perform all your calculations using the base units. Then if you want a value using miles and hours, you can substitute your new units into the expression.

v = 10*units.miles/units.hour
print v # = 2794*m/(625*s)

print v.subs(converter) # = 10*mile/hour

Use ars answer for getting the docs. Sympy's repository is here: https://github.com/sympy/sympy

There is a README file in the docs folder which describes how to create html docs.

这篇关于SymPy:如何根据其他表达式返回表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:14