One rule working but not the other

30 views
Skip to first unread message

Sukruti

unread,
Aug 4, 2025, 12:26:18 PMAug 4
to xAct Tensor Computer Algebra
Hello,

In the attached notebook I have two rules, NewRule1 and NewRule2, with the exact same expressions, yet one of them is working while the other is not.

Any idea what's the reason and how to resolve the issue?

Cheers
Sukruti
Rule not working.nb

Thomas Bäckdahl

unread,
Aug 4, 2025, 12:39:45 PMAug 4
to xa...@googlegroups.com
Hi!

When you encounter strange behaviors like this, it is best to investigate if the rules are as equal as they look. Use InputForm or possibly FullForm to see what the rules actually are.

InputForm@NewRule1
reveals the problem. The first rules looks like
HoldPattern[1*RicciScalarCD[] ...
while for
InputForm@NewRule2
the first rule looks like
HoldPattern[RicciScalarCD[] ...

The problem is that HoldPattern will not simplify 1*RicciScalarCD[].. to RicciScalarCD[]..., while copy and paste did simplify it.
When applying the rule Mathematica will never find the pattern 1*RicciScalarCD[]..., so NewRule1 is not doing anything.

My advice is to work with equations. Manipulate the equations, and then transform the equations into substitution rules. Avoid manipulating rules as much as possible.

Regards
Thomas
--
You received this message because you are subscribed to the Google Groups "xAct Tensor Computer Algebra" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xact+uns...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/xact/582d4deb-82b2-4bf4-bfb7-9fc4985aa738n%40googlegroups.com.

Sukruti

unread,
Aug 4, 2025, 2:32:43 PMAug 4
to xAct Tensor Computer Algebra
Hi,

Thanks for the suggestions and the explanation. It's good to come to know about InputForm and FullForm and I will use them in the future if I encounter such issues.

On comparing the InputForm expressions of the LHS of NewRule1 and NewRule2, the only difference I see is that there's a factor of 1 present in NewRule1. So I tried applying NewRule1 on 1*OrigTerm but it still didn't work. It's strange.

While RuleWithCoeff has been defined starting from an equation, the definition of NewRule1 comes from a special case of RuleWithCoeff. I have dozens of rules like RuleWithCoeff which I need to take such special cases of. The starting points of those special cases are the rules defined via MakeRules instead of equations. So at least in this context, manipulating equations before defining the rules to get the special case rules does not seem to be a choice unless I start from scratch and redo everything using equations.

Cheers
Sukruti

Thomas Bäckdahl

unread,
Aug 5, 2025, 3:40:37 AMAug 5
to xa...@googlegroups.com
Hi!

All of these things are manifestations of evaluation control in Mathematica.
If you write something it is usually evaluated before the result is stored or displayed.
However, you can stop it from being evaluated with Hold, HoldPattern etc.
This is why manipulation of your patterns containing HoldPattern does not work the way you expected.

For instance
HoldPattern[aaa*bbb + ccc] /. {aaa -> 1, ccc -> 0}
gives
HoldPattern[1*bbb + 0]
The internal expression 1*bbb + 0 is not evaluated to bbb because HoldPattern stops the evaluation.
If you just write 1*bbb + 0 it is automatically evaluated to bbb, so 1*bbb + 0 would never appear in any ordinary expression.

If you temporarily want to evaluate what is in the HoldPattern, you can for instance do
% /. HoldPattern -> FFF /. FFF -> HoldPattern
Here, HoldPattern[1*bbb + 0] is changed to FFF[1*bbb + 0] which does not stop evaluation, so it is turned into FFF[bbb]. Finally, this is changed to HoldPattern[bbb].
Here, FFF can be any symbol that is not used for something else.

However, this trick might cause problems due to the evaluation of the left hand side of the rules. There might be a good reason for the HoldPattern to be there in the first place. It is not always necessary though.

The special property of HoldPattern is that Attributes[HoldPattern] contains HoldAll. This stops the evaluation. You an read more about evaluation in the Mathematica help under tutorial/Evaluation.

If you do manipulate long lists of rules in this way, it is very easy to get something unexpected. For instance you might get a rule 0->"something complicated". Applying a rule like that is a bad idea.
Your long list of questions to this forum regarding unexpected rules shows that manipulating rules is difficult and can cause problems unless you understand every single detail of what is happening.
Therefore, I would strongly recommend that you do as much of the manipulations as possible in the form of equations. Only after the manipulation, you turn the equation into a rule with for instance EqToRule and apply it.
For instance
expr /. EqToRule@myeq1

If you are using this many times, you can do
myrule1=EqToRule@myeq1;
expr/.myrule1
instead, but I seldom bother to do that.
Equations are also easier to display and read. Therefore, you would notice an equation like 0="something" before you try to turn that into a bad rule of the form 0->"something".

Regards
Thomas
Reply all
Reply to author
Forward
0 new messages