MakeRule for Arbitrary Derivatives of Expression

166 views
Skip to first unread message

Benedict

unread,
Dec 11, 2020, 6:38:42 AM12/11/20
to xAct Tensor Computer Algebra
Can I create rules that contain arbitrary derivatives of an expression? For example, when x+y=0, then also the time derivative d/dt(x+y)=0. It works for abstract expressions, but when working with the xTras package, it does not work for combinations of metric components (called C11, C12, C13, C22, C23, C33 in the following). I have to set several rules, such as

cTFRule = MakeRule[{-C11[t[], x[], y[], z[]], 
    C22[t[], x[], y[], z[]] + C33[t[], x[], y[], z[]]}];
c0TFRule = MakeRule[{-Derivative[1, 0, 0, 0][C11][t, x, y, z], 
    Derivative[1, 0, 0, 0][C22][t, x, y, z] + Derivative[1, 0, 0, 0][C33][t, x, y, z]}];
c00TFRule = MakeRule[{-Derivative[2, 0, 0, 0][C11][t, x, y, z], 
    Derivative[2, 0, 0, 0][C22][t, x, y, z] + Derivative[2, 0, 0, 0][C33][t, x, y, z]}];

Can I somehow circumvent this problem? Maybe something like

c0TFRule = MakeRule[{-Derivative[_a, _b, _c, _d][C11][t, x, y, z], 
    Derivative[a, b, c, d][C22][t, x, y, z] + Derivative[a, b, c, d][C33][t, x, y, z]}];

such that when ever any derivative of the expression C11+C22+C33=0 is taken, it is set to zero?

Please, find attached a MWE that shows the behaviour.

MakeRulesMWE.nb

Bruno Le Floch

unread,
Dec 11, 2020, 11:32:45 AM12/11/20
to xa...@googlegroups.com
Hello Benedict and all,

Here is a partial answer for the particular case of scalar fields.

On 12/11/20 11:57 AM, Benedict wrote:
> Can I create rules that contain arbitrary derivatives of an expression? For
> example, when x+y=0, then also the time derivative d/dt(x+y)=0. It works for
> abstract expressions, but when working with the xTras package, it does not work
> for combinations of metric components (called C11, C12, C13, C22, C23, C33 in
> the following). I have to set several rules, such as
>
> cTFRule = MakeRule[{-C11[t[], x[], y[], z[]], 
>     C22[t[], x[], y[], z[]] + C33[t[], x[], y[], z[]]}];

For a scalar field, MakeRule is not super useful if I understand correctly.
Here you can accomodate arbitrary derivatives (and more) by the rules

{
C11[vars___] :> - C22[vars] - C33[vars] ,
der_[C11][vars___] :> - der[C22][vars] - der[C33][vars]
}

where the first rule deals with the case without derivatives and the second rule
deals with derivatives ("der" will match "Derivative[a,b,c,d]" for any a,b,c,d,
without having to give names to them). If you want a more restrictive rule that
only matches derivatives, replace the second rule above by

Derivative[n___][C11][vars___]
:> - Derivative[n][C22][vars] - Derivative[n][C33][vars]

where now "n" is the Sequence of derivative orders, just like vars matches the
sequence of arguments t[],x[],y[],z[].

Bruno

Benedict

unread,
Dec 11, 2020, 2:05:07 PM12/11/20
to xAct Tensor Computer Algebra
Hello Bruno,

Thank you very much for your answer!

This way of writing it seems to have another advantage: It is easier to make a collection of rules than with the MakeRule[{}] command, as you can directly put all rules into one "tag". :)

Can I somehow use the more restrictive rule (Derivative[n___][C11][vars___]:> - Derivative[n][C22][vars] - Derivative[n][C33][vars])) to make a tensor divergence-free, i.e. replacing

c1DFRule =  MakeRule[{-Derivative[0, 1, 0, 0][C11][t[], x[], y[], z[]], 
   Derivative[0, 0, 1, 0][C12][t[], x[], y[], z[]] +     Derivative[0, 0, 0, 1][C13][t[], x[], y[], z[]]}]; 
c2DFRule =  MakeRule[{-Derivative[0, 1, 0, 0][C12][t[], x[], y[], z[]], 
   Derivative[0, 0, 1, 0][C22][t[], x[], y[], z[]] +     Derivative[0, 0, 0, 1][C23][t[], x[], y[], z[]]}];
c3DFRule =   MakeRule[{-Derivative[0, 1, 0, 0][C13][t[], x[], y[], z[]], 
    Derivative[0, 0, 1, 0][C23][t[], x[], y[], z[]] + 
     Derivative[0, 0, 0, 1][C33][t[], x[], y[], z[]]}];

by one rule? Or is there another way of accomplishing this, such that it also works when other derivatives are taken on top of such a combination of terms?

Thanks again!

Benedict

Bruno Le Floch

