GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
evmax.c
Go to the documentation of this file.
00001 /*  evmax.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 library
00005  *  public license (LGPL). ( See the lgpl.license file for details.)
00006  * ------------------------------------------------------------------------
00007  */
00008 #include <stdlib.h>
00009 #include "ccmath.h"
00010 double evmax(double *a, double *u, int n)
00011 {
00012     double *p, *q, *qm, *r, *s, *t;
00013 
00014     double ev, evm, c, h;
00015 
00016     int kc;
00017 
00018     q = (double *)calloc(n, sizeof(double));
00019     qm = q + n;
00020     *(qm - 1) = 1.;
00021     ev = 0.;
00022     for (kc = 0; kc < 200; ++kc) {
00023         h = c = 0.;
00024         evm = ev;
00025         for (p = u, r = a, s = q; s < qm;) {
00026             *p = 0.;
00027             for (t = q; t < qm;)
00028                 *p += *r++ * *t++;
00029             c += *p * *p;
00030             h += *p++ * *s++;
00031         }
00032         ev = c / h;
00033         c = sqrt(c);
00034         for (p = u, s = q; s < qm;) {
00035             *p /= c;
00036             *s++ = *p++;
00037         }
00038         if (((c = ev - evm) < 0. ? -c : c) < 1.e-16 * (ev < 0. ? -ev : ev)) {
00039             free(q);
00040             return ev;
00041         }
00042     }
00043     free(q);
00044     for (kc = 0; kc < n;)
00045         u[kc++] = 0.;
00046     return 0.;
00047 }