Wolfram Research

Function Repository Resource:

FormalizeSymbols (2.2.0) current version: 2.4.0 »

Source Notebook

Convert symbols in an expression into their formal versions

Contributed by: Paco Jain (Wolfram Research)

ResourceFunction["FormalizeSymbols"][expr]

converts all symbols in expr to their formal counterparts where possible.

Details and Options

ResourceFunction["FormalizeSymbols"] has the HoldFirst attribute to prevent evaluation of defined symbols prior to replacement.
ResourceFunction["FormalizeSymbols"] works on single-character named symbols for which a "formal" counterpart exists. The System` symbols Pi, N and C are never replaced.
By default, ResourceFunction["FormalizeSymbols"] only replaces symbols in the current context, as given by $Context. To replace symbols from other contexts, use the option "ReplacementContexts":
"ReplacementContexts"Automatica list of contexts for which to formalize symbols (or All)
Replaced symbols will always be in the System` context in output regardless of the context in which they began.

Examples

Basic Examples (3) 

Convert symbols to their formal counterparts:

In[1]:=
ResourceFunction["FormalizeSymbols"][a + b]
Out[1]=

FormalizeSymbols works on Latin, Greek and other symbols for which there exists a formal version in the Wolfram Language:

In[2]:=
ResourceFunction["FormalizeSymbols"][\[Phi]/8]
Out[2]=
In[3]:=
ResourceFunction["FormalizeSymbols"][Cos[\[Sampi]]]
Out[3]=

FormalizeSymbols works on both uppercase and lowercase letters:

In[4]:=
ResourceFunction["FormalizeSymbols", ResourceVersion->"2.2.0"][(r/A) \[Lambda] + \[CapitalLambda]]
Out[4]=

Scope (2) 

FormalizeSymbols does not affect symbols with multi-character symbol names:

In[5]:=
ResourceFunction["FormalizeSymbols"][a + blah]
Out[5]=

By default, FormalizeSymbols only affects symbols in the current context (here Global`). To allow replacement of symbols in other contexts, use the option "ReplacementContexts":

In[6]:=
ResourceFunction["FormalizeSymbols"][myContext`a + b]
Out[6]=
In[7]:=
ResourceFunction["FormalizeSymbols"][myContext`a + b, "ReplacementContexts" -> All]
Out[7]=

Options (4) 

ReplacementContexts (4) 

The default setting of "ReplacementContexts"Automatic replaces only symbols in $Context (here Global`):

In[8]:=
ResourceFunction["FormalizeSymbols"][myContext`a + b]
Out[8]=

The "ReplacementContexts" option can be set to a list of contexts for which symbols should be formalized:

In[9]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`b, "ReplacementContexts" -> {"myContext`"}]
Out[9]=

With the setting "ReplacementContexts"All, symbols in any context other than System` are replaced:

In[10]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`b, "ReplacementContexts" -> All]
Out[10]=

Non-default settings of "ReplacementContexts" can result in unwanted symbol collisions:

In[11]:=
ResourceFunction["FormalizeSymbols"][myContext`a + yourContext`a, "ReplacementContexts" -> All]
Out[11]=

Properties and Relations (2) 

FormalizeSymbols has the HoldFirst attribute to prevent evaluation of defined symbols prior to replacement:

In[12]:=
a = 2;
ResourceFunction["FormalizeSymbols"][a + B]
Out[12]=

Symbols within the System` context are never replaced. In particular, this includes Pi, N and C:

In[13]:=
ResourceFunction["FormalizeSymbols"][n \[Pi]/2, "ReplacementContexts" -> All]
Out[13]=
In[14]:=
ResourceFunction["FormalizeSymbols"][Hold[N[a + b]], "ReplacementContexts" -> All]
Out[14]=
In[15]:=
ResourceFunction["FormalizeSymbols"][C[1] + \[Lambda], "ReplacementContexts" -> All]
Out[15]=

Version History

  • 2.4.0 – 21 July 2023
  • 2.3.0 – 28 June 2023
  • 2.2.0 – 24 June 2021

Related Resources

License Information