Opened 8 years ago

Last modified 7 years ago

#13355 closed defect

Plot fails if a function implicitly needs complex intermediate values — at Version 2

Reported by: tkluck Owned by: burcin
Priority: major Milestone: sage-6.3
Component: symbolics Keywords: evalf, fast_callable
Cc: kcrisman, eviatarbach Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by tkluck)

Plotting currently fails when plotting a symbolic function that evaluates to complex values:

sage: def evalf_func(self,x,parent=None):
....:     return parent(I*x) if parent!=None else I*x
sage: f = function('f', evalf_func=evalf_func)
sage: plot(abs(f(x)),0,5)
verbose 0 (2392:, generate_plot_points) WARNING: When plotting, failed to evaluate function at 199 points.
verbose 0 (2392:, generate_plot_points) Last error message: 'unable to simplify to float approximation'

This is because plot uses fast_float, and fast_float cannot deal with the intermediate complex value.

The attached patch first changes these things:

  • it adds a method to class Function that allows one to see what output range is to be expected for a given input range. By default, it will return a complex field.
  • it overrides this method for builtin functions, which return floats for float input
  • this is then used by fast_float to throw an exception if complex values are to be expected

This exception will cause plot to fall back to fast_callable instead of fast_float. However, fast_callable needs patching too:

  • it now uses the _evalf_ method when available instead of just __call__. Otherwise, plot will try to coerce the result to float, which also cannot deal with complex intermediate values.

An alternative to this step would be to fix float coercion, which I haven't looked into.

Change History (3)

Changed 8 years ago by tkluck

comment:1 Changed 8 years ago by tkluck

  • Status changed from new to needs_review

comment:2 Changed 8 years ago by tkluck

  • Description modified (diff)
Note: See TracTickets for help on using tickets.