2
$\begingroup$

Suppose I have a polynomial like this: $$a=x_{j_1} + x_{j_1}x_{j_2} + x_{j_1}x_{j_2}x_{j_3} + ...+x_{j_1}x_{j_2}x_{j_3}...x_{j_n}$$

I want to create a function that takes this polynomial and does the following: $$f[a]=\sum_{j_1}^px_{j_1}+\sum_{j_1}^p\sum_{j_2}^{j_1}x_{j_1}x_{j_2}+\sum_{j_1}^p\sum_{j_2}^{j_1}\sum_{j_3}^{j_2}x_{j_1}x_{j_2}x_{j_3}+...+\sum_{j_1}^p\sum_{j_2}^{j_1}\sum_{j_3}^{j_2}...\sum_{j_n}^{j_{n-1}}x_{j_1}x_{j_2}x_{j_3}...x_{j_{n-1}}x_{j_n}$$ where $p$ is a fixed number.

In practice, my polynomials are not limited to those three variables. The $x$'s are labelled by a unique index $j_i$. That is what I mean by variable terms to sum over. I am not sure how to make the code 1. recognize that it has $n$ factors in a term (corresponding to $\sum_{j_1>j_2>...>j_n}x_{j_1}x_{j_2}...x_{j_n}$), and 2. sum over those terms.

I looked at a similar question here where it was for $n$ number of terms but I have not been able to successfully adapt that solution.

$\endgroup$
  • $\begingroup$ Do you have an attempt at a solving this? Do you have a reference to earlier solutions (and can you include those in a solution attempt)? Showing functional code that doesn't do what you want would be helpful for anyone looking to provide a solution. $\endgroup$ – Mark R Jun 13 at 23:43
  • $\begingroup$ Do you want $j_1>j_2>\ldots>j_n$ or $j_1\geq j_2\geq \ldots \geq j_n$? Your definition of f[a] doesn't seem to match either one of those restrictions. $\endgroup$ – JimB Jun 14 at 15:48
3
$\begingroup$

Update: A function that takes an expression of the specific form and an integer to construct the desired sums:

