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

  • Mathematica
  • Thread starter 76Ahmad
  • Start date
  • Tags
    Mathematica
In summary, the conversation is about how to add a list of coefficients together and the desired result is demonstrated using a For loop and a table. An alternative method using Table and PrimeQ is also suggested for better efficiency.
  • #1
76Ahmad
48
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
  • #2
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}
 
  • #3
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...
 

1. How can I solve an equation in Mathematica?

To solve an equation in Mathematica, you can use the built-in function "Solve" or "NSolve". For example, if you have the equation x^2 + 2x + 1 = 0, you can write "Solve[x^2 + 2x + 1 == 0, x]" to find the solutions. If the equation contains numerical values, you can use "NSolve" instead.

2. How do I plot a function in Mathematica?

To plot a function in Mathematica, you can use the "Plot" function. For example, if you want to plot the function y = x^2, you can write "Plot[x^2, {x, -5, 5}]" to plot the function from x = -5 to x = 5. You can also add labels, legends, and customize the appearance of the plot using various options.

3. How do I define a variable in Mathematica?

To define a variable in Mathematica, you can use the "=" symbol. For example, if you want to define the variable x as 5, you can write "x = 5" in the input. You can also use the "Clear" function to clear all the defined variables.

4. Can I import data into Mathematica?

Yes, you can import data into Mathematica using the "Import" function. This function supports various file formats, including CSV, Excel, and text files. You can also use the "Dataset" function to manipulate and analyze data in a tabular format.

5. How do I get help with a specific function in Mathematica?

To get help with a specific function in Mathematica, you can use the "Documentation" function or press F1 on your keyboard while the cursor is on the function name. This will open the documentation page for that function, where you can find usage examples, options, and related functions.

Back
Top