# Solving a System with some Row Sum Constraints

My question is about solving a linear solving system with variables $m_{i,j}, 1\le i \le 20, j\le 1\le 10$ with

(1) nonnegative entries,

(2) zeros in prescribed locations,

(3) given row sums.

I'd rather not define 200 variables, so I try to define the unknowns by $m_{i,j}$,

m[[i,j]], {1,i,20},{1,j,10}


I am requiring that row $i$ has $0$s when

GreatestPrimeFactor[i]>7


where GreatestPrimeFactor is defined as

GreatestPrimeFactor := 1
GreatestPrimeFactor[n_Integer?Positive] := FactorInteger[n][[-1, 1]]


so I use

ReplacePart[m, {i_,j_}/; GreatestPrimeFactor[i]>7->0]


For the nonzero rows, I want their sum to be $1/5$.

Sum[m[i,j],{j,1,10}]=1/5


This sum cannot always be true due to my constraint above (which forces some rows to be 0). However, I am unsure of how to tell Mathematica that I only want this to happen when Prime[i]<=4.

How can I input this into the Solve tool? I have only seen this tool used when there are few equations and few variables.

• Doesn't the requirement that row i has all zeros if PrimePi[i] > 4 just mean that rows 11 through 20 are all zero? – aardvark2012 Oct 7 '17 at 10:09
• That was a mistake. I meant to make row i zero when i has a prime factor that exceeds 7 (the 4 came from PrimePi, whereas 7 is the largest prime less than 10). Will edit. – The Substitute Oct 7 '17 at 10:20
• How about mat = Pick[Array[m, {20, 10}], GreatestPrimeFactor[#] <= 7 & /@ Range] and then Solve[Total[#] == 1/5 & /@ mat, Flatten[mat]]? – aardvark2012 Oct 7 '17 at 10:56
• @aardvark2012 This didn't work regardless of whether I included the definition of GreatestPrimeFactor. – The Substitute Oct 7 '17 at 11:45

You can use FindInstance, e.g.:

array = Array[a, {20, 10}] /.
a[i_, j_] :> 0 /; FactorInteger[i][[-1, 1]] > 7;
lhs = (Total /@ array) /. 0 -> Nothing;
rhs = Table[1/5, Length[lhs]];
var = Variables[lhs];
sol = FindInstance[{lhs == rhs, And @@ (# >= 0 & /@ var)}, var, 2];
Row[MatrixForm[N[array /. #]] & /@ sol] • Would FindInstance still work if I were to include sums for columns? – The Substitute Oct 7 '17 at 11:13
• @TheSubstitute try it and see :) – ubpdqn Oct 7 '17 at 11:15
• @TheSubstitute you need to be deal with potential zero columns and then join the two sets of equations...sorry am off to bed but suggest making sure eqns are setup correctly before passing to FindInstance. – ubpdqn Oct 7 '17 at 11:28