Re: Trying to speed up a function

• To: mathgroup at smc.vnet.net
• Subject: [mg91844] Re: Trying to speed up a function
• From: dh <dh at metrohm.ch>
• Date: Wed, 10 Sep 2008 05:07:32 -0400 (EDT)
• References: <g9t6jq\$j66\$1@smc.vnet.net>

```
Hi Abhishek,

no mistery here. By definition Table[expr,{n}] completely evaluates expr

n times. Therefore, it is a good idea to take out redundant

calculations. Note also that Table has the attribute "HoldAll" and does

therefore not evaluate its arguments.

Daniel

gophergoon at gmail.com wrote:

> 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}

>

--

Daniel Huber

Metrohm Ltd.

Oberdorfstr. 68

CH-9100 Herisau

Tel. +41 71 353 8585, Fax +41 71 353 8907

E-Mail:<mailto:dh at metrohm.com>

Internet:<http://www.metrohm.com>

```

• Prev by Date: Re: How to change the FrontEnd Encoding?
• Next by Date: Re: How can I do a "grep -v" equivalent in Import[]?
• Previous by thread: Trying to speed up a function
• Next by thread: LU Decomposition w/o Pivoting