GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
royston.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
4 #include "local_proto.h"
5 
6 
7 /*-
8  * driver program for AS 181: Royston's extension of the Shapiro-Wilk
9  * W statistic to n=2000
10  * needs as181.c as177.c as241.c Cdhc_dcmp.c as66.c
11  */
12 
13 double *Cdhc_royston(double *x, int n)
14 {
15  static double y[2];
16  double *a, eps, w, pw, mean = 0, ssq = 0, *xcopy;
17  int i, ifault, n2;
18 
19  n2 = (int)floor((double)n / 2);
20 
21 #ifndef lint
22  if ((a = (double *)malloc(n2 * sizeof(double))) == NULL) {
23  fprintf(stderr, "Memory error in royston\n");
24  exit(EXIT_FAILURE);
25  }
26  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
27  fprintf(stderr, "Memory error in royston\n");
28  exit(EXIT_FAILURE);
29  }
30 #endif /* lint */
31 
32  for (i = 0; i < n; ++i) {
33  xcopy[i] = x[i];
34  mean += x[i];
35  }
36  mean /= n;
37 
38  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
39 
40  for (i = 0; i < n; ++i)
41  ssq += (mean - x[i]) * (mean - x[i]);
42 
43  wcoef(a, n, n2, &eps, &ifault);
44 
45  if (ifault == 0)
46  wext(xcopy, n, ssq, a, n2, eps, &w, &pw, &ifault);
47  else {
48  fprintf(stderr, "Error in wcoef()\n");
49  return (double *)NULL;
50  }
51 
52  if (ifault == 0) {
53  y[0] = w;
54  y[1] = pw;
55  }
56  else {
57  fprintf(stderr, "Error in wcoef()\n");
58  return (double *)NULL;
59  }
60 
61  free(a);
62  free(xcopy);
63 
64  return y;
65 }
void free(void *)
#define NULL
Definition: ccmath.h:32
#define x
void * malloc(YYSIZE_T)
void wext(double x[], int n, double ssq, double a[], int n2, double eps, double *w, double *pw, int *ifault)
Definition: as181.c:25
double * Cdhc_royston(double *x, int n)
Definition: royston.c:13
int Cdhc_dcmp(const void *i, const void *j)
Definition: dcmp.c:1
float mean(IClass_statistics *statistics, int band)
Helper function for computing mean.
void wcoef(double a[], int n, int n2, double *eps, int *ifault)
Definition: as181.c:175