From 24f9f88af51e75d1d934ea8bfa164bb4d6bf4f36 Mon Sep 17 00:00:00 2001 From: Janita Willumsen Date: Sat, 23 Sep 2023 15:13:24 +0200 Subject: [PATCH] Finish problem 5 and 6 --- src/main.cpp | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index e69de29..3a55c90 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -0,0 +1,127 @@ +/** @file main.cpp + * @brief Main program for Project 2 + * + * The program performs the Jacobi rotation method. + * The size of the matrix, and number of transformations + * performed are written to file. + * Eigenvector correstonding to the 3 smallest eigenvalues + * for matrices of size 6x6 and 100x100 are written to file. + * + * @author Cory Alexander Balaton (coryab) + * @author Janita Ovidie Sandtrøen Willumsen (janitaws) + * @bug No known bugs + */ +#include +#include +#include + +#include "utils.hpp" +#include "matrix.hpp" +#include "jacobi.hpp" + + +void write_transformation(int N, bool dense) +{ + std::ofstream ofile; + + ofile.open("../latex/output/transform_dense.csv"); + ofile << "N,T" << std::endl; + + // Increase size of matrix, start at 5 to avoid logic_error of N=4 + for (int i = 5; i <= N; i++) { + arma::mat A = arma::mat(i, i).randn(); + A = arma::symmatu(A); + arma::vec eigval; + arma::mat eigvec; + + int iters; + bool converged; + + jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged); + + // Write size, number of iterations, and converge status to file + ofile << i << "," << iters << std::endl; + } + ofile.close(); +} + + +void write_transformation(int N) +{ + std::ofstream ofile; + + double h; + double a, d; + + ofile.open("../latex/output/transform_tridiag.csv"); + ofile << "N,T" << std::endl; + + // Increase size of matrix, start at 5 to avoid logic_error of N=4 + for (int i = 5; i <= N; i++) { + h = 1. / (double) (i+1); + a = -1. / (h*h), d = 2. / (h*h); + + arma::mat A = create_symmetric_tridiagonal(i, a, d); + arma::vec eigval; + arma::mat eigvec; + + int iters; + bool converged; + + jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged); + + // Write size, number of iterations, and converge status to file + ofile << i << "," << iters << std::endl; + } + ofile.close(); +} + + +void write_eigenvec(int N) +{ + double h = 1. / (double) (N+1); + double a = -1. / (h*h); + double d = 2. / (h*h); + double x = 0.; + + arma::mat A = create_symmetric_tridiagonal(N, a, d); + arma::vec eigval; + arma::mat eigvec; + + int iters; + bool converged; + + jacobi_eigensolver(A, 10e-14, eigval, eigvec, 100000, iters, converged); + + std::ofstream ofile; + ofile.open("../latex/output/eigenvector_" + std::to_string(N) + ".csv"); + ofile << "x,Vector 1,Vector 2,Vector 3" << std::endl; + + ofile << scientific_format(0., 16) << "," + << scientific_format(0., 16) << "," + << scientific_format(0., 16) << "," + << scientific_format(0., 16) << std::endl; + + for (int i = 0; i < N; i++) { + x += h; + ofile << scientific_format(x, 16)<< "," + << scientific_format(eigvec(i,0), 16) << "," + << scientific_format(eigvec(i,1), 16) << "," + << scientific_format(eigvec(i,2), 16) << std::endl; + } + ofile << scientific_format(1., 16) << "," + << scientific_format(0., 16) << "," + << scientific_format(0., 16) << "," + << scientific_format(0., 16) << std::endl; + ofile.close(); +} + + +int main() +{ + write_transformation(100); + write_transformation(100, true); + write_eigenvec(6); + write_eigenvec(100); + return 0; +}