# Very long function defined - long calculation time, how to simplify

my problem is kind of straight forward:

I want to simulate something, and in order to do so, i calculated some realisations.

In the end, i have, for now, 500 (complex) functions and saved them. Each of them now has about 120 kB. I calculated the average, so I get a function that takes about 60MB if I save it.

No problem here.

Now I wanted to plot this average-function (or at least the Real-part of it). Problem is: loading and/or plotting this equation takes unholy long.

Is there a possibility to simplify this equation ? (Simplify and FullSimplify doesnt work - I need something different)

edit:

To explain what I did and why the defined function-files are this big: What I did was to define a function $f_{p}(t,s)=\sum_z r_z^2\cdot\exp(i(100-z)(t-s))$. Here, every $r_z$ is randomly distributed and i take the sum over 2000 different values. And I need p=500 different realisations (different set of random numbers) of these function. In the end I wanted to calculate the mean, e.g. $mean(t)=\frac{1}{500}\sum_p f_p(t,0)$. Worked fine. Now I saved $mean(t)$ as an ".m" file, as well as the real part $ReMean(t)$. Worked too. Now I wanted to load the file and plot it. For p=4 instead of p=500 it works in a tolerable time (so no problem in the code), but for p=500 not. As I said, the function $ReMean(t)$ , saved as an ".m" file, has roughly 60 MB.

• With now specifics on what you do and how the function looks like, I doubt anybody can help you. Also, what exactly do you mean by "function"? How do you save it? Do you really mean that the expression is 120kB big? – Lukas Lang Sep 16 '17 at 16:24
• Thanks for the answer! I'll edit my post as soon as some calculations are finished (can't open anything at the time...) And yes, the expression is this long/big. – Martin Sep 16 '17 at 16:38
• $z$ ist just some index i'm summing up over, for the $r_z$ it just means that for every $z$ I've got a new gaussian distributed random number with expectationvalue and variance that vary for each $z$.$z$ in the exponent takes 500 different values that lie next to each other, e.g. there are $z$ values in some intervall $[a,b]$ with. – Martin Sep 16 '17 at 19:11
• For example z takes 500 values with equal distance to each other in $[1,2]$ and for every value there is some random $r_z$number associated to it – Martin Sep 16 '17 at 19:19
• I just made something up. It doesn't matter, I think, to issue of speed, whether I did it exactly right. (Even if $r_z$ is supposed to be real, it makes no difference to the speed.) – Michael E2 Sep 16 '17 at 19:21

If you don't need symbolic expressions for the functions, I would suggest sticking as closely as possible to numeric evaluation. All you need to store is the vector of values of $z$ and for each $p$, the vector of $r_z$ values. The $r$ values may be stored as a $p \times z$ matrix of values. Below, the code shows $f_p$ being computed for all $p$ using the identical code used to compute $f$ for single $p$.

(* calculates OP's function; for a list of r vectors *)
ClearAll[myfun];
myfun[r_?MatrixQ, z_?VectorQ, x_] := Dot[r^2, Exp[I (100 - z) x]]; (* computes all f_p *)
myfun[r_?VectorQ, z_?VectorQ, x_] := Dot[r^2, Exp[I (100 - z) x]]; (* computes f for a
single vector r *)
rr = RandomComplex[1 + I, {500, 2000}];  (* made-up data: p x z array of r_z values *)
zz = RandomReal[100, 2000];              (* made-up z indices *)

DumpSave["/tmp/foo.mx", {"rr", "zz"}]  (* save just the parameters *)
(*  {"rr", "zz"}  *)

Clear[rr, zz];                         (* clear rr, zz to test Get *)
<< /tmp/foo.mx; // AbsoluteTiming      (* read (Get, <<) the parameters back in *)
(*  {0.006875, Null}  *)

meanfn[x_?NumericQ] := Mean[Re@myfun[rr, zz, x]];
Plot3D[
meanfn[t - s],
{t, 0, 0.1}, {s, 0, 0.1}
] // AbsoluteTiming


Given the speed of Dot and the Math Kernel Library, I'm not sure how this could be made faster.