Mathematik online lernen im Mathe-Forum. Nachhilfe online
Startseite » Forum » Python-Programm

Python-Programm

Universität / Fachhochschule

Sonstiges

Tags: Sonstig

 
Antworten Neue Frage stellen Im Forum suchen
Neue Frage
Alashi

Alashi aktiv_icon

23:19 Uhr, 13.12.2023

Antworten
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

Für alle, die mir helfen möchten (automatisch von OnlineMathe generiert):
"Ich möchte die Lösung in Zusammenarbeit mit anderen erstellen."
Online-Nachhilfe in Mathematik
Antwort
HAL9000

HAL9000

08:25 Uhr, 14.12.2023

Antworten
Das ist also eine Übung, die passenden Bibliotheksfunktionen rauszusuchen - nicht, den Algorithmus selbst zu implementieren?

Zumindest solltest du in der ersten Zeile deiner Anwendungsmatrix A besser 15/2 statt -15/2 schreiben. Und wenn die inverse Matrix am Schluss angezeigt werden soll, dann sollte es doch doch "print(A_inv)" statt nur "A_inv" lauten, oder?
Alashi

Alashi aktiv_icon

09:45 Uhr, 14.12.2023

Antworten
Ja Natürlich Sie haben Recht.
was meinten Sie damit (die passenden Bibliotheksfunktionen rauszusuchen - nicht, den Algorithmus selbst zu implementieren)? weil in der frage steht:

Implementieren Sie in einem Python-Programm eine LR-Zerlegung mit Spaltenpivotsuche und Zeilenvertauschung entsprechend dem Algorithmus aus der Vorlesung.
Hierzu können Sie das Jupyter-Notebook 504.ipynb als Grundlage verwenden. Benutzen Sie diese Implementierung, um zu einer gegebenen regulären Matrix A die Inverse A^−1
zu berechnen.


Und ich hab das Jupyter-Notebook 504.ipynb als Grundlage verwendet.
Antwort
HAL9000

HAL9000

10:21 Uhr, 14.12.2023

Antworten
> Und ich hab das Jupyter-Notebook 504.ipynb als Grundlage verwendet.

Das wusste ich ja nicht, dass ihr da irgendeine Vorlage habt. Für mich klang der ursprünglich von dir gepostete Aufgabentext so, dass man den Algorithmus "von der Pike auf" zu programmieren hat.


P.S.: Und unterlasse bitte diese Crosspostings ohne Querverlinkung. Hier im Forum mag das gestattet sein - anderswo nicht:

www.matheboard.de/thread.php?threadid=605119
Alashi

Alashi aktiv_icon

11:25 Uhr, 14.12.2023

Antworten
Danke für die Rückmeldung.

Sorry ich wollte einfach sicher sein dass meine lösungen >Richtig sind. ich hab mich abgemeldet von matheboard .

Antwort
HAL9000

HAL9000

12:24 Uhr, 14.12.2023

Antworten
> ich hab mich abgemeldet von matheboard .

Das fordert keiner. Auch Crosspostings sind legitim - es gehört nur zur Netiquette, dass du dann dort jeweils (mit Link) darauf hinweist, wo du die Frage sonst schon überall gestellt hast, damit sich die Leute nicht unnötig abmühen, wenn doch die Frage andernorts schon hinreichend gut beantwortet wurde.
Alashi

Alashi aktiv_icon

13:23 Uhr, 14.12.2023

Antworten
ich könnte es verbessern


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:")
Alashi

Alashi aktiv_icon

13:23 Uhr, 14.12.2023

Antworten
ich könnte es verbessern


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:")
Antwort
HAL9000

HAL9000

14:54 Uhr, 14.12.2023

Antworten
Irgendwie scheint als letzte Zeile

print(identity_matrix)

zu fehlen. Ich hab keine Lust, das Skript jetzt manuell Punkt für Punkt durchzugehen - führe es einfach aus, und wenn am Ende WIRKLICH die Einheitsmatrix rauskommt, dann scheint ja alles in Ordnung zu sein.


Diese Frage wurde automatisch geschlossen, da der Fragesteller kein Interesse mehr an der Frage gezeigt hat.