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

Partial substitutions of Mul objects do not work for mixed-variable terms #1359

Open
tbabej opened this issue Nov 10, 2017 · 7 comments
Open

Comments

@tbabej
Copy link

tbabej commented Nov 10, 2017

Consider the following example, which works as expected:

>>> import symengine
>>> x,y,z = symengine.symbols('x y z')
>>> eq = 2*x*y*y + 3*x*x + 4*y*y
>>> eq.subs(y*y, z)
4*z + 2*x*z + 3*x**2

If we try to substitute x*y instead of y*y, which is still contained in the first term, the substitution does not happen:

>>> eq.subs(x*y, z)
2*x*y**2 + 3*x**2 + 4*y**2

This only happens for non-exact matches, exact matches do work:

>>> (2*x*y + 3*y).subs(x*y, z)
3*y + 2*z
@isuruf
Copy link
Member

isuruf commented Nov 10, 2017

Yes, subs is kind of dumb that way. What happens is it will check whether the expression is equal to 2*x*y**2, then x*y**2 then x then y**2.
This should be fixed though. Original behaviour can stay in xreplace, but subs needs to be fixed.

@tbabej
Copy link
Author

tbabej commented Nov 10, 2017

@isuruf Can you point me to the place in the code base where this logic is implemented? I can contribute a fix.

@isuruf
Copy link
Member

isuruf commented Nov 10, 2017

Thanks. It's implemented in XReplaceVisitor at https://github.com/symengine/symengine/blob/master/symengine/subs.h#L74-L108

This method shouldn't be changed, but overriden in the SubsVisitor class which is here, https://github.com/symengine/symengine/blob/master/symengine/subs.h#L270

@eeshan9815
Copy link
Contributor

@tbabej @isuruf Hey, I am new to symengine. If no one is working on it, I would like to start contributing by fixing this issue.

@ShikharJ
Copy link
Member

Go for it @eeshan9815

@tbabej
Copy link
Author

tbabej commented Jan 17, 2018 via email

@eeshan9815
Copy link
Contributor

@isuruf @ShikharJ @tbabej I think I found another issue with subs:
While this works as expected:

>>> (2*x*y + 3*y).subs(x*y, z)
3*y + 2*z

This does not:

>>> (2*x*y).subs(x*y, z)
2*x*y

instead of 2*z
Can someone explain why, and how to fix this issue as well? I have had some progress with the original issue.

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

No branches or pull requests

4 participants