| 1 | ;; The histogram structure |
|---|
| 2 | ;; Liam Healy, Mon Jan 1 2007 - 11:32 |
|---|
| 3 | ;; Time-stamp: <2008-02-19 22:33:01EST histogram.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | ;;; Define, make, copy histograms in one or two dimensions. |
|---|
| 9 | |
|---|
| 10 | (defgo histogram (size &rest from-or-ranges) |
|---|
| 11 | (list `(make-histogram ,size |
|---|
| 12 | ,(when from-or-ranges |
|---|
| 13 | `(when (typep (first ,from-or-ranges) 'histogram) |
|---|
| 14 | (first ,from-or-ranges)))) |
|---|
| 15 | 'free |
|---|
| 16 | (when from-or-ranges |
|---|
| 17 | (lambda (sym) |
|---|
| 18 | `(unless (typep (first ,from-or-ranges) 'histogram) |
|---|
| 19 | (set-ranges ,sym ,@from-or-ranges)))))) |
|---|
| 20 | |
|---|
| 21 | (defclass histogram () |
|---|
| 22 | ((pointer |
|---|
| 23 | :initarg :pointer :accessor pointer |
|---|
| 24 | :documentation "A C pointer to the GSL representation of the histogram.") |
|---|
| 25 | (number-of-bins :initarg :number-of-bins :accessor number-of-bins)) |
|---|
| 26 | (:documentation |
|---|
| 27 | "A histogram, including bin boundaries and bin contents.")) |
|---|
| 28 | |
|---|
| 29 | (defmfun alloc-histo-1 (object) |
|---|
| 30 | "gsl_histogram_alloc" |
|---|
| 31 | (((number-of-bins object) size)) |
|---|
| 32 | :export nil |
|---|
| 33 | :index (letm histogram) |
|---|
| 34 | :c-return (cr :pointer) |
|---|
| 35 | :return ((assign-pointer object cr))) |
|---|
| 36 | |
|---|
| 37 | (defmfun alloc-histo-2 (object) |
|---|
| 38 | "gsl_histogram2d_alloc" |
|---|
| 39 | (((first (number-of-bins object)) size) |
|---|
| 40 | ((second (number-of-bins object)) size)) |
|---|
| 41 | :export nil |
|---|
| 42 | :index (letm histogram) |
|---|
| 43 | :c-return (cr :pointer) |
|---|
| 44 | :return ((assign-pointer object cr))) |
|---|
| 45 | |
|---|
| 46 | (defmacro histo-1d2d |
|---|
| 47 | (object base-function-name &optional args1 args2) |
|---|
| 48 | `(if (listp (number-of-bins ,object)) |
|---|
| 49 | (,(intern |
|---|
| 50 | (concatenate 'string (string base-function-name) "-2")) |
|---|
| 51 | ,object ,@(or args2 args1)) |
|---|
| 52 | (,(intern |
|---|
| 53 | (concatenate 'string (string base-function-name) "-1")) |
|---|
| 54 | ,object ,@args1))) |
|---|
| 55 | |
|---|
| 56 | (defmethod alloc ((object histogram)) |
|---|
| 57 | (histo-1d2d object alloc-histo)) |
|---|
| 58 | |
|---|
| 59 | (defmfun free-histo-1 (object) |
|---|
| 60 | "gsl_histogram_free" |
|---|
| 61 | (((pointer object) :pointer)) |
|---|
| 62 | :c-return :void |
|---|
| 63 | :export nil |
|---|
| 64 | :index (letm histogram)) |
|---|
| 65 | |
|---|
| 66 | (defmfun free-histo-2 (object) |
|---|
| 67 | "gsl_histogram2d_free" |
|---|
| 68 | (((pointer object) :pointer)) |
|---|
| 69 | :c-return :void |
|---|
| 70 | :export nil |
|---|
| 71 | :index (letm histogram)) |
|---|
| 72 | |
|---|
| 73 | (defmethod free ((object histogram)) |
|---|
| 74 | (histo-1d2d object free-histo)) |
|---|
| 75 | |
|---|
| 76 | ;;; GSL documentation does not state what the return value for the |
|---|
| 77 | ;;; C function means; assumed to be error code. |
|---|
| 78 | (defmfun set-ranges-1 (histogram ranges) |
|---|
| 79 | "gsl_histogram_set_ranges" |
|---|
| 80 | (((pointer histogram) :pointer) |
|---|
| 81 | ((gsl-array ranges) :pointer) ((dim0 ranges) size)) |
|---|
| 82 | :export nil |
|---|
| 83 | :index (letm histogram) |
|---|
| 84 | :documentation ; FDL |
|---|
| 85 | "Set the ranges of the existing histogram using |
|---|
| 86 | the gsl-vector of ranges. The values of the histogram |
|---|
| 87 | bins are reset to zero. The ranges array should contain the |
|---|
| 88 | desired bin limits. The ranges can be arbitrary, subject to the |
|---|
| 89 | restriction that they are monotonically increasing.") |
|---|
| 90 | |
|---|
| 91 | (defmfun set-ranges-2 (histogram x-ranges y-ranges) |
|---|
| 92 | "gsl_histogram2d_set_ranges" |
|---|
| 93 | (((pointer histogram) :pointer) |
|---|
| 94 | ((gsl-array x-ranges) :pointer) ((dim0 x-ranges) size) |
|---|
| 95 | ((gsl-array y-ranges) :pointer) ((dim0 y-ranges) size)) |
|---|
| 96 | :export nil |
|---|
| 97 | :index (letm histogram)) |
|---|
| 98 | |
|---|
| 99 | (export 'set-ranges) |
|---|
| 100 | (defun set-ranges (histogram &rest ranges) |
|---|
| 101 | "Set the ranges of the existing histogram using |
|---|
| 102 | the gsl-vector(s) of ranges. The values of the histogram |
|---|
| 103 | bins are reset to zero. The ranges array(s) should contain the |
|---|
| 104 | desired bin limits. The ranges can be arbitrary, subject to the |
|---|
| 105 | restriction that they are monotonically increasing. |
|---|
| 106 | For a 2d histogram, supply two gsl-vectors." |
|---|
| 107 | (histo-1d2d histogram set-ranges |
|---|
| 108 | ((first ranges)) |
|---|
| 109 | ((first ranges) (second ranges)))) |
|---|
| 110 | |
|---|
| 111 | ;;; GSL documentation does not state what the return value for the |
|---|
| 112 | ;;; C function means; assumed to be error code. |
|---|
| 113 | (defmfun set-ranges-uniform-1 (histogram minimum maximum) |
|---|
| 114 | "gsl_histogram_set_ranges_uniform" |
|---|
| 115 | (((pointer histogram) :pointer) (minimum :double) (maximum :double)) |
|---|
| 116 | :export nil |
|---|
| 117 | :index set-ranges-uniform |
|---|
| 118 | :documentation ; FDL |
|---|
| 119 | "Set the ranges of the existing histogram h to cover |
|---|
| 120 | the range xmin to xmax uniformly. The values of the |
|---|
| 121 | histogram bins are reset to zero. The bin ranges are shown in the table |
|---|
| 122 | below, |
|---|
| 123 | bin[0] corresponds to xmin <= x < xmin + d |
|---|
| 124 | bin[1] corresponds to xmin + d <= x < xmin + 2 d |
|---|
| 125 | ...... |
|---|
| 126 | bin[n-1] corresponds to xmin + (n-1)d <= x < xmax |
|---|
| 127 | where d is the bin spacing, d = (xmax-xmin)/n.") |
|---|
| 128 | |
|---|
| 129 | ;;; GSL documentation does not state what the return value for the |
|---|
| 130 | ;;; C function means; assumed to be error code. |
|---|
| 131 | (defmfun set-ranges-uniform-2 |
|---|
| 132 | (histogram x-minimum x-maximum y-minimum y-maximum) |
|---|
| 133 | "gsl_histogram2d_set_ranges_uniform" |
|---|
| 134 | (((pointer histogram) :pointer) |
|---|
| 135 | (x-minimum :double) (x-maximum :double) |
|---|
| 136 | (y-minimum :double) (y-maximum :double)) |
|---|
| 137 | :export nil |
|---|
| 138 | :index set-ranges-uniform) |
|---|
| 139 | |
|---|
| 140 | (export 'set-ranges-uniform) |
|---|
| 141 | (defun set-ranges-uniform (histogram &rest limits) |
|---|
| 142 | ;; FDL |
|---|
| 143 | "Set the ranges of the existing histogram h to cover |
|---|
| 144 | the range xmin to xmax uniformly. The values of the |
|---|
| 145 | histogram bins are reset to zero. The bin ranges are shown in the table |
|---|
| 146 | below, |
|---|
| 147 | bin[0] corresponds to xmin <= x < xmin + d |
|---|
| 148 | bin[1] corresponds to xmin + d <= x < xmin + 2 d |
|---|
| 149 | ...... |
|---|
| 150 | bin[n-1] corresponds to xmin + (n-1)d <= x < xmax |
|---|
| 151 | where d is the bin spacing, d = (xmax-xmin)/n." |
|---|
| 152 | (histo-1d2d histogram set-ranges-uniform |
|---|
| 153 | ((first limits) (second limits)) |
|---|
| 154 | ((first limits) (second limits) |
|---|
| 155 | (third limits) (fourth limits)))) |
|---|
| 156 | |
|---|
| 157 | (defmfun copy-1 (destination source) |
|---|
| 158 | "gsl_histogram_memcpy" |
|---|
| 159 | (((pointer destination) :pointer) ((pointer source) :pointer)) |
|---|
| 160 | :export nil |
|---|
| 161 | :index copy |
|---|
| 162 | :documentation ; FDL |
|---|
| 163 | "Copy the histogram source into the pre-existing |
|---|
| 164 | histogram destination, making the latter into |
|---|
| 165 | an exact copy of the former. |
|---|
| 166 | The two histograms must be of the same size.") |
|---|
| 167 | |
|---|
| 168 | (defmfun copy-2 (destination source) |
|---|
| 169 | "gsl_histogram2d_memcpy" |
|---|
| 170 | (((pointer destination) :pointer) ((pointer source) :pointer)) |
|---|
| 171 | :export nil |
|---|
| 172 | :index copy) |
|---|
| 173 | |
|---|
| 174 | (defmethod copy ((destination histogram) (source histogram)) |
|---|
| 175 | (histo-1d2d destination copy (source))) |
|---|
| 176 | |
|---|
| 177 | (defmfun clone-1 (source) |
|---|
| 178 | "gsl_histogram_memcpy" |
|---|
| 179 | (((pointer source) :pointer)) |
|---|
| 180 | :export nil |
|---|
| 181 | :index clone |
|---|
| 182 | :documentation ; FDL |
|---|
| 183 | "Create a new histogram which is an |
|---|
| 184 | exact copy of the histogram source, and return the pointer.") |
|---|
| 185 | |
|---|
| 186 | (defmfun clone-2 (source) |
|---|
| 187 | "gsl_histogram2d_memcpy" |
|---|
| 188 | (((pointer source) :pointer)) |
|---|
| 189 | :export nil |
|---|
| 190 | :index clone |
|---|
| 191 | :documentation ; FDL |
|---|
| 192 | "Create a new histogram which is an |
|---|
| 193 | exact copy of the histogram source, and return the pointer.") |
|---|
| 194 | |
|---|
| 195 | (export 'clone) |
|---|
| 196 | (defun clone (source) |
|---|
| 197 | ;; FDL |
|---|
| 198 | "Create a new histogram which is an |
|---|
| 199 | exact copy of the histogram source, and return the pointer." |
|---|
| 200 | (histo-1d2d source clone)) |
|---|
| 201 | |
|---|
| 202 | (defun make-histogram (size &optional from) |
|---|
| 203 | "Make a histogram, optionally filling it with |
|---|
| 204 | data from an existing histogram." |
|---|
| 205 | (let ((ret |
|---|
| 206 | (make-instance 'histogram :number-of-bins size |
|---|
| 207 | :pointer (when from (clone from))))) |
|---|
| 208 | (unless (pointer ret) |
|---|
| 209 | (alloc ret)) |
|---|
| 210 | ret)) |
|---|