Render an Expression containing Python variables without symplifying or evaluating it.

38 views
Skip to first unread message

poofjunior

unread,
May 28, 2016, 3:40:45 PM5/28/16
to sympy
Hi all,

Often in Python, I'll punch in an equation based on constants like this:

mass = 10
acceleration = 9.8
print(mass * acceleration)

I'd love to be able to use Sympy to find/replace these variables in an expression and then print the unicode result. For me I'd appreciate being able to see the nicely rendered expression to make sure I typed it in correctly.

vars_dict = {"mass":10,
             "acceleration": 9.8}
render_expression("mass * acceleration", vars_dict)

where the output looks like the following:

    9.8⋅10

Or.. something more complicated:

constants = {"lbs_per_bucket": 4,
             "lbs": 2,
             "lbs_friend": 3}
render_expression("(lbs/lbs_per_bucket + lbs_friend)**2", constants)

       2
⎛2    ⎞ 
⎜─ + 3⎟ 
⎝4    ⎠ 

Is there a built-in sympy way of rendering an expression that contains python variables without evaluating it?

On the flip-side, is there a way of defining a multi-variable expression, like x + 2y and then inputting the value of y such that sympy just solves for x? In that sense I could include variables from python take take on the defined symbol values.

Cheers--and thanks for taking a look!


Reply all
Reply to author
Forward
0 new messages