How To Know If Two Vectors Are Colinear? (Python)
5 comments

“Collinearity of vectors is the silent language of geometry, where lines speak in unison and points align with purpose.” — Unknown
In the exciting world of linear algebra, vectors play a starring role. But sometimes, we need to know if these vectors are just roommates sharing a line, or if they’re truly on the same path. This is where the concept of collinearity comes in!
What is Collinearity?

Imagine two arrows pointing in space. We say these vectors, denoted by fancy arrows like → , are collinear if they lie on the same straight line. It doesn’t matter if they have the same length or point in the same direction; as long as they’re parallel (or anti-parallel) and share that line, they’re considered collinear. Think of train tracks — any two points along those tracks represent collinear vectors!
How to Check for Collinearity
In order for any two vectors to be collinear, they need to satisfy certain conditions. Here are the important conditions of vector collinearity:
- Condition 1: Two vectors 𝑝→ and 𝑞→ are considered to be collinear vectors if there exists a scalar ’n’ such that 𝑝→ = n · 𝑞→
- Condition 2: Two vectors 𝑝→ and 𝑞→ are considered to be collinear vectors if and only if the ratio of their corresponding coordinates are equal. This condition is not valid if one of the components of the given vector is equal to zero.
- Condition 3: Two vectors 𝑝→ and 𝑞→ are considered to be collinear vectors if their cross product is equal to the zero vector. This condition can be applied only to three-dimensional or spatial problems.ctors
def collinearity(x1, y1, x2, y2):
"""
This function checks if two vectors starting from the origin are collinear.
Args:
x1, y1: Coordinates of the first vector.
x2, y2: Coordinates of the second vector.
Returns:
True if the vectors are collinear, False otherwise.
"""
# Handle zero vector case (collinear with all vectors)
if (x1 == 0 and y1 == 0) or (x2 == 0 and y2 == 0):
return True
# One vector is the origin (0, 0)
if x1 == 0 or y1 == 0:
return x2 * y1 == y2 * x1 # Check if other vector is a multiple of this one
# Avoid division by zero for non-origin cases
elif x2 == 0:
return y1 == 0 # Only consider horizontal line (x2 = 0)
else:
# Check for proportional coordinates (excluding zero cases handled above)
return (y1 / x1) == (y2 / x2)
# Test cases
print(collinearity(1, 1, 1, 1)) # True
print(collinearity(1, 2, 2, 4)) # True
print(collinearity(1, 1, 6, 1)) # False
print(collinearity(1, 2, -1, -2)) # True
print(collinearity(1, 2, 1, -2)) # False
print(collinearity(4, 0, 11, 0)) # True
print(collinearity(0, 1, 6, 0)) # False (not origin)
print(collinearity(4, 4, 0, 4)) # False (not same slope)
print(collinearity(0, 0, 0, 0)) # True (zero vector)
print(collinearity(0, 0, 1, 0)) # True (y-axis)
print(collinearity(5, 7, 0, 0)) # True
ND Solution For Two Vectors
import numpy as np
def is_zero_vector(vector):
"""
Check if a vector is the zero vector in NumPy.
Args:
vector (numpy.ndarray): The vector to check.
Returns:
bool: True if the vector is the zero vector, False otherwise.
"""
return np.count_nonzero(vector) == 0
# Example usage
print(np.array([0, 0, 0])) # True
print(np.array([1, 0, 0])) # False
def are_vectors_collinear(v1, v2):
"""
Test if two vectors are collinear.
Args:
v1 (numpy.ndarray): The first vector as a 1-D NumPy array.
v2 (numpy.ndarray): The second vector as a 1-D NumPy array.
Returns:
bool: True if the vectors are collinear, False otherwise.
"""
# If either of them is the zero vector
if (is_zero_vector(v1) or is_zero_vector(v2)):
return True
# Calculate the cross product of the two vectors
cross_product = np.cross(v1, v2)
# Check if the cross product is equal to the NULL Vector (zero vector)
is_collinear = np.allclose(cross_product, np.zeros(3))
return is_collinear
Conclusion
Collinearity is a fundamental concept in linear algebra and statistics, which refers to the relationship between two or more variables that are highly correlated.
If you liked this content I’d appreciate an upvote or a comment. That helps me improve the quality of my posts as well as getting to know more about you, my dear reader.
Muchas gracias!
Follow me for more content like this.
X | PeakD | Rumble | YouTube | Linked In | GitHub | PayPal.me | Medium
Down below you can find other ways to tip my work.
BankTransfer: "710969000019398639", // CLABE
BAT: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
ETH: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875",
BTC: "33xxUWU5kjcPk1Kr9ucn9tQXd2DbQ1b9tE",
ADA: "addr1q9l3y73e82hhwfr49eu0fkjw34w9s406wnln7rk9m4ky5fag8akgnwf3y4r2uzqf00rw0pvsucql0pqkzag5n450facq8vwr5e",
DOT: "1rRDzfMLPi88RixTeVc2beA5h2Q3z1K1Uk3kqqyej7nWPNf",
DOGE: "DRph8GEwGccvBWCe4wEQsWsTvQvsEH4QKH",
DAI: "0x33CD7770d3235F97e5A8a96D5F21766DbB08c875"
Comments