# Is this approach for testing orthogonality/parallelity of vectors wrong as I think?

In a math book four methods are written for testing parallelity/orthogonality of two vectors that are(notice $\vec v$ and $\vec w$ are approximations of vectors and we have x,y and z components of them and also $Approx(Expression)$ means Approximated value of Expression):

1. if $(Approx(\left\lvert v⃗ .w⃗ \right\rvert) < Approx(threshold))$ then (vectors are assumably orthogonal))

2. if $(Approx(\left\lVert v⃗ ×w⃗ \right\rVert) < Approx(threshold))$ then (vectors are assumably parallel)

Now it seemed right to me until I saw another approach:

1. if $(Approx \left(\frac{ \left\lvert v⃗ .w⃗ \right\rvert }{ ∥v⃗ ∥×∥w⃗ ∥ } \right) < Approx(threshold))$ then (vectors are assumably orthogonal)

2. if $(Approx \left (\frac{ \left\lVert v⃗ ×w⃗ \right\rVert }{ ∥v⃗ ∥×∥w⃗ ∥ } \right ) < Approx(threshold) )$ then (vectors are assumably parallel)

Now $\theta$ being the angle between $\vec v$ and $\vec w$ that are approximations of our vectors You can notice in the second approach what is calculated is approximation of cosθ or sinθ but in book's approach it is approximation of $∥v⃗ ∥×∥w⃗ ∥ ×$ ( cosθ OR sinθ ) hence I immediately sensed there might be something wrong with book's approach, so do you also think the book's approach is flawed?

## 3 Answers

The thing I see wrong with the first four approaches is that they all depend on the magnitudes of the vectors. Very short vectors could be neither parallel nor orthogonal, and could still show up as parallel or orthogonal or -- get this -- both, depending on what you set "threshold" to be.

So I prefer the methods you show next. But, even then, parallelism and orthogonality all depend completely on $\theta$, so why not drop the vector magnitudes out of the expressions altogether?

• what do you mean there's the possibility of very short vectors being neither parallel nor orthogonal? and of course by normalizing both vectors the expression will be as simple as Cosθ dropping magnitudes out of expression – Pooria Aug 13 '14 at 2:37
• If the product of the two magnitudes is less than the threshold, the tests for parallel and orthogonal vectors (from the first set of tests) will all pass regardless of what the angle is between the vectors. – David K Aug 13 '14 at 13:05
• " ... by normalizing both vectors the expression will be as simple as Cosθ ... " Exactly! – bob.sacamento Aug 13 '14 at 15:17

It's already been pointed out that all of the first four approaches appear not to account for the possibility that the vector magnitudes are relatively small. Some of the tests could give false positive results (that is, they could say that two vectors are orthogonal or parallel when the vectors are not) because the product of the magnitudes and the trigonometric function is below the threshold due to the small magnitudes rather than due to the smallness of the trigonometric function.

Tests 2 and 4 have an additional flaw. The function $\cos \theta$ is very near $1$ even when $\theta$ is relatively quite far from zero, and $\sin \theta$ is very near $1$ even when $\theta$ is relatively quite far from $\pi/2$. It is much better to test with a function that varies rapidly as $\theta$ varies around the sought-for value.

Some of the formulas fail to take the absolute value of a quantity when they should, with the result that a large negative value of $\cos \theta$ or $\sin \theta$ could cause the test to give a false positive.

While it's true that $\vec u \cdot \vec v = \| \vec u \| \| \vec v \| \cos \theta,$ and using the right-hand side of that equation helps to explain why the algorithms work (or do not work), when writing out the algorithm itself I think it helps if one does not make this substitution for $\vec u \cdot \vec v.$ So I might write a test for orthogonality this way: consider $\vec u$ and $\vec v$ orthogonal if $$\frac{|\,\vec u \cdot \vec v\,|}{\| \vec u \| \| \vec v \|} < \epsilon,$$ where $\epsilon$ is my threshold value. In fact the left-hand side of this formula is a way to compute $|\cos \theta|,$ as you can clearly see if you substitute $\| \vec u \| \| \vec v \| \cos \theta$ for $\vec u \cdot \vec v.$ But after you have made that substitution, you no longer have an expression that tells you how to compute $|\cos \theta|.$

By the way, since $\| \vec w \| = \sqrt{\vec w \cdot \vec w},$ and because square roots tend to be more expensive to compute than products of scalars, a computationally more efficient test might be $$\frac{(\vec u \cdot \vec v)^2}{\| \vec u \|^2 \| \vec v \|^2} < \epsilon^2.$$ The reason for writing the test that way has everything to do with how the test works, and nothing to do with why it works. Writing an algorithm should be all about how; proving it should be about why.

There is a similar concern about tests that use $\sin \theta$: how does one compute that value so that the test can be applied? One option involves the cross product $\vec u \times \vec v$, but another option is not to compute $\sin \theta$ at all. Instead you could normalize the two vectors (create unit vectors in the same direction) as follows: $$\begin{eqnarray} \hat u & = & \frac{1}{\|u\|}\vec u, \\ \hat v & = & \frac{1}{\|v\|}\vec v. \end{eqnarray}$$ Then consider $\vec u$ and $\vec v$ parallel if either $\| \hat u - \hat v \| < \epsilon$ or $\| \hat u + \hat v \| < \epsilon$ (that is, if the normalized vector $\hat u$ is nearly equal to $\hat v$ or nearly equal to $-\hat v$). Again, you may be able to use the fact that $\|\vec w\| < \epsilon$ if and only if $(\vec w \cdot \vec w)^2 < \epsilon^2,$ provided that $\epsilon > 0.$

• I did some corrections but still preserved fundamental fault that I've addressed in my answer – Pooria Aug 18 '14 at 15:00

So I found out what exactly is very wrong with approach of the book, I'll explain it regarding first method and analysis of others is similar. To test orthogonality of vectors we can check if condition $\left\lvert \cos\theta \right\rvert < OurThreshold$($\theta$ being the angle between $\vec v$ and $\vec w$ that are approximations of our vectors) named condition_2 is true, so lets note these two facts( Approx(Expression) means Approximated value of Expression and in each side of Inequation $Approx(Expression)+Error=ExpressionRealValue$):

• If condition: $$Approx(\left\lvert \vec v . \vec w \right\rvert) + Error < Approx(\left\lVert \vec v \right\rVert \times \left\lVert \vec w \right\rVert \times OurThreshold) + Error$$ named condition_1 is true then condition_2 is also true

We Remove errors from expression_1 so it becomes: $$Approx(\left\lvert \vec v . \vec w \right\rvert) < Approx(\left\lVert \vec v \right\rVert \times \left\lVert \vec w \right\rVert \times OurThreshold)$$

Now if condition_1is true then we assume condition_2 is true and so vectors are assumably orthogonal.

In the book's approach threshold is considered to be an arbitrary small positive value and we name this threshold "BookThreshold", the condition in first method of the book's approach: $$Approx(\left\lvert \vec v . \vec w \right\rvert) < Approx(BookThreshold)$$ is equivalent to condition: $$Approx(\left\lvert \vec v . \vec w \right\rvert) < Approx(\alpha \times \left\lVert \vec v \right\rVert \times \left\lVert \vec w \right\rVert \times OurThreshold)$$ So noticing that additional $\alpha$ value it's obvious checking conditions of book's approach doesn't do us any good