       Re: How to avoid repeated recalculation of the same function

• To: mathgroup at smc.vnet.net
• Subject: [mg132685] Re: How to avoid repeated recalculation of the same function
• From: Roland Franzius <roland.franzius at uos.de>
• Date: Sun, 4 May 2014 02:28:16 -0400 (EDT)
• Delivered-to: l-mathgroup@mail-archive0.wolfram.com
• Delivered-to: l-mathgroup@wolfram.com
• Delivered-to: mathgroup-outx@smc.vnet.net
• Delivered-to: mathgroup-newsendx@smc.vnet.net
• References: <ljvdcg\$bal\$1@smc.vnet.net>

```Am 02.05.2014 08:18, schrieb pgeipi10 at gmail.com:
> Hi,
>
> I'm doing a calculaton that's purly symbolic (no graphing, numerical integration, etc.).
>
> Suppose I have a function f[x_]:=... that's very complex to build. In fact, f[x] ends up being a manageable expression (about 30 characters) but it takes Mathematica about 30 min to build that expression.
>
> Another function g[] uses the function f[x] and references it many times. I've discovered that g[] actually builds f[x] every time it's referenced which takes 30 minutes each time. Theoretically, Mathematica could build it once and then use the resulting expression every time it's referenced.
>
> So how do I accomplish that? That is, how do I make it build f[x] once and then use the resulting expression when it's needed?

Mostly its enough to set (=) which evaluates the right hand side before
writing the definition

f[x_]=Integrate[1/(1+x^2),x]

?f

ArtTan[x]

The standard SetDelayed[ a,b ] - definitions don't evaluate b.

You can always write

SetDelayed@@{a, FullSimplify[b] }

to force b to be reduced to the simplest explicit form before making the
definition with :=.

But sometimes one wants to evaluate an expression but define the
expression with free patterns not to be evaluated.

For this case the best method is to use Evaluate, a function that
overides the Hold-Attribute and forces Hold-arguments in functions to
evaluated during input time

g[x_,a_ ]:= Integrate[1/(a + x^2),x]
h[x_,a_] := Evaluate[Integrate[1/(a + x^2),x]]

Unfortunately Mathematica lacks the input Attribute "Immediate", so
useful in the language FORTH.

This makes it very complicated to force the parser to evaluate certain
functions always regardless of Hold Attributes of function expression
are fed to.

The only workaround I see is to use the mouse selecting and
Shift+Contol+Enter replacement shortcut that Evaluates an expression in
the input line already and replaces it there with its current value,
text-symbolically

j[x_,a_]:= Shift+Ctr+Enter("FullSimplify[Integrate[1/(a + x^2),x]]")

--

Roland Franzius

```

• Prev by Date: Period of numerically defined function and generality
• Next by Date: LeafCount and ByteCount Lie in Mathematica
• Previous by thread: Re: How to avoid repeated recalculation of the same function
• Next by thread: Re: Differential Equation