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
Conversion of symbolic functions with latex_name or nargs from maxima and sympy is broken #31047
Comments
Commit: |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:3
Thank you very much for taking care of this long standing issue! I've performed a few tests and the fix seems good to me. Let us wait for some other viewpoints before setting a positive review. Also it would be nice if the patchbot could run on this ticket branch. |
comment:4
Another issue with symbolic functions is #27492. It might also be related to the way symbolic functions are stored in the symbol table. |
Author: Marius Gerbershagen |
comment:6
I am not able to judge the quality of the fix, but you should at least add doctests. Eric, could you please provide the tests you did so that they can be added as well ? |
comment:7
I can comment on one question in the description: since it is possible to do the following:
I think our hand is forced on taking into account LaTeX names. It may not be advisable to create many functions with the same print name, it's not forbidden. In fact, automatic code could easily create many functions, and they should not interfere with the latex names used elsewhere. You're probably going to run into problems if you do this kind of stuff in other interfaces (particularly maxima), though. |
comment:8
Replying to @nbruin:
It should be forbidden, IMHO. As pointed out in the ticket description, I don't see any use case for such a feature. Since maxima has no concept of LaTeX name and maxima is currently heavily used for simplifications in Sage symbolic calculus, it would be safe to forbid to declare a function with an already existing name but a different LaTeX name.
What do you mean by "other interfaces (particularly maxima)", given that this ticket is about the maxima interface? |
comment:9
Replying to @sagetrac-tmonteil:
Here is a doctest adapted from the ticket description:
|
comment:10
The sympy interfaces suffers from exactly the same problem, for example in
|
comment:11
Replying to @nbruin:
But in basically all cases, sagemath already assumes that the print name can be treated as a unique identifier for the function. The documentation never mentions anything else. External interfaces rely on the assumption. The sagemath prompt treats the print name as a unique identifier: when I create two functions with the same print name and two different latex names and then type in the function name in the prompt, I get only one of the two functions. Therefore, the "feature" that one can create two functions with the same print name but different latex names is in my opinion first of all profoundly useless (because most of the stuff one would want to do with these functions won't work properly) and secondly serves only as a pitfall to confuse unsuspecting users. Given that the sympy interface is also broken (it is not unlikely that other interfaces may also suffer from the same problem), in my opinion the only sane solution is to patch |
comment:12
Replying to @spaghettisalat:
+1
Yes, this seems the route to go. |
comment:13
In any case, there is something weird about the way some kind of "unique representation" for functions (and variables) is handled (both with and without the branch):
|
comment:15
I have implemented a basic fix to the specific problem reported on this ticket, i.e. only for the conversion of symbolic functions from maxima. The inconsistent behaviour of |
comment:16
patchbot reports:
|
comment:21
needs rebase |
comment:24
Just a superficial comment (because I don't know this part of the code very well): |
comment:26
Everything I added is already covered with tests, either the ones I added in this ticket or existing tests for other functionality that depends on the stuff I changed. Therefore I added only some docstrings. I hope that is finally enough to get this merged. Is there anybody more familiar with this part of sagemath who we could cc to review this? |
Reviewer: Eric Gourgoulhon |
comment:27
I've performed a few tests and everything seems OK. Thanks for the fix! Regarding :comment:24, the class |
comment:29
I have copied some of the tests to the new helper functions created in the new commits. (Not that this would make any difference since as I said before, there were already tests for the functionality and all I've done now is to spread the tests out into more places. I don't want to be too rude here, but just counting whether a function has doctests or not seems like a pretty shitty way to measure test coverage.) |
comment:30
Replying to @spaghettisalat:
Hard to disagree with this, but we don't have a better way. |
comment:31
Replying to @spaghettisalat:
OK, this provides only indirect doctests, but let's move on in order to have the fix merged in 9.3!
Beside testing, another virtue of doctests is to illustrate quickly the use of the function; this is useful even for helper functions, i.e. for functions that only developers are supposed to take a look at. |
Changed branch from public/bug_convert_symbolic_function_from_maxima to |
comment:33
This ticket causes an issue with conversions in the Mathematica interface, see #31756. Do you have an idea that might solve this? |
Changed commit from |
comment:34
An unrelated comment on this change: -def symbolic_expression_from_string(s, syms=None, accept_sequence=False):
+def symbolic_expression_from_string(s, syms={}, accept_sequence=False): Usually it is best to avoid using The way it is used in this particular case does not seem to make a problem, though, as the value is not mutated inside the function. |
When converting a
NewSymbolicFunction
to a maxima expression and back, sage sometimes returns the correct symbolic function and sometimes it creates a new function with the same name. This happens only when the function has additional information (i.e. alatex_name
ornargs
) attached to it. For example:returns an expression with a new
Cp
function which is not equal to the original one and which doesn't have a latex name, but only if theassume(phi >= 0)
happens before defining the function.The issue is that the conversion functions hold a local copy of the symbol table which is not kept in sync with the actual symbol table that new functions are added to. If the conversion functions do not find the function by name in the local copy,
function_factory
is invoked which checks for bothname
,latex_name
andnargs
when looking for already registered functions. Since of course the maxima expression doesn't include thelatex_name
, it doesn't find the already registered function and creates a new one.I have implemented a fix which checks both the local and global copies of the symbol table, but I'm not sure this is the right way to fix things. First, it is not clear to me why the conversion functions have a local copy of the symbol table in the first place. Second, it makes no sense to me that
function_factory
looks for a matchinglatex_name
when registering a new function. I see no use case for having to functions with the samename
and differentlatex_name
registered. After all, if I type in the name of the function in the sagemath prompt, I will only get the second definition which I typed in and thus I can't use the first definition anyway.See also #14608 comment:9 and links therein for more discussion about this issue.
CC: @nbruin @egourgoulhon @rwst @DaveWitteMorris
Component: symbolics
Author: Marius Gerbershagen
Branch:
be11386
Reviewer: Eric Gourgoulhon
Issue created by migration from https://trac.sagemath.org/ticket/31047
The text was updated successfully, but these errors were encountered: