[Date Index]
[Thread Index]
[Author Index]
Re: Partial evaulation of function terms
*To*: mathgroup at smc.vnet.net
*Subject*: [mg53260] Re: [mg53230] Partial evaulation of function terms
*From*: DrBob <drbob at bigfoot.com>
*Date*: Mon, 3 Jan 2005 04:29:36 -0500 (EST)
*References*: <200501020912.EAA27721@smc.vnet.net>
*Reply-to*: drbob at bigfoot.com
*Sender*: owner-wri-mathgroup at wolfram.com
> Is there a way to force Mathematica to evaluate all
> subterms which already can be evaluated?
If amp, freq, and t are all varying from call to call, there are no such subterms (so far as I can see).
You can probably do slightly better, though, with this:
Clear@parFun
parFun[amps_, freq_][t_] := amps.Sin[(2Pi*freq*t) Range@Length@amps]
parFun[{1/3, 1/2}, 220][t]
(1/3)*Sin[440*Pi*t] + (1/2)*Sin[880*Pi*t]
It is possible to optimize somewhat if you decide (for instance) that Length@amp changes much less often than amp, freq, or t, then this may help:
Clear[parFun, parFun1]
parFun1[n_Integer] := parFun1[n] = Evaluate@Sin[(2Pi*#) Range@n] &
parFun[amps_, freq_][t_] := amps.parFun1[Length@amps][freq*t]
parFun1[2]
parFun[{1/3, 1/2}, 220][t]
(outputs)
{Sin[2*Pi*#1], Sin[4*Pi*#1]} &
(1/3)*Sin[440*Pi*t] + (1/2)*Sin[880*Pi*t]
If the values of amp won't change too often, you can go a step farther:
Clear[parFun, parFun1]
parFun1[n_Integer] := parFun1[n] = Evaluate@Sin[(2Pi*#) Range@n] &
parFun[amps_] := parFun[amps] = Evaluate[amps.parFun1[Length@amps][#]] &
parFun[amps_, freq_][t_] := parFun[amps][freq*t]
parFun1[2]
parFun[{1/3, 1/2}]
parFun[{1/3, 1/2}][220t]
parFun[{1/3, 1/2}, 220][t]
(outputs)
{Sin[2*Pi*#1], Sin[4*Pi*#1]} &
(1/3)*Sin[2*Pi*#1] + (1/2)*Sin[4*Pi*#1] &
(1/3)*Sin[440*Pi*t] + (1/2)*Sin[880*Pi*t]
(1/3)*Sin[440*Pi*t] + (1/2)*Sin[880*Pi*t]
That allows alternate calling sequences and requires storage for each value of "amps".
I doubt this will save a great deal of time over your original code, however.
Bobby
On Sun, 2 Jan 2005 04:12:39 -0500 (EST), Erich Neuwirth <erich.neuwirth at univie.ac.at> wrote:
> I have the following function definition
>
> ParFun[amps_, freq_] := Function[t, Sum[amps[[i]]*Sin[2*Pi*freq*i*t], {
> i, Length[amps]}]]
>
> Then, the following works:
>
> ParFun[{1,1/2},220][t]//InputForm
> Sin[440*Pi*t] + Sin[880*Pi*t]/2
>
> but
> ParFun[{1,1/2},220]//InputForm
> yields
> Function[t$, Sum[{1, 1/2}[[i]]*Sin[2*Pi*220*i*t$], {i, Length[{1, 1/2}]}]]
>
> Is there a way to force Mathematica to evaluate all
> subterms which already can be evaluated?
> This function will be called in Play very often, so it would make things
> much faster if you could assign the function with all possible
> constants evaluated to a local variable and do the many calls to the
> function for producing numerical values with the "partially
> preevaluated" expression.
>
>
>
>
--
DrBob at bigfoot.com
www.eclecticdreams.net
Prev by Date:
**Mercator projection from http://mathworld.wolfram.com/MercatorProjection.html with missing earth.pts**
Next by Date:
**Re: Partial evaulation of function terms**
Previous by thread:
**Re: Partial evaulation of function terms**
Next by thread:
**Re: Partial evaulation of function terms**
| |