## Archive for October, 2011

Welcome to this month’s ‘A month of Math software.’ If you missed the September edition then why not take a look at http://www.walkingrandomly.com/?p=3534. All previous editions can be found at the Month of Math Software Archives. If you have some mathematical software news for the November 2011 edition then feel free to contact me.

**General Mathematics and Statistics**

- After being in beta for a while, version 4 of GeoGebra has been released. I confess that I’ve never used it but it looks great and it’s free!
- Wolfram’s Mathematica has seen a minor update with version 8.0.4. The previous version was 8.0.1 and the list of changes between the two is given here.
- The free MATLAB clone, Octave, has seen a bug-fix upgrade with version 3.4.3.
- Version 2.17.12 of the computational algebra package, Magma, has been released. The change log is at http://magma.maths.usyd.edu.au/magma/releasenotes/2/17/12/
- Rene Grothmann has updated his Euler Math Toolbox, a numerical package that has some similarities to MATLAB, to version 12.9. This new release includes the LSODA algorithm for stiff equations. He gives examples of the new functionality at http://euler.rene-grothmann.de/Programs/Examples/Stiff%20Equation.html

**Mathematics on GPUs**

- Jacket, the GPU-accelerated toolbox for MATLAB has been upgraded to version 1.8.2. The new functionality is listed at http://wiki.accelereyes.com/wiki/index.php/Release_Notes#09.2F29.2F2011_-v1.8.2
- Mark 0.5 of the Numberical Algorithms Group’s GPU library has been released. This new version includes routines such as LU factorization, Cholesky factorization and random numbers from the Gamma distribution. One piece of good news is that the library is currently free for academic use.
- The AMD Core Math Library for Graphic Processors (ACML-GPU) was discontinued this month….superseded by the AMD Accelerated Parallel Processing Math Libraries (APPML).

**Scientific Plotting**

- Matplotlib is a very capable plotting library for the Python programming language and it has just been updated. Version 1.1 has lots of nice new features and you can read about them all at http://matplotlib.sourceforge.net/users/whats_new.html
- DISLIN is a plotting library that can be called from many languages including C and Fortran. October saw it updated to version 10.1.5 and you can see what’s new from DISLIN’s news page.

**Mobile Mathematics**

- I discovered a couple of free MATLAB clones for Android this month – Addi and Mathmatiz. My favourite of the two is Addi, partly because Mathmatiz insists on serving me adverts while using it. I’d happily pay for a version of Mathmatiz that didn’t include adverts though!
- One mobile application that I’ve been meaning to mention for months is MathScript. This Android based application allows you to write and run python programs directly on your device. It also comes with some basic plotting functionality and a full version of SymPy which turns it into a very capable mathematical compute engine.
- Maplesoft have released The Maple Player for iPad which allows interactive Maple documents to be used on Apple’s tablet devices. This first release comes with a few pre-installed documents that shows the sort of thing that the software is capable of. The initial set of topics includes Laplace Transforms, a function plotter, integration tutor and more. At the moment we can’t publish our own Maple documents to iPad but it looks like this is what Maplesoft are planning for the future.

Typical…I leave my iPad at home and this happens

I can’t WAIT to try this out. Blog post from Maplesoft about it at http://www.mapleprimes.com/maplesoftblog/127071-Maple-And-The-IPad?sp=127071

May I be the first to ask **“When is an Android version coming out?”**

Back in May 2010, The Mathworks released MATLAB Mobile which allows you to connect to a remote MATLAB session via an iPhone. I took a quick look and was less than impressed since what I REALLY wanted was the ability to run MATLAB code natively on my phone. Many other people, however, liked what The Mathworks had done but what THEY really wanted was an Android version. There is so much demand for an Android version of MATLAB Mobile that there is even a Facebook page campaigning for it. Will there ever be anything MATLABy that fully satisfies Android toting geeks such as me?

Enter Addi, an Android based MATLAB/Octave clone that has the potential to please a lot of people, including me. Based on the Java MATLAB library, JMathLib, Addi already has a lot going for it including the ability to execute .m file scripts and functions natively on your device, basic plotting (via an add-on package called AddiPlot) and the rudimentary beginnings of a toolbox system (See AddiMappingPackage). All of this is completely free and brought to us by just one man, Corbin Champion.

It works pretty well on my Samsung Galaxy S apart from the occasional glitch where I can’t see what I’m typing for short periods of time. Writing MATLAB code using the standard Android keyboard is also a bit of a pain but I believe that a custom on-screen keyboard is in the works which will hopefully improve things. As you might expect, there is only a limited subset of MATLAB commands available (essentially everything listed at http://www.jmathlib.de/docs/handbook/index.php sans the plotting functions) but there is enough to be fun and useful…just don’t expect to be able to run advanced, toolbox heavy codes straight out of the box.

Where Addi really shines, however, is on an ASUS EEE Transformer. Sadly, I don’t have one but a friend of mine let me install Addi on his and after five minutes of playing around I was in love (It even includes command history!). Some have pointed out to me that life would probably be easier with a netbook running Linux and Octave but where’s the fun in that :) To be honest, I actually find it much more fun using a limited version of MATLAB because it makes me do so much more myself rather than providing a function for every conceivable calculation…great for learning and fiddling around.

