both.lisp 11.6 KB
Newer Older
1
2
;; Functions for both vectors and matrices.
;; Liam Healy 2008-04-26 20:48:44EDT both.lisp
3
;; Time-stamp: <2009-11-18 10:46:49EST both.lisp>
4
5
6

(in-package :gsl)

7
8
9
10
;;;;****************************************************************************
;;;; Administrative (internal use)
;;;;****************************************************************************

Liam Healy's avatar
Liam Healy committed
11
(defmfun alloc-from-block ((object vector) blockptr)
12
  ("gsl_" :category :type "_alloc_from_block")
Liam Healy's avatar
Liam Healy committed
13
  ((blockptr :pointer)
14
   (0 sizet)				; offset
15
   ((size object) sizet)			; number of elements
16
   (1 sizet))				; stride
17
18
  :definition :generic
  :c-return :pointer
19
  :export nil
20
  :documentation "Allocate memory for the GSL struct given a block pointer.")
21

Liam Healy's avatar
Liam Healy committed
22
(defmfun alloc-from-block ((object matrix) blockptr)
23
  ("gsl_" :category :type "_alloc_from_block")
Liam Healy's avatar
Liam Healy committed
24
  ((blockptr :pointer)
25
26
27
28
29
   (0 sizet)				; offset
   ((first (dimensions object)) sizet)	; number of rows
   ((second (dimensions object)) sizet)	; number of columns
   ((second (dimensions object)) sizet))	; "tda" = number of columns for now
  :definition :methods
30
31
32
33
34
35
36
  :c-return :pointer
  :export nil)

;;;;****************************************************************************
;;;; Bulk operations
;;;;****************************************************************************

37
38
39
40
(defmfun set-all ((object both) value)
  ("gsl_" :category :type "_set_all")
  (((mpointer object) :pointer) (value :element-c-type))
  :definition :generic
Liam Healy's avatar
Liam Healy committed
41
  :element-types #+fsbv t #-fsbv :no-complex
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  :inputs (object)
  :outputs (object)
  :c-return :void
  :documentation "Set all elements to the value.")

(defmfun set-zero ((object both))
  ("gsl_"  :category :type "_set_zero")
  (((mpointer object) :pointer))
  :definition :generic
  :inputs (object)
  :outputs (object)
  :c-return :void
  :documentation "Set all elements to 0.")

Liam Healy's avatar
Liam Healy committed
56
57
58
59
60
61
62
63
64
65
(defmfun copy-to-destination
    ((source both) (destination both))
  ("gsl_" :category :type "_memcpy")
  (((mpointer destination) :pointer) ((mpointer source) :pointer))
  :definition :methods
  :inputs (source)
  :outputs (destination))

(defmfun copy-making-destination
    ((source both) &aux 
Liam Healy's avatar
Liam Healy committed
66
67
     (destination
      (make-marray element-type :dimensions (dimensions source))))
68
69
  ("gsl_" :category :type "_memcpy")
  (((mpointer destination) :pointer) ((mpointer source) :pointer))
Liam Healy's avatar
Liam Healy committed
70
  :definition :methods
71
  :inputs (source)
Liam Healy's avatar
Liam Healy committed
72
73
  :outputs (destination)
  :return (destination))
74
75
76
77
78
79
80
81
82
83
84

(defmfun swap ((a both) (b both))
  ("gsl_" :category :type "_swap")
  (((mpointer a) :pointer) ((mpointer b) :pointer))
  :definition :generic
  :inputs (a b)
  :outputs (a b)
  :documentation			; FDL
  "Exchange the elements of a and b
   by copying.  The two must have the same dimensions.")

85
86
87
;;; These functions are here as debugging tools only; they do not insure
;;; coherency between the C and CL sides and therefore should not be
;;; used in production.
88
;;; For matrix functions, there should be another index argument
89
#|
90
(defmfun set-value ((object vector) index value)
91
  ("gsl_"  :category :type "_set")
92
  (((mpointer object) :pointer) (index sizet) (value :element-c-type))
93
  :definition :generic
Liam Healy's avatar
Liam Healy committed
94
  :element-types #+fsbv t #-fsbv :no-complex
95
96
97
98
99
100
101
  :inputs (object)
  :outputs (object)
  :c-return :void
  :export nil				; debugging only
  :documentation
  "Set single element to the value.  For debugging only; do not use.")

102
(defmfun get-value ((object vector) index)
103
  ("gsl_"  :category :type "_get")
104
  (((mpointer object) :pointer) (index sizet))
105
106
107
108
109
110
  :definition :generic
  :inputs (object)
  :c-return :element-c-type
  :export nil				; debugging only
  :documentation
  "Set single element to the value.  For debugging only; do not use.")
111
|#
112

113
;;;;****************************************************************************
114
;;;; Elementwise arithmetic operations overwriting an array
115
116
;;;;****************************************************************************

117
118
119
120
121
;;; These are overwriting elementwise arithmetic operations.  Where
;;; there are two array arguments, the operation acts on both arrays
;;; on corresponding elements.  The result is placed in the first
;;; array, overwriting the original contents.

Liam Healy's avatar
Liam Healy committed
122
123
124
;;; Errors in GSL:
;;; 1) complex operations in older versions of GSL
;;; https://savannah.gnu.org/bugs/index.php?22478
125
;;; Fixed in 1.12, can change the :no-complex spec.
Liam Healy's avatar
Liam Healy committed
126

