You all know the Newton method to approximate the roots of a function, don't you? My goal in this task is to introduce you into an interesting aspect of this algorithm.

Newton's algorithm converges only for certain, but most of all complex input values. If you picture the convergence of the method for all input values over the complex plane, you usually get a beautiful fractal like this:

Image from Wikimedia commons

Specifications

The goal of this task is, to generate such fractals. This means, that you get a polynomial as the input and have to print out the corresponding fractal as an image in a format of your choice as the output.

Input

The input is a whitespace-separated list of complex numbers. They are written down in the style <Real part><iImaginary part>, like this number: 5.32i3.05. You may assume, that the input number has no more than 4 decimal places and is smaller than 1000. The first of them must not be zero. For example, this could be an input to your program:

1 -2i7.5 23.0004i-3.8 i12 0 5.1233i0.1

The numbers are interpreted as the coefficients of a polynomial, beginning with the highest power. Throughout the rest of this specification, the input polynomial is called P. The above input is equal to this polynomial:

The input may come to you either from stdin, from an argument passed to the program or from a prompt displayed to your program. You may assume, that the input does not contain any leading or trailing whitespace characters.

Rendering

You have to render the fractal in the following way:

Choose as many colors as roots of P plus an extra color for divergence

For each number in the visible plane, determine whether the method converges and if yes to which root. Color the point according to the result.

Do not print rulers or other fancy things

Print a black point at the points, that are the polynomials roots for orientation. You may print up to four pixels around each root.

Find a way to choose the visible plane in a way, that all roots are distinguishable and spread widely across it, if possible. Although a perfect placement of the output frame is not required, I reserve the right to refuse to accept an answer that chooses the frame in an unacceptable way, eg. always at the same coordinates, all roots are in one point, etc.

The output image should have a size of 1024*1024 pixels.

Rendering time is 10 minutes maximum

Using single precision floating-point values is enough

Output

The output should be a raster graphics image in a file format of your choice, readable by standard software for a brand X operating system. If you want to use a rare format, consider adding a link to a website where one can download a viewer for it.

Output the file to stdout. If your language does not support putting something to stdout or if you find this option less convenient, find another way. In any way, it must be possible to save the generated image.

Restrictions

No image processing libraries

No fractal generating libraries

The shortest code wins

Extensions

If you like this task, you could try to color the points according to the speed of convergence or some other criteria. I'd like to see some interesting results.

It's easily broken down into separate tasks, and if your language supports complex floating point numbers natively then you can save a large chunk. I have a not-fully-golfed version running at 1600 chars, of which 340 are the complex number class. It doesn't yet identify the roots and use colours, but I'm trying to track down what I presume is a bug in the N-R code. (Finding a root of x^3-1 starting at -0.5+0.866i surely shouldn't diverge!)
–
Peter TaylorMay 10 '11 at 22:06

1

clarified: EIther part may be dropped if zero, but not both.
–
FUZxxlMay 12 '11 at 7:32

Finds display bounds (and roots) by finding convergence points for a bunch of random samples. It then draws the graph by computing the convergence points for each starting point and using a hash function to get randomish colors for each convergence point. Look very closely and you can see the roots marked.

The scale is chosen using the Fujiwara bound on the absolute value of the complex roots of a polynomial; I then multiply that bound by 1.5. I do adjust brightness by convergence rate, so the roots will be in the brightest patches. Therefore it's logical to use white rather than black to mark the approximate locations of the roots (which is costing me 41 chars for something which can't even be done "correctly". If I label all points which converge to within 0.5 pixels of themselves then some roots come out unlabelled; if I label all points which converge to within 0.6 pixels of themselves then some roots come out labelled over more than one pixel; so for each root I label the first point encountered to converge to within 1 pixel of itself).

@FUZxxl, the image is from the old version. I'll upload one with the rate of convergence later. But the problem with marking the roots is determining which pixel to mark. It's the classic problem that with floating point you can't use exact equality tests, so you have to compare to an epsilon. As a result, I don't have "canonical" values for the roots. I could mark pixels which converge in one step, but that doesn't guarantee to mark anything, and could mark a block of 4 pixels for a single root.
–
Peter TaylorMay 11 '11 at 14:17

@Peter Taylor: As you see, Keith Randall also found a solution to that problem. I added this requirement as an extra difficulty. One approach to do it would be to calculate the nearest pixel for each root and then checking each pixel for being equal to it.
–
FUZxxlMay 11 '11 at 15:31

@FUZxxl, you haven't understood my point. "The nearest pixel" of a root is not well defined. However, I can hack something in which might work for all the test cases you throw at it, and I get the impression that that will make you happy. I'm going to colour it white, not black, because that's more logical.
–
Peter TaylorMay 11 '11 at 15:43

My profile pic should soon change to x^6-9x^3+8, carefully designed by choosing the roots and then using Wolfram Alpha to simplify the polynomial. Ok, I cheated by swapping the hues around afterwards in the GIMP.
–
Peter TaylorMay 11 '11 at 22:55