GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 double *Cdhc_royston(double *x, int n)
12 {
13  static double y[2];
14  double *a, eps, w, pw, mean = 0, ssq = 0, *xcopy;
15  int i, ifault, n2;
16 
17  n2 = (int)floor((double)n / 2);
18 
19 #ifndef lint
20  if ((a = (double *)malloc(n2 * sizeof(double))) == NULL) {
21  fprintf(stderr, "Memory error in royston\n");
22  exit(EXIT_FAILURE);
23  }
24  if ((xcopy = (double *)malloc(n * sizeof(double))) == NULL) {
25  fprintf(stderr, "Memory error in royston\n");
26  exit(EXIT_FAILURE);
27  }
28 #endif /* lint */
29 
30  for (i = 0; i < n; ++i) {
31  xcopy[i] = x[i];
32  mean += x[i];
33  }
34  mean /= n;
35 
36  qsort(xcopy, n, sizeof(double), Cdhc_dcmp);
37 
38  for (i = 0; i < n; ++i)
39  ssq += (mean - x[i]) * (mean - x[i]);
40 
41  wcoef(a, n, n2, &eps, &ifault);
42 
43  if (ifault == 0)
44  wext(xcopy, n, ssq, a, n2, eps, &w, &pw, &ifault);
45  else {
46  fprintf(stderr, "Error in wcoef()\n");
47  return (double *)NULL;
48  }
49 
50  if (ifault == 0) {
51  y[0] = w;
52  y[1] = pw;
53  }
54  else {
55  fprintf(stderr, "Error in wcoef()\n");
56  return (double *)NULL;
57  }
58 
59  free(a);
60  free(xcopy);
61 
62  return y;
63 }
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:11
void * malloc(unsigned)
void free(void *)
#define x