# Setting a numerical value to a sum

bear with me as I'm very inexperienced with Mathematica, although I think my problem is reasonably straight forward:

I'm currently trying to find a matrix from a simple expression. I've found a way to extract the matrix, however there still remains a constraint that I must somehow impose in order for the matrix to be correct. Here is what I have:

nn = 10;
row[n_] := Sum[(p + 1 + R + 3 n^2) a[p], {p, 0, nn - 1}] + \[Alpha] a[n - 1]
vector = Table[a[i], {i, 0, nn - 1}];
matrix = Table[D[row[i], vector[[j]]], {i, 1, nn}, {j, 1, nn}]


This produces a matrix which is fantastic. However, I still have two "boundary conditions" left to impose which are (written in Mathematica code):

Sum[a[n],{n,0,nn-1}]=0
Sum[n^2a[n],{n,0,nn-1}]=0


Does anyone have any ideas of how I can impose these constraints upon the above set of code, such that the end matrix takes them into account?

Thank you :)

Edit:

nn = 10;
row[n_] := Sum[(p + 1 + R + 3 n^2) a[p], {p, 0, nn - 1}] + \[Alpha] a[n - 1]
vector = Table[a[i], {i, 0, nn - 1}];
matrix = Table[D[row[i], vector[[j]]], {i, 1, nn}, {j, 1, nn}]
matrix /. { a -> -Sum[i^2 a[i],{i,2,nn-1}] - Sum[a[i],{i,1,nn-1}], a -> -Sum[i^2 a[i],{i,2,nn-1}]}


You may use the 2 equations to eliminate 2 of the a[n]. E.g. to eliminate aand a:

From Sum[a[n],{n,0,nn-1}]=0 and Sum[n^2a[n],{n,0,nn-1}]=0 we get:

a== -Sum[a[i],{i,1,nn-1}]
a== -Sum[i^2 a[i],{i,2,nn-1}]


Or:

a==  -Sum[i^2 a[i],{i,2,nn-1}] - Sum[a[i],{i,1,nn-1}]
a== -Sum[i^2 a[i],{i,2,nn-1}]


To insert this into matrix, you can use ReplaceAll (/.) and a Rule (.. -> ..), look this up in the manual:

matrix /. {
a -> -Sum[i^2 a[i],{i,2,nn-1}] - Sum[a[i],{i,1,nn-1}],
a -> -Sum[i^2 a[i],{i,2,nn-1}]}

• Thank you very much for your answer. I see, so I can impose these conditions upon the matrix simply by rearranging the constraining sums. How would I go about inserting this into the code? Mar 23, 2021 at 11:02
• I added this to my answer. Mar 23, 2021 at 11:07
• Ah yes, amazing stuff! I've added an edit to my original post which is what I think my code should now look like. Does it look okay? Thank you again for you time, it is greatly appreciated. Mar 23, 2021 at 11:26