Changed all the licence notices in all the files.
[pspp-builds.git] / src / histogram.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2004 Free Software Foundation, Inc.
3    Written by John Darrington <john@darrington.wattle.id.au>
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <math.h>
21 #include <gsl/gsl_histogram.h>
22 #include <assert.h>
23 #include "histogram.h"
24 #include "chart.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
38   bin_width = chart_rounded_tick((x_max - x_min)/ bins);
39   bin_width_2 = bin_width / 2.0;
40     
41   n =  ceil( x_max / (bin_width_2) ) ; 
42   if ( ! (n % 2 ) ) n++;
43   upper_limit = n * bin_width_2;
44
45   n =  floor( x_min / (bin_width_2) ) ; 
46   if ( ! (n % 2 ) ) n--;
47   lower_limit = n * bin_width_2;
48
49   gsl_histogram_set_ranges_uniform(hist, lower_limit, upper_limit);
50
51
52   return hist;
53 }
54