GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
solv.c
Go to the documentation of this file.
00001 /*  solv.c    CCMATH mathematics library source code.
00002  *
00003  *  Copyright (C)  2000   Daniel A. Atkinson    All rights reserved.
00004  *  This code may be redistributed under the terms of the GNU general
00005  *  public license. ( See the gpl.license file for details.)
00006  * ------------------------------------------------------------------------
00007  */
00008 #include <stdlib.h>
00009 #include "ccmath.h"
00010 int solv(double *a, double *b, int n)
00011 {
00012     int i, j, k, lc;
00013 
00014     double *ps, *p, *q, *pa, *pd;
00015 
00016     double *q0, s, t, tq = 0., zr = 1.e-15;
00017 
00018     q0 = (double *)calloc(n, sizeof(double));
00019     for (j = 0, pa = a, pd = a; j < n; ++j, ++pa, pd += n + 1) {
00020         if (j) {
00021             for (i = 0, q = q0, p = pa; i < n; ++i, p += n)
00022                 *q++ = *p;
00023             for (i = 1; i < n; ++i) {
00024                 lc = i < j ? i : j;
00025                 for (k = 0, p = pa + i * n - j, q = q0, t = 0.; k < lc; ++k)
00026                     t += *p++ * *q++;
00027                 q0[i] -= t;
00028             }
00029             for (i = 0, q = q0, p = pa; i < n; ++i, p += n)
00030                 *p = *q++;
00031         }
00032         s = fabs(*pd);
00033         lc = j;
00034         for (k = j + 1, ps = pd; k < n; ++k) {
00035             if ((t = fabs(*(ps += n))) > s) {
00036                 s = t;
00037                 lc = k;
00038             }
00039         }
00040         tq = tq > s ? tq : s;
00041         if (s < zr * tq) {
00042             free(q0);
00043             return -1;
00044         }
00045         if (lc != j) {
00046             t = b[j];
00047             b[j] = b[lc];
00048             b[lc] = t;
00049             for (k = 0, p = a + n * j, q = a + n * lc; k < n; ++k) {
00050                 t = *p;
00051                 *p++ = *q;
00052                 *q++ = t;
00053             }
00054         }
00055         for (k = j + 1, ps = pd, t = 1. / *pd; k < n; ++k)
00056             *(ps += n) *= t;
00057     }
00058     for (j = 1, ps = b + 1; j < n; ++j) {
00059         for (k = 0, p = a + n * j, q = b, t = 0.; k < j; ++k)
00060             t += *p++ * *q++;
00061         *ps++ -= t;
00062     }
00063     for (j = n - 1, --ps, pd = a + n * n - 1; j >= 0; --j, pd -= n + 1) {
00064         for (k = j + 1, p = pd, q = b + j, t = 0.; k < n; ++k)
00065             t += *++p * *++q;
00066         *ps -= t;
00067         *ps-- /= *pd;
00068     }
00069     free(q0);
00070     return 0;
00071 }