Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+1 vote

A newbie question, I guess. I am challenged to zoom in on specific terms and apply a rule to them.

I want to say "zoom in on the 4th and 8th term in a 9 term statement and apply a substitution rule", specifically when all the terms are complex expressions with multiple indexes and partial derivatives. How do I do that?

In more detail, I have a complex expression with 9 terms and each term has a lot of indexes, but for simplicity lets just call these 9 terms

A + B + C + D + E + F + G + H + I

What I want to do, for example, is zoom in on terms D and H and apply a subsitution rule which is itself involving a lot of indexes and a partial derivative.

My problem is that all these terms involve partial derivatives as well as indexes and zoom does not seem to capture these well.

Here is example code that provides perhaps some context:

from cdb.relativity.abstract import *
from cdb.core.manip import *


ch = christoffel_from_metric();
ch_2 := \Gamma^{\rho}_{\nu\sigma}\Gamma^{\sigma}_{\rho\mu};

Now how do I zoom in on the 4th and 8th terms of this last expression? It can be through the zoom() function, but I am also happy with take_match() or any of the manip functions. I wasn't able to solve it with them.

Many thanks GPN

in General questions by (1.2k points)
edited by


cadabra has an algorithm that allows you to restrict the manipulations of a expression to a specific portion (I guess that you know that because you're using the terminology zoom to explaing yourself).

What you want might be possible, but it would be beneficious if you share at least a couple of terms of the REAL expression (or a more realistic scenario) to work with.

Regards, Dox.

Thank you Dox.

I edited and added some example code to the original question. In this context, how would I zoom in on the 4th and 8th terms?

Thanks GPN

Just a quick comment: in general cadabra discourages selecting of particular terms in an expression purely by the position at which they appear in the sum (like "the 4th term"). Similarly, cadabra discourages referencing explicit index names (like "select the term where the index contraction has a \mu index"). The reason is simply that these are brittle constructs: there is nothing special about a term being the 4th in a sum, or a dummy index being called \mu.

So that is why zoom requires that you write a pattern which should match the terms you want to zoom in on.

Having said that, there is something to be said for an extension of the zoom command which can select particular terms based on their position, as it seems to be something that may make quick computations easier. I'll think about it.

Thank you, that is much appreciated. GPN

2 Answers

+1 vote
Best answer


You posed a very interesting question. I got a bit further in the direction you wanted. Let me explain.

Following your example, I stated with these definitions (properties):

from cdb.relativity.abstract import christoffel_from_metric


ch = christoffel_from_metric()

Now, the expression to manipulate is the following:

ch_2 := \Gamma^{\rho}_{\nu\sigma}\Gamma^{\sigma}_{\rho\mu};

Let's say that you want to manipulate the fourth term. My approach is to define a new expression using the target term

old = ch_2[3]
old := 4 @(old);


  • In the last code block the first definition of the variable old is a python command (note the assignation through the equal sign (without colon), so it don't require to end in semi-colon.
  • The re-definition of old is a cadabra expression (uses the :=) whose purpose is to eliminate the numerical factor, which raises an error in the next step.

Next, I define a new expression which might be a copy of old, i.e. new := @(old) to be manipulated through cadabra algorithms, or just a hand-made expression. The goal is to define a substitution rule.

In the code block below I use the second scenario:

new := A_{\mu \nu};
rl := @(old) -> @(new);

Finally, I apply the substitution rule to the original expression ch_2:

substitute(ch_2, rl);

Hope this could be useful for your case! Dox.

by (12.5k points)
selected by

Thank you!

I learn from this many things, including the fact that

old = ch_2[3]

will actually capture the 4th term. This is exactly what I wanted!

Appreciated. Many thanks. GPN

0 votes

I worked on this and here may be an answer, but its a completely different direction from what I requested.

I used the example given here and multiplied my complicated expression by a Kronecker delta:

ex2 := @(ex) δ^{\nu}_{\lambda}.

I also multipled the subsitution rule, using


Having done that,


worked. I hope someone else can benefit.

I would still appreciate knowing: is there some way to call out a specific desired term, say terms 4 and 8 as in my example, and zoom in on them?

Many thanks GPN

by (1.2k points)
edited by