def LU_decomp(n): h = 1 / (n + 1) x = np.linspace(0, 1, n + 2)[1:-1] q = h**2 * f(x) A = tridiag(n) t0 = time.time() init = pylib() LUdecomp = init.luDecomp( A) # LU-decomp., pivot permutation, row interchange (+1/-1) LUbacksub = init.luBackSubst(LUdecomp[0], LUdecomp[1], q) T = time.time() - t0 return LUbacksub, T, x
v[1:-1], elapsed_time = TDCMA(f, n, runs) # Write results to file with open(DATADIR + "TDCMA.csv", 'a') as file: file.write('{},{:.2e}\n'.format(n, elapsed_time)) # Run LU decomposition and back substitution ns = [10**i for i in range(1, lu_exponent + 1)] for n in ns: algo_time = 0 for run in range(1, runs + 1): h = 1. / (n + 1) x = np.linspace(0, 1, n + 2) u = f(x) * h**2 A = build_toeplitz(-1, 2, -1, n) lib = pylib() start = time.time() A, index, t = lib.luDecomp(A) lib.luBackSubst(A, index, u[1:-1]) algo_time += time.time() - start average_algo_time = algo_time / float(runs) # Write results to file with open(DATADIR + "LU_timing.csv", 'a') as file: file.write('{},{:.2e}\n'.format(n, average_algo_time)) # Write input parameters to file with open(DATADIR + 'last_run.txt', 'w') as file: file.write('exponent: {}\n'.format(exponent)) file.write('lu_exponent: {}\n'.format(lu_exponent)) file.write('runs: {}\n'.format(runs))
# coding=utf-8 # Code to test and demonstrate the adaptive-step rk4 found in the library. #Written by Kyrre Ness Sjøbæk import computationalLib, numpy lib = computationalLib.pylib() import math def derivsSHM(y, t): """ Example derivs for solving SHM equation x'' + x = 0 which can be expressed as two coupled diff eq's for the variables x and v: x' = v v' = - x Input: - Array y[] which contains the values for x, v at time t - Array t, the time to evaluate at (useful for external forces etc.) I have chosen the following convention: - y[0] = x(t) - y[1] = v(t) Output: - Array containing the derivatives at point t, same convention as with y[]: return[0] = x'(t)
if len(sys.argv) == 1: print "Number of integration points:" n = int(sys.stdin.readline()) print "Integration limits (a, b)" (a,b) = sys.stdin.readline().split(",") a = int(a) b = int(b) elif len(sys.argv) == 4: n = int(sys.argv[1]) a = int(sys.argv[2]) b = int(sys.argv[3]) else: print "Usage: python", sys.argv[0], "N a b" sys.exit(0) #Definitions m = pylib(inputcheck=False,cpp=False) def integrand(x): return 4./(1. + x*x) #Integrate with Gaus-legendre! (x,w) = m.gausLegendre(a,b,n) int_gaus = numpy.sum(w*integrand(x)) #Final output print "integration of f(x) 4/(1+x**2) from",a,"to",b,"with",n,"meshpoints:" print "Gaus-legendre:", int_gaus print "Trapezoidal: ", m.trapezoidal(a,b,n, integrand) print "Simpson: ", m.simpson(a,b,n,integrand)
# coding=utf-8 #Simple matrix inversion example #Translated to Python by Kyrre Ness Sjøbæk import numpy, computationalLib, math myLib = computationalLib.pylib(inputcheck=False, cpp=False) A = numpy.array([[1,3,4],[3,4,6],[4,6,8]],numpy.double) print "Initial matrix A:\n", A #LU-decompose A matr = A.copy() (matr,index,d) = myLib.luDecomp(matr); print "LU-decomposed matrix:\n", matr print "Index array:\n", index print "Parity:\n", d #Invert A column-by-column: Ainv = numpy.zeros((3,3),numpy.double) for j in xrange(3): col = numpy.zeros(3) col[j] = 1; Ainv[:,j] = myLib.luBackSubst(matr,index,col) print "Inverse of A:\n", Ainv #Test matrix inversion: AxAinv should be identity test = True ZERO = 1.0E-10
const_1 = -2.0 * const_2 orb_factor = orb_l * (orb_l + 1) #Calculate array of potential values V = numpy.zeros(max_step + 1, numpy.double) for i in xrange(max_step + 1): r = r_min + i * step V[i] = potential(r) + orb_factor / r**2 #(include centrifugal term) #Calculate elements of the matrix to be diagonalized d = numpy.zeros(max_step, numpy.double) #diagonal elements e = numpy.zeros(max_step, numpy.double) #off-diagonal elements z = numpy.zeros( (max_step, max_step), numpy.double) #matrix for eigenvectors (only used as a dummy-argument to #tqli() in the current version of this program) for i in xrange(max_step): d[i] = const_1 + V[i + 1] e[i] = const_2 z[i, i] = 1 #(identity matrix) #Diagonalize and obtain eigenvalues. The eigenvalues are stored in d. computationalLib.pylib(cpp=False).tqli(d, e, z) #Sort the eigenvalues (smallest to largest) d = numpy.msort(d) #Output to file output()
# coding=utf-8 # Code to test and demonstrate ODE solvers in the library. # In order to use rk2 or euler instead of rk4, just change rk4 -> rk2 # or rk4 -> euler in the library function calls #Written by Kyrre Ness Sjøbæk import computationalLib, numpy lib = computationalLib.pylib() def derivsSHM(y,t): """ Example derivs for solving SHM equation x'' + x = 0 which can be expressed as two coupled diff eq's for the variables x and v: x' = v v' = - x Input: - Array y[] which contains the values for x, v at time t - Array t, the time to evaluate at (useful for external forces etc.) I have chosen the following convention: - y[0] = x(t) - y[1] = v(t) Output: - Array containing the derivatives at point t, same convention as with y[]: return[0] = x'(t) return[1] = v'(t)
step = (r_max - r_min) / max_step const_2 = -1.0 / step**2 const_1 = -2.0 * const_2 orb_factor = orb_l * (orb_l + 1) #Calculate array of potential values V = numpy.zeros(max_step+1,numpy.double) for i in xrange(max_step+1): r = r_min + i*step V[i] = potential(r) + orb_factor/r**2 #(include centrifugal term) #Calculate elements of the matrix to be diagonalized d = numpy.zeros(max_step,numpy.double) #diagonal elements e = numpy.zeros(max_step,numpy.double) #off-diagonal elements z = numpy.zeros((max_step,max_step),numpy.double) #matrix for eigenvectors (only used as a dummy-argument to #tqli() in the current version of this program) for i in xrange(max_step): d[i] = const_1 + V[i+1] e[i] = const_2 z[i,i] = 1 #(identity matrix) #Diagonalize and obtain eigenvalues. The eigenvalues are stored in d. computationalLib.pylib(cpp=False).tqli(d,e,z) #Sort the eigenvalues (smallest to largest) d = numpy.msort(d) #Output to file output()
# coding=utf-8 #Simple matrix inversion example #Translated to Python by Kyrre Ness Sjøbæk import numpy, computationalLib, math myLib = computationalLib.pylib(inputcheck=False, cpp=False) A = numpy.array([[1, 3, 4], [3, 4, 6], [4, 6, 8]], numpy.double) print "Initial matrix A:\n", A #LU-decompose A matr = A.copy() (matr, index, d) = myLib.luDecomp(matr) print "LU-decomposed matrix:\n", matr print "Index array:\n", index print "Parity:\n", d #Invert A column-by-column: Ainv = numpy.zeros((3, 3), numpy.double) for j in xrange(3): col = numpy.zeros(3) col[j] = 1 Ainv[:, j] = myLib.luBackSubst(matr, index, col) print "Inverse of A:\n", Ainv #Test matrix inversion: AxAinv should be identity test = True ZERO = 1.0E-10