# 在 Python 中使用 Pivoting 進行高斯消除

Isaac Tony 2022年5月17日

• 將一行與一個標量相乘，然後將其與另一行相加或減去。
• 交換行。
• 將行與標量相乘。

## 在 Python 中使用樞軸進行高斯消除

``````from numpy import array, zeros, fabs, linalg

a = array(
[
[0, 6, -1, 2, 2],
[0, 3, 4, 1, 7],
[5, 1, 0, 3, -1],
[3, 1, 3, 0, 2],
[4, 4, 1, -2, 1],
],
float,
)
# the b matrix constant terms of the equations
b = array([5, 7, 2, 3, 4], float)

print("Solution by NumPy:")

print(linalg.solve(a, b))

n = len(b)
x = zeros(n, float)

# first loop specifys the fixed row
for k in range(n - 1):
if fabs(a[k, k]) < 1.0e-12:

for i in range(k + 1, n):
if fabs(a[i, k]) > fabs(a[k, k]):
a[[k, i]] = a[[i, k]]
b[[k, i]] = b[[i, k]]
break

# applies the elimination below the fixed row

for i in range(k + 1, n):
if a[i, k] == 0:
continue

factor = a[k, k] / a[i, k]
for j in range(k, n):
a[i, j] = a[k, j] - a[i, j] * factor
# we also calculate the b vector of each row
b[i] = b[k] - b[i] * factor
print(a)
print(b)

x[n - 1] = b[n - 1] / a[n - 1, n - 1]
for i in range(n - 2, -1, -1):
sum_ax = 0

for j in range(i + 1, n):
sum_ax += a[i, j] * x[j]

x[i] = (b[i] - sum_ax) / a[i, i]

print("The solution of the system is:")
print(x)
``````

``````Solution by NumPy:
[ 0.38947368  0.49473684 -0.10877193  0.12982456  0.83157895]
[[ 5.00000000e+00  1.00000000e+00  0.00000000e+00  3.00000000e+00  -1.00000000e+00]
[ 0.00000000e+00  3.00000000e+00  4.00000000e+00  1.00000000e+00  7.00000000e+00]
[ 0.00000000e+00  0.00000000e+00  4.50000000e+00  0.00000000e+00  6.00000000e+00]
[ 0.00000000e+00  4.44089210e-16  0.00000000e+00  3.52702703e+00  2.95945946e+00]
[ 0.00000000e+00  0.00000000e+00  0.00000000e+00  0.00000000e+00  2.11354647e+00]]
[2.         7.         4.5        2.91891892 1.75758075]
The solution of the system is:
[ 0.38947368  0.49473684 -0.10877193  0.12982456  0.83157895]
``````

``````from numpy import array, zeros, fabs, linalg
``````

``````a = array(
[
[0, 6, -1, 2, 2],
[0, 3, 4, 1, 7],
[5, 1, 0, 3, -1],
[3, 1, 3, 0, 2],
[4, 4, 1, -2, 1],
],
float,
)
# the b matrix constant terms of the equations
b = array([5, 7, 2, 3, 4], float)
``````

``````for k in range(n - 1):
if fabs(a[k, k]) < 1.0e-12:

for i in range(k + 1, n):
if fabs(a[i, k]) > fabs(a[k, k]):
a[[k, i]] = a[[i, k]]
b[[k, i]] = b[[i, k]]
break
``````

``````for i in range(k+1, n):
# first row should remain intact
if a[i, k] == 0:
continue
# calculating the factor
factor = a[k, k]/a[i, k]
for j in range(k, n):
a[i, j] = a[k, j] - a[i, j]*factor
# calculating the b vector
b[i] = b[k] - b[i]*factor
print(a)
print(b)
``````

``````# calculating the value of x beginning from n-1
x[n - 1] = b[n - 1] / a[n - 1, n - 1]
for i in range(n - 2, -1, -1):
sum_ax = 0
# calculating the value of the sum
for j in range(i + 1, n):
sum_ax += a[i, j] * x[j]
# calculating the value of x
x[i] = (b[i] - sum_ax) / a[i, i]

print(x)
``````

``````[[ 2.00000000e+00  3.00000000e+00  4.00000000e+00  1.00000000e+00
7.00000000e+00]
[ 0.00000000e+00  7.00000000e+00 -1.00000000e+00  3.00000000e+00
1.00000000e+00]
[ 0.00000000e+00  0.00000000e+00 -1.30000000e+01  2.00000000e+00
-2.10000000e+01]
[ 0.00000000e+00  0.00000000e+00  0.00000000e+00  2.81250000e+00
5.81250000e+00]
[ 0.00000000e+00  8.88178420e-16  0.00000000e+00 -4.44089210e-16
4.95734797e+00]]
[  7.           5.         -14.           0.625        0.15371622]
The solution of the system is:
[0.02170543 0.79224806 1.05116279 0.15813953 0.03100775]
``````

``````print("The numpy solution:")
print(linalg.solve(a, b))
``````

``````Solution by Nuumpy:
[0.02170543 0.79224806 1.05116279 0.15813953 0.03100775]
``````

Isaac Tony is a professional software developer and technical writer fascinated by Tech and productivity. He helps large technical organizations communicate their message clearly through writing.