Hello
I write program that can solve linear equations with LUP decomposition method.I know that my algorithm is true and every part of my program do what I want. when I call function lupDecomposition() inside the lupSolve() the program crash.This is my related code:
int makeUpperDiagonal(Matrix * src){
int i,j;
for(i = 0;i < src->rowCount;i++){
for(j = 0; j <src->colCount;j++){
if(i > j)
src->elements[i][j] = 0;
}
}
return 0;
}
int makeLowerDiagonal(Matrix * src){
int i,j;
for(i = 0;i < src->rowCount;i++){
for(j = 0; j< src->colCount; j++){
if(i < j)
src->elements[i][j] = 0;
}
}
return 0;
}
int lupDecompose(Matrix* mat,MyVector* p,Matrix* u,Matrix* l){
int i,k,j,index;
double pivot;
for(i= 0; i< mat->colCount-1;i++){
p->elements[i] = i;
}
for( k = 0; k < mat->colCount;k++){
pivot = 0;
for( i = k; i < mat->colCount;i++){
if(mat->elements[i][k] > pivot){
pivot = mat->elements[i][k];
index = i;
}
}
// if(pivot == 0){
// printf("Singular Matrix Entered");
// }
vectorItemExchange(p,k,index);
for(i = 0; i < mat->colCount;i++){
itemEchange(mat,k,i,index,i);
}
for(i = k+1; i < mat->colCount;i++){
mat->elements[i][k] = mat->elements[i][k]/mat->elements[k][k];
for(j = k+1;j < mat->colCount;j++){
mat->elements[i][j]= mat->elements[i][j] - mat->elements[i][k] * mat->elements[k][j];
}
}
}
//matrixPrint(mat);
printf("\n");
for(i = 0; i < mat->rowCount;i++){ //copying main matrix item to u and l
for(j = 0; j< mat->colCount;j++){
u->elements[i][j] = mat->elements[i][j];
l->elements[i][j] = mat->elements[i][j];
}
}
printf("\n");
makeUpperDiagonal(u);
makeLowerDiagonal(l);
matrixPrint(u);
printf("\n");
matrixPrint(l);
printf("\n");
return 0;
}
int lupSolve(Matrix* mat,MyVector* b,MyVector* x){
int i,j;
double t;
MyVector* y;
y = malloc(sizeof(MyVector));
newVector(mat->rowCount,y);
MyVector* p;
p = malloc(sizeof(MyVector));
newVector(3,p);
Matrix* u;
u = malloc(sizeof(Matrix));
newMatrix(3,3,u);
Matrix* l;
l = malloc(sizeof(Matrix));
newMatrix(3,3,l);
[B]lupDecompose(mat,p,u,l);
printf("I am here");[/B]
for(i= 0; i < mat->rowCount;i++){
for(j = 0; j < i-1;j++){
t = l->elements[i][j] * y->elements[j];
}
int num;
num = p->elements[i];
y->elements[i] = b->elements[num] - t;
}
for(i = mat->rowCount; i > 1 ;i--){
for(j = i+1; j < mat->rowCount ;j++){
t = u->elements[i][j] * x->elements[j];
}
x->elements[i] = (y->elements[i] - t) / (u->elements[i][i]);
}
return 0;
}
I aspected that the statement "I am here" in function lupSolve() be written on console but it seems that the program running is stopped without any error