| 1 | /* histogram/pdf2d.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * 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 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 21 | #include <stdlib.h> |
| 22 | #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/ |
| 23 | #include "gsl_histogram.h"cxx . . $ `<file>/external/gsl/gsl_histogram.h`/ |
| 24 | #include "gsl_histogram2d.h"cxx . . $ `<file>/external/gsl/gsl_histogram2d.h`/ |
| 25 | |
| 26 | #include "gsl_histogram__find.c"cxx . . $ `<file>/external/gsl/gsl_histogram__find.c`/ |
| 27 | |
| 28 | int |
| 29 | gsl_histogram2d_pdf_samplecxx . . $ gsl_histogram2d_pdf_sample(e0829e6057188aa2). (const gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf# * p, |
| 30 | double r1, double r2, |
| 31 | double *x, double *y) |
| 32 | { |
| 33 | size_t k; |
| 34 | int status; |
| 35 | |
| 36 | /* Wrap the exclusive top of the bin down to the inclusive bottom of |
| 37 | the bin. Since this is a single point it should not affect the |
| 38 | distribution. */ |
| 39 | |
| 40 | if (r2 == 1.0) |
| 41 | { |
| 42 | r2 = 0.0; |
| 43 | } |
| 44 | if (r1 == 1.0) |
| 45 | { |
| 46 | r1 = 0.0; |
| 47 | } |
| 48 | |
| 49 | status = findcxx . . $ find(eb889e606cf7f29f). (p->nxcxx . . $ $anonymous_type_2826d176da817e9d_1#nx. * p->nycxx . . $ $anonymous_type_2826d176da817e9d_1#ny., p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum., r1, &k); |
| 50 | |
| 51 | if (status) |
| 52 | { |
| 53 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("cannot find r1 in cumulative pdf", GSL_EDOM); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | size_t i = k / p->nycxx . . $ $anonymous_type_2826d176da817e9d_1#ny.; |
| 58 | size_t j = k - (i * p->nycxx . . $ $anonymous_type_2826d176da817e9d_1#ny.); |
| 59 | double delta = (r1 - p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum.[k]) / (p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum.[k + 1] - p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum.[k]); |
| 60 | *x = p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.[i] + delta * (p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.[i + 1] - p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.[i]); |
| 61 | *y = p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange.[j] + r2 * (p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange.[j + 1] - p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange.[j]); |
| 62 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf# * |
| 67 | gsl_histogram2d_pdf_alloccxx . . $ gsl_histogram2d_pdf_alloc(d84e4e360c82f46c). (const size_t nx, const size_t ny) |
| 68 | { |
| 69 | const size_t n = nx * ny; |
| 70 | |
| 71 | gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf# *p; |
| 72 | |
| 73 | if (n == 0) |
| 74 | { |
| 75 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("histogram2d pdf length n must be positive integer", |
| 76 | GSL_EDOM, 0); |
| 77 | } |
| 78 | |
| 79 | p = (gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf#)); |
| 80 | |
| 81 | if (p == 0) |
| 82 | { |
| 83 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram2d pdf struct", |
| 84 | GSL_ENOMEM, 0); |
| 85 | } |
| 86 | |
| 87 | p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). ((nx + 1) * sizeof (double)); |
| 88 | |
| 89 | if (p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange. == 0) |
| 90 | { |
| 91 | freecxx . . $ free(5d22bdacc48458e8). (p); /* exception in constructor, avoid memory leak */ |
| 92 | |
| 93 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram2d pdf xranges", |
| 94 | GSL_ENOMEM, 0); |
| 95 | } |
| 96 | |
| 97 | p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). ((ny + 1) * sizeof (double)); |
| 98 | |
| 99 | if (p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange. == 0) |
| 100 | { |
| 101 | freecxx . . $ free(5d22bdacc48458e8). (p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.); |
| 102 | freecxx . . $ free(5d22bdacc48458e8). (p); /* exception in constructor, avoid memory leak */ |
| 103 | |
| 104 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram2d pdf yranges", |
| 105 | GSL_ENOMEM, 0); |
| 106 | } |
| 107 | |
| 108 | p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). ((n + 1) * sizeof (double)); |
| 109 | |
| 110 | if (p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum. == 0) |
| 111 | { |
| 112 | freecxx . . $ free(5d22bdacc48458e8). (p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange.); |
| 113 | freecxx . . $ free(5d22bdacc48458e8). (p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.); |
| 114 | freecxx . . $ free(5d22bdacc48458e8). (p); /* exception in constructor, avoid memory leak */ |
| 115 | |
| 116 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram2d pdf sums", |
| 117 | GSL_ENOMEM, 0); |
| 118 | } |
| 119 | |
| 120 | p->nxcxx . . $ $anonymous_type_2826d176da817e9d_1#nx. = nx; |
| 121 | p->nycxx . . $ $anonymous_type_2826d176da817e9d_1#ny. = ny; |
| 122 | |
| 123 | return p; |
| 124 | } |
| 125 | |
| 126 | int |
| 127 | gsl_histogram2d_pdf_initcxx . . $ gsl_histogram2d_pdf_init(fde4643c72443cc9). (gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf# * p, const gsl_histogram2dcxx . . $ gsl_histogram2d# * h) |
| 128 | { |
| 129 | size_t i; |
| 130 | const size_t nx = p->nxcxx . . $ $anonymous_type_2826d176da817e9d_1#nx.; |
| 131 | const size_t ny = p->nycxx . . $ $anonymous_type_2826d176da817e9d_1#ny.; |
| 132 | const size_t n = nx * ny; |
| 133 | |
| 134 | if (nx != h->nxcxx . . $ $anonymous_type_2826d176da817e9d_0#nx. || ny != h->nycxx . . $ $anonymous_type_2826d176da817e9d_0#ny.) |
| 135 | { |
| 136 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("histogram2d size must match pdf size", GSL_EDOM); |
| 137 | } |
| 138 | |
| 139 | for (i = 0; i < n; i++) |
| 140 | { |
| 141 | if (h->bincxx . . $ $anonymous_type_2826d176da817e9d_0#bin.[i] < 0) |
| 142 | { |
| 143 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("histogram bins must be non-negative to compute" |
| 144 | "a probability distribution", GSL_EDOM); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | for (i = 0; i < nx + 1; i++) |
| 149 | { |
| 150 | p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.[i] = h->xrangecxx . . $ $anonymous_type_2826d176da817e9d_0#xrange.[i]; |
| 151 | } |
| 152 | |
| 153 | for (i = 0; i < ny + 1; i++) |
| 154 | { |
| 155 | p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange.[i] = h->yrangecxx . . $ $anonymous_type_2826d176da817e9d_0#yrange.[i]; |
| 156 | } |
| 157 | |
| 158 | { |
| 159 | double mean = 0, sum = 0; |
| 160 | |
| 161 | for (i = 0; i < n; i++) |
| 162 | { |
| 163 | mean += (h->bincxx . . $ $anonymous_type_2826d176da817e9d_0#bin.[i] - mean) / ((double) (i + 1)); |
| 164 | } |
| 165 | |
| 166 | p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum.[0] = 0; |
| 167 | |
| 168 | for (i = 0; i < n; i++) |
| 169 | { |
| 170 | sum += (h->bincxx . . $ $anonymous_type_2826d176da817e9d_0#bin.[i] / mean) / n; |
| 171 | p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum.[i + 1] = sum; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | void |
| 180 | gsl_histogram2d_pdf_freecxx . . $ gsl_histogram2d_pdf_free(56347440927b480c). (gsl_histogram2d_pdfcxx . . $ gsl_histogram2d_pdf# * p) |
| 181 | { |
| 182 | freecxx . . $ free(5d22bdacc48458e8). (p->xrangecxx . . $ $anonymous_type_2826d176da817e9d_1#xrange.); |
| 183 | freecxx . . $ free(5d22bdacc48458e8). (p->yrangecxx . . $ $anonymous_type_2826d176da817e9d_1#yrange.); |
| 184 | freecxx . . $ free(5d22bdacc48458e8). (p->sumcxx . . $ $anonymous_type_2826d176da817e9d_1#sum.); |
| 185 | freecxx . . $ free(5d22bdacc48458e8). (p); |
| 186 | } |