Fix a crashes where histograms of a single datum were trying to be created.
[pspp] / src / math / histogram.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2004, 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/histogram.h"
20
21 #include <gsl/gsl_histogram.h>
22 #include <math.h>
23
24 #include "libpspp/message.h"
25 #include "libpspp/assertion.h"
26 #include "libpspp/cast.h"
27 #include "math/chart-geometry.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31 #define N_(msgid) msgid
32
33
34 #include "gl/xalloc.h"
35
36 void
37 histogram_add (struct histogram *h, double y, double c)
38 {
39   struct statistic *stat = &h->parent;
40   stat->accumulate (stat, NULL, c, 0, y);
41 }
42
43
44
45 static void
46 acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
47 {
48   struct histogram *hist = UP_CAST (s, struct histogram, parent);
49
50   gsl_histogram_accumulate (hist->gsl_hist, y, c);
51 }
52
53
54 static void
55 destroy (struct statistic *s)
56 {
57   struct histogram *h = UP_CAST (s, struct histogram, parent);
58   gsl_histogram_free (h->gsl_hist);
59   free (s);
60 }
61
62
63 struct histogram *
64 histogram_create (double bin_width, double min, double max)
65 {
66   int bins;
67   struct histogram *h = xmalloc (sizeof *h);
68   struct statistic *stat = &h->parent;
69   double upper_limit, lower_limit;
70   const double half_bin_width = bin_width / 2.0;
71
72   /* -1 if the lower end of the range contains more unused space
73      than the upper end.
74      +1 otherwise.  */
75   short sparse_end = 0;
76
77   if (max == min)
78     {
79       msg (MW, _("Not creating histogram because the data contains less than 2 distinct values"));
80       free (h);
81       return NULL;
82     }
83
84   assert (max > min);
85
86   lower_limit = floor (min / half_bin_width) - 1;
87   upper_limit = floor (max / half_bin_width) + 1;
88   
89   if (remainder (min, half_bin_width > remainder (max, half_bin_width)))
90     sparse_end = -1;
91   else
92     sparse_end = +1;
93
94   /* The range must be an EVEN number of half bin_widths */
95   if ( (int)(upper_limit - lower_limit) % 2)
96     {
97       /* Extend the range at the end which gives the least unused space */
98       if (sparse_end == +1)
99         lower_limit --;
100       else
101         upper_limit ++;
102       
103       /* Now the other end has more space */
104       sparse_end *= -1;
105     }
106
107   /* But the range should be aligned to an ODD number of
108      half bin widths, so that the labels are aesthetically pleasing ones. */
109   if ( (int)lower_limit % 2 == 0)
110     {
111       lower_limit += -sparse_end ;
112       upper_limit += -sparse_end ;
113     }
114
115   bins = (upper_limit - lower_limit) / 2.0;
116
117   upper_limit *= half_bin_width;
118   lower_limit *= half_bin_width;
119
120   h->gsl_hist = gsl_histogram_alloc (bins);
121
122   gsl_histogram_set_ranges_uniform (h->gsl_hist, lower_limit, upper_limit);
123
124   stat->accumulate = acc;
125   stat->destroy = destroy;
126
127   return h;
128 }
129