       Re: Unexpected Behavior: SetDelayed versus Set

• To: mathgroup at smc.vnet.net
• Subject: [mg120324] Re: Unexpected Behavior: SetDelayed versus Set
• From: Leonid Shifrin <lshifr at gmail.com>
• Date: Tue, 19 Jul 2011 06:55:10 -0400 (EDT)
• References: <201107181013.GAA26867@smc.vnet.net>

```Brian,

The short answer is that, when you define a function using SetDelayed, as in

f[args]:=rhs,

Then having your r.h.s reference global symbols (other than formal
parameters)
is generally an invitation for trouble (unless you know precisely what you
are
doing, such as when you create closures with encapsulated mutable state,
for example).

It is a pity that we don't have FAQ, since this has been discussed many
times
before. Here are a few links I am aware of, discussing and explaining
this problem:

http://forums.wolfram.com/mathgroup/archive/2010/May/msg00102.html

(see David Park's post in the above thread)

http://stackoverflow.com/questions/6046626/a-problem-in-mathematica-8-with-function-declaration/

http://stackoverflow.com/questions/6236458/plot-using-with-versus-plot-using-block-mathematica/

I am sure that there are many more discussions on this topic, but even those
will give you a rather good and comprehensive picture.

Regards,
Leonid

On Mon, Jul 18, 2011 at 2:13 PM, blamm64 <blamm64 at charter.net> wrote:

> Group,
>
> Please consider the following:
>
> gc = 3217*(10^-2)*12;
> orificeCD=65*(10^-2);
> kTorificeL=1/(orificeCD^2);
> reyTorifice=12;
> cLohm=(117*(10^-4))*1714*(385*(10^-2));
> nuStd=19*155*(10^-5);  rhoStd=3014*(10^-5);
>
> aLinQS[area_]:=(kTorificeL*reyTorifice*nuStd*rhoStd)/
> (2*area*2*Sqrt[area/Pi]*gc);
>
>
> Clear[taSD,taS,area,l,q];
>
> taSD[l_,q_] := area/.Solve[((l*q)/cLohm)^2==q*(aLinQS[area]
>
> taS[l_,q_] = area/.Solve[((l*q)/cLohm)^2==q*(aLinQS[area]
>
> {Dimensions[taSD[l,q]],Dimensions[taS[l,q]]}
> {{4},{4}}
>
> lm=485;qm=1925*(10^-3);
>
> N[taSD[lm,qm]]//InputForm
>
> {0.0015350502096417261}
>
> N[taS[lm,qm]]//InputForm
>
> {-0.0015301809337073798 - 4.86928826122106*^-6*I,
> -0.0015301809337073798 + 4.86928826122106*^-6*I,
>  0.0015253116577730662, 0.0015350502096416934}
>
> Now, this is really unexpected to  me, especially in light of the of
> the Dimensions result where Symbols are substituted in the rhs of both
> definitions.
>
> For some unknown to me reason the SetDelayed definition returns the
> last member of the solution set when the rhs of the SetDelayed
> definition has numerial values for <l> and <q>.
>
> Obviously, again, I am missing something; but this time whatever it is
> I am missing has me gasping for air.
>
> -Brian L.
>
>

```

• Prev by Date: Re: Solve never calls Equal?
• Next by Date: Re: MultinormalDistribution Question
• Previous by thread: Unexpected Behavior: SetDelayed versus Set
• Next by thread: Re: Unexpected Behavior: SetDelayed versus Set