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