#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Aufgabenkontrolle12.py

# Diese Datei soll nur importiert, aber nicht bearbeitet werden.

import numpy as np

def TestOrthoL2(liste):
    print('Teste Orthonormalität der Basispolynome bezüglich des L2-Skalarprodukts:')
    m = len(liste)
    for i in range(m):
        if not np.allclose((liste[i]*liste[i]).integral(-1,1), 1):
            print('Test ist fehlgeschlagen!')
            return
        for j in range(i+1, m):
            if not np.allclose((liste[i]*liste[j]).integral(-1,1), 0):
                print('Test ist fehlgeschlagen!')
                return
    print('Test war erfolgreich!')
    return

####
def QRTest(A,testfunktion):
    '''QRTest(A,testfunktion) testet folgende Dinge:
    * Ist A eine m x n Matrix mit m>=n
    * Gilt Rang(A)=n
    Dann wird Q,R = testfunktion(A) berechnet und folgendes getestet:
    * Ist R eine obere Dreicksmatrix
    * Ist Q eine orthogonale Matrix
    * Gilt A=Q*R
    '''
    m,n=np.shape(A)
    assert m>=n, 'Es muss m>=n gelten'
    print('m>=n')
    assert np.linalg.matrix_rank(A)==n, 'Der Rang von A muss n sein'
    print('Rang(A)=n')
    Q,R=testfunktion(A)
    assert not (R-np.triu(R)).any(), 'R ist keine obere Dreiecksmatrix'
    print('R ist obere Dreieckmatrix')
    mq,nq=np.shape(Q)
    assert mq==nq, 'Q ist nicht quadratisch'
    print('Q ist quadratisch')
    assert np.linalg.norm(Q.T@Q-np.eye(mq),2)<10**(-10), 'Q ist keine orthogonale Matrix'
    print('Q ist orthogonal')
    assert np.linalg.norm(Q@R-A)<10**(-10), 'Q*R ist nicht A'
    print('QR=A')
    print('Alles super!')
