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