reduce/apply/values corrections
[CommonLispStat.git] / lsmath.lsp
blob75713f646ac2f0d8251039a9f6c641003ec69611
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 ;; Shadow the symbols in the lisp package that will be redefined
20 (:shadowing-import-from :lisp-stat-object-system
21 slot-value call-method call-next-method)
22 (:shadow expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan
23 asin acos atan sinh cosh tanh asinh acosh atanh float random
24 truncate floor ceiling round minusp zerop plusp evenp oddp
25 < <= = /= >= > complex conjugate realpart imagpart phase
26 min max logand logior logxor lognot ffloor fceiling
27 ftruncate fround signum cis)
28 (:export ^ ** expt + - * / mod rem pmin pmax abs 1+ 1- log exp sqrt sin cos
29 tan asin acos atan sinh cosh tanh asinh acosh atanh float random
30 truncate floor ceiling round minusp zerop plusp evenp oddp < <= =
31 /= >= > complex conjugate realpart imagpart phase min max
32 logand logior logxor lognot ffloor fceiling ftruncate fround
33 signum cis)
34 (:documentation "Vectorization of numerical functions"))
36 (in-package :lisp-stat-math)
38 ;;; Import some symbols
40 #+(and kcl fast-c-code internal-c-math)
41 (progn
42 ;; (import 'ls-basics::install-rv-function)
43 (import '(ls-basics::rv-expt ls-basics::rv-+ ls-basics::rv--
44 ls-basics::rv-* ls-basics::rv-/ ls-basics::rv-mod
45 ls-basics::rv-rem ls-basics::rv-pmin ls-basics::rv-pmax
46 ls-basics::rv-1+ ls-basics::rv-1- ls-basics::rv-exp
47 ls-basics::rv-log ls-basics::rv-sqrt ls-basics::rv-sin
48 ls-basics::rv-cos ls-basics::rv-tan ls-basics::rv-atan
49 ls-basics::rv-float ls-basics::rv-random ls-basics::rv-floor
50 ls-basics::rv-ceiling ls-basics::rv-truncate ls-basics::rv-round
51 ls-basics::rv-zerop ls-basics::rv-plusp ls-basics::rv-minusp
52 ls-basics::rv-oddp ls-basics::rv-evenp ls-basics::rv-<
53 ls-basics::rv-<= ls-basics::rv-= ls-basics::rv-/=
54 ls-basics::rv->= ls-basics::rv-> ls-basics::rv-complex
55 ls-basics::rv-realpart ls-basics::rv-imagpart
56 ls-basics::rv-conjugate)))
58 ;; found in lisp-stat-float
59 ;; (import '(ls-basics::base-expt ls-basics::base-log ls-basics::base-exp
60 ;; ls-basics::base-sqrt ls-basics::base-sin ls-basics::base-cos
61 ;; ls-basics::base-tan ls-basics::base-asin ls-basics::base-acos
62 ;; ls-basics::base-atan ls-basics::base-sinh ls-basics::base-cosh
63 ;; ls-basics::base-tanh ls-basics::base-asinh ls-basics::base-acosh
64 ;; ls-basics::base-atanh ls-basics::base-float ls-basics::base-abs
65 ;; ls-basics::base-phase ls-basics::base-ffloor
66 ;; ls-basics::base-fceiling ls-basics::base-ftruncate
67 ;; ls-basics::base-fround ls-basics::base-signum
68 ;; ls-basics::base-cis))
71 ;;; Patch up some type definitions
73 (deftype float () 'common-lisp:float)
74 (deftype complex () 'common-lisp:complex)
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
77 ;;;
78 ;;; Install the vectorized math functions
79 ;;;
80 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82 (make-rv-function ^ base-expt x y)
83 (make-rv-function ** base-expt x y)
84 (make-rv-function expt base-expt x y)
86 (make-rv-function + common-lisp:+)
87 (make-rv-function-1 - common-lisp:-)
88 (make-rv-function * common-lisp:*)
89 (make-rv-function-1 / common-lisp:/)
90 (make-rv-function mod common-lisp:mod x y)
91 (make-rv-function rem common-lisp:rem x y)
92 (make-rv-function-1 pmin common-lisp:min)
93 (make-rv-function-1 pmax common-lisp:max)
94 (make-rv-function abs base-abs x)
95 (make-rv-function 1+ common-lisp:1+ x)
96 (make-rv-function 1- common-lisp:1- x)
98 (make-rv-function-1 log base-log)
99 (make-rv-function exp base-exp x)
100 (make-rv-function sqrt base-sqrt x)
102 (make-rv-function sin base-sin x)
103 (make-rv-function cos base-cos x)
104 (make-rv-function tan base-tan x)
105 (make-rv-function asin base-asin x)
106 (make-rv-function acos base-acos x)
107 (make-rv-function-1 atan base-atan)
108 (make-rv-function sinh base-sinh x)
109 (make-rv-function cosh base-cosh x)
110 (make-rv-function tanh base-tanh x)
111 (make-rv-function asinh base-asinh x)
112 (make-rv-function acosh base-acosh x)
113 (make-rv-function atanh base-atanh x)
115 (make-rv-function-1 float base-float)
116 (make-rv-function-1 random common-lisp:random)
118 (make-rv-function-1 floor common-lisp:floor)
119 (make-rv-function-1 ceiling common-lisp:ceiling)
120 (make-rv-function-1 truncate common-lisp:truncate)
121 (make-rv-function-1 round common-lisp:round)
123 (make-rv-function zerop common-lisp:zerop x)
124 (make-rv-function plusp common-lisp:plusp x)
125 (make-rv-function minusp common-lisp:minusp x)
126 (make-rv-function oddp common-lisp:oddp x)
127 (make-rv-function evenp common-lisp:evenp x)
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:>=)
134 (make-rv-function-1 > common-lisp:>)
136 (make-rv-function-1 complex common-lisp:complex)
137 (make-rv-function realpart common-lisp:realpart x)
138 (make-rv-function imagpart common-lisp:imagpart x)
139 (make-rv-function conjugate common-lisp:conjugate x)
140 (make-rv-function phase base-phase x)
142 (defun min-1 (x)
143 (if (numberp x)
145 (let* ((seq (compound-data-seq x))
146 (first (elt seq 0))
147 (result (if (numberp first) first (min-1 first))))
148 (if (consp seq)
149 (dolist (x (rest seq) result)
150 (let ((r (if (numberp x) x (min-1 x))))
151 (if (common-lisp:< r result) (setf result r))))
152 (let ((n (length seq)))
153 (declare (fixnum n))
154 (dotimes (i n result)
155 (declare (fixnum i))
156 (let* ((x (aref seq i))
157 (r (if (numberp x) x (min-1 x))))
158 (if (common-lisp:< r result) (setf result r)))))))))
160 (defun min (x &optional (y nil has-y) &rest args)
161 (if (and (null args) (numberp x) (numberp y))
162 (common-lisp:min x y)
163 (if has-y (min-1 (cons x (cons y args))) (min-1 x))))
165 (defun max-1 (x)
166 (if (numberp x)
168 (let* ((seq (compound-data-seq x))
169 (first (elt seq 0))
170 (result (if (numberp first) first (max-1 first))))
171 (if (consp seq)
172 (dolist (x (rest seq) result)
173 (let ((r (if (numberp x) x (max-1 x))))
174 (if (common-lisp:> r result) (setf result r))))
175 (let ((n (length seq)))
176 (declare (fixnum n))
177 (dotimes (i n result)
178 (declare (fixnum i))
179 (let* ((x (aref seq i))
180 (r (if (numberp x) x (max-1 x))))
181 (if (common-lisp:> r result) (setf result r)))))))))
183 (defun max (x &optional (y nil has-y) &rest args)
184 (if (and (null args) (numberp x) (numberp y))
185 (common-lisp:max x y)
186 (if has-y (max-1 (cons x (cons y args))) (max-1 x))))
188 (make-rv-function logand common-lisp:logand)
189 (make-rv-function logior common-lisp:logior)
190 (make-rv-function logxor common-lisp:logxor)
191 (make-rv-function lognot common-lisp:lognot x)
193 (make-rv-function-1 ffloor base-ffloor)
194 (make-rv-function-1 fceiling base-fceiling)
195 (make-rv-function-1 ftruncate base-ftruncate)
196 (make-rv-function-1 fround base-fround)
197 (make-rv-function signum base-signum x)
198 (make-rv-function cis base-cis x)