Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 19 down vote accepted

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)?

share|improve this answer
2  
+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/… –  Leonid Shifrin Jan 18 '12 at 0:45
    
@Szabolcs: The question I linked to gives a case of where you need this. –  Mike Bantegui Jan 18 '12 at 0:49
    
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. –  Mike Bantegui Jan 18 '12 at 1:01

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.

share|improve this answer
    
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. –  Mike Bantegui Jan 18 '12 at 0:42

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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