1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*main*/
void main(void)
{
Matrix B,s,x;
int i=2,j=3;
/*Matrix Dimensions*/
int m=4,l=1;
double val=1.0,d=2.0;
InitMatrix(&s,&m,&l);
InitMatrix(&B,&m,&m);
/*B Matrix*/
B.val[0]=3;
B.val[1]=2;
B.val[2]=-1;
B.val[3]=2;
B.val[4]=1;
B.val[5]=4;
B.val[6]=0;
B.val[7]=2;
B.val[8]=2;
B.val[9]=1;
B.val[10]=2;
B.val[11]=-1;
B.val[12]=1;
B.val[13]=1;
B.val[14]=-1;
B.val[15]=3;
/**********/
/*s Matrix*/
s.val[0]=-2;
s.val[1]=1;
s.val[2]=3;
s.val[3]=4;
/**********/
PrintMatrix(B);
printf("\n");
PrintMatrix(s);
printf("\n");
d=GetElement(B,m,m);
printf("%f",d);
printf("\n");
printf("\n");
x=LUsolver(&B,&s);
PrintMatrix(x);
}
/*Matrix struct*/
#include "Matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Allocation of a Matrix and set to 0
// Input: M - Pointer to Matrix
// m - Number of rows
// n - Number of columns
// Output: M - Allocated Matrix
void InitMatrix(Matrix *M,const int *m,const int *n)
{
M->alloc=1;
M->rows=*m;
M->cols=*n;
M->val=(double *)malloc((*m)*(*n)*sizeof(double));
}
//Deallocation of a Matrix if allocated
// Input: M - Pointer to Matrix
// Output: M - Deallocated Matrix
void DelMatrix(Matrix *M)
{
if (M->alloc==1)
{
M->alloc=0;
M->rows=0;
M->cols=0;
free(M->val);
M->val=NULL;
}
}
//Fills in an allocated Matrix with a double val
// Input: M - Matrix to be filled in
// val - the number to fill in the Matrix
// Output: M - Filled Matrix
void Number(const Matrix *M,const double *val)
{
int i;
if (M->alloc==1)
{
for(i=0;i<M->cols*M->rows;i++)
M->val[i]=*val;
}
}
//Returns the element (i,j) of Matrix M
// Input: M - Matrix
// k - row i
// l - column j
// Output: element (i,j)
double GetElement(const Matrix M,const int k,const int l)
{
int p,m;
m=M.rows;
p=(k-1)*m+(l-1);
return(M.val[p]);
}
//Changes the value of element (i,j) of Matrix M with a double
// Input: M - Matrix
// k - row i
// l - column j
// y - double to change element (i,j)
// Output: Matrix M with element (i,j) changed to double y
void PutElement(const Matrix *M,const int *k,const int *l,const double y)
{
int p,m,i,j;
m=M->rows;
i=*k,j=*l;
p=(i-1)*m+(j-1);
M->val[p]=y;
}
//Prints the Matrix M
// Input: M - Matrix
// Output: Printed Matrix
void PrintMatrix(const Matrix M)
{
int m=0,n=0,i=0,j=0,k,l;
double d;
m=M.rows;
n=M.cols;
for(i;i<m;i++)
{
k=i+1;
j=0;
for(j;j<n;j++)
{
l=j+1;
d=GetElement(M,k,l);
if(d>=0)
printf(" ");
printf("%f",d);
printf(" ");
}
printf("\n");
}
}
/*LUsolver*/
Matrix LUsolver(const Matrix *A,const Matrix *s)
{
Matrix L,U,y,x;
int i=1,j,k,m;
//assist variables d,w,c,z
double d,w,c,z=0,sum1,sum2,sum3,sum4;
m=A->rows;
InitMatrix(&L,&m,&m);
InitMatrix(&U,&m,&m);
InitMatrix(&y,&m,&i);
InitMatrix(&x,&m,&i);
Number(&L,&z);
Number(&U,&z);
/******************************************/
/*************LU FACTORIZATION*************/
/******************************************/
//l(1,1)
i=1;
d=GetElement(*A,i,i);
PutElement(&L,&i,&i,d);
//diag. of U
for(i=1;i<=m;i++)
{
PutElement(&U,&i,&i,1.0);
}
//first row of U
i=1;
d=GetElement(L,i,i);
for(j=2;j<=m;j++)
{
w=GetElement(*A,i,j);
z=w/d;
PutElement(&U,&i,&j,z);
}
//first col. of L
for(j=2;j<=m;j++)
{
w=GetElement(*A,j,i);
PutElement(&L,&j,&i,w);
}
//rest of the matrices L,U except l(n,n)
for(i=2;i<=m-1;i++)
{
sum1=0;
for(k=1;k<=i-1;k++)
sum1+=GetElement(L,i,k)*GetElement(U,k,i);
d=GetElement(*A,i,i)-sum1;
PutElement(&L,&i,&i,d);
for(j=i+1;j<=m;j++)
{
sum2=0;
for(k=1;k<=i-1;k++)
sum2+=GetElement(L,i,k)*GetElement(U,k,j);
w=(GetElement(*A,i,j)-sum2)/GetElement(L,i,i);
PutElement(&U,&i,&j,w);
sum3=0;
for(k=1;k<=i-1;k++)
sum3+=GetElement(L,j,k)*GetElement(U,k,i);
c=GetElement(*A,j,i)-sum3;
PutElement(&L,&j,&i,c);
}
}
//l(n,n)
sum4=0;
for(k=1;k<=m-1;k++)
sum4+=GetElement(L,m,k)*GetElement(U,k,m);
d=GetElement(*A,m,m)-sum4;
PutElement(&L,&m,&m,d);
/******************************************/
/******************************************/
/*******SOLUTION TO THE SYSTEM LUx=s*******/
/******************************************/
//assist variables d,w,c set to zero and k refers to the single column of a vector-matrix
d=0;w=0;c=0;z=0; /**/k=1;/**/
i=1;j=0;
//Forward Substitution, system Ly=s
d=GetElement(*s,i,i);
w=GetElement(L,i,i);
c=d/w;
PutElement(&y,&i,&i,c);
for(i=2;i<=m;i++)
{
sum1=0;
for(j=1;j<=i-1;j++)
{
d=GetElement(L,i,j);
w=GetElement(y,j,k);
sum1+=d*w;
}
d=GetElement(*s,i,k);
w=GetElement(L,i,i);
c=1/w;
z=c*(d-sum1);
PutElement(&y,&i,&k,z);
}
//Backward Substitution, system Ux=y
d=GetElement(y,m,k);
PutElement(&x,&m,&k,d);
for(i=m-1;i>=1;i--)
{
sum1=0;
for(j=i+1;j<=m;j++)
{
d=GetElement(U,i,j);
w=GetElement(x,j,k);
sum1+=d*w;
}
d=GetElement(y,i,k);
c=d-sum1;
PutElement(&x,&i,&k,c);
}
return (x);
}
|