Reordering indices in alphabetical order

30 views
Skip to first unread message

Sukruti

unread,
Sep 29, 2025, 9:23:09 AM (4 days ago) Sep 29
to xAct Tensor Computer Algebra
Hello,

Does anyone know how to make Mathematica reorder the indices of a given tensorial expression with free indices, in ascending alphabetical order?

Attached notebook shows the question more explicitly.

Best,
Sukruti
How to reorder indices.nb

Thomas Bäckdahl

unread,
Sep 29, 2025, 12:15:24 PM (4 days ago) Sep 29
to xa...@googlegroups.com

Hi!

This was a bit more complicated than I thought because FindFreeIndices sorts the indices, so it can not be used to extract the index ordering. However, FindIndices keeps the order of the indices.

The following two functions might do what you need. SortFreeInds can be dangerous, in particular if you have indices belonging to different VBundles. In that case you have to separate the indices into groups belonging to the same VBundle and only sort within each group. I have also ignored the possibility of blocked indices.

FreeIndsDown[expr_] := 
 ReplaceIndex[
  Evaluate@expr, (# -> DownIndex[#]) & /@ 
   List @@ FindFreeIndices[Evaluate@expr]]

SortFreeInds[expr_] := 
 ReplaceIndex[Evaluate@expr, 
  Thread[(# -> Sort@#) &@
      DeleteCases[List @@ #, 
       Alternatives @@ xAct`xTensor`Private`TakeEPairs[#]] &@
    FindIndices[Evaluate@expr]]]

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/1f764d21-79e9-4310-8b34-6664f6a72ed2n%40googlegroups.com.

Sukruti

unread,
6:18 AM (7 hours ago) 6:18 AM
to xAct Tensor Computer Algebra

Thomas Bäckdahl

unread,
7:23 AM (6 hours ago) 7:23 AM
to xa...@googlegroups.com

Hi!

The first function is not supposed to sort the indices, just move them down.
The idea was that if you want to identify expressions regardless of ordering of indices and index positions, you use both functions, one after the other. If you want to keep the index positions, but sort the free indices, you just use the second function.

Regards
Thomas

Sukruti

unread,
10:30 AM (3 hours ago) 10:30 AM
to xAct Tensor Computer Algebra
Hi,

Oh I see, FreeIndsDown[] is not supposed to sort the indices. In that case, both functions are indeed working as intended.

It's true my aim is to identify expressions regardless of the ordering and positions of indices, and for this purpose the two functions you defined are useful when applied one after the other.

Since FindFreeIndices[] sorts the free indices, I initially thought it might also be usable for sorting the indices of a given expression, similar to what SortFreeInds[] does. But I haven’t yet managed to define a sorting function based on FindFreeIndices[]. It’s not necessary right now since I’m working with only one VBundle, for which SortFreeInds[] suffices. Still, it’s an interesting observation that although FindFreeIndices[] returns indices in sorted order, it’s not obvious how to leverage that for defining the desired sorting function.

Best,
Sukruti
Reply all
Reply to author
Forward
0 new messages