MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Trying to speed up a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg91704] Trying to speed up a function
  • From: gophergoon at gmail.com
  • Date: Sat, 6 Sep 2008 02:05:52 -0400 (EDT)

Say I have a previously defined matrix 'vecs' and I want to calculate
certain sums of columns. I find it easier to write in terms of the
Transpose but it seems that when I put Transpose[vecs] in the
definition of a table, mathematica evaluates the transpose over and
over to get at every element of the matrix. Which takes much longer
than doing it in 2 steps (2nd method). I only found this out by trial
and error so I am curious about the explanation for this behaviour and
how to avoid it in general.

BTW I am aware that the expression can be written in 1 line without
using Transpose also, but I'd still like to know how to tell that if a
sub-expression in a function is going to be evaluated repeatedly.
Thanks,
Abhishek

In[15]:= Dimensions[vecs]

Out[15]= {1620, 1620}

In[23]:= Table[Total[Transpose[vecs][[n]][[811 ;; 1620]]], {n, 1,
1620}]; // Timing

Out[23]= {27.844, Null}

In[24]:= vecst = Transpose[vecs];
Table[Total[vecst[[n]][[811 ;; 1620]]], {n, 1, 1620}]; // Timing

Out[25]= {0.016, Null}


  • Prev by Date: An Argument Problem
  • Next by Date: LU Decomposition w/o Pivoting
  • Previous by thread: Re: An Argument Problem
  • Next by thread: Re: Trying to speed up a function