SymPy: How to return an expression in terms of other expressions (expressions)?

I am new to SymPy and have a question that might be basic. Or, I might just misunderstand 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 the other hand: can I find the official PDF (or other print version) of the full SymPy documentation? (I am under severe restrictions on using the Internet at work and am tired of working at home on weekends.)

Update:

This is what I got as a result of the Prelude proposal, but it is unlikely to like it so much, because it seems dirty. Comments and welcome to WTF.

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))

Using:

>>> x = in_terms_of('J',['gram','mile','hour'])
>>> x
'9765625000*mile**2*gram/(1951609*hour**2)'
>>> in_terms_of(x,['J'])
'J'
+3
3

sympy.physics.units, , , , , , , . .

5280 , 0,3048 .

, , - , .

, , :

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

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

, .

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

. , , .

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

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

ars . Sympy : https://github.com/sympy/sympy

docs README, , html-.

+2

sympy- expr1 expr2, :

In [120]: import sympy.physics.units as units

In [121]: expr1 = units.m / units.s

In [122]: expr2 = units.miles / units.hour

In [123]: a = (1397/3125) * expr1 / expr2

In [124]: a
Out[124]: 1

, . Quantities :

In [125]: import quantities as pq

In [126]: a = pq.Quantity(1397/3125, 'm/s')

In [127]: a
Out[127]: array(0.44703999999999999) * m/s

In [128]: a.units = pq.mile / pq.hour

In [129]: a
Out[129]: array(1.0) * mi/h

Unum.

PDF- sympy docs, Sphinx to PDF .

+1

quantities:

In [1]: from quantities import *
In [2]: v = 1397*m / (3125*s)
In [3]: v
Out[3]: array(0.44704) * m/s
In [8]: v.units = mile/hour
In [9]: v
Out[9]: array(1.0) * mi/h
+1
source

Source: https://habr.com/ru/post/1727968/


All Articles