| 1 | /* gsl_histogram2d_calloc_range.c |
| 2 | * Copyright (C) 2000 Simone Piccardi |
| 3 | * |
| 4 | * This library 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 3 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, |
| 10 | * but 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 |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | /*************************************************************** |
| 20 | * |
| 21 | * File gsl_histogram2d_calloc_range.c: |
| 22 | * Routine to create a variable binning 2D histogram providing |
| 23 | * the input range vectors. Need GSL library and header. |
| 24 | * Do range check and allocate the histogram data. |
| 25 | * |
| 26 | * Author: S. Piccardi |
| 27 | * Jan. 2000 |
| 28 | * |
| 29 | ***************************************************************/ |
| 30 | #include "gsl__config.h" |
| 31 | #include <stdlib.h> |
| 32 | #include "gsl_errno.h" |
| 33 | #include "gsl_histogram2d.h" |
| 34 | /* |
| 35 | * Routine that create a 2D histogram using the given |
| 36 | * values for X and Y ranges |
| 37 | */ |
| 38 | gsl_histogram2d * |
| 39 | gsl_histogram2d_calloc_range (size_t nx, size_t ny, |
| 40 | double *xrange, |
| 41 | double *yrange) |
| 42 | { |
| 43 | size_t i, j; |
| 44 | gsl_histogram2d *h; |
| 45 | |
| 46 | /* check arguments */ |
| 47 | |
| 48 | if (nx == 0) |
| 49 | { |
| 50 | GSL_ERROR_VAL ("histogram length nx must be positive integer", |
| 51 | GSL_EDOM, 0); |
| 52 | } |
| 53 | |
| 54 | if (ny == 0) |
| 55 | { |
| 56 | GSL_ERROR_VAL ("histogram length ny must be positive integer", |
| 57 | GSL_EDOM, 0); |
| 58 | } |
| 59 | |
| 60 | /* init ranges */ |
| 61 | |
| 62 | for (i = 0; i < nx; i++) |
| 63 | { |
| 64 | if (xrange[i] >= xrange[i + 1]) |
| 65 | { |
| 66 | GSL_ERROR_VAL ("histogram xrange not in increasing order", |
| 67 | GSL_EDOM, 0); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | for (j = 0; j < ny; j++) |
| 72 | { |
| 73 | if (yrange[j] >= yrange[j + 1]) |
| 74 | { |
| 75 | GSL_ERROR_VAL ("histogram yrange not in increasing order" |
| 76 | ,GSL_EDOM, 0); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /* Allocate histogram */ |
| 81 | |
| 82 | h = (gsl_histogram2d *) malloc (sizeof (gsl_histogram2d)); |
| 83 | |
| 84 | if (h == 0) |
| 85 | { |
| 86 | GSL_ERROR_VAL ("failed to allocate space for histogram struct", |
| 87 | GSL_ENOMEM, 0); |
| 88 | } |
| 89 | |
| 90 | h->xrange = (double *) malloc ((nx + 1) * sizeof (double)); |
| 91 | |
| 92 | if (h->xrange == 0) |
| 93 | { |
| 94 | /* exception in constructor, avoid memory leak */ |
| 95 | free (h); |
| 96 | |
| 97 | GSL_ERROR_VAL ("failed to allocate space for histogram xrange", |
| 98 | GSL_ENOMEM, 0); |
| 99 | } |
| 100 | |
| 101 | h->yrange = (double *) malloc ((ny + 1) * sizeof (double)); |
| 102 | |
| 103 | if (h->yrange == 0) |
| 104 | { |
| 105 | /* exception in constructor, avoid memory leak */ |
| 106 | free (h); |
| 107 | |
| 108 | GSL_ERROR_VAL ("failed to allocate space for histogram yrange", |
| 109 | GSL_ENOMEM, 0); |
| 110 | } |
| 111 | |
| 112 | h->bin = (double *) malloc (nx * ny * sizeof (double)); |
| 113 | |
| 114 | if (h->bin == 0) |
| 115 | { |
| 116 | /* exception in constructor, avoid memory leak */ |
| 117 | free (h->xrange); |
| 118 | free (h->yrange); |
| 119 | free (h); |
| 120 | |
| 121 | GSL_ERROR_VAL ("failed to allocate space for histogram bins", |
| 122 | GSL_ENOMEM, 0); |
| 123 | } |
| 124 | |
| 125 | /* init histogram */ |
| 126 | |
| 127 | /* init ranges */ |
| 128 | |
| 129 | for (i = 0; i <= nx; i++) |
| 130 | { |
| 131 | h->xrange[i] = xrange[i]; |
| 132 | } |
| 133 | |
| 134 | for (j = 0; j <= ny; j++) |
| 135 | { |
| 136 | h->yrange[j] = yrange[j]; |
| 137 | } |
| 138 | |
| 139 | /* clear contents */ |
| 140 | |
| 141 | for (i = 0; i < nx; i++) |
| 142 | { |
| 143 | for (j = 0; j < ny; j++) |
| 144 | { |
| 145 | h->bin[i * ny + j] = 0; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | h->nx = nx; |
| 150 | h->ny = ny; |
| 151 | |
| 152 | return h; |
| 153 | } |