Plotters Part II

October 7, 2010

Yesterday I started some ramblings about plotters. I am sorry to disappoint Alex who was looking foward to hearing about devious schemers. Instead, we have some brief thoughts about mathematical environments, plot libraries, and equation graphers. Tomorrow(ish) will be GUI apps.

Mathematical Environments. Many folk swear by gargantuan packages that provide a programming environment, algorithm libraries, graphics utilities, algebraic manipulation, and so on. Popular packages include IDL, Matlab, Maple and Mathematica . These are very powerful, but with a bit of a learning curve, and of course they all cost lots of dosh. The only free, open source, alternative I am aware of that tries to be a complete Matlab-like replacement is Sage. I haven’t tried this out myself, and would be interested to hear of experiences. The other thing worth mentioning is R which started life as a statistics package and seems to have ballooned into an all-singing all-dancing kind of thing. It has some fans, who have emerged in my recent comment stream, but I haven’t tried it yet. Well, I started trying to read some documentation and nearly lost the will to live.

Plot Libraries. For astronomers at least, the grandaddy is PGPlot, started in 1983 by Tim Pearson and still in everyday use by lots of people. The latest version is 5.2.2 from 2001. It is a library of primitive routines, callable in Fortran or C. Several people have written interactive front ends – WIP, PONGO, and QDP (see Part IV).  PLplot is intended as an updated version of PGPLOT. It has bindings to a very wide range languages, including C, Fortran, Python, Perl, Java, C++, and several others. If Java is your thing, you might also want to look at JFreeChart. If Python is your thing you might want Pyx. It has high level things as well as primitives, and because Python has an interactive mode, you can use it in somewhat the manner you might use Gnuplot. However, as you can see from this example it is distinctly less streamlined and friendly than Gnuplot. Matplotlib is another Python library with both primitives and high level stuff, and ambitions to be a complete environment, a la Matlab. It looks good, but I couldn’t make it work…  There are links to lots more Pythonesque bits and pieces here. So if you want to include plotting in your own programs, there is a lot of choice, but it looks to me like the standard choice these days is PLplot .

Equation Graphers. If all you want is a quick plot of a mathematical function, you can do with this with Gnuplot and cousins, but you are likely to want to explore it – pan and zoom, read out values. In this case an interactive GUI style application makes sense. On the Mac you have this pre-loaded with Grapher, which started life as Graphing Calculator, still independently available here for Mac and Windows. If you want an entertaining hour, you can listen to Ron Avitzur telling the strange story of this application. (There is also a written version of this story.) For Linux folk, there is the wonderfully easy Lybniz – why isn’t there a Macports version ? Another nice lightweight free MacApp is Edengraph but it can’t do some things that Grapher can – like 3D functions, vector fields, independent axis adjustment. Two fairly glitzy commercial applications are A&G Equation Grapher, its cousin Graphing Calculator 3D and Golden Software Grapher. Graphing Calculator 3D has a free version.

Enough for now. Part III will be about GUI-style plotting applications.

Python Provocation

October 2, 2010

Posting slowed down a bit recently. A few life difficulties took precedent followed by almost two solid weeks of chairing the Astronomy Grants Panel. Doesn’t feel right to write about that in any detail for obvious reasons, but I will just say that this article in THES suggesting that we would better off with a lottery is a complete pile of dingo’s kidneys. Maybe I will work up on a post on that, as it made me cross.

But maybe I am just in tetchy mood.  I am even falling out of love with Python.

Being a chap of a certain age, I spent a long time stubbornly persisting with Fortran until it got too embarassing to admit. So I mugged up C. Pretty good, but didn’t feel right. Next up Java. Hello World etc. Java was just too strict and boring and pernickety. Engineer’s language really, not a scientist’s language or a hacker’s language. All that object oriented stuff. Mystical mumbo jumbo. I like algorithms ! Give me procedures !!!

I was getting fed up. Then someone said “try Python” and reluctantly I did. Then, lo, all was warmth and happiness, and the light shone upon the face of the deep. It was easy and flexible, and object oriented but not so you really noticed. You could use it interactively, or write simple scripts, or build massive symphonies if you wished. It came with all sorts of internet goodies built in. Most important, it was extensible and had community backing. Numpy/Scipy seemed the right thing to back, and astro stuff was appearing.

But it still seems a bit ugly, and I have to keep a big notes file full of tricks and reminders of how to do things, cos somehow it doesn’t stick from one month to the next unless you keep using it. Installing and updating stuff is getting gradually easier, but still clunky. There are weird incompatibilities between one version and the next within 2.x, whereas you’d have thought the 2.x world should have been completely backwards compatible, with 3.x becoming a new world. I hear from some developer contacts that this is even more of a problem for key packages like Scipy, with quite frequent changes to the API which mean that your scripts keep breaking.

Then of course there is the speed thing. One of my favourite packages is Pyxplot, a kind of re-imagining of Gnuplot. (I will be writing a post about this and other plotters sometime soon..) Pyxplot used to be written in Python, but the latest version has been completely re-implemented in C. I asked Dominic Ford why, and he explained that it was now ten times faster and took a tenth of the memory. Hard to argue with that.

