0
$\begingroup$

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] := 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.

$\endgroup$
4
  • $\begingroup$ Doesn't the requirement that row i has all zeros if PrimePi[i] > 4 just mean that rows 11 through 20 are all zero? $\endgroup$ Oct 7, 2017 at 10:09
  • $\begingroup$ 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[10], whereas 7 is the largest prime less than 10). Will edit. $\endgroup$ Oct 7, 2017 at 10:20
  • $\begingroup$ How about mat = Pick[Array[m, {20, 10}], GreatestPrimeFactor[#] <= 7 & /@ Range[20]] and then Solve[Total[#] == 1/5 & /@ mat, Flatten[mat]]? $\endgroup$ Oct 7, 2017 at 10:56
  • $\begingroup$ @aardvark2012 This didn't work regardless of whether I included the definition of GreatestPrimeFactor. $\endgroup$ Oct 7, 2017 at 11:45

1 Answer 1

1
$\begingroup$

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]

enter image description here

$\endgroup$
3
  • $\begingroup$ Would FindInstance still work if I were to include sums for columns? $\endgroup$ Oct 7, 2017 at 11:13
  • $\begingroup$ @TheSubstitute try it and see :) $\endgroup$
    – ubpdqn
    Oct 7, 2017 at 11:15
  • $\begingroup$ @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. $\endgroup$
    – ubpdqn
    Oct 7, 2017 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.