9.8. Mapping at specified positions

We use the package ExpressionManipulation` (written by David Park & Ted Ersek) to have full control on the positions of an expression

Suppose we have an expression like this, which is clearly 0 because U is antisymmetric and S is symmetric:

In[965]:=

expr = U[a, b, c] v[-a] v[-b] v[-c] + U[a, b, c] v[-a] S[-b, -c] + TT[-b, -c] v[b] v[c] - TT[-d, -e] v[d] v[e]

Out[965]=

S_bc^   U_   ^abc v_a^ + U_   ^abc v_a^  v_b^  v_c^ + τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e

In[966]:=

Simplification[expr]

Out[966]=

0

Each object in the expression can be identified in a tree-like form by its position. In this case there are four positions at level 1:

In[967]:=

expr//ColorTerms

Out[967]=

{1}  S_bc^   U_   ^abc v_a^ + {2}  U_   ^abc v_a^  v_b^  v_c^ + {3}  τ_bc^   v_ ^b v_ ^c + {4}   -τ_de^   v_ ^d v_ ^e

We can evaluate a given function at one or several positions:

In[968]:=

MapAt[Simplification, expr, {2}]

Out[968]=

S_bc^   U_   ^abc v_a^ + τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e

In[969]:=

MapAt[Simplification, expr, {{1}, {2}}]

Out[969]=

τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e

In order to map a function over several terms simultaneously we need to make use of the concept of ExtendedPosition (also from the ExpressionManipulation` package) and the new function NewMapAt. See notes for ExtendedPosition and eP.

In[970]:=

MapAt[f, expr, {{3}, {4}}]

Out[970]=

f[τ_bc^   v_ ^b v_ ^c] + f[-τ_de^   v_ ^d v_ ^e] + S_bc^   U_   ^abc v_a^ + U_   ^abc v_a^  v_b^  v_c^

In[971]:=

NewMapAt[f, expr, eP[{}, {3, 4}]]

Out[971]=

f[τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e] + S_bc^   U_   ^abc v_a^ + U_   ^abc v_a^  v_b^  v_c^

We can also localize a given pattern:

In[972]:=

expr//ColorPositionsOfPattern[U[__]]

Out[972]=

S_bc^   ({1, 2}  U_   ^abc) v_a^ + ({2, 1}  U_   ^abc) v_a^  v_b^  v_c^ + τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e

or map a function onto the occurrences of that pattern:

In[973]:=

positions = Position[expr, U[__]]

Out[973]=

{{1, 2}, {2, 1}}

In[974]:=

MapAt[f, expr, positions]

Out[974]=

f[U_   ^abc] S_bc^   v_a^ + f[U_   ^abc] v_a^  v_b^  v_c^ + τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e

In case the expression is held, we can use EvaluateAt (also from the package ExpressionManipulation`) instead of MapAt:

In[975]:=

Hold[U[a, b, c] v[-a] v[-b] v[-c] + U[a, b, c] v[-a] S[-b, -c] + TT[-b, -c] v[b] v[c] - TT[-d, -e] v[d] v[e] + 1 + 2]

Out[975]=

Hold[U_   ^abc v_a^  v_b^  v_c^ + U_   ^abc v_a^  S_bc^   + τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e + 1 + 2]

In[976]:=

MapAt[Simplification, %, {1, 1}]

Out[976]=

In[977]:=

EvaluateAt[{1, 1}, Simplification][%%]

Out[977]=

Hold[0 + U_   ^abc v_a^  S_bc^   + τ_bc^   v_ ^b v_ ^c - τ_de^   v_ ^d v_ ^e + 1 + 2]


Created by Mathematica  (May 16, 2008) Valid XHTML 1.1!