Skip to content

Instantly share code, notes, and snippets.

@dr-alberto
Created November 28, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dr-alberto/2c16915d2998e4676543c182c81af214 to your computer and use it in GitHub Desktop.
Save dr-alberto/2c16915d2998e4676543c182c81af214 to your computer and use it in GitHub Desktop.
Check the linear depencency of two vectors using Numpy
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