9
$\begingroup$

As the title says: What's the purpose of Unique?

  • I understand that it generates some unique symbol, but when and for what is it to be used? Are there applications in practical solutions, or is it for the backend of programs (I noticed that Block etc use variable names similar to what Unique creates)?
  • How do I use a variable generated by Unique? Do I store its name in another variable and then use that as a pointer of some sort by converting its content (e.g. $123) to an expression somehow?
$\endgroup$
3
  • 1
    $\begingroup$ Have you seen this? $\endgroup$ Commented Jan 21, 2012 at 7:35
  • $\begingroup$ That's a question Unique is the answer for. I'm aware of what it does, just not when and how. $\endgroup$
    – David
    Commented Jan 21, 2012 at 9:02
  • 1
    $\begingroup$ Take a look at this answer of mine where I needed a variable number of localized variables. I'm also showing how to use Temporary to avoid polluting the namespace with a large number of symbols. $\endgroup$
    – Szabolcs
    Commented Jan 21, 2012 at 9:07

2 Answers 2

17
$\begingroup$

Probably the most common use of Unique is in situations when you need a large number of local variables (and sometimes a variable number of local variables) so using Module is either inconvenient or impossible. In that case you can use the construction: vars= Table[Unique[x],{n}] or something of this kind. You can find a few examples in the archives of the MathGroup. One that I remember being quite pleased with myself can be found here:

http://mathforum.org/kb/thread.jspa?forumID=79&threadID=1185003&messageID=3868818

$\endgroup$
3
  • 4
    $\begingroup$ Welcome to Mathematica.SE, and thank you for the answer! I have slightly reformatted your answer to align it with the conventions of this site. You can click the 'edit' link to see how to achieve this effect. If you have any questions, please don't hesitate to ask, either in a comment here, in the "meta" section of this site, or by directly emailing me! $\endgroup$
    – Szabolcs
    Commented Jan 21, 2012 at 9:12
  • 3
    $\begingroup$ I second that! Great that you are here, Andrzej! $\endgroup$ Commented Jan 21, 2012 at 9:24
  • 1
    $\begingroup$ Welcome Andrzej. great that you joned. $\endgroup$
    – user21
    Commented Jan 21, 2012 at 9:38
6
$\begingroup$

Here are two applications I have run into:

1) Creating functions which, given some input, output a system of equations to be solved, or which create a system of equations and solves them.

1a) Create an n $\times$ n matrix of unknown variables:

varMatrix[m,n]:=Table[Unique[],{m},{n}]

1b) Modify a polynomial variety (or rather, a system of polynomials which we wish to make zero simultaneously) so that a certain variable becomes invertible, via the standard trick:

varietyWithInvertibleVariable[variety_,variable_]:=
  Append[variety, variable * Unique[] - 1]

2) Associating symbols to expressions:

reference[x_]:= Module[{ref},
  (reference[x]=ref=Unique[];
   dereference[ref]=x;
   ref
  )

I've used this to construct formal linear combinations of data structures and manipulate them algebraically.

$\endgroup$
1
  • 1
    $\begingroup$ Another example of 2 would be to render an expression inert (so it won't evaluate) by replacing all of the symbols with memoized undefined symbols. $\endgroup$ Commented Jan 25, 2013 at 4:50

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.