Skip to content

Commit

Permalink
Trac #17684: density_plot() is broken with functions involving symbol…
Browse files Browse the repository at this point in the history
…ic expressions

As reported on [http://ask.sagemath.org/question/25617/density_plot-
defed-python-function-typeerror/ this ask question], the following does
not work:

{{{
sage: f1(a, b) = 1 - b / a
sage: f2(a, b) = 1 - a / b
sage: def f12(a, b):
....:         if a - b < 0:
....:                 return f1(a, b)
....:         else:
....:                 return f2(a, b)
sage: density_plot(f,(1,2),(1,2))
KeyError: 'text/plain'
}}}

While the following works:

{{{
sage: f1(a, b) = 1 - b / a
sage: f2(a, b) = 1 - a / b
sage: def f12(a, b):
....:     if a - b < 0:
....:         return RDF(f1(a, b))
....:     else:
....:         return RDF(f2(a, b))
sage: density_plot(f12,(1,2),(1,2))
}}}

URL: https://trac.sagemath.org/17684
Reported by: tmonteil
Ticket author(s): Dave Morris
Reviewer(s): Karl-Dieter Crisman
  • Loading branch information
Release Manager committed Mar 1, 2021
2 parents e7399bf + bb5a812 commit 826e5c1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/sage/plot/density_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,14 +294,21 @@ def f(x,y): return x**2 * cos(x*y)
sage: density_plot((x*y)^(1/2), (x,0,3), (y,0,500))
Graphics object consisting of 1 graphics primitive
Check that :trac:`17684` is fixed, i.e., symbolic values can be plotted::
sage: def f(x,y):
....: return SR(x)
sage: density_plot(f, (0,1), (0,1))
Graphics object consisting of 1 graphics primitive
"""
from sage.plot.all import Graphics
from sage.plot.misc import setup_for_eval_on_grid
from sage.rings.real_double import RDF
g, ranges = setup_for_eval_on_grid([f], [xrange, yrange], options['plot_points'])
g = g[0]
xrange, yrange = [r[:2] for r in ranges]

xy_data_array = [[g(x,y) for x in xsrange(*ranges[0], include_endpoint=True)]
xy_data_array = [[RDF(g(x,y)) for x in xsrange(*ranges[0], include_endpoint=True)]
for y in xsrange(*ranges[1], include_endpoint=True)]

g = Graphics()
Expand Down

0 comments on commit 826e5c1

Please sign in to comment.