GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4187339ee
sv2u1v.c
Go to the documentation of this file.
1 /* sv2u1v.c CCMATH mathematics library source code.
2  *
3  * Copyright (C) 2000 Daniel A. Atkinson All rights reserved.
4  * This code may be redistributed under the terms of the GNU library
5  * public license (LGPL). ( See the lgpl.license file for details.)
6  * ------------------------------------------------------------------------
7  */
8 #include <stdlib.h>
9 #include "ccmath.h"
10 int sv2u1v(double *d, double *a, int m, double *v, int n)
11 {
12  double *p, *p1, *q, *pp, *w, *e;
13 
14  double s, t, h, r, sv;
15 
16  int i, j, k, mm, nm, ms;
17 
18  if (m < n)
19  return -1;
20  w = (double *)calloc(m + n, sizeof(double));
21  e = w + m;
22  for (i = 0, mm = m, p = a; i < n; ++i, --mm, p += n + 1) {
23  if (mm > 1) {
24  sv = h = 0.;
25  for (j = 0, q = p, s = 0.; j < mm; ++j, q += n) {
26  w[j] = *q;
27  s += *q * *q;
28  }
29  if (s > 0.) {
30  h = sqrt(s);
31  if (*p < 0.)
32  h = -h;
33  s += *p * h;
34  s = 1. / s;
35  t = 1. / (w[0] += h);
36  sv = 1. + fabs(*p / h);
37  for (k = 1, ms = n - i; k < ms; ++k) {
38  for (j = 0, q = p + k, r = 0.; j < mm; q += n)
39  r += w[j++] * *q;
40  r = r * s;
41  for (j = 0, q = p + k; j < mm; q += n)
42  *q -= r * w[j++];
43  }
44  for (j = 1, q = p; j < mm;)
45  *(q += n) = w[j++] * t;
46  }
47  *p = sv;
48  d[i] = -h;
49  }
50  if (mm == 1)
51  d[i] = *p;
52  }
53  for (i = 0, q = v, p = a; i < n; ++i) {
54  for (j = 0; j < n; ++j, ++q, ++p) {
55  if (j < i)
56  *q = 0.;
57  else if (j == i)
58  *q = d[i];
59  else
60  *q = *p;
61  }
62  }
63  atou1(a, m, n);
64  for (i = 0, mm = n, nm = n - 1, p = v; i < n; ++i, --mm, --nm, p += n + 1) {
65  if (i && mm > 1) {
66  sv = h = 0.;
67  for (j = 0, q = p, s = 0.; j < mm; ++j, q += n) {
68  w[j] = *q;
69  s += *q * *q;
70  }
71  if (s > 0.) {
72  h = sqrt(s);
73  if (*p < 0.)
74  h = -h;
75  s += *p * h;
76  s = 1. / s;
77  t = 1. / (w[0] += h);
78  sv = 1. + fabs(*p / h);
79  for (k = 1, ms = n - i; k < ms; ++k) {
80  for (j = 0, q = p + k, r = 0.; j < mm; q += n)
81  r += w[j++] * *q;
82  for (j = 0, q = p + k, r *= s; j < mm; q += n)
83  *q -= r * w[j++];
84  }
85  for (k = 0, p1 = a + i; k < m; ++k, p1 += n) {
86  for (j = 0, q = p1, r = 0.; j < mm;)
87  r += w[j++] * *q++;
88  for (j = 0, q = p1, r *= s; j < mm;)
89  *q++ -= r * w[j++];
90  }
91  }
92  *p = sv;
93  d[i] = -h;
94  }
95  if (mm == 1)
96  d[i] = *p;
97  p1 = p + 1;
98  if (nm > 1) {
99  sv = h = 0.;
100  for (j = 0, q = p1, s = 0.; j < nm; ++j, ++q)
101  s += *q * *q;
102  if (s > 0.) {
103  h = sqrt(s);
104  if (*p1 < 0.)
105  h = -h;
106  sv = 1. + fabs(*p1 / h);
107  s += *p1 * h;
108  s = 1. / s;
109  t = 1. / (*p1 += h);
110  for (k = n, ms = n * (n - i); k < ms; k += n) {
111  for (j = 0, q = p1, pp = p1 + k, r = 0.; j < nm; ++j)
112  r += *q++ * *pp++;
113  for (j = 0, q = p1, pp = p1 + k, r *= s; j < nm; ++j)
114  *pp++ -= r * *q++;
115  }
116  for (j = 1, q = p1 + 1; j < nm; ++j)
117  *q++ *= t;
118  }
119  *p1 = sv;
120  e[i] = -h;
121  }
122  if (nm == 1)
123  e[i] = *p1;
124  }
125  atovm(v, n);
126  qrbdu1(d, e, a, m, v, n);
127  for (i = 0; i < n; ++i) {
128  if (d[i] < 0.) {
129  d[i] = -d[i];
130  for (j = 0, p = v + i; j < n; ++j, p += n)
131  *p = -*p;
132  }
133  }
134  free(w);
135  return 0;
136 }
void atou1(double *a, int m, int n)
Definition: atou1.c:9
void atovm(double *v, int n)
Definition: atovm.c:8
int qrbdu1(double *d, double *e, double *u, int m, double *v, int n)
Definition: qrbdu1.c:9
double t
Definition: r_raster.c:39
double r
Definition: r_raster.c:39
void free(void *)
int sv2u1v(double *d, double *a, int m, double *v, int n)
Definition: sv2u1v.c:10