127
(defmfun elt+ ((a both) (b both))
128
  ("gsl_" :category :type "_add")
129
  (((mpointer a) :pointer) ((mpointer b) :pointer))
130
  :definition :generic
131
  :element-types #.(if (have-at-least-gsl-version '(1 12)) t :no-complex)
132
133
134
135
136
137
138
  :inputs (a b)
  :outputs (a)
  :return (a)
  :documentation			; FDL
  "Add the elements of b to the elements of vector a
   The two must have the same dimensions.")

139
(defmfun elt+ ((a both) (x float))
140
141
142
  ("gsl_" :category :type "_add_constant")
  (((mpointer a) :pointer) (x :double))
  :definition :methods
143
  :element-types :no-complex
144
145
146
147
148
149
  :inputs (a)
  :outputs (a)
  :return (a)
  :documentation			; FDL
  "Add the scalar double-float x to all the elements of array a.")

150
151
152
153
154
155
156
157
158
159
160
161
162
#+fsbv
(defmfun elt+ ((a both) (x complex))
  ("gsl_" :category :type "_add_constant")
  (((mpointer a) :pointer) (x :element-c-type))
  :definition :methods
  :element-types :complex
  :inputs (a)
  :outputs (a)
  :return (a)
  :gsl-version (1 12)
  :documentation			; FDL
  "Add the scalar complex x to all the elements of array a.")

163
164
(defmethod elt+ ((x float) (a marray))
  (elt+ a x))
165
  
166
(defmfun elt- ((a both) (b both))
167
168
169
  ("gsl_" :category :type "_sub")
  (((mpointer a) :pointer) ((mpointer b) :pointer))
  :definition :generic
170
  :element-types #.(if (have-at-least-gsl-version '(1 12)) t :no-complex)
171
172
173
174
175
176
  :inputs (a b)
  :outputs (a)
  :return (a)
  :documentation			; FDL
  "Subtract the elements of b from the elements of a.
   The two must have the same dimensions.")
177

178
179
180
(defmethod elt- ((a marray) (x float))
  (elt+ a (- x)))

181
(defmfun elt* ((a vector) (b vector))
182
183
184
  ("gsl_" :category :type "_mul")
  (((mpointer a) :pointer) ((mpointer b) :pointer))
  :definition :generic
185
  :element-types #.(if (have-at-least-gsl-version '(1 12)) t :no-complex)
186
187
188
189
190
191
192
  :inputs (a b)
  :outputs (a)
  :return (a)
  :documentation			; FDL
  "Multiply the elements of a by the elements of b.
   The two must have the same dimensions.")

193
(defmfun elt* ((a matrix) (b matrix))
194
195
  ("gsl_" :category :type "_mul_elements")
  (((mpointer a) :pointer) ((mpointer b) :pointer))
196
  :definition :methods
Liam Healy's avatar
Liam Healy committed
197
  :element-types :no-complex
198
199
200
201
  :inputs (a b)
  :outputs (a)
  :return (a))

202
(defmfun elt/ ((a vector) (b vector))
203
204
205
  ("gsl_" :category :type "_div")
  (((mpointer a) :pointer) ((mpointer b) :pointer))
  :definition :generic
206
  :element-types #.(if (have-at-least-gsl-version '(1 12)) t :no-complex)
207
208
209
210
211
212
213
  :inputs (a b)
  :outputs (a)
  :return (a)
  :documentation			; FDL
  "Divide the elements of a by the elements of b.
   The two must have the same dimensions.")

214
(defmfun elt/ ((a matrix) (b matrix))
215
216
  ("gsl_" :category :type "_div_elements")
  (((mpointer a) :pointer) ((mpointer b) :pointer))
217
  :definition :methods
Liam Healy's avatar
Liam Healy committed
218
  :element-types :no-complex
219
220
221
222
  :inputs (a b)
  :outputs (a)
  :return (a))

223
(defmethod elt/ ((a marray) (x number))
224
225
  (elt* a (/ x)))

226
(defmfun elt* ((a both) (x float))
227
  ("gsl_" :category :type "_scale")
228
  (((mpointer a) :pointer) (x :double))
229
  :definition :methods
230
  :element-types :no-complex
231
232
233
234
  :inputs (a)
  :outputs (a)
  :return (a)
  :documentation			; FDL
235
  "Multiply the elements of a by the scalar double-float factor x.")
236

237
238
239
240
241
242
243
244
245
246
247
248
249
#+fsbv
(defmfun elt* ((a both) (x complex))
  ("gsl_" :category :type "_scale")
  (((mpointer a) :pointer) (x :element-c-type))
  :definition :methods
  :element-types :complex
  :inputs (a)
  :outputs (a)
  :return (a)
  :gsl-version (1 12)
  :documentation			; FDL
  "Multiply the elements of a by the scalar complex factor x.")

250
251
(defmethod elt* ((x float) (a marray))
  (elt* a x))
252
253
254
255
256
257
258
259
260

;;;;****************************************************************************
;;;; Maximum and minimum elements
;;;;****************************************************************************

(defmfun mmax ((a both))
  ("gsl_" :category :type "_max")
  (((mpointer a) :pointer))
  :definition :generic
261
  :element-types :no-complex
262
263
264
265
266
267
268
269
270
  :inputs (a)
  :c-return :element-c-type
  :documentation			; FDL
  "The maximum value in a.")

(defmfun mmin ((a both))
  ("gsl_" :category :type "_min")
  (((mpointer a) :pointer))
  :definition :generic
271
  :element-types :no-complex
272
273
274
275
276
277
278
  :inputs (a)
  :c-return :element-c-type
  :documentation			; FDL
  "The minimum value in a.")

(defmfun minmax ((a both))
  ("gsl_" :category :type "_minmax")
279
280
281
  (((mpointer a) :pointer)
   (min (:pointer :element-c-type))
   (max (:pointer :element-c-type)))
282
  :definition :generic
283
  :element-types :no-complex
284
285
286
287
288
289
290
291
292
  :inputs (a)
  :c-return :void
  :documentation			; FDL
  "The minimum and maximum values in a.")

(defmfun min-index ((a vector))
  ("gsl_" :category :type "_min_index")
  (((mpointer a) :pointer))
  :definition :generic
293
  :element-types :no-complex
294
  :inputs (a)
295
  :c-return sizet
296
297
298
299
300
301
  :documentation			; FDL
  "The index of the minimum value in a.  When there are several
  equal minimum elements, then the lowest index is returned.")

(defmfun min-index ((a matrix))
  ("gsl_" :category :type "_min_index")
302
  (((mpointer a) :pointer) (imin (:pointer sizet)) (jmin (:pointer sizet)))
303
  :definition :methods
304
  :element-types :no-complex
305
306
307
308
309
310
311
  :inputs (a)
  :c-return :void)

(defmfun max-index ((a vector))
  ("gsl_" :category :type "_max_index")
  (((mpointer a) :pointer))
  :definition :generic
312
  :element-types :no-complex
313
  :inputs (a)
314
  :c-return sizet
315
316
317
318
319
320
  :documentation			; FDL
  "The index of the maximum value in a.  When there are several
  equal maximum elements, then the lowest index is returned.")

(defmfun max-index ((a matrix))
  ("gsl_" :category :type "_max_index")
321
  (((mpointer a) :pointer) (imin (:pointer sizet)) (jmin (:pointer sizet)))
322
  :definition :methods
323
  :element-types :no-complex
324
325
326
327
328
  :inputs (a)
  :c-return :void)

(defmfun minmax-index ((a vector))
  ("gsl_" :category :type "_minmax_index")
329
  (((mpointer a) :pointer) (imin (:pointer sizet)) (jmin (:pointer sizet)))
330
  :definition :generic
331
  :element-types :no-complex
332
333
334
335
336
337
338
339
340
341
  :inputs (a)
  :c-return :void
  :documentation			; FDL
  "The indices of the minimum and maximum values in a.
  When there are several equal minimum elements then the lowest index is
  returned.  Returned indices are minimum, maximum; for matrices
  imin, jmin, imax, jmax.")

(defmfun minmax-index ((a matrix))
  ("gsl_" :category :type "_minmax_index")
342
343
344
  (((mpointer a) :pointer)
   (imin (:pointer sizet)) (jmin (:pointer sizet))
   (imax (:pointer sizet)) (jmax (:pointer sizet)))
345
  :definition :methods
346
  :element-types :no-complex
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
  :inputs (a)
  :c-return :void)

;;;;****************************************************************************
;;;; Properties
;;;;****************************************************************************

(defmfun mzerop ((a both))
  ("gsl_" :category :type "_isnull")
  (((mpointer a) :pointer))
  :definition :generic
  :inputs (a)
  :c-return :boolean
  :documentation			; FDL
  "All elements of a are zero.")
Liam Healy's avatar
Liam Healy committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391

(defmfun mplusp ((a both))
  ("gsl_" :category :type "_ispos")
  (((mpointer a) :pointer))
  :definition :generic
  :gsl-version (1 9)
  :inputs (a)
  :c-return :boolean
  :documentation			; FDL
  "All elements of a are positive.")

(defmfun mminusp ((a both))
  ("gsl_" :category :type "_isneg")
  (((mpointer a) :pointer))
  :definition :generic
  :gsl-version (1 9)
  :inputs (a)
  :c-return :boolean
  :documentation			; FDL
  "All elements of a are negative.")

(defmfun non-negative-p ((a both))
  ("gsl_" :category :type "_isnonneg")
  (((mpointer a) :pointer))
  :definition :generic
  :gsl-version (1 10)
  :inputs (a)
  :c-return :boolean
  :documentation			; FDL
  "All elements of a are non-negative.")