Opened 5 years ago

Last modified 5 years ago

#22401 new defect

Let substitute_function handle anon functions explicitly

Reported by: rws Owned by:
Priority: major Milestone: sage-7.6
Component: symbolics Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by rws)

substitute_function expects two function objects as argument not expressions (f(x)). This leads to the (to some users) unexpected:

sage: f = function('f')(x)
....: g = function('g')(x)
....: df = f(x).diff(x)
sage: f.substitute_function(f,g)
sage: f(1).substitute_function(f,g)
sage: df.substitute_function(f,g)
diff(f(x), x)
sage: df(1).substitute_function(f,g)

(same with f(x)=function...)

The problem is that f and g are not function objects like sin. Taking this into account:

sage: f.substitute_function(f.operator(), g.operator())
sage: f(1).substitute_function(f.operator(), g.operator())
sage: df.substitute_function(f.operator(), g.operator())
diff(g(x), x)
sage: df(1).substitute_function(f.operator(), g.operator())

The ticket should make substitute_function raise an exception with a hint. Note that the mess results from function('f') returning a function object but function('f')(x) returning f(x) and users assigning this to f or f(x) instead of just doing

sage: f=function('f')
sage: f(1).substitute_function(f,g)

Documentation of substitute_function could be made more explicit as well.

Change History (2)

comment:1 Changed 5 years ago by nbruin

The problem that "function" returned f(x) has been fixed since #17447, so I don't think this is an issue anymore. You can see it in the syntax that is used in the description: in function('f')(x) a function gets called explicitly to turn it into an expression.

Rather than substitute_function trying to guess how to sanitize its arguments, perhaps it should raise an exception if it can tell its argument will for sure not appear in the "function" slot.

comment:2 Changed 5 years ago by rws

  • Description modified (diff)
  • Summary changed from Let substitute_function handle anon functions smoothly to Let substitute_function handle anon functions explicitly
Note: See TracTickets for help on using tickets.