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

Derivative.expr must be parenthesized when printed #18349

Open
mcpl-sympy opened this issue Jan 16, 2020 · 3 comments
Open

Derivative.expr must be parenthesized when printed #18349

mcpl-sympy opened this issue Jan 16, 2020 · 3 comments

Comments

@mcpl-sympy
Copy link
Contributor

Here is an example:

from sympy import Derivative, Function
from sympy.abc import x
f = Function('f')
g = Function('g', commutative=False)
### These two result are printed identically
Derivative(f(x), x) * g(x)
Derivative(f(x)*g(x), x)

Thus, I suggest that Derivative.expr be parenthesized with PRECEDENCE['Mul'] when printed.

@mohitacecode
Copy link
Contributor

mohitacecode commented Jan 16, 2020

I don't think so because when you replace f(x) and g(x) with some expression then both Derivative(f(x), x) * g(x) and Derivative(f(x)*g(x), x) will evaluate diffrently.

They just look same in this case but they are not same.
For example:
f(x) = x2, g(x) = x3

@gschintgen
Copy link
Contributor

I can't reproduce this issue in isympy (outputs are parenthesized), but in a Jupyter notebook with MathJax this is indeed an issue. (Without the commutative=False, the factor g(x) is put in front and there's no ambiguity.)

@mohitacecode :

They just look same in this case but they are not same.

And that's why this is an issue!

@oscarbenjamin
Copy link
Contributor

The problem is the latex printer (isympy uses the pretty printer):

In [58]: d1 = Derivative(f(x)*g(x), x)                                                                                                         

In [59]: d2 = Derivative(f(x), x) * g(x)                                                                                                       

In [60]: d1                                                                                                                                    
Out[60]: 
d            
──(f(x)g(x))
dx           

In [61]: d2                                                                                                                                    
Out[61]: 
d            
──(f(x))g(x)
dx           

In [62]: latex(d1)                                                                                                                             
Out[62]: '\\frac{d}{d x} f{\\left(x \\right)} g{\\left(x \\right)}'

In [63]: latex(d2)                                                                                                                             
Out[63]: '\\frac{d}{d x} f{\\left(x \\right)} g{\\left(x \\right)}'

In [64]: latex(d1) == latex(d2)                                                                                                                
Out[64]: True

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

No branches or pull requests

4 participants