Cholesky Decomposition হচ্ছে একটি বিশেষ পদ্ধতি যার সাহায্যে একটি symmetric, positive definite matrix কে দুটি বিশেষ matrices এর গুনফল হিসেবে প্রকাশ করা যায়। এই ২ টি বিভাজিত ম্যাট্রিক্স এর একটি Lower Triangular Matrix (L) এবং অন্যটি তার Transpose (LT)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<math.h> | |
#define size 4 | |
int cholesky(){ | |
float A[size][size]= {{16,4,4,-4},{4,10,4,2},{4,4,6,-2},{-4,2,-2,4}}; | |
float L[size][size]={0}; | |
float LT[size][size]={0}; | |
float b[size]={32,26,20,-6}; | |
int i,j,k; | |
float x[size]={0}; | |
float z[size]={0}; | |
float temp; | |
/*Implementation of Cholesky algorithm*/ | |
for(i=0;i<size;i++){ | |
for(j=0;j<=i;j++){ | |
temp=0; | |
if(j==i){ | |
for(k=0;k<j;k++){ | |
temp = temp +(L[j][k])*(L[j][k]); | |
} | |
L[j][j]=(sqrt((A[j][j]-temp))); | |
}else { | |
for(k=0;k<j;k++){ | |
temp=temp +((L[i][k]*L[j][k])); | |
} | |
//L[i][j]=(1/(L[j][j]))*((A[i][j])-temp); | |
L[i][j]=((A[i][j])-temp)/(L[j][j]); | |
} | |
if(i<j){ | |
L[i][j]=0; | |
} | |
} | |
} | |
/*LT = Transpose of L*/ | |
for(i=0;i<size;i++){ | |
for(j=0;j<size;j++){ | |
LT[i][j]=L[j][i]; | |
} | |
} | |
temp = 0; | |
/* forward substitutionLz=b*/ | |
for(i=0; i<size; i++){ | |
z[i]=b[i]; | |
for(j=0; j<i; j++){ | |
z[i]= z[i]-L[i][j]*z[j]; | |
} | |
z[i] = z[i]/LT[i][i]; | |
} | |
/* backward substitution LTx=z*/ | |
for(i=size-1; i>=0; i--) { | |
x[i]= z[i]; | |
for(j=i+1; j<size; j++) | |
{ | |
x[i]= x[i]-LT[i][j]*x[j]; | |
} | |
x[i] = x[i]/LT[i][i]; | |
} | |
return 0; | |
} | |
int main(){ | |
cholesky(); | |
return 0; | |
} |
October 20, 2017
0 comments:
Post a Comment