Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New problem with substitution and paranthesis handling #13333

Closed
ttxtea opened this issue Sep 21, 2017 · 4 comments · Fixed by #15371
Closed

New problem with substitution and paranthesis handling #13333

ttxtea opened this issue Sep 21, 2017 · 4 comments · Fixed by #15371

Comments

@ttxtea
Copy link

ttxtea commented Sep 21, 2017

Something has changed in the way substitutions work in sympy version 1.1.1. on python3.4.2

In [3]: sympy.S('R*F').subs({'R':'8314.4*mjoule/(mole*kelvin)','F':'96484.6*(coulomb/mole)'})
Out[3]: 8314.4*mjoule/(mole*kelvin)*96484.6*(coulomb/mole)

In [4]: sympy.S('R/F').subs({'R':'8314.4*mjoule/(mole*kelvin)','F':'96484.6*(coulomb/mole)'})
Out[4]: 8314.4*mjoule/(mole*kelvin)/96484.6*(coulomb/mole)

with sympy 1.0 on python2.7.9 this yielded

In [3]: sympy.S('R*F').subs({'R':'8314.4*mjoule/(mole*kelvin)','F':'96484.6*(coulomb/mole)'})
Out[3]: 802211558.24*coulomb*mjoule/(kelvin*mole**2)

In [4]: sympy.S('R/F').subs({'R':'8314.4*mjoule/(mole*kelvin)','F':'96484.6*(coulomb/mole)'})
Out[4]: 0.0861733375067109*mjoule/(coulomb*kelvin)

The update seems to have wronged something.

@jksuom
Copy link
Member

jksuom commented Sep 21, 2017

It looks like SymPy is doing exactly what is prescibed:

>>> srepr(S('R*F'))
"Mul(Symbol('F'), Symbol('R'))"
>>> srepr(S('R*F').subs({'R':'8314.4*mjoule/(mole*kelvin)','F':'96484.6*(coulomb/mole)'}))
"Mul(Symbol('8314.4*mjoule/(mole*kelvin)'), Symbol('96484.6*(coulomb/mole)'))"

Before substitution, S('R*F') is a Mul expression with two symbols as arguments. The names of the symbols are Python strings and the substitution will replace them with new (long) strings. This should probably not be regarded as an error.

The values in the dictionary, '8314.4*mjoule/(mole*kelvin)' and '96484.6*(coulomb/mole)', are Python strings. To obtain SymPy objects, the quotes should be omitted.

@ttxtea
Copy link
Author

ttxtea commented Sep 21, 2017

But the division is wrong, isn't it?? It should be

/(coulomb*mole)

not the same as the multiplication. The srepr is fine

sympy.srepr(sympy.S('R/F').subs({'R':'8314.4*mjoule/(mole*kelvin)','F':'96484.6*(coulom
   ...: b/mole)'}))
Out[4]: "Mul(Symbol('8314.4*mjoule/(mole*kelvin)'), Pow(Symbol('96484.6*(coulomb/mole)'), Integer(-1)))"

but souldn't Pow((coulomb/mole),Integer(-1)) should produce mole/coulomb.

@ttxtea
Copy link
Author

ttxtea commented Sep 21, 2017

Sorry for the messy example with the "Nernst equation". Maybe this is clearer: In sympy 1.1.1.

In [9]: sympy.S('1/a').subs({'a':'2*d/c'})
Out[9]: 1/2*d/c

but

In [10]: sympy.srepr(sympy.S('1/a').subs({'a':'2*d/c'}))
Out[10]: "Pow(Symbol('2*d/c'), Integer(-1))"

In sympy 1.0

In [3]: sympy.S('1/(a)').subs({'a':'2*d/c'})
Out[3]: c/(2*d)

and

In [4]: sympy.srepr(sympy.S('1/(a)').subs({'a':'2*d/c'}))
Out[4]: "Mul(Rational(1, 2), Symbol('c'), Pow(Symbol('d'), Integer(-1)))"

@ttxtea
Copy link
Author

ttxtea commented Nov 30, 2017

Note that it is probably the repr() function that causes the problem. In isympy, where the output is based on srepr it is fine. So maybe the bug is somewhere in the __repr__ function?

A workaround is to use additional parenthesis. like

sympy.S('1/a').subs({'a':'(2*d/c)'})

But the backwards compatibility to sympy 1.0 is not given and projects like brian2 have problems with old model definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants