GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
house.c
Go to the documentation of this file.
00001 /*  house.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 void house(double *a, double *d, double *dp, int n)
00011 {
00012     double sc, x, y, h;
00013 
00014     int i, j, k, m, e;
00015 
00016     double *qw, *qs, *pc, *p;
00017 
00018     qs = (double *)calloc(2 * n, sizeof(double));
00019     for (j = 0, qw = qs + n, pc = a; j < n; pc += n + 1)
00020         qw[j++] = *pc;
00021     for (j = 0, pc = a; j < n - 2; ++j, pc += n + 1) {
00022         m = n - j - 1;
00023         for (i = 1, sc = 0.; i <= m; ++i)
00024             sc += pc[i] * pc[i];
00025         if (sc > 0.) {
00026             sc = sqrt(sc);
00027             if ((x = *(pc + 1)) < 0.) {
00028                 y = x - sc;
00029                 h = 1. / sqrt(-2. * sc * y);
00030             }
00031             else {
00032                 y = x + sc;
00033                 h = 1. / sqrt(2. * sc * y);
00034                 sc = -sc;
00035             }
00036             for (i = 0, qw = pc + 1; i < m; ++i) {
00037                 qs[i] = 0.;
00038                 if (i)
00039                     qw[i] *= h;
00040                 else
00041                     qw[i] = y * h;
00042             }
00043             for (i = 0, e = j + 2, p = pc + n + 1, h = 0.; i < m;
00044                  ++i, p += e++) {
00045                 qs[i] += (y = qw[i]) * *p++;
00046                 for (k = i + 1; k < m; ++k) {
00047                     qs[i] += qw[k] * *p;
00048                     qs[k] += y * *p++;
00049                 }
00050                 h += y * qs[i];
00051             }
00052             for (i = 0; i < m; ++i) {
00053                 qs[i] -= h * qw[i];
00054                 qs[i] += qs[i];
00055             }
00056             for (i = 0, e = j + 2, p = pc + n + 1; i < m; ++i, p += e++) {
00057                 for (k = i; k < m; ++k)
00058                     *p++ -= qw[i] * qs[k] + qs[i] * qw[k];
00059             }
00060         }
00061         d[j] = *pc;
00062         dp[j] = sc;
00063     }
00064     d[j] = *pc;
00065     dp[j] = *(pc + 1);
00066     d[j + 1] = *(pc + n + 1);
00067     for (j = 0, pc = a, qw = qs + n; j < n; ++j, pc += n + 1) {
00068         *pc = qw[j];
00069         for (i = 1, p = pc + n; i < n - j; p += n)
00070             pc[i++] = *p;
00071     }
00072     free(qs);
00073 }