Addi is a fantastic free MATLAB clone for Android based devices that I would heartily recommend to all MATLAB fans. Get it, try it and let me know what you think :)

The NAG C Library is one of the largest commercial collections of numerical software currently available and I often find it very useful when writing MATLAB mex files. “*Why is that?*” I hear you ask.

One of the main reasons for writing a mex file is to gain more speed over native MATLAB. However, one of the main problems with writing mex files is that you have to do it in a low level language such as Fortran or C and so you lose much of the ease of use of MATLAB.

In particular, you lose straightforward access to most of the massive collections of MATLAB routines that you take for granted. Technically speaking that’s a lie because you could use the mex function **mexCallMATLAB** to call a MATLAB routine from within your mex file but then you’ll be paying a time overhead every time you go in and out of the mex interface. Since you are going down the mex route in order to gain speed, this doesn’t seem like the best idea in the world. This is also the reason why you’d use the NAG C Library and not the NAG Toolbox for MATLAB when writing mex functions.

One way out that I use often is to take advantage of the NAG C library and it turns out that it is extremely easy to add the NAG C library to your mex projects on Windows. Let’s look at a trivial example. The following code, nag_normcdf.c, uses the NAG function **nag_cumul_normal **to produce a simplified version of MATLAB’s normcdf function (laziness is all that prevented me from implementing a full replacement).

/* A simplified version of normcdf that uses the NAG C library * Written to demonstrate how to compile MATLAB mex files that use the NAG C Library * Only returns a normcdf where mu=0 and sigma=1 * October 2011 Michael Croucher * www.walkingrandomly.com */ #include <math.h> #include "mex.h" #include "nag.h" #include "nags.h" void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *in,*out; int rows,cols,num_elements,i; if(nrhs>1) { mexErrMsgIdAndTxt("NAG:BadArgs","This simplified version of normcdf only takes 1 input argument"); } /*Get pointers to input matrix*/ in = mxGetPr(prhs[0]); /*Get rows and columns of input matrix*/ rows = mxGetM(prhs[0]); cols = mxGetN(prhs[0]); num_elements = rows*cols; /* Create output matrix */ plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); /* Assign pointer to the output */ out = mxGetPr(plhs[0]); for(i=0; i<num_elements; i++){ out[i] = nag_cumul_normal(in[i]); } }

To compile this in MATLAB, just use the following command

mex nag_normcdf.c CLW6I09DA_nag.lib

If your system is set up the same as mine then the above should ‘just work’ (see **System Information** at the bottom of this post). The new function works just as you would expect it to

>> format long >> format compact >> nag_normcdf(1) ans = 0.841344746068543

Compare the result to normcdf from the statistics toolbox

>> normcdf(1) ans = 0.841344746068543

So far so good. I could stop the post here since all I really wanted to do was say **‘The NAG C library is useful for MATLAB mex functions and it’s a doddle to use – here’s a toy example and here’s the mex command to compile it’**

However, out of curiosity, I looked to see if my toy version of normcdf was any faster than The Mathworks’ version. Let there be 10 million numbers:

>> x=rand(1,10000000);

Let’s time how long it takes MATLAB to take the normcdf of those numbers

>> tic;y=normcdf(x);toc Elapsed time is 0.445883 seconds. >> tic;y=normcdf(x);toc Elapsed time is 0.405764 seconds. >> tic;y=normcdf(x);toc Elapsed time is 0.366708 seconds. >> tic;y=normcdf(x);toc Elapsed time is 0.409375 seconds.

Now let’s look at my toy-version that uses NAG.

>> tic;y=nag_normcdf(x);toc Elapsed time is 0.544642 seconds. >> tic;y=nag_normcdf(x);toc Elapsed time is 0.556883 seconds. >> tic;y=nag_normcdf(x);toc Elapsed time is 0.553920 seconds. >> tic;y=nag_normcdf(x);toc Elapsed time is 0.540510 seconds.

So my version is slower! Never mind, I’ll just make my version parallel using OpenMP – Here is the code: nag_normcdf_openmp.c

