generic.lisp 12.5 KB
Newer Older
1
;; Generic functions arithmetic functions on generalized numbers
Liam M. Healy's avatar
Liam M. Healy committed
2
;; Liam Healy Tue Feb  9 1999 - 21:38
3
;; Time-stamp: <2014-01-05 14:14:06EST generic.lisp>
Liam M. Healy's avatar
Liam M. Healy committed
4

5
;; Copyright 2011, 2013, 2014 Liam M. Healy
Liam M. Healy's avatar
Liam M. Healy committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;; Distributed under the terms of the GNU General Public License
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(in-package :antik)

;;;;****************************************************************************
;;;; Convert numeric types
;;;;****************************************************************************

(defgeneric coerce (number like)
  (:documentation "Make number into an object similar to like, or
                   if like is a number type, into that type.")
  (:method (x like) (error 'coerce-undefined :object x :to like))
  (:method ((number number) (like number))
	   (cl:coerce number (type-of like)))
  (:method ((number symbol) (like number))
	   (error "Can't coerce symbol."))
  (:method (number (like symbol))
	   (if (eq like 't)
	       number
	     (if (subtypep like 'number)
39
		 (cl:coerce number like)
Liam M. Healy's avatar
Liam M. Healy committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	       (error "Don't know how to coerce to ~a" like))))
  (:method ((number sequence) like)
	   (map (type-of number) (lambda (x) (coerce x like)) number))
  (:method (object (like null))
    (error 'coerce-nil :object object)))

(defgeneric gconstant (name like)
  (:method ((name (eql 'pi)) like)
	   (coerce pi like)))

;;; (with-converted-numbers ('double-float x y z) (fuzzle x y z))
(defmacro with-converted-numbers ((like &rest symbols) &body body)
  (let ((lets 
	 (mapcar (lambda (x) (list x `(coerce ,x ,like))) symbols)))
    ;; can't us pi here, error is:
    ;; Error: SYMBOL-MACROLET symbol PI is globally declared SPECIAL
    `(symbol-macrolet ((gpi (gconstant 'pi ,like)))
       (let ,lets
	 ,@body))))

;;;;****************************************************************************
;;;; Dyadic functions
;;;;****************************************************************************

;;; There is a general problem with using coerce in these functions;
;;; if the second argument is T, the first argument will be returned
;;; and the function will recurse infinitely.  
(defgeneric +i (a b)
  (:documentation "Addition of generalized or regular numbers.")
69
  (:method ((a number) (b number)) (cl:+ a b)))
Liam M. Healy's avatar
Liam M. Healy committed
70
71
72
73

(defun + (&rest args)
  "Addition of generalized or regular numbers with an
   arbitrary number of arguments."
74
75
  (when (member nil args) (warn 'null-argument))
  (if (single args) (first args) (reduce #'+i args)))
Liam M. Healy's avatar
Liam M. Healy committed
76
77
78
79
80

(defgeneric -i (a b)
  (:documentation
   "Subtraction/negation of generalized or regular numbers, internal.
    Users call -.")
81
  (:method ((a number) (b number)) (cl:- a b)))
Liam M. Healy's avatar
Liam M. Healy committed
82
83
84

(defun - (&rest args)
  "Subtraction of generalized or regular numbers."
85
86
  (when (member nil args) (warn 'null-argument))
  (if (single args) (-i 0 (first args)) (reduce #'-i args)))
Liam M. Healy's avatar
Liam M. Healy committed
87
88
89

(defgeneric *i (a b)
  (:documentation "Multiplication of generalized or regular numbers.")
90
  (:method ((a number) (b number)) (cl:* a b)))
Liam M. Healy's avatar
Liam M. Healy committed
91
92
93
94

(defun * (&rest args)
  "Multiplication of generalized or regular numbers with an
   arbitrary number of arguments."
95
  (when (member nil args) (warn 'null-argument))
96
  (if (single args) (first args) (reduce #'*i args)))
Liam M. Healy's avatar
Liam M. Healy committed
97
98
99
100

(defgeneric /i (a b)
  (:documentation "Division of generalized or regular numbers, internal.
    Users call /.")
101
  (:method ((a number) (b number)) (cl:/ a b)))
Liam M. Healy's avatar
Liam M. Healy committed
102
103
104
105

(defun / (&rest args)
  "Division of generalized or regular numbers for an
   arbitrary number of arguments."
106
107
108
109
  (when (member nil args) (warn 'null-argument))
  (if (single args)
      (/ 1 (first args))
      (reduce #'/i args)))
Liam M. Healy's avatar
Liam M. Healy committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

;;; Put these into with-generalized numbers?
(defmacro incf (ref &optional (delta 1))
  `(setf ,ref (+i ,ref ,delta)))

(defmacro decf (ref &optional (delta 1))
  `(setf ,ref (- ,ref ,delta)))

;;;;****************************************************************************
;;;; Monadic functions
;;;;****************************************************************************

(defun sqrt (num)
  "The square root of the generalized or regular number."
  (expt num 1/2))

(defgeneric sin (num)
  (:documentation "The sine of the generalized or regular number.")
  (:method ((num rational))
	   (cl:sin (coerce num *read-default-float-format*)))
  (:method ((num number))
	   (cl:sin num))
  (:method ((x sequence))
	   (map (type-of x) #'sin x)))

(defgeneric cos (num)
  (:documentation "The cosine of the generalized or regular number.")
  (:method ((num rational))
	   (cl:cos (coerce num *read-default-float-format*)))
  (:method ((num number))    
	   (cl:cos num))
  (:method ((x sequence))
	   (map (type-of x) #'cos x)))

(defgeneric tan (num)
  (:documentation "The tangent of the generalized or regular number.")
  (:method ((num rational))
	   (cl:tan (coerce num *read-default-float-format*)))
  (:method ((num number))
	   (cl:tan num))
  (:method ((x sequence))
	   (map (type-of x) #'tan x)))

(defparameter *radian* 1)
(defun make-radian (x)
  "From the number x, return the representation of an angle in radians."
  (* *radian* x))

(defgeneric asin (arg)
  (:documentation "The arcsine of the generalized or regular number.")
  (:method ((arg number))
    (when (and (not (complexp arg)) (> (abs arg) 1))
      (restart-case 
	  (error 'making-complex-number :operation 'asin :number arg)
	(accept () :report "Accept complex answer.")
	(truncate ()
	  :report "Truncate argument to ±1."
	  (return-from asin
	    (if (plusp arg) (cl:asin 1.0d0) (cl:asin -1.0d0))))
	(hyperbolic-function ()
	  :report "Use the inverse hyperbolic sine."
	  (return-from asin (cl:asinh arg)))))
    (make-radian (cl:asin arg))))

(defgeneric acos (arg)
  (:documentation "The arccosine of the generalized or regular number.")
  (:method ((arg number))
    (when (and (not (complexp arg)) (> (abs arg) 1))
      (restart-case 
	  (error 'making-complex-number :operation 'acos :number arg)
	(accept () :report "Accept complex answer.")
	(truncate ()
	  :report "Truncate argument to ±1."
	  (return-from acos
	    (if (plusp arg) (cl:acos 1.0d0) (cl:acos -1.0d0))))
	(hyperbolic-function ()
	  :report "Use the inverse hyperbolic cosine."
	  (return-from acos (cl:acosh arg)))))
    (make-radian (cl:acos arg))))

Liam M. Healy's avatar
Liam M. Healy committed
190
191
192
193
194
(defgeneric atan (num &optional den default zero-enough)
  (:documentation "The arctangent of the generalized or regular number.  If  absolute value of the arguments are below zero-enough, default is returned.")
  (:method ((num number)
	    &optional (den 1) (default 0) (zero-enough (* 64 double-float-epsilon)))
	   ;; zero-enough should be keyed to epsilon of num type
Liam M. Healy's avatar
Liam M. Healy committed
195
    (make-radian
Liam M. Healy's avatar
Liam M. Healy committed
196
197
198
199
200
201
202
203
     (if (and (< (cl:abs num) zero-enough) (< (cl:abs den) zero-enough))
	 default
	 (cl:atan (if (typep num 'rational)
		      (coerce num *read-default-float-format*)
		      num)
		  (if (zerop num)
		      (signum den)
		      den))))))
Liam M. Healy's avatar
Liam M. Healy committed
204

205
(export 'return-zero)
Liam M. Healy's avatar
Liam M. Healy committed
206
207
208
209
210
211
212
213
214
215
216
217
218
(defgeneric expt (num exponent)
  (:documentation "Raise the number to the exponent.")
  (:method ((num number) (exponent number))
    ;; Most calculations we do are real,
    ;; so trap the production of complex numbers.
    ;; Presumably if the number is already complex,
    ;; we don't mind complex numbers.
    (when (and (not (complexp num)) (minusp num) (not (integerp exponent)))
      (restart-case 
	  (error 'making-complex-number :operation 'expt :number num)
	(accept () :report "Accept complex answer.")
	(absolute-value ()
	  :report "Take the absolute value of the argument and return a real."
219
220
221
222
	  (setq num (abs num)))
	(return-zero ()
	  :report "Return zero."
	  (return-from expt 0.0))))
Liam M. Healy's avatar
Liam M. Healy committed
223
224
225
226
227
228
229
230
231
232
233
    (cl:expt num exponent)))

(defgeneric exp (num)
  (:documentation "The natural exponent e^num of the generalized or regular number.")
  (:method ((num rational))
	   (cl:exp (coerce num *read-default-float-format*)))
  (:method ((num number))
	   (cl:exp num))
  (:method ((x sequence))
	   (map (type-of x) #'exp x)))

234
(defgeneric log (num &optional base)
Liam M. Healy's avatar
Liam M. Healy committed
235
  (:documentation "The natural logarithm of the generalized or regular number.")
236
237
238
239
240
241
242
243
  (:method ((num number) &optional base)
    (if base
	(cl:log num base)
	(cl:log num)))
  (:method ((x sequence) &optional base)
    (if base
	(map (type-of x) (alexandria:rcurry 'log base) x)
	(map (type-of x) #'log x))))
Liam M. Healy's avatar
Liam M. Healy committed
244
245
246
247
248
249
250
251

(defgeneric abs (num)
  (:documentation "The absolute value.")
  (:method ((num number))
	   (cl:abs num))
  (:method ((seq sequence))
	   (map (type-of seq) #'abs seq)))

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
(defgeneric sinh (num)
  (:documentation "The hyperbolic sine of the generalized or regular number.")
  (:method ((num rational))
	   (cl:sinh (coerce num *read-default-float-format*)))
  (:method ((num number))
	   (cl:sinh num))
  (:method ((x sequence))
	   (map (type-of x) #'sinh x)))

(defgeneric cosh (num)
  (:documentation "The hyperbolic cosine of the generalized or regular number.")
  (:method ((num rational))
	   (cl:cosh (coerce num *read-default-float-format*)))
  (:method ((num number))    
	   (cl:cosh num))
  (:method ((x sequence))
	   (map (type-of x) #'cosh x)))

(defgeneric tanh (num)
  (:documentation "The hyperbolic tangent of the generalized or regular number.")
  (:method ((num rational))
	   (cl:tanh (coerce num *read-default-float-format*)))
  (:method ((num number))
	   (cl:tanh num))
  (:method ((x sequence))
	   (map (type-of x) #'tanh x)))

Liam M. Healy's avatar
Liam M. Healy committed
279
280
281
282
283
284
285
;;;;****************************************************************************
;;;; Comparisons
;;;;****************************************************************************

(defgeneric numbcomp (x)
  (:documentation "Function for number comparison.  If this function is
   defined for a generalized number and returns an object for which all
286
   comparison functions such as <, >, etc. are defined,
Liam M. Healy's avatar
Liam M. Healy committed
287
288
289
290
   then all comparison functions will automatically be defined
   for that object.")
  (:method ((x number)) x))

291
292
293
294
295
296
(defun multiarg-comparison (op args)
  "Extend a comparison operator from two to any number of arguments."
  (every op args (rest args)))

(defun >= (&rest args) (multiarg-comparison '>=i args))
(defgeneric >=i (a b)
Liam M. Healy's avatar
Liam M. Healy committed
297
298
299
300
301
  (:documentation "Greater than or equal.")
  (:method ((a number) (b number))
	   (cl:>= a b))
  (:method (a b) (cl:>= (numbcomp a) (numbcomp b))))

302
303
(defun > (&rest args) (multiarg-comparison '>i args))
(defgeneric >i (a b)
Liam M. Healy's avatar
Liam M. Healy committed
304
305
306
307
  (:documentation "Greater than")
  (:method ((a number) (b number)) (cl:> a b))
  (:method (a b) (cl:> (numbcomp a) (numbcomp b))))

308
309
(defun <= (&rest args) (multiarg-comparison '<=i args))
(defgeneric <=i (a b)
Liam M. Healy's avatar
Liam M. Healy committed
310
311
312
313
314
  (:documentation "Less than or equal.")
  (:method ((a number) (b number))
	   (cl:<= a b))
  (:method (a b) (cl:<= (numbcomp a) (numbcomp b))))

315
316
(defun < (&rest args) (multiarg-comparison '<i args))
(defgeneric <i (a b)
Liam M. Healy's avatar
Liam M. Healy committed
317
318
319
320
  (:documentation "Less than.")
  (:method ((a number) (b number)) (cl:< a b))
  (:method (a b) (cl:< (numbcomp a) (numbcomp b))))

321
322
(defun = (&rest args) (multiarg-comparison '=i args))
(defgeneric =i (a b)
Liam M. Healy's avatar
Liam M. Healy committed
323
324
325
  (:documentation "Numeric equal")
  (:method ((a number) (b number))
	   (if (cl:= a b) a))
326
  #+(or) 
Liam M. Healy's avatar
Liam M. Healy committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
  (:method (a b) (funcall-dyadic '= (numbcomp a) (numbcomp b))))

(defgeneric plusp (a)
  (:documentation "Positive.")
  (:method ((a number))
	   (cl:plusp a))
  (:method (a) (cl:plusp (numbcomp a))))

(defgeneric minusp (a)
  (:documentation "Negative")
  (:method ((a number))
	   (cl:minusp a))
  (:method (a) (cl:minusp (numbcomp a))))

(defgeneric zerop (a)
  (:documentation "Zero.")
  (:method ((a number))
	   (cl:zerop a))
  (:method ((x sequence))
346
	   (and x (every #'zerop x)))
Liam M. Healy's avatar
Liam M. Healy committed
347
348
  (:method (a) (cl:zerop (numbcomp a))))

Liam M. Healy's avatar
Liam M. Healy committed
349
350
351
352
(defgeneric floor (number &optional divisor)
  (:documentation "Greatest multiple of divisor less than number.")
  (:method ((number number) &optional (divisor 1))
	   (cl:floor number divisor)))
Liam M. Healy's avatar
Liam M. Healy committed
353

Liam M. Healy's avatar
Liam M. Healy committed
354
355
356
357
(defgeneric round (number &optional divisor)
  (:documentation "Nearest multiple of divisor to number.")
  (:method ((number number) &optional (divisor 1))
	   (cl:round number divisor)))
Liam M. Healy's avatar
Liam M. Healy committed
358
359
360
361
362
363
364
365
366
367
368

(defgeneric signum (a)
  (:documentation "Signum(a).")
  (:method ((a number)) (cl:signum a))
  (:method (a) (cl:signum (numbcomp a))))

(defun min (&rest args)
  (reduce (lambda (x y) (if (< x y) x y)) args))

(defun max (&rest args)
  (reduce (lambda (x y) (if (> x y) x y)) args))