Add Coefficient Lists in Mathematica: Learn How to Sum Groups of Coefficients

  • Context: Mathematica 
  • Thread starter Thread starter 76Ahmad
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

This discussion focuses on summing groups of coefficients in Mathematica using the CoefficientList and PolynomialMod functions. The user initially attempts to print coefficient lists for prime numbers but seeks a method to aggregate these coefficients. The proposed solution involves using the Table and Sum functions to efficiently compute the desired results without relying on Print or For loops. The final output correctly aggregates the coefficients into structured lists for m values ranging from 2 to 4.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with polynomial manipulation in Mathematica
  • Knowledge of prime number identification using PrimeQ
  • Basic programming concepts such as loops and data structures
NEXT STEPS
  • Explore advanced Mathematica functions like Table and Sum for data aggregation
  • Learn about polynomial operations in Mathematica, specifically PolynomialMod
  • Investigate efficient coding practices in Mathematica to minimize the use of loops
  • Study the application of the Select function for filtering prime numbers in ranges
USEFUL FOR

Mathematica users, mathematicians, and educators looking to enhance their skills in polynomial manipulation and data aggregation within the software.

76Ahmad
Messages
46
Reaction score
0
Hi all, a quistion about how to add the cofficients list to gether?
here is an example:

For[m = 2, m ≤ 4 , m++,
For[p = 2, p ≤ m^2, p++,
If[PrimeQ[p], Print[CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x]]]]]

{1,0,1}
{1,1,1}

{1,1,1,1}
{1,0,0,2}
{1,2,3,4}
{1,4,3,6}

{1,0,0,0,1}
{1,2,0,2,1}
{1,1,1,1,1}
{1,3,6,3,1}
{1,7,6,7,1}
{1,9,6,9,1}


How to add every grup of cofficients together?
the reuslt should be like that:

{2,1,2}

{4,7,7,13}

{6,22,19,22,6}

please help :))
 
Physics news on Phys.org
In[4]:=For[m=2,m≤4,m++,
Print[
Apply[Plus,
Reap[
For[p=2,p≤m^2,p++,
If[PrimeQ[p],Sow[CoefficientList[PolynomialMod[Expand[(1-x)^m],p],x]]]
]
][[2,1]]
]
]
]

From In[4]:={2,1,2}
From In[4]:={4,7,7,13}
From In[4]:={6,22,19,22,6}
 
You don't really want to use Print[] to extract you data since it's not easily accessed after that.

The way to do what you want with a minimal modification of your code would to be something like

Code:
For[m = 2, m <= 4, m++,
 For[p = 2, p <= m^2, p++,
  If[PrimeQ[p], Print[data[m, p] = 
     CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x]], 
   data[m, p] = 0]]]
Table[Sum[data[m, p], {p, 2, m^2}], {m, 2, 4}]

which returns
Code:
{{2, 1, 2}, {4, 7, 7, 13}, {6, 22, 19, 22, 6}}

But you really should avoid using explicit Do's, For's, etc... as much as possible.

Something like

Code:
Table[Sum[
    CoefficientList[PolynomialMod[Expand[(1 - x)^m], p], x],
    {p, Select[Range[2, m^2], PrimeQ]}], 
  {m, 2, 4}]

might be a better way of getting the result...