1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2004, 2008 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/>. */
18 #include "histogram.h"
20 #include <gl/xalloc.h>
21 #include <libpspp/assertion.h>
23 #include <gsl/gsl_histogram.h>
24 #include "chart-geometry.h"
29 histogram_add (struct histogram *h, double y, double c)
31 ((struct statistic *)h)->accumulate ((struct statistic *) h, NULL, c, 0, y);
37 acc (struct statistic *s, const struct ccase *cx UNUSED, double c, double cc UNUSED, double y)
39 struct histogram *hist = (struct histogram *) s;
41 gsl_histogram_accumulate (hist->gsl_hist, y, c);
46 destroy (struct statistic *s)
48 struct histogram *h = (struct histogram *) s;
49 gsl_histogram_free (h->gsl_hist);
55 histogram_create (int bins, double min, double max)
57 struct histogram *h = xmalloc (sizeof *h);
58 struct statistic *stat = (struct statistic *) h;
59 double upper_limit, lower_limit;
61 double bin_width = chart_rounded_tick ((max - min) / (double) bins);
62 double bin_width_2 = bin_width / 2.0;
64 int n = ceil (max / (bin_width_2) ) ;
68 if ( ! (n % 2 ) ) n++;
69 upper_limit = n * bin_width_2;
71 n = floor (min / (bin_width_2) ) ;
72 if ( ! (n % 2 ) ) n--;
73 lower_limit = n * bin_width_2;
75 h->gsl_hist = gsl_histogram_alloc (bins);
76 gsl_histogram_set_ranges_uniform (h->gsl_hist, lower_limit, upper_limit);
78 stat->accumulate = acc;
79 stat->destroy = destroy;