-
Notifications
You must be signed in to change notification settings - Fork 0
/
funkcje.py
105 lines (86 loc) · 2.88 KB
/
funkcje.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import numpy as np
def Doolittle(A):
"""Algorytm Doolittle'a wyswietlajacy rozklad LU macierzy"""
n = len(A)
L = np.zeros((n, n), dtype= np.float64)
U = np.zeros((n, n), dtype= np.float64)
A= A.astype(np.float64)
for k in range(0, n):
L[k][k]= 1
for j in range(k, n):
suma =0
s=0
while (s<=k-1):
suma = suma + (L[k][s]* U[s][j])
s=s+1
U[k][j] = A[k][j] - suma
for i in range(k+1,n):
suma =0
s=0
while (s <= k-1):
suma = suma + (L[i][s]* U[s][k])
s=s+1
if U[k][k] == 0.0:
raise ZeroDivisionError("Zero encountered at the matrix A diagonal.")
else:
L[i][k] = (A[i][k] - suma)/ U[k][k]
return L, U
def Gauss_regular(A,B):
"""Algorytm podstawowej eliminacji Gaussa"""
n = len(B)
A= A.astype(np.float64)
B= B.astype(np.float64)
#Faza eliminacji i sprowadzenie do macierzy trojkatnej gornej
for k in range(0, n-1):
for i in range(k+1, n):
if A[k][k] == 0.0:
raise ZeroDivisionError("Zero encountered at the matrix A diagonal.")
else:
z = A[i][k]/A[k][k]
A[i][k+1:n] = A[i, k+1:n] - z *A[k,k+1:n]
B[i]= B[i] - z*B[k]
#Faza podstawiania wstecz
for k in range(n-1, -1, -1):
if A[k][k] == 0.0:
raise ZeroDivisionError("Zero encountered at the matrix A diagonal.")
else:
B[k]= (B[k] -np.dot( A[k,k+1:n], B[k+1:n]))/A[k,k]
return B
def zamienW(M, i,j):
"""Funkcja pomocnicza zamienajaca porzadek wierszy"""
if len(M.shape) == 1:
M[i],M[j] = M[j],M[i]
else:
M[[i,j],:] = M[[j,i], :]
def Gauss_scaled(A, B):
"""Algorytm elimiacji gaussa z czesciowym wyborem elementow glownych"""
n = len(A)
S = np.zeros(n) #skala
P = np.array(range(n)) #permutacje
A= A.astype(np.float64)
B= B.astype(np.float64)
#skala wierszy
for i in range(n):
S[i]= max(abs(A[i,:]))
for k in range(0, n-1):
#wybor elementu glownego
for j in range(k+1, n):
if np.abs(A[j][k])/S[j] >= abs(A[i][k])/S[i]:
pivot =j
#zamiana wierszy
if pivot != k:
zamienW(B,k,pivot)
zamienW(S,k,pivot)
zamienW(A,k,pivot)
for i in range(k+1, n):
A[k][k] = 0.0 if np.isclose(A[k][k], 0) else A[k][k]
z = A[i][k] / A[k][k]
A[i][k] = z
for j in range(k+1, n):
A[i][j] = A[i][j] - z* A[k][j]
B[i] = B[i] - z*B[k]
B[n-1] = B[n-1]/A[n-1,n-1]
for k in range(n-2,-1,-1):
A[k][k] = 0.0 if np.isclose(A[k][k], 0) else A[k][k]
B[k] = (B[k] - np.dot(A[k,k+1:n],B[k+1:n]))/A[k,k]
return B