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