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
Lambdify doesn't recognize derivative symbol if cse is enabled #26404
Comments
I thought this would work, but it doesn't:
|
cse is called on the expressions and then the cse results are passed to the printer which runs a preprocessor on the results that do the dummifying. I'm guessing those operations should be swapped for things to work correctly. |
Hello, I've looked a bit into the code, I think the issue is specific to using xd = x.diff(t). This case is not tested(referring to test_lambdify.py) and with the current implementation, the Dummy_XY are not placed correctly in the expression calculated by cse since you would need to look recursively and replace recursively in the Derivative(x0, t) when x0 is replaced with a dummy (although looking recursively might not be needed if we could implement a simple way to link them) . I'll look into it more and indeed, if we call cse after the printer it works perfectly, but then the results are not used. |
The dummy replacement works recursively already. Maybe just look into having the preprocessor apply the dummy subs to all of the cse output. |
Actually this code looks like the preprocessor is applied to all outputs of cse: https://github.com/sympy/sympy/blob/master/sympy/utilities/lambdify.py#L1133-L1140 |
It looks like in the CSE process, which happens first, the argument of the derivative gets swapped out:
This would then cause the dummy replacement to be missed because it is looking for
|
One option would be a flag to cse() to have it skip |
Maybe a pre and post optimization could be used for cse, something like:
|
That's what I ended up thinking to
A flag could work, I was also thinking that we could consider the derivative (Derivative(x0, t)) like a replacement but I think the flag would be more practical.
It would work to, but what are the advantages to this compared to the flag possibility?(since with this possibility, we would go trhough the expression twice and still ignore the derivatives) |
It is a flag that is already present, so we would not need to implement a new kwarg in cse. |
But isn't there some cases where we want the arguments of the derivative also changed accordingly?( instead of Derivative(x,t), we would want Derivative(x0,t)) I've been trying to implement the change(not using pre-prost method but a flad indicating cse is called inside lambdify), but I'm running into a difficulty and I don't know how to resolve it. I've added an optionnal parameter in cse that indicates that it is called inside a lambdify function. When I simply used it as a non-optionnal parameter (put right after expr), the function worked perfectly and the bug was resolved. However, when put as an optionnal parameter, the value of flag_lambdify(name of the flag) isn't modified, and I've noticed that it is the same for the optionnal parameter list. To see this problem, add the new flag variable, add a print(list, flag_lambdify) under the header of the function cse and enter the lines of code above that produced the bug. The print is also executed 2 times( you'll have a first print with the right values, and a second one with the default values). If you want, you can also see the modified files in my sympy repo(although I'm not sure I left the print(list, flag_lambdify) when I pushed). Also, I've not put the flag_lamdbify as a non-optionnal parameter because I think it is used in other files, but if it is not it would solve the problem( but not the problem with the list variable) |
Here is a minimal reproducer:
I am not sure if this issue is easily solvable as the problem seems to be that it tries to print the expression
x0 + Derivative(x0, t)
with the[(x0, x(t))]
as subexpressions.The text was updated successfully, but these errors were encountered: