Rank of Matrices in Mathematica

In summary: ...will not work if some of these variables have values that make them not be in the subsequent rows.
  • #1
Newtime
348
0
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.
 
Physics news on Phys.org
  • #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:
Code:
In[1]:= mat={{a,b,c},{d,e,f},{g,h,i}};
        MatrixRank[mat]
Out[2]= 3
Then you can test if a given matrix of minors has a zero solution using something like
Code:
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
Code:
minimalRank[mat_, dom_:Complexes] := Module[{i = 1}, While[!testZeroMinors[mat, i, dom], i++]; i-1]

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

Code:
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]
         minimalRank[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}};
Solve[m[[1]]==j*m[[2]]&&m[[2]]==k*m[[3]],Union[m[[1]],m[[2]]]]

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
Newtime said:
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

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

with

Code:
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.
 

1. What is the rank of a matrix in Mathematica?

The rank of a matrix in Mathematica is a measure of the linear independence of its rows and columns. It is the maximum number of linearly independent rows or columns in the matrix. The rank can be calculated using the MatrixRank function in Mathematica.

2. How can I find the rank of a specific matrix in Mathematica?

To find the rank of a specific matrix in Mathematica, you can use the MatrixRank function. Simply input the matrix as an argument and the function will return the rank. For example, MatrixRank[{{1,2,3},{4,5,6},{7,8,9}}] will return a rank of 2.

3. Can a matrix have a rank of 0 in Mathematica?

Yes, a matrix can have a rank of 0 in Mathematica. This means that all the rows and columns of the matrix are linearly dependent and can be expressed as a combination of other rows and columns. In this case, the matrix is said to be singular or degenerate.

4. How does the rank of a matrix affect its properties in Mathematica?

The rank of a matrix can affect its properties in Mathematica in various ways. For example, a non-singular matrix (rank ≠ 0) can be inverted, while a singular matrix (rank = 0) cannot. Additionally, the rank can also affect the solution space of a system of linear equations represented by the matrix.

5. Is the rank of a matrix in Mathematica affected by the size of the matrix?

Yes, the rank of a matrix in Mathematica can be affected by the size of the matrix. Generally, a larger matrix may have a higher rank as it has more rows and columns to potentially be linearly independent. However, this is not always the case as the rank also depends on the values within the matrix.

Similar threads

Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
92
  • Calculus and Beyond Homework Help
Replies
6
Views
300
  • Advanced Physics Homework Help
Replies
8
Views
738
  • Linear and Abstract Algebra
Replies
15
Views
1K
  • Linear and Abstract Algebra
Replies
14
Views
2K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
897
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top