def withtau(n): plist = genprimelisttd(100000) t = triangular(n) while tau(t) < 500: # you must have at least 9 factors to have 500 divisors. incremented = False while (len(factorize(t, plist)) < 9): n += 1 incremented = True t = triangular(n) if not incremented: n += 1 t = triangular(n) print n print (list_divisors(t))
def least_squares(x, y, d, X): ''' Least squares polynomial Input: - x : x-axis known values - y : y-axis known values - d : degree of polynomial you want to use - X : query points x axis Output: - Y : values of the least squares polynomial at the query points ''' n = d + 1 # Build the Vandermonde matrix A = np.ones((len(x), n)) for i in range(1, n): A[:, i] = x * A[:, i - 1] # QR factorization (Q, R) = qr_factor(np.asmatrix(A)) # Least squares vector is vector z such that T*z = g1 g = Q.T * np.asmatrix(y).T g1 = g[0:n] T = R[0:n, 0:n] p = triangular(T, g1, 0) # Evaluation of polynomial in query points Y = np.polyval(np.flip(p, 0), X) return Y
def oldfashioned(): plist = genprimelisttd(100000) n = 5025 t = triangular(n) while count_divisors(t) < 500: # you must have at least 9 factors to have 500 divisors. incremented = False while (len(list_factors(t, plist)) < 11) or (len(set(list_factors(t, plist))) < 9): n += 1 incremented = True t = triangular(n) if not incremented: n += 1 t = triangular(n) print n print (list_divisors(t))
def pivotamento(quantidade_equacoes, coeficientes): incognitas = [0 for i in range(quantidade_equacoes)] for coluna in range(quantidade_equacoes): p = menor_linha(quantidade_equacoes, coeficientes, coluna) if p == -1: return -1, incognitas if p != coluna: troca_elementos(coeficientes, coluna, p) for linha in range(coluna + 1, quantidade_equacoes): efetua_operacoes(quantidade_equacoes, coeficientes, linha, coluna) if coeficientes[quantidade_equacoes - 1][quantidade_equacoes - 1] == 0: return -1, incognitas incognitas = triangular.triangular(quantidade_equacoes, coeficientes) return 0, incognitas
import numpy as np from triangular import triangular ################################################################################ # Lower triangular matrix A = np.matrix('12 0 0 0 0;' '18 38 0 0 0;' '51 4 57 0 0;' '53 3 8 2 0;' '31 26 4 16 60', float) # Known terms vector b = np.matrix('68; 9; 46; 43; 35') # Solve the system x = triangular(A, b, 1) # Check if np.allclose(b, A*x) == False: raise Exception('Lower triangular test failure') ################################################################################ # Upper triangular matrix A = np.matrix('12 48 2 51 49;' ' 0 38 32 51 5;' ' 0 0 57 2 31;' ' 0 0 0 2 53;' ' 0 0 0 0 60', float) # Known terms vector b = np.matrix('7; 31; 32; 34; 22') # Solve the system
'43 49 61 60 22 27;' '65 25 56 38 43 43;' '37 0 24 42 26 56;' '26 28 41 15 42 24;' '13 50 48 36 46 28 ', float) print('A = \n', A) # LU factorization (deep-copy of A because we need it or the "check") B, p = palu_factor(np.matrix(A)) print('B = \n', B) # Extract lower part L = np.matrix(np.tril(B, -1) + np.identity(6)) print('L = \n', L) # Extract upper part U = np.matrix(np.triu(B)) print('U = \n', U) # Check if np.allclose(A[p], L * U) == False: raise Exception('LU factorization test failure') # TEST: System Resolution # Ax = b => PAx = Pb => LUx = Pb # LUx = Pb b = np.matrix('33; 35; 2; 49; 53; 21') # Lk = Pb (note the permutation of b using the index vector p) k = triangular(L, b[p], 1) # Ux = k x = triangular(U, k, 0) # Check if np.allclose(b, A * x) == False: raise Exception('System Resolution test failure')
# Symmetric positive definite matrix A = np.matrix('5 1.2 0.3 -0.6;' '1.2 6 -0.4 0.9;' '0.3 -0.4 8 1.7;' '-0.6 0.9 1.7 10') print('A = \n', A) # Computation of the L factor L = chol_factor(A) print('L = \n', L) # Check if np.allclose(A, np.dot(L, L.transpose())) == False: raise Exception('QR factorizzation test failure') # TEST: System Resolution # Ax = LL'x = b b = np.matrix("68; 9; 45; 35") print('b = \n', b) # Lk = b k = triangular(L, b, 1) print('k = \n', k) # L'x = k x = triangular(L.transpose(), k, 0) print('x = \n', x) # Check b1 = np.dot(A, x) print('b1 = \n', b1) if np.allclose(b, b1) == False: raise Exception('System resolution failure')
A = np.matrix( '29 11 59 9 13;' '41 27 3 13 1;' '53 62 23 13 50;' '61 61 54 40 29;' '49 27 0 18 62', float) print('A = \n', A) # LU factorization (deep-copy of A because we need it or the "check") B = lu_factor(np.matrix(A)) print('B = \n', B) # Extract lower part L = np.matrix(np.tril(B, -1) + np.identity(5)) print('L = \n', L) # Extract upper part U = np.matrix(np.triu(B)) print('U = \n', U) # Check if np.allclose(A, L * U) == False: raise Exception('LU factorizzation test failure') # TEST: System Resolution # Ax = LUx = b b = np.matrix('68; 9; 45; 43; 35') # Lk = b k = triangular(L, b, 1) # Ux = k x = triangular(U, k, 0) # Check if np.allclose(b, A * x) == False: raise Exception('System resolution test failure')