SymPy: How to return an expression in terms of other [removed]s)?

前端 未结 3 1489
無奈伤痛
無奈伤痛 2021-01-03 05:40

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

3条回答
  •  渐次进展
    2021-01-03 05:56

    In sympy units you convert from expr1 to expr2 by dividing:

    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
    

    The only issue is that you end up with a dimensionless quantity. You might also consider the Quantities package for this type of thing:

    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
    

    There's also Unum.

    I don't know of a PDF version of the sympy docs, but you might be able to check the distribution out from their repository and use Sphinx to generate a PDF for yourself.

提交回复
热议问题