Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / math / tukey-hinges.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "math/tukey-hinges.h"
20
21 #include <math.h>
22
23 #include "libpspp/assertion.h"
24 #include "libpspp/cast.h"
25 #include "math/order-stats.h"
26
27 #include "gl/xalloc.h"
28
29 void
30 tukey_hinges_calculate (const struct tukey_hinges *th, double hinge[3])
31 {
32   double a[3];
33   double a_star[3];
34   int i;
35   const struct order_stats *os = &th->parent;
36
37   for (i = 0 ; i < 3 ; ++i)
38     {
39       a_star[i] = os->k[i].tc - os->k[i].cc;
40       a[i] = a_star[i] / os->k[i].c_p1;
41
42       if (a_star[i] < 1)
43         {
44           if (os->k[i].c_p1 >= 1 )
45             {
46               hinge[i] = (1 - a_star[i]) * os->k[i].y
47                 + a_star[i] * os->k[i].y_p1;
48             }
49           else
50             {
51               hinge[i] = (1 - a[i]) * os->k[i].y
52                 + a[i] * os->k[i].y_p1;
53             }
54         }
55       else
56         {
57           hinge[i] = os->k[i].y_p1;
58         }
59
60     }
61 }
62
63 static void
64 destroy (struct statistic *s)
65 {
66   struct tukey_hinges *th = UP_CAST (s, struct tukey_hinges, parent.parent);
67   struct order_stats *os = &th->parent;
68
69   free (os->k);
70   free (s);
71 };
72
73 struct tukey_hinges *
74 tukey_hinges_create (double W, double c_min)
75 {
76   double d;
77   struct tukey_hinges *th = xzalloc (sizeof (*th));
78   struct order_stats *os = &th->parent;
79   struct statistic *stat = &os->parent;
80
81   assert (c_min >= 0);
82
83   os->n_k = 3;
84   os->k = xcalloc (sizeof (*os->k), 3);
85
86   if ( c_min >= 1.0)
87     {
88       d = floor ((W + 3) / 2.0) / 2.0;
89
90       os->k[0].tc = d;
91       os->k[1].tc = W/2.0 + 0.5;
92       os->k[2].tc = W + 1 - d;
93     }
94   else
95     {
96       d = floor ((W/c_min + 3.0)/ 2.0) / 2.0 ;
97       os->k[0].tc = d * c_min;
98       os->k[1].tc = (W + c_min) / 2.0;
99       os->k[2].tc = W + c_min * (1 - d);
100     }
101
102
103   stat->destroy = destroy;
104
105   return th;
106 }