Matrix multiplication speed-up trick on MATLAB

April 23rd, 2013

I was recently working on some MATLAB code with Manchester University’s David McCormick.  Buried deep within this code was a function that was called many,many times…taking up a significant amount of overall run time.  We managed to speed up an important part of this function by almost a factor of two (on his machine) simply by inserting two brackets….a new personal record in overall application performance improvement per number of keystrokes.

The code in question is hugely complex, but the trick we used is really very simple.  Consider the following MATLAB code

>> a=rand(4000);
>> c=12.3;
>> tic;res1=c*a*a';toc
Elapsed time is 1.472930 seconds.

With the insertion of just two brackets, this runs quite a bit faster on my Ivy Bridge quad-core desktop.

>> tic;res2=c*(a*a');toc
Elapsed time is 0.907086 seconds.

So, what’s going on? Well, we think that in the first version of the code, MATLAB first calculates c*a to form a temporary matrix (let’s call it temp here) and then goes on to find temp*a’.  However, in the second version, we think that MATLAB calculates a*a’ first and in doing so it takes advantage of the fact that the result of multiplying a matrix by its transpose will be symmetric which is where we get the speedup.

Another demonstration of this phenomena can be seen as follows

>> a=rand(4000);
>> b=rand(4000);
>> tic;a*a';toc
Elapsed time is 0.887524 seconds.
>> tic;a*b;toc
Elapsed time is 1.473208 seconds.
>> tic;b*b';toc
Elapsed time is 0.966085 seconds.

Note that the symmetric matrix-matrix multiplications are faster than the general, non-symmetric one.

Mathematica 9 benchmark on Microsoft Surface Pro

April 18th, 2013

A friend of mine recently got hold of a Microsoft Surface Pro tablet and he let me have a play on it for a couple of hours.  So, I installed Mathematica 9 and ran the benchmark.  A screenshot of the result is below with the Surface’s result in blue.  Not bad for a tablet!

Touch controlled Manipulates were a lot of fun too.  If only I could run such things on my iPad as appeared to be promised in http://blog.wolfram.com/2012/02/17/a-preview-of-cdf-on-ipad/

My only other comment is that the Touch Cover is truly awful, reminding me of ye-olde ZX81, but I’ve been told that the Type Cover is much better

April 4th, 2013

When I first started this blog, there were only really two methods by which readers could keep up with new content – by subscribing to the RSS feed or by regularly dropping by the site to see what’s new. Since then readers have steadily been requesting other ways to follow the blog and, for the most part, I have obliged.  Here’s a list of current methods:

• Subscribe to the RSS feed
• Follow me on Twitter – I post every WR article to my twitter feed along with whatever else I find interesting. Twitter is also a great way of contacting me and is the social media platform on which I am most active.
• WalkingRandomly on Facebook – A small following compared to the other channels but useful to some it seems.
• Drop by the site whenever the mood strikes you

A Month of Math Software – March 2013

April 3rd, 2013

Welcome to the latest edition of A Month of Math Software where I look back over the last month and report on all that is new and shiny in the world of mathematical software.  I’ve recently restarted work after the Easter break and so it seems fitting that I offer you all Easter Eggs courtesy of Lijia Yu and R.  Enjoy!

General purpose mathematical systems