MathGroup Archive 2009

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

Search the Archive

Re: Functions inside modules with external definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96519] Re: Functions inside modules with external definitions
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sun, 15 Feb 2009 03:22:29 -0500 (EST)
  • References: <gn5ugm$h02$1@smc.vnet.net>

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
> 
> 
Evaluate is irrelevant to your problem - which is that you need to 
replace the variables in your expression.

I would use a different symbol for the pattern variables:

g[xx_: x,yy_: y]:=(expression/.{x->xx,y->yy});

I am not sure why you require to define 'f' in such a complex way. There 
may be easier ways to solve your underlying problem - I suggest you 
describe what it is you want to do.

David Bailey
http://www.dbaileyconsultancy.co.uk



  • Prev by Date: Re: newbie here,, need help with parametrics
  • Next by Date: Re: Functions inside modules with external definitions
  • Previous by thread: Re: Functions inside modules with external definitions
  • Next by thread: Re: Functions inside modules with external definitions