2022-11-18 09:38:00 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
Created on Sat Nov 7 13:26:09 2020
|
|
|
|
|
|
|
|
Höhere Mathematik 1, Serie 8, Gerüst für Aufgabe 2
|
|
|
|
|
|
|
|
Description: calculates the QR factorization of A so that A = QR
|
|
|
|
Input Parameters: A: array, n*n matrix
|
|
|
|
Output Parameters: Q : n*n orthogonal matrix
|
|
|
|
R : n*n upper right triangular matrix
|
|
|
|
Remarks: none
|
|
|
|
Example: A = np.array([[1,2,-1],[4,-2,6],[3,1,0]])
|
|
|
|
[Q,R]=Serie8_Aufg2(A)
|
|
|
|
|
|
|
|
@author: knaa
|
|
|
|
"""
|
2022-11-18 19:34:54 +01:00
|
|
|
import numpy as np
|
2022-11-18 09:38:00 +01:00
|
|
|
|
|
|
|
def Serie8_Aufg2(A):
|
2022-11-18 19:34:54 +01:00
|
|
|
unknown = 0
|
|
|
|
|
2022-11-18 09:38:00 +01:00
|
|
|
|
|
|
|
A = np.copy(A) #necessary to prevent changes in the original matrix A_in
|
|
|
|
A = A.astype('float64') #change to float
|
|
|
|
|
|
|
|
n = np.shape(A)[0]
|
|
|
|
|
|
|
|
if n != np.shape(A)[1]:
|
|
|
|
raise Exception('Matrix is not square')
|
|
|
|
|
|
|
|
Q = np.eye(n)
|
|
|
|
R = A
|
2022-11-18 19:34:54 +01:00
|
|
|
|
2022-11-18 09:38:00 +01:00
|
|
|
for j in np.arange(0,n-1):
|
2022-11-18 19:34:54 +01:00
|
|
|
a = np.copy(unknown).reshape(n-j,1)
|
|
|
|
e = np.eye(unknown)[:,0].reshape(n-j,1)
|
2022-11-18 09:38:00 +01:00
|
|
|
length_a = np.linalg.norm(a)
|
2022-11-18 19:34:54 +01:00
|
|
|
if a[0] >= 0: sig = unknown
|
|
|
|
else: sig = unknown
|
|
|
|
v = unknown
|
|
|
|
u = unknown
|
|
|
|
H = unknown
|
2022-11-18 09:38:00 +01:00
|
|
|
Qi = np.eye(n)
|
2022-11-18 19:34:54 +01:00
|
|
|
Qi[j:,j:] = unknown
|
|
|
|
R = unknown
|
|
|
|
Q = unknown
|
2022-11-18 09:38:00 +01:00
|
|
|
|
|
|
|
return(Q,R)
|
|
|
|
|
|
|
|
|
2022-11-18 19:34:54 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
A = np.array([
|
|
|
|
[1, -2, 3],
|
|
|
|
[-5, 4, 1],
|
|
|
|
[2, -1, 3]
|
|
|
|
])
|
|
|
|
b = np.array([
|
|
|
|
[1],
|
|
|
|
[9],
|
|
|
|
[5]
|
|
|
|
])
|
2022-11-18 09:38:00 +01:00
|
|
|
|
2022-11-18 19:34:54 +01:00
|
|
|
Serie8_Aufg2(A)
|