MathGroup Archive 2011

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

Search the Archive

Re: Declaring the golbal variables in Packages

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115966] Re: Declaring the golbal variables in Packages
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Fri, 28 Jan 2011 06:10:57 -0500 (EST)
  • References: <ihravp$8na$1@smc.vnet.net>

Hi,

> The coding is as in the following, Please take a look at
> "Begin["`Global`"]; "
> in the 4th line. Is this right or not? That is the question.

it is not wrong in a strict sense, but it does not do what I think you
expect: it will create a private context Work`MyPackages`cMode`Global`,
which is an unusual name for a private context :-)

> =========Package========================
> 
> BeginPackage["Work`MyPackages`cMode`"];
> cMode::usage = "cMode[NEvanU_,NEvanL_,NEvanC_] returns the number \
> of mode (propagation + evanescent) mode";
> Begin["`Global`"];
> cMode[NEvanU_, NEvanL_, NEvanC_] :=
>   Module[{},
> 	NPropU = Floor[2*f*Take[a, l]*Sqrt[eps1*mue1]];
>     NModeU = NPropU + NEvanU;
> 	NPropL = Floor[2*f*Take[b, s]*Sqrt[eps1*mue1]];
>     NModeL = NPropL + NEvanL;
>     NPropC = Floor[2*f*alpha*Sqrt[eps0*mue0]];
>     NModeC = NPropC + NEvanC;
> Return[{NModeU, NModeL, NModeC}]]
> End[];
> EndPackage[];
> 
> ==============Execution in the Notebook==============================
> Remove["`*"];
> l = 4; s = 2;
> a = {31/10^3, 12/10^3, 11/10^3, 23/10^3};
> Tu = {40/10^3, 25/10^3, 20/10^3, 30/10^3};
> b = {5/10^3, 5/10^3};
> Tl = {55/10^3, 45/10^3};
> alpha = 120/10^3;
> Tup = Table[Sum[Tu[[i]], {i, 1, i}], {i, 1, l}];
> Tlp = Table[Sum[Tl[[i]], {i, 1, i}], {i, 1, s}];
> d = 6/10^3; h = 15.27/10^3;
> eps0 = 1/((36*Pi)*10^9); mue0 = (4*Pi)/10^7;
> epsr = 4; muer = 1;
> eps1 = eps0*epsr; mue1 = mue0*muer;
> f = 5*10^9; w = 2*Pi*f;
> k0 = w*Sqrt[eps0*mue0]; k1 = w*Sqrt[eps1*mue1];
> theta = 0;
> kx = k0*Sin[theta];
> kz = k0*Cos[theta];
> 
> {ModeU, ModeL, ModeC} = cMode[1, 1, 1]
> 
> ==============error messages in the output=========
> Take::normal: Nonatomic expression expected at position 1 in
> Take[Work`MyPackages`cMode`Global`a,Work`MyPackages`cMode`Global`l]. >>
> 

I think that what you want is that the function cMode will access the
symbols a and b from the Global` context. First of all I think this is a
horrible idea and if you do things like that you are undermining the
purpose of using contexts/packages at all. Nevertheless you could
achieve what you want by using explicit Global`a and Global`b in your
code. You cold also use a Needs["Global`"] after the Begin[] so that
Global` is on the $ContextPath and these symbols would be found if they
exist, but that will only work if a and b will already exist when the
package is loaded, otherwise they will still be created in the private
context.

There are some other constructs which make me think you should have a
look at the documentation for the functions you are using: a Module with
no variables isn't actually doing anything and the Return is also
something that you just don't need at the end of your function definition...

Maybe you want to explain what you really are trying to achieve in a
bigger picture so we can show you how you would to that in a less clumsy
way...

hth,

albert


  • Prev by Date: Re: Experimental`'NumericalFunction''
  • Next by Date: Re: Simple n-tuple problem - with no simple solution
  • Previous by thread: Re: CellContext - Not ready for primetime use (opinion).
  • Next by thread: implementation for Binarize