# Rewriting partitions using exponents

I'm looking for a way to re-express a partition given in full form, like $$\{{2, 2, 1, 1}\}$$, into the shortened form $$\{2^2, 1^2\}$$, i.e. given a partition with repeated entries, count the number of repetitions of a given entry, and convert this (without evaluating the $$a^b$$) to exponential form.

I'm aware that "Tally" will produce the correct count:

Tally[{2,2,1,1}]


correctly returns $$\{\{2,2\},\{1,2\}\}$$ but converting this to $$2^2 1^2$$ is the part that gives me trouble.

A final refinement would be that, when $$1$$ occurs as an exponent, it is NOT displayed, i.e. $$\{4,1,1\}$$ is displayed in shortened form as $$\{4, 1^2\}$$. The separation by "," is optional but if the entries are in the double digits it makes a little more sense to have the ",".

• Removed my comment, but a polished version now as an answer. :) May 30, 2020 at 19:25
• @kirma yes the addition of the thin space is a nice touch as it will obviously separate entries even is they contain more than 1 digit. May 30, 2020 at 19:33

DisplayForm@RowBox@
Riffle[If[#2 == 1, #1, #1^#2] & @@@
Tally[HoldForm /@ {2, 2, 1, 50, 50, 50}],
"\[ThinSpace]"]


$$2^2\, 1\, 50^3$$

You can also apply TraditionalForm and TeXForm gracefully on this (but only after DisplayForm.

Update

Fix issue pointed out by @ZeroTheHero

Tally[{2, 2, 1, 1}] /. {x_Integer, y_Integer} :> If[y == 1, y, Defer[x^y]]
(* {2^2, 1^2} *)


Tally[{2, 2, 1, 1}] /. {x_Integer, y_Integer} :> If[x == 1, x, Defer[x^y]]

(* {2^2, 1} *)

• This doesn’t quite work as the output should be {2^2,1^2}. I suspect your condition on x should be on the exponent y instead. May 31, 2020 at 14:31
• @ZeroTheHero You are right, I misinterpreted the question. Thanks for catching that. May 31, 2020 at 17:00
• no worries. Thanks for fixing. Also a nice solution. May 31, 2020 at 18:24

This may be useful:

Times @@ HoldForm /@ {2, 2, 1, 50, 50, 50}


1 22 503

• Interesting way to handle exponents, but I wonder if ordering of these items is important... May 30, 2020 at 20:00
• @kirma In a partition the entry at position $i$ is necessarily greater or equal to the following entry at position $i+1$, so there's an implicit (partial) ordering of entries. May 30, 2020 at 20:16
Defer @* Power @@@ Tally[{2, 2, 1, 1, 3}]

{2^2, 1^2, 3^1}


Perhaps a very simple answer would be enough. I suggest

Times@@ToString /@ {2, 2, 1, 50, 50, 50}


which returns

$$1\, 2^2\, 50^3$$