-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 is no longer able to deal with sympy.vector.BaseScalar
#26690
Comments
Likely the same fundamental issue reported at #26663 |
I think this issue is different enough to merit its own issue. I will see if I can fix this separately (looking at this quickly I think I see a way). |
I added a commit to gh-26678 (and test) which I think fixes this issue. |
I bisected the regression here to 82f80c0 from gh-25913. I think that |
Perhaps |
Somehow it does (I find it challenging to grasp the control flow in lambdify.py), the code that gets compiled uses dummy symbols, but the arguments in the doc-string has the invalid identifiers. |
It tries to print the arguments here: sympy/sympy/utilities/lambdify.py Line 1145 in ff21452
|
Sorry I was vague: I know, I have stepped through this, that's how I came up with adding |
The problem is that this code tries to print the argument first before deciding to use a Dummy: sympy/sympy/utilities/lambdify.py Lines 1187 to 1228 in 73ae5e9
That whole function should be rewritten in a simpler more robust way. The different special cases should not be needed. They mostly amount to the same thing which is just that it might be necessary to replace the arg with a Dummy. There should be no need to special case DeferredVector , Function , Derivative . Probably the is_symbol check should be replaced with is_Symbol .
|
Agreed. Although I will not find the time in the near future for any ambitious rewrite. |
Probably |
This is a quick fix: diff --git a/sympy/utilities/lambdify.py b/sympy/utilities/lambdify.py
index be06906291..f1602508e7 100644
--- a/sympy/utilities/lambdify.py
+++ b/sympy/utilities/lambdify.py
@@ -1210,8 +1210,12 @@ def _preprocess(self, args, expr):
elif isinstance(arg, DeferredVector):
s = str(arg)
elif isinstance(arg, Basic) and arg.is_symbol:
- s = self._argrepr(arg)
- if dummify or not self._is_safe_ident(s):
+ s = None
+ if not dummify and arg.is_Symbol:
+ s = self._argrepr(arg)
+ if not self._is_safe_ident(s):
+ s = None
+ if s is None:
dummy = Dummy()
if isinstance(expr, Expr):
dummy = uniquely_named_symbol( |
It used to be necessary to use this so you could provide an index-able argument to your lambdified function, but it is no longer needed because |
Unfortunately this fails some tests where a MatrixSymbol is the parameter and the expectation is to pass a numpy array as the argument. An even simpler fix then is just: diff --git a/sympy/utilities/lambdify.py b/sympy/utilities/lambdify.py
index be06906291..18383bf640 100644
--- a/sympy/utilities/lambdify.py
+++ b/sympy/utilities/lambdify.py
@@ -1210,7 +1210,7 @@ def _preprocess(self, args, expr):
elif isinstance(arg, DeferredVector):
s = str(arg)
elif isinstance(arg, Basic) and arg.is_symbol:
- s = self._argrepr(arg)
+ s = str(arg)
if dummify or not self._is_safe_ident(s):
dummy = Dummy()
if isinstance(expr, Expr):
|
Fix according to @oscarbenjamin's comment: sympy#26690 (comment)
We should keep this open until the fix is confirmed in 1.13rc2. |
@Davide-sd I have just pushed SymPy 1.13.0rc2 to PyPI which should have the fix for this issue. Can you test with it? |
Run the tests, everything works fine now. Thanks everyone, I'm closing the issue. |
Example code:
Lambdifying with sympy 1.12.1. Note that instances of
BaseScalar
are converted to dummy symbols.Lambdifying with sympy 1.13 rc1: no longer supported. That was essential to properly plot vector fields with my plotting module.
The text was updated successfully, but these errors were encountered: