       Re: speed of evaluation

• To: mathgroup at smc.vnet.net
• Subject: [mg26203] Re: [mg26178] speed of evaluation
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Thu, 30 Nov 2000 22:02:18 -0500 (EST)
• References: <200011300604.BAA08363@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Bernd Ebersberger wrote:
>
> dear all,
>
> i have a problem with the speed of mathematica 4.0
>
> i have function f that is a list and it contains several (more than 200)
> functions as elements. a simple example is attached at  the end of the
> mail - the functions, i deal with are much more complex than the ones in the
> example here.
>
> the functions depend on more than 40 variables althouhg it's only three in
> the example.
>
> f[{x1_,x2_, ... x40_,x41_ ...}]
>
> then i have a dataset containing about 600 datapoints for the {x1_,x2_, ...
> x40_,x41_ ...} variables (in the list dataEx in the example just randomly
> created) and i have a target value for each datapoint (in the list dataEnd
> in the example also randomly created in the example)
>
> what i want to do is evaluate each function in the function-list f on each
> datapoint and compute the sum of the squared distances to the target values.
>
> i do that with:
>
> ****************************** example
> ********************************************
>
> dataEx = Table[{Random[], Random[], Random[]}, {600}];
> dataEnd = Table[Random[], {600}];
> f[{x1_, x2_, x3_}] := {x1^2 + x3^0.5, x1, x2, 3 x3/(1 + x1), x3 5 x2^(x3/4),
>     x1 + x2 + x3, x3 - x2*x2, x1^2}
> popSize = Length[f[{x1, x2, x3}]];
> res1 =
>     Re[Plus @@
>         Map[Map[#^2 &, #] &,
>           N[Map[(Table[1, {popSize}]*#) &, Flatten[dataEnd]] -
>               Map[f, dataEx]]]]
>
> ****************************************************************************
> *******
>
> the problem is that the evaluation of that number of functions (200
> functions) on 600 datapoints containing 40 and more variables takes quite a
> while. as i have to repeat that process for a couple of houndred times (f
> changes from run to run!!!) this ads up.
>
> does anybody have a suggestion how to speed up the process?
>
> kind regards
>
> bernd.

Difficult to know what would constitute a sufficiently fast evaluation
as you give no baseline. Also I am not entirely sure I follow what
exactly gets squared and compared to target values. Below is my best
guess. I created alot of "random" functions and used dimensions in
accord with the guidelines you state. It takes around 20 seconds to do a
full evaluation on my 300 Mhz Linux box. So a couple hundred of these
would run you around an hour.

funclist = {Identity, Sqrt, Sin, Cos, Tan, Exp, Log,
Times, Times, Times, Times, Power, Power};

randfunc[n_, nargs_, flist_] := Function[Evaluate[Module[
{len=Length[flist], arg1, func, fax},
Sum[
func = funclist[[Random[Integer, {1,len}]]];
arg1 = Random[Integer,{1,nargs}];
If [func===Power, Power[Slot[arg1],Random[Integer,{-3,3}]],
(* else *) If [func===Times,
fax = Random[Integer,{1,Min[nargs,4]}];
Product[Slot[Random[Integer,{1,nargs}]], {fax}],
(* else *) func[Slot[arg1]]
]],
{n}
]
]]]

randfunclist[nfuns_Integer, n_Integer, nargs_Integer, flist_] :=
Table[randfunc[n,nargs,flist], {nfuns}]

In:= rflist = randfunclist[200, 6, 40, funclist];

In:= vals = Table[Random[], {600}, {40}];

In:= targets = Table[Random[], {600}];

In:= Timing[vecs = Transpose[Outer[Apply, rflist, vals, 1]];]

Out= {19.07 Second, Null}

In:= Timing[result =
Table[vecs[[j]].vecs[[j]] - targets[[j]], {j,Length[targets]}];]

Out= {1.58 Second, Null}

Depending on your functions, you may be able to improve on this by use
of Compile[].

Daniel Lichtblau
Wolfram Research

```

• References:
• Prev by Date: Re: speed of evaluation
• Previous by thread: speed of evaluation
• Next by thread: Re: speed of evaluation