Debian Codegraph @

skmisc/loess/src/loessc.c

raw symbols SCIP
1 /*
2 * The authors of this software are Cleveland, Grosse, and Shyu.
3 * Copyright (c) 1989, 1992 by AT&T.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose without fee is hereby granted, provided that this entire notice
6 * is included in all copies of any software which is or includes a copy
7 * or modification of this software and in all copies of the supporting
8 * documentation for such software.
9 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
10 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
11 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
12 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
13 */
14 #include "S.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <limits.h>
19
20 /* Declarations */
21
22 static void
23 loess_workspace(int *d, int *n, double *span, int *degree,
24 int *nonparametric, int *drop_square,
25 int *sum_drop_sqr, int *setLf);
26
27 static void
28 loess_prune(int *parameter, int *a, double *xi, double *vert,
29 double *vval);
30
31 static void
32 loess_grow(int *parameter, int *a, double *xi, double *vert,
33 double *vval);
34
35 static void
36 loess_free(void);
37
38 #define min(x,y) ((x) < (y) ? (x) : (y))
39 #define max(x,y) ((x) > (y) ? (x) : (y))
40 #define GAUSSIAN 1
41 #define SYMMETRIC 0
42
43 static int *iv = NULL, liv, lv, tau;
44 static double *v = NULL;
45
46 extern char *error_message;
47 extern int error_status;
48
49
50 void F77_SUB(lowesa)(double*, int*, int*, int*, int*, double*, double*);
51 void F77_SUB(lowesb)(double*, double*, double*, double*, int*, int*, double*);
52 void F77_SUB(lowesc)(int*, double*, double*, double*, double*, double*);
53 void F77_SUB(lowesd)(int*, int*, int*, double*, int*, int*,
54 double*, int*, int*, int*, int*);
55 void F77_SUB(lowese)(int*, double*, int*, double*, double*);
56 void F77_SUB(lowesf)(double*, double*, double*, int*, double*,
57 int*, double*, double*, int*, double*);
58 void F77_SUB(lowesl)(int*, double*, int*, double*, double*);
59 void F77_SUB(ehg169)(int*, int*, int*, int*, int*, int*,
60 double*, int*, double*, int*, int*, int*);
61 void F77_SUB(ehg196)(int*, int*, double*, double*);
62 void F77_SUB(ehg182)(int *i);
63 void F77_SUB(ehg183a)(char *s, int *nc,int *i,int *n,int *inc);
64 void F77_SUB(ehg184a)(char *s, int *nc, double *x, int *n, int *inc);
65
66
67 void
68 loess_raw(double *y, double *x, double *weights, double *robust, int *d,
69 int *n, double *span, int *degree, int *nonparametric,
70 int *drop_square, int *sum_drop_sqr, double *cell, char **surf_stat,
71 double *surface, int *parameter, int *a, double *xi, double *vert,
72 double *vval, double *diagonal, double *trL, double *one_delta,
73 double *two_delta, int *setLf)
74 {
75 int zero = 0, one = 1, two = 2, nsing, i, k;
76 double *hat_matrix, *LL, dzero = 0;
77
78
79 *trL = 0;
80 loess_workspace(d, n, span, degree, nonparametric, drop_square,
81 sum_drop_sqr, setLf);
82 v[1] = *cell;
83 if(!strcmp(*surf_stat, "interpolate/none")) {
84 F77_SUB(lowesb)(x, y, robust, &dzero, &zero, iv, v);
85 F77_SUB(lowese)(iv, v, n, x, surface);
86 loess_prune(parameter, a, xi, vert, vval);
87 }
88 else if (!strcmp(*surf_stat, "direct/none")) {
89 F77_SUB(lowesf)(x, y, robust, iv, v, n, x, &dzero, &zero, surface);
90 }
91 else if (!strcmp(*surf_stat, "interpolate/1.approx")) {
92 F77_SUB(lowesb)(x, y, weights, diagonal, &one, iv, v);
93 F77_SUB(lowese)(iv, v, n, x, surface);
94 nsing = iv[29];
95 for(i = 0; i < (*n); i++) *trL = *trL + diagonal[i];
96 F77_SUB(lowesa)(trL, n, d, &tau, &nsing, one_delta, two_delta);
97 loess_prune(parameter, a, xi, vert, vval);
98 }
99 else if (!strcmp(*surf_stat, "interpolate/2.approx")) {
100 F77_SUB(lowesb)(x, y, weights, &dzero, &zero, iv, v);
101 F77_SUB(lowese)(iv, v, n, x, surface);
102 nsing = iv[29];
103 F77_SUB(ehg196)(&tau, d, span, trL);
104 F77_SUB(lowesa)(trL, n, d, &tau, &nsing, one_delta, two_delta);
105 loess_prune(parameter, a, xi, vert, vval);
106 }
107 else if (!strcmp(*surf_stat, "direct/approximate")) {
108 F77_SUB(lowesf)(x, y, weights, iv, v, n, x, diagonal, &one, surface);
109 nsing = iv[29];
110 for(i = 0; i < (*n); i++) *trL = *trL + diagonal[i];
111 F77_SUB(lowesa)(trL, n, d, &tau, &nsing, one_delta, two_delta);
112 }
113 else if (!strcmp(*surf_stat, "interpolate/exact")) {
114 hat_matrix = Calloc((*n)*(*n), double);
115 LL = Calloc((*n)*(*n), double);
116 F77_SUB(lowesb)(x, y, weights, diagonal, &one, iv, v);
117 F77_SUB(lowesl)(iv, v, n, x, hat_matrix);
118 F77_SUB(lowesc)(n, hat_matrix, LL, trL, one_delta, two_delta);
119 F77_SUB(lowese)(iv, v, n, x, surface);
120 loess_prune(parameter, a, xi, vert, vval);
121 Free(hat_matrix);
122 Free(LL);
123
124 }
125 else if (!strcmp(*surf_stat, "direct/exact")) {
126 hat_matrix = Calloc((*n)*(*n), double);
127 LL = Calloc((*n)*(*n), double);
128 F77_SUB(lowesf)(x, y, weights, iv, v, n, x, hat_matrix, &two, surface);
129 F77_SUB(lowesc)(n, hat_matrix, LL, trL, one_delta, two_delta);
130 k = (*n) + 1;
131 for(i = 0; i < (*n); i++)
132 diagonal[i] = hat_matrix[i * k];
133 Free(hat_matrix);
134 Free(LL);
135 }
136 loess_free();
137 }
138
139 void
140 loess_dfit(double *y, double *x, double *x_evaluate, double *weights,
141 double *span, int *degree, int *nonparametric,
142 int *drop_square, int *sum_drop_sqr, int *d, int *n, int *m,
143 double *fit)
144 {
145 int zero = 0;
146 double dzero = 0.0;
147 loess_workspace(d, n, span, degree, nonparametric, drop_square,
148 sum_drop_sqr, &zero);
149 F77_SUB(lowesf)(x, y, weights, iv, v, m, x_evaluate, &dzero, &zero, fit);
150 loess_free();
151 }
152
153 void
154 loess_dfitse(double *y, double *x, double *x_evaluate, double *weights,
155 double *robust, int *family, double *span, int *degree,
156 int *nonparametric, int *drop_square, int *sum_drop_sqr,
157 int *d, int *n, int *m, double *fit, double *L)
158 {
159 int zero = 0, two = 2;
160 double dzero = 0.0;
161 loess_workspace(d, n, span, degree, nonparametric, drop_square,
162 sum_drop_sqr, &zero);
163 if(*family == GAUSSIAN)
164 F77_SUB(lowesf)(x, y, weights, iv, v, m, x_evaluate, L, &two, fit);
165 else if(*family == SYMMETRIC)
166 {
167 F77_SUB(lowesf)(x, y, weights, iv, v, m, x_evaluate, L, &two, fit);
168 F77_SUB(lowesf)(x, y, robust, iv, v, m, x_evaluate, &dzero, &zero, fit);
169 }
170 loess_free();
171 }
172
173 void
174 loess_ifit(int *parameter, int *a, double *xi, double *vert, double *vval,
175 int *m, double *x_evaluate, double *fit)
176 {
177 loess_grow(parameter, a, xi, vert, vval);
178 F77_SUB(lowese)(iv, v, m, x_evaluate, fit);
179 loess_free();
180 }
181
182 void
183 loess_ise(double *y, double *x, double *x_evaluate, double *weights,
184 double *span, int *degree, int *nonparametric, int *drop_square,
185 int *sum_drop_sqr, double *cell, int *d, int *n, int *m,
186 double *fit, double *L)
187 {
188 int zero = 0, one = 1;
189 double dzero = 0.0;
190 loess_workspace(d, n, span, degree, nonparametric, drop_square,
191 sum_drop_sqr, &one);
192 v[1] = *cell;
193 F77_SUB(lowesb)(x, y, weights, &dzero, &zero, iv, v);
194 F77_SUB(lowesl)(iv, v, m, x_evaluate, L);
195 loess_free();
196 }
197
198 // Set global variables tau, lv, liv , and allocate global arrays
199 // v[1..lv], iv[1..liv]
200 static void
201 loess_workspace(int *d, int *n, double *span, int *degree,
202 int *nonparametric, int *drop_square, int *sum_drop_sqr,
203 int *setLf)
204 {
205 int D, N, tau0, nvmax, nf, i;
206 double dlv, dliv;
207
208 D = *d;
209 N = *n;
210 nvmax = max(200, N);
211 nf = min(N, (int) floor(N * (*span) + 1e-5));
212 if (nf <= 0) {
213 error_status = 1;
214 error_message = "span is too small";
215 return;
216 }
217 tau0 = ((*degree) > 1) ? ((D + 2) * (D + 1) / 2) : (D + 1);
218 tau = tau0 - (*sum_drop_sqr);
219 dlv = 50 + (3 * D + 3) * nvmax + N + (tau0 + 2) * nf;
220 dliv = 50 + (pow(2.0, (double) D) + 4.0) * nvmax + 2.0 * N;
221
222 if (*setLf) {
223 dlv = dlv + (D + 1.0) * nf * (double) nvmax;
224 dliv = dliv + nf * (double) nvmax;
225 }
226
227 if (dlv < INT_MAX && dliv < INT_MAX) {
228 lv = (int) dlv;
229 liv = (int) dliv;
230 } else {
231 error_status = 1;
232 error_message = "workspace required is too large";
233 return;
234 }
235
236 iv = Calloc(liv, int);
237 v = Calloc(lv, double);
238
239 F77_SUB(lowesd)(iv, &liv, &lv, v, d, n, span, degree, &nf,
240 &nvmax, setLf);
241 iv[32] = *nonparametric;
242 for(i = 0; i < D; i++)
243 iv[i + 40] = drop_square[i];
244 }
245
246 static void
247 loess_prune(int *parameter, int *a, double *xi, double *vert, double *vval)
248 {
249 int d, vc, a1, v1, xi1, vv1, nc, nv, nvmax, i, k;
250
251 d = iv[1];
252 vc = iv[3] - 1;
253 nc = iv[4];
254 nv = iv[5];
255 a1 = iv[6] - 1;
256 v1 = iv[10] - 1;
257 xi1 = iv[11] - 1;
258 vv1 = iv[12] - 1;
259 nvmax = iv[13];
260
261 for(i = 0; i < 5; i++)
262 parameter[i] = iv[i + 1];
263 parameter[5] = iv[21] - 1;
264 parameter[6] = iv[14] - 1;
265
266 for(i = 0; i < d; i++) {
267 k = nvmax * i;
268 vert[i] = v[v1 + k];
269 vert[i + d] = v[v1 + vc + k];
270 }
271 for(i = 0; i < nc; i++) {
272 xi[i] = v[xi1 + i];
273 a[i] = iv[a1 + i];
274 }
275 k = (d + 1) * nv;
276 for(i = 0; i < k; i++)
277 vval[i] = v[vv1 + i];
278 }
279
280 static void
281 loess_grow(int *parameter, int *a, double *xi, double *vert, double *vval)
282 {
283 int d, vc, nc, nv, a1, v1, xi1, vv1, i, k;
284
285 d = parameter[0];
286 vc = parameter[2];
287 nc = parameter[3];
288 nv = parameter[4];
289 liv = parameter[5];
290 lv = parameter[6];
291 iv = Calloc(liv, int);
292 v = Calloc(lv, double);
293
294 iv[1] = d;
295 iv[2] = parameter[1];
296 iv[3] = vc;
297 iv[5] = iv[13] = nv;
298 iv[4] = iv[16] = nc;
299 iv[6] = 50;
300 iv[7] = iv[6] + nc;
301 iv[8] = iv[7] + vc * nc;
302 iv[9] = iv[8] + nc;
303 iv[10] = 50;
304 iv[12] = iv[10] + nv * d;
305 iv[11] = iv[12] + (d + 1) * nv;
306 iv[27] = 173;
307
308 v1 = iv[10] - 1;
309 xi1 = iv[11] - 1;
310 a1 = iv[6] - 1;
311 vv1 = iv[12] - 1;
312
313 for(i = 0; i < d; i++) {
314 k = nv * i;
315 v[v1 + k] = vert[i];
316 v[v1 + vc - 1 + k] = vert[i + d];
317 }
318 for(i = 0; i < nc; i++) {
319 v[xi1 + i] = xi[i];
320 iv[a1 + i] = a[i];
321 }
322 k = (d + 1) * nv;
323 for(i = 0; i < k; i++)
324 v[vv1 + i] = vval[i];
325
326 F77_SUB(ehg169)(&d, &vc, &nc, &nc, &nv, &nv, v+v1, iv+a1,
327 v+xi1, iv+iv[7]-1, iv+iv[8]-1, iv+iv[9]-1);
328 }
329
330 static void
331 loess_free(void)
332 {
333 Free(v);
334 Free(iv);
335 }
336
337 /* begin ehg's FORTRAN-callable C-codes */
338
339 void
340 F77_SUB(ehg182)(int *i)
341 {
342 char *mess, mess2[50];
343 switch(*i){
344 case 100:
345 mess="Wrong version number in lowesd. Probably typo in caller.";
346 break;
347 case 101:
348 mess="d>dMAX in ehg131. Need to recompile with increased dimensions.";
349 break;
350 case 102:
351 mess="liv too small. (Discovered by lowesd)";
352 break;
353 case 103:
354 mess="lv too small. (Discovered by lowesd)";
355 break;
356 case 104:
357 mess="Span too small. Fewer data values than degrees of freedom.";
358 break;
359 case 105:
360 mess="k>d2MAX in ehg136. Need to recompile with increased dimensions.";
361 break;
362 case 106:
363 mess="lwork too small";
364 break;
365 case 107:
366 mess="Invalid value for kernel";
367 break;
368 case 108:
369 mess="Invalid value for ideg";
370 break;
371 case 109:
372 mess="lowstt only applies when kernel=1.";
373 break;
374 case 110:
375 mess="Not enough extra workspace for robustness calculation";
376 break;
377 case 120:
378 mess="Zero-width neighborhood. Make span bigger";
379 break;
380 case 121:
381 mess="All data on boundary of neighborhood. make span bigger";
382 break;
383 case 122:
384 mess="Extrapolation not allowed with blending";
385 break;
386 case 123:
387 mess="ihat=1 (diag L) in l2fit only makes sense if z=x (eval=data).";
388 break;
389 case 171:
390 mess="lowesd must be called first.";
391 break;
392 case 172:
393 mess="lowesf must not come between lowesb and lowese, lowesr, or lowesl.";
394 break;
395 case 173:
396 mess="lowesb must come before lowese, lowesr, or lowesl.";
397 break;
398 case 174:
399 mess="lowesb need not be called twice.";
400 break;
401 case 175:
402 mess="Need setLf=.true. for lowesl.";
403 break;
404 case 180:
405 mess="nv>nvmax in cpvert.";
406 break;
407 case 181:
408 mess="nt>20 in eval.";
409 break;
410 case 182:
411 mess="svddc failed in l2fit.";
412 break;
413 case 183:
414 mess="Did not find edge in vleaf.";
415 break;
416 case 184:
417 mess="Zero-width cell found in vleaf.";
418 break;
419 case 185:
420 mess="Trouble descending to leaf in vleaf.";
421 break;
422 case 186:
423 mess="Insufficient workspace for lowesf.";
424 break;
425 case 187:
426 mess="Insufficient stack space.";
427 break;
428 case 188:
429 mess="lv too small for computing explicit L.";
430 break;
431 case 191:
432 mess="Computed trace L was negative; something is wrong!";
433 break;
434 case 192:
435 mess="Computed delta was negative; something is wrong!";
436 break;
437 case 193:
438 mess="Workspace in loread appears to be corrupted.";
439 break;
440 case 194:
441 mess="Trouble in l2fit/l2tr";
442 break;
443 case 195:
444 mess="Only constant, linear, or quadratic local models allowed";
445 break;
446 case 196:
447 mess="degree must be at least 1 for vertex influence matrix";
448 break;
449 case 999:
450 mess="not yet implemented";
451 break;
452 default:
453 sprintf(mess=mess2,"Assert failed; error code %d\n",*i);
454 break;
455 }
456 error_status = 1;
457 error_message = mess;
458 }
459
460 void F77_SUB(ehg183a)(char *s, int *nc, int *i, int *n,int *inc)
461 {
462 char mess[4000], num[20];
463 int j;
464 strncpy(mess,s,*nc);
465 mess[*nc] = '\0';
466 for (j=0; j<*n; j++) {
467 snprintf(num, 20, " %d",i[j * *inc]);
468 strcat(mess,num);
469 }
470 strcat(mess,"\n");
471 error_status = 1;
472 error_message = mess;
473 }
474
475 void F77_SUB(ehg184a)(char *s, int *nc, double *x, int *n, int *inc)
476 {
477 char mess[4000], num[30];
478 int j;
479 strncpy(mess,s,*nc);
480 mess[*nc] = '\0';
481 for (j=0; j<*n; j++) {
482 snprintf(num,30," %.5g",x[j * *inc]);
483 strcat(mess,num);
484 }
485 strcat(mess,"\n");
486 error_status = 1;
487 error_message = mess;
488 }