/* A simplified version of normcdf that uses the NAG C library * Written to demonstrate how to compile MATLAB mex files that use the NAG C Library * Only returns a normcdf where mu=0 and sigma=1 * October 2011 Michael Croucher * www.walkingrandomly.com */ #include <math.h> #include "mex.h" #include "nag.h" #include "nags.h" #include <omp.h> void do_calculation(double in[],double out[],int num_elements) { int i,tid; #pragma omp parallel for shared(in,out,num_elements) private(i,tid) for(i=0; i<num_elements; i++){ out[i] = nag_cumul_normal(in[i]); } } void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *in,*out; int rows,cols,num_elements; if(nrhs>1) { mexErrMsgIdAndTxt("NAG_NORMCDF:BadArgs","This simplified version of normcdf only takes 1 input argument"); } /*Get pointers to input matrix*/ in = mxGetPr(prhs[0]); /*Get rows and columns of input matrix*/ rows = mxGetM(prhs[0]); cols = mxGetN(prhs[0]); num_elements = rows*cols; /* Create output matrix */ plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); /* Assign pointer to the output */ out = mxGetPr(plhs[0]); do_calculation(in,out,num_elements); }

Compile that with

mex COMPFLAGS="$COMPFLAGS /openmp" nag_normcdf_openmp.c CLW6I09DA_nag.lib

and on my quad-core machine I get the following timings

>> tic;y=nag_normcdf_openmp(x);toc Elapsed time is 0.237925 seconds. >> tic;y=nag_normcdf_openmp(x);toc Elapsed time is 0.197531 seconds. >> tic;y=nag_normcdf_openmp(x);toc Elapsed time is 0.206511 seconds. >> tic;y=nag_normcdf_openmp(x);toc Elapsed time is 0.211416 seconds.

This is faster than MATLAB and so normal service is resumed :)

**System Information**

- 64bit Windows 7
- MATLAB 2011b
- NAG C Library Mark 9 – CLW6I09DAL
- Visual Studio 2008
- Intel Core i7-2630QM processor

Consider a periodic sequence, seq_{p}, with period p

seq_{p} = *a*_{1}, *a*_{2}, …, *a*_{p}, *a*_{1}, *a*_{2}, …, *a*_{p}, *a*_{1}, *a*_{2}, …, *a*_{p}, …

Two sub-sequences, each of length N where 2< N< p, are produced by choosing two random start points in seq_{p} and using the next N-1 numbers. What is the probability that these two sub-sequences will overlap?

If you take M>2 such length N sub-sequences then what is the probability that **any** two will overlap?

For a more concrete example, assume that p = 2^19937 – 1 (yes, a very big number) and consider 10,000 sub-sequences each of length 10^9.

Disclaimer: I don’t have the solution to this and haven’t yet tried to find it

Welcome to September’s A Month of Math Software. Last month’s can be found here and the archive is here. If you’ve got news for next month’s edition then contact me.

**General mathematics and statistics
**

- The commercial computer algebra package, MAGMA, has been upgraded to version 2.17-11. The change log is at magma.maths.usyd.edu.au/magma/releasenotes/2/17/11/
- Version 2011b of MATLAB has been released. I’m all excited about the improved GPU functionality in the parallel computing toolbox along with the that fact that the MATLAB compiler now supports GPU-enabled code. This little combination means that we’ll be able to include a small amount of MATLAB/GPU support on Manchester University’s Condor pool. If GPU support isn’t your thing then maybe you’d like to hear about updates to the MATLAB comparison tool from Mike on the MATLAB Desktop.
- Version 2.13.2 of the extremely popular free, open source statistics pacakge, R, has been released. This is intended to be the last of the 2.13 series and you can find out what’s new by going to https://stat.ethz.ch/pipermail/r-announce/2011/000543.html

**Freely available linear algebra software**

- Jack Dongarra et al have updated their survey of freely available software for linear algebra.
- Version 2.4.2 of PLASMA (Parallel Linear Algebra for Scalable Multi-core Architectures) has been released. Click here for a list of the new stuff.

**Math software in the blogs**

- Sage: Creating a Viable Free Open Source Alternative to Magma, Maple, Mathematica, and MATLAB – a blog post by Sage’s creator, William Stein
- Robert Talbert asks questions about the fundamental theorem of calculus while looking at Maple, Mathematica and Wolfram Alpha. On a related note I had fun with an integral with MATLAB and Mathematica.
*“Neil Bickford calculated the first 458 million terms for the continued fraction of pi, breaking the previous record of 180 million. He used*– Check out the full story at The Wolfram Blog: From Pi to Puzzles*Mathematica*to develop his code and verify his results—which he posted shortly after he turned 13.”- The R Graph Gallery has gone social. Read about the details at Revolutions – http://blog.revolutionanalytics.com/2011/09/the-r-graph-gallery-goes-social.html
- Two Suns in the Sunset – NASA’s Kepler mission has discovered a planet orbiting two stars, something like Tatooine in Star Wars. Yeah, I know that this hasn’t got anything to do with math software but it is very cool!