18
$\begingroup$

In a previous question on collecting terms for a multivariable polynomial, I answered with a solution that required a unused symbol to be used for a temporary substitution and back substitution.

It works and everything, but the problem is it depends on the behavior that a symbol is not defined. This is generally a bad idea because a user could have a legitimate use for the symbol we defined. It may even be in the polynomial in question.

This sort of thing can also come up in other contexts, when there may be a need for a temporary symbol. I know I've needed this before and I've often had to think of something random to use.

Is there any way I can get a unique and guaranteed-never-used symbol name for these things?

$\endgroup$

4 Answers 4

21
$\begingroup$

Unique will do precisely this. Try for example Unique[x], which returns a symbol with a name similar to x$123.

Here I should mention the Temporary attribute as well, which, when associated with a symbol, causes that symbol to be removed from the system when it's no longer referenced. This is occasionally useful when you need Unique.

But whenever you do something like this, the question comes up: why can't you just use localization (Module/Block)?

$\endgroup$
3
  • 2
    $\begingroup$ +1. As to Module-Block, there are cases when you do need Unique, for example when you want to create some number of unique symbols, and that number is only known at run-time. One situation like that happens when we use dummy symbols to hide certain things from the pattern-matcher, as e.g. here: stackoverflow.com/questions/8700934/… $\endgroup$ Jan 18, 2012 at 0:45
  • $\begingroup$ @Szabolcs: The question I linked to gives a case of where you need this. $\endgroup$ Jan 18, 2012 at 0:49
  • $\begingroup$ All good answers, but I'm going to accept this one because you were first and you have some extra explanation w.r.t. some features of Unique. $\endgroup$ Jan 18, 2012 at 1:01
13
$\begingroup$

There is a special-purpose function for this, Unique:

In[270]:= Unique[]
Out[270]= $3

In[271]:= Unique[a]
Out[271]= a$4318

However, these symbols will be guaranteed to be unique only within a given Mathematica session.

$\endgroup$
1
  • $\begingroup$ Per session uniqueness is fine for me. That alone is way more than I'd usually need since I create, use, then throw away the symbols all within a Block or Module. $\endgroup$ Jan 18, 2012 at 0:42
9
$\begingroup$

It's not random (uses a counter), but Unique[] will generate a new unused symbol.

$\endgroup$
9
$\begingroup$

Unique[] is the function that does exactly what you want. However, do note that Unique uses $ModuleNumber and increments it, so if your code depends on the value of $ModuleNumber or if you mess with it, you should be aware of the consequences.

$\endgroup$

Your Answer

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

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