       Re: Functions inside modules with external definitions

• To: mathgroup at smc.vnet.net
• Subject: [mg96505] Re: Functions inside modules with external definitions
• From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
• Date: Sun, 15 Feb 2009 03:19:55 -0500 (EST)
• References: <gn5ugm\$h02\$1@smc.vnet.net>

```Hi,

and my question is "Why do you use Module[] ?"

expression = a x^c + b y^(1 - c);

mx = Block[{f, expression = expression},
f[x_: x, y_: y][a_ : a, b_: b, c_: c] := Evaluate@expression;
f[k1, k2][.2, .3, .4]
]

work as expected.

Regards
Jens

E. Martin-Serrano wrote:
> Hi,
>
>
>
> Please review the following and see weather my question has and deserves an
> answer. It is a about defining functions local to modules but using external
> definitions of the right hand side of the local function and passed as
> parameters to the module.
>
>
>
>
>
> expression = a x ^c + b y ^(1-c)  (* or whatever expression *)
>
>
>
> Clear[f];
>
>
>
> f[x_: x,y_: y][a_: a,b_: b,c_: c]:=Evaluate@expression
>
>
>
> ?f
>
> f[k1,k2][.2,.3,.4]
>
>
>
> (* Functions inside modules where the RHS of the function has been assigned
> to a variable *)
>
> (* first form works as I expected, but uses an extrenal definition for a
> local function (the name is local) *)
>
>
>
> m1=
>
>  Module[{f},
>
>   f[x_: x,y_: y][a_ :a,b_: b,c_: c]:=Evaluate@expression;
>
>   f[k1,k2][.2,.3,.4]
>
>   ]
>
>
>
> (* second form.  I expected it worked as the one above but it does not; the
> expression formed outside is assigned to a local variable with the same name
> *)
>
>
>
> m2 =
>
>  Module[{f, expression = expression},
>
>   f[x_: x, y_: y][a_ :a,b_: b,c_: c]:=Evaluate@expression;
>
>   f[k1,k2][.2,.3,.4]
>
>   ]
>
>
>
> (* third form. I expected it worked as the first one above but it does not;
> the expression is assigned directly to a local variable *)
>
>
>
> m3 =
>
>  Module[{f,expression = a x ^c + b y ^(1-c)},
>
>   f[x_: x,y_: y][a_ :a,b_: b,c_: c]:=Evaluate@expression;
>
>   f[k1,k2][.2,.3,.4]
>
>   ]
>
>
>
> (* fourth form. I expected it worked as the first one above but it does not;
> the expression is assigned within the body of the module o a local variable
> *)
>
>
>
> m4 =
>
>  Module[{f,expr},
>
>   expr =expression;
>
>   f[x_: x,y_: y][a_ :a,b_: b,c_: c]:=Evaluate@expr;
>
>   f[k1,k2][.2,.3,.4]
>
>   ]
>
>
>
> (* My question is how to construct a function inside a module in manner
> similar to the following *)
>
>
>
> expression = a x ^c + b y ^(1-c);
>
> mx =
>
>  Module[{f,expression = expression},
>
>   f[x_: x,y_: y][a_ :a,b_: b,c_: c]:=Evaluate@expression;
>
>   f[k1,k2][.2,.3,.4]
>
>   ]
>
>
>
> (* and working as the following works *)
>
>
>
> m1=
>
>  Module[{f},
>
>   f[x_: x,y_: y][a_ :a,b_: b,c_: c]:=Evaluate@expression;
>
>   f[k1,k2][.2,.3,.4]
>
>   ]
>
>
>
> Many thanks
>
>
>
>
>
> E. martin-Serrano
>
>

```

• Prev by Date: Re: Functions inside modules with external definitions
• Next by Date: optimization
• Previous by thread: Re: Functions inside modules with external definitions
• Next by thread: Re: Re: Functions inside modules with external definitions