comments on approach for CFFI with existing system.
[CommonLispStat.git] / lsmath.lsp
blob0205216e1a92df5a764a40c81f917387e937aa9e
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 another world...
14 (defpackage :lisp-stat-math
15 (:use :common-lisp
16 :lisp-stat-object-system
17 :lisp-stat-macros
18 :lisp-stat-float)
19 (:shadowing-import-from :lisp-stat-object-system
20 slot-value call-method call-next-method)
21 (:shadow expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan
22 asin acos atan sinh cosh tanh asinh acosh atanh float random
23 truncate floor ceiling round minusp zerop plusp evenp oddp
24 < <= = /= >= > complex conjugate realpart imagpart phase
25 min max logand logior logxor lognot ffloor fceiling
26 ftruncate fround signum cis)
27 (:export ^ ** expt + - * / mod rem pmin pmax abs 1+ 1- log exp sqrt sin cos
28 tan asin acos atan sinh cosh tanh asinh acosh atanh float random
29 truncate floor ceiling round minusp zerop plusp evenp oddp < <= =
30 /= >= > complex conjugate realpart imagpart phase min max
31 logand logior logxor lognot ffloor fceiling ftruncate fround
32 signum cis)
33 (:documentation "Vectorization of numerical functions"))
35 (in-package :lisp-stat-math)
37 ;;; Import some symbols
39 #+(and kcl fast-c-code internal-c-math)
40 (progn
41 ;; (import 'ls-basics::install-rv-function)
42 (import '(ls-basics::rv-expt ls-basics::rv-+ ls-basics::rv--
43 ls-basics::rv-* ls-basics::rv-/ ls-basics::rv-mod
44 ls-basics::rv-rem ls-basics::rv-pmin ls-basics::rv-pmax
45 ls-basics::rv-1+ ls-basics::rv-1- ls-basics::rv-exp
46 ls-basics::rv-log ls-basics::rv-sqrt ls-basics::rv-sin
47 ls-basics::rv-cos ls-basics::rv-tan ls-basics::rv-atan
48 ls-basics::rv-float ls-basics::rv-random ls-basics::rv-floor
49 ls-basics::rv-ceiling ls-basics::rv-truncate ls-basics::rv-round
50 ls-basics::rv-zerop ls-basics::rv-plusp ls-basics::rv-minusp
51 ls-basics::rv-oddp ls-basics::rv-evenp ls-basics::rv-<
52 ls-basics::rv-<= ls-basics::rv-= ls-basics::rv-/=
53 ls-basics::rv->= ls-basics::rv-> ls-basics::rv-complex
54 ls-basics::rv-realpart ls-basics::rv-imagpart
55 ls-basics::rv-conjugate)))
57 ;; found in lisp-stat-float
58 ;; (import '(ls-basics::base-expt ls-basics::base-log ls-basics::base-exp
59 ;; ls-basics::base-sqrt ls-basics::base-sin ls-basics::base-cos
60 ;; ls-basics::base-tan ls-basics::base-asin ls-basics::base-acos
61 ;; ls-basics::base-atan ls-basics::base-sinh ls-basics::base-cosh
62 ;; ls-basics::base-tanh ls-basics::base-asinh ls-basics::base-acosh
63 ;; ls-basics::base-atanh ls-basics::base-float ls-basics::base-abs
64 ;; ls-basics::base-phase ls-basics::base-ffloor
65 ;; ls-basics::base-fceiling ls-basics::base-ftruncate
66 ;; ls-basics::base-fround ls-basics::base-signum
67 ;; ls-basics::base-cis))
70 ;;; Patch up some type definitions
72 (deftype float () 'common-lisp:float)
73 (deftype complex () 'common-lisp:complex)
75 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76 ;;;
77 ;;; Install the vectorized math functions
78 ;;;
79 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81 (make-rv-function ^ base-expt x y)
82 (make-rv-function ** base-expt x y)
83 (make-rv-function expt base-expt x y)
85 (make-rv-function + common-lisp:+)
86 (make-rv-function-1 - common-lisp:-)
87 (make-rv-function * common-lisp:*)
88 (make-rv-function-1 / common-lisp:/)
89 (make-rv-function mod common-lisp:mod x y)
90 (make-rv-function rem common-lisp:rem x y)
91 (make-rv-function-1 pmin common-lisp:min)
92 (make-rv-function-1 pmax common-lisp:max)
93 (make-rv-function abs base-abs x)
94 (make-rv-function 1+ common-lisp:1+ x)
95 (make-rv-function 1- common-lisp:1- x)
97 (make-rv-function-1 log base-log)
98 (make-rv-function exp base-exp x)
99 (make-rv-function sqrt base-sqrt x)
101 (make-rv-function sin base-sin x)
102 (make-rv-function cos base-cos x)
103 (make-rv-function tan base-tan x)
104 (make-rv-function asin base-asin x)
105 (make-rv-function acos base-acos x)
106 (make-rv-function-1 atan base-atan)
107 (make-rv-function sinh base-sinh x)
108 (make-rv-function cosh base-cosh x)
109 (make-rv-function tanh base-tanh x)
110 (make-rv-function asinh base-asinh x)
111 (make-rv-function acosh base-acosh x)
112 (make-rv-function atanh base-atanh x)
114 (make-rv-function-1 float base-float)
115 (make-rv-function-1 random common-lisp:random)
117 (make-rv-function-1 floor common-lisp:floor)
118 (make-rv-function-1 ceiling common-lisp:ceiling)
119 (make-rv-function-1 truncate common-lisp:truncate)
120 (make-rv-function-1 round common-lisp:round)
122 (make-rv-function zerop common-lisp:zerop x)
123 (make-rv-function plusp common-lisp:plusp x)
124 (make-rv-function minusp common-lisp:minusp x)
125 (make-rv-function oddp common-lisp:oddp x)
126 (make-rv-function evenp common-lisp:evenp x)
128 (make-rv-function-1 < common-lisp:<)
129 (make-rv-function-1 <= common-lisp:<=)
130 (make-rv-function-1 = common-lisp:=)
131 (make-rv-function-1 /= common-lisp:/=)
132 (make-rv-function-1 >= common-lisp:>=)
133 (make-rv-function-1 > common-lisp:>)
135 (make-rv-function-1 complex common-lisp:complex)
136 (make-rv-function realpart common-lisp:realpart x)
137 (make-rv-function imagpart common-lisp:imagpart x)
138 (make-rv-function conjugate common-lisp:conjugate x)
139 (make-rv-function phase base-phase x)
141 (defun min-1 (x)
142 (if (numberp x)
144 (let* ((seq (compound-data-seq x))
145 (first (elt seq 0))
146 (result (if (numberp first) first (min-1 first))))
147 (if (consp seq)
148 (dolist (x (rest seq) result)
149 (let ((r (if (numberp x) x (min-1 x))))
150 (if (common-lisp:< r result) (setf result r))))
151 (let ((n (length seq)))
152 (declare (fixnum n))
153 (dotimes (i n result)
154 (declare (fixnum i))
155 (let* ((x (aref seq i))
156 (r (if (numberp x) x (min-1 x))))
157 (if (common-lisp:< r result) (setf result r)))))))))
159 (defun min (x &optional (y nil has-y) &rest args)
160 (if (and (null args) (numberp x) (numberp y))
161 (common-lisp:min x y)
162 (if has-y (min-1 (cons x (cons y args))) (min-1 x))))
164 (defun max-1 (x)
165 (if (numberp x)
167 (let* ((seq (compound-data-seq x))
168 (first (elt seq 0))
169 (result (if (numberp first) first (max-1 first))))
170 (if (consp seq)
171 (dolist (x (rest seq) result)
172 (let ((r (if (numberp x) x (max-1 x))))
173 (if (common-lisp:> r result) (setf result r))))
174 (let ((n (length seq)))
175 (declare (fixnum n))
176 (dotimes (i n result)
177 (declare (fixnum i))
178 (let* ((x (aref seq i))
179 (r (if (numberp x) x (max-1 x))))
180 (if (common-lisp:> r result) (setf result r)))))))))
182 (defun max (x &optional (y nil has-y) &rest args)
183 (if (and (null args) (numberp x) (numberp y))
184 (common-lisp:max x y)
185 (if has-y (max-1 (cons x (cons y args))) (max-1 x))))
187 (make-rv-function logand common-lisp:logand)
188 (make-rv-function logior common-lisp:logior)
189 (make-rv-function logxor common-lisp:logxor)
190 (make-rv-function lognot common-lisp:lognot x)
192 (make-rv-function-1 ffloor base-ffloor)
193 (make-rv-function-1 fceiling base-fceiling)
194 (make-rv-function-1 ftruncate base-ftruncate)
195 (make-rv-function-1 fround base-fround)
196 (make-rv-function signum base-signum x)
197 (make-rv-function cis base-cis x)