-
Notifications
You must be signed in to change notification settings - Fork 0
/
GaussElimin.py
44 lines (35 loc) · 1.02 KB
/
GaussElimin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# coding: utf-8
# # Welcome to Jupyter!
from numpy import dot
from numpy import array
def gaussElimin(a,b):
n = len(b)
for k in range(0,n-1):
for i in range(k+1,n):
if a[i,k]!=0.0:
lam =a[i,k]/a[k,k]
a[i,k+1:n] = a[i,k+1:n]-lam*a[k,k+1:n]
b[i]=b[i]-lam*b[k]
for k in range(n-1,-1,-1):
b[k] = (b[k]-dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
return b
a = array([[1,1,1,1],[0,1,-1,-1],[0,0,-5,-8],[0,0,0,11]])
b = array([[10],[-5],[-47],[44]])
gaussElimin(a,b)
a = array([[1,-2,2],[2,-3,-3],[4,1,6]])
b = array([[-2],[4],[3]])
gaussElimin(a,b)
from numpy import dot
from numpy import array
def gaussElimin(a,b):
n = len(b)
for k in range(0,n-1):
for i in range(k+1,n):
if a[i,k]!=0.0:
lam =a[i,k]/a[k,k]
a[i,k+1:n] = a[i,k+1:n]-lam*a[k,k+1:n]
b[i]=b[i]-lam*b[k]
for k in range(n-1,-1,-1):
b[k] = (b[k]-dot(a[k,k+1:n],b[k+1:n]))/a[k,k]
return b