MakeRule with Conditions

65 views
Skip to first unread message

Sukruti

unread,
Jun 4, 2025, 2:15:51 PMJun 4
to xAct Tensor Computer Algebra
Hello,

I'm having some difficulty understanding how MakeRule works. I gave it some conditions ((! PairQ[\[Zeta], \[Delta]] && ! PairQ[\[Eta], \[Delta]] && ! PairQ[\[Alpha], \[Delta]])) to check before applying the rule. But the rule is getting applied even when the conditions are not met.

The notebook is attached. Can someone explain what's going wrong?

Thanks in advance!

Best,
Sukruti
MakeRule with Conditions.nb

Thomas Bäckdahl

unread,
Jun 4, 2025, 3:09:40 PMJun 4
to xa...@googlegroups.com
Hi!

It looks like MakeRule leaves the condition as it stands, and adds it to all rules as a last step.
MakeRule makes many versions of your left hand side with index rearrangements etc. However, these rearrangements are not done to your extra conditions.

Your example contains too may rules to easily see what is going on, but you can for instance replace your condition with Print[\[Zeta],\[Eta],\[Alpha],\[Delta]].
This will show you what these indices are identified as in each case of the rule that matches the index contractions.

I am not sure how to solve your problem in the best way though. But at least this gives an explanation.

José: Can you answer how conditions are supposed to be manipulated in MakeRule?

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/28481179-e304-4551-ae04-4be00a75e2bfn%40googlegroups.com.

Sukruti

unread,
Jun 4, 2025, 3:58:02 PMJun 4
to xAct Tensor Computer Algebra

Juan Margalef

unread,
Jun 4, 2025, 4:36:43 PMJun 4
to xAct Tensor Computer Algebra
Hi!

The problem is that when you write PairQ[a,b] you should write PairQ[a,-b]. Notice how PairQ[a,a] returns False while PairQ[a,-a] returns True. Here you have a minimal working example:

In[2]:= DefManifold[M,
  4, {\[Alpha], \[Beta], \[Gamma], \[Delta], \[Epsilon], \[Zeta], \
\[Eta], \[Theta], \[Iota], \[Kappa], \[Lambda], \[Mu], \[Nu], \[Xi], \
\[Sigma], \[Rho], \[Tau]}];
DefTensor[T[\[Alpha], -\[Beta]], M]

In[4]:= rule1 =
 MakeRule[{T[\[Alpha], -\[Beta]], 0, \[Alpha] =!= \[Beta]}] (* This is a less elegant way *)
rule2 = MakeRule[{T[\[Alpha], -\[Beta]],
   0, ! PairQ[\[Alpha], -\[Beta]]}]

In[6]:= {T[\[Beta], -\[Beta]], T[\[Alpha], -\[Beta]],
  T[\[Gamma], -\[Gamma]], T[\[Gamma], -\[Delta]]} /. rule1
{T[\[Beta], -\[Beta]], T[\[Alpha], -\[Beta]], T[\[Gamma], -\[Gamma]],
  T[\[Gamma], -\[Delta]]} /. rule2

Out[6]= {T[\[Beta], -\[Beta]] , 0, T[\[Gamma], -\[Gamma]] , 0}
Out[7]= {T[\[Beta], -\[Beta]] , 0, T[\[Gamma], -\[Gamma]] , 0}

I hope this helps!

Juan Margalef

Juan Margalef

unread,
Jun 4, 2025, 5:08:44 PMJun 4
to xAct Tensor Computer Algebra
PS. It might not be that simple. I tried that solution to your problem, and it only works if you remove MetricOn -> All. It seems that all indices are put up so the rules of my minimal example must be changed:

rule3 = MakeRule[{T[\[Alpha], -\[Beta]], 0, \[Alpha] =!= -\[Beta]},
  MetricOn -> All]
rule4 = MakeRule[{T[\[Alpha], -\[Beta]], 0, ! PairQ[\[Alpha], \[Beta]]},
   MetricOn -> All]

In any case, the problem might be related to what Thomas was alluding to, but it's hard to see with such a complicated example. I would recommend that you try some examples with less symmetry and see if you are able to find a simpler example with this problem appears.

Sukruti

unread,
Jun 16, 2025, 8:12:49 AMJun 16
to xAct Tensor Computer Algebra
Hi,

I understood what was going wrong in the definition of Iden1 in the file I shared in the first email in this thread.

The conditions I had given were getting implemented but they were not sufficient because MetricOn was set to All instead of None. If MetricOn had been set to None, then the rule would have worked as needed (cf. Iden3 in the attached notebook).

In order to define Iden1 correctly while setting MetricOn to All, a few additional conditions need to be added, viz, ! PairQ[\[Alpha], \[Zeta]] && ! PairQ[\[Alpha], \[Eta]] && ! PairQ[\[Zeta], \[Eta]], as shown in the attached file (cf. Iden4 in the attached notebook).

Although the question I asked in my first email is resolved now, it is still a mystery to me how merely adding a condition to print the indices (cf. Iden2 in the attached notebook), changes the way it acts on a given term.

Best,
Sukruti
MakeRule with Conditions 2.nb

Thomas Bäckdahl

unread,
Jun 16, 2025, 9:33:10 AMJun 16
to xa...@googlegroups.com

Hi!

The reason that Iden2 is not doing anything apart from printing is that the Print command returns Null, not True.
And[True, Null] returns Null, so the rule is never applied.
If you really want the Print command, you might want to write something like (Print[\[Zeta], \[Eta], \[Alpha], \[Delta]] == Null) instead of just the Print command.

The main problem though is that the indices in the left and right hand sides of the rule is rearranged for all the 736 cases in Iden1. However, the condition is added without any rearrangement. Hence, the rule is not doing what you intended it to do. By accident your extra conditions might stop the rule being applied in the cases you wanted to avoid, but it is not really checking what you intended.
To actually remove some cases from MakeRule correctly is very difficult. You might have to copy and rewrite the entire MakeRule function. Unfortunately this is easier said than done because it is a really complicated function.

In your case I would recommend to not use MakeRule with conditions. 
One possibility is to try MakeRule just with MetricOn->None and no extra conditions, possibly using several starting versions of the equation. I am not sure exactly what you wanted to avoid, so this might or might not work for you.
Another possibility might be to manually figure out a minimal set of replacement rules with conditions. These can then be written without MakeRule.
Yet another possibility could be to use MakeCompareRule (in SymManipulator) or a modified version of it.
The difference between MakeRule and MakeCompare rule is the following.
MakeRule makes a list of all possible rearrangements that might be needed to recognize the left hand side.
MakeCompareRule just makes one rule with a general pattern ignoring the index positions. When the rule is applied, it checks if the terms that matches the general pattern can be rewritten as the left hand side using a special version of EqualExpressionsQ. In that case the index replacement rule that was needed to rewrite the left hand side to the found pattern is appled to the right hand side and that is returned instead.
MakeCompareRule or the more convenient EqToCompareRule does not find contractions of the left hand side, so you need to make rules for all contracted versions you are interested in. It looked like you wanted to avoid some contractions, so that might be a good thing for you.

Just a warning. Try not to display the rule generated by MakeCompareRule or EqToCompareRule, at least if you are using $PrePrint = ScreenDollarIndices;. ScreenDollarIndices gets confused and throws errors. The rule should still work. If you want to look at it you can use InputForm on it. Due to xAct`SymManipulator`Private` on all variable names, it might be difficult to read though.

Regards
Thomas

Sukruti

unread,
Jun 16, 2025, 10:39:33 AMJun 16
to xAct Tensor Computer Algebra
Hi Thomas,

Thank you for your detailed explanation.

When I use the condition ! Pair[\[Alpha], \[Beta]], I am trying to tell MakeRule to act only if \[Alpha] and \[Beta] are different from each other so that they don't get contracted with each other.

Is this the correct way to make MakeRule follow that condition or would something else be better?

In the attached notebook, I show that I run into a problem when trying to enforce that condition by using ! Pair[\[Alpha], \[Beta]]. The rule simply doesn't work on including this!

Any idea what could be going wrong here and how to enforce the required condition?

Thanks!

Best,
Sukruti
MakeRule with a Condition.nb

Thomas Bäckdahl

unread,
Jun 16, 2025, 11:36:15 AMJun 16
to xa...@googlegroups.com

Hi!

First of all it is PairQ, not Pair. 
Secondly, as I said, you should not use conditions in MakeRule like this because it is not going to do the right thing. \[Alpha] and \[Beta] are not going to be in the indicated positions in all 512 rules that MakeRule is producing.

With MakeRule you can not in a good way tell if indices are allowed to be contracted or not. MetricOn->All or MetricOn->None only tells if indices are allowed to be raised or lowered. It does not check if indices are contracted or not. Due to the problems I described in my previous answers, this really can not be done with MakeRule unless you rewrite the code for it considerably.

What I would suggest instead is to define your substitution as an equation.

RelationEq1 = 
  RiemannCD[\[Alpha], \[Gamma], \[Delta], \[Epsilon]]*
    RiemannCD[\[Beta], -\[Gamma], -\[Delta], -\[Epsilon]] == 
   2*RicciCD[\[Alpha], \[Gamma]]*RicciCD[\[Beta], -\[Gamma]] - 
    g[\[Alpha], \[Beta]]*RicciCD[-\[Gamma], -\[Delta]]*
     RicciCD[\[Gamma], \[Delta]] - 
    RicciCD[\[Alpha], \[Beta]]*
     RicciScalarCD[] + (g[\[Alpha], \[Beta]]*RicciScalarCD[]^2)/4 + 
    2*RicciCD[\[Gamma], \[Delta]]*
     RiemannCD[\[Alpha], -\[Gamma], \[Beta], -\[Delta]] + \
(g[\[Alpha], \[Beta]]*
       RiemannCD[-\[Gamma], -\[Delta], -\[Epsilon], -\[Zeta]]*
       RiemannCD[\[Gamma], \[Delta], \[Epsilon], \[Zeta]])/4;

Then you can make a replacement rule from the equation with EqToCompareRule.

ReplacementRule1 = EqToCompareRule@RelationEq1;

This rule can now be applied to your expressions. This will allow indices to be moved up or down, rearranged or renamed, but not contracted. 

In this case there is only one possible contraction, and that is the one you wanted to avoid. Hence, this is fine.

However, there might be more complicated cases where you want to allow some contractions. In that case you have to make an equation for each contracted case and apply EqToCompareRule to each case and collected in a list. If it is difficult to figure out which contractions are possible, the xTras function AllContractions can help. Unfortunately, that function does not work for expressions with dummy indices. Therefore, you would need define a new tensor with the same number of free indices and symmetry group as your left hand side. AllContractions on this new tensor will then give you a list of all possible contractions. From that it is not too difficult to construct a list of contracted relations. But in most cases it is easy to manually figure out which contracted relations one might want to use, so AllContractions are usually not needed.

Regards
Thomas

Reply all
Reply to author
Forward
0 new messages