GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
sv2uv.c
Go to the documentation of this file.
1 /* sv2uv.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 sv2uv(double *d, double *a, double *u, 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  ldumat(a, u, m, n);
54  for (i = 0, q = a; i < n; ++i) {
55  for (j = 0; j < n; ++j, ++q) {
56  if (j < i)
57  *q = 0.;
58  else if (j == i)
59  *q = d[i];
60  }
61  }
62  for (i = 0, mm = n, nm = n - 1, p = a; i < n; ++i, --mm, --nm, p += n + 1) {
63  if (i && mm > 1) {
64  sv = h = 0.;
65  for (j = 0, q = p, s = 0.; j < mm; ++j, q += n) {
66  w[j] = *q;
67  s += *q * *q;
68  }
69  if (s > 0.) {
70  h = sqrt(s);
71  if (*p < 0.)
72  h = -h;
73  s += *p * h;
74  s = 1. / s;
75  t = 1. / (w[0] += h);
76  sv = 1. + fabs(*p / h);
77  for (k = 1, ms = n - i; k < ms; ++k) {
78  for (j = 0, q = p + k, r = 0.; j < mm; q += n)
79  r += w[j++] * *q;
80  for (j = 0, q = p + k, r *= s; j < mm; q += n)
81  *q -= r * w[j++];
82  }
83  for (k = 0, p1 = u + i; k < m; ++k, p1 += m) {
84  for (j = 0, q = p1, r = 0.; j < mm;)
85  r += w[j++] * *q++;
86  for (j = 0, q = p1, r *= s; j < mm;)
87  *q++ -= r * w[j++];
88  }
89  }
90  *p = sv;
91  d[i] = -h;
92  }
93  if (mm == 1)
94  d[i] = *p;
95  p1 = p + 1;
96  if (nm > 1) {
97  sv = h = 0.;
98  for (j = 0, q = p1, s = 0.; j < nm; ++j, ++q)
99  s += *q * *q;
100  if (s > 0.) {
101  h = sqrt(s);
102  if (*p1 < 0.)
103  h = -h;
104  sv = 1. + fabs(*p1 / h);
105  s += *p1 * h;
106  s = 1. / s;
107  t = 1. / (*p1 += h);
108  for (k = n, ms = n * (n - i); k < ms; k += n) {
109  for (j = 0, q = p1, pp = p1 + k, r = 0.; j < nm; ++j)
110  r += *q++ * *pp++;
111  for (j = 0, q = p1, pp = p1 + k, r *= s; j < nm; ++j)
112  *pp++ -= r * *q++;
113  }
114  for (j = 1, q = p1 + 1; j < nm; ++j)
115  *q++ *= t;
116  }
117  *p1 = sv;
118  e[i] = -h;
119  }
120  if (nm == 1)
121  e[i] = *p1;
122  }
123  ldvmat(a, v, n);
124  qrbdv(d, e, u, m, v, n);
125  for (i = 0; i < n; ++i) {
126  if (d[i] < 0.) {
127  d[i] = -d[i];
128  for (j = 0, p = v + i; j < n; ++j, p += n)
129  *p = -*p;
130  }
131  }
132  free(w);
133  return 0;
134 }
int qrbdv(double *d, double *e, double *u, int m, double *v, int n)
Definition: qrbdv.c:9
void free(void *)
void ldumat(double *a, double *u, int m, int n)
Definition: ldumat.c:9
double t
Definition: r_raster.c:39
void ldvmat(double *a, double *v, int n)
Definition: ldvmat.c:8
int sv2uv(double *d, double *a, double *u, int m, double *v, int n)
Definition: sv2uv.c:10
double r
Definition: r_raster.c:39