1
$\begingroup$

So, I'm trying to create two sets of similar variables, for example: a, b, c. This would be used with the same function. What I tried to do is creating two contexts "A`" and "B`" where the variables would have their own association:

Begin["A`"];
a=1;
b=2;
c=3;
End[];

Begin["B`"];
a=4;
b=5;
c=6;
End[];

Then I desired to create a function in the Global` context so it would be accessible to the former ones:

f=Compile[{x},(a*x + b)/c]

Then, I would go back to the desired context "A`" or "B`", and since no function f is defined there Mathematica would look for the next f in $ContextPath order. But, of course, since the function is defined in the Global` context, the values of a, b, and c used in its definition need to be in the same Context (at least the way I defined it).

The thing is, what I'm trying is to be able to reuse functions so I don't have to write two similar programs for two different sets of variables.

I was hoping, when I found out about Context, to be able to move from one context to the other and by that selecting the right set of variables to be used by the functions. Is it possible? If not, what would be the best approach?

Also I found a little confusing how Contexts are explained in the Help Section. I thought that they should be thought of like directories. So if I'm in Global` and evaluated Begin["`A`"], when I then evaluated $ContextPath I should get {...,System`, Global`, A`}, which I didn't.

Thanks

$\endgroup$
1
  • $\begingroup$ see closely related: Begin vs Module and all topics linked there. $\endgroup$
    – Kuba
    Commented May 23, 2017 at 6:59

1 Answer 1

1
$\begingroup$

So I think your fundamental issue is that when you define:

f=Compile[{x},(a*x + b)/c]

(or as is the more standard Mathematica syntax)

f[x_]:=(a*x+b)/c

The variables are automatically bound to $Context, if there is no prior definition form them on the context path.

We can see this here:

In[35]:= Begin["A`"]

Out[35]= "A`"

In[36]:= f[x_] := (a + b)*x

In[37]:= End[]

Out[37]= "A`"

In[38]:= A`f // DownValues

Out[38]= {HoldPattern[A`f[A`x_]] :> (A`a + A`b) A`x}

All of the symbols are placed in the "A`" context.

In general the Context mechanism is used for defining functions in such a way that you know there will be no collisions with something that already exists.

Classically, this is how Mathematica packages are built (although there is a new-style of package definition that seems to do a lot of the context management automatically).

We do something like:

BeginPackage["A`"]
(*Sets $Context to "A`" and sets $ContextPath to {"System`","A`"}*)

symbol1::usage="Baby's first symbol";
(*Use a symbol to add it to the symbol table for context "A`"*)

Begin["`Private`"];
(*Begins a chunk of private code that won't end up on $ContextPath so that symbols may be used without fear of colliding with anything else*)

symbol1:=RandomReal[];
(*"A`" is on $ContextPath so this is really A`symbol1*)

ra
nd
om
sym*bol/s
(*None of these will appear on $ContextPath so we can use them freely without fear of collisions*)

End[];
(*Ends the Private block*)

EndPackage[];
(*Resets the $Context and $ContextPath to what they were begin BeginPackage was used*)

It is merely a scoping mechanism. You can use relative paths, e.g. I can write `Private`a to define the symbol in in a private extension off of $Context, but at the moment of use / definition these are resolved into definite symbols.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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