views:

3014

answers:

7

Disclaimer
This is not strictly a programming question, but most programmers soon or later have to deal with math (especially algebra), so I think that the answer could turn out to be useful to someone else in the future.

Now the problem
I'm trying to check if m vectors of dimension n are linearly independent. If m == n you can just build a matrix using the vectors and check if the determinant is != 0. But what if m < n?

Any hints?

+12  A: 

Construct a matrix of the vectors (one row per vector), and perform a Gaussian elimination on this matrix. If any of the matrix rows cancels out, they are not linearly independent.

The trivial case is when m > n, in this case, they cannot be linearly independent.

David Hanak
Could you please explain better your solution? I should execute a gaussian elimination on what exactly?
tunnuz
On the vectors. Vector 1 = column 1, vector 2 = column 2 etc..
Gamecat
Let's say you have the 2 vectors (2 3) (4 6). They map to the following set of equations: `2x + 3y = a` and `4x + 6y = b`. If you try a gaussian elimination of x, you end up with `0x + 0y = 2a - b`. Having the zeros indicates that the two vectors are not independant.Generalize for `M` and `N`.
Pierre
+4  A: 

Construct a matrix M whose rows are the vectors and determine the rank of M. If the rank of M is less than m (the number of vectors) then there is a linear dependence. In the algorithm to determine the rank of M you can stop the procedure as soon as you obtain one row of zeros, but running the algorithm to completion has the added bonanza of providing the dimension of the spanning set of the vectors. Oh, and the algorithm to determine the rank of M is merely Gaussian elimination.

Take care for numerical instability. See the warning at the beginning of chapter two in Numerical Recipes.

Jason
Can I use partial pivoting with this gaussian elimination?
tunnuz
+2  A: 

If m<n, you will have to do some operation on them (there are multiple possibilities: Gaussian elimination, orthogonalization, etc., almost any transformation which can be used for solving equations will do) and check the result (eg. Gaussian elimination => zero row or column, orthogonalization => zero vector, SVD => zero singular number)

However, note that this question is a bad question for a programmer to ask, and this problem is a bad problem for a program to solve. That's because every linearly dependent set of n<m vectors has a different set of linearly independent vectors nearby (eg. the problem is numerically unstable)

jpalecek
Yes, but not every independent set has a dependent set nearby.
mattiast
True, but that just means every output of the algorithm on dependent set is somewhat bogus
jpalecek
A: 

See also this video lecture.

tunnuz
A: 

If computing power is not a problem, probably the best way is to find singular values of the matrix. Basically you need to find eigenvalues of M'*M and look at the ratio of the largest to the smallest. If the ratio is not very big, the vectors are independent.

mattiast
A: 

What if I find a zero on the pivotal element during the gaussian elimination?

tunnuz
Simply swap two columns; that should not change the crux of the algorithm.
David Hanak
A: 

Another way to check that m row vectors are linearly independent, when put in a matrix M of size mxn, is to compute

det(M * M^T)

i.e. the determinant of a mxm square matrix. It will be zero if and only if M has some dependent rows. However Gaussian elimination should be in general faster.

Fanfan