# How do I determine if two 2d vector parallel?

Well, we've learned how to detect whether two vectors are perpendicular to each other using dot product.

 a.b=0


if two vectors parallel, which command is relatively simple.

for 3d vector, we can use cross product.

for 2d vector, use what?

for example,

 a={1,3}, b={4,x};
a//b


How to use a equation to solve $x$.

I tried it, but this is a little complex.

  Projection[a, b] - a == 0

• Is this question related to the software Mathematica or is it a math question? If so, then you should ask this rather in math.stackexchange.com Nov 29, 2016 at 12:08
• I know how to solve it in math. I just don't know how to use Mathematica to realize it. Nov 29, 2016 at 12:12
• Why not? What have you tried? It's important to always show what you did and where you got stuck. Nov 29, 2016 at 12:15

$a$ and $b$ are parallel if $a = \kappa b$. Try

MatrixRank[{a, b}] == 1


for an easy way to test this. This works only if neither of the vectors have norm 0. Symbolic vector components (parameters) are considered independent by MatrixRank, so this method considers vectors parallel only if they are parallel for any value of the parameters.

For a fully general symbolic solution use

Reduce[a = k b, k]

a = {x, y};
b = {x, z};
Reduce[a == k b, k]

(* (y == z && k == 1) || (x == 0 && z != 0 && k == y/z) || (z == 0 && y == 0 && x == 0) *)

a = {1, 3};
b = {4, x};
Reduce[a == k b, {k, x}]

(* k == 1/4 && x == 12 *)

• for example, a={1,2}, b={3,x},a//b, I hope solve a equation to get value of $x$. how to do this? Nov 29, 2016 at 12:15
• @tiankonghewo Your comment shows that you didn't describe your problem precisely. This is why it's important to show what you have tried. It is not at all clear from your question that this is what you wanted. Write the mathematical definition of what "parallel" means ($a=\kappa b$) and put it in Reduce . Nov 29, 2016 at 12:21
• a = {1, 2};b = {4, x};Reduce[a == k *b && k \[Element] Integers, {x, k}] this do not work. Nov 29, 2016 at 12:26
• @tiankonghewo It seems to me that it works fine. The equations you wrote have no solution, and Reduce tells you this. The only solution is k==1/4, which is not an integer. Nov 29, 2016 at 12:27
• yes, you are right. Nov 29, 2016 at 12:32

Based on the definition of the scalar product for $d$-dimensional vectos

$$a \cdot b = |a| |b| \cos(\phi)$$

you can create a test based only on vector operations. The vectors are parallel, if and only if the angle between them is $\phi = 0$ or $\phi = \pi$. So, if the following quantity $q$

$$q = \frac{|a \cdot b|}{|a| |b|}$$

equals to 1, then the vectors are parallel. Mathematica code

(*Dimension*)
d = 7;
(*Generate vectors*)
a = RandomReal[{-1, 1}, d];
b = RandomReal[]*a;
(*Test*)
If[Abs[a.b]/(Norm[a]*Norm[b]) == 1, Print["Parallel"],
Print["Not parallel"]]


Parallel

Based on $q$ you can solve your problem for $x$. But I think you can figure that out.

• Reduce[(a.b)^2 == a.a b.b] is indeed better than my suggestion of Reduce[a==k b]. For a={x,y}; b={x,z} your way gives the very clean x == 0 || y == z. Probably this should be the accepted answer. Nov 29, 2016 at 12:47

Try this:

rule = {x_, y_} -> {x, y, 0};
Cross[a /. rule, b /. rule]


if

  a = {1, 1};
b = {0.1, 0.1};

Cross[a /. rule, b /. rule]
(*  {0., 0., 0.}  *)


or alternatively

Cross @@ Map[Replace[#, rule] &, {a, b}]


Have fun!