create account

How To Know If Two Vectors Are Colinear? (Python) by simplestack

View this thread on: hive.blogpeakd.comecency.com
· @simplestack ·
How To Know If Two Vectors Are Colinear? (Python)
<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/AJg6T2jpCnaevrDxTvNAJodxvT759JzekYDoAtNz5KNjpUFaosAoeg6o9RrmGEx.png)
</center>

> “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?

<center>
![image.png](https://files.peakd.com/file/peakd-hive/simplestack/23tbF5JXhBu5syFjYN36tMJeu6qyM4BcL1ung6bFegrvN1uv9BNAngkMppX4LpTt7fr1e.png)
</center>

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](https://twitter.com/edca3911) | [PeakD](https://peakd.com/@simplestack) | [Rumble](https://rumble.com/user/simplestack) | [YouTube](https://www.youtube.com/@simple-stack-by-ed) | [Linked In](https://www.linkedin.com/in/edwardcasanova/) | [GitHub](https://github.com/ed3899) | [PayPal.me](https://paypal.me/edca3899?country.x=MX&locale.x=es_XC) | [Medium](https://medium.com/@ed.wacc1995/subscribe)*

*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"
```
👍  , , , ,
👎  
properties (23)
authorsimplestack
permlinkhow-to-know-if-two-vectors-are-colinear-python
categoryalgorithms
json_metadata"{"app":"peakd/2025.3.5","format":"markdown","description":"“Collinearity of vectors is the silent language of geometry, where lines speak in unison and points align wit....","portfolio":true,"tags":["algorithms","python","dsa","softwareengineering","computerscience","softwaredevelopment","vectors","linearalgebra","science","math"],"users":["simplestack","simple-stack-by-","ed.wacc1995"],"image":["https://files.peakd.com/file/peakd-hive/simplestack/AJg6T2jpCnaevrDxTvNAJodxvT759JzekYDoAtNz5KNjpUFaosAoeg6o9RrmGEx.png","https://files.peakd.com/file/peakd-hive/simplestack/23tbF5JXhBu5syFjYN36tMJeu6qyM4BcL1ung6bFegrvN1uv9BNAngkMppX4LpTt7fr1e.png"]}"
created2025-03-28 01:25:09
last_update2025-03-28 01:25:09
depth0
children5
last_payout2025-04-04 01:25:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,680
author_reputation-7,742,141,582
root_title"How To Know If Two Vectors Are Colinear? (Python)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,725,804
net_rshares5,310,212,191
author_curate_reward""
vote details (6)
@hivewatchers · (edited)
Hello.
Welcome to Hive.

<b>To confirm your authorship of the content, could you please add the word "Hive" to your LinkedIn profile?</b> 

https://www.linkedin.com/in/edwardcasanova/

You can remove this mention once we confirm the authorship.

Thank you.

More Info: <a href="https://hive.blog/hivewatchers/@steemcleaners/the-guide-for-identity-content-verification-reporting-and-lookup">Introducing Identity/Content Verification Reporting & Lookup</a>
properties (22)
authorhivewatchers
permlinkstu15u
categoryalgorithms
json_metadata{"app":"hiveblog/0.1","links":["https://www.linkedin.com/in/edwardcasanova/"]}
created2025-03-28 11:56:21
last_update2025-03-28 15:30:03
depth1
children2
last_payout2025-04-04 11:56:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length454
author_reputation-81,697,209,573,209
root_title"How To Know If Two Vectors Are Colinear? (Python)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,731,964
net_rshares0
@simplestack ·
Are you kidding me? I solved the problem
properties (22)
authorsimplestack
permlinkre-hivewatchers-stu75y
categoryalgorithms
json_metadata{"tags":["algorithms"],"app":"peakd/2025.3.6"}
created2025-03-28 14:06:51
last_update2025-03-28 14:06:51
depth2
children0
last_payout2025-04-04 14:06:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length40
author_reputation-7,742,141,582
root_title"How To Know If Two Vectors Are Colinear? (Python)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,734,255
net_rshares0
@simplestack ·
@hivewatchers done, check it out

![image.png](https://files.peakd.com/file/peakd-hive/simplestack/23zamiU4xSQrkWzTiHMoWxb6LuexjtgVf7G59GUzJWNmqQnW2RKnmL5soAxHqnUxLhd6Y.png)

and go to the link to make sure im not editing the html lol
properties (22)
authorsimplestack
permlinkre-hivewatchers-stub96
categoryalgorithms
json_metadata{"tags":["algorithms"],"app":"peakd/2025.3.6","image":["https://files.peakd.com/file/peakd-hive/simplestack/23zamiU4xSQrkWzTiHMoWxb6LuexjtgVf7G59GUzJWNmqQnW2RKnmL5soAxHqnUxLhd6Y.png"],"users":["hivewatchers"]}
created2025-03-28 15:34:21
last_update2025-03-28 15:34:21
depth2
children0
last_payout2025-04-04 15:34:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length235
author_reputation-7,742,141,582
root_title"How To Know If Two Vectors Are Colinear? (Python)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,736,327
net_rshares0
@x-rain ·
Don't hurry to re-post your stuff from Medium - it won't earn you much. Better to engage for a while and only then offer your best contest. Find relevant communities and connections first.
👍  
properties (23)
authorx-rain
permlinkre-simplestack-stzud2
categoryalgorithms
json_metadata{"tags":["algorithms"],"app":"peakd/2025.3.6","image":[],"users":[]}
created2025-03-31 15:15:03
last_update2025-03-31 15:15:03
depth1
children1
last_payout2025-04-07 15:15:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length188
author_reputation708,699,409,726,601
root_title"How To Know If Two Vectors Are Colinear? (Python)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,799,157
net_rshares104,998,814
author_curate_reward""
vote details (1)
@simplestack ·
@x-rain thx for the suggestion m8 but reposting in peakD ain't only about revenue or profits for me. It is about sharing an idea, expanding and allowing others free access to it asap. Think of PeakD as a backup in case Medium wakes up one day and decides to ban me. 

Again, I appreciate your advice ^^ 
properties (22)
authorsimplestack
permlinkre-x-rain-stzx62
categoryalgorithms
json_metadata{"tags":["algorithms"],"app":"peakd/2025.3.6","image":[],"users":["x-rain"]}
created2025-03-31 16:15:39
last_update2025-03-31 16:15:39
depth2
children0
last_payout2025-04-07 16:15:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length303
author_reputation-7,742,141,582
root_title"How To Know If Two Vectors Are Colinear? (Python)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id141,800,925
net_rshares0