# Compute the rank of a matrix with variable entries

Say I have a matrix like $$M=\left( \begin{array}{c c c} x & xz & w-2x \\ wz^3 & xy & z \\ y^2-z^3 & x+w & z+x^5 \end{array} \right)$$

is it possible to ask Mathematica the rank of $M$, depending on $(x,y,z,w)$ ?

-
From the docs : "MatrixRank works on both numerical and symbolic matrices. " –  b.gatessucks Apr 23 '13 at 8:48
Yes, I can read that, but it doesn't look like it gives you the rank depending on the values of the symbols. It doesn't include the possibilities of some symbols being zero for example –  Abramo Apr 23 '13 at 9:07
@b.gatessucks I guess what OP need is some result which includes all degenerated situations (such as With[{x = 0, y = 1, z = 1, w = 0}, MatrixRank[(*the matrix*)]]). –  Silvia Apr 23 '13 at 9:10
Try Reduce[Det[M]==0] to get conditions for when rank is not full. Can then investigate minors to see when it drops further. –  Daniel Lichtblau Apr 23 '13 at 14:31

I'm not sure how useful it will be (because there are lots of possibilities), but one way to attack this problem is to observe that M loses rank exactly when the determinant goes to zero. So you can use Solve to investigate. With

M[x_, y_, z_, w_]:={{x, x z, w - 2 x}, {w z^3, x y, z}, {y^2 - z^3, x + w, z + x^5}};


you can try

Solve[Det[M[x, y, z, w]] == 0, {x, y, z, w}]


This gives a very large answer, but maybe there are some insights to be had. Alternatively, you could exploit the fact that the matrix loses rank whenever any of the eigenvalues are zero (this is true because the det is the product of the eigenvalues). So you could instead try:

Solve[Eigenvalues[M[x, y, z, w]] == 0, {x, y, z, w}]


which again gives a fairly large answer.

-

I'd like to consider this problem from geometric view.

Suppose your original matrix is $m=(r_1,r_2,r_3)^\text{T}$, where $r_i$ are row vectors:

mm = {{x, x z, w - 2 x}, {w z^3, x y, z}, {y^2 - z^3, w + x, x^5 + z}};


So,

# The condition for $\text{rank}(m)=1$

The rank-1 condition should be $r_i\times r_j=0 \land r_j\times r_k=0 \land r_k\times r_i=0$:

rank1Condition = And @@ Flatten[Thread /@ {
Cross[#[[1]], #[[2]]] == 0,
Cross[#[[2]], #[[3]]] == 0,
Cross[#[[3]], #[[1]]] == 0}] &@mm


which can be further reduced by

rank1ConditionReduced = Reduce[rank1Condition, {x, y, z, w}] // ExpandAll;


which is a huge expression:

To verify its validity, we test 5 instances:

AbsoluteTiming[ instance = FindInstance[rank1ConditionReduced, {x, y, z, w}, Complexes, 5];]


{6.508372, Null}

MatrixRank /@ (mm /. instance // N[#, 20] &)


{1, 1, 1, 1, 1}

# The condition for $\text{rank}(m)=2$

Similarly we have rank-2 condition $(r_1\times r_2\neq 0\lor r_2\times r_3\neq 0\lor r_3\times r_1\neq 0)\land \text{det}(m)=0$

rank2Condition = And[Or @@ (
And @@ Thread[Cross[#[[1]], #[[2]]] != 0] & /@
Subsets[mm, {2}]),
Det[mm] == 0]


Unfortunately, rank2Condition takes too long for Reduce and FindInstance, maybe some other approach would perform better.

-
M[x_,y_,z_,w_]:={{x,x z,w-2x},{w z^3,x y,z},{y^2-z^3,x+w,z+x^5}}
MR[x_, y_, z_, w_]:=MatrixRank[M[x,y,z,w]]


For example

MR[1,2,3,4]

-
Ok, sure this is useful, thanks. But isn't there a way to ask Mathematica to find out for which values of $x,y,z,w$ the rank collapses? –  Abramo Apr 23 '13 at 9:32