generic.lisp 13.2 KB
Newer Older
Liam M. Healy's avatar
Liam M. Healy committed
1
2
;; Generic functions for generalized numbers
;; Liam Healy Tue Feb  9 1999 - 21:38
3
;; Time-stamp: <2011-09-09 22:58:09EDT generic.lisp>
Liam M. Healy's avatar
Liam M. Healy committed
4
5
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
39
40
41
42
43
44

;; Copyright 2011 Liam M. Healy
;; 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)

;;; Generic functions and methods for arithmetic methods on 
;;; generalized numbers.
;;; This needs some work -
;;; * handle both sequences and scalars without commenting out stuff?
;;;    (see /i; check type then call-next-method?)

;;;;****************************************************************************
;;;; 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)
45
		 (cl:coerce number like)
Liam M. Healy's avatar
Liam M. Healy committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
	       (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.")
  (:method ((a number) (b number))
76
    (cl:+ a b))
Liam M. Healy's avatar
Liam M. Healy committed
77
78
  (:method (a b) (funcall-dyadic '+i a b 'zerop))
  (:method ((a sequence) (b sequence))
79
    (map (type-of a) #'+i a b)))
Liam M. Healy's avatar
Liam M. Healy committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93

(defun + (&rest args)
  "Addition of generalized or regular numbers with an
   arbitrary number of arguments."
  (reduce #'+i args))

(defgeneric -i (a b)
  (:documentation
   "Subtraction/negation of generalized or regular numbers, internal.
    Users call -.")
  (:method ((a number) (b number))
    (cl:- a b))
  (:method (a b)
    (funcall-dyadic
94
     '-i a b (lambda (x) (or (null x) (zerop x))) (lambda (x) (* -1 x))))
Liam M. Healy's avatar
Liam M. Healy committed
95
96
97
  (:method ((a sequence) (b sequence))
    (if (null b)
	(*i -1 a)
98
	(map (type-of a) #'-i a b))))
Liam M. Healy's avatar
Liam M. Healy committed
99
100
101

(defun - (&rest args)
  "Subtraction of generalized or regular numbers."
102
  (if (single args) (-i (first args) nil) (reduce #'-i args)))
Liam M. Healy's avatar
Liam M. Healy committed
103
104
105
106
107

(defgeneric *i (a b)
  (:documentation "Multiplication of generalized or regular numbers.")
  ;; Ordinary number multiplication
  (:method ((a number) (b number))
108
    (cl:* a b))
109
  (:method (a b) (funcall-dyadic '*i a b))
Liam M. Healy's avatar
Liam M. Healy committed
110
111
  ;; Two sequences
  (:method ((a sequence) (b sequence))
112
113
114
115
116
    (if (null b)
	a			      ; null second arg - return first
	(progn
	  (assert (eq (length a) (length b)) (a b)
		  "Sequence arguments are not of the same length.")
117
	  (map (type-of a) #'*i a b)))))
Liam M. Healy's avatar
Liam M. Healy committed
118
119
120
121

(defun * (&rest args)
  "Multiplication of generalized or regular numbers with an
   arbitrary number of arguments."
122
  (if (single args) (first args) (reduce #'*i args)))
Liam M. Healy's avatar
Liam M. Healy committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

(defgeneric /i (a b)
  (:documentation "Division of generalized or regular numbers, internal.
    Users call /.")
  (:method ((a number) (b number))
    (cl:/ a b))
  (:method (a b) (funcall-dyadic '/i a b 'null (lambda (x) (/ 1 x))))
  ;; Division can be done as reciprocal and multiplication, or vice versa
  ;; For each generalized number, one of these must be specified
  ;; as a specialized method.
  ;; see below for reciprocal (:method (a (b null)) (/i 1 a)) 
  (:method (a b) (*i a (/ b)))
  ;; Two sequences
  (:method ((a sequence) (b sequence))
    (progn
      (assert (eq (length a) (length b)) (a b)
	      "Sequence arguments are not of the same length.")
      (map (type-of a) #'/i a b))))

(defun / (&rest args)
  "Division of generalized or regular numbers for an
   arbitrary number of arguments."
145
  (if (single args) (/i 1 (first args)) (reduce #'/i args)))
Liam M. Healy's avatar
Liam M. Healy committed
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

;;; 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
226
227
228
229
230
(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
231
    (make-radian
Liam M. Healy's avatar
Liam M. Healy committed
232
233
234
235
236
237
238
239
     (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

(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."
	  (setq num (abs num)))))
    (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)))

266
(defgeneric log (num &optional base)
Liam M. Healy's avatar
Liam M. Healy committed
267
  (:documentation "The natural logarithm of the generalized or regular number.")
268
269
270
271
272
273
274
275
  (: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
276
277
278
279
280
281
282
283

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

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
;;;;****************************************************************************
;;;; 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
   comparison functions such as g<, g>, etc. are defined,
   then all comparison functions will automatically be defined
   for that object.")
  (:method ((x number)) x))

(defgeneric >= (a b)
  (:documentation "Greater than or equal.")
  (:method ((a number) (b number))
	   (cl:>= a b))
  (:method (a b) (cl:>= (numbcomp a) (numbcomp b))))

(defgeneric > (a b)
  (:documentation "Greater than")
  (:method ((a number) (b number)) (cl:> a b))
  (:method (a b) (cl:> (numbcomp a) (numbcomp b))))

(defgeneric <= (a b)
  (:documentation "Less than or equal.")
  (:method ((a number) (b number))
	   (cl:<= a b))
  (:method (a b) (cl:<= (numbcomp a) (numbcomp b))))

(defgeneric < (a b)
  (:documentation "Less than.")
  (:method ((a number) (b number)) (cl:< a b))
  (:method (a b) (cl:< (numbcomp a) (numbcomp b))))

(defgeneric = (a b)
  (:documentation "Numeric equal")
  (:method ((a number) (b number))
	   (if (cl:= a b) a))
  (: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))
368
	   (and x (every #'zerop x)))
Liam M. Healy's avatar
Liam M. Healy committed
369
370
  (:method (a) (cl:zerop (numbcomp a))))

Liam M. Healy's avatar
Liam M. Healy committed
371
372
373
374
(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
375

Liam M. Healy's avatar
Liam M. Healy committed
376
377
378
379
(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
380
381
382
383
384
385
386
387
388
389
390

(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))