Skip to content

Instantly share code, notes, and snippets.

@hamzaavvan
Created October 6, 2019 09:48
Show Gist options
  • Save hamzaavvan/46815a646dca28e620570c47804c93c9 to your computer and use it in GitHub Desktop.
Save hamzaavvan/46815a646dca28e620570c47804c93c9 to your computer and use it in GitHub Desktop.
Python - Solving Linear Equeation Matrices
from sympy.interactive import printing
from sympy import Eq, solve_linear_system, Matrix
from numpy import linalg
import numpy as np
import sympy as sp
printing.init_printing(use_latex=True)
eq1 = sp.Function('eq1')
eq2 = sp.Function('eq2')
x, y = sp.symbols('x y')
eq1 = Eq(2*x-y,-4)
eq2 = Eq(3*x-y,-2)
display(eq1)
display(eq2)
r1 = [2,-1,-4]
r2 = [3,-1,-2]
sys = Matrix((r1, r2))
display(sys)
display(solve_linear_system(sys, x,y))
nr1 = [2,-1]
nr2 = [3,-1]
nmat = np.array([nr1, nr2])
#display(nmat)
cons = np.array([-4,-2])
ans = linalg.solve(nmat, cons)
#display(ans)
x=int(ans[0])
y=int(ans[1])
print("x = %i"%x)
print("y = %i"%y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment