ReplaceAll (/.)
Usage
Notes
Further Examples
ReplaceAll replaces once at all levels of the expression.
In[1]:=
|
Out[1]=
|
This is the same command written with the slash-dot notation.
In[2]:=
|
Out[2]=
|
Replace applies only to the whole expression.
In[3]:=
|
Out[3]=
|
In[4]:=
|
Out[4]=
|
This replaces a by b first. Then there are two b's that get replaced by d.
In[5]:=
|
Out[5]=
|
By contrast, this replaces a by b and b by d at one time.
In[6]:=
|
Out[6]=
|
ReplaceAll applies the set of rules exactly once on each part of the expression.
In[7]:=
|
Out[7]=
|
ReplaceRepeated uses ReplaceAll repeatedly until the expression stops changing.
In[8]:=
|
Out[8]=
|
Here is the same command written with the slash-slash-dot notation.
In[9]:=
|
Out[9]=
|
This constructs a list of replacements from two lists.
In[10]:=
|
Out[10]=
|
This applies the rules.
In[11]:=
|
Out[11]=
|
This rule replaces expressions whose heads are Integer, Rational, or Plus by their logarithms.
In[12]:=
|
Out[12]=
|
In[13]:=
|
Out[13]=
|
Using ReplaceAll, only the first prime number in this list is replaced by its cube.
In[14]:=
|
Out[14]=
|
Using ReplaceRepeated, all of the prime numbers are replaced by their cubes.
In[15]:=
|
Out[15]=
|
Here is an expression.
In[16]:=
|
Here subexpressions of expr with head f or g and one or more arguments are squared.
In[17]:=
|
Out[17]=
|
This does the same thing.
In[18]:=
|
Out[18]=
|
Here only subexpressions which are products whose factors have the form _[__] get squared.
In[19]:=
|
Out[19]=
|
Here all expressions of the form _[__] that are free of Power or Times get squared.
In[20]:=
|
Out[20]=
|
Here a rule is applied repeatedly subject to the given conditions. The rule extracts all subexpressions of the form m p + n q as expressions with head h where m and n are integers and m = -n.
In[21]:=
|
Out[21]=
|
This defines the function evaluatePatternMatches. In the held expression heldexpr, it replaces subexpressions that match the pattern patt by their evaluated counterparts.
In[22]:=
|
In[23]:=
|
Out[23]=
|
In[24]:=
|
See also the Further Examples for Repeated.
|