MathGroup Archive 2009

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

Search the Archive

Re: Re: Functions inside modules with external definitions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96568] Re: [mg96505] Re: Functions inside modules with external definitions
  • From: "E. Martin-Serrano" <eMartinSerrano at telefonica.net>
  • Date: Mon, 16 Feb 2009 07:00:27 -0500 (EST)
  • References: <gn5ugm$h02$1@smc.vnet.net> <200902150819.DAA07828@smc.vnet.net>

Jens,

I use Module[] because the definition of f[] is inside a Module with a lot
of dynamic stuff (say 200 lines of code); and f[] (and other function
definitions, local to the module, are the root of all the dynamic treatment.
I never thought of Block as a construct or "container" for dynamic programs.
But maybe I was wrong.

Well, I tried many other approaches before posting but I did want to mess up
the example with other details. 

I will review the other answers I have had, see what happens, and prepare a
full feedback if necessary. But, please, I would appreciate your remarks to
this as your solution works as needed. (Although, inside Block[]).

Regards

Emilio.
 

-----Original Message-----
From: Jens-Peer Kuska [mailto:kuska at informatik.uni-leipzig.de] 
Sent: Sunday, February 15, 2009 9:20 AM
To: mathgroup at smc.vnet.net
Subject: [mg96568] [mg96505] Re: Functions inside modules with external definitions

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: linear regression with errors in both variables
  • Next by Date: Re: Shortest Path Problem
  • Previous by thread: Re: Functions inside modules with external definitions
  • Next by thread: Conditional evaluations of functions