Skip to content
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

diff(f,t) should work even with Function objects #17701

Closed
rwst opened this issue Jan 30, 2015 · 17 comments
Closed

diff(f,t) should work even with Function objects #17701

rwst opened this issue Jan 30, 2015 · 17 comments

Comments

@rwst
Copy link

rwst commented Jan 30, 2015

sage: t = var('t')
sage: sage: f = function('f')(t)
sage: diff(f,x,x)
0
sage: desolve(diff(f,t) - (f+1)/(t+1), f)
(_C - 1/(t + 1))*(t + 1)

sage: sage: f = function('f')
sage: diff(f,t) 
...
TypeError: unable to convert f to a symbolic expression
sage: diff(sin,t)
...
TypeError: unable to convert f to a symbolic expression

The same error can be had with the simple SR(sin), i.e., any function object in contrast to the resp. function call, which returns an Expression.

Since function objects can be used in other context it seems they should be part of symbolic expressions too. This ticket however will limit itself to giving a better error message in SR.coerce and handling Function objects in calculus/functional.py:derivative().

See also #15025

Component: calculus

Author: Ralf Stephan

Branch/Commit: u/rws/diff_f_t__should_work_even_with_function_objects @ 0cecec3

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/17701

@rwst rwst added this to the sage-6.5 milestone Jan 30, 2015
@kcrisman
Copy link
Member

comment:1

Possibly related to the shenanigans induced by the distinction at #17447? So maybe user error (though obviously completely understandable given the terrible documentation for this)? Agreed on the uselessness of the error message, though presumably the traceback would help an experienced Sage developer... who is not the likely recipient of an error message on this, unfortunately! More like someone in calculus or ODE. Yuck.

@rwst
Copy link
Author

rwst commented Jan 31, 2015

comment:2

Replying to @kcrisman:

Possibly related to the shenanigans induced by the distinction at #17447?

That too but I think either SR(f) or diff(f,t) should work even with NewSymbolicFunction. If not improve the error message.

@rwst rwst changed the title desolve TypeError snub SR(f) or diff(f,t) should work even with NewSymbolicFunction Feb 1, 2015
@rwst

This comment has been minimized.

@rwst rwst modified the milestones: sage-6.5, sage-6.8 Jul 9, 2015
@rwst rwst changed the title SR(f) or diff(f,t) should work even with NewSymbolicFunction diff(f,t) should work even with Function objects Jul 9, 2015
@rwst
Copy link
Author

rwst commented Jul 9, 2015

comment:5

However, having diff(f,t) a shortcut for diff(f(t),t) is less useful because the other instances of f in the argument to desolve would still have to be coerced. And it is impossible to know what is the variable there.

@rwst
Copy link
Author

rwst commented Jul 9, 2015

@rwst
Copy link
Author

rwst commented Jul 9, 2015

Author: Ralf Stephan

@rwst
Copy link
Author

rwst commented Jul 9, 2015

New commits:

0cecec317701: diff(f,t) should work even with Function objects

@rwst
Copy link
Author

rwst commented Jul 9, 2015

Commit: 0cecec3

@rwst

This comment has been minimized.

@tscrim
Copy link
Collaborator

tscrim commented Mar 29, 2016

Reviewer: Travis Scrimshaw

@tscrim
Copy link
Collaborator

tscrim commented Mar 29, 2016

comment:9

Should we expect this to work?

sage: f = function('f')
sage: diff(f, x=x)

Also, in an effort to be an uniform with python, could you make the error message start with a lowercase letter?

@tscrim tscrim modified the milestones: sage-6.8, sage-7.2 Mar 29, 2016
@nbruin
Copy link
Contributor

nbruin commented Mar 31, 2016

comment:10

Replying to @tscrim:

Should we expect this to work?

sage: f = function('f')
sage: diff(f, x=x)

I'd say no. This would be significant feature creep into the interface of diff.

I am a little worried about

sage: diff(f,x,x)
D[0, 0](f)(x, x) + 2*D[0, 1](f)(x, x) + D[1, 1](f)(x, x)

where almost certainly a user who would type this would be hoping for

D[0, 0](f)(x)

I think the ambiguity in meaning when multiple arguments are given makes it safer to disallow it altogether.

I wouldn't mind if we wouldn't support this at all, but if we do, I recommend that we only allow repetition of a single argument variable, and interpret it as the n-th derivative of a univariate function evaluated at that argument

@rwst
Copy link
Author

rwst commented Nov 6, 2016

comment:12

With Sage develop:

sage: t = var('t')
sage: sage: f = function('f')(t)
sage: diff(f,x,x)
0
sage: desolve(diff(f,t) - (f+1)/(t+1), f)
(_C - 1/(t + 1))*(t + 1)

sage: sage: f = function('f')
sage: diff(f,t) 
...
TypeError: unable to convert f to a symbolic expression
sage: diff(sin,t)
...
TypeError: unable to convert sin to a symbolic expression

So an error does only happen with anonymous functions (NewSymbolicFunction) and bare Function objects.

With this branch there is

sage: diff(sin,t)
cos(t)
sage: diff(f,t)
diff(f(t), t)
sage: diff(f,x,x)
D[0, 0](f)(x, x) + 2*D[0, 1](f)(x, x) + D[1, 1](f)(x, x)

of which the latter has to be improved according to Nils.

@rwst

This comment has been minimized.

@nbruin
Copy link
Contributor

nbruin commented Dec 4, 2021

comment:14

Replying to @rwst:

With this branch there is

sage: diff(sin,t)
cos(t)
sage: diff(f,t)
diff(f(t), t)
sage: diff(f,x,x)
D[0, 0](f)(x, x) + 2*D[0, 1](f)(x, x) + D[1, 1](f)(x, x)

of which the latter has to be improved according to Nils.

I'd say the first ones need to be improved as well: if diff(sin,t)==cos(t) then diff apparently evaluates sin at t and then differentiates it. I think it's very confusing to let diff evaluate its argument.
For instance, if f=(x^2).function(x) then f is callable, so extending the above definition would give diff(f,t)==diff(t^2,t)=2*t. Instead, we have the perfectly acceptable diff(f,t)== (0*x).function(x): f is perfectly understood to be a function in x, and hence differentiating with respect to t would involve the derivative of x wrt. t, and x is not indicated to depend on t.

Having derivatives of functions with anonymous argument with respect to a named variable result in error seems desirable to me and that's the current behaviour.

So I'd advocate for closing this ticket as "notabug/wontfix".

@nbruin nbruin removed this from the sage-7.2 milestone Dec 4, 2021
@tscrim
Copy link
Collaborator

tscrim commented Dec 5, 2021

comment:15

I agree with you that there is no reasonable well-defined behavior and anything other than an error will likely cause confusion. However, the error message is a bit cryptic, and suggests to me that this behavior might accidentally be changed. Perhaps we can try to clean that up a bit?

@nbruin
Copy link
Contributor

nbruin commented Dec 5, 2021

comment:16

Replying to @tscrim:

I agree with you that there is no reasonable well-defined behavior and anything other than an error will likely cause confusion. However, the error message is a bit cryptic, and suggests to me that this behavior might accidentally be changed. Perhaps we can try to clean that up a bit?

Sure, catching the call and raising a different error would be fine with me.

@mkoeppe mkoeppe closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants