Opened 7 years ago

Last modified 7 months ago

#11837 new enhancement

Make Newton basin plotting fun and easy

Reported by: kcrisman Owned by: jason, was
Priority: major Milestone:
Component: graphics Keywords: fractal newton complex plot
Cc: jason, evandel, beezer Merged in:
Authors: Karl-Dieter Crisman, Simon King Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description

One of the best fractals for actually finding out new things is looking at Newton basins - see David Joyce's beautiful generator, for instance.

This is not yet in Sage. It should be, and isn't that hard to get a rough mockup by modifying the complex plot code.

Attachments (8)

newton_basins.spyx (7.1 KB) - added by kcrisman 7 years ago.
VERY rough draft of eventual file - not a Mercurial patch
cubicbasin.png (6.7 KB) - added by kcrisman 7 years ago.
What you get with this slow implementation - at least it works!
Betterfractal.png (12.8 KB) - added by kcrisman 7 years ago.
With Simon's improvements
WithIterations.png (43.7 KB) - added by kcrisman 7 years ago.
With Simon's Cython and naive keeping track of iterations needed to converge
newton_basins_more_cython.spyx (7.3 KB) - added by SimonKing 7 years ago.
Put more cython into the basins
newton_basins_with_iterations.spyx (8.3 KB) - added by kcrisman 7 years ago.
Incorporates Simon's improvements and adds naive keeping track of iterations
newton_basins_with_iterations_and_double.spyx.txt (8.7 KB) - added by SimonKing 7 years ago.
Naive keeping track of iterations, using a simplified distance test
newton_basins_with_iterations_and_double.spyx (8.6 KB) - added by gh-bryangingechen 7 months ago.
A version of "newton_basins_with_iterations_and_double.spyx.txt​ (8.7 KB) - added by SimonKing?" minimally updated to work with SageMath 8.2

Download all attachments as: .zip

Change History (30)

Changed 7 years ago by kcrisman

VERY rough draft of eventual file - not a Mercurial patch

comment:1 Changed 7 years ago by kcrisman

Here is how I envision one using the current file.

  • Download the raw file.
  • Cut and paste the entire file into one notebook cell.
  • Prepend the line %cython to the cell.
  • Evaluate.
  • Have fun!

With this code, the next picture attached gives at least something recognizable after a few minutes. It needs MUCH more efficient use of Cython, primarily; the algorithm is right, as far as I can tell, except maybe the counter can be ditched or needs to be improved.

Changed 7 years ago by kcrisman

What you get with this slow implementation - at least it works!

comment:2 Changed 7 years ago by kcrisman

The picture is of the fractal associated with (x^2-1)(x-I). The code is

basin_plot([-1,i,1],(-3,3),(-3,3),plot_points=100,figsize=7)

Note that the documentation in the spyx file is all wrong. This is VERY rough, as noted above.

Feedback welcome!

comment:3 Changed 7 years ago by SimonKing

If you evaluate the contents of your spyx file in a %cython cell in the notebook, you will find two links just below the cell. The first contains the c-file created from the cython code. The second is the annotated version of the code.

I suggest to look at the annotated code. The lines of the cython code are individually coloured in different shades of yellow. If a line is very dark then the single cython line corresponds to many c-lines. By clicking on the line number, you can see how each line is translated into c.

You will find that your code is mostly dark yellow. If you want to make it fast, the lines that are most frequently executed should be white.

Another tool: Use %prun on your functions (but it could be that it will only work on the command line - I tried in the notebook, but it didn't work). It will show you the internal Python function calls that took the most time (I think it can not show you calls to Cython functions). So, %prun may give you an idea what part of your code needs work most urgently.

Using

sage: %prun basin_plot([-1,i,1],(-3,3),(-3,3),plot_points=100,figsize=7)

on the command line, where I had replaces 2/3 by 2./3 in the code, I found:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    5.396    5.396   15.362   15.362 {_home_king_SAGE_work_attributes_newton_basins_spyx_1.basin_plot}
104309/71386    3.291    0.000    5.029    0.000 complex_interval_field.py:261(__call__)
  1049154    0.771    0.000    0.771    0.000 {isinstance}
    59576    0.700    0.000    0.721    0.000 polynomial_ring.py:301(_element_constructor_)
38450/19225    0.638    0.000    3.372    0.000 complex_field.py:279(_element_constructor_)
    38458    0.521    0.000    3.043    0.000 qqbar.py:2951(interval)
    59576    0.507    0.000    1.629    0.000 polynomial_ring_constructor.py:47(PolynomialRing)
    57675    0.474    0.000    2.629    0.000 complex_field.py:246(__call__)
    59576    0.297    0.000    0.369    0.000 {sage.structure.parent_gens.normalize_names}
    38458    0.296    0.000    0.597    0.000 qqbar.py:2927(interval_diameter)
   186759    0.262    0.000    0.262    0.000 complex_interval_field.py:242(_real_field)
    59576    0.215    0.000    0.733    0.000 polynomial_ring_constructor.py:442(_single_variate)
    13738    0.213    0.000    0.307    0.000 number_field.py:5075(_coerce_non_number_field_element_in)

So, it seems to me that most time is spent for internal calls to the complex interval field, and to the function interval in qqbar.py. That may be surprising, because, if I am not mistaken, you simply want to work with complex numbers, but not with algebraic numbers.

In other words, it would be worth while to find out how your code uses complex interval fields and algebraic numbers, and to rewrite it such that only "usual" complex doubles are used. It may very well be that the algebraic numbers arise in a coercion happening behind the scenes.

comment:4 follow-up: Changed 7 years ago by kcrisman

Simon,

Thanks for the prun tip - I had forgotten about that. I'm certainly aware of the yellow html :) but unfortunately it was nearly ALL yellow and I'm not sure how to effectively Cythonize much of it, hence my email to sage-devel/edu.

Just adding cdefs is, in my experience, a recipe for disaster.

Also interesting about the prun. This is mostly just taken from the complex plot stuff, but prunning (?) that shows just a couple calls to complex intervals. I assume it is mostly happening in the which_root, maybe if abs(varia-root)<2/3. I don't like all the calls to isinstance and the polynomial ring consructors, either. Someone who understands how fast_callable constructs things better might be able to help, too :(

Thank you!

comment:5 in reply to: ↑ 4 Changed 7 years ago by SimonKing

Replying to kcrisman:

Just adding cdefs is, in my experience, a recipe for disaster.

That's true. One first needs to have a good guess what yellow code is most frequently executed. %prun might indicate where the problem is hidden, and the annotation indicates how to get rid of it.

Also interesting about the prun. This is mostly just taken from the complex plot stuff, but prunning (?) that shows just a couple calls to complex intervals. I assume it is mostly happening in the which_root, maybe if abs(varia-root)<2/3.

It makes sense to try that separately. I.e., %prun one call to which_root.

I don't like all the calls to isinstance and the polynomial ring consructors, either.

Yes. They are irritating. I am rather sure that isinstance is called in things like sage.rings.ring.is_Ring or so.

Someone who understands how fast_callable constructs things better might be able to help,

Sorry, fast_callable is a mystery to me.

comment:6 Changed 7 years ago by SimonKing

Here are some more detailed remarks on the code:

  • In which_root, the expression abs(varia-root) seems to be rather expensive. If you know the type of varia and root and if you know how abs is computed for that type, you may help the compiler. See below: Indeed that seems to be part of the problem.
  • Since which_root is frequently called, it would make sense to improve the inner loop:
    cdef int counter
    for counter from 0<=counter<20:
        varia = newtf(varia)
        ...
    
  • Again, since which_root is frequently called, it would make sense to reveal that roots is a list (could be done in the argument line of basin_plot`).
  • I just tested that one gets
    AttributeError: 'function' object has no attribute 'variables'
    
    when trying to create f1 as a fast callable. I don't know how that can be worked around.
  • The strange calls to complex interval field and so on really occur when you do varia-root. This is because varia is a complex double, but root (depending on the input) may be a symbolic expression. In the application, you have
    sage: roots = [-1,i,1]
    sage: prefunc = prod([(x-root) for root in roots])
    sage: newtf = fast_callable(newt(prefunc), domain=CDF, expect_one_var=True)
    sage: varia = newtf(roots[1])
    sage: root = roots[1]
    sage: type(varia)
    <type 'sage.rings.complex_double.ComplexDoubleElement'>
    sage: type(root)
    <type 'sage.symbolic.expression.Expression'>
    sage: %prun varia-root
    
       Ordered by: internal time
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.000    0.000    0.001    0.001 <string>:1(<module>)
          4/3    0.000    0.000    0.000    0.000 complex_interval_field.py:261(__call__)
          2/1    0.000    0.000    0.000    0.000 complex_field.py:279(_element_constructor_)
           39    0.000    0.000    0.000    0.000 {isinstance}
            2    0.000    0.000    0.000    0.000 qqbar.py:2951(interval)
            2    0.000    0.000    0.000    0.000 polynomial_ring.py:301(_element_constructor_)
            3    0.000    0.000    0.000    0.000 complex_field.py:246(__call__)
            2    0.000    0.000    0.000    0.000 polynomial_ring_constructor.py:47(PolynomialRing)
            2    0.000    0.000    0.000    0.000 qqbar.py:2927(interval_diameter)
    ...
    

So, it would make sense to first convert the given roots to complex doubles. If that isn't good enough, one may even cdef them as such. And, in addition, cythonise the innermost loop. I'll soon test if it helps...

comment:7 Changed 7 years ago by SimonKing

  • Authors changed from Karl-Dieter Crisman to Karl-Dieter Crisman, SImon King

I just attached newton_basins_more_cython.spyx, which improves the timing by a factor of almost 100:

With the original version of the code together with the 2./3, I get

sage: %time basin_plot([-1,i,1],(-3,3),(-3,3),plot_points=100,figsize=7)
CPU times: user 13.70 s, sys: 0.00 s, total: 13.70 s
Wall time: 13.74 s

With the code that I just attached, I get

sage: %time basin_plot([-1,i,1],(-3,3),(-3,3),plot_points=100,figsize=7)
CPU times: user 0.15 s, sys: 0.00 s, total: 0.15 s
Wall time: 0.15 s

How is that done?

Apparently, the optimization should mainly take place in which_root. Since it is not possible to turn it into a fast_callable, I cythonised it instead. More precisely, I introduced a cdef'd class RootFinder, that carries all relevant data (the list of roots, the function newtf and also the cutoff 2/3) as a cdef'd attribute.

Originally, the function f1 is a lambda function (slow!) that calls the Python function which_root (slow!) by providing two arguments, where the first argument is always the same (redundant!). I replaced it by the method Finder.which_root, where Finder is an appropriate instance of RootFinder.

Inside which_root, further improvements happened:

  • First of all, the list of roots is transformed into a list of complex doubles (because, in your original example, the complex unit i is a symbolic expression, not a complex double, which was responsible for the slowness).
  • Moreover, Cython is informed that the list of roots is a list, and that both the root and "varia" are complex doubles.
  • We know that all our complex doubles have the same parent. Hence, we avoid the parental tests that are part of __sub__ and call _sub_ instead (single underscore).
  • Since the counter loop is rather tight, counter is declared as an int.

comment:8 Changed 7 years ago by kcrisman

This is great work, Simon. I'll try it out over the weekend. All of these ideas make a lot of sense, but it would have taken me many, many hours of stumbling to do things like this. The only one I did after I posted last night was to make counter an int :)

Even the list of roots all being complex doubles is very sensible and should have occurred to me immediately, but never did - and I wouldn't have know how to Cythonize that in any case, I only knew about the most basic Cython types. There are all kinds of optimizations in this code of that kind, and I doubt I would have ever gotten there on my own. But it makes a GREAT case study in how to use Cython, because there are so many places things are improved.

Thanks.

Changed 7 years ago by kcrisman

With Simon's improvements

comment:9 Changed 7 years ago by kcrisman

Okay, this is great.

basin_plot([-1,1,3],(-.2,.1),(-.1,.1),plot_points=1000,figsize=7)

is here. It only takes 20 seconds. Still not ideal, but usable for this kind of detail.


I still feel like there are possible speedups - especially in terms of doing something smarter than just searching through all the roots each time looking for whether they are close enough. But I think that this is probably sufficient for turning into a patch. I would want to base it on the long-suffering #11273, which I need to finally finish reviewing, and which has modularized the color plotting a bit. Thank you VERY much for this teamwork.

Changed 7 years ago by kcrisman

With Simon's Cython and naive keeping track of iterations needed to converge

comment:10 Changed 7 years ago by kcrisman

Okay, now I have something very naive with iterations. Picture (same code) here and code here.

Caveats:

  • I am pretty sure that the lightness thing could be improved - there are infinitely many functions to pick from, and messing with the one from complex_plot didn't help in my first tries.
  • The way the alpha is used is almost assuredly inefficient. Ideally we would have a 'master list' of the roots and then one would search for one of the n times 20 combinations of root and iteration level, and assign a color to that from the 'master list' (dictionary?) instead of multiplying by alpha 3 times for every single point!
  • Probably the way I just slapped in a tuple for the new output of the which_root is inefficient too - presumably one could at least define the type coming out of it, if not find some better way to represent it.
  • The exact (blue) circle in the picture, which is not flush with the second iteration ellipse, probably indicates that the way the iterations are determined is too aggressive/not aggressive enough (not sure which).

But it does work, and even looks somewhat decent. Can't compete with Java yet, but it would be really nice to have this in Sage.

comment:11 Changed 7 years ago by SimonKing

Outch! I just saw that my code had a bug: In the loop for counter, I had for counter from 0<=counter<20, and additionally I had counter += 1. So, both my and your new spyx file need to be modified.

Changed 7 years ago by SimonKing

Put more cython into the basins

comment:12 Changed 7 years ago by SimonKing

Just updated my patch. I hope that this change does not alter the pictures too much.

comment:13 Changed 7 years ago by SimonKing

Here is another slowness of the current code: varia._sub_(root).abs(), which needs to compute a square root. But in order to find out whether varia._sub_(root).abs()<2/3, we could more easily test whether the sum of the squares of real and imaginary part is smaller than 4/3.

Changed 7 years ago by kcrisman

Incorporates Simon's improvements and adds naive keeping track of iterations

Changed 7 years ago by SimonKing

Naive keeping track of iterations, using a simplified distance test

comment:14 Changed 7 years ago by kcrisman

Thanks for catching that!

comment:15 follow-up: Changed 7 years ago by SimonKing

I have attached newton_basins_with_iterations_and_double.spyx.txt. I am sorry that I have not been aware of Windows' automatic name extension (I don't usually write texts under windows), and also I hope that it did not use any fancy format.

Also I just noticed that the first line of the file contains %cython - of course that needs to be removed if you want to attach it.

Anyway. The trick is to define self.cutoff = 4./9, and to use a temporary cdef ComplexDoubleElement diff = varia._sub_(root), so that (diff._complex.dat[0]**2 + diff._complex.dat[1]**2) < self.cutoff is fairly cheap.

Here are the data on sagenb:

%time
basin_plot([-1,1,3],(-.2,.1),(-.1,.1),plot_points=1000,figsize=7)
CPU time: 15.59 s,  Wall time: 15.60 s

The same computation with the first "iteration" code takes CPU time: 24.15 s, Wall time: 24.17 s.

comment:16 Changed 7 years ago by SimonKing

  • Authors changed from Karl-Dieter Crisman, SImon King to Karl-Dieter Crisman, Simon King

comment:17 Changed 6 years ago by kcrisman

Possibly also related: a "pure Python" GPU (I guess?) implementation by "kriskda" on github.

comment:18 Changed 18 months ago by kcrisman

Probably related: #23257

Changed 7 months ago by gh-bryangingechen

A version of "newton_basins_with_iterations_and_double.spyx.txt​ (8.7 KB) - added by SimonKing?" minimally updated to work with SageMath 8.2

comment:19 in reply to: ↑ 15 Changed 7 months ago by gh-bryangingechen

Replying to SimonKing:

I have attached newton_basins_with_iterations_and_double.spyx.txt. I am sorry that I have not been aware of Windows' automatic name extension (I don't usually write texts under windows), and also I hope that it did not use any fancy format.

I found this ticket when googling for Sage functionality to plot Newton basins. I guess this ticket has been abandoned, but just in case it's useful to anyone else that finds this, I've just attached newton_basins_with_iterations_and_double.spyx which updates this (mainly by adding / changing the imported modules) so that it works with my SageMath 8.2 (in the jupyter notebook on Mac OS X 10.13.4).

comment:20 follow-up: Changed 7 months ago by kcrisman

Tickets are never abandoned, only forgotten ... that is great news. If you would like to, we would appreciate a "formal" review of the code and attaching it as a branch. Basically, one wants someone to verify that the code is correct and that things work, and especially that there are sufficient tests and examples for people to use.

Simon, if you're listening, thoughts? No one person has to vouch for the entire package, it can be a review of previous people's work too.

comment:21 in reply to: ↑ 20 Changed 7 months ago by SimonKing

Replying to kcrisman:

Tickets are never abandoned, only forgotten ...

Indeed. Even after reading the attachment that I supplied, I don't recognise that I wrote it. So, total amnesia.

Simon, if you're listening, thoughts? No one person has to vouch for the entire package, it can be a review of previous people's work too.

What would be the plan?

The fact that the attachment is a .spyx file means that it used to be an experimental piece of code that people could play with by attaching it. In order to create a proper branch, it would be needed to find a folder in sage/src/ where the code could fit. And so far I am not sure where it could belong.

It could perhaps be classified as "educational". But do we have a dedicated folder for educational stuff?

comment:22 Changed 7 months ago by kcrisman

Actually I think it could fit in a few different folders. The dynamics may be appropriate - indeed, see this file which may duplicate some or all of this (it's quite likely). Weird things have landed in src/sage/calculus as well.

However, what I meant by asking you was whether you had already reviewed some of the code in said .spyx file. If not, that is fine, just checking.

Note: See TracTickets for help on using tickets.