unread,
Dec 11, 2020, 3:27:46 PM12/11/20
to xa...@googlegroups.com
On 12/11/20 7:39 PM, Benedict wrote:
> divergence-free, i.e. replacing
>
> c1DFRule =  MakeRule[{-Derivative[0, 1, 0, 0][C11][t[], x[], y[], z[]], 
>    Derivative[0, 0, 1, 0][C12][t[], x[], y[], z[]] + Derivative[0, 0, 0,
> 1][C13][t[], x[], y[], z[]]}]; 
>
> by one rule? Or is there another way of accomplishing this, such that it also
> works when other derivatives are taken on top of such a combination of terms?


c1DFRule = Derivative[n0_, n1_?Positive, n2_, n3_][C11][vars___] :> (
- Derivative[n0, n1-1, n2+1, n3][C12][vars]
- Derivative[n0, n1-1, n2, n3+1][C13][vars])

should do the trick, I think (of course you need the same for C21,C22,C23 and
for C31,C32,C33; it can also be generalized to any dimension, but that gets
super ugly).

Probably for your use case the above is enough and is the right approach. More
generally it might be nice to have a general tool for this. Something like the
following (not tested much)

(**************)
HoldPattern[MyMakeRules[Derivative[k___][f_][vars___], replacement_]] :=
{
Derivative[k][f][vars] :> replacement,
Derivative[n___][f][vars] :>
With[{l = {n} - {k}},
D @@ Join[{replacement}, Transpose[{{vars}, l}]] /;
AllTrue[l, # >= 0 &]]
};
MyMakeRules[f_[vars___], replacement_] :=
{
f[vars] :> replacement,
Derivative[n___][f][vars] :>
D @@ Join[{replacement}, Transpose[{{vars}, {n}}]]
};
(**************)
(* Then get the various rules for traceless divergence-free:*)
(**************)
txyz=Sequence[t[],x[],y[],z[]];
cTFRules = MyMakeRules[C11[txyz], -C22[txyz] - C33[txyz]]
c1DFRules = MyMakeRules[D[C11[txyz],x[]], -D[C12[txyz],y[]]-D[C13[txyz],z[]]]
c2DFRules = MyMakeRules[D[C21[txyz],x[]], -D[C22[txyz],y[]]-D[C23[txyz],z[]]]
c3DFRules = MyMakeRules[D[C31[txyz],x[]], -D[C32[txyz],y[]]-D[C33[txyz],z[]]]
(**************)

Bruno

Jose

unread,
Dec 13, 2020, 2:03:02 PM12/13/20
to xAct Tensor Computer Algebra
Hi,

Let me point out the last of the questions here:


where I comment on the limitations of MakeRule. In general, I'd suggest to try to use the full power of the Wolfram Language pattern matcher, which goes much beyond what MakeRule can do.

For example, a possible way to handle simultaneously functions and derivatives is using something like:

    rule = C11 -> Function[{t, x, y, z}, -C22[t, x, y, z] - C33[t, x, y, z]]

Note that there are no brackets in the variables of Function. They have nothing to do with the xTensor scalar fields and could be called a, b, c, d instead.

Then both of these work:

    C11[t[], x[], y[], z[]] /. rule
    Derivative[0, 1, 0, 0][C11][t[], x[], y[], z[]] /. rule

Cheers,
Jose.

Benedict

unread,
Dec 16, 2020, 8:17:18 AM12/16/20
to xAct Tensor Computer Algebra
Thank you very much for this answer. This simplifies the notation quite a bit. :)

However, I run into an issue when I try to replace a function with a term that contains derivatives:

testrule = 
 c23 -> Function[{t, x, y, z}, 
   Derivative[0, 0, 1, 1][\[Gamma]][t, x, y, z]].

Then, 

((Derivative[1, 0, 0, 0][c23[t[], x[], y[], z[]]] /. testrule) - 
   Derivative[1, 0, 1, 1][\[Gamma][t[], x[], y[], 
     z[]]]) // FullSimplify

does not return 0 but 

Derivative[1, 0, 0, 0][Derivative[0, 0, 1, 1][\[Gamma]][t[], x[], y[], z[]]] - Derivative[1, 0, 1, 1][\[Gamma][t[], x[], y[], z[]]].

What is the proper way of writing this?

Benedict

unread,
Dec 16, 2020, 8:36:03 AM12/16/20
to xAct Tensor Computer Algebra
The proper way of writing it is as follows:

testrule = {der_[c23][vars___] :> 
    der[Derivative[0, 0, 1, 1][\[Gamma]]][vars]};

There was a bracket missing.

Bruno Le Floch

unread,
Dec 16, 2020, 3:21:59 PM12/16/20
to Benedict, xAct Tensor Computer Algebra
Jose's approach of using a rule that gives a Function is the right one and works
perfectly fine in your case. I retract my earlier suggestion.

(*This rule you wrote in a previous email is correct.*)
testrule =
c23 -> Function[{t, x, y, z},
Derivative[0, 0, 1, 1][\[Gamma]][t, x, y, z]];

(*But you had not used Derivative correctly in your test, which should be as
follows.*)
((Derivative[1, 0, 0, 0][c23][t[], x[], y[], z[]] /. testrule) -
Derivative[1, 0, 1, 1][\[Gamma]][t[], x[], y[],
z[]]) // FullSimplify

Best regards,
Bruno
Reply all
Reply to author
Forward
0 new messages