Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Functions inside modules with external definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96494] Functions inside modules with external definitions
  • From: "E. Martin-Serrano" <eMartinSerrano at telefonica.net>
  • Date: Sat, 14 Feb 2009 03:14:13 -0500 (EST)

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: Logarithmic Plot
  • Next by Date: Re: Re: Logarithmic Plot
  • Previous by thread: Re: Benchmark performance Under AIX
  • Next by thread: Re: Functions inside modules with external definitions