1

I am looking for a way to get {5,2} for a Cycles object, like: Cycles[{{1,2,3,4,5},{6,7}}]. This could help with conjugacy classes.

The function should give back the length of the cycles the permutation consists of, in the natural ordering of the cycles as defined by Mathematica (e.g. Cycles[{{6,7},{1,2,3,4,5}}] would still have a "shape" of {5,2}).

Balint Pato
  • 125
  • 5
  • 1
    Are you looking for something like Length /@ First @ Cycles[{{1, 2, 3, 4, 5}, {6, 7}}]? (It could be useful to apply a Sort to the result as well, I'm not sure) As a function definition: cycleShape[Cycles[x_List]] := Sort[Length /@ x] – thorimur Oct 04 '21 at 16:55
  • It is not clear what you are asking. Please be explicit about what you want to achieve. Don't just give an example and let people guess. – Szabolcs Oct 04 '21 at 16:57
  • @Szabolcs, I added a clarification. – Balint Pato Oct 04 '21 at 17:16
  • @thorimur, yes, that's exactly I was looking for. Thank you for the help, if you add a response, I'll accept it as an answer. – Balint Pato Oct 04 '21 at 17:16
  • That is not what numpy's shape does ... The equivalent of np.shape is Dimensions. – Szabolcs Oct 04 '21 at 18:37
  • You are completely right @Szabolcs, thank you. Removed the confusing reference. – Balint Pato Oct 04 '21 at 20:19

2 Answers2

2

If you're looking for something like Length /@ First @ Cycles[{{1, 2, 3, 4, 5}, {6, 7}}], we can easily package that in a function:

cycleShape[Cycles[x_List]] := Length /@ x

The question then is how you want the "shape" to be ordered; for example, should Cycles[{{1, 2, 3, 4, 5}, {6, 7}}] have shape {5, 2} or {2, 5}? If we want to put the shape into a canonical form based only on the list of cycle lengths we can just use Sort:

cycleShape[Cycles[x_List]] := Sort[Length /@ x]

But if we want it to reflect the canonical ordering of the cycles, we can simply use the original definition. This is because Cycles automatically sorts its argument (try evaluating Cycles[{{6, 7}, {1, 2, 3, 4, 5}}] by itself). However, using the first definition (without Sort applied afterwards) would mean that, for example, Cycles[{{1, 2}, {3, 4, 5}}] and Cycles[{{1, 2, 5}, {3, 4}}] (both of which are in canonical Mathematica form) would have different shapes, despite being conjugate permutations (by the permutation Cycles[{{1, 3}, {2, 4}}]).

thorimur
  • 9,010
  • 18
  • 32
1
shape = Apply[Map @ Length];

shape @ Cycles[{{1, 2, 3, 4, 5}, {6, 7}}]
{5, 2}
shape @ Cycles[{{6, 7}, {1, 2, 3, 4, 5}}]
{5, 2}
kglr
  • 394,356
  • 18
  • 477
  • 896