Avoid a crash when attempting to allocate very large matrices.
[pspp.git] / src / math / tukey-hinges.c
blob1a5d01fcb85e153aec4b01ed6f852ad330a37d7c
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2008, 2009, 2011 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #include <config.h>
19 #include "math/tukey-hinges.h"
21 #include <math.h>
23 #include "libpspp/assertion.h"
24 #include "libpspp/cast.h"
25 #include "math/order-stats.h"
27 #include "gl/xalloc.h"
29 void
30 tukey_hinges_calculate (const struct tukey_hinges *th, double hinge[3])
32 double a[3];
33 double a_star[3];
34 int i;
35 const struct order_stats *os = &th->parent;
37 for (i = 0 ; i < 3 ; ++i)
39 a_star[i] = os->k[i].tc - os->k[i].cc;
40 a[i] = a_star[i] / os->k[i].c_p1;
42 if (a_star[i] < 1)
44 if (os->k[i].c_p1 >= 1)
46 hinge[i] = (1 - a_star[i]) * os->k[i].y
47 + a_star[i] * os->k[i].y_p1;
49 else
51 hinge[i] = (1 - a[i]) * os->k[i].y
52 + a[i] * os->k[i].y_p1;
55 else
57 hinge[i] = os->k[i].y_p1;
63 static void
64 destroy (struct statistic *s)
66 struct tukey_hinges *th = UP_CAST (s, struct tukey_hinges, parent.parent);
67 free (th);
70 struct tukey_hinges *
71 tukey_hinges_create (double W, double c_min)
73 double d;
74 struct tukey_hinges *th = XZALLOC (struct tukey_hinges);
75 struct order_stats *os = &th->parent;
76 struct statistic *stat = &os->parent;
78 assert (c_min >= 0);
80 os->n_k = 3;
81 os->k = th->k;
83 if (c_min >= 1.0)
85 d = floor ((W + 3) / 2.0) / 2.0;
87 os->k[0].tc = d;
88 os->k[1].tc = W/2.0 + 0.5;
89 os->k[2].tc = W + 1 - d;
91 else
93 d = floor ((W/c_min + 3.0)/ 2.0) / 2.0 ;
94 os->k[0].tc = d * c_min;
95 os->k[1].tc = (W + c_min) / 2.0;
96 os->k[2].tc = W + c_min * (1 - d);
100 stat->destroy = destroy;
102 return th;