# Transformation Rules and Definitions

expr/.lhs->rhs | apply a transformation rule to expr |

expr/.{lhs_{1}->rhs_{1},lhs_{2}->rhs_{2},…} | try a sequence of rules on each part of expr |

expr/.rules | try rules once on each part of expr |

expr//.rules | try rules repeatedly until the result no longer changes |

expr/.rules | apply rules to all subparts of expr |

Replace[expr,rules] | apply rules to the whole of expr only |

Replace[expr,rules,levspec] | apply rules to parts of expr on levels specified by levspec |

Replace[expr,rules] | apply rules in one way only |

ReplaceList[expr,rules] | apply rules in all possible ways |

Dispatch[rules] | create a representation of a list of rules that includes dispatch tables |

expr/.drules | apply rules that include dispatch tables |

expr/.lhs->rhs | apply a transformation rule to a specific expression |

lhs=rhs | assign a value which defines a transformation rule to be used whenever possible |

*permanent*. They continue to be used for the duration of the session, unless you explicitly clear or overwrite them.

x=. | remove the value assigned to the object x |

Clear[x,y,…] | clear all the values of x, y, … |

i++ | increment the value of i by 1 |

i-- | decrement i |

++i | pre‐increment i |

--i | pre‐decrement i |

i+=di | add di to the value of i |

i-=di | subtract di from i |

x*=c | multiply x by c |

x/=c | divide x by c |

x=y=value | assign the same value to both x and y |

{x,y}={value_{1},value_{2}} | assign different values to x and y |

{x,y}={y,x} | interchange the values of x and y |

PrependTo[v,elem] | prepend elem to the value of v |

AppendTo[v,elem] | append elem |

v={v,elem} | make a nested list containing elem |

a[i]=value | add or overwrite a value |

a[i] | access a value |

a[i]=. | remove a value |

?a | show all defined values |

Clear[a] | clear all defined values |

Table[a[i],{i,1,n}] or Array[a,n] | convert to an explicit List |

*particular*expression f[a] occurs, it is to be replaced by b. But the definition says nothing about expressions such as f[y], where f appears with another "index".

f[x]=value | definition for a specific expression x |

f[x_]=value | definition for any expression, referred to as x |

*mapping*. When you define values for, say, f[1] and f[2], you specify the image of this mapping for various discrete points in its domain. Defining a value for f[x_] specifies the image of f on a continuum of points.

*specific*

*expression*f[x] is still used, but the new general definition for f[x_] is now used to find a value for f[y]:

*when*the expression rhs is evaluated. lhs=rhs is an

*immediate assignment*, in which rhs is evaluated at the time when the assignment is made. lhs:=rhs, on the other hand, is a

*delayed assignment*, in which rhs is not evaluated when the assignment is made, but is instead evaluated each time the value of lhs is requested.

lhs=rhs
(immediate assignment)
| rhs is evaluated when the assignment is made |

lhs:=rhs
(delayed assignment)
| rhs is evaluated each time the value of lhs is requested |

lhs=rhs | rhs is intended to be the "final value" of lhs (e.g. f[x_]=1-x^2) |

lhs:=rhs | rhs gives a "command" or "program" to be executed whenever you ask for the value of lhs (e.g. f[x_]:=Expand[1-x^2]) |

*recursion relations*in the Wolfram Language. In a typical case, a recursion relation gives the value of a function with an integer argument in terms of values of the same function with arguments , , etc. The Fibonacci function definition used above is an example of this kind of recursion relation. The point is that if you calculate say by just applying the recursion relation over and over again, you end up having to recalculate quantities like many times. In a case like this, it is therefore better just to

*remember*the value of , and look it up when you need it, rather than having to recalculate it.

*downvalues*of f.

*upvalues*, which allow definitions to be associated with symbols that do not appear directly as their head.

f[g]^=value or f[g[args]]^=value | |

make assignments to be associated with g, rather than f | |

f[g]^:=value or f[g[args]]^:=value | |

make delayed assignments associated with g | |

f[arg_{1},arg_{2},…]^=value | make assignments associated with the heads of all the arg_{i} |

f[…]:=rhs | downvalue for f |

f/:f[g[…]][…]:=rhs | downvalue for f |

g/:f[…,g,…]:=rhs | upvalue for g |

g/:f[…,g[…],…]:=rhs | upvalue for g |

expr=value | define a value to be used whenever possible |

N[expr]=value | define a value to be used for numerical approximation |

N[expr]=value | define a numerical value to be used when default numerical precision is requested |

N[expr,{n,Infinity}]=value | define a numerical value to be used when n‐digit precision and any accuracy is requested |

DownValues[f] | give the list of downvalues of f |

UpValues[f] | give the list of upvalues of f |

DownValues[f]=rules | set the downvalues of f |

UpValues[f]=rules | set the upvalues of f |