3
$\begingroup$

I have written two versions of the function below in a package. When I load the package and call the function, Why does the first version where the variable s has no explicit Context return the long variable name? Or why do I have to put the variable in the Global context to return the short name? While every other function component is automatically assigned to the package context. Is there something weird happening with the Block function?

x={{k1p -> 6.680629079305845`, k1m -> 9.100970498785017`, 
    k2p -> 5.9669213788246225`, state -> 1.`}, {k1p -> 6.680629079305845`, 
    k1m -> 9.100970498785017`, k2p -> 5.9669213788246225`, state -> 0.`}}
threadParameters[epsilon_/;epsilon==1,k_,params_,
signal_:signal,numberOfStrains_:numberOfStrains]:=
  Block[{variateList,s,i,j},
   variateList = params;
    (*Prepare each jth simulation parameter list by
     adding the appropriate signal strength*)
   mutantParameters =
    Table[
     Join[{s->j},variateList[[i]]],
     {j,signal},
     {i,1,numberOfStrains}
    ]
 ];
 threadParameters[1, 1, x,{1,3,5},2]
({SimulatorV2Debug`Private`s->1,k1p->6.68063,k1m->9.10097,k2p>5.96692,state->1.}
 {SimulatorV2Debug`Private`s->1,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->0.}
 {SimulatorV2Debug`Private`s->3,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->1.}
 {SimulatorV2Debug`Private`s->3,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->0.}
 {SimulatorV2Debug`Private`s->5,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->1.}
 {s->5,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->0.})

threadParameters[epsilon_/;epsilon==1,k_,params_,
signal_:signal,numberOfStrains_:numberOfStrains]:=
  Block[{variateList,s,i,j},
   variateList = params;
    (*Prepare each jth simulation parameter list by
     adding the appropriate signal strength*)
   mutantParameters =
    Table[
     Join[{Global`s->j},variateList[[i]]],
     {j,signal},
     {i,1,numberOfStrains}
    ]
 ];
 threadParameters1[1, 1, x,{1,3,5},2]
({s->1,k1p->6.68063,k1m->9.10097,k2p>5.96692,state->1.}
 {s->1,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->0.}
 {s->3,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->1.}
 {s->3,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->0.}
 {s->5,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->1.}
 {s->5,k1p->6.68063,k1m->9.10097,k2p->5.96692,state->0.})
$\endgroup$
4
$\begingroup$

Every symbol always has a context. Most of the time we are in the Global context.

$Context
(* "Global`" *)

Normally we don't have to specify a variable as

Global`s

Simply writing s is sufficient provided we are in the Global context.

Inside your package the symbols and functions are defined in the

SimulatorV2Debug`Private

context. In the function threadParameters inside the Block the variable s is from that context.

That is why you get the two different results.

One

s is in the

SimulatorV2Debug`Private

context and you get for example:

{SimulatorV2Debug`Private`s->1,k1p->6.68063,k1m->9.10097,k2p>5.96692,state->1.}

Two

In your function you explcity use:

Join[{Global`s->j},variateList[[i]]],

so s is in the Global context and you get:

{s->1,k1p->6.68063,k1m->9.10097,k2p>5.96692,state->1.}

Note that this is the same as:

{Global`s->1,k1p->6.68063,k1m->9.10097,k2p>5.96692,state->1.}

Simply because $Context is Global, the long name is not written.

PS

I didn't want to introduce a diversion but in your second definition you are missing a comma here:

threadParameters[epsilon_/;epsilon==1,k_,params_

I suspect that is simply a copy and paste error.

$\endgroup$
  • $\begingroup$ Ah yes, that is a typo. Now fixed. $\endgroup$ – tarhawk Nov 7 '15 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.