diff --git a/qr.py b/qr.py index ebe08b63c8c16c26dcc663c17c304953661ad963..cdb069842ed2578e232db059eec26575e9381581 100644 --- a/qr.py +++ b/qr.py @@ -16,8 +16,12 @@ def givens_rotation(A, i, j): np.ndarray: Rotation Matrix ``J`` as above. """ # TODO - c = abs(A[j][j]) / ((A[i][j] ** 2 + A[j][j] ** 2) ** 0.5) - s = abs(A[i][j]) / ((A[i][j] ** 2 + A[j][j] ** 2) ** 0.5) + if abs(A[j][j]) == 0.0: + p = ((A[i][j] ** 2 + A[j][j] ** 2) ** 0.5) + else: + p = np.sign(A[j][j]) * ((A[i][j] ** 2 + A[j][j] ** 2) ** 0.5) + c = A[j][j] / p + s = A[i][j] / p n, m = np.shape(A) J = np.eye(n) J[j][j] = c @@ -99,7 +103,15 @@ def main(): Hier kann beliebiger Testcode stehen, der bei der Korrektur vollständig ignoriert wird. """ - pass + arr1 = np.array(([3, -4], [3, 5])) + J1 = givens_rotation(arr1, 1, 0) + arr2 = np.array(([3, 4], [3, 5])) + J2 = givens_rotation(arr2, 1, 0) + Q, R = qr_decompose(arr1) + print(f"J1= {J1}") + print(f"J2= {J2}") + print(f"J1*arr= {np.dot(J1, arr1)}") + print(f"J2*arr= {np.dot(J2, arr2)}") if __name__ == "__main__": main() \ No newline at end of file