Pythonda bosh elementni tanlash bilan Gauss usuli, diagonalda 0ni amalga oshirish kiritilgan

|
Pythonda bosh elementni tanlash bilan Gauss usuli, diagonalda 0ni amalga oshirish kiritilgan
|
bet | 96/135 | Sana | 22.05.2024 | Hajmi | 15,08 Mb. | | #250347 |
Bog'liq Fizik jarayonlarni kompyuterda modellashtirishPythonda bosh elementni tanlash bilan Gauss usuli, diagonalda 0ni amalga oshirish kiritilgan.
1 def gaussPivotFunc(matrix):
2 for nrow in range(len(matrix)):
3 # nrow satr nomeriga teng
4 # np.argmax kichiklashtirilgan matritsada maksimal element bilan satr nomerinin qaytaradi
# nrow satridan boshlangan bo’lganligi uchun, natijaga nrowni qo’shish kerak
6.pivot = nrow + np.argmax(abs(matrix[nrow:, nrow]))
7 if pivot != nrow:
8 # swap
9 # matrix[nrow], matrix[pivot] = matrix[pivot], matrix[nrow] - не работает.
10 # pastda yozilgan kabi satrlarni taqdim etish mumkin matrix[[nrow,
11 pivot]] = matrix[[pivot, nrow]]
12 row = matrix[nrow]
13 divider = row[nrow] # diagonal element
14 if abs(divider) < 1e-10:
15 # Diagonalda deyarli nol. Davom etishning ma’nosi yo’q, hisob natijasi qat’iy emas
16 raise ValueError(f"Matritsa moslashmagan. {nrow} ustunda maksimal element: {divider:.3g}")
17 # diagonal elementga bo’lib yuboramiz.
18 row /= divider
19 # pastda joylashgan satrlardan keltirilgan satrni ayirish kerak bo’ladi
20 for lower_row in matrix[nrow+1:]:
21 factor = lower_row[nrow] # nrow kolonkasida satr elementi
22 lower_row -= factor*row # nrow kolonkasida nolni olish uchunayiramiz
23 # diagonal ko’rinishga keltiramiz
24 make_identity(matrix)
25 return matrix
Yana bitta Gauss usuliga doir dasturni Pythonda keltiramiz:
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
106
|
import copy, numpy
a = numpy.array([[2., 2, 1, 1],
[1, 2, 1, 1],
[0, 1, 1, 2]])
print(a)
def gaussFunc(a):#В metod haqiqiy sonlardan iborat massivni bering))
eps = 1e-16
c = numpy.array(a) #с bizga kerak bo’ladi
a = numpy.array(a) #NumPy kutubhonasini qo’llab biz a massiv kopiyasini ishlatamiz
# (nima uchun? Python-dinamik tipizatsiyali dasturlashtirish tili
# u ixtiyoriy ob’ektga keladi va uni massivga kiritishga tog’ri bo’ladi )
len1 = len(a[:, 0])# A matritsaning o’lchovini, y’ani n ni
len2 = len(a[0, :])#n+1
# vectB = copy.deepcopy(a[:, len1])#вектор B в Ax=B
for g in range(len1):
max = abs(a[g][g]) #saqlash uchun o’zgaruvchi
my = g #navbatdagi ustundagi maximum
t1 = g # g
while t1 < len1: # q ustunida maksimumni qidirish
if abs(a[t1][g]) > max:
max = abs(a[t1][g])
my = t1
t1 += 1
if abs(max) < eps: #Agar topilgan maksimum < 0(mashinanikidan 0 dan kichik bo’lsa) hatoni tashlab yuboramiz
raise DetermExeption("Check determinant") #matritsaning detirmenanti 0ga teng
if my != g:
a[g][:], a[my][:] = a[my][:], a[g][:] #nabatdagi satrni boshqa satr bilan almashtiramiz
# maksimum swap(),shundaymi?)
amain = float(a[g][g]) #navbatdagi x oldida koeffitsient
z = g
while z < len2:
#navbatdagi oldida butun satrni a ga bo’lamiz
a[g][z] = a[g][z] / amain #x esa 1 bo’ladi
z += 1
j = g + 1
while j < len1: #koeffitsientga ko’paytirilgan satrni ayiramiz
b = a[j][g] # keyingisidan natijada nollardan iborat ustun olamiz.
z = g Global tsikl toki bo’lguncha amalga oshadi
while z < len2: #pastki uchburchak matritsada nollar bo’lmaydi
a[j][z] = a[j][z] - a[g][z] * b
z += 1
j += 1
a = backTrace(a, len1) #Gauss usulining teskari yo’li
print("Hatolik:")
print(vectorN(c, a, len1)) #bog’lanmaslik vektori normasini chiqarish
return a
class DetermExeption(Exception):#Hatolik, matritsa determinantini hisoblang def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
def backTrace(a, len1):#orqaga yurish
a = numpy.array(a)
i = len1 - 1
while i > 0:
j = i - 1
while j >= 0:
a[j][len1] = a[j][len1] - a[j][i] * a[i][len1]
j -= 1
i -= 1
return a[:, len1]
def vectorN(c, a, len1): # c-boshlang’ich matritsa a-javob len 1-ustunlar soni, vectB-vektor B
c = numpy.array(c)
a = numpy.array(a)
vectB = copy.deepcopy(c[:, len1])
b = numpy.zeros((len1))
i = 0
while i < len1: #bog’lanmaslik vektorini olish uchun olingan x-larni qo’yamiz
j = 0
while j < len1:
b[i] += c[i][j] * a[j]
j += 1
i = i + 1
c = copy.deepcopy(b)
for i in range(len1):
c[i] = abs(c[i] - vectB[i])#bog’lanmaslik vektoridan B vektorini olamiz
#normani olamiz
return c
b = gaussFunc(a)
print("Javob:")
print(b)
|
|
| |