Need to continue the lisp-ification of numerics to avoid CFFI when possible.
[CommonLispStat.git] / lsmath.lsp
blobc20e7b3e9893047a98e9502c8d9d09b8779b0d54
1 ;;; -*- mode: lisp -*-
2 ;;; Copyright (c) 2005--2007, by A.J. Rossini <blindglobe@gmail.com>
3 ;;; See COPYRIGHT file for any additional restrictions (BSD license).
4 ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp.
6 ;;;; lsmath -- Install vectorized arithmetic functions
7 ;;;;
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
9 ;;;; unrestricted use.
11 ;;; Package Setup
13 (in-package :cl-user)
15 (defpackage :lisp-stat-math
16 (:use :common-lisp
17 :lisp-stat-object-system
18 :lisp-stat-macros
19 :lisp-stat-compound-data
20 :lisp-stat-float)
21 (:shadowing-import-from :lisp-stat-object-system
22 slot-value call-method call-next-method)
23 (:shadow expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan
24 asin acos atan sinh cosh tanh asinh acosh atanh float random
25 truncate floor ceiling round minusp zerop plusp evenp oddp
26 < <= = /= >= > complex conjugate realpart imagpart phase
27 min max logand logior logxor lognot ffloor fceiling
28 ftruncate fround signum cis)
29 (:export ^ ** expt + - * / mod rem pmin pmax abs 1+ 1- log exp sqrt sin cos
30 tan asin acos atan sinh cosh tanh asinh acosh atanh float random
31 truncate floor ceiling round minusp zerop plusp evenp oddp < <= =
32 /= >= > complex conjugate realpart imagpart phase min max
33 logand logior logxor lognot ffloor fceiling ftruncate fround
34 signum cis)
35 (:documentation "Vectorization of numerical functions"))
37 (in-package :lisp-stat-math)
39 ;;; Patch up some type definitions
41 (deftype float () 'common-lisp:float)
42 (deftype complex () 'common-lisp:complex)
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;;
46 ;;; Install the vectorized math functions
47 ;;;
48 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50 (make-rv-function ^ base-expt x y)
51 (make-rv-function ** base-expt x y)
52 (make-rv-function expt base-expt x y)
54 (make-rv-function + common-lisp:+)
55 (make-rv-function-1 - common-lisp:-)
56 (make-rv-function * common-lisp:*)
57 (make-rv-function-1 / common-lisp:/)
58 (make-rv-function mod common-lisp:mod x y)
59 (make-rv-function rem common-lisp:rem x y)
60 (make-rv-function-1 pmin common-lisp:min)
61 (make-rv-function-1 pmax common-lisp:max)
62 (make-rv-function abs base-abs x)
63 (make-rv-function 1+ common-lisp:1+ x)
64 (make-rv-function 1- common-lisp:1- x)
66 (make-rv-function-1 log base-log)
67 (make-rv-function exp base-exp x)
68 (make-rv-function sqrt base-sqrt x)
70 (make-rv-function sin base-sin x)
71 (make-rv-function cos base-cos x)
72 (make-rv-function tan base-tan x)
73 (make-rv-function asin base-asin x)
74 (make-rv-function acos base-acos x)
75 (make-rv-function-1 atan base-atan)
76 (make-rv-function sinh base-sinh x)
77 (make-rv-function cosh base-cosh x)
78 (make-rv-function tanh base-tanh x)
79 (make-rv-function asinh base-asinh x)
80 (make-rv-function acosh base-acosh x)
81 (make-rv-function atanh base-atanh x)
83 (make-rv-function-1 float base-float)
84 (make-rv-function-1 random common-lisp:random)
86 (make-rv-function-1 floor common-lisp:floor)
87 (make-rv-function-1 ceiling common-lisp:ceiling)
88 (make-rv-function-1 truncate common-lisp:truncate)
89 (make-rv-function-1 round common-lisp:round)
91 (make-rv-function zerop common-lisp:zerop x)
92 (make-rv-function plusp common-lisp:plusp x)
93 (make-rv-function minusp common-lisp:minusp x)
94 (make-rv-function oddp common-lisp:oddp x)
95 (make-rv-function evenp common-lisp:evenp x)
97 (make-rv-function-1 < common-lisp:<)
98 (make-rv-function-1 <= common-lisp:<=)
99 (make-rv-function-1 = common-lisp:=)
100 (make-rv-function-1 /= common-lisp:/=)
101 (make-rv-function-1 >= common-lisp:>=)
102 (make-rv-function-1 > common-lisp:>)
104 (make-rv-function-1 complex common-lisp:complex)
105 (make-rv-function realpart common-lisp:realpart x)
106 (make-rv-function imagpart common-lisp:imagpart x)
107 (make-rv-function conjugate common-lisp:conjugate x)
108 (make-rv-function phase base-phase x)
110 (defun min-1 (x)
111 (if (numberp x)
113 (let* ((seq (compound-data-seq x))
114 (first (elt seq 0))
115 (result (if (numberp first) first (min-1 first))))
116 (if (consp seq)
117 (dolist (x (rest seq) result)
118 (let ((r (if (numberp x) x (min-1 x))))
119 (if (common-lisp:< r result) (setf result r))))
120 (let ((n (length seq)))
121 (declare (fixnum n))
122 (dotimes (i n result)
123 (declare (fixnum i))
124 (let* ((x (aref seq i))
125 (r (if (numberp x) x (min-1 x))))
126 (if (common-lisp:< r result) (setf result r)))))))))
128 (defun min (x &optional (y nil has-y) &rest args)
129 (if (and (null args) (numberp x) (numberp y))
130 (common-lisp:min x y)
131 (if has-y (min-1 (cons x (cons y args))) (min-1 x))))
133 (defun max-1 (x)
134 (if (numberp x)
136 (let* ((seq (compound-data-seq x))
137 (first (elt seq 0))
138 (result (if (numberp first) first (max-1 first))))
139 (if (consp seq)
140 (dolist (x (rest seq) result)
141 (let ((r (if (numberp x) x (max-1 x))))
142 (if (common-lisp:> r result) (setf result r))))
143 (let ((n (length seq)))
144 (declare (fixnum n))
145 (dotimes (i n result)
146 (declare (fixnum i))
147 (let* ((x (aref seq i))
148 (r (if (numberp x) x (max-1 x))))
149 (if (common-lisp:> r result) (setf result r)))))))))
151 (defun max (x &optional (y nil has-y) &rest args)
152 (if (and (null args) (numberp x) (numberp y))
153 (common-lisp:max x y)
154 (if has-y (max-1 (cons x (cons y args))) (max-1 x))))
156 (make-rv-function logand common-lisp:logand)
157 (make-rv-function logior common-lisp:logior)
158 (make-rv-function logxor common-lisp:logxor)
159 (make-rv-function lognot common-lisp:lognot x)
161 (make-rv-function-1 ffloor base-ffloor)
162 (make-rv-function-1 fceiling base-fceiling)
163 (make-rv-function-1 ftruncate base-ftruncate)
164 (make-rv-function-1 fround base-fround)
165 (make-rv-function signum base-signum x)
166 (make-rv-function cis base-cis x)