Solved Task 2c+d
This commit is contained in:
		
							parent
							
								
									0074722ad3
								
							
						
					
					
						commit
						60db5aa861
					
				| 
						 | 
					@ -15,10 +15,10 @@ Example: A = np.array([[1,2,-1],[4,-2,6],[3,1,0]])
 | 
				
			||||||
@author: knaa
 | 
					@author: knaa
 | 
				
			||||||
"""
 | 
					"""
 | 
				
			||||||
import numpy as np
 | 
					import numpy as np
 | 
				
			||||||
 | 
					import timeit
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Aufgabe 2a
 | 
				
			||||||
def Serie8_Aufg2(A):
 | 
					def Serie8_Aufg2(A):
 | 
				
			||||||
    unknown = 0
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    A = np.copy(A)                       #necessary to prevent changes in the original matrix A_in
 | 
					    A = np.copy(A)                       #necessary to prevent changes in the original matrix A_in
 | 
				
			||||||
    A = A.astype('float64')              #change to float
 | 
					    A = A.astype('float64')              #change to float
 | 
				
			||||||
| 
						 | 
					@ -55,7 +55,7 @@ def Serie8_Aufg2(A):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    # Beispiel
 | 
					    # Beispiel aus Skript
 | 
				
			||||||
    # A = np.array([[1, 2, -1], [4, -2, 6], [3, 1, 0]])
 | 
					    # A = np.array([[1, 2, -1], [4, -2, 6], [3, 1, 0]])
 | 
				
			||||||
    # b = np.array([
 | 
					    # b = np.array([
 | 
				
			||||||
    #     [9],
 | 
					    #     [9],
 | 
				
			||||||
| 
						 | 
					@ -63,7 +63,7 @@ if __name__ == '__main__':
 | 
				
			||||||
    #     [9]
 | 
					    #     [9]
 | 
				
			||||||
    # ])
 | 
					    # ])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Example from Task 1
 | 
					    # Beispiel aus Aufgabe 1
 | 
				
			||||||
    A = np.array([
 | 
					    A = np.array([
 | 
				
			||||||
        [1, -2, 3],
 | 
					        [1, -2, 3],
 | 
				
			||||||
        [-5, 4, 1],
 | 
					        [-5, 4, 1],
 | 
				
			||||||
| 
						 | 
					@ -77,6 +77,7 @@ if __name__ == '__main__':
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [Q,R]=Serie8_Aufg2(A)
 | 
					    [Q,R]=Serie8_Aufg2(A)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Aufgabe 2b
 | 
				
			||||||
    n = len(b) - 1
 | 
					    n = len(b) - 1
 | 
				
			||||||
    QTb = Q.T @ b
 | 
					    QTb = Q.T @ b
 | 
				
			||||||
    result = [0 for i in range(n+1)]
 | 
					    result = [0 for i in range(n+1)]
 | 
				
			||||||
| 
						 | 
					@ -94,3 +95,26 @@ if __name__ == '__main__':
 | 
				
			||||||
    print("\nQ:\n", Q)
 | 
					    print("\nQ:\n", Q)
 | 
				
			||||||
    print("\nR:\n", R)
 | 
					    print("\nR:\n", R)
 | 
				
			||||||
    print("\Result:\n", result)
 | 
					    print("\Result:\n", result)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Aufgabe 2c
 | 
				
			||||||
 | 
					    t1 = timeit.repeat("Serie8_Aufg2(A)", "from __main__ import Serie8_Aufg2, A", number=100)
 | 
				
			||||||
 | 
					    t2 = timeit.repeat("np.linalg.qr(A)", "from __main__ import np, A", number=100)
 | 
				
			||||||
 | 
					    avg_t1 = np.average(t1) / 100
 | 
				
			||||||
 | 
					    avg_t2 = np.average(t2) / 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    print("Geschwindigkeit mit 3x3 Matrix:")
 | 
				
			||||||
 | 
					    print("Benötigte Zeit mit eigener Funktion:", avg_t1)
 | 
				
			||||||
 | 
					    print("Benötigte Zeit mit Numpy:", avg_t2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Aufgabe 2d
 | 
				
			||||||
 | 
					    Test = np.random.rand(100,100)
 | 
				
			||||||
 | 
					    t1 = timeit.repeat("Serie8_Aufg2(Test)", "from __main__ import Serie8_Aufg2, Test", number=100)
 | 
				
			||||||
 | 
					    t2 = timeit.repeat("np.linalg.qr(Test)", "from __main__ import np, Test", number=100)
 | 
				
			||||||
 | 
					    avg_t1 = np.average(t1) / 100
 | 
				
			||||||
 | 
					    avg_t2 = np.average(t2) / 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    print("Geschwindigkeit mit 100x100 Matrix:")
 | 
				
			||||||
 | 
					    print("Benötigte Zeit mit eigener Funktion:", avg_t1)
 | 
				
			||||||
 | 
					    print("Benötigte Zeit mit Numpy:", avg_t2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Die von Numpy zur Verfügung gestellt Funktion ist wesentlich effizienter.
 | 
				
			||||||
		Loading…
	
		Reference in New Issue