One of the great things about the Wolfram Demonstrations Project is the fact that source code is included. This means that it is straightforward to take someone else’s code, learn how it works and then maybe tweak it to suit yourself. Back in February I did exactly that and used the code in ‘s Tangram puzzle as the basis for my own Valentine’s day version – The Broken Heart Tangram puzzle.
Things move on though and another demonstrations author, Karl Scherer has produced his own versions of both the Broken Heart and the Traditional tangram puzzles. Karl’s versions are faster, sleeker and generally more fun to use. As an added bonus I get to look at the source code to see how he did it – everyone’s a winner!
MATLAB is an incredibly powerful piece of software that is used by students and researchers in a wide variety of disciplines. If you have some maths to do then MATLAB can probably help you out – curve fitting, statistics, plotting, signal processing, optimization, linear algebra, symbolic calculus – the list just goes on and on. There is one big problem though – much of this stuff doesn’t actually come with the basic install of MATLAB.
Let’s say that you are a new academic researcher and you have just purchased a copy of MATLAB at a cost of several hundred pounds. At first it seems that it can do everything that you require and so you are happy. One day though, you find yourself needing to find the minimum of a constrained nonlinear multivariable function. After a bit of searching you realise that the function you need is called fmincon but, when you try to use it, you discover that it is part of an add-on called the Optimization Toolbox which costs a couple of hundred pounds. You duly pay for the toolbox and are happy once again.
Some time later in your career you find yourself in need of a good quasi random number generator to help you implement a monte-carlo integration scheme. MATLAB can help you out in the form of the sobolset and haltonset functions but you have to pay yet more money to get access to the Statistics Toolbox.
MATLAB has a toolbox for almost every eventuality:
- You need to symbolically integrate a function. – buy the Symbolic Toolbox
- You need to do some advanced curve fitting. – buy the Curve Fitting Toolbox
- Working with Splines? – buy the Spline Toolbox
- Financial Mathematics?- buy the Financial Toolbox
You get the idea. I guess Mathworks do this for a good reason – by splitting off these more specialised functions into separate products they can keep the price of the basic version of MATLAB down at a reasonable level. This is a good thing for many users. After all, who wants to pay for a lot of functionality they will never use.
There are problems with this model though. If you are unlucky enough to have a problem that requires several of these specialised functions then MATLAB can be a very expensive solution for you. If happen to be the head of a research group with, say 20 members, and all of them require access to several of these specialised toolboxes then MATLAB can be an extremely expensive option for you.
Finally, if you are someone like me and you need to maintain network licenses for over 50 different MATLAB toolboxes with varying numbers of seats for an entire large university and co-ordinate who pays for what and give recommendations on which toolboxes academics might choose for teaching and research then the whole MATLAB toolbox system can become rather well….tiring. The license related issues alone are enough to give you sleepless nights – trust me on this!
NAG – A new kind of toolbox for MATLAB
Wouldn’t it be nice if there was a MATLAB toolbox that did the work of several other toolboxes which also had an amazingly straightforward licensing system? Well, the Numerical Algorithms Group (NAG) have come up with a contender for such a toolbox – The NAG toolbox for MATLAB.
NAG have got history – they have been around for a long time. They published the first version of their numerical libraries (which they refer to as Mark 1) back in 1971 so they are even older than I am. Mark 1 of the NAG libraries contained 98 different functions and could be had in 2 flavors – ANSI Fortran or Algol 60! Fast forward to 2008 and they are up to Mark 21 of their Fortran library that contains around 1,500 different functions, which is a lot by anyone’s measure. Subjects covered by these functions include optimization, statistics, splines, curve fitting, numerical quadrature, differential equations and a whole lot more. The recent release of the NAG toolbox for MATLAB allows you to call most of these functions directly from MATLAB – not a whiff of Fortran anywhere.
Did you notice anything about the subjects covered by the NAG library in the paragraph above? That’s right – they look just like the names of various MATLAB toolboxes. A very quick survey of the NAG functions on offer suggests to me that you might be able to substitute the NAG toolbox for MATLAB for the following standard MATLAB toolboxes:
- Statistics toolbox
- Optimization toolbox
- Curve fitting toolbox
- Spline toolbox
- Partial Differential Equation toolbox
That’s at least 5 toolboxes in one – which is great news and it gets better. If your institution has a site license for the NAG Fortran library then, at the time of writing at least, it will also have a site license for the NAG toolbox for MATLAB. That’s a genuine site license as well – one that you could use to install the toolbox on every machine owned by your institution. No mucking around with concurrent network licenses here – which is one major point in it’s favour from my point of view.
As you might expect, however, there are complications. The NAG toolbox may well offer similar functionality to the MATLAB toolboxes listed above but you cannot use it as a simple drop in replacement for them. For example, if you want to create quasi random numbers using the sobol sequence then the function you would use from the MATLB statistics toolbox is sobolset whereas if you use the NAG toolbox then the function you need is called G05YAF. The exact properties of these two functions may differ as well since they have probably been implemented differently and the two companies have rather different ways of doing things.
While on the subject of function naming – I have to say that I simply don’t like the naming convention used by the NAG toolbox for MATLAB (which is identical to what they use for their Fortran library). Let’s take the function mentioned above as an example – G05YAF. It’s hardly memorable is it? The MATLAB equivalent – sobolset – is much more reasonable in my opinion. Of course your mileage may vary but I have been brought up on systems such as Mathematica, MATLAB and, more recently, Python and in all of these systems the naming conventions used are almost obvious. Let’s take the function for calculating the eigenvalues of a real general matrix as another example:
- Mathematica: Eigenvalues
- Matlab: eig()
- NAG toolbox: F02EBF()
Of course the NAG naming convention isn’t completely random even though it may look like it at first sight of the function names above. It’s very well organised in fact and you can quickly get used to it once you understand the system but it definitely looks out of place when put up against its competitors in my opinion.
Although I keep comparing the NAG product with those of the Mathworks, it’s probably worth mentioning that NAG have not set out to build a product that directly competes with the MATLAB toolboxes. They are very different beasts and so if you are looking for a one to one correspondence between the NAG toolbox and MATLAB then you are going to be disappointed. NAG were around long before the Mathworks and they have their own particular way of doing things. I’m not saying that one approach is better than another but they are certainly different.
As a particular example of what I mean consider finding the eigenvalues of a matrix again. The approach taken by the Mathworks is to provide you with one function in MATLAB – eig(). The algorithm that MATLAB uses to actually find the eigenvalues depends upon the type of matrix you give it. The system takes a look at your matrix at run-time and does its best to come up with the optimum algorithm to find the solution.
NAG take a rather different approach. They provide lots of individual functions (click here to see them) that can find eigenvalues for various matrix types and they expect the user to choose the most appropriate one.
These differences can make a direct comparison between NAG and MATLAB rather difficult – if it were easy I would have done it by now. In my opinion you should not be thinking “What is the NAG equivalent to the MATLAB function xyz” as there might not be a direct equivalent, instead you should be thinking “I need a function that does abc – which routine in the NAG toolbox might help me?”
If you find that you need to do something that is not contained within the NAG toolbox then you have essentially have three options.
- Try to find something in the Mathwork’s set of toolboxes that does what you need and use that instead.
- Code the algorithm yourself – or find someone else’s solution on the web.
- Email NAG and tell them what you want to do.
I have taken option 3 on behalf of myself and several users at my university and am very pleased to say that NAG have always responded. In some cases they told us exactly which function within the NAG toolbox we needed to use and in other cases they have actually implemented a suitable algorithm for us
There will be several functions in the next release of the NAG library (and hence the NAG toolbox for MATLAB) simply because we asked for them. That’s what I call customer service! Of course I can’t promise that they will do this in all cases but I have personally found them to be very approachable.
I might not like NAG’s naming convention but I seriously like the quality of their routines. If you came to me with two solutions to a problem – one which came from the NAG libraries and one which came from some other source then I would almost always trust the NAG result. Writing numerical code is pretty much the main focus of their business and they have been doing it for over 30 years so they know their onions (as my Australian friend Barrie would say). Take a quick look at the scientific literature on google scholar and you will find thousands of references to the NAG libraries – if thousands of academic feel that they can trust them then so can you.
Another high quality part of the NAG toolbox for MATLAB is the documentation. Every single function has been meticulously documented and contains details of the algorithms involved, references to the original literature and fully working example programs. It integrates well with MATLAB’s standard documentation system and so appears to the user as if it were any other MATLAB toolbox. The only thing that is missing is a set of demo’s that can be run directly from the help system. Every standard Mathwork’s toolbox has a nice set of illustrative demo applications that you can start running with just a few mouse clicks but the NAG toolbox has none. This is a shame isn’t exactly a show-stopper and I am reliably informed that there will be a whole set of demo’s included in the next version.
- Truly massive set of robust, accurate numerical routines that could potentially negate the need for a whole set of Mathwork’s tooboxes (write me for more details)
- Superb documentation and first class customer support.
- Easy license administration for academic institutions.
- Esoteric naming convention.
- I couldn’t find a price for an individual who wants to buy the toolbox. NAG’s license model seems to be geared more towards site licenses.
The NAG toolbox for MATLAB is a great piece of software that deserves to be in the toolkit of everyone who is in the business of writing numerical code.
Full disclosure and the usual disclaimers
- I work for the University of Manchester in the UK but these are my opinions alone and do not necessarily reflect the policy of the University.
- NAG once bought me lunch but so have the Mathworks so it all evens out nicely.
- I have never been paid by either company to do anything – I’m just a customer.
- Comments are welcomed. Even if you disagree with me.
Other articles like this one:
- How to call the NAG Libraries from Excel using VBA
- What’s new in Mathematica 6.0.3
- Interacting with SAGE – Part 1
If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.
This morning I received the following in my email and it made me smile. It’s almost certainly apocryphal but that doesn’t make it any less amusing.
At an American university, there were four sophomores taking chemistry and all of them had an ‘A’ so far.
These four friends were so confident that, the weekend before finals, they decided to visit some friends and have a big party. They had a great time but, after all the hearty partying, they slept all day Sunday and didn’t make it back to the university until early Monday morning.
Rather than taking the final then, they decided that after the final, they would explain to their professor why they missed it. They said that they visited friends but on the way back they had a flat tyre. As a result, they missed the final. The professor agreed they could make up the final the next day. The guys were excited and relieved.
They studied that night for the exam.
The next day the professor placed them in separate rooms and gave them a test booklet. They quickly answered the first problem worth 5 points. Cool, they thought! Each one in separate rooms, thinking this was going to be easy. …
Then they turned the page. On the second page was written….
For 95 points: Which tyre?
I recently discovered a wonderful mathematical podcast called The Math Factor from the University of Arkansas and in their current episode they pose the following question.
“What is the smallest positive counting number that Google can’t find”
Let’s try a few searches to see what we can find:
- ’10′ gives 15,360,000,000 hits so that is definitely not what we are looking for!
- ’35672′ gives 809,000 hits – better but still nowhere near
- ’567906′ gives 6,400 hits – getting closer!
As you might expect it is easy to come up with any old example of an integer that google doesn’t know about but the smallest will be somewhat harder to find. For example as I write this there are no hits for ’56790636782356487252′ but that is rather large (and, thanks to this page, will probably result in a google hit soon)!
Can you do any better? If so please do not leave a comment here telling me what it is and please do not blog about it on your own site because, as soon as you do, it will lose the very property that makes it interesting. Instead, please send a message to the guys at Math Factor by using this link and tell them Walking Randomly sent you!
Update (8th September 2008): Michael Lugo has more to say on this question over at God Plays Dice.
I’ve got that Friday feeling! Here are some links for you reading pleasure.
Who’s afraid of the LHC? Where it is explained that while the appearance of a planet destroying black hole is possible on September 10th , it isn’t very probable.
The Pandora – Open the box and get the latest information on the object of my latest bout of gadget lust.
Sudoku Variations – Bored of standard Sudoku? Try out the large number of variations described here.
Part of my job is to look after Manchester University‘s site license for the NAG libraries. If you have never heard of the NAG (Numerical Algorithms Group) libraries before, and if your work involves any kind of numerical computation, then I highly recommend that you check them out as they are very good at what they do. One senior researcher at Manchester referred to them as ‘The gold standard of numerical computing.’ High praise indeed and praise that I completely agree with.
The NAG libraries are written in Fortran but you don’t have to be coding in Fortran in order to use them. With a bit of effort you can call them from many different programming environments such as Python, Visual Basic, C (in fact there are C-specific versions of the libraries) and MATLAB (via the NAG Toolbox for MATLAB).
A few months ago I had a visit from some very worried looking students who needed to call the NAG libraries from Excel using Visual Basic for Applications (VBA) and they had no idea where to start. Sure, NAG have some VBA examples on their website but they assume that the reader already knows a fair amount about both the NAG libraries and VBA – knowledge that these students simply didn’t have.
I took a look at what they wanted to do and said that if they came to meet me in a couple of days time then I would put together a simple piece of code that would push them in the right direction. The code would be well commented, I told them, and would cover all of the concepts that they would need in order to put together their application. They looked very grateful and relieved.
I didn’t have the heart to tell them that I had never written a single piece of VBA code in my life!
So, off I went, learning just enough VBA to work with the NAG libraries. The staff at NAG helped me out when I got stuck and, I’m happy to say, I had just what these students needed by the time of our next meeting. I’d like to stress that I didn’t do their work for them – not even close! They told me what NAG functions they wanted to use and all I did was code up example VBA scripts that called those functions for various sample problems. This was all the help I gave them as I felt that it fell within my remit of ‘supporting the NAG libraries at Manchester’ without crossing the line of actually doing their work for them.
I looked at the pile of hand written notes that had been made while I was learning VBA and thought that they could do with being typed up. After all, I would probably have forgotten most of it by the time I was next visited by some students.
To cut a long story short, these notes ended up becoming a technical report that was published on NAG’s website today. So, if you find yourself needing to call the NAG libraries from within Excel 2003 then you might find them useful. As always, feedback is welcomed.
Thanks to all of the Staff at NAG who helped me clean up the mess that was the first draft – I have really enjoyed working with you all and hope to do so again soon.
If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.
Update (3rd March 2009): The article referred to in this article has been updated – click here for details.
Lemmings is one of those video games that is so popular that almost everyone has heard of it. Released back in 1991 it has been around for over half of my life and forms an integral part of my youthful memories. In my early high school years I would go to my rich friend’s house and play Lemmings on their Amiga. Back at home, my brother and I would while away the hours playing it on our own, much less powerful and more humble, ZX Spectrum.
A couple of years later – while doing my A-Levels and during my first year of University – I would play Lemmings on my Acorn Archimedes before finally graduating to the DOS version on my 233MhZ PC around the time of my undergraduate finals. During the PhD years I played Lemmings on several different systems that came my way (thanks to donations from friends and, of course, ebay) such as the Sega Master System, Sega Saturn and Sega Game Gear. These days I tend to play it on my Playstation Portable. I have played Lemmings alone and with friends, I have played it on the plane, the train and while I should be doing more important things (such as my PhD thesis). I have played it on computers, games consoles, calculators and palm pilots. Where ever I go – Lemmings seems to follow.
Recently though something was missing in my life, I had a Windows Mobile based phone but no Lemmings. Disaster! Fortunately, Johannes Zeppenfeld has come to my rescue with a Windows Mobile clone of Lemmings that seems to work absolutely perfectly and, what’s more, it’s free!
You need a file (main.dat) from the original DOS version of Lemmings in order to play this version (no…I won’t give you the file so don’t ask) but, once you have it, installation is a snap! Johannes left this file out to try and avoid possible copyright issues and I can’t say I blame him.
I am loving this version – it is practically flawless. Thanks Johannes – you’re a star!
If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.