Page MenuHomec4science

main.cpp
No OneTemporary

File Metadata

Created
Fri, Nov 1, 06:28

main.cpp

/**
* @file main.cpp
* @author Sergio Hernandez
* This file is part of the Conjugate Gradient Project
*
* Ths is the entry point of the project.
*
*/
#include <iostream>
#include<vector>
#include "CG_Serial.hpp"
int main() {
//TODO Temporal - using Tridiagonal matrix
int N = 1000;
std::vector<double> A(N*N,0.0);//Filled with 0.0
std::vector<double> b(N, 1.0);//Filled with 1.0
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (j == i+1){
if (i < N-1 ) {
A[i + N * j] = 1.0; //Upper
}
}
if (j == i-1){
if (i > 0 ){
A[i+N*j] = 1.0; //Downer
}
}
if (j == i){
A[i+N*j] = -2.0; //Main diagonal
}
}
}
double tol = 1e-10;
int max_iter = 10*N;
std::vector<double> x_0 (N,0.0);
CG_Serial solver = CG_Serial(A,b,tol,max_iter);
std::tuple<std::vector<double>,int> result = solver.computeCG(x_0);
std::vector<double> x = std::get<0>(result);
int k = std::get<1>(result);
std::cout << "Solution was found after : "<<k<<" iterations.\n";
std::cout<<"Solution x: \n";
for(int i=0;i<x.size();i++){
std::cout<< x[i]<<"\n";
}
return 0;
}

Event Timeline