Wednesday, December 22, 2010

Using Lush with Gnuplot

I had not been using Lush for about a year and yesterday I wanted to write a few lines of code to recreate a plot from a linear algebra textbook. This is the sort of thing that people turn to Matlab for: write a few lines of code to generate some data, call plot once or twice, done. When I am on the top of my game I can do the same with Lush, it would not take me more than ten minutes.


The plot in question shows the roots of Wilkinson's polynomial in the complex plane and illustrates that polynomial root finding is an ill-conditioned problem. The thicker black dots are the known roots of the polynomial and the many little red dots are the roots returned by a polynomial root finder after perturbing the polynomial's coefficients slightly. More precisely, the program randomly perturbed the coefficients, called the root finder and plotted the roots in red. Repeating this 100 times you get a plot like the one above.

The point of the plot is that the exact location of the roots as a function of the polynomial coefficients is very sensitive for this polynomial. Perturbing the coefficients cause changes in the roots' locations many orders of magnitude larger than the changes in the coefficients.

The point of this blog post, on the other hand, is to show how easy it is to generate that plot in Lush. Here is the function that created the plot (the complete script is in Lush's demos directory).

(defun wilkinson-demo (&optional (n-pertubations 100))
(let* ((p *p-wilkinson*)
(n ($n *p-wilkinson*))
(s ($> [@ 1e-10] n-pertubations n))
(coeffs (* ($< p n-pertubations) (+ 1 (rand s))))
(plotter (new Gnuplot 'interactive ())) )
(plot
(title "Roots of Wilkinson's polynomial")
(points (++> *roots-wilkinson* [0]) (lc 'black) (pt 7) (ps 1))
(do ((c coeffs))
(points (poly-solve c) (lc 'red) (pt 7) (ps 0.2)) ))
plotter))

Just a few lines, nice. But yesterday it took me more than half a day to get this done. Not because I forgot so many things about the language, but because I had not fully rebuilt everything I needed after upgrading my laptop a few months back: The gnuplot installed on Ubuntu simply did not work with Lush and I had to figure out/remember why.

Long story short, if you want to use gnuplot with Lush (you do), then you need to build it yourself. That is, download the sources, install all the development packages for the dependencies that make gnuplot most powerful and pleasant to use (libreadline, wxWidgets, cairo, pango, libgd), and build it.

I installed the most recent version 4.4. and the gnuplot demos in Lush all worked fine.

No comments: