1
$\begingroup$

I want to use the package NCAlgebra. However after successfully installing the package and trying to write some code, basic functions like

SetNonCommutative[...]

is collored in red with the statement:

A symbol occurs in more than one context: One of the definitions is shadowed.

How can I force Mathematica to use the function from the package?

$\endgroup$
  • 2
    $\begingroup$ Did you quit the kernel and then reload the package? $\endgroup$ – Jason B. Feb 9 '16 at 9:50
  • 1
    $\begingroup$ @JasonB Thanks this resolves the problem! $\endgroup$ – Anne O'Nyme Feb 9 '16 at 10:05
  • $\begingroup$ Glad to help Anne. $\endgroup$ – Jason B. Feb 9 '16 at 10:07
3
$\begingroup$

Quitting and restarting may not always be the best way to handle shadowing, as it destroys the results of any computations performed. One can also use Remove. From the tutorial Contexts

If you once introduce a symbol that shadows existing symbols, it will continue to do so until you either rearrange $ContextPath, or explicitly remove the symbol. You should realize that it is not sufficient to clear the value of the symbol; you need to actually remove the symbol completely from the Wolfram Language. You can do this using the function Remove[s].

Example. First I Quit[] for a clean start. I then define foo, which creates the symbol Global`foo.

Quit[]

foo = 1
(*  1  *)

Now let's say I load a package called "Foo`", which creates its own variable foo. Then the following two commands in effect happen somewhere along the way.

Foo`foo = 2
(*  2  *)

AppendTo[$ContextPath, "Foo`"]
(*
  {"StreamingLoader`", "IconizeLoader`", "CloudObjectLoader`", 
   "PacletManager`", "System`", "Global`", "Foo`"}
*)

At this point foo is shadowed and appears in red.

Mathematica graphics

Let's say that Global`foo is unnecessary. Then the following removes the shadowing.

Remove["Global`foo"]

The cells in which foo and Foo`foo appeared above now look like this:

Mathematica graphics


Another alternative it is to live with the shadowing and always type Global`foo for one's own foo. In a properly written package, internal references to the package's foo should always refer to Foo`foo. Whether it is more convenient to type the context or remove the symbol and use a different, unshadowed variable name is up to the user.


Related Q&A:

$\endgroup$

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.