Python occupies a strange territory between the easy peasy world of “download this app and start clicking” and the stern world of “if you don’t know what a makefile is, you’d better look somewhere else mate”. At first I thought this was precisely its strength : grown up stuff for busy people. But now I ain’t so sure. Neither use nor ornament, as EG used to say.

Over to you Ross.

Calculators that do what you want

June 7, 2009

I have been on a hunt for the sort of calculator a scientist really wants, and thought I would share some results with youse all. But first let me explain :

I often feel the need for a quick calculation whose complexity is somewhere  between a simple sum and a full blown programme. The sort of thing I have in mind is “how much rest mass energy is in that star ?”, or “what would be the black body luminosity from that object?”. You don’t want to write and compile a C program with loops and so on; but neither do you want to navigate lots of menus and laborious button clickings. You just want to jump in and type 2.3 * Msun * c^2 and get the answer. Or for the second example, something like R=3.28e6 and then T=11000 and then 4*pi*R^2 * BB(R,T); and then T=12000 and repeat. You get the idea.

Unix purists would now explain that you have some of this functionality pre-supplied with bc , and in fact you can do calculations with awk, but these are pretty clunky. These days I do this sort of thing with Python in interactive mode. After firing up Python I load up a short module containing some constants – i.e. I put this file in my PYTHONPATH and type  from astroconst import *.  Then indeed I can just type 2.3 * Msun * c**2. I keep meaning to add some simple functions to address the second type of example, but haven’t gotten round to it. Then I began to wonder the other day whether there are tools out there somewhere that have already done this, so I did a bit of a web crawl. There are gazillions of online and downloadable things that emulate traditional button calculators, some of which have good maths and science-y buttons, but this is not what I was after. There are also web based things for specific purposes – like Ned Wright’s excellent cosmocalc – but again not what I was looking for.

I turned up several very interesting and impressive tools. Give ’em a go.

Plain Calc. Mac only. Plain is the word. When you launch it, there is nothing but an empty white window. Beautiful ! But you can type expressions, create variables and use them, and define one-line functions. The history of what you have done scrolls back up, just like a terminal window. You can save your current worksheet and reload it later; so this way you can save your favourite astro constants etc, and get them back tomorrow. However, what you save and reload is literally the worksheet you were in the middle of, complete with the whole history, whereas what you really want is to simply save the variable and function definitions. So… this app is really fantastic and almost exactly what you want, but … maybe just too plain for some; Mac only; and creating your saved “astro-world” is a bit clunky.

PEMDAS Another Mac App. Similar to Plain Calc but with just a little more GUI-ness, so you can for example see your saved variables at a glance. On the negative side, it doesn’t have user-defined functions. Its at an early stage (0.2.4) so a lot of things could improve… Once its finished its going to cost money – but not much.

Google calculator Now why didn’t I know this before ? In any Google search bar, try typing 3.42*sqrt(13)/(4.2*sin(16)). Even weirder, try G/h. The beast obviously knows some physical constants as well as maths functions. But the “more about calc” link above doesn’t tell you what constants are available….

SpeedCrunch This is available for Windows, Mac, and Linux. Like PEMDAS it combines a plain typing window with GUI elements, but you can switch these on and off. (e.g. you can have it with or without a keypad). It is very nicely designed and easy to use. You can define variables and display their current state. It has an extensive set of built-in constants, but you apply these by clicking on them. They don’t have variable-like names that you can type (e.g. “Msun”).  SpeedCrunch has a very extensive built in function list, but doesn’t have user defined functions, although it seems this is on the to-do list. Like Plain Calc, you can save your session, and when re-loaded you get the whole history. So… this a fantastic app, and is clearly going to get even better, but … its a shame it doesn’t have user-defined functions, and the lack of names for the constants is a niggle.

Python As discussed above, running Python in interpreter mode does pretty much what you want. It comes pre-supplied with Mac and most Linux distros, and is pretty easy to install on Windows.  It has a wide set of functions, you can create variables as you wish, and you can define functions. You can write your own modules with a library of constants or simple functions which you can then use. So thats pretty much everything..  But … there are some useability niggles. (i) Integer division gives the “floor”, i.e. 7/2 gives 3, and 7/9 gives 0. If you really want 7.0/2.0 = 3.5 you have to explicitly put the decimal points in. (ii) Results automatically come with seventeen figs, until a number is too big. So 1.3e7*2.4e9 gives 31200000000000000.0 whereas 1.3e8*2.4e9 gives 3.12e17. If you are writing a module, you can format output however you like, with all that %f stuff etc. But in interpreter mode you are stuck with the default. There doesn’t seem to be a command for “switch to 3 sig figs” or “output everything in exponential notation”. (iii) Python stores reals as integer fractions. This produces some peculiar results. If you define “x=0.3” and then type “x” the interpreter returns 0.29999999999999999. This is all perfectly logical, but like the 7/2 business, the result is that the Python interpreter does not behave like a human would like his/her calculator to behave.

Calc. This is a Linux thing, but is also installable on Mac with Macports or Fink. I haven’t had time to look at this properly yet, but it seems to be a mini-programming language with a syntax pretty much like C and the ability to save and load scripts as well as variables. So it may well be the bees knees, but I couldn’t see an obvious advantage over Python, or a nice tool like Speedcrunch. But may I was just running out of steam.

So, lots of really good things but nothing quite right. Can any Python pundits out there tell me how to fix those interpreter mode niggles ?