| 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 |
#ifdef HAVE_CONFIG_H |
| 93 |
# include <config.h> |
| 94 |
#endif |
| 95 |
|
| 96 |
#include <stdlib.h> |
| 97 |
#include <stdio.h> |
| 98 |
#include <math.h> |
| 99 |
|
| 100 |
#ifdef HAVE_UNISTD_H |
| 101 |
# include <unistd.h> |
| 102 |
#endif |
| 103 |
|
| 104 |
#include "ls_matrix.h" |
| 105 |
|
| 106 |
|
| 107 |
#define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
int *nr_ivector(long nl, long nh) |
| 113 |
{ |
| 114 |
int *v; |
| 115 |
|
| 116 |
v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int))); |
| 117 |
return v-nl+NR_END; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
double **nr_matrix(long nrl, long nrh, long ncl, long nch) |
| 123 |
|
| 124 |
{ |
| 125 |
long i, nrow=nrh-nrl+1, ncol=nch-ncl+1; |
| 126 |
double **m; |
| 127 |
|
| 128 |
|
| 129 |
m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*))); |
| 130 |
|
| 131 |
if (!m) |
| 132 |
{ |
| 133 |
fprintf(stderr, "Memory failure in routine 'nr_matrix'.\n"); |
| 134 |
exit(1); |
| 135 |
} |
| 136 |
|
| 137 |
m += NR_END; |
| 138 |
m -= nrl; |
| 139 |
|
| 140 |
|
| 141 |
m[nrl] = (double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double))); |
| 142 |
if (!m[nrl]) |
| 143 |
{ |
| 144 |
fprintf(stderr, "Memory failure in routine 'matrix'\n"); |
| 145 |
exit(1); |
| 146 |
} |
| 147 |
|
| 148 |
m[nrl] += NR_END; |
| 149 |
m[nrl] -= ncl; |
| 150 |
|
| 151 |
for (i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; |
| 152 |
|
| 153 |
|
| 154 |
return m; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
void nr_free_ivector(int *v, long nl ) |
| 159 |
{ |
| 160 |
free( (char *) (v+nl-NR_END)); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
void nr_free_matrix(double **m, long nrl, long nrh, long ncl, long nch) |
| 165 |
|
| 166 |
{ |
| 167 |
free((char *) (m[nrl]+ncl-NR_END)); |
| 168 |
free((char *) (m+nrl-NR_END)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
int nr_gaussj(double **a, int n, double **b, int m) |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
{ |
| 182 |
int *indxc, *indxr, *ipiv; |
| 183 |
int i, icol = 0, irow = 0, j, k, l, ll; |
| 184 |
double big, dum, pivinv, temp; |
| 185 |
|
| 186 |
int bexists = ((m != 0) || (b == 0)); |
| 187 |
|
| 188 |
indxc = nr_ivector(1,n); |
| 189 |
indxr = nr_ivector(1,n); |
| 190 |
ipiv = nr_ivector(1,n); |
| 191 |
|
| 192 |
for (j=1;j<=n;j++) ipiv[j] = 0; |
| 193 |
|
| 194 |
for (i=1;i<=n;i++) |
| 195 |
{ |
| 196 |
big = 0.0; |
| 197 |
for (j=1;j<=n;j++) |
| 198 |
if (ipiv[j] != 1) |
| 199 |
for (k=1;k<=n;k++) |
| 200 |
{ |
| 201 |
if (ipiv[k] == 0) |
| 202 |
{ |
| 203 |
if (fabs(a[j][k]) >= big) |
| 204 |
{ |
| 205 |
big = fabs(a[j][k]); |
| 206 |
irow = j; |
| 207 |
icol = k; |
| 208 |
} |
| 209 |
} |
| 210 |
else |
| 211 |
if (ipiv[k] > 1) { |
| 212 |
nr_free_ivector(ipiv,1 ); |
| 213 |
nr_free_ivector(indxr,1 ); |
| 214 |
nr_free_ivector(indxc,1 ); |
| 215 |
return -1; |
| 216 |
} |
| 217 |
} |
| 218 |
++(ipiv[icol]); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
if (irow != icol) |
| 230 |
{ |
| 231 |
for (l=1;l<=n;l++) SWAP(a[irow][l],a[icol][l]) |
| 232 |
if (bexists) for (l=1;l<=m;l++) SWAP(b[irow][l],b[icol][l]) |
| 233 |
} |
| 234 |
indxr[i] = irow; |
| 235 |
indxc[i] = icol; |
| 236 |
if (fabs(a[icol][icol]) < 1E-8) { |
| 237 |
nr_free_ivector(ipiv,1 ); |
| 238 |
nr_free_ivector(indxr,1 ); |
| 239 |
nr_free_ivector(indxc,1 ); |
| 240 |
return -1; |
| 241 |
} |
| 242 |
pivinv = 1.0/a[icol][icol]; |
| 243 |
a[icol][icol] = 1.0; |
| 244 |
for (l=1;l<=n;l++) a[icol][l] *= pivinv; |
| 245 |
if (bexists) for (l=1;l<=m;l++) b[icol][l] *= pivinv; |
| 246 |
for (ll=1;ll<=n;ll++) |
| 247 |
if (ll != icol ) |
| 248 |
{ |
| 249 |
dum = a[ll][icol]; |
| 250 |
a[ll][icol] = 0.0; |
| 251 |
for (l=1;l<=n;l++) a[ll][l] -= a[icol][l]*dum; |
| 252 |
if (bexists) for (l=1;l<=m;l++) b[ll][l] -= b[icol][l]*dum; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
for (l=n;l>=1;l--) |
| 262 |
{ |
| 263 |
if (indxr[l] != indxc[l]) |
| 264 |
for (k=1;k<=n;k++) |
| 265 |
SWAP(a[k][indxr[l]],a[k][indxc[l]]) |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
nr_free_ivector(ipiv,1 ); |
| 271 |
nr_free_ivector(indxr,1 ); |
| 272 |
nr_free_ivector(indxc,1 ); |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
void nr_copymat(double **orig, int n, double **copy) |
| 278 |
|
| 279 |
{ |
| 280 |
long i, j; |
| 281 |
|
| 282 |
if ((orig==0)||(copy==0)||(n==0)) return; |
| 283 |
|
| 284 |
for (i=1;i<=n;i++) |
| 285 |
for (j=1;j<=n;j++) |
| 286 |
copy[i][j] = orig[i][j]; |
| 287 |
} |
| 288 |
|
| 289 |
void nr_multmat(double **m1, int n, double **m2, double **prod) |
| 290 |
{ |
| 291 |
long i, j, k; |
| 292 |
|
| 293 |
if ((m1==0)||(m2==0)||(prod==0)||(n==0)) return; |
| 294 |
|
| 295 |
for (i=1;i<=n;i++) |
| 296 |
for (j=1;j<=n;j++) |
| 297 |
{ |
| 298 |
prod[i][j] = 0.0; |
| 299 |
for(k=1;k<=n;k++) prod[i][j] += m1[i][k]*m2[k][j]; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
void nr_printmat(double **a, int n) |
| 306 |
{ |
| 307 |
int i,j; |
| 308 |
|
| 309 |
printf("\n"); |
| 310 |
for(i=1;i<=n;i++) |
| 311 |
{ |
| 312 |
for(j=1;j<=n;j++) |
| 313 |
printf("% 9.4f ", a[i][j]); |
| 314 |
printf("\n"); |
| 315 |
} |
| 316 |
printf("\n"); |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
void testmat( void ) |
| 322 |
{ |
| 323 |
double **mat1, **mat2, **mat3; |
| 324 |
double invmaxlong; |
| 325 |
int loop, i, j, n = 20; |
| 326 |
long maxlong = RAND_MAX; |
| 327 |
int maxloop = 2; |
| 328 |
|
| 329 |
invmaxlong = 1.0/(double)maxlong; |
| 330 |
mat1 = nr_matrix(1, n, 1, n ); |
| 331 |
mat2 = nr_matrix(1, n, 1, n ); |
| 332 |
mat3 = nr_matrix(1, n, 1, n ); |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
for(loop=0;loop<maxloop;loop++) |
| 337 |
{ |
| 338 |
if (loop != 0) |
| 339 |
for(i=1;i<=n;i++) |
| 340 |
for(j=1;j<=n;j++) |
| 341 |
mat1[i][j] = 2.0 - 4.0*invmaxlong*(double) rand(); |
| 342 |
|
| 343 |
printf("Original matrix:\n"); |
| 344 |
nr_printmat( mat1, n ); |
| 345 |
|
| 346 |
nr_copymat( mat1, n, mat2 ); |
| 347 |
|
| 348 |
i = nr_gaussj( mat2, n, 0, 0 ); |
| 349 |
|
| 350 |
if (i) printf("Singular matrix.\n"); |
| 351 |
|
| 352 |
printf("Inverted matrix:\n"); |
| 353 |
nr_printmat( mat2, n ); |
| 354 |
|
| 355 |
nr_multmat( mat1, n, mat2, mat3 ); |
| 356 |
|
| 357 |
printf("Original multiplied by inverse:\n"); |
| 358 |
nr_printmat( mat3, n ); |
| 359 |
|
| 360 |
|
| 361 |
#if 0 |
| 362 |
if (loop < maxloop-1) ; |
| 363 |
#endif |
| 364 |
} |
| 365 |
|
| 366 |
nr_free_matrix( mat1, 1, n, 1, n ); |
| 367 |
nr_free_matrix( mat2, 1, n, 1, n ); |
| 368 |
nr_free_matrix( mat3, 1, n, 1, n ); |
| 369 |
} |