4
$\begingroup$

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?

Thanks!

  • 0
    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.2012-07-21
  • 0
    The only thing that bothers me now is that all the solutions require checks for equality between floating point numbers...2012-07-21
  • 1
    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$.2012-07-21
  • 1
    Both answers below are unlikely to work unless you have exact data and exact computations.2012-07-21
  • 1
    @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.2012-07-22
  • 0
    i'm using linear dependence to check if two lines are parallel, skew, the same or there is intersection. So far it's been working with random lines generated using integers, but if the lines were generated with floats it could not work...2012-07-22

2 Answers 2

2

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 } else {      //Here the vectors are linearly dependent } 
  • 0
    I assume this is the cross vector between the given vectors as Will Jagy pointed out, right?2012-07-21
  • 0
    No, this is just checking if the ratios are the same, but avoids zero division.2012-07-21
  • 0
    @copper.hat, it is the cross product, just out of order and with a missing $\; - \;$ sign on the $zx$ pair.2012-07-22
  • 1
    @WillJagy: Wow,I never looked at it that way. Curious! So, my earlier no should be a yes...2012-07-22
  • 0
    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.2012-07-22
1

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.