Speeding Up Numerical Matrix Calculations ???
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1091] Speeding Up Numerical Matrix Calculations ???
- From: Paul E Howland <PEHOWLAND at taz.dra.hmg.gb>
- Date: Sun, 14 May 1995 21:11:31 -0400
It is common in many signal processing applications to formulate problems in terms of simple functions of lots of matrices. Hence, I guess, the existence of MATLAB. My question is quite simple. Mathematica can be numerically very slow. How can I speed up my numerical calculations involving matrices that consist only of machine size numbers? It appears that Mathematica handles numerical matrices in much the same way as it does symbolic matrices. Here's a test I did using Mathematica V2.2.3 on a 486/33 PC running Windows 3.1. In[1]:= F[a_?MatrixQ, b_?MatrixQ, c_?MatrixQ] := Transpose[a]a + 3 b c + Transpose[c]c In[2]:= NF[a_?MatrixQ, b_?MatrixQ, c_?MatrixQ] := N[Transpose[a]a + 3 b c + Transpose[c]c] Thus I've defined two (arbitrary) functions that manipulate matrices. NF is simply N[F] - I want to see whether wrapping N[] around a calculation causes Mathematica to invoke some numerically optimised code. Now generate some machine sized numerical matrices ... In[3]:= a = Table[Random[], {i,10}, {j,10}]; In[4]:= b = Table[Random[], {i,10}, {j,10}]; In[5]:= c = Table[Random[], {i,10}, {j,10}]; And some symbolic matrices ... In[6]:= x = Table[FromCharacterCode[Random[Integer, {100,122}]], {i, 10}, {j,10}]; In[7]:= y = Table[FromCharacterCode[Random[Integer, {100,122}]], {i, 10}, {j,10}]; In[8]:= z = Table[FromCharacterCode[Random[Integer, {100,122}]], {i, 10}, {j,10}]; And compare timings. First compare F[] on both sets of data ... In[9] := Timing[Do[F[a,b,c], {i,10}]] Out[9] = {3.185 Second, Null} In[10] := Timing[Do[F[x,y,z], {i,10}]] Out[10] = {2.087 Second, Null} thus the symbolic processing is faster (!). Now see what the effect of wrapping N[] around the code is, for numerical data... In[11] := Timing[Do[NF[a,b,c], {i,10}]] Out[11] = {3.24 Second, Null} ... it makes it marginally slower! Presumably due to the extra time required by Mathematica to interpret the N[] function. It is not possible to use Compile[] on a function that accepts matrices as arguments because Compile[] only accepts _Integer, _Real and _Complex as its data types (and not _List). Is there any means of getting Mathematica to manipulate numerical matrices more quickly (other than using Matlab or writing a C program and then using MathLink)? It would be nice if Mathematica was written such that when N[] was wrapped around simple numerical matrix operations, optimised numerical code was invoked. Paul E Howland Long Range Ground Radar Systems Section tel. +44 (0)1684 895767 LSC2 Division, Room BY209 fax. +44 (0)1684 896315 Defence Research Agency email: PEHOWLAND at DRA.HMG.GB Malvern, Worcs, WR14 3PS, UK. -----------------------------------------------------------------------------