Professional Documents
Culture Documents
Numpy NP Scipy - Linalg La Sympy SP: Import As Import As Import As
Numpy NP Scipy - Linalg La Sympy SP: Import As Import As Import As
In [66]:
import numpy as np
import scipy.linalg as la
import sympy as sp
sp.init_printing()
In [67]:
print(A)
print(b)
[[ 1 2 3 -1]
[ 2 5 4 8]
[ 4 2 2 1]
[ 6 4 -1 -2]]
[[10]
[ 8]
[-2]
[ 4]]
In [68]:
#matriks augmented
B = np.hstack([A,b])
print(B)
[[ 1 2 3 -1 10]
[ 2 5 4 8 8]
[ 4 2 2 1 -2]
[ 6 4 -1 -2 4]]
In [69]:
def swap_row(A,i,j):
n = A.shape[0] #tentukan jumlah baris
E = np.eye(n)
E[i,i] = 0
E[j,j] = 0
E[i,j] = 1
E[j,i] = 1
return E@A
In [70]:
def get_row(A,i,j):
n = A.shape[0]
E = np.zeros((n,n))
E[j,i] = 1
return E@A
In [71]:
def scale_row(A,i,s):
n = A.shape[0]
E = np.eye(b)
E[i,i] = s
return E@A
In [72]:
In [72]:
#proses eliminasi
#dimulai dari 0
B = B-(2/2)*get_row(B, 0, 1)
B = B-(4/4)*get_row(B, 0, 2)
B = B-(6/6)*get_row(B, 0, 3)
print(B)
[[ 1. 2. 3. -1. 10.]
[ 1. 3. 1. 9. -2.]
[ 3. 0. -1. 2. -12.]
[ 5. 2. -4. -1. -6.]]
In [73]:
B = B - get_row(B,1,2)
print(B)
[[ 1. 2. 3. -1. 10.]
[ 1. 3. 1. 9. -2.]
[ 2. -3. -2. -7. -10.]
[ 5. 2. -4. -1. -6.]]
In [ ]:
In [ ]: