Re: speed of evaluation
- To: mathgroup at smc.vnet.net
- Subject: [mg26198] Re: [mg26178] speed of evaluation
- From: "Gareth J. Russell" <russell at cerc.columbia.edu>
- Date: Thu, 30 Nov 2000 22:02:14 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Dear Bernd,
First off, your code could be a lot simpler!
Your version:
In[35]:=
Timing[ Re[Plus @@
Map[Map[#^2 &, #] &,
N[Map[(Table[1, {popSize}]*#) &, Flatten[dataEnd]] -
Map[f, dataEx]]]]]
Out[35]=
{0.133333 Second, {286.071, 103.065, 90.7069, 492.413, 2603.74, 774.867,
215.515, 123.846}}
Short version:
In[36]:=
Timing[Apply[Plus, (dataEnd - Map[f, dataEx])^2]]
Out[36]=
{0.116667 Second, {286.071, 103.065, 90.7069, 492.413, 2603.74, 774.867,
215.515, 123.846}}
This saves a LITTLE bit of time, but not much. It's the evaluation of your
function that is the limiting step. So, why not try a compiled version?
fc = Compile[{{x, _Real, 2}}, {x[[1]]^2 + x[[3]]^0.5, x[[1]], x[[2]],
3 x[[3]]/(1 + x[[1]]), x[[3]] 5 x[[2]]^(x[[3]]/4),
x[[1]] + x[[2]] + x[[3]], x[[3]] - x[[2]]*x[[2]], x[[1]]^2}]
In[40]:=
Timing[Apply[Plus, (dataEnd - Map[fc, dataEx])^2]]
Out[40]=
{0.0333333 Second, {286.071, 103.065, 90.7069, 492.413, 2603.74, 774.867,
215.515, 123.846}}
This is a lot faster!
Gareth
bernd.ebersberger at wiso.uni-augsburg.de 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?
==================================================
Dr. Gareth J. Russell
NEW ADDRESS and E-MAIL FROM 1ST SEPTEMBER, 1999
Center for Environmental Research and Conservation
MC 5556
Columbia University
1200 Amsterdam Avenue
New York, NY 10027, U.S.A.
Phone: ++1 212 854 5094
Fax: ++1 212 854 8188
E-mail: russell at cerc.columbia.edu
WWW: http://web.utk.edu/~grussell (NO CHANGE)
OLD ADDRESS (AND STILL MY EMPLOYERS)
Department of Ecology and Evolutionary Biology
University of Tennessee
569 Dabney Hall
Knoxville, TN 37996-1610, USA
==================================================