GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 dcmp.c as66.c
11  */
12 
13 double *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), 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 }
int dcmp(const void *i, const void *j)
Definition: dcmp.c:1
int y
Definition: plot.c:34
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 * royston(double *x, int n)
Definition: royston.c:13
void * malloc(YYSIZE_T)
int
Definition: g3dcolor.c:48
return NULL
Definition: dbfopen.c:1394
void free(void *)
int n
Definition: dataquad.c:291
void wcoef(double a[], int n, int n2, double *eps, int *ifault)
Definition: as181.c:175