ClearAll[sumF]
sumF[poly_, p_] :=  Module[{var, indices, iteratorlist, 
   parts = {First@#, #2} & @@ DeleteDuplicates /@ 
      Transpose[Cases[poly /. Subscript[a_, Subscript[b_, c_]] :> 
          Subscript[a, Superscript[b, c]], Subscript[a_, b_] :> {a, b}, All]]},
  {var, indices} = parts;
  iteratorlist = Reverse[Insert[#, 1, 2]&/@ Partition[Reverse@Prepend[indices, p], 2, 1]];
  Total@Table[Sum[Times @@ (Subscript[var, #] & /@ index[[All, 1]]), 
    Evaluate[## & @@ index]], 
   {index, iteratorlist[[;; #]] & /@ Range[Length@indices]}] /. Superscript -> Subscript]

Examples:

poly1 = Subscript[x, i] + Subscript[x, i] Subscript[x, j] + 
   Subscript[x, i] Subscript[x, j] Subscript[x, k];
TeXForm @ poly1

$x_i x_j x_k+x_i x_j+x_i$

sumF[poly1, p] 

enter image description here

poly2 = Subscript[z, i] + Subscript[z, i] Subscript[z, j] + 
  Subscript[z, i] Subscript[z, j] Subscript[z, k] + 
  Subscript[z, i] Subscript[z, j] Subscript[z, k] Subscript[z, m]
TeXForm @ poly2

$z_i z_j z_k z_m+z_i z_j z_k+z_i z_j+z_i$

sumF[poly2, p] 

enter image description here

poly3 = Total[Times@@@(Take[Subscript[w, Subscript[j, #]] & /@ Range[7], #]& /@ Range[7])]
TeXForm[poly3]

$w_{j_2} w_{j_1}+w_{j_2} w_{j_3} w_{j_1}+w_{j_2} w_{j_3} w_{j_4} w_{j_1}+w_{j_2} w_{j_3} w_{j_4} w_{j_5} w_{j_1}+w_{j_2} w_{j_3} w_{j_4} w_{j_5} w_{j_6} w_{j_1}+w_{j_2} w_{j_3} w_{j_4} w_{j_5} w_{j_6} w_{j_7} w_{j_1}+w_{j_1}$

sumF[poly3, p]

enter image description here

Original answer:

ClearAll[sum]
sum[p_, a_: x] := Total@
    Table[Sum[Times @@ (Subscript[a, #] & /@ index[[All, 1]]), Evaluate[## & @@ index]], 
   {index, {{i, 1, p}, {j, 1, i}, {k, 1, j}}[[;; #]] & /@ {1, 2, 3}}]

sum[p]

enter image description here

 TeXForm[sum@p]

$\sum _{i=1}^p \sum _{j=1}^i \sum _{k=1}^j x_i x_j x_k+\sum _{i=1}^p \sum _{j=1}^i x_i x_j+\sum _{i=1}^p x_i$

sum[3] // TeXForm

$\small x_1^3+x_2 x_1^2+x_3 x_1^2+x_1^2+x_2^2 x_1+x_3^2 x_1+x_2 x_1+x_2 x_3 x_1+x_3 x_1+x_1+x_2^3+x_3^3+x_2^2+x_2 x_3^2+x_3^2+x_2+x_2^2 x_3+x_2 x_3+x_3$

$\endgroup$
  • $\begingroup$ Thank you for your answer. I think the way I posed my question was misleading. Please check the updated version. $\endgroup$ – Alonso Perez Lona Jun 14 at 9:19
  • 1
    $\begingroup$ @AlonsoPerezLona, please see the update. $\endgroup$ – kglr Jun 14 at 9:44
2
$\begingroup$

I don't see how the right-hand side of f[a_] can result from a (unless f separates each term in a rather than keeping the sum intact).

To obtain the right-hand side of f[a_] it seems that one only needs to know the maximum number of terms in the products. Here's a function that will do that:

f[n_] := Sum[SymmetricPolynomial[i, Table[Subscript[x, j], {j, n}]], {i, n}]

f[3]

$$x_2 x_1+x_2 x_3 x_1+x_3 x_1+x_1+x_2+x_2 x_3+x_3$$

For polynomial functions in the form you're using you should take a look at AugmentedSymmetricPolynomial, PowerSymmetricPolynomial, SymmetricReduction, and PolynomialReduce. Also, mathStatica (a Mathematica package for purchase) has conversion functions for converting functions such as yours to PowerSymmetricPolynomial's which makes calculations much, much efficient (and makes the calculation even possible when there are zillions of terms).

$\endgroup$
1
$\begingroup$

I did this:

myEquation = \!\(\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(p\)]\(Subscript[x, 
i]\)\) + \!\(\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(p\)]\(\*UnderoverscriptBox[\(\[Sum]\), \(j\), \(i\)]Subscript[x, 
  i] Subscript[x, j]\)\) + \!\(\*UnderoverscriptBox[\(\[Sum]\), \(i\), \(p\)]\(\*UnderoverscriptBox[\(\[Sum]\), \(j\), \(i\)]\(\*UnderoverscriptBox[\(\[Sum]\), \(k\), \(j\)]Subscript[x, i] Subscript[x, j] Subscript[x, k]\)\)\)

And then by saying

myEquation /. p -> 3

You get this:

Subscript[x, 1] + 
\!\(\*SubsuperscriptBox[\(x\), \(1\), \(2\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(1\), \(3\)]\) + Subscript[x, 2] + 
 Subscript[x, 1] Subscript[x, 2] + \!\(
\*SubsuperscriptBox[\(x\), \(1\), \(2\)]\ 
\*SubscriptBox[\(x\), \(2\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(2\), \(2\)]\) + Subscript[x, 1] 
\!\(\*SubsuperscriptBox[\(x\), \(2\), \(2\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(2\), \(3\)]\) + Subscript[x, 3] + 
 Subscript[x, 1] Subscript[x, 3] + \!\(
\*SubsuperscriptBox[\(x\), \(1\), \(2\)]\ 
\*SubscriptBox[\(x\), \(3\)]\) + Subscript[x, 2] Subscript[x, 3] + 
 Subscript[x, 1] Subscript[x, 2] Subscript[x, 3] + \!\(
\*SubsuperscriptBox[\(x\), \(2\), \(2\)]\ 
\*SubscriptBox[\(x\), \(3\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(3\), \(2\)]\) + Subscript[x, 1] 
\!\(\*SubsuperscriptBox[\(x\), \(3\), \(2\)]\) + Subscript[x, 2] 
\!\(\*SubsuperscriptBox[\(x\), \(3\), \(2\)]\) + 
\!\(\*SubsuperscriptBox[\(x\), \(3\), \(3\)]\)

Is this what you were hoping for?

$\endgroup$
  • $\begingroup$ Of course, it looks nicer in a notebook. $\endgroup$ – Mark R Jun 14 at 5:44
  • $\begingroup$ Thank you for your answer. I think the way I posed my question was misleading. Please check the updated version. $\endgroup$ – Alonso Perez Lona Jun 14 at 9:19
  • $\begingroup$ Got it. @kglr solution seems quite nice. $\endgroup$ – Mark R Jun 14 at 18:53

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.