# Mathematica How to determine arbitrary constants with "Solve?"

1. Jan 7, 2017

### aheight

Hi,

I am using Solve to solve a system of linear equations with more unknowns than equations. For example, I have 36 variables and Solve returns 30 equations. Six of the variables I can assign a value of one. But I can only figure out which ones are arbitrary by manually looking at the equations. In the example below, the subscripts 0, 1,2, 10, 11, and 12 are arbitrary. But I would want to solve larger systems and manually looking for the arbitrary variables is not practical. Just to make the notation clear, the subscripts are 0,1,2,3,4,5,10,11,12,13,14,15,20,21,22,23 24,25 and so on for a total of 36.

Would someone know of a way that I can programatically determine which ones are arbitrary and then assign them for example a value of one and then get the rest of them?

$\left\{\left\{s_3\to -10 s_0-6 s_1-3 s_2,s_4\to 15 s_0+8 s_1+3 s_2,s_5\to -6 s_0-3 s_1-s_2,s_{13}\to -40 s_0-16 s_1-4 s_2-10 s_{10}-6 s_{11}-3 s_{12},s_{14}\to 80 s_0+32 s_1+8 s_2+15 s_{10}+8 s_{11}+3 s_{12},s_{15}\to -40 s_0-16 s_1-4 s_2-6 s_{10}-3 s_{11}-s_{12},s_{20}\to -10 s_0-4 s_{10},s_{21}\to -20 s_0-10 s_1-4 s_{10}-4 s_{11},s_{22}\to -30 s_0-20 s_1-10 s_2-4 s_{10}-4 s_{11}-4 s_{12},s_{23}\to 220 s_0+94 s_1+26 s_2+36 s_{10}+20 s_{11}+8 s_{12},s_{24}\to -160 s_0-64 s_1-16 s_2-24 s_{10}-12 s_{11}-4 s_{12},s_{25}\to 0,s_{30}\to 20 s_0+6 s_{10},s_{31}\to 60 s_0+20 s_1+12 s_{10}+6 s_{11},s_{32}\to 120 s_0+60 s_1+20 s_2+18 s_{10}+12 s_{11}+6 s_{12},s_{33}\to -240 s_0-96 s_1-24 s_2-36 s_{10}-18 s_{11}-6 s_{12},s_{34}\to 0,s_{35}\to 0,s_{40}\to -15 s_0-4 s_{10},s_{41}\to -61 s_0-15 s_1-12 s_{10}-4 s_{11},s_{42}\to -154 s_0-61 s_1-15 s_2-24 s_{10}-12 s_{11}-4 s_{12},s_{43}\to 0,s_{44}\to 0,s_{45}\to 0,s_{50}\to 4 s_0+s_{10},s_{51}\to 16 s_0+4 s_1+3 s_{10}+s_{11},s_{52}\to 40 s_0+16 s_1+4 s_2+6 s_{10}+3 s_{11}+s_{12},s_{53}\to 0,s_{54}\to 0,s_{55}\to 0\right\}\right\}$

And here is what I get when I assign the arbitrary ones to a value of one:

$\left\{\left\{s_3\to -19,s_4\to 26,s_5\to -10,s_{13}\to -79,s_{14}\to 146,s_{15}\to -70,s_{20}\to -14,s_{21}\to -38,s_{22}\to -72,s_{23}\to 404,s_{24}\to -280,s_{25}\to 0,s_{30}\to 26,s_{31}\to 98,s_{32}\to 236,s_{33}\to -420,s_{34}\to 0,s_{35}\to 0,s_{40}\to -19,s_{41}\to -92,s_{42}\to -270,s_{43}\to 0,s_{44}\to 0,s_{45}\to 0,s_{50}\to 5,s_{51}\to 24,s_{52}\to 70,s_{53}\to 0,s_{54}\to 0,s_{55}\to 0\right\}\right\}$

2. Jan 7, 2017

### Staff: Mentor

Assuming the equations are linearly independent, then any set of 6 should be arbitrary.

3. Jan 7, 2017

### aheight

Hi Dale,

I tried using the first six, s0 through s5 but they must not be linearly independent then because Mathematica then returns an incomplete solution set (containing some s_i's). I have been working on the problem and have reduced it to coding in Mathematica, selecting from two lists, those elements that are not in both lists. This code:

val1 = theSolution // Flatten
val2 = Association[val1]
Keys[val2]
Flatten[theCoefficients]
iList = Intersection[Flatten[theCoefficients], Keys[val2]]

returns two lists: all the coefficients, and all the explicit solutions returned by Solve:

$\left\{s_0,s_1,s_2,s_3,s_4,s_5,s_{10},s_{11},s_{12},s_{13},s_{14},s_{15},s_{20},s_{21},s_{22},s_{23},s_{24},s_{25},s_{30},s_{31},s_{32},s_{33},s_{34},s_{35},s_{40},s_{41},s_{42},s_{43},s_{44},s_{45},s_{50},s_{51},s_{52},s_{53},s_{54},s_{55}\right\}$

$\left\{s_3,s_4,s_5,s_{13},s_{14},s_{15},s_{20},s_{21},s_{22},s_{23},s_{24},s_{25},s_{30},s_{31},s_{32},s_{33},s_{34},s_{35},s_{40},s_{41},s_{42},s_{43},s_{44},s_{45},s_{50},s_{51},s_{52},s_{53},s_{54},s_{55}\right\}$

note the second list is missing s0, s1, s2, s10,s11, and s12. Would you know of a compact way of selecting which elements of the two list are not in both of the list? I suppose I could do this by brute force checking each one (not exactly sure how) but I was just wondering if there is a more rule-oriented way of doing it?

Last edited: Jan 7, 2017
4. Jan 7, 2017

### Staff: Mentor

For this type of problem, I would strongly recommend casting it as a matrix so that you can use the many existing linear algebra routines.

You can use CoefficientArray to cast it into matrix form. Then you should look at functions like NullSpace

5. Jan 7, 2017

### aheight

Yes, of course Dale. I should analyze the problem from the perspective of linear algebra.

Last edited: Jan 7, 2017
6. Jan 7, 2017

### Staff: Mentor

By the way, although I recommend the linear algebra approach, if you ever need to do this the function you want is Complement.

7. Jan 7, 2017

### aheight

Thanks. That does the trick! Also I've made progress since you recommended the linear algebra approach: What I have is a homogeneous system of M equations in N unknowns. That system will have a set of infinite solutions (the kind I'm looking for) only if there exists at most N-1 linearly independent equations and I can easily determine this by using MatrixRank of the coefficient matrix. Here's the code I use:

theNormalForm =
Normal[CoefficientArrays[#, Flatten[theCoefficients]]] & /@
theEquations;
theCMatrix = #[[2]] & /@ theNormalForm;
Print["number of equations", Length[theEquations]];
Print["number of variables: ", Length[Flatten[theCoefficients]]];
Print["Matrix Rank: ", MatrixRank[theCMatrix]];

When the MatrixRank is at most one less than the number of variables, I know I have a good (non-trivial) solution.

Last edited by a moderator: Jan 9, 2017
8. Jan 8, 2017

### aheight

Last edited: Jan 8, 2017
9. Jan 10, 2017

### Ben Niehoff

By the way, Solve is often capable of working out which variables can be arbitrary. You can give it something like this

Code (Text):
Solve[a+b+c == 7, {a,b,c}]
It will throw a warning, but it will spit out a solution, probably this one:

Code (Text):
{{a -> 7-b-c}}
Then any variables that remain on the right-hand-side of the list of Rules returned can be considered arbitrary.