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

fast_float yields infinity when Python does, but should handle bigger numbers #13559

Open
nthiery opened this issue Oct 2, 2012 · 10 comments
Open

Comments

@nthiery
Copy link
Contributor

nthiery commented Oct 2, 2012

It sounds like the adaptative algorithm fails to find suitable
evaluation points when plotting large functions (like exp) in loglog
scale. In the following example, the exp function gets drawn with only
three points:

sage: plot([n^2,exp(n)], xmin=1, xmax=10^5, ymin=1,ymax=10^10, scale="loglog")
verbose 0 (2397: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 198 points.
verbose 0 (2397: plot.py, generate_plot_points) Last error message: ''

If xmax is replaced by 10^10, the function is not even drawn:

sage: sage: plot([n^2,exp(n)], xmin=1, xmax=10^10, ymin=1,ymax=10^10, scale="loglog")
verbose 0 (2397: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 199 points.
verbose 0 (2397: plot.py, generate_plot_points) Last error message: ''

On the other hand, the equivalent semilogy plot works smoothly:

sage: plot([10^n,exp(10^n)], xmin=0, xmax=5, ymin=1,ymax=10^10, scale="semilogy")
verbose 0 (2397: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 86 points.
verbose 0 (2397: plot.py, generate_plot_points) Last error message: ''

(Such plots are typically useful in classes about algorithmic complexity http://combinat.sagemath.org/doc/thematic_tutorials/agregation-option-calcul-formel/tris_et_complexite.html)

Depends on #15030

CC: @eviatarbach

Component: basic arithmetic

Keywords: agregation

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

@nthiery
Copy link
Contributor Author

nthiery commented Oct 2, 2012

Changed keywords from none to agregation

@ppurka
Copy link
Member

ppurka commented Oct 3, 2012

comment:2

It is not a problem with loglog. There is the same problem with normal plots.

sage: var('n')
sage: plot([n^2,exp(n)], xmin=1, xmax=10^5, ymin=1,ymax=10^10)
verbose 0 (2395: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 198 points.
verbose 0 (2395: plot.py, generate_plot_points) Last error message: ''

FYI, the change to loglog/semilog* scale happens only during the very end when show() is called. This happens after the generation of the plot points. Though I can't understand why semilogy is working fine for you (you still get the warnings though).

@nthiery
Copy link
Contributor Author

nthiery commented Oct 4, 2012

comment:3

Replying to @ppurka:

FYI, the change to loglog/semilog* scale happens only during the very end when show() is called. This happens after the generation of the plot points.

Really??? Ouch! In a x log scale one certainly would want to disperse ploting points differently.

@kcrisman
Copy link
Member

kcrisman commented Oct 4, 2012

comment:4

FYI, the change to loglog/semilog* scale happens only during the very end when show() is called. This happens after the generation of the plot points.

Really??? Ouch! In a x log scale one certainly would want to disperse ploting points differently.

True, but that would be another ticket. And sometimes one would want to plot the same data in two different ways, so we wouldn't want to remove that entirely.

@nthiery
Copy link
Contributor Author

nthiery commented Oct 14, 2012

comment:5

Hi!

Replying to @kcrisman:

True, but that would be another ticket.

Well, unless there is a quick solution for just the issue stated in this ticket, I am happy recycling it to whatever the right fix should be (taking into account the log scale early or delaying the generation of the evaluation points to show).

And sometimes one would want to plot the same data in two different ways, so we wouldn't want to remove that entirely.

I don't know the current implementation, so there might be technical obstructions I can't see; however, in principle, isn't the data really the function rather than the points? In that case, should'nt the points just be recalculated as needed?

@ppurka
Copy link
Member

ppurka commented Oct 15, 2012

comment:6

Replying to @nthiery:

I don't know the current implementation, so there might be technical obstructions I can't see; however, in principle, isn't the data really the function rather than the points? In that case, should'nt the points just be recalculated as needed?

Suppose you want to plot the points (0, 1), (1, 10), (2, 100), (3, 1000). Then what you could do is send these points to matplotlib and ask it to plot it in a linear scale by using, say, pyplot.plot(x, y). Alternatively, if you want semilogy plot, you could do pyplot.semilogy(x, y), where x and y are the data points along the x and y axes. Note that we do not send the "linearized" data points [0, 1, 2, 3] (obtained by taking log of [1, 10, 100, 1000] to the base 10) as the y list to matplotlib.

Now, suppose you want to plot 10**n for large values of n. You would still do the same thing. Find the values of this function in the linear scale and then pass on the computed values to matplotlib to plot it on the logarithmic scale. In either case, the computation of the values of the function is being done on the linear scale. And it is this computation that is failing in your examples. As of now, this problem needs a fix even on the linear scale, let alone the log scale.

@ppurka
Copy link
Member

ppurka commented Oct 15, 2012

comment:7

In fact, I just realized why you are getting the errors. The problem is with fast_float.

sage: set_verbose(1)
sage: p = plot_loglog(exp(x), (1, 10^5), plot_points=2)
verbose 1 (2397: plot.py, generate_plot_points)
Unable to compute f(100000.0) (time = 19.237264)
sage: exp(100000.0).n()
2.80666336042612e43429
sage: from sage.ext.fast_eval import fast_float
sage: f(x) = exp(x)
sage: v = f.variables()
sage: F = fast_float(f, *v)
sage: F(100000.0)
inf

Maybe you are better off generating the list of data points by using exact arithmetic in Sage and then passing off the list to list_plot.

@kcrisman
Copy link
Member

comment:8

In fact, I just realized why you are getting the errors. The problem is with fast_float.

sage: from sage.ext.fast_eval import fast_float
sage: f(x) = exp(x)
sage: v = f.variables()
sage: F = fast_float(f, *v)
sage: F(100000.0)
inf

Huh, that is not good.

sage: F(709.7)
1.6549840276802644e+308
sage: F(709.8)
inf

That's as much bisecting as I want to do. And really, here is what is going on, I suspect.


In [3]: import math

In [6]: math.exp(709.8)
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)

/Users/.../<ipython console> in <module>()

OverflowError: math range error

In [7]: math.exp(709.7)
Out[7]: 1.6549840276802644e+308

So fast_float really is doing floats, but we need something better than that.

@kcrisman kcrisman changed the title loglog plots of "large" function fail to find good evaluation points fast_float yields infinity when Python does, but should handle bigger numbers Oct 15, 2012
@kcrisman kcrisman assigned aghitza and unassigned jasongrout and williamstein Oct 17, 2012
@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@ppurka
Copy link
Member

ppurka commented Sep 14, 2013

Dependencies: #15030

@ppurka
Copy link
Member

ppurka commented Sep 14, 2013

comment:11

The solution of this ticket depends on #15030 and this ask.sagemath thread.

How? I suppose we can introduce a plot keyword precision=53 that gets passed on to fast_callable and one can increase that to get higher precision but slower plots.

@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@mkoeppe mkoeppe removed this from the sage-6.4 milestone Dec 29, 2022
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

8 participants