sympy / sympy

A computer algebra system written in pure Python

Home Page:https://sympy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

collect function bug

ferymi6041 opened this issue · comments

if you run the code below in python, it can't collect the very simple expr with mv-derivative.

from sympy import Symbol, symbols, collect, expand
x = Symbol("x", real = True)
theta = Symbol("theta", real = True)
t = Symbol("t", real = True)
u = symbols('u', real = True, cls=Function)(x, theta, t)

expr = u.diff(x, x, t) * x + u.diff(x, x, t) * t
expr = collect(expand(expr), u.diff(x, x, t))

What did you get? After running this code expr is (x+t)*u.diff(x,x,t) and that is what I would expect.

You are using a very old version of SymPy. Update to the latest version.

This works as expected since sympy 1.7 which was released in 2020:

In [1]: from sympy import Symbol, symbols, collect, expand
   ...: x = Symbol("x", real = True)
   ...: theta = Symbol("theta", real = True)
   ...: t = Symbol("t", real = True)
   ...: u = symbols('u', real = True, cls=Function)(x, theta, t)
   ...: 
   ...: expr = u.diff(x, x, t) * x + u.diff(x, x, t) * t

In [2]: expr
Out[2]: 
     3                      3              
    ∂                      ∂               
t⋅──────(u(x, θ, t)) + x⋅──────(u(x, θ, t))
    2                      2xtxt            

In [3]: expr = collect(expand(expr), u.diff(x, x, t))

In [4]: expr
Out[4]: 
           3              
          ∂               
(t + x)⋅──────(u(x, θ, t))
          2xt

but, if you run the code below you get the error this time:

from sympy import Symbol, symbols, collect, expand, Function
x = Symbol("x", real = True)
theta = Symbol("theta", real = True)
t = Symbol("t", real = True)
u = symbols('u', real = True, cls = Function)(x, theta, t)
v = symbols('v', real = True, cls = Function)(x, theta, t)

expr = u.diff(x, x, t) * x + u.diff(x, x, t) * t + x * v.diff(x, t)

expr = collect(expand(expr), u.diff(x, x, t))
print(expr)

when you have two multi-variable like u and v or more in expr collect can't be done but mathematically it should do it very easily

Okay, I can reproduce that now.

Closing as duplicate of gh-26681