| 1 | /* histogram/pdf.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 | |
| 25 | #include "gsl_histogram__find.c"cxx . . $ `<file>/external/gsl/gsl_histogram__find.c`/ |
| 26 | |
| 27 | double |
| 28 | gsl_histogram_pdf_samplecxx . . $ gsl_histogram_pdf_sample(16d5fabdd4af40f8). (const gsl_histogram_pdfcxx . . $ gsl_histogram_pdf# * p, double r) |
| 29 | { |
| 30 | size_tcxx . . $ size_t# i; |
| 31 | int status; |
| 32 | |
| 33 | /* Wrap the exclusive top of the bin down to the inclusive bottom of |
| 34 | the bin. Since this is a single point it should not affect the |
| 35 | distribution. */ |
| 36 | |
| 37 | if (r == 1.0) |
| 38 | { |
| 39 | r = 0.0; |
| 40 | } |
| 41 | |
| 42 | status = findcxx . . $ find(eb889e606cf7f29f). (p->ncxx . . $ $anonymous_type_62a17a7d119001a7_1#n., p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum., r, &i); |
| 43 | |
| 44 | if (status) |
| 45 | { |
| 46 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("cannot find r in cumulative pdf", GSL_EDOM, 0); |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | double delta = (r - p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum.[i]) / (p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum.[i + 1] - p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum.[i]); |
| 51 | double x = p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range.[i] + delta * (p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range.[i + 1] - p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range.[i]); |
| 52 | return x; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | gsl_histogram_pdfcxx . . $ gsl_histogram_pdf# * |
| 57 | gsl_histogram_pdf_alloccxx . . $ gsl_histogram_pdf_alloc(fd689f857169c2d6). (const size_tcxx . . $ size_t# n) |
| 58 | { |
| 59 | gsl_histogram_pdfcxx . . $ gsl_histogram_pdf# *p; |
| 60 | |
| 61 | if (n == 0) |
| 62 | { |
| 63 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("histogram pdf length n must be positive integer", |
| 64 | GSL_EDOM, 0); |
| 65 | } |
| 66 | |
| 67 | p = (gsl_histogram_pdfcxx . . $ gsl_histogram_pdf# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (gsl_histogram_pdfcxx . . $ gsl_histogram_pdf#)); |
| 68 | |
| 69 | if (p == 0) |
| 70 | { |
| 71 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram pdf struct", |
| 72 | GSL_ENOMEM, 0); |
| 73 | } |
| 74 | |
| 75 | p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). ((n + 1) * sizeof (double)); |
| 76 | |
| 77 | if (p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range. == 0) |
| 78 | { |
| 79 | freecxx . . $ free(5d22bdacc48458e8). (p); /* exception in constructor, avoid memory leak */ |
| 80 | |
| 81 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram pdf ranges", |
| 82 | GSL_ENOMEM, 0); |
| 83 | } |
| 84 | |
| 85 | p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum. = (double *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). ((n + 1) * sizeof (double)); |
| 86 | |
| 87 | if (p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum. == 0) |
| 88 | { |
| 89 | freecxx . . $ free(5d22bdacc48458e8). (p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range.); |
| 90 | freecxx . . $ free(5d22bdacc48458e8). (p); /* exception in constructor, avoid memory leak */ |
| 91 | |
| 92 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for histogram pdf sums", |
| 93 | GSL_ENOMEM, 0); |
| 94 | } |
| 95 | |
| 96 | p->ncxx . . $ $anonymous_type_62a17a7d119001a7_1#n. = n; |
| 97 | |
| 98 | return p; |
| 99 | } |
| 100 | |
| 101 | int |
| 102 | gsl_histogram_pdf_initcxx . . $ gsl_histogram_pdf_init(cfaf1669fcd9a9f6). (gsl_histogram_pdfcxx . . $ gsl_histogram_pdf# * p, const gsl_histogramcxx . . $ gsl_histogram# * h) |
| 103 | { |
| 104 | size_tcxx . . $ size_t# i; |
| 105 | size_tcxx . . $ size_t# n = p->ncxx . . $ $anonymous_type_62a17a7d119001a7_1#n.; |
| 106 | |
| 107 | if (n != h->ncxx . . $ $anonymous_type_62a17a7d119001a7_0#n.) |
| 108 | { |
| 109 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("histogram length must match pdf length", GSL_EINVAL); |
| 110 | } |
| 111 | |
| 112 | for (i = 0; i < n; i++) |
| 113 | { |
| 114 | if (h->bincxx . . $ $anonymous_type_62a17a7d119001a7_0#bin.[i] < 0) |
| 115 | { |
| 116 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("histogram bins must be non-negative to compute" |
| 117 | "a probability distribution", GSL_EDOM); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | for (i = 0; i < n + 1; i++) |
| 122 | { |
| 123 | p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range.[i] = h->rangecxx . . $ $anonymous_type_62a17a7d119001a7_0#range.[i]; |
| 124 | } |
| 125 | |
| 126 | { |
| 127 | double mean = 0, sum = 0; |
| 128 | |
| 129 | for (i = 0; i < n; i++) |
| 130 | { |
| 131 | mean += (h->bincxx . . $ $anonymous_type_62a17a7d119001a7_0#bin.[i] - mean) / ((double) (i + 1)); |
| 132 | } |
| 133 | |
| 134 | p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum.[0] = 0; |
| 135 | |
| 136 | for (i = 0; i < n; i++) |
| 137 | { |
| 138 | sum += (h->bincxx . . $ $anonymous_type_62a17a7d119001a7_0#bin.[i] / mean) / n; |
| 139 | p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum.[i + 1] = sum; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void |
| 148 | gsl_histogram_pdf_freecxx . . $ gsl_histogram_pdf_free(7f777ba31d3ca147). (gsl_histogram_pdfcxx . . $ gsl_histogram_pdf# * p) |
| 149 | { |
| 150 | freecxx . . $ free(5d22bdacc48458e8). (p->rangecxx . . $ $anonymous_type_62a17a7d119001a7_1#range.); |
| 151 | freecxx . . $ free(5d22bdacc48458e8). (p->sumcxx . . $ $anonymous_type_62a17a7d119001a7_1#sum.); |
| 152 | freecxx . . $ free(5d22bdacc48458e8). (p); |
| 153 | } |