Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sympy is not doing the substitution of a symbol with a value when it has a specified assumption

I’m using the subs method to replace certain parameters in an expression with values prior to solving the equation.

The following simple example works fine:

from sympy import Symbol

Q = Symbol("Q")
exp1 = Q + 1
print(exp1.subs({'Q': 1}))  # prints 2

However, if the symbol has an assumption such as real or positive specified this does not work:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Q = Symbol("Q", positive=True)
exp1 = Q + 1
print(exp1.subs({'Q': 1}))  # prints Q + 1

Why is this and what am I doing wrong?

>Solution :

Don’t use a string key.

print(exp1.subs({Q: 1}))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading