Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to determine with code (c++ for example) if two 3D vectors are linearly dependent.

I know that if I could determine that the expression $ v_1 = k · v_2 $is true then they are linearly dependent; they are linearly independent otherwise.

I've tried to construct an equation system to determine that, but since there could be zeros anywhere it gets very tricky and could end with divisions by zero and similar.

I've also though about using some matrices/determinants, but since the matrix would look like:

$$ \begin{matrix} x_1 & y_1 & z_1\\ x_2 & y_2 & z_2\\ \end{matrix} $$

i don't see an easy way to check for the the linear dependency... any ideas?


share|cite|improve this question
Note from one of the answers below: two vectors are dependent if and only if their vector cross product is $(0,0,0).$ If the entries do not get too large, this is good, but there is a numerical problem with large entries and inaccurate cancellation. I prefer the other, entries that are $0$ must match, then check that you get matching ratios for all nonzero entry pairs. – Will Jagy Jul 21 '12 at 22:10
The only thing that bothers me now is that all the solutions require checks for equality between floating point numbers... – Xavier Arias Botargues Jul 21 '12 at 22:34
Numerically, it is difficult to decide linear dependence, it usually comes down to some tolerance. You could normalize the two vectors to get $u,v$ say, then check if $\|u-\langle v,u \rangle v\| < \epsilon$, for some $\epsilon>0$. – copper.hat Jul 21 '12 at 22:37
Both answers below are unlikely to work unless you have exact data and exact computations. – copper.hat Jul 21 '12 at 22:58
@copper.hat is right. If you're working with floating-point numbers, I would say checking for linear independence is an ill-posed problem. Perhaps there is another way to do whatever you're trying to achieve without this linear independence check. – Rahul Jul 22 '12 at 0:57
up vote 2 down vote accepted

Here is the portion of the code you need:

if((x1*y2 - x2*y1) != 0 || (x1*z2 - x2*z1) != 0 || (y1*z2 - y2*z1) != 0)
     //Here you have independent vectors
     //Here the vectors are linearly dependent
share|cite|improve this answer
I assume this is the cross vector between the given vectors as Will Jagy pointed out, right? – Xavier Arias Botargues Jul 21 '12 at 22:34
No, this is just checking if the ratios are the same, but avoids zero division. – copper.hat Jul 21 '12 at 23:00
@copper.hat, it is the cross product, just out of order and with a missing $\; - \;$ sign on the $zx$ pair. – Will Jagy Jul 22 '12 at 0:03
@WillJagy: Wow,I never looked at it that way. Curious! So, my earlier no should be a yes... – copper.hat Jul 22 '12 at 0:52
I have never thought of the cross product as a measure of how far the vectors are 'out of ratio'. Interesting perspective, thanks for pointing it out. – copper.hat Jul 22 '12 at 4:20

If $x_1/x_2 = y_1/y_2 = z_1/z_2$ then $v_1 = k v_2.$ Care needs to be taken for the zeros though. If any component is zero, then the corresponding component needs to be zero.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.