# Difference between variables and expressions for ReplaceAll

I'm doing some GR calculations with xAct. I get an expression that is heavy. In order to simplify the results, I try to use the function ReplaceAll in simple cases. Here's an expression I want to simplify. The $$R_{abcd}R^{abcd}$$ is the one I want to replace. To make it simple, I put $$1$$:

ReplaceAll[perturb,
x_*RiemannCD[a, b, c, d] RiemannCD[-a, -b, -c, -d]*y_ -> 1]


where perturb is a variable containing the expression above. In its definition I used :

\[Delta]Sphi =  VarD[\[Phi][]][Lc + Lm] // ContractMetric;
perturb = Perturbation[\[Delta]Sphi, 0] // ExpandPerturbation // SortCovDs // ContractMetric



Here is the InputForm for perturb:

PD[-a][PD[a][\[Phi][]]] + PD[a][PD[-a][\[Phi][]]] -
4*\[Alpha]*RicciCD[-a, -b]*RicciCD[a, b]*
Derivative[1][F][\[Phi][]] + \[Alpha]*RicciScalarCD[]^2*
Derivative[1][F][\[Phi][]] +
\[Alpha]*RiemannCD[-a, -b, -c, -d]*RiemannCD[a, b, c, d]*
Derivative[1][F][\[Phi][]] +
8*\[Beta]^3*RicciCD[-a, -b]*RicciCD[a, b]*\[Psi][]*
Derivative[1][F][\[Phi][]] -
2*\[Beta]^3*RicciScalarCD[]^2*\[Psi][]*
Derivative[1][F][\[Phi][]] -
2*\[Beta]^3*RiemannCD[-a, -b, -c, -d]*RiemannCD[a, b, c, d]*\[Psi][]*
Derivative[1][F][\[Phi][]] + \[Beta]^3*\[Psi][]^2*
Derivative[1][F][\[Phi][]]


Here is the problem:

• With perturb, the function gives me back the same expression, without any replacement.
• If instead I directly put the expression above, it perfectly works !

Can you explain what's going on and how I can to not to place everytime the whole expression ?

Thanks !

TrodaroX

• Welcome to Mathematica StackExchange! Please edit the question and include the definition of perturb (copy and paste the InputForm from Mathematica) :) Commented Mar 29 at 15:00
• Hmm, I guess I wasn't really clear. Copy the output of the perturb, so that we can try the code ourself. perturb = -2β^3 RiemannCD[...] ... Commented Mar 29 at 15:23

You have to explicitely specify the lengths of factors in products, eg. the number of factors fact may be 0 to $$\infty$$

-2 \[Beta]^3 riemann1["\[Del]", a, b, c, d] riemann2["\[Del]", a, b,
c, d] \[Psi] F'[\[Phi]] + \[Beta]^3 \[Psi]^2 F'[\[Phi]] /. {fact___*
riemann1["\[Del]", a, b, c, d] riemann2["\[Del]", a, b, c, d] :>Times[ fact]}


$$\beta ^3 \psi ^2 F'(\phi )-2 \beta ^3 \psi F'(\phi )$$

Very important: Enclose fact in a Times, because the Pattern match yields a sequence of factors expanded in the outer Plus as a sum, not as a product.

• Hey ! Thanks for your answer. When I try to execute ReplaceAll[perturb,{fact___* RiemannCD[-a, -b, -c, -d] RiemannCD[a, b, c, d] :>Times[ fact]} this still gives me the unchanged expression. Even with /. notation. Commented Mar 29 at 17:06

I solved the problem by using the MakeRule method of xAct

ruleG = MakeRule[{RiemannCD[a, b, c, d] RiemannCD[-a, -b, -c, -d],
G - RicciScalarCD[]^2 + 4* RicciCD[a, b] RicciCD[-a, -b]}];
RiemannCD[-a, -b, -c, -d]*RiemannCD[a, b, c, d] /. ruleG


it gives:

G + 4*RicciCD[-a, -b]*RicciCD[a, b] - RicciScalarCD[]^2