MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Problems with Set, SetDelayed and replacement rules...


I would write the definitions as follows, using SubValues to hold the
parameters for f.

f[g_, h_][x_] := g/h x
g[p_] = 1 + p;
h[p_] = 1 + p^2;

Then you could evaluate your function as follows.

f[g[p], h[p]][x]
((1 + p)*x)/(1 + p^2)

If you use a specific value for p then you might have

f[g[1], h[1]][x]

If you wanted you could also use...

params = {g -> g[p], h -> h[p]};
f[g, h][x] /. params /. p -> 1

Separating the parameters in the SubValues part of the definition, [g,h],
also allows you do differentiate and integrate with respect to x.


f[g[3], h[3]]'[x]

Integrate[f[g, h][x], x]

Integrate[f[g[1], h[1]][x], x]

So one form of the expression should serve all your needs.

David Park
djmp at

From: Johannes [mailto:ml.johannes at]


I am working a lot with physics equations where I have on the one hand
variables, and on the other hand paramters, which depend on other parameters
which are only given by a polynome.
In the following (very simplified example) f is a function which depends on
variable x and parameters g and h which depend both on parameter p:

f[x_]:= g/h*x;
g[p_]= 1+p;

For numerical application, I would like to define a second function f with p
as second variable. This I have done with different methods, and there I
noticed a difference that I can't explain

f1[x_, p_] = f[x] /. params;
f2[x_, p_] := f[x] /. {g->g[p],h->h[p]};
f3[x_, p_] := f[x] /. params;

If I am now calculating f1[1,1], f2[1,1] and f3[1,1], f1 and f2 deliver as
expected a numerical value, but for f3[1,1] p isn't replaced by its value 1.

Can anybody explain me the differences, especially the differences between
f2 and f3? I suspected it to be exactly identical.

And does anybody know a better method to treat parameters? My problem is
that when doing analytical transformations (Derivatives, Integrations...),
an immediate replacement of the parameters g and h would make the result
very hard to read, so I was searching for an easy method to replace them as
late as possible.

Thanks in advance for any help,

  • Prev by Date: Re: creating dirac bra-ket notations in Mathematica
  • Next by Date: Re: Problems with Set, SetDelayed and replacement rules...
  • Previous by thread: Re: Problems with Set, SetDelayed and replacement rules...
  • Next by thread: Re: Problems with Set, SetDelayed and replacement rules...