def f(i, j): A = matrix.Mat([x]) + dx * ei(j) B = matrix.Mat([x]) a = A.A[0] b = B.A[0] val = (1.0 * (matrix.Mat(F(a)) - matrix.Mat(F(b))) * (1. / dx)) #print(val) return val.g(0, i)
def F(X): s = 0 for i in range(M): xi = x[i] yi = y[i] xx = matrix.Mat([xi]) tt = matrix.Mat([yi]) yy = f(X)(xx) val = abs(tt - yy)**2 s = s + val if verbose2: print("s=", s) return 1.0 * s / (M + 1)
def J(F, x, dx=.01): n = len(x) m = len(F(x)[0]) #print("|x|=",n,"|F(x).A[0]|=",m) def d(F, x, dx): I = matrix.Mat([[]]).identity(n) def ei(i): L = [] for j in range(n): L.append(I.g(i, j)) return matrix.Mat([L]) def f(i, j): A = matrix.Mat([x]) + dx * ei(j) B = matrix.Mat([x]) a = A.A[0] b = B.A[0] val = (1.0 * (matrix.Mat(F(a)) - matrix.Mat(F(b))) * (1. / dx)) #print(val) return val.g(0, i) return f G = matrix.Mat([[]]).zero(m, n) #print("n,m=",n,m) for j in range(n): for i in range(m): G.s(i, j, d(F, x, dx)(i, j)) return G
def RndMat(n, m, a, b): A = matrix.Mat([[]]).zero(n, m) for j in range(m): for i in range(n): v = random.uniform(a, b) A.s(i, j, v) return A
def LUPInvert(A): B = A.copy() n, m = B.shape assert (n == m) N = n Tol = 1e-8 flag, C, P = LUPDecompose(B, Tol) IA = matrix.Mat([[]]).zero(m, n) for j in range(N): for i in range(N): if abs(P.g(i, 0) - j) < 0.1: IA.s(i, j, 1.0) else: IA.s(i, j, 0.0) for k in range(i): IA.s(i, j, IA.g(i, j) - C.g(i, k) * IA.g(k, j)) for i in range(N - 1, -1, -1): for k in range(i + 1, N): IA.s(i, j, IA.g(i, j) - C.g(i, k) * IA.g(k, j)) IA.s(i, j, 1.0 * IA.g(i, j) / C.g(i, i)) return IA
def RndMat(n): A = matrix.Mat([[]]).zero(n,n) for j in range(n): for i in range(n): v = random.uniform(-1,1) A.s(i,j,v) return A
def Newtons_nonlinear(G, x0, N=20, epsilon=.1): F = lambda X: matrix.Mat(G(X)).transpose() k = 1 x = matrix.Mat([x0]) while k <= N: b_k = (-1 * F(x.A[0])) A_k = J(G, x.A[0], 0.01) #print("b_k:",b_k.shape) #print("A_k:",A_k.shape) y = (LU.solve(A_k, b_k)).transpose() x = x + y # x(n+1) - x(n) = y if abs(y) < epsilon: return x.flatten().A[0] break k = k + 1 print("Error-maxiters exceeded") return x.flatten().A[0]
def LUPDecompose(B, Tol): n, m = B.shape A = B.copy() assert (n == m) N = n P = matrix.Mat([[]]).zero(N + 1, 1) for i in range(N + 1): P.s(i, 0, i) for i in range(N): maxA = 0.0 imax = i for k in range(i, N): absA = abs(A.g(k, i)) if (absA > maxA): maxA = absA imax = k if (maxA < Tol): return 0 if (imax != i): j = P.g(i, 0) P.s(i, 0, P.g(imax, 0)) P.s(imax, 0, j) ptr = matrix.Mat([[]]).zero(N, 1) for k in range(N): ptr.s(k, 0, A.g(i, k)) for k in range(N): A.s(i, k, A.g(imax, k)) for k in range(N): A.s(imax, k, ptr.g(k, 0)) P.s(N, 0, P.g(N, 0) + 1) for j in range(i + 1, N): A.s(j, i, 1.0 * A.g(j, i) / A.g(i, i)) for k in range(i + 1, N): A.s(j, k, 1.0 * A.g(j, k) - A.g(j, i) * A.g(i, k)) return 1, A, P
def app(A, f): n, m = A.shape # B is [f(A[i,j])]_ij B = matrix.Mat([[]]).zero(n, m) for j in range(m): for i in range(n): v = A.g(i, j) w = f(v) B.s(i, j, w) return B
def F2(self, U): W = [] idx = 0 for l in range(self.L - 1): n = self.T[l] m = self.T[l + 1] idx2 = idx + n * m V = matrix.Mat([U[idx:idx2]]) Wl = V.reshape((n, m)) W.append(Wl) idx = idx2 self.W = W return
def GradientDescent(F, x0, eps=0.0001, eta=0.001, N=1000, verbose=False): if verbose: print(eps, eta, N, verbose) count = 0 x = matrix.Mat([x0]) while (count < N): dF = grad(F)(x.A[0]) if verbose: print(dF.A, ",") x_new = x - eta * dF if abs(x - x_new) < eps: break x = x_new.copy() count = count + 1 #print("count=",count,str(x),str(F(x.A[0]))) return x.A[0]
def d(F, x, dx): I = matrix.Mat([[]]).identity(n) def ei(i): L = [] for j in range(n): L.append(I.g(i, j)) return matrix.Mat([L]) def f(i, j): A = matrix.Mat([x]) + dx * ei(j) B = matrix.Mat([x]) a = A.A[0] b = B.A[0] val = (1.0 * (matrix.Mat(F(a)) - matrix.Mat(F(b))) * (1. / dx)) #print(val) return val.g(0, i) return f
def LUPSolve(A, b): B = A.copy() n, m = B.shape assert (n == m) N = n Tol = 1e-8 flag, C, P = LUPDecompose(B, Tol) p, q = b.shape assert ((p == N) and (q == 1)) x = matrix.Mat([[]]).zero(N, 1) for i in range(N): x.s(i, 0, b.g(P.g(i, 0), 0)) for k in range(i): x.s(i, 0, x.g(i, 0) - C.g(i, k) * x.g(k, 0)) for i in range(N - 1, -1, -1): for k in range(i + 1, N): x.s(i, 0, x.g(i, 0) - C.g(i, k) * x.g(k, 0)) x.s(i, 0, 1.0 * x.g(i, 0) / C.g(i, i)) return x
import matrix import optimize as opt from copy import deepcopy from math import factorial as fa from math import sqrt ## def arr(self): ## return np.array(self.A) A = matrix.Mat([[5, 4, 5], [1, 2, 3], [4, 3, 2]]) I = A.identity(A.shape[0]) B = matrix.Mat([[5, 4], [1, 2], [2, 4]]) C = A.dot(B) b = matrix.Mat([[1, 3, 2]]).transpose() if 1: print("A =\n", A) print("I =\n", I) print("b =\n", b) print('-' * 30) n, m = A.shape names = list(map(lambda i: 'x%d' % i, range(n))) for i in range(n): s = '' for j in range(m): t = str(A.g(i, j)) + '*' + names[j] + ' + ' s = s + t s = s[:-3] t = str(b.g(i, 0)) s = s + ' = ' + t
import matrix import LU import random def RndMat(n): A = matrix.Mat([[]]).zero(n,n) for j in range(n): for i in range(n): v = random.uniform(-1,1) A.s(i,j,v) return A A = matrix.Mat([[1,2,3],[3,2,1],[4,2,1]]) b = matrix.Mat([[1,2,3]]).transpose() Tol = 1e-8 x = LU.solve(A,b) print("A = ",A) print("b = ",b) print("x = LUPSolve(A,b) =",x) print("A.dot(x)=",A.dot(x)) B = LU.inv(A) print("B = LUPInvert(A) = ",B) print("A.dot(B) = ",A.dot(B)) print("LUPDeterminant(A) =",LU.det(A)) print("A.det() = ", A.det()) A = RndMat(9) import datetime print("A = RndMat(9)") t0 = datetime.datetime.now() print("LU.det(A) =",LU.det(A))
def predict(self, x): return self.forward(matrix.Mat([x + [1]])).A[0][:-1]
import matrix from math import sqrt A = matrix.Mat([[2,4,8],[2,1,5],[-2,6,10]]) print("A=",A) print("Using cofactor expansion for determinant:") print("A.det()=",A.det()) A = matrix.Mat([[1,4,7],[3,0,5]]) B = matrix.Mat([[2,4,1],[1,2,3]]) print("A=",A) print("B=",B) print("A.shape = ",A.shape) print("B.shape = ",B.shape) print("M(n,m) - n x m matrices") print("A.inner(B) = (B.adj().dot(A)).tr()") print("A.inner(B) =",A.inner(B)) print("|A-B| = (A-B).norm_inner() =",(A-B).norm_inner()) print("d(A,B) = A.dist(B) = |A-B| =",A.dist(B)) print("abs(A) =",abs(A)) print("abs(B) =",abs(B)) C = matrix.Mat([[1,2],[3,2],[4,5]]) print("C=",C) print("C.shape = ",C.shape) D = A.dot(C) # not A * C in implementation! print("D = A * C = A.dot(C) =",D) print("4*A = ",4*A)
def ei(i): L = [] for j in range(n): L.append(I.g(i, j)) return matrix.Mat([L])