from numpy import * import matplotlib.pyplot as plt # Definere matriser A = array([[1,-2,1],[2,-3,4],[3,-8,0]]) B = array([[1,2,0],[3,4,1],[5,6,3]]) # Summere, subtrahere, gange, potens print(A+B) print(A-B) print(A@B) print(3*A) print(matrix(A)**3) # Transponering, invers, determinant print(A.T) print(linalg.inv(A)) print(linalg.det(A)) # Radvektor, s?ylevektor a = array([[1,2,3]]) b = array([[4],[5],[6]]) # skalarprodukt p? to forskjellige m?ter print(a@b) print(dot(a,b)) # Matrise-vektor multiplikasjon print(A@b) # Komponentvise operasjoner A = array([[1,2],[3,4]]) B = array([[0,pi/2],[pi,3*pi/2]]) print(A**2) print(A*B) print(sin(B)) print(B/A) print(exp(A*B**2)) # min, max, sum, prod print(a.min()) print(a.max()) print(b.max()) print(A.max(0)) print(sum(a)) print(prod(a)) # lengde av vektor p? to forskjellige m?ter print(sqrt(sum(a**2))) print(linalg.norm(a)) # plotting x = linspace(-1,1,100) plt.plot(x,x**2) plt.plot(x,x**4) plt.axis('equal') plt.show() # Sette sammen matriser A = array([[1,2,3],[1,2,3],[1,2,3]]) B = array([[4,5,6],[4,5,6],[4,5,6]]) C = array([[7,8,9],[7,8,9],[7,8,9]]) D = array([[10,11,12],[10,11,12],[10,11,12]]) print(block([[A,B]])) print(block([[A],[B]])) print(block([[A,B],[C,D]])) # Trekke ut en del av matrisen print(A[1:3,1:3]) # Definere nyttige matriser print(eye(3)) print(zeros((3,3))) print(ones((3,3)))