# Introduce formal symbols indexed by natural numbers

I'm going to describe a toy example here which I'm hoping with some help, I can then soup up into the more complex application I have in mind.

Suppose the input is a list of lists, and for now take the example:

list = {{1,1,3,7}, {2}, {1,2,2,2,3,8}}

Quite simply, I want to sum over list and produce a certain polynomial in formal symbols $$P_1, P_2, ...,P_N$$ for some $$N$$ I choose. My main conceptual obstacle is how to implement these symbols in Mathematica!

There is a simple rule for the polynomial--for the example list above, the polynomial should be:

$$P_{1}^{2} \cdot P_{2} \,+ \,P_{1} \,+\, P_{1}^{3} \cdot P_{3}$$

because in the first entry of list there are two elements repeated once and one element repeated twice. Similarly, in the third entry, there are three elements repeated once, and one element repeated three times, which explains the last term. So the subscripts of the $$P$$'s should encode number of times an element repeats.

I have a schematic idea of how this should go, but can't really get started because I don't know how to deal with these formal symbols! I want to first sum over list. For a given entry, say list[[i]], I want to take a product over m with range {m,1, Max[list[[i]]]}. And for each iteration of this product, I want to produce one of the $$P$$'s where the subscript is Count[list[[i]],m].

I hope this makes sense, and I'd appreciate any help!

• Does Total[Times @@@ (Subsuperscript[p, ##] & @@@ Normal@GroupBy[Tally@#, Last, Length] & /@ list)] works? (you can work with If to remove power 1) May 23 at 4:19
• That does seem to work, thanks a lot! I had no idea about Subsuperscript but hopefully with that I can soup this up to what I really need. If you cared to make this an answer, I'd accept it. May 23 at 4:53
• I agree with BobHanlon here, it is not good advice to use Subsuperscript on symbolic calculations. See this Q&A for a longer explanation of alternatives and the reasoning behind them. Probably @BenIzd may want to reassess their advise? May 23 at 11:34
• I 100% agree with what Bob Hanlon said and Leonid Shifrin answered in the Q&A, the code I added was only about showing the formation without considering its limitations. Sadly editing comments is limited to 5 minutes. If it confuses future readers, I'll happily delete the comment. May 23 at 13:10

It is often useful to avoid Subscript, Superscript, and Subsuperscript and instead to just use indexed variables formatted to display in whatever desired fashion. With a slight modification to Ben Izd's method

Format[p[m_, n_]] := Subscript[p, m]^n

list = {{1, 1, 3, 7}, {2}, {1, 2, 2, 2, 3, 8}};

Total[Times @@@ (p @@@ Normal@GroupBy[Tally@#, Last, Length] & /@ list)] EDIT: With functions

Total[Times @@@ (p[##][x] & @@@
Normal@GroupBy[Tally@#, Last, Length] & /@ list)] p[m_, n_][x_] := f[m, n, x]

• This is also great, thanks! I'm curious, is it easy to define a function with these $P$'s as variables? Is it as simple as, say, TestFunc[p[1,1]_]:=... provided I have already defined them in your version? May 23 at 7:05
• What I meant was more that I wanted to define a function $F(N, p_{1}, \ldots, p_{N})$. As opposed to having the $p$'s be functions of some variable. Was just curious if you could use p[n,1] as a variable in defining a Mathematica function. But anyways, I'm sure I can get to the bottom of this. Thanks! May 23 at 17:46
• Unless the definition of F restricts the form of its arguments, it shouldn't care what argument values are used. n=5; F[n, ##]&@@(p[#,1]&/@Range[n]) May 23 at 17:56