Display only the non-zero components of a given tensor, with their corresponding indices

133 views
Skip to first unread message

José Ferreira

unread,
Nov 22, 2023, 12:28:21 PM11/22/23
to xAct Tensor Computer Algebra
Hello there,

I've been trying out different tools for symbolic calculus in GR (xAct, SageManifold, Diffgeo), and now I'm mostly investing my time on xAct.
One feature that I is available on Diffgeo, that was very convenient, was a function named display, which displayed only the non-zero components of a given tensor and their corresponding indices, without duplicates.
This means that a piece of code that writing something as simple as display[Christoffel], one would immediately obtain in the output cell of the notebook
https://i.imgur.com/9ehtB2H.png

I would therefore like to ask you whether there is something that achieves the same but in xAct (or xCoba for that matter).
I've dug into the code for Diffgeo, and it seems that the relevant portion for the display function is the following
https://i.imgur.com/n7tLfTB.png
However, I'm unable to adapt it to make it work with quantities provided in xAct.

Could this feature be achieved by some already pre-existing routine in xAct?
If not, would somebody be willing to help me port this code to work with quantities found in xAct?

Thanks,
Best,
José Ferreira

Jose

unread,
Nov 22, 2023, 11:04:37 PM11/22/23
to xAct Tensor Computer Algebra
Hi,

I attach a notebook with a function CTensorEntries that does something like this.

Cheers,
Jose.

CTensorEntries.nb

José Ferreira

unread,
Nov 23, 2023, 10:27:02 AM11/23/23
to xAct Tensor Computer Algebra
Wow, not only was that a very fast response but also a very instructive notebook!
Many thanks!

Best,
José Ferreira

José Ferreira

unread,
Nov 23, 2023, 4:23:57 PM11/23/23
to xAct Tensor Computer Algebra
I'll take the opportunity to say that I wanted something like the previous image, but with the tensor itself showing and the right positions.
This took a minimal adaptation from the code you have sent me, but I'll share it so that people can have something to easily copy and paste that works out of the box.

CTensorEntries[ctensor_CTensor, T_?xTensorQ] := Module[{assoc},
   ToTensorRules[T, ctensor];
   frules = Join[
     First[TensorValues[T]] /. Last[TensorValues[T]],
     Last[TensorValues[T]]
     ];
   assoc =
    GroupBy[
     DeleteCases[frules, _ -> 0],
     Last -> First
     ];
   Grid[
    Reverse /@ List @@@ Normal[Column /@ assoc],
    Frame -> {False, All},
    Spacings -> {2, 1}
    ]
   ];

CTensorEntries[ctensor : CTensor[array_, bases_, _],
  name_String : "T"] := Module[{vbundles, indices, sym, T, res},
  vbundles = VBundleOfBasis /@ bases;
  indices = NewIndexIn /@ vbundles;
  sym = TensorSymmetry[array] /. {cyc : System`Cycles[cycs_], sign_} :>
      Times[sign, Cycles @@ cycs];
  If[ListQ[sym], sym = GenSet @@ sym];
  Block[{$DefInfoQ = False},
   DefTensor[T @@ indices, vbundles, sym, PrintAs -> name]];
  res = CTensorEntries[ctensor, T]
  ]

You can use this by calling CTensorEntries[<tensor>, "<tensor symbol>"]

Best,
José Ferreira
Reply all
Reply to author
Forward
0 new messages