Opened 6 years ago
Last modified 22 months ago
#14270 needs_info defect
Remove function call syntax for symbolic expressions
Reported by: | ppurka | Owned by: | burcin |
---|---|---|---|
Priority: | major | Milestone: | sage-8.2 |
Component: | symbolics | Keywords: | |
Cc: | nthiery, kini | Merged in: | |
Authors: | Punarbasu Purkayastha | Reviewers: | |
Report Upstream: | N/A | Work issues: | fix combinat/tutorial.py |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
The function call syntax for symbolic expressions has been deprecated for almost six years. It is about time it raised some errors. This will prevent people from getting confused because of behavior like this.
Apply trac_14270-raise_error_on_function-call.patch to devel/sage.
See also the threads
Attachments (1)
Change History (22)
comment:1 Changed 6 years ago by
- Description modified (diff)
- Status changed from new to needs_review
comment:2 Changed 6 years ago by
- Status changed from needs_review to needs_work
- Work issues set to fix doctests
comment:3 follow-ups: ↓ 4 ↓ 14 Changed 6 years ago by
- Work issues changed from fix doctests to fix combinat/tutorial.py
comment:4 in reply to: ↑ 3 Changed 6 years ago by
- Cc nthiery added
Replying to ppurka:
I am able to fix all the doctests except for the following one in combinat/tutorial.py.
<snip>
What should I do with this portion of the tutorial? Delete this?
I'm cc:ing Nicolas, who should know what is going on here.
comment:5 follow-ups: ↓ 6 ↓ 16 Changed 6 years ago by
I'd also say that I think the prep tutorial one should still talk about this at some length, to explain (in the event this is done) why this doesn't work, because a lot of people will now and forevermore expect that it will work. Similarly, most of these examples presumably should be moved to the new syntax, perhaps even with an explanatory remark as to exactly why that is the syntax. For instance, how would one do the matrix example h(x)
now? We should be careful not to remove anything, just to change how it works to the appropriate way post-this-ticket.
Rant I don't actually want to rehash any more, but put here for completeness:
<rant> Because distinguishing between a function and a symbolic expression is an unnatural, CS-y thing to do; any symbolic expression is obviously a function of all variables in it, mathematically; for any function with more than one
var
I agree we don't want this (as indeed the example in the prep document points out) but for one-variable expressions (pace Jason, who will immediately ask whetherx+y-y
is one variable) it seems worth the potential for confusion... </rant>
comment:6 in reply to: ↑ 5 Changed 6 years ago by
Replying to kcrisman:
I'd also say that I think the prep tutorial one should still talk about this at some length, to explain (in the event this is done) why this doesn't work, because a lot of people will now and forevermore expect that it will work.
IMHO, the people who expect this to still work need to change their code. It has been in deprecated mode for over four years. That's more than enough time to change their habit and old code. I think someone hasn't complained before either because they are complacent or because they don't use this at all.
While teaching a course with Sage, I remember that we ourselves ran into this problem with the students. It was annoying and confusing because we were unaware of the code and how to fix it. We would just ask the students to ignore those warnings. What would a beginner do after defining f(x) = x^2
? The most natural thing would be to do f(2)
or something similar to "see" that it can actually evaluate. Now imagine the same with f = x^2
and what you get is the deprecation message and then the correct answer. Second time it is evaluated, there is no deprecation message, so a beginner will wonder what just happened in the first invocation. It is not a favorable impression. It gives the impression of a half-done software.
I agree with you that it should be explained in the tutorial that there is a difference between symbolic functions and symbolic expressions and python functions.
comment:7 Changed 6 years ago by
- Cc kini added
comment:8 Changed 6 years ago by
- Milestone changed from sage-5.11 to sage-5.12
comment:9 Changed 5 years ago by
- Milestone changed from sage-6.1 to sage-6.2
comment:10 Changed 5 years ago by
- Milestone changed from sage-6.2 to sage-6.3
comment:11 Changed 5 years ago by
- Milestone changed from sage-6.3 to sage-6.4
comment:12 Changed 4 years ago by
- Description modified (diff)
This is actually the same as #8214, but that has no code, so I'm copying the two links from there to here before declaring it a dupe.
comment:13 Changed 4 years ago by
This ticket is unlikely to get fixed unless someone who knows the code really well addresses comment:3
comment:14 in reply to: ↑ 3 Changed 4 years ago by
Replying to ppurka:
I am able to fix all the doctests except for the following one in combinat/tutorial.py.
OK, from what you've written down here, I can come up with one way of rewriting the example.
sage: C, z = var('C,z'); sage: sys = [ C == z + C*C ] sage: sol = solve(sys, C, solution_dict=True); sol [{C: -1/2*sqrt(-4*z + 1) + 1/2}, {C: 1/2*sqrt(-4*z + 1) + 1/2}] sage: s0 = sol[0][C]; s1 = sol[1][C]
sage: C = s0; C -1/2*sqrt(-4*z + 1) + 1/2
I think this should be deleted. Use s0 if you want that and just leave C bound to the variable. It's already bad enough that we need "C as a function" further down.
sage: equadiff (4*z - 1)*D[0](C)(z) - 2*C(z) + 1 == 0 sage: Cf = sage.symbolic.function_factory.function('C')
Didn't we need Cf before to arrive at equadiff
? How did C(z)
get into that expression in the first place?
sage: equadiff.substitute_function(Cf, s0) # Original answer is the deprecation + answer ... TypeError: %d format: a number is required, not NoneType
I think the following should work (it does in vanilla, without a deprecation warning):
sage: equadiff.substitute_function(Cf, s0.function(z))
Let us try to "fix" this (
z
is a symbolic variable). Somehow the following works (beats me why it does):sage: equadiff.substitute_function(Cf(z), s0) (4*z - 1)*D[0](C)(z) - 2*C(z) + 1 == 0 # OK, so this seems to work
I'm not so sure it "works". It doesn't give an error (which is surprising in its own right; perhaps that should change), but I don't think it gives the same answer. In vanilla:
sage: equadiff.substitute_function(Cf(z), s0) (4*z - 1)*D[0](C)(z) - 2*C(z) + 1 == 0 sage: equadiff.substitute_function(Cf, s0.function(z)) (4*z - 1)/sqrt(-4*z + 1) + sqrt(-4*z + 1) == 0
But now the next command in the tutorial gives
False
instead ofTrue
Rightly so, judging from the results above.
Why is all this happening? Looking at the documentation of
substitute_function
shows that it should be used for substituting functions, not anything elsedef substitute_function(self, original, new): """ Returns this symbolic expressions all occurrences of the function *original* replaced with the function *new*.And what exactly are we substituting above?
sage: type(Cf) sage.symbolic.function_factory.NewSymbolicFunction sage: type(s0) sage.symbolic.expression.ExpressionWe are substituting a symbolic function with a symbolic expression! How was this even working earlier?
I guess because symbolic expressions were deprecated "functions": you could call then with symbolic arguments and get a symbolic expression, at the cost of a deprecation warning.
What should I do with this portion of the tutorial? Delete this?
No, just do
sage: equadiff.substitute_function(Cf, s0.function(z))
That's probably a useful example to have anyway. It shows how to turn a symbolic expression into a function in a non-ambiguous way, because you need to specify argument order.
comment:15 Changed 4 years ago by
- Description modified (diff)
comment:16 in reply to: ↑ 5 ; follow-ups: ↓ 18 ↓ 21 Changed 22 months ago by
Replying to kcrisman:
<rant> Because distinguishing between a function and a symbolic expression is an unnatural, CS-y thing to do; any symbolic expression is obviously a function of all variables in it, mathematically; for any function with more than one
var
I agree we don't want this (as indeed the example in the prep document points out) but for one-variable expressions (pace Jason, who will immediately ask whetherx+y-y
is one variable) it seems worth the potential for confusion... </rant>
I actually agree and I don't see why a function call with non-relational argument should not be allowed IF there is only one variable in the expression. The confusion comes from the fact that things like sqrt(2)(x+y)
almost always are typos where the *
is missing. These should be marked as error. Now Sage cannot distinguish between f=x^2; f(2)
and sqrt(2)(x+y)
because f
is replaced immediately with x^2
and so in both cases an expression has its __call__
member executed. BUT, first with sqrt(2)(x+y)
the expression has no variables and so it can be doubtlessy marked as error; secondly, before f
is silently expanded to x^2
it is seen by the parser---maybe the parser can distinguish between calls that are done to f
(like calls to functions) and other calls, and only allow the former?
comment:17 Changed 22 months ago by
- Milestone changed from sage-6.4 to sage-8.2
- Status changed from needs_work to needs_info
comment:18 in reply to: ↑ 16 Changed 22 months ago by
Replying to rws:
maybe the parser can distinguish between calls that are done to
f
(like calls to functions) and other calls, and only allow the former?
That would be adding even more special cases just for Sage.
In Python, you expect the following two to do the same thing
f = x^2 print(f(2))
and
print((x^2)(2))
It would be very surprising if somehow these would become different.
comment:19 Changed 22 months ago by
But of course Jason's x+y-y
still has to be dealt with, if only to ignore it ...
comment:20 Changed 22 months ago by
This is equality (or zero) proving in the symbolic ring, so it can be arbitrarily complex. Ignore it, yes.
comment:21 in reply to: ↑ 16 Changed 22 months ago by
... The confusion comes from the fact that things like
sqrt(2)(x+y)
almost always are typos where the*
is missing. These should be marked as error.
This is now #23684
I am able to fix all the doctests except for the following one in combinat/tutorial.py.
The example which fails seems to make no sense to me. A
substitute_function
method is being used to substitute a function with a symbolic expression.Let us try to "fix" this (
z
is a symbolic variable). Somehow the following works (beats me why it does):But now the next command in the tutorial gives
False
instead ofTrue
Why is all this happening? Looking at the documentation of
substitute_function
shows that it should be used for substituting functions, not anything elseAnd what exactly are we substituting above?
We are substituting a symbolic function with a symbolic expression! How was this even working earlier?
What should I do with this portion of the tutorial? Delete this?