8c809093335d68a1aad52f0cb3f91e0400508864
[pspp-builds.git] / src / math / histogram.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include <math.h>
21 #include <gsl/gsl_histogram.h>
22 #include <assert.h>
23 #include "histogram.h"
24 #include "chart-geometry.h"
25
26
27 gsl_histogram *
28 histogram_create(double bins, double x_min, double x_max)
29 {
30   int n;
31   double bin_width ;
32   double bin_width_2 ;
33   double upper_limit, lower_limit;
34
35   gsl_histogram *hist = gsl_histogram_alloc(bins);
36
37   bin_width = chart_rounded_tick((x_max - x_min)/ bins);
38   bin_width_2 = bin_width / 2.0;
39
40   n =  ceil( x_max / (bin_width_2) ) ;
41   if ( ! (n % 2 ) ) n++;
42   upper_limit = n * bin_width_2;
43
44   n =  floor( x_min / (bin_width_2) ) ;
45   if ( ! (n % 2 ) ) n--;
46   lower_limit = n * bin_width_2;
47
48   gsl_histogram_set_ranges_uniform(hist, lower_limit, upper_limit);
49
50   return hist;
51 }
52