| 1 | ;; Updating and accessing histogram elements. |
|---|
| 2 | ;; Liam Healy, Mon Jan 1 2007 - 14:43 |
|---|
| 3 | ;; Time-stamp: <2008-03-27 23:09:26EDT updating-accessing.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | (defmfun increment-fix (histogram value) |
|---|
| 9 | "gsl_histogram_increment" |
|---|
| 10 | (((pointer histogram) :pointer) (value :double)) |
|---|
| 11 | :index increment |
|---|
| 12 | :export nil) |
|---|
| 13 | |
|---|
| 14 | (defmfun increment-weight (histogram value weight) |
|---|
| 15 | "gsl_histogram_accumulate" |
|---|
| 16 | (((pointer histogram) :pointer) (value :double) (weight :double)) |
|---|
| 17 | :index increment |
|---|
| 18 | :export nil) |
|---|
| 19 | |
|---|
| 20 | (defun-optionals increment (histogram value &optional weight) |
|---|
| 21 | -fix -weight |
|---|
| 22 | ;; FDL |
|---|
| 23 | "Update the histogram by adding the weight |
|---|
| 24 | (which defaults to 1.0) to the |
|---|
| 25 | bin whose range contains the coordinate x. |
|---|
| 26 | |
|---|
| 27 | If x lies in the valid range of the histogram then the function |
|---|
| 28 | returns zero to indicate success. If x is less than the lower |
|---|
| 29 | limit of the histogram then the function issues a warning :EDOM, and |
|---|
| 30 | none of bins are modified. Similarly, if the value of x is greater |
|---|
| 31 | than or equal to the upper limit of the histogram then the function |
|---|
| 32 | issues a warning :EDOM, and none of the bins are modified. The error |
|---|
| 33 | handler is not called, however, since it is often necessary to compute |
|---|
| 34 | histograms for a small range of a larger dataset, ignoring the values |
|---|
| 35 | outside the range of interest.") |
|---|
| 36 | |
|---|
| 37 | (defmfun maref ((histogram histogram) &rest i) |
|---|
| 38 | "gsl_histogram_get" |
|---|
| 39 | (((pointer histogram) :pointer) ((first i) size)) |
|---|
| 40 | :type :method |
|---|
| 41 | :c-return :double |
|---|
| 42 | :documentation ; FDL |
|---|
| 43 | "Return the contents of the i-th bin of the histogram. |
|---|
| 44 | If i lies outside the valid range of indices for the |
|---|
| 45 | histogram then an error (:EDOM) is signalled.") |
|---|
| 46 | |
|---|
| 47 | (defmfun range (histogram i) |
|---|
| 48 | "gsl_histogram_get_range" |
|---|
| 49 | (((pointer histogram) :pointer) (i size) |
|---|
| 50 | (lower :double) (upper :double)) |
|---|
| 51 | :documentation ; FDL |
|---|
| 52 | "Find the upper and lower range limits of the i-th |
|---|
| 53 | bin of the histogram. If the index i is valid then the |
|---|
| 54 | corresponding range limits are stored in lower and upper. |
|---|
| 55 | The lower limit is inclusive (i.e. events with this coordinate are |
|---|
| 56 | included in the bin) and the upper limit is exclusive (i.e. events with |
|---|
| 57 | the coordinate of the upper limit are excluded and fall in the |
|---|
| 58 | neighboring higher bin, if it exists). |
|---|
| 59 | If i lies outside the valid range of indices for |
|---|
| 60 | the histogram, then the error :EDOM is signalled.") |
|---|
| 61 | |
|---|
| 62 | (defmfun gsl-max-range (histogram) |
|---|
| 63 | "gsl_histogram_max" |
|---|
| 64 | (((pointer histogram) :pointer)) |
|---|
| 65 | :c-return :double |
|---|
| 66 | :documentation ; FDL |
|---|
| 67 | "The maximum upper range limit of the histogram.") |
|---|
| 68 | |
|---|
| 69 | (defmfun gsl-min-range (histogram) |
|---|
| 70 | "gsl_histogram_min" |
|---|
| 71 | (((pointer histogram) :pointer)) |
|---|
| 72 | :c-return :double |
|---|
| 73 | :documentation ; FDL |
|---|
| 74 | "The minimum lower range limit of the histogram.") |
|---|
| 75 | |
|---|
| 76 | (defmfun bins (histogram) |
|---|
| 77 | "gsl_histogram_bins" |
|---|
| 78 | (((pointer histogram) :pointer)) |
|---|
| 79 | :c-return :int |
|---|
| 80 | :documentation ; FDL |
|---|
| 81 | "The number of bins in the histogram.") |
|---|
| 82 | |
|---|
| 83 | (defmfun reset (histogram) |
|---|
| 84 | "gsl_histogram_reset" |
|---|
| 85 | (((pointer histogram) :pointer)) |
|---|
| 86 | :c-return :void |
|---|
| 87 | :documentation ; FDL |
|---|
| 88 | "Reset all the bins in the histogram to zero.") |
|---|
| 89 | |
|---|
| 90 | (defmfun histogram-find-1 (histogram value) |
|---|
| 91 | "gsl_histogram_find" |
|---|
| 92 | (((pointer histogram) :pointer) (value :double) (bin size)) |
|---|
| 93 | :export nil |
|---|
| 94 | :index histogram-find |
|---|
| 95 | :documentation ; FDL |
|---|
| 96 | "Finds the bin number which covers the coordinate value in |
|---|
| 97 | the histogram. The bin is located using a binary search. The |
|---|
| 98 | search includes an optimization for histograms with uniform |
|---|
| 99 | range, and will return the correct bin immediately in this |
|---|
| 100 | case. If the value is found in the range of the histogram |
|---|
| 101 | then the function returns the index. If value lies outside |
|---|
| 102 | the valid range of the histogram then the error :EDOM is |
|---|
| 103 | signalled.") |
|---|
| 104 | |
|---|
| 105 | (defmfun histogram-find-2 (histogram x-value y-value) |
|---|
| 106 | "gsl_histogram2d_find" |
|---|
| 107 | (((pointer histogram) :pointer) |
|---|
| 108 | (x-value :double) (y-value :double) |
|---|
| 109 | (xbin size) (ybin size)) |
|---|
| 110 | :export nil |
|---|
| 111 | :index histogram-find) |
|---|
| 112 | |
|---|
| 113 | (export 'histogram-find) |
|---|
| 114 | (defun histogram-find (histogram &rest values) |
|---|
| 115 | (histo-1d2d histogram histogram-find |
|---|
| 116 | ((first values)) |
|---|
| 117 | ((first values) (second values)))) |
|---|
| 118 | |
|---|
| 119 | (letm ((histo (histogram 10))) ; should be a gsl-warning here, how to check? |
|---|
| 120 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 121 | (increment histo -2.0d0)) |
|---|
| 122 | ;;; Examples and unit test |
|---|
| 123 | |
|---|
| 124 | #| |
|---|
| 125 | (make-tests histogram |
|---|
| 126 | ;; The first one gives a warning while compiling in SBCL, |
|---|
| 127 | ;; should only give a warning while runnin. |
|---|
| 128 | (letm ((histo (histogram 10))) |
|---|
| 129 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 130 | (increment histo -2.0d0)) |
|---|
| 131 | (letm ((histo (histogram 10))) |
|---|
| 132 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 133 | (increment histo 2.7d0) |
|---|
| 134 | (increment histo 6.9d0 2.0d0) |
|---|
| 135 | (maref histo 1)) |
|---|
| 136 | (letm ((histo (histogram 10))) |
|---|
| 137 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 138 | (increment histo 2.7d0) |
|---|
| 139 | (increment histo 6.9d0 2.0d0) |
|---|
| 140 | (maref histo 2)) |
|---|
| 141 | (letm ((histo (histogram 10))) |
|---|
| 142 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 143 | (increment histo 2.7d0) |
|---|
| 144 | (increment histo 6.9d0 2.0d0) |
|---|
| 145 | (maref histo 6)) |
|---|
| 146 | (letm ((histo (histogram 10))) |
|---|
| 147 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 148 | (increment histo 2.7d0) |
|---|
| 149 | (increment histo 6.9d0 2.0d0) |
|---|
| 150 | (maref histo 16)) |
|---|
| 151 | (letm ((histo (histogram 10))) |
|---|
| 152 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 153 | (increment histo 2.7d0) |
|---|
| 154 | (increment histo 6.9d0 2.0d0) |
|---|
| 155 | (values (gsl-min-range histo) (gsl-max-range histo))) |
|---|
| 156 | (letm ((histo (histogram 10))) |
|---|
| 157 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 158 | (increment histo 2.7d0) |
|---|
| 159 | (increment histo 6.9d0 2.0d0) |
|---|
| 160 | (bins histo)) |
|---|
| 161 | (letm ((histo (histogram 10))) |
|---|
| 162 | (set-ranges-uniform histo 0.0d0 10.0d0) |
|---|
| 163 | (increment histo 2.7d0) |
|---|
| 164 | (increment histo 6.9d0 2.0d0) |
|---|
| 165 | (histogram-find histo 5.5d0))) |
|---|
| 166 | |# |
|---|
| 167 | |
|---|
| 168 | (LISP-UNIT:DEFINE-TEST HISTOGRAM |
|---|
| 169 | (LISP-UNIT:ASSERT-ERROR |
|---|
| 170 | 'GSL-CONDITION |
|---|
| 171 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 172 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 173 | (INCREMENT HISTO -2.0d0))) |
|---|
| 174 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 175 | (LIST 0.0d0) |
|---|
| 176 | (MULTIPLE-VALUE-LIST |
|---|
| 177 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 178 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 179 | (INCREMENT HISTO 2.7d0) |
|---|
| 180 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 181 | (MAREF HISTO 1)))) |
|---|
| 182 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 183 | (LIST 1.0d0) |
|---|
| 184 | (MULTIPLE-VALUE-LIST |
|---|
| 185 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 186 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 187 | (INCREMENT HISTO 2.7d0) |
|---|
| 188 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 189 | (MAREF HISTO 2)))) |
|---|
| 190 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 191 | (LIST 2.0d0) |
|---|
| 192 | (MULTIPLE-VALUE-LIST |
|---|
| 193 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 194 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 195 | (INCREMENT HISTO 2.7d0) |
|---|
| 196 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 197 | (MAREF HISTO 6)))) |
|---|
| 198 | (LISP-UNIT:ASSERT-ERROR |
|---|
| 199 | 'GSL-CONDITION |
|---|
| 200 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 201 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 202 | (INCREMENT HISTO 2.7d0) |
|---|
| 203 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 204 | (MAREF HISTO 16))) |
|---|
| 205 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 206 | (LIST 0.0d0 10.0d0) |
|---|
| 207 | (MULTIPLE-VALUE-LIST |
|---|
| 208 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 209 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 210 | (INCREMENT HISTO 2.7d0) |
|---|
| 211 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 212 | (VALUES (GSL-MIN-RANGE HISTO) |
|---|
| 213 | (GSL-MAX-RANGE HISTO))))) |
|---|
| 214 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 215 | (LIST 10) |
|---|
| 216 | (MULTIPLE-VALUE-LIST |
|---|
| 217 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 218 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 219 | (INCREMENT HISTO 2.7d0) |
|---|
| 220 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 221 | (BINS HISTO)))) |
|---|
| 222 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 223 | (LIST 5) |
|---|
| 224 | (MULTIPLE-VALUE-LIST |
|---|
| 225 | (LETM ((HISTO (HISTOGRAM 10))) |
|---|
| 226 | (SET-RANGES-UNIFORM HISTO 0.0d0 10.0d0) |
|---|
| 227 | (INCREMENT HISTO 2.7d0) |
|---|
| 228 | (INCREMENT HISTO 6.9d0 2.0d0) |
|---|
| 229 | (HISTOGRAM-FIND HISTO 5.5d0))))) |
|---|