ignore fontlock droppings.
[CommonLispStat.git] / lsmath.lsp
blob30d8fa6b4cc6a81902bf718c670c0cebfc179ba1
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 ;;; Import some symbols
41 #+(and kcl fast-c-code internal-c-math)
42 (progn
43 ;; (import 'ls-basics::install-rv-function)
44 (import '(ls-basics::rv-expt ls-basics::rv-+ ls-basics::rv--
45 ls-basics::rv-* ls-basics::rv-/ ls-basics::rv-mod
46 ls-basics::rv-rem ls-basics::rv-pmin ls-basics::rv-pmax
47 ls-basics::rv-1+ ls-basics::rv-1- ls-basics::rv-exp
48 ls-basics::rv-log ls-basics::rv-sqrt ls-basics::rv-sin
49 ls-basics::rv-cos ls-basics::rv-tan ls-basics::rv-atan
50 ls-basics::rv-float ls-basics::rv-random ls-basics::rv-floor
51 ls-basics::rv-ceiling ls-basics::rv-truncate ls-basics::rv-round
52 ls-basics::rv-zerop ls-basics::rv-plusp ls-basics::rv-minusp
53 ls-basics::rv-oddp ls-basics::rv-evenp ls-basics::rv-<
54 ls-basics::rv-<= ls-basics::rv-= ls-basics::rv-/=
55 ls-basics::rv->= ls-basics::rv-> ls-basics::rv-complex
56 ls-basics::rv-realpart ls-basics::rv-imagpart
57 ls-basics::rv-conjugate)))
59 ;; found in lisp-stat-float
60 ;; (import '(ls-basics::base-expt ls-basics::base-log ls-basics::base-exp
61 ;; ls-basics::base-sqrt ls-basics::base-sin ls-basics::base-cos
62 ;; ls-basics::base-tan ls-basics::base-asin ls-basics::base-acos
63 ;; ls-basics::base-atan ls-basics::base-sinh ls-basics::base-cosh
64 ;; ls-basics::base-tanh ls-basics::base-asinh ls-basics::base-acosh
65 ;; ls-basics::base-atanh ls-basics::base-float ls-basics::base-abs
66 ;; ls-basics::base-phase ls-basics::base-ffloor
67 ;; ls-basics::base-fceiling ls-basics::base-ftruncate
68 ;; ls-basics::base-fround ls-basics::base-signum
69 ;; ls-basics::base-cis))
72 ;;; Patch up some type definitions
74 (deftype float () 'common-lisp:float)
75 (deftype complex () 'common-lisp:complex)
77 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 ;;;
79 ;;; Install the vectorized math functions
80 ;;;
81 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
83 (make-rv-function ^ base-expt x y)
84 (make-rv-function ** base-expt x y)
85 (make-rv-function expt base-expt x y)
87 (make-rv-function + common-lisp:+)
88 (make-rv-function-1 - common-lisp:-)
89 (make-rv-function * common-lisp:*)
90 (make-rv-function-1 / common-lisp:/)
91 (make-rv-function mod common-lisp:mod x y)
92 (make-rv-function rem common-lisp:rem x y)
93 (make-rv-function-1 pmin common-lisp:min)
94 (make-rv-function-1 pmax common-lisp:max)
95 (make-rv-function abs base-abs x)
96 (make-rv-function 1+ common-lisp:1+ x)
97 (make-rv-function 1- common-lisp:1- x)
99 (make-rv-function-1 log base-log)
100 (make-rv-function exp base-exp x)
101 (make-rv-function sqrt base-sqrt x)
103 (make-rv-function sin base-sin x)
104 (make-rv-function cos base-cos x)
105 (make-rv-function tan base-tan x)
106 (make-rv-function asin base-asin x)
107 (make-rv-function acos base-acos x)
108 (make-rv-function-1 atan base-atan)
109 (make-rv-function sinh base-sinh x)
110 (make-rv-function cosh base-cosh x)
111 (make-rv-function tanh base-tanh x)
112 (make-rv-function asinh base-asinh x)
113 (make-rv-function acosh base-acosh x)
114 (make-rv-function atanh base-atanh x)
116 (make-rv-function-1 float base-float)
117 (make-rv-function-1 random common-lisp:random)
119 (make-rv-function-1 floor common-lisp:floor)
120 (make-rv-function-1 ceiling common-lisp:ceiling)
121 (make-rv-function-1 truncate common-lisp:truncate)
122 (make-rv-function-1 round common-lisp:round)
124 (make-rv-function zerop common-lisp:zerop x)
125 (make-rv-function plusp common-lisp:plusp x)
126 (make-rv-function minusp common-lisp:minusp x)
127 (make-rv-function oddp common-lisp:oddp x)
128 (make-rv-function evenp common-lisp:evenp x)
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:>=)
135 (make-rv-function-1 > common-lisp:>)
137 (make-rv-function-1 complex common-lisp:complex)
138 (make-rv-function realpart common-lisp:realpart x)
139 (make-rv-function imagpart common-lisp:imagpart x)
140 (make-rv-function conjugate common-lisp:conjugate x)
141 (make-rv-function phase base-phase x)
143 (defun min-1 (x)
144 (if (numberp x)
146 (let* ((seq (compound-data-seq x))
147 (first (elt seq 0))
148 (result (if (numberp first) first (min-1 first))))
149 (if (consp seq)
150 (dolist (x (rest seq) result)
151 (let ((r (if (numberp x) x (min-1 x))))
152 (if (common-lisp:< r result) (setf result r))))
153 (let ((n (length seq)))
154 (declare (fixnum n))
155 (dotimes (i n result)
156 (declare (fixnum i))
157 (let* ((x (aref seq i))
158 (r (if (numberp x) x (min-1 x))))
159 (if (common-lisp:< r result) (setf result r)))))))))
161 (defun min (x &optional (y nil has-y) &rest args)
162 (if (and (null args) (numberp x) (numberp y))
163 (common-lisp:min x y)
164 (if has-y (min-1 (cons x (cons y args))) (min-1 x))))
166 (defun max-1 (x)
167 (if (numberp x)
169 (let* ((seq (compound-data-seq x))
170 (first (elt seq 0))
171 (result (if (numberp first) first (max-1 first))))
172 (if (consp seq)
173 (dolist (x (rest seq) result)
174 (let ((r (if (numberp x) x (max-1 x))))
175 (if (common-lisp:> r result) (setf result r))))
176 (let ((n (length seq)))
177 (declare (fixnum n))
178 (dotimes (i n result)
179 (declare (fixnum i))
180 (let* ((x (aref seq i))
181 (r (if (numberp x) x (max-1 x))))
182 (if (common-lisp:> r result) (setf result r)))))))))
184 (defun max (x &optional (y nil has-y) &rest args)
185 (if (and (null args) (numberp x) (numberp y))
186 (common-lisp:max x y)
187 (if has-y (max-1 (cons x (cons y args))) (max-1 x))))
189 (make-rv-function logand common-lisp:logand)
190 (make-rv-function logior common-lisp:logior)
191 (make-rv-function logxor common-lisp:logxor)
192 (make-rv-function lognot common-lisp:lognot x)
194 (make-rv-function-1 ffloor base-ffloor)
195 (make-rv-function-1 fceiling base-fceiling)
196 (make-rv-function-1 ftruncate base-ftruncate)
197 (make-rv-function-1 fround base-fround)
198 (make-rv-function signum base-signum x)
199 (make-rv-function cis base-cis x)