Python-Programm eine LR-Zerlegung

Aufrufe: 175     Aktiv: 14.12.2023 um 12:52

0
1) Implementieren Sie in einem Python-Programm eine LR-Zerlegung mit Spaltenpivotsuche und Zeilenvertauschung.Benutzen Sie diese Implementierung, um zu einer gegebenen regulären Matrix A die Inverse A^−1 zu berechnen.
2) Testen Sie Ihr Programm anhand der folgenden Matrix

A = ( 0 -4 10 15/2
-2 6 3 10
2 -6 7 -11/2
-2 10 -12 0 )
in dem Sie es die Inverse sowie das Produkt A^−1 A berechnen und ausgeben lassen.

ich hab es gelöst aber nicht sicher ob es richtg ist oder nicht, kann jemand es nachsehen.


import numpy as np
from scipy.linalg import lu_factor, lu_solve

# Function to perform LU decomposition and calculate the inverse of a matrix
def lu_decomposition_inverse(matrix):
# Perform LU decomposition with partial pivoting
lu, piv = lu_factor(matrix)

# The number of rows of the matrix
n = matrix.shape[0]

# The inverse matrix
inverse_matrix = np.zeros_like(matrix)

# Solve the equation Ax = I for each column of the identity matrix to find the inverse
for i in range(n):
# Create the i-th column of the identity matrix
identity_column = np.zeros(n)
identity_column[i] = 1

# Solve the linear system using the LU decomposition and update the inverse matrix
inverse_matrix[:, i] = lu_solve((lu, piv), identity_column)

return inverse_matrix

# Define the matrix A from the task
A = np.array([[0, -4, 10, -15/2],
[-2, 6, 3, 10],
[2, -6, 7, -11/2],
[-2, 10, -12, 0]], dtype='float64')

# Calculate the inverse of A
A_inv = lu_decomposition_inverse(A)

# Display the results
A_inv
Diese Frage melden
gefragt

Punkte: 48

 

1
So, und da sieht man dann auch was zu Eurer LR-Zerlegung: Was ist laut Vorlesung bei Euch eine LR-Zerlegung? Präzise Definition.
Und Programme testen wird hier kaum einer, Du sollst es testen.
  ─   mikn 13.12.2023 um 23:30

A = L · R .
Zeile von R : a11 = l11 r11 = r11 ⇐⇒ r11 = a11
.
.
.
a1n = l11 r1n = r1n ⇐⇒ r1n = a1n

Spalte von L:
a21 = l21 r11 = l21 ⇐⇒ l21 = a21/a11
.
.
an1 = ln1 r11 = ln1 ⇐⇒ ln1 = an1/a11
Ax = b
⇐⇒PAx = Pb
⇐⇒L Rx= Pb
Ly = b′
Rx = y

Der Aufwand zur Berechnung der LR-Zerlegung ist NLR(n) = 2/3 ^n3 + O(n^2).

für Algorithmus LR-Zerlegung und Lösung von Ax = b

Input: A ∈ K^n×n, b ∈ K^n (werden uberschrieben)
Output: L ∈ K^n×n in aij , j < i, (lii = 1 wird nicht gespeichert)
R ∈ K^n×n in aij , j ≥ i
p : {1, . . . , n} → {1, . . . , n}
x ∈ K^n
for (k = 1; k < n; k = k + 1) do
Finde r ∈ {k,...,n} so dass ark ̸= 0; {sonst Fehler}
if (r ̸= k) then {tausche Zeile k mit Zeile r}
for (j = 1; j ≤ n; j = j + 1) do
t = akj ; akj = arj ; arj = t;
.
.
.
for (k = n; k ≥ 1; k = k − 1) do {Ruckwärtseinsetzen}
t = 0;
for (j = k + 1; j ≤ n; j = j + 1) do
t = t + akj · xj ;
end for
xk = (yk − t)/akk;
end for.



so was in der Vorlesung nehmen wir aber ich gucke immer in google und youtube es ist schwer in der Vorlesung es zu verstehen.
  ─   abdull 14.12.2023 um 11:09

1
Du musst dich an den Definitionen deiner Vorlesung orientieren. Für Erklärungen usw. kann man auch anderswo schauen. Ich habe nicht nach der Berechnung, sondern nach der Def. der LR-Zerlegung gefragt. Das obige ist widersprüchlich.   ─   mikn 14.12.2023 um 11:25

Klar mache ich. (die LR_zerlegung ist gleich in der Uni oder im internet sogar im Internet leichter)

dieser Code habe ich gerade implementiert :


import numpy as np
from scipy.linalg import lu_factor, lu_solve

def lu_decomposition_inverse(matrix):
# Perform LU decomposition with partial pivoting
lu, piv = lu_factor(matrix)

# The number of rows of the matrix
n = matrix.shape[0]

# The inverse matrix
inverse_matrix = np.zeros_like(matrix)

# Solve the equation Ax = I for each column of the identity matrix to find the inverse
for i in range(n):
# Create the i-th column of the identity matrix
identity_column = np.zeros(n)
identity_column[i] = 1

# Solve the linear system using the LU decomposition and update the inverse matrix
inverse_matrix[:, i] = lu_solve((lu, piv), identity_column)

return inverse_matrix

# Define the matrix A from the task
A = np.array([[0, -4, 10, 15/2],
[-2, 6, 3, 10],
[2, -6, 7, -11/2],
[-2, 10, -12, 0]], dtype='float64')

# Calculate the inverse of A using the implemented function
A_inv = lu_decomposition_inverse(A)

# Test the result by multiplying A with its inverse (should give an identity matrix)
identity_matrix = np.dot(A, A_inv)

# Display the results
print("Original Matrix A:")
print(A)
print("\nInverse Matrix A^-1:")
print(A_inv)
print("\nProduct of A and A^-1:")
print(identity_matrix)
  ─   abdull 14.12.2023 um 12:49
Kommentar schreiben
0 Antworten