def __pow__(self, other): # avoid cyclic import problems from linalg import inverse if not isinstance(other, int): raise ValueError('only integer exponents are supported') if not self.__rows == self.__cols: raise ValueError('only powers of square matrices are defined') n = other if n == 0: return eye(self.__rows) if n < 0: n = -n neg = True else: neg = False i = n y = 1 z = self.copy() while i != 0: if i % 2 == 1: y = y * z z = z*z i = i // 2 if neg: y = inverse(y) return y
def test_inverse_random(self): T = np.random.randn(100, 100) actual = inverse(T) expected = LA.inv(T) self.assertTrue(np.allclose(actual, expected))
def test_inverse_simple(self): T = np.array([ [2, 1, 1, 0], [4, 3, 3, 1], [8, 7, 9, 5], [6, 7, 9, 8] ]) actual = inverse(T) expected = LA.inv(T) self.assertTrue(np.allclose(actual, expected))
def onWheel(self, ev): cx, cy = self.canvas.canvasxy(ev.x, ev.y) sf = 1.1 if (ev.delta < 0): sf = 1 / sf # scale all objects on canvas self.canvas.zoom(cx, cy, sf) if self.img: imgsc = tuple(int(sf * c) for c in self.simg.size) ox, oy = self.canvas.canvasxy(0, 0) cw, ch = self.canvas.winfo_reqwidth(), self.canvas.winfo_reqheight( ) cow, coh = self.canvas.canvasxy(cw, ch) if imgsc[0] > cw and imgsc[1] > ch: isz = (cw, ch) else: isz = imgsc xf = self.canvas.xform() xf = deepcopy(xf) xf = inverse(xf) #self.simg = self.img.transform(self.img.size,Image.AFFINE,data=(xf[0][0],xf[0][1],xf[0][3],xf[1][0],xf[1][1],xf[1][3])) self.simg = self.img.transform(imgsc, Image.AFFINE, data=(xf[0][0], 0, 0, 0, xf[1][1], 0)) self.pimg = ImageTk.PhotoImage(self.simg) #self.canvas.itemconfig(self.imgcid, image = self.pimg) x, y = self.canvas.coords(self.imgcid) print(x, y, self.img.size, ox, oy, cow, coh) if self.imgcid: self.canvas.delete(self.imgcid) self.imgcid = self.canvas.create_image(0, 0, image=self.pimg, anchor=tk.NW) sys.stdout.flush()
import numpy as np import numpy.linalg as LA from linalg.cholesky import Cholesky from linalg import inverse if __name__ == "__main__": A = np.array([ [0.01, 0.010001, .5], [0.02, 0.020001, 0.], [0.03, 0.030001, 0.5], ]) T = A.T @ A b = np.array([.1, 2., .3]) actual = Cholesky(T).solve(b) expected = LA.solve(T, b) diff = np.linalg.norm(actual - expected) cond = np.linalg.norm(A) * np.linalg.norm(inverse(A)) print("Condition number {:.2f} - solution difference: {}".format( cond, diff)) A = np.random.randn(3, 3) T = A.T @ A actual = Cholesky(T).solve(b) expected = LA.solve(T, b) diff = np.linalg.norm(actual - expected) cond = np.linalg.norm(A) * np.linalg.norm(inverse(A)) print("Condition number {:.2f} - solution difference: {}".format( cond, diff))
def c2o(self, cx, cy): c2om = inverse(self.OtoC) o = vapply(c2om, (cx, cy, 0, 1)) return o[0], o[1]
#!/usr/bin/env python3 import linalg if __name__ == '__main__': resp = input( "What would you like to do?\n1)Find an inverse\n2)Find a determinant\n3)Solve a linear system of equations" "\n4)Multiply two matrices\n5)Find LU Decomposition\n6)Find dominant eigenvalue with accompanying eigenvector\n" ) if resp == '1': linalg.print_matrix(linalg.inverse(linalg.input_matrix())) elif resp == '2': print(linalg.determinant(linalg.input_matrix())) elif resp == '3': print_matrix(linalg.linear_system()) elif resp == '4': linalg.print_matrix( linalg.product( linalg.input_matrix(prompt='Input left matrix in order'), linalg.input_matrix(prompt='Input right matrix in order'))) elif resp == '5': lu = linalg.lu_decomposition(linalg.input_matrix()) linalg.print_matrix(lu[0]) linalg.print_matrix(lu[1]) elif resp == '6': lu = linalg.lu_decomposition(linalg.input_matrix()) value, vector = linalg.power_method( lu, int(input('How many iterations?\n')), linalg.input_matrix((len(lu[0]), 1), 'Input an estimate')) print('Eigenvalue: {}\nEigenvector: {}'.format( value, [row[0] for row in vector])) else: print('selection not valid')