diff --git a/hw1-conjugate-gradient/conjugate_gradient.py b/hw1-conjugate-gradient/conjugate_gradient.py index 0938075c..75363f83 100644 --- a/hw1-conjugate-gradient/conjugate_gradient.py +++ b/hw1-conjugate-gradient/conjugate_gradient.py @@ -1,57 +1,60 @@ ### THIS PROGRAM RUNS THE CONJUGATE GRADIENT ALGORITHM -### IMPORT LIBRARIES +################## IMPORT LIBRARIES ################################ import numpy as np import scipy as sp #import matplotlib.pyplt as plt -### DEFINE FUNCTION : conjgrad -### DOES : CG algorithm + +############# DEFINE FUNCTION : conjgrad ########################### +################# DOES : CG algorithm ############################## def conjgrad(A, b, x, tolerance, max_iterations): # Dimension of problem dim = len(b) # Convert input to column vectors b.reshape(dim, -1) x.reshape(dim, -1) print(x.shape) # Initialization k = 0 r = b - np.einsum('ij,j', A, x) # b - A @ x p = r if (np.sqrt(np.sum(r**2)) < tolerance): return (x, k) # CG Algorithm for i in range(0, max_iterations): alpha = np.einsum('j,j',r,r) / np.einsum('j,j',p,np.einsum('ij,j', A, p)) x = x + alpha*p r_next = r - alpha*np.einsum('ij,j', A, p) beta = np.einsum('j,j',r_next,r_next)/np.einsum('j,j',r,r) r = r_next p = r + beta*p k = k + 1 if (np.sqrt(np.sum(r**2)) < tolerance): break return (x,k) # want to return x-array that minimizes S(x) -### INPUT ARGUMENTS + +################################ INPUT ARGUMENTS ################################################### #A = np.array([[2.0, 3.0, 5.0], [2.0, 3.0 , 6.0], [3.0, 6.0, 9.0]]) #b = np.array([2.0, 3.0, 5.0]) #x = np.array([10.0, 20.5, 0.0]).T # initial guess A = np.array([[4.0, 0], [1.0, 3.0]]) b = np.array([0.0, 1.0]) x = np.array([4, 4]) # initial guess max_iterations = 1000 tolerance = 0.001 -### RUN, PRINT RESULTS + +############################## RUN, PRINT RESULTS ################################################## result, k = conjgrad(A, b, x, tolerance, max_iterations) print("Minimum found at position ", result, " after ", k, " iterations.") diff --git a/hw1-conjugate-gradient/main.py b/hw1-conjugate-gradient/main.py new file mode 100644 index 00000000..818f5747 --- /dev/null +++ b/hw1-conjugate-gradient/main.py @@ -0,0 +1,32 @@ +### IMPORT LIBRARIES + +import numpy as np +import scipy as sp +import argparse + +#import quadratic_function +#import optimizer +#import conjugate_gradient + + +### DEFINE PARAMETERS / COEFFICIENTS + +A = np.array([[4.0, 0.0], [1.0, 3.0]]) +b = np.array([0.0, 1.0]) + + +### ARGPARSE EXAMPLE : TO DO + +parser = argparse.ArgumentParser(description='Process Minimization Schemes') +#parser.add_argument("A", help="Get the matrix A coefficients", +# type=string) + +parser.add_argument('A', metavar='N', type=float, nargs='+', + help='input matrix') +#parser.add_argument('b', metavar='--b', type=float, nargs='+', +# help='input vector') + +args = parser.parse_args() +print(args.A) +print(args.b) +