Rank of Matrices in Mathematica

...will not work if some of these variables have values that make them not be in the subsequent rows.f
  • #1
The problem: I need to find the (minimal) rank of some matrix which is basically all parameters. For example, when i ask for the rank of [tex]\begin{pmatrix} a& b& c \\ d& e& f \\ g& h& i \end{pmatrix}[/tex], I get 3. I would like to get 1, since (excluding the possibility of a matrix of all 0's) it could be the case that row two is a multiple of row one, etc. Is there anyway to do this? If not, does anyone have any idea for a good script that could accomplish this?

I know Mathematica is advanced enough to recognize things like multiple of rows on its own. For instance, if I input a general rank one matrix [tex]\begin{pmatrix} s_1t_1& s_1t_2& s_1t_3 \\ s_2t_1& s_2t_2& s_2t_3 \\ s_3t_1& s_3t_2& s_3t_3 \end{pmatrix}[/tex], I get 1.
  • #2
Warning: I'm not an expert in linear algebra... there's probably a better way to solve this problem.

The Mathematica documentation page on http://reference.wolfram.com/mathematica/ref/Minors.html#ES_3_" says
The rank is the largest k for which Minors[mat, k] contains nonzero entries:
And I think that this might be a [STRIKE]good[/STRIKE] possible way to go.

As you said in your post:
In[1]:= mat={{a,b,c},{d,e,f},{g,h,i}};
Out[2]= 3
Then you can test if a given matrix of minors has a zero solution using something like
testZeroMinors[mat_, rnk_, dom_: Complexes] := Not[{} === FindInstance[
  And[And@@Thread[Minors[mat, rnk] == 0], Or@@Thread[Variables[mat] != 0], 
      Element[Variables[mat], dom]], Variables[mat]]]
Then the minimal rank (assuming that not all free parameters are zero) can be found using
minimalRank[mat_, dom_:Complexes] := Module[{i = 1}, While[!testZeroMinors[mat, i, dom], i++]; i-1]

We can test this using some simple examples:
In[5]:= minimalRank[{{a,b,c},{d,e,f},{g,h,i}}]
Out[5]= 1
Out[6]= 2
Out[7]= 1
Out[8]= 3

In[9]:= mat2={{s1},{s2},{s3}}.{{t1,t2,t3}}
Out[9]= {{s1 t1,s1 t2,s1 t3},{s2 t1,s2 t2,s2 t3},{s3 t1,s3 t2,s3 t3}}
In[10]:= MatrixRank[mat2]
Out[10]= 1
Out[11]= 1
Last edited by a moderator:
  • #3
If you give Mathematica 9 different symbols, a thru i, it makes the assumption they are 9 independent quantities.

Usually, the most general solution to the problem (which in your case would be "well, the rank could be anything between 0 and 3 inclusive, depending on the values of the parameters") is not very useful. That's why Mathematica doesn't work that way.

You more or less answered your own question with your second example. If you define some relationships between the entries in the matrix, Mathematica will use all the infomation you gave it.

Note, in your the second example, it is also considering the 6 parameters to be independent. For example it is not considering special cases like s1 = s2 = s3 = 0, or t1 = t2 = t3 = 0.
  • #4
Hi AlephZero,

I think that the OP knows this. He wanted a function that gives the minimal rank a matrix could have assuming that the free parameters are all independent and in some sense "non-trivial". Mathematica gives the result that holds for almost all if the parameter space. (i.e. up to a set of measure zero).

I gave a solution that gives the minimal rank assuming that at least one of the free parameters is nonzero - which is close to what the OP wanted. The conditions in the FindInstance can be tweaked to the case that he's actually interested in.
  • #5
Thanks AlephZero and Simon Tyler for your help, the code runs quite well. However, it is not quite what I need, but I think this is mainly due to my inability to explain what I need the computer to actually do.

For example, I'm running test right now where the minimalRank is given to me as 1 for arbitrary parameters a,b. However, if I plug in ANY values for these numbers, I get the minimalRank as 2, as I expect. (I can prove by hand that indeed a and b are non-zero, but proving by hand that the minimalRank is two even with this knowledge is much more difficult, hence my desire for computer aid.) Is there a way to hard-code the program so that it doesn't consider the possibility of a or b being 0? I would think this would be very simple to do, but my programming knowledge is extremely limited.

Thanks again for the help.
  • #6
Would something like this help you?

In[31]:= m={{a,b,c},{d,e,f},{g,h,i}};

Out[32]= {{a->g j k,b->h j k,c->i j k,d->g k,e->h k,f->i k}}

In[33]:= m/.First[%]

Out[33]= {{g j k,h j k,i j k},{g k,h k,i k},{g,h,i}}

In[34]:= RowReduce[%]

Out[34]= {{1, h/g, i/g}, {0, 0, 0}, {0, 0, 0}}

and so this arbitrary matrix can have rank 1, but only if some of your variables are nonzero and others can take values to make this rank 1.

Note that Union is collecting the variables in the subsequent rows of the matrix for Solve and this simple method won't work if elements of your rows consist of more general expressions, but perhaps you can adapt this to whatever form your matrix actually has.

Furthermore, I don't believe what is done above is necessarily a general solution to your problem. I might imagine a case where row 2 is really independent and only rows 1,3,4,... can be "solved away" to give you a rank of 2. I've thought about this a bit, but don't see an elegant solution to the problem at the moment.
Last edited:
  • #7
Is there a way to hard-code the program so that it doesn't consider the possibility of a or b being 0? I would think this would be very simple to do, but my programming knowledge is extremely limited.

At the moment the code assumes that at least one of the variables is non-zero
If you want none of your variables to be zero, then replace

Or@@Thread[Variables[mat] != 0]


And@@Thread[Variables[mat] != 0]

Don't forget though, you can have many different parametrizations of the same space of matrices:
eg the span over the reals of {{a, 0, 0}, {0, b, 0}, {0, 0, 1}}
is the same as the span over the reals of {{a, 0, 0}, {0, a-b, 0}, {0, 0, 1}}
But this will be given different minimal ranks by the program.
So, think carefully about you matrix parametrizations and the questions you ask.

Suggested for: Rank of Matrices in Mathematica