def steepest_descent_conjugate_gradient_compare(): n=100; random_matrix = gen_diagdom_sym_matrix(n) b = convert_vec_mat(matrix_mult(random_matrix,[[1]] * n)) #the solution should be a vector of 1s. (solution1,iterCount1) = matrix_solve_steepest_descent(random_matrix,b,0.0001,10000,True) (solution2, iterCount2) = matrix_solve_conjugate_gradient(random_matrix, b, 0.0001, 10000, True) print("Steepest Descent " + str(n) + "x" + str(n) + ": Absolute Error=" + str(abs_error_2norm([1] * n,solution1)) + " Iteration Count=" + str(iterCount1)) print("Conjugate Gradient " + str(n) + "x" + str(n) + ": Absolute Error=" + str(abs_error_2norm([1] * n,solution2)) + " Iteration Count=" + str(iterCount2))
def steepest_descent_conjugate_gradient_gauss_seidel_compare(): n=100; random_matrix = gen_diagdom_sym_matrix(n) b = convert_vec_mat(matrix_mult(random_matrix,[[1]] * n)) #the solution should be a vector of 1s. start_time = time.time() (solution1,iterCount1) = matrix_solve_steepest_descent(random_matrix,b,0.00000000001,10000,True) time1 = time.time() - start_time start_time = time.time() (solution2, iterCount2) = matrix_solve_conjugate_gradient(random_matrix, b, 0.00000000001, 10000, True) time2 = time.time() - start_time start_time = time.time() (solution3, iterCount3) = matrix_solve_gauss_seidel(random_matrix, b, 0.00000000001, 10000, True) time3 = time.time() - start_time print("Steepest Descent " + str(n) + "x" + str(n) + ": Absolute Error=" + str(abs_error_2norm([1] * n,solution1)) + " Iteration Count=" + str(iterCount1) + " time=" + str(time1)) print("Conjugate Gradient " + str(n) + "x" + str(n) + ": Absolute Error=" + str(abs_error_2norm([1] * n,solution2)) + " Iteration Count=" + str(iterCount2) + " time=" + str(time2)) print("Gauss Seidel " + str(n) + "x" + str(n) + ": Absolute Error=" + str(abs_error_2norm([1] * n,solution3)) + " Iteration Count=" + str(iterCount3) + " time=" + str(time3))
def matrix_solve_gauss_seidel_test(): matrix1 = gen_rand_matrix(100) matrix2 = gen_sym_matrix(100) matrix3 = gen_diagdom_matrix(100) matrix4 = gen_diagdom_sym_matrix(100) matrices = [matrix1, matrix2, matrix3, matrix4] vector = [1] * len(matrix1) for i in range(len(matrices)): b = matrix_mult(matrices[i], convert_vec_mat(vector)) starttime = time.time() solution = matrix_solve_gauss_seidel(matrices[i], convert_vec_mat(b), 0.00001, 1000, getIterCount=True) print("Time: " + str(time.time() - starttime)) print("# of iterations: " + str(solution[1])) print(solution[0])