Archive for the ‘NAG Library’ Category

March 26th, 2009

The first couple of implementations of Mark 22 of the NAG (Numerical Algorithms Group) Library have been released today.  Although written in Fortran, this set of highly regarded numerical routines can be called from many languages including Java, Python and Visual Basic.   Products also exist to allow you to call the NAG routines from MATLAB and Maple but these haven’t been updated yet.

NAG divide the functionality of their library into a series of chapters such as local optimisation, random number generators and smoothing in statistics. This latest version of the library adds three completely new chapters along with additions to many of the existing ones. The new chapters are

  • Wavelet Transforms
  • Global Optimisation (That’s right – GLOBAL optimisation!  No longer are you restricted to local optimisation problems)
  • Further linear algebra support routines

Of the 192 new functions that NAG have added to this release some of the ones that caught my eye include

  • Evaluation of Lambert’s W function for real values.
  • A new routine for computing the matrix exponential of a real-valued matrix.
  • A routine to compute the nearest correlation matrix to a real square matrix.
  • A suite of routines for evaluating various option pricing formulae.
  • A new routine for performing ProMax rotations.
  • Improved quasi and pseudo random number generators.

A full description of all of the new stuff can be found on NAG’s website.  So far you can only get 32bit and 64bit Linux versions of Mark 22 but I expect other versions to be available soon.

I make no attempt to hide the fact that I am a big fan of NAG and their products and this latest release adds a lot of great new functionality. Enjoy!

March 3rd, 2009

I recently made a few minor updates to my original Fortran NAG / Excel tutorial and so am making it available here. If anyone finds this useful or has any other detail to add then let me know.  I consider this to be an evolving document and will consider additions and modifications on request.

The tutorial assumes you are using the thread safe version of Mark 21 of the Fortran libraries – FLDLL214ML

Some people have questioned why I ever looked into this combination and my answer is simple – If you find yourself needing to do some serious numerical work with Microsoft Excel then (in my opinion at least) you are advised to hand over the numerical heavy lifting to something that is capable.  The NAG library is more than capable.

Click here to download the tutorial in pdf format.

December 4th, 2008

Almost 10 years ago now, I was a teaching assistant for an Introduction to Fortran course at the University of Sheffield.  I remember being told by one of the other PhD students that ‘Fortran is a dying language and so we are wasting our time teaching this stuff.  No one will be using Fortran in 10 years time.’

Fast forward 10 years and Fortran is still going strong in the research and numerical communities.  If you are doing numerics and you want fast code then Fortran is an option that you simply can’t ignore. It is also required for doing things like writing user defined materials in the finite element analysis package, Abaqus.   One of the best Fortran compilers on the market (in my opinion at least) is the NAG Fortran Compiler and their Linux version has recently been updated to version 5.2.

It now includes support for almost all of the features in the Fortran 2003 standard and they have also added quadruple precision support – something that the people I support have wanted for a long time.  A full list of changes can be found on NAG’s website.

Finally a note to self – they have changed the name of the executable from f95 to nagfor – this will generate support queries…you know it will!

September 23rd, 2008

Introduction

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.

Function Naming

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.

Functionality Differences

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.

  1. Try to find something in the Mathwork’s set of toolboxes that does what you need and use that instead.
  2. Code the algorithm yourself – or find someone else’s solution on the web.
  3. 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.

Quality

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.

Conclusions

Pros:

  • 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.

Cons:

  • 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:

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.

September 4th, 2008

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 updatedclick here for details.