MathGroup Archive 2009

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

Search the Archive

User Defined Global Variables - How to do it?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg95338] User Defined Global Variables - How to do it?
  • From: Donald DuBois <donabc at comcast.net>
  • Date: Fri, 16 Jan 2009 06:07:16 -0500 (EST)

I am trying to define my own global variables which will be known to every function that I write just  like the system global variable "$HomeDirectory".  I have a function which 
has been written to read values from a disk file and using those values calculate the values
of the global variables.

I do not know where to put this fuhction so that, at the begiinning of a Mathematica session, these user defined global varialbes are available to be used by any function that I have written that is part of the packages that are autmatically loaded at the beginning of a session.

I have previously defined my own global variables in a file called init.m and this file is located in directory:

C:\Documents and Settings\Donald\Application Data\Mathematica\Autoload\Foo

(I have created the last directory "Foo" off of Autoload.)  

This method works for global variables that are defined by a simple 
assignment statement within the file init.m:


globalVarA = value 


Every function that I have written and put into a package - and these 
packages are loaded *** automatically  ***  at the beginning of a Mathematica
session -  can use and knows the value of globalVarA which is defined
in init.m.  So, before the packages are loaded at the first time I hit Shift + Enter  the system already knows the value of globalVarA because it was defined in the init.m file. 

I now need global variables that cannot be defined so easily by a simple
assignment statement.  Their  values have to be read in and calculated in some function
that I call.  The sole purpose of this function is to read in and calculate the values
of these global variables.  There are two problems: 

(1) I don't know how
to make these values globally available to all other functions that are used
in the session and that are defined in the packages I created. 

(2) I don't know how to get this function [the function which calculates the values
of the global variables) to "start up" automatically BEFORE I ever have to
hit the first Shift + Enter.  This function which calculaes the values of global variables uses itself some of the other functions I defined in the packages I created which are automatically loaded at session start time. 

For instance, below is  the schematic defintion of a function where I am 
calculating the values of two global variables, globalVarB and globalVarC
that I want every other function to have access to [access to the values
in these global variables]  if they need the 
values of these two variables in their own calculations, just like every 
function has access to the  system global variable "$HomeDirectory".

globalDefintions[] := Module[{},

    (* Read in values and do calculations for globalVarB and globalVarC  ")
  
     Return[{globalVarB, globalVarC}]
     
    ];(* End Module globalDefinitions *)

Then the function is invoked: 

    {globalVarB, globalVarC} = globalDefinitions[];

Where can I put the definition of this function called globalDefintions and its invocation so that:

(1) the user does not have to explicity invoke the
globalDefinition function at the beginning of every 
Mathematica session

(2) every function in any package that I write should know the values
of these global variables. (and these packages that I wrote  are currently automatically loaded at the beginning of a Mathematica session).

I have tried putting the above defintion of the globalDefinition function in the
init.m file and then calling this function from within
the init.m file, but that doesn't work.  I have tried the same thing by
putting the globalDefinition function in the Private and Public
sections of a package that I have written
(which, as stated above, is automatically loaded at the beginning of every
session) and invoked the function within the package - also in a the private and
public sections of the package 
(in a cell in the package that is an initialization cell) but that does not work either.

Are there any solutions?

Thanks in advance for your help.


  • Prev by Date: Re: Mathematica and Access
  • Next by Date: Re: Intermediate computational results
  • Previous by thread: Re: Finding a sine wave
  • Next by thread: Re: User Defined Global Variables - How to do it?