diff --git a/Cholesky/CMakeLists.txt b/Cholesky/CMakeLists.txt new file mode 100644 index 0000000..e7fcacc --- /dev/null +++ b/Cholesky/CMakeLists.txt @@ -0,0 +1,10 @@ +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + +# The next lines represent a list of all the executables in the folder (chapter) +# There is typically one executable per exercise. +# As you solve exercises, you need to add them here. +# The syntax is add_executable(executable_name source_file_name) + + +add_executable(main main.cpp Cholesky.cpp Functions.cpp) + \ No newline at end of file diff --git a/Cholesky/Cholesky.cpp b/Cholesky/Cholesky.cpp new file mode 100644 index 0000000..9828037 --- /dev/null +++ b/Cholesky/Cholesky.cpp @@ -0,0 +1,39 @@ +#include "Cholesky.hpp" + +//Default constructor// +Cholesky::Cholesky(double **m_A, double *m_b, int m_size) { + A=m_A; + b=m_b; + size=m_size; + Cholesky_matrices Structure=Cholesky_L(A,b,size); + L=Structure.L_matrix; + L_T=Structure.L_T_matrix; + b_star=Structure.b_star_vector; +} + +//Copy constructor// +Cholesky::Cholesky(const Cholesky &otherCholesky) { + A=otherCholesky.A; + b=otherCholesky.b; + b_star=otherCholesky.b_star; + size=otherCholesky.size; + L=otherCholesky.L; + L_T=otherCholesky.L_T; +} + +double** Cholesky::get_L() { + return L; +} + +double** Cholesky::get_L_T() { + return L_T; +} + +double* Cholesky::get_b_star(){ + return b_star; +} + +double* Cholesky::Solve() { + double* z=Up(L_T,b_star,size); + return z; +} \ No newline at end of file diff --git a/Cholesky/Cholesky.hpp b/Cholesky/Cholesky.hpp new file mode 100644 index 0000000..1a9f4f2 --- /dev/null +++ b/Cholesky/Cholesky.hpp @@ -0,0 +1,24 @@ +#ifndef CHOLESKYHEADERREF +#define CHOLESKYHEADERREF + +#include "Functions.hpp" + +class Cholesky{ +private: + int size; + double** A; + double* b; + double* b_star; + double** L; + double** L_T; + +public: + Cholesky(const Cholesky& otherCholesky); + Cholesky(double** m_A,double* m_b, int m_size); + double** get_L(); + double** get_L_T(); + double* get_b_star(); + double* Solve(); +}; + +#endif \ No newline at end of file diff --git a/Cholesky/Functions.cpp b/Cholesky/Functions.cpp new file mode 100644 index 0000000..ea76ffe --- /dev/null +++ b/Cholesky/Functions.cpp @@ -0,0 +1,292 @@ +#include +#include +#include +#include "Functions.hpp" +#include "Cholesky.hpp" + +double** AllocateMemory(int rows, int cols){ + /*Allocates memory for matrix A*/ + double** A; + A=new double* [rows]; + for (int i=0;iA[index][col]){ + index=i; + } + } + return index; +} + +void SwapRows (double** A,int last_index, int new_index, int size){ + double* temp=AllocateMemory(size); + for (int i=0;i-1;i--){ + x[i]+=b_star[i]; + for (int j=i+1;j=0;j--){ + x[i]-=L[i][j]*x[j]; + } + x[i]/=L[i][i]; + } + return x; +} + +double** Transpose(double** A,int size){ + double** temp=Copy(A,size); + for (int i=0;i +#include +#include +#include "Functions.hpp" +#include "Cholesky.hpp" + + +int main(int argc, char *argv[]){ + int size=3; + double** A=AllocateMemory(size,size); + double* b=AllocateMemory(size); + + A[0][0]=25;A[0][1]=15;A[0][2]=-5; + A[1][0]=15;A[1][1]=18;A[1][2]=0; + A[2][0]=-5;A[2][1]=0;A[2][2]=11; + + b[0]=1;b[1]=6;b[2]=4; + std::cout<<"Matrix A:"<