Created
November 28, 2020 21:58
-
-
Save dr-alberto/2c16915d2998e4676543c182c81af214 to your computer and use it in GitHub Desktop.
Check the linear depencency of two vectors using Numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.linalg import lu | |
def linearly_dependent(x, y): | |
if len(x) == 2 and len(y) == 2: | |
matrix = np.array([x, y]).T | |
return np.linalg.det(matrix) == 0.0 | |
else: | |
matrix = np.array([x, y]) | |
_, u = lu(matrix, permute_l=True) | |
zero_rows = np.where(~u.any(axis=1))[0] | |
if zero_rows.size > 0: # Check array is empty | |
return True | |
else: | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment