Image Interpolation with Contour Stencils
|
00001 00016 #include <stdio.h> 00017 #include "basic.h" 00018 00033 int InvertMatrix(double *InverseData, double *AData, int N) 00034 { 00035 double *c = 0, *d = 0, *Aj, *Ak, *Inversek; 00036 double Temp, Scale, Sum; 00037 int i, j, k, Success = 0; 00038 00039 00040 if(!(c = (double *)Malloc(sizeof(double)*N)) 00041 || !(d = (double *)Malloc(sizeof(double)*N))) 00042 goto Catch; 00043 00044 for(k = 0, Ak = AData; k < N - 1; k++, Ak += N) 00045 { 00046 Scale = 0.0; 00047 00048 for(i = k; i < N; i++) 00049 if((Temp = fabs(Ak[i])) > Scale) 00050 Scale = Temp; 00051 00052 if(Scale == 0.0) 00053 { 00054 ErrorMessage("Singular matrix.\n"); 00055 goto Catch; /* Singular matrix */ 00056 } 00057 00058 for(i = k; i < N; i++) 00059 Ak[i] /= Scale; 00060 00061 for(Sum = 0.0, i = k; i < N; i++) 00062 Sum += Ak[i]*Ak[i]; 00063 00064 Temp = (Ak[k] >= 0.0)? sqrt(Sum) : -sqrt(Sum); 00065 Ak[k] += Temp; 00066 c[k] = Temp*Ak[k]; 00067 d[k] = -Scale*Temp; 00068 00069 for(j = k + 1, Aj = Ak + N; j < N; j++, Aj += N) 00070 { 00071 for(Scale = 0.0, i = k; i < N; i++) 00072 Scale += Ak[i] * Aj[i]; 00073 00074 Scale /= c[k]; 00075 00076 for(i = k; i < N; i++) 00077 Aj[i] -= Scale*Ak[i]; 00078 } 00079 } 00080 00081 d[N-1] = Ak[k]; 00082 00083 if(d[N-1] == 0.0) 00084 { 00085 ErrorMessage("Singular matrix.\n"); 00086 goto Catch; /* Singular matrix */ 00087 } 00088 00089 for(k = 0, Inversek = InverseData; k < N; k++, Inversek += N) 00090 { 00091 for(i = 0; i < N; i++) 00092 Inversek[i] = -AData[k]*AData[i]/c[0]; 00093 00094 Inversek[k] += 1.0; 00095 00096 for(j = 1, Aj = AData + N; j < N-1; j++, Aj += N) 00097 { 00098 for(Scale = 0.0, i = j; i < N; i++) 00099 Scale += Aj[i]*Inversek[i]; 00100 00101 Scale /= c[j]; 00102 00103 for(i = j; i < N; i++) 00104 Inversek[i] -= Scale*Aj[i]; 00105 } 00106 00107 Inversek[j] /= d[N-1]; 00108 00109 for(i = N - 2; i >= 0; i--) 00110 { 00111 for(Sum = 0.0, j = i + 1, Aj = AData + N*(i + 1); j < N; j++, Aj += N) 00112 Sum += Aj[i]*Inversek[j]; 00113 00114 Inversek[i] = (Inversek[i] - Sum)/d[i]; 00115 } 00116 } 00117 00118 Success = 1; /* Finished successfully */ 00119 Catch: /* Clean up */ 00120 Free(d); 00121 Free(c); 00122 return Success; 00123 }