RE: Problems with Set, SetDelayed and replacement rules...
- To: mathgroup at smc.vnet.net
- Subject: [mg72292] RE: [mg72274] Problems with Set, SetDelayed and replacement rules...
- From: "David Park" <djmp at earthlink.net>
- Date: Mon, 18 Dec 2006 06:56:22 -0500 (EST)
Johannes, 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] x If you wanted you could also use... params = {g -> g[p], h -> h[p]}; f[g, h][x] /. params /. p -> 1 x 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,h]'[x] g/h f[g[3], h[3]]'[x] 2/5 Integrate[f[g, h][x], x] (g*x^2)/(2*h) Integrate[f[g[1], h[1]][x], x] x^2/2 So one form of the expression should serve all your needs. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Johannes [mailto:ml.johannes at gmail.com] Hi, 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; h[p_]=1+p^2; params={g->g[p],h->h[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, Johannes