7
$\begingroup$

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 ",".

$\endgroup$
2
  • $\begingroup$ Removed my comment, but a polished version now as an answer. :) $\endgroup$ – kirma May 30 '20 at 19:25
  • $\begingroup$ @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. $\endgroup$ – ZeroTheHero May 30 '20 at 19:33
7
$\begingroup$
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.

$\endgroup$
7
$\begingroup$

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} *)
$\endgroup$
3
  • 1
    $\begingroup$ 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. $\endgroup$ – ZeroTheHero May 31 '20 at 14:31
  • $\begingroup$ @ZeroTheHero You are right, I misinterpreted the question. Thanks for catching that. $\endgroup$ – Rohit Namjoshi May 31 '20 at 17:00
  • $\begingroup$ no worries. Thanks for fixing. Also a nice solution. $\endgroup$ – ZeroTheHero May 31 '20 at 18:24
6
$\begingroup$

This may be useful:

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

1 22 503

$\endgroup$
2
  • $\begingroup$ Interesting way to handle exponents, but I wonder if ordering of these items is important... $\endgroup$ – kirma May 30 '20 at 20:00
  • 2
    $\begingroup$ @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. $\endgroup$ – ZeroTheHero May 30 '20 at 20:16
5
$\begingroup$
Defer @* Power @@@ Tally[{2, 2, 1, 1, 3}]
{2^2, 1^2, 3^1}
$\endgroup$
1
$\begingroup$

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 $$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.