testsupport unit tests work and verify numerical equality approach.
[CommonLispStat.git] / linalg.lsp
blobf86ca5f2e52b267de9f9f75561fae0db057da5b3
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.
5 ;;;
6 ;;; what this should do:
7 ;;; #1 - use CFFI (and possibly Verazanno) to import C/C++.
8 ;;; #2 - what to do for Fortran? Possibly: C <-> bridge, or CLapack?
9 ;;; problem: would be better to have access to Fortran. For
10 ;;; example, think of Doug Bates comment on reverse-calls (as
11 ;;; distinct from callbacks). It would be difficult if we don't
12 ;;; -- however, has anyone run Lapack or similar through F2CL?
13 ;;; Answer: yes, Matlisp does this.
14 ;;;
15 ;;; #3 - Use a lisp-based matrix system drop-in? (matlisp, femlisp, clem, ...?)
16 ;;;
19 ;;;; linalg -- Lisp-Stat interface to basic linear algebra routines.
20 ;;;;
21 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
22 ;;;; unrestricted use.
24 ;;;
25 ;;; Package Setup
26 ;;;
28 (in-package :cl-user)
30 (defpackage :lisp-stat-linalg
31 (:use :common-lisp
32 :cffi
33 :lisp-stat-ffi-int
34 :lisp-stat-math
35 :lisp-stat-types
36 :lisp-stat-float
37 :lisp-stat-compound-data
38 :lisp-stat-linalg-data
39 :lisp-stat-matrix)
40 (:shadowing-import-from :lisp-stat-math
41 expt + - * / ** mod rem abs 1+ 1- log exp sqrt sin cos tan
42 asin acos atan sinh cosh tanh asinh acosh atanh float random
43 truncate floor ceiling round minusp zerop plusp evenp oddp
44 < <= = /= >= > complex conjugate realpart imagpart phase
45 min max logand logior logxor lognot ffloor fceiling
46 ftruncate fround signum cis)
47 (:export chol-decomp lu-decomp lu-solve determinant inverse
48 sv-decomp qr-decomp rcondest make-rotation spline
49 kernel-dens kernel-smooth
50 fft make-sweep-matrix sweep-operator ax+y eigen
52 check-real ;; for optimize
54 covariance-matrix matrix print-matrix solve
55 backsolve eigenvalues eigenvectors accumulate cumsum combine
56 lowess))
58 (in-package #:lisp-stat-linalg)
60 ;;; CFFI Support
62 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 ;;;
64 ;;; Lisp Interfaces to Linear Algebra Routines
65 ;;;
66 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
68 ;;;
69 ;;; Cholesky Decomposition
70 ;;;
72 (cffi:defcfun ("ccl_chol_decomp_front" ccl-chol-decomp-front)
73 :int (x :pointer) (y :int) (z :pointer))
74 (defun chol-decomp-front (x y z)
75 (ccl-chol-decomp-front x y z))
77 ;;;;
78 ;;;; LU Decomposition
79 ;;;;
81 (cffi:defcfun ("ccl_lu_decomp_front" ccl-lu-decomp-front)
82 :int (x :pointer) (y :int) (z :pointer) (u :int) (v :pointer))
83 (defun lu-decomp-front (x y z u v)
84 (ccl-lu-decomp-front x y z u v))
86 (cffi:defcfun ("ccl_lu_solve_front" ccl-lu-solve-front)
87 :int (x :pointer) (y :int) (z :pointer) (u :pointer) (v :int))
88 (defun lu-solve-front (x y z u v)
89 (ccl-lu-solve-front x y z u v))
91 (cffi:defcfun ("ccl_lu_inverse_front" ccl-lu-inverse-front)
92 :int (x :pointer) (y :int) (z :pointer) (u :pointer) (v :int) (w :pointer))
93 (defun lu-inverse-front (x y z u v w)
94 (ccl-lu-inverse-front x y z u v w))
96 ;;;;
97 ;;;; SV Decomposition
98 ;;;;
100 (cffi:defcfun ("ccl_sv_decomp_front" ccl-sv-decomp-front)
101 :int (x :pointer) (y :int) (z :int) (u :pointer) (v :pointer))
102 (defun sv-decomp-front (x y z u v)
103 (ccl-sv-decomp-front x y z u v))
105 ;;;;
106 ;;;; QR Decomposition
107 ;;;;
109 (cffi:defcfun ("ccl_qr_decomp_front" ccl-qr-decomp-front)
110 :int (x :pointer) (y :int) (z :int) (u :pointer) (v :pointer) (w :int))
111 (defun qr-decomp-front (x y z u v w)
112 (ccl-qr-decomp-front x y z u v w))
114 ;;;;
115 ;;;; Estimate of Condition Number for Lower Triangular Matrix
116 ;;;;
118 (cffi:defcfun ("ccl_rcondest_front" ccl-rcondest-front)
119 :double (x :pointer) (y :int))
120 (defun rcondest-front (x y)
121 (ccl-rcondest-front x y))
123 ;;;;
124 ;;;; Make Rotation Matrix
125 ;;;;
127 (cffi:defcfun ("ccl_make_rotation_front" ccl-make-rotation-front)
128 :int (x :int) (y :pointer) (z :pointer) (u :pointer) (v :int) (w :double))
129 (defun make-rotation-front (x y z u v w)
130 (ccl-make-rotation-front x y z u v (float w 1d0)))
132 ;;;;
133 ;;;; Eigenvalues and Eigenvectors
134 ;;;;
136 (cffi:defcfun ("ccl_eigen_front" ccl-eigen-front)
137 :int (x :pointer) (y :int) (z :pointer) (u :pointer) (v :pointer))
138 (defun eigen-front (x y z u v)
139 (ccl-eigen-front x y z u v))
141 ;;;;
142 ;;;; Spline Interpolation
143 ;;;;
145 (cffi:defcfun ("ccl_range_to_rseq" ccl-range-to-rseq)
146 :int (x :int) (y :pointer) (z :int) (u :pointer))
147 (defun la-range-to-rseq (x y z u)
148 (ccl-range-to-rseq x y z u))
150 (cffi:defcfun ("ccl_spline_front" ccl-spline-front)
151 :int (x :int) (y :pointer) (z :pointer) (u :int) (v :pointer) (w :pointer) (a :pointer))
152 (defun spline-front (x y z u v w a)
153 (ccl-spline-front x y z u v w a))
155 ;;;;
156 ;;;; Kernel Density Estimators and Smoothers
157 ;;;;
159 (cffi:defcfun ("ccl_kernel_dens_front" ccl-kernel-dens-front)
160 :int (x :pointer) (y :int) (z :double) (u :pointer) (v :pointer) (w :int) (a :int))
161 (defun kernel-dens-front (x y z u v w a)
162 (ccl-kernel-dens-front x y (float z 1d0) u v w a))
164 (cffi:defcfun ("ccl_kernel_smooth_front" ccl-kernel-smooth-front)
165 :int (x :pointer) (y :pointer) (z :int) (u :double) (v :pointer) (w :pointer) (a :int) (b :int))
166 (defun kernel-smooth-front (x y z u v w a b)
167 (ccl-kernel-smooth-front x y z (float u 1d0) v w a b))
169 ;;;;
170 ;;;; Lowess Smoother Interface
171 ;;;;
173 (cffi:defcfun ("ccl_base_lowess_front" ccl-base-lowess-front)
174 :int (x :pointer) (y :pointer) (z :int) (u :double) (v :int) (w :double) (a :pointer) (b :pointer) (c :pointer))
175 (defun base-lowess-front (x y z u v w a b c)
176 (ccl-base-lowess-front x y z (float u 1d0) v (float w 1d0) a b c))
178 ;;;;
179 ;;;; FFT
180 ;;;;
182 (cffi:defcfun ("ccl_fft_front" ccl-fft-front)
183 :int (x :int) (y :pointer) (z :pointer) (u :int))
184 (defun fft-front (x y z u)
185 (ccl-fft-front x y z u))
190 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
191 ;;;;
192 ;;;; Lisp to C number conversion and checking
193 ;;;;
194 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
196 ;;;;
197 ;;;; Lisp to/from C sequence and matrix conversion and checking
198 ;;;;
200 (defun is-cons (a)
201 "FIXME:AJR this is not used anywhere?"
202 (if (consp a) 1 0))
204 (defun check-fixnum (a)
205 (if (/= 0 (la-data-mode a)) (error "not an integer sequence - ~s" a)))
207 (defun check-real (data)
208 (let ((data (compound-data-seq data)))
209 (cond
210 ((vectorp data)
211 (let ((n (length data)))
212 (declare (fixnum n))
213 (dotimes (i n)
214 (declare (fixnum i))
215 (check-one-real (aref data i)))))
216 ((consp data) (dolist (x data) (check-one-real x)))
217 (t (error "bad sequence - ~s" data)))))
219 (defun vec-assign (a i x) (setf (aref a i) x))
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
223 ;;;;
224 ;;;; Lisp Interfaces to Linear Algebra Routines
225 ;;;;
226 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
227 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
229 ;;; FIXME: use dpbt[f2|rf], dpbstf, dpot[f2|rf]; dpptrf, zpbstf, zpbt[f2|rf]
230 ;;; remember: factorization = decomposition, depending on training.
232 (defun chol-decomp (a &optional (maxoffl 0.0))
233 "Args: (a)
234 Modified Cholesky decomposition. A should be a square, symmetric matrix.
235 Computes lower triangular matrix L such that L L^T = A + D where D is a
236 diagonal matrix. If A is strictly positive definite D will be zero.
237 Otherwise, D is as small as possible to make A + D numerically strictly
238 positive definite. Returns a list (L (max D))."
239 (check-square-matrix a)
240 (check-real a)
241 (let* ((n (array-dimension a 0))
242 (result (make-array (list n n)))
243 (dpars (list maxoffl 0.0)))
244 (check-real dpars)
245 (let ((mat (la-data-to-matrix a +mode-re+))
246 (dp (la-data-to-vector dpars +mode-re+)))
247 (unwind-protect
248 (progn
249 (chol-decomp-front mat n dp)
250 (la-matrix-to-data mat n n +mode-re+ result)
251 (la-vector-to-data dp 2 +mode-re+ dpars))
252 (la-free-matrix mat n)
253 (la-free-vector dp)))
254 (list result (second dpars))))
257 ;;; REPLACE with
258 ;;; (matlisp:lu M)
259 ;;; i.e. result use by:
260 ;;; (setf (values (lu-out1 lu-out2 lu-out3)) (matlisp:lu my-matrix))
261 ;;; for solution, ...
262 ;;; for lu-solve:
263 ;;; (matlisp:gesv a b &opt ipivot)
265 (defun lu-decomp (a)
266 "Args: (a)
267 A is a square matrix of numbers (real or complex). Computes the LU
268 decomposition of A and returns a list of the form (LU IV D FLAG), where
269 LU is a matrix with the L part in the lower triangle, the U part in the
270 upper triangle (the diagonal entries of L are taken to be 1), IV is a vector
271 describing the row permutation used, D is 1 if the number of permutations
272 is odd, -1 if even, and FLAG is T if A is numerically singular, NIL otherwise.
273 Used bu LU-SOLVE."
274 (check-square-matrix a)
275 (let* ((n (array-dimension a 0))
276 (mode (max +mode-re+ (la-data-mode a)))
277 (result (list (make-array (list n n)) (make-array n) nil nil)))
278 (let ((mat (la-data-to-matrix a mode))
279 (iv (la-vector n +mode-in+))
280 (d (la-vector 1 +mode-re+))
281 (singular 0))
282 (unwind-protect
283 (progn
284 (setf singular (lu-decomp-front mat n iv mode d))
285 (la-matrix-to-data mat n n mode (first result))
286 (la-vector-to-data iv n +mode-in+ (second result))
287 (setf (third result) (la-get-double d 0))
288 (setf (fourth result) (if (= singular 0.0) nil t)))
289 (la-free-matrix mat n)
290 (la-free-vector iv)
291 (la-free-vector d)))
292 result))
294 (defun lu-solve (lu lb)
295 "Args: (lu b)
296 LU is the result of (LU-DECOMP A) for a square matrix A, B is a sequence.
297 Returns the solution to the equation Ax = B. Signals an error if A is
298 singular."
299 (let ((la (first lu))
300 (lidx (second lu)))
301 (check-square-matrix la)
302 (check-sequence lidx)
303 (check-sequence lb)
304 (check-fixnum lidx)
305 (let* ((n (num-rows la))
306 (result (make-sequence (if (consp lb) 'list 'vector) n))
307 (a-mode (la-data-mode la))
308 (b-mode (la-data-mode lb)))
309 (if (/= n (length lidx)) (error "index sequence is wrong length"))
310 (if (/= n (length lb)) (error "right hand side is wrong length"))
311 (let* ((mode (max +mode-re+ a-mode b-mode))
312 (a (la-data-to-matrix la mode))
313 (indx (la-data-to-vector lidx +mode-in+))
314 (b (la-data-to-vector lb mode))
315 (singular 0))
316 (unwind-protect
317 (progn
318 (setf singular (lu-solve-front a n indx b mode))
319 (la-vector-to-data b n mode result))
320 (la-free-matrix a n)
321 (la-free-vector indx)
322 (la-free-vector b))
323 (if (/= 0.0 singular) (error "matrix is (numerically) singular"))
324 result))))
326 (defun determinant (a)
327 "Args: (m)
328 Returns the determinant of the square matrix M."
329 (let* ((lu (lu-decomp a))
330 (la (first lu))
331 (n (num-rows a))
332 (d1 (third lu))
333 (d2 0.d0))
334 (declare (fixnum n))
335 (flet ((fabs (x) (float (abs x) 0.d0)))
336 (dotimes (i n (* d1 (exp d2)))
337 (declare (fixnum i))
338 (let* ((x (aref la i i))
339 (magn (fabs x)))
340 (if (= 0.0 magn) (return 0.d0))
341 (setf d1 (* d1 (/ x magn)))
342 (setf d2 (+ d2 (log magn))))))))
344 (defun inverse (a)
345 "Args: (m)
346 Returns the inverse of the the square matrix M; signals an error if M is ill
347 conditioned or singular"
348 (check-square-matrix a)
349 (let ((n (num-rows a))
350 (mode (max +mode-re+ (la-data-mode a))))
351 (declare (fixnum n))
352 (let ((result (make-array (list n n) :initial-element 0)))
353 (dotimes (i n)
354 (declare (fixnum i))
355 (setf (aref result i i) 1))
356 (let ((mat (la-data-to-matrix a mode))
357 (inv (la-data-to-matrix result mode))
358 (iv (la-vector n +mode-in+))
359 (v (la-vector n mode))
360 (singular 0))
361 (unwind-protect
362 (progn
363 (setf singular (lu-inverse-front mat n iv v mode inv))
364 (la-matrix-to-data inv n n mode result))
365 (la-free-matrix mat n)
366 (la-free-matrix inv n)
367 (la-free-vector iv)
368 (la-free-vector v))
369 (if (/= singular 0) (error "matrix is (numerically) singular"))
370 result))))
372 ;;;;
373 ;;;; SV Decomposition
374 ;;;;
376 (defun sv-decomp (a)
377 "Args: (a)
378 A is a matrix of real numbers with at least as many rows as columns.
379 Computes the singular value decomposition of A and returns a list of the form
380 (U W V FLAG) where U and V are matrices whose columns are the left and right
381 singular vectors of A and W is the sequence of singular values of A. FLAG is T
382 if the algorithm converged, NIL otherwise."
383 (check-matrix a)
384 (let* ((m (num-rows a))
385 (n (num-cols a))
386 (mode (max +mode-re+ (la-data-mode a)))
387 (result (list (make-array (list m n))
388 (make-array n)
389 (make-array (list n n))
390 nil)))
391 (if (< m n) (error "number of rows less than number of columns"))
392 (if (= mode +mode-cx+) (error "complex SVD not available yet"))
393 (let ((mat (la-data-to-matrix a mode))
394 (w (la-vector n +mode-re+))
395 (v (la-matrix n n +mode-re+))
396 (converged 0))
397 (unwind-protect
398 (progn
399 (setf converged (sv-decomp-front mat m n w v))
400 (la-matrix-to-data mat m n mode (first result))
401 (la-vector-to-data w n mode (second result))
402 (la-matrix-to-data v n n mode (third result))
403 (setf (fourth result) (if (/= 0.0 converged) t nil)))
404 (la-free-matrix mat m)
405 (la-free-vector w)
406 (la-free-matrix v n))
407 result)))
410 ;;;;
411 ;;;; QR Decomposition
412 ;;;;
414 (defun qr-decomp (a &optional pivot)
415 "Args: (a &optional pivot)
416 A is a matrix of real numbers with at least as many rows as columns. Computes
417 the QR factorization of A and returns the result in a list of the form (Q R).
418 If PIVOT is true the columns of X are first permuted to place insure the
419 absolute values of the diagonal elements of R are nonincreasing. In this case
420 the result includes a third element, a list of the indices of the columns in
421 the order in which they were used."
422 (check-matrix a)
423 (let* ((m (num-rows a))
424 (n (num-cols a))
425 (mode (max +mode-re+ (la-data-mode a)))
426 (p (if pivot 1 0))
427 (result (if pivot
428 (list (make-array (list m n))
429 (make-array (list n n))
430 (make-array n))
431 (list (make-array (list m n)) (make-array (list n n))))))
432 (if (< m n) (error "number of rows less than number of columns"))
433 (if (= mode +mode-cx+) (error "complex QR decomposition not available yet"))
434 (let ((mat (la-data-to-matrix a mode))
435 (v (la-matrix n n +mode-re+))
436 (jpvt (la-vector n +mode-in+)))
437 (unwind-protect
438 (progn
439 (qr-decomp-front mat m n v jpvt p)
440 (la-matrix-to-data mat m n mode (first result))
441 (la-matrix-to-data v n n mode (second result))
442 (if pivot (la-vector-to-data jpvt n +mode-in+ (third result))))
443 (la-free-matrix mat m)
444 (la-free-matrix v n)
445 (la-free-vector jpvt))
446 result)))
448 ;;;;
449 ;;;; Estimate of Condition Number for Lower Triangular Matrix
450 ;;;;
452 (defun rcondest (a)
453 "Args: (a)
454 Returns an estimate of the reciprocal of the L1 condition number of an upper
455 triangular matrix a."
456 (check-square-matrix a)
457 (let ((mode (max +mode-re+ (la-data-mode a)))
458 (n (num-rows a)))
459 (if (= mode +mode-cx+)
460 (error "complex condition estimate not available yet"))
461 (let ((mat (la-data-to-matrix a mode))
462 (est 0.0))
463 (unwind-protect
464 (setf est (rcondest-front mat n))
465 (la-free-matrix mat n))
466 est)))
468 ;;;;
469 ;;;; Make Rotation Matrix
470 ;;;;
472 (defun make-rotation (x y &optional alpha)
473 "Args: (x y &optional alpha)
474 Returns a rotation matrix for rotating from X to Y, or from X toward Y
475 by angle ALPHA, in radians. X and Y are sequences of the same length."
476 (check-sequence x)
477 (check-sequence y)
478 (if alpha (check-one-real alpha))
479 (let* ((n (length x))
480 (mode (max +mode-re+ (la-data-mode x) (la-data-mode y)))
481 (use-angle (if alpha 1 0))
482 (angle (if alpha (float alpha 0.0) 0.0))
483 (result (make-array (list n n))))
484 (if (/= n (length y)) (error "sequences not the same length"))
485 (if (= mode +mode-cx+) (error "complex data not supported yet"))
486 (let ((px (la-data-to-vector x +mode-re+))
487 (py (la-data-to-vector y +mode-re+))
488 (rot (la-matrix n n +mode-re+)))
489 (unwind-protect
490 (progn
491 (make-rotation-front n rot px py use-angle angle)
492 (la-matrix-to-data rot n n +mode-re+ result))
493 (la-free-vector px)
494 (la-free-vector py)
495 (la-free-matrix rot n))
496 result)))
498 ;;;;
499 ;;;; Eigenvalues and Vectors
500 ;;;;
502 (defun eigen (a)
503 "Args: (a)
504 Returns list of list of eigenvalues and list of eigenvectors of square,
505 symmetric matrix A. Third element of result is NIL if algorithm converges.
506 If the algorithm does not converge, the third element is an integer I.
507 In this case the eigenvalues 0, ..., I are not reliable."
508 (check-square-matrix a)
509 (let ((mode (max +mode-re+ (la-data-mode a)))
510 (n (num-rows a)))
511 (if (= mode +mode-cx+) (error "matrix must be real and symmetric"))
512 (let ((evals (make-array n))
513 (evecs (make-list (* n n)))
514 (pa (la-data-to-vector (compound-data-seq a) +mode-re+))
515 (w (la-vector n +mode-re+))
516 (z (la-vector (* n n) +mode-re+))
517 (fv1 (la-vector n +mode-re+))
518 (ierr 0))
519 (unwind-protect
520 (progn
521 (setf ierr (eigen-front pa n w z fv1))
522 (la-vector-to-data w n +mode-re+ evals)
523 (la-vector-to-data z (* n n) +mode-re+ evecs))
524 (la-free-vector pa)
525 (la-free-vector z)
526 (la-free-vector w)
527 (la-free-vector fv1))
528 (list (nreverse evals)
529 (nreverse (mapcar #'(lambda (x) (coerce x 'vector))
530 (split-list evecs n)))
531 (if (/= 0 ierr) (- n ierr))))))
533 ;;;;
534 ;;;; Spline Interpolation
535 ;;;;
537 (defun make-smoother-args (x y xvals)
538 (check-sequence x)
539 (check-real x)
540 (when y
541 (check-sequence y)
542 (check-real y))
543 (unless (integerp xvals)
544 (check-sequence xvals)
545 (check-real xvals))
546 (let* ((n (length x))
547 (ns (if (integerp xvals) xvals (length xvals)))
548 (result (list (make-list ns) (make-list ns))))
549 (if (and y (/= n (length y))) (error "sequences not the same length"))
550 (list x y n (if (integerp xvals) 0 1) ns xvals result)))
552 (defun get-smoother-result (args) (seventh args))
554 (defmacro with-smoother-data ((x y xvals is-reg) &rest body)
555 `(progn
556 (check-sequence ,x)
557 (check-real ,x)
558 (when ,is-reg
559 (check-sequence ,y)
560 (check-real ,y))
561 (unless (integerp ,xvals)
562 (check-sequence ,xvals)
563 (check-real ,xvals))
564 (let* ((supplied (not (integerp ,xvals)))
565 (n (length ,x))
566 (ns (if supplied (length ,xvals) ,xvals))
567 (result (list (make-list ns) (make-list ns))))
568 (if (and ,is-reg (/= n (length ,y)))
569 (error "sequences not the same length"))
570 (if (and (not supplied) (< ns 2))
571 (error "too few points for interpolation"))
572 (let* ((px (la-data-to-vector ,x +mode-re+))
573 (py (if ,is-reg (la-data-to-vector ,y +mode-re+)))
574 (pxs (if supplied
575 (la-data-to-vector ,xvals +mode-re+)
576 (la-vector ns +mode-re+)))
577 (pys (la-vector ns +mode-re+)))
578 (unless supplied (la-range-to-rseq n px ns pxs))
579 (unwind-protect
580 (progn ,@body
581 (la-vector-to-data pxs ns +mode-re+ (first result))
582 (la-vector-to-data pys ns +mode-re+ (second result)))
583 (la-free-vector px)
584 (if ,is-reg (la-free-vector py))
585 (la-free-vector pxs)
586 (la-free-vector pys))
587 result))))
589 (defun spline (x y &key (xvals 30))
590 "Args: (x y &key xvals)
591 Returns list of x and y values of natural cubic spline interpolation of (X,Y).
592 X must be strictly increasing. XVALS can be an integer, the number of equally
593 spaced points to use in the range of X, or it can be a sequence of points at
594 which to interpolate."
595 (with-smoother-data (x y xvals t)
596 (let ((work (la-vector (* 2 n) +mode-re+))
597 (error 0))
598 (unwind-protect
599 (setf error (spline-front n px py ns pxs pys work))
600 (la-free-vector work))
601 (if (/= error 0) (error "bad data for splines")))))
603 ;;;;
604 ;;;; Kernel Density Estimators and Smoothers
605 ;;;;
607 (defun kernel-type-code (type)
608 (cond ((eq type 'u) 0)
609 ((eq type 't) 1)
610 ((eq type 'g) 2)
611 (t 3)))
613 (defun kernel-dens (x &key (type 'b) (width -1.0) (xvals 30))
614 "Args: (x &key xvals width type)
615 Returns list of x and y values of kernel density estimate of X. XVALS can be an
616 integer, the number of equally spaced points to use in the range of X, or it
617 can be a sequence of points at which to interpolate. WIDTH specifies the
618 window width. TYPE specifies the lernel and should be one of the symbols G, T,
619 U or B for gaussian, triangular, uniform or bisquare. The default is B."
620 (check-one-real width)
621 (with-smoother-data (x nil xvals nil) ;; warning about deleting unreachable code is TRUE -- 2nd arg=nil!
622 (let ((code (kernel-type-code type))
623 (error 0))
624 (setf error (kernel-dens-front px n width pxs pys ns code))
625 (if (/= 0 error) (error "bad kernel density data")))))
627 (defun kernel-smooth (x y &key (type 'b) (width -1.0) (xvals 30))
628 "Args: (x y &key xvals width type)
629 Returns list of x and y values of kernel smooth of (X,Y). XVALS can be an
630 integer, the number of equally spaced points to use in the range of X, or it
631 can be a sequence of points at which to interpolate. WIDTH specifies the
632 window width. TYPE specifies the lernel and should be one of the symbols G, T,
633 U or B for Gaussian, triangular, uniform or bisquare. The default is B."
634 (check-one-real width)
635 (with-smoother-data (x y xvals t)
636 (let ((code (kernel-type-code type))
637 (error 0))
638 (kernel-smooth-front px py n width pxs pys ns code)
639 ;; if we get the Lisp version ported from C, uncomment below and
640 ;; comment above. (thanks to Carlos Ungil for the initial CFFI
641 ;; work).
642 ;;(kernel-smooth-Cport px py n width pxs pys ns code)
643 (if (/= 0 error) (error "bad kernel density data")))))
647 (defun kernel-smooth-Cport (px py n width ;;wts wds ;; see above for mismatch?
648 xs ys ns ktype)
649 "Port of kernel_smooth (Lib/kernel.c) to Lisp.
650 FIXME:kernel-smooth-Cport : This is broken.
651 Until this is fixed, we are using Luke's C code and CFFI as glue."
652 (cond ((< n 1) 1.0)
653 ((and (< n 2) (<= width 0)) 1.0)
654 (t (let* ((xmin (min px))
655 (xmax (max px))
656 (width (/ (- xmax xmin) (+ 1.0 (log n)))))
657 (dotimes (i (- ns 1))
658 (setf (aref ys i)
659 (let ((wsum 0.0)
660 (ysum 0.0))
661 (dotimes (j (- n 1)) )
662 ;;;possible nasty errors...
664 (let*
665 ((lwidth (if wds (* width (aref wds j)) width))
666 (lwt (* (kernel-Cport (aref xs i) (aref px j) lwidth ktype) ;; px?
667 (if wts (aref wts j) 1.0))))
668 (setf wsum (+ wsum lwt))
669 (setf ysum (if py (+ ysum (* lwt (aref py j)))))) ;; py? y?
671 ;;; end of errors
672 (if py
673 (if (> wsum 0.0)
674 (/ ysum wsum)
675 0.0)
676 (/ wsum n)))))
677 (values ys)))))
679 (defun kernel-Cport (x y w ktype)
680 "Port of kernel() (Lib/kernel.c) to Lisp.
681 x,y,w are doubles, type is an integer"
682 (if (<= w 0.0)
684 (let ((z (- x y)))
685 (cond ((eq ktype "B")
686 (let* ((w (* w 2.0))
687 (z (* z 0.5)))
688 (if (and (> z -0.5)
689 (< z 0.5))
690 (/ (/ (* 15.0 (* (- 1.0 (* 4 z z)) ;; k/w
691 (- 1.0 (* 4 z z)))) ;; k/w
692 8.0)
694 0)))
695 ((eq ktype "G")
696 (let* ((w (* w 0.25))
697 (z (* z 4.0))
698 (k (/ (exp (* -0.5 z z))
699 (sqrt (* 2 PI)))))
700 (/ k w)))
701 ((eq ktype "U")
702 (let* ((w (* 1.5 w))
703 (z (* z 0.75))
704 (k (if (< (abs z) 0.5)
706 0.0)))
707 (/ k w)))
708 ((eq ktype "T")
709 (cond ((and (> z -1.0)
710 (< z 0.0))
711 (+ 1.0 z)) ;; k
712 ((and (> z 0.0)
713 (< z 1.0))
714 (- 1.0 z)) ;; k
715 (t 0.0)))
716 (t (values 0.0))))))
719 ;;;;
720 ;;;; Lowess Smoother Interface
721 ;;;;
723 (defun |base-lowess| (s1 s2 f nsteps delta)
724 (check-sequence s1)
725 (check-sequence s2)
726 (check-real s1)
727 (check-real s2)
728 (check-one-real f)
729 (check-one-fixnum nsteps)
730 (check-one-real delta)
731 (let* ((n (length s1))
732 (result (make-list n)))
733 (if (/= n (length s2)) (error "sequences not the same length"))
734 (let ((x (la-data-to-vector s1 +mode-re+))
735 (y (la-data-to-vector s2 +mode-re+))
736 (ys (la-vector n +mode-re+))
737 (rw (la-vector n +mode-re+))
738 (res (la-vector n +mode-re+))
739 (error 0))
740 (unwind-protect
741 (progn
742 (setf error (base-lowess-front x y n f nsteps delta ys rw res))
743 (la-vector-to-data ys n +mode-re+ result))
744 (la-free-vector x)
745 (la-free-vector y)
746 (la-free-vector ys)
747 (la-free-vector rw)
748 (la-free-vector res))
749 (if (/= error 0) (error "bad data for lowess"))
750 result)))
753 static LVAL add_contour_point(i, j, k, l, x, y, z, v, result)
754 int i, j, k, l;
755 RVector x, y;
756 RMatrix z;
757 double v;
758 LVAL result;
760 LVAL pt;
761 double p, q;
763 if ((z[i][j] <= v && v < z[k][l]) || (z[k][l] <= v && v < z[i][j])) {
764 xlsave(pt);
765 pt = mklist(2, NIL);
766 p = (v - z[i][j]) / (z[k][l] - z[i][j]);
767 q = 1.0 - p;
768 rplaca(pt, cvflonum((FLOTYPE) (q * x[i] + p * x[k])));
769 rplaca(cdr(pt), cvflonum((FLOTYPE) (q * y[j] + p * y[l])));
770 result = cons(pt, result);
771 xlpop();
773 return(result);
776 LVAL xssurface_contour()
778 LVAL s1, s2, mat, result;
779 RVector x, y;
780 RMatrix z;
781 double v;
782 int i, j, n, m;
784 s1 = xsgetsequence();
785 s2 = xsgetsequence();
786 mat = xsgetmatrix();
787 v = makedouble(xlgetarg());
788 xllastarg();
790 n = seqlen(s1); m = seqlen(s2);
791 if (n != numrows(mat) || m != numcols(mat)) xlfail("dimensions do not match");
792 if (data_mode(s1) == CX || data_mode(s2) == CX || data_mode(mat) == CX)
793 xlfail("data must be real");
795 x = (RVector) data_to_vector(s1, RE);
796 y = (RVector) data_to_vector(s2, RE);
797 z = (RMatrix) data_to_matrix(mat, RE);
799 xlsave1(result);
800 result = NIL;
801 for (i = 0; i < n - 1; i++) {
802 for (j = 0; j < m - 1; j++) {
803 result = add_contour_point(i, j, i, j+1, x, y, z, v, result);
804 result = add_contour_point(i, j+1, i+1, j+1, x, y, z, v, result);
805 result = add_contour_point(i+1, j+1, i+1, j, x, y, z, v, result);
806 result = add_contour_point(i+1, j, i, j, x, y, z, v, result);
809 xlpop();
811 free_vector(x);
812 free_vector(y);
813 free_matrix(z, n);
815 return(result);
820 ;;; FFT
822 ;;; FIXME:ajr
823 ;;; ??replace with matlisp:fft and matlisp:ifft (the latter for inverse mapping)
825 (defun fft (x &optional inverse)
826 "Args: (x &optional inverse)
827 Returns unnormalized Fourier transform of X, or inverse transform if INVERSE
828 is true."
829 (check-sequence x)
830 (let* ((n (length x))
831 (mode (la-data-mode x))
832 (isign (if inverse -1 1))
833 (result (if (consp x) (make-list n) (make-array n))))
834 (let ((px (la-data-to-vector x +mode-cx+))
835 (work (la-vector (+ (* 4 n) 15) +mode-re+)))
836 (unwind-protect
837 (progn
838 (fft-front n px work isign)
839 (la-vector-to-data px n +mode-cx+ result))
840 (la-free-vector px)
841 (la-free-vector work))
842 result)))
845 ;;; SWEEP Operator: FIXME: use matlisp
848 (defun make-sweep-front (x y w n p mode has_w x_mean result)
849 (declare (fixnum n p mode has_w))
850 (let ((x_data nil)
851 (result_data nil)
852 (val 0.0)
853 (dxi 0.0)
854 (dyi 0.0)
855 (dv 0.0)
856 (dw 0.0)
857 (sum_w 0.0)
858 (dxik 0.0)
859 (dxjk 0.0)
860 (dyj 0.0)
861 (dx_meani 0.0)
862 (dx_meanj 0.0)
863 (dy_mean 0.0)
864 (has-w (if (/= 0 has_w) t nil))
865 (RE 1))
866 (declare (long-float val dxi dyi dv dw sum_w dxik dxjk dyj
867 dx_meani dx_meanj dy_mean))
868 ;; (declare-double val dxi dyi dv dw sum_w dxik dxjk dyj
869 ;; dx_meani dx_meanj dy_mean)
871 (if (> mode RE) (error "not supported for complex data yet"))
873 (setf x_data (compound-data-seq x))
874 (setf result_data (compound-data-seq result))
876 ;; find the mean of y
877 (setf val 0.0)
878 (setf sum_w 0.0)
879 (dotimes (i n)
880 (declare (fixnum i))
881 (setf dyi (makedouble (aref y i)))
882 (when has-w
883 (setf dw (makedouble (aref w i)))
884 (incf sum_w dw)
885 (setf dyi (* dyi dw)))
886 (incf val dyi))
887 (if (not has-w) (setf sum_w (float n 0.0)))
888 (if (<= sum_w 0.0) (error "non positive sum of weights"))
889 (setf dy_mean (/ val sum_w))
891 ;; find the column means
892 (dotimes (j p)
893 (declare (fixnum j))
894 (setf val 0.0)
895 (dotimes (i n)
896 (declare (fixnum i))
897 (setf dxi (makedouble (aref x_data (+ (* p i) j))))
898 (when has-w
899 (setf dw (makedouble (aref w i)))
900 (setf dxi (* dxi dw)))
901 (incf val dxi))
902 (setf (aref x_mean j) (/ val sum_w)))
904 ;; put 1/sum_w in topleft, means on left, minus means on top
905 (setf (aref result_data 0) (/ 1.0 sum_w))
906 (dotimes (i p)
907 (declare (fixnum i))
908 (setf dxi (makedouble (aref x_mean i)))
909 (setf (aref result_data (+ i 1)) (- dxi))
910 (setf (aref result_data (* (+ i 1) (+ p 2))) dxi))
911 (setf (aref result_data (+ p 1)) (- dy_mean))
912 (setf (aref result_data (* (+ p 1) (+ p 2))) dy_mean)
914 ;; put sums of adjusted cross products in body
915 (dotimes (i p)
916 (declare (fixnum i))
917 (dotimes (j p)
918 (declare (fixnum j))
919 (setf val 0.0)
920 (dotimes (k n)
921 (declare (fixnum k))
922 (setf dxik (makedouble (aref x_data (+ (* p k) i))))
923 (setf dxjk (makedouble (aref x_data (+ (* p k) j))))
924 (setf dx_meani (makedouble (aref x_mean i)))
925 (setf dx_meanj (makedouble (aref x_mean j)))
926 (setf dv (* (- dxik dx_meani) (- dxjk dx_meanj)))
927 (when has-w
928 (setf dw (makedouble (aref w k)))
929 (setf dv (* dv dw)))
930 (incf val dv))
931 (setf (aref result_data (+ (* (+ i 1) (+ p 2)) (+ j 1))) val)
932 (setf (aref result_data (+ (* (+ j 1) (+ p 2)) (+ i 1))) val))
933 (setf val 0.0)
934 (dotimes (j n)
935 (declare (fixnum j))
936 (setf dxik (makedouble (aref x_data (+ (* p j) i))))
937 (setf dyj (makedouble (aref y j)))
938 (setf dx_meani (makedouble (aref x_mean i)))
939 (setf dv (* (- dxik dx_meani) (- dyj dy_mean)))
940 (when has-w
941 (setf dw (makedouble (aref w j)))
942 (setf dv (* dv dw)))
943 (incf val dv))
944 (setf (aref result_data (+ (* (+ i 1) (+ p 2)) (+ p 1))) val)
945 (setf (aref result_data (+ (* (+ p 1) (+ p 2)) (+ i 1))) val))
946 (setf val 0.0)
947 (dotimes (j n)
948 (declare (fixnum j))
949 (setf dyj (makedouble (aref y j)))
950 (setf dv (* (- dyj dy_mean) (- dyj dy_mean)))
951 (when has-w
952 (setf dw (makedouble (aref w j)))
953 (setf dv (* dv dw)))
954 (incf val dv))
955 (setf (aref result_data (+ (* (+ p 1) (+ p 2)) (+ p 1))) val)))
957 ;;; FIXME: use matlisp
958 (defun sweep-in-place-front (a rows cols mode k tol)
959 "Sweep algorithm for linear regression."
960 (declare (long-float tol))
961 (declare (fixnum rows cols mode k))
962 (let ((data nil)
963 (pivot 0.0)
964 (aij 0.0)
965 (aik 0.0)
966 (akj 0.0)
967 (akk 0.0)
968 (RE 1))
969 (declare (long-float pivot aij aik akj akk))
971 (if (> mode RE) (error "not supported for complex data yet"))
972 (if (or (< k 0) (>= k rows) (>= k cols)) (error "index out of range"))
974 (setf tol (max tol machine-epsilon))
975 (setf data (compound-data-seq a))
977 (setf pivot (makedouble (aref data (+ (* cols k) k))))
979 (cond
980 ((or (> pivot tol) (< pivot (- tol)))
981 (dotimes (i rows)
982 (declare (fixnum i))
983 (dotimes (j cols)
984 (declare (fixnum j))
985 (when (and (/= i k) (/= j k))
986 (setf aij (makedouble (aref data (+ (* cols i) j))))
987 (setf aik (makedouble (aref data (+ (* cols i) k))))
988 (setf akj (makedouble (aref data (+ (* cols k) j))))
989 (setf aij (- aij (/ (* aik akj) pivot)))
990 (setf (aref data (+ (* cols i) j)) aij))))
992 (dotimes (i rows)
993 (declare (fixnum i))
994 (setf aik (makedouble (aref data (+ (* cols i) k))))
995 (when (/= i k)
996 (setf aik (/ aik pivot))
997 (setf (aref data (+ (* cols i) k)) aik)))
999 (dotimes (j cols)
1000 (declare (fixnum j))
1001 (setf akj (makedouble (aref data (+ (* cols k) j))))
1002 (when (/= j k)
1003 (setf akj (- (/ akj pivot)))
1004 (setf (aref data (+ (* cols k) j)) akj)))
1006 (setf akk (/ 1.0 pivot))
1007 (setf (aref data (+ (* cols k) k)) akk)
1009 (t 0))))
1011 ;; FIXME: use matlisp
1012 (defun make-sweep-matrix (x y &optional w)
1013 "Args: (x y &optional weights)
1014 X is matrix, Y and WEIGHTS are sequences. Returns the sweep matrix of the
1015 (weighted) regression of Y on X"
1016 (check-matrix x)
1017 (check-sequence y)
1018 (if w (check-sequence w))
1019 (let ((n (num-rows x))
1020 (p (num-cols x)))
1021 (if (/= n (length y)) (error "dimensions do not match"))
1022 (if (and w (/= n (length w))) (error "dimensions do not match"))
1023 (let ((mode (max (la-data-mode x)
1024 (la-data-mode x)
1025 (if w (la-data-mode w) 0)))
1026 (result (make-array (list (+ p 2) (+ p 2))))
1027 (x-mean (make-array p))
1028 (y (coerce y 'vector))
1029 (w (if w (coerce w 'vector)))
1030 (has-w (if w 1 0)))
1031 (make-sweep-front x y w n p mode has-w x-mean result)
1032 result)))
1034 (defun sweep-in-place (a k tol)
1035 (check-matrix a)
1036 (check-one-fixnum k)
1037 (check-one-real tol)
1038 (let ((rows (num-rows a))
1039 (cols (num-cols a))
1040 (mode (la-data-mode a)))
1041 (let ((swept (sweep-in-place-front a rows cols mode k tol)))
1042 (if (/= 0 swept) t nil))))
1044 (defun sweep-operator (a columns &optional tolerances)
1045 "Args: (a indices &optional tolerances)
1046 A is a matrix, INDICES a sequence of the column indices to be swept. Returns
1047 a list of the swept result and the list of the columns actually swept. (See
1048 MULTREG documentation.) If supplied, TOLERANCES should be a list of real
1049 numbers the same length as INDICES. An index will only be swept if its pivot
1050 element is larger than the corresponding element of TOLERANCES."
1051 (check-matrix a)
1052 (check-sequence columns)
1053 (if tolerances (check-sequence tolerances))
1054 (check-real a)
1055 (check-fixnum columns)
1056 (if tolerances (check-real tolerances))
1057 (do ((tol .0000001)
1058 (result (copy-array a))
1059 (swept-columns nil)
1060 (columns (coerce columns 'list) (cdr columns))
1061 (tolerances (if (consp tolerances) (coerce tolerances 'list))
1062 (if (consp tolerances) (cdr tolerances))))
1063 ((null columns) (list result swept-columns))
1064 (let ((col (first columns))
1065 (tol (if (consp tolerances) (first tolerances) tol)))
1066 (if (sweep-in-place result col tol)
1067 (setf swept-columns (cons col swept-columns))))))
1071 ;;; AX+Y
1074 ;;; matlisp:axpy
1076 (defun ax+y (a x y &optional lower)
1077 "Args (a x y &optional lower)
1078 Returns (+ (matmult A X) Y). If LOWER is not nil, A is taken to be lower
1079 triangular.
1080 This could probably be made more efficient."
1081 (check-square-matrix a)
1082 (check-sequence x)
1083 (check-sequence y)
1084 (check-real a)
1085 (check-real x)
1086 (check-real y)
1087 (let* ((n (num-rows a))
1088 (result (make-list n))
1089 (a (compound-data-seq a)))
1090 (declare (fixnum n))
1091 (if (or (/= n (length x)) (/= n (length y)))
1092 (error "dimensions do not match"))
1093 (do* ((tx (make-next-element x) (make-next-element x))
1094 (ty (make-next-element y))
1095 (tr (make-next-element result))
1096 (i 0 (+ i 1))
1097 (start 0 (+ start n))
1098 (end (if lower (+ i 1) n) (if lower (+ i 1) n)))
1099 ((<= n i) result)
1100 (declare (fixnum i start end))
1101 (let ((val (get-next-element ty i)))
1102 (dotimes (j end)
1103 (declare (fixnum j))
1104 (setf val (+ val (* (get-next-element tx j)
1105 (aref a (+ start j))))))
1106 (set-next-element tr i val)))))
1112 ;;;;
1113 ;;;; Linear Algebra Functions
1114 ;;;;
1116 (defun matrix (dim data)
1117 "Args: (dim data)
1118 returns a matrix of dimensions DIM initialized using sequence DATA
1119 in row major order."
1120 (let ((dim (coerce dim 'list))
1121 (data (coerce data 'list)))
1122 (make-array dim :initial-contents (split-list data (nth 1 dim)))))
1124 (defun flatsize (x)
1125 (length x)) ;; FIXME: defined badly!!
1127 (defun print-matrix (a &optional (stream *standard-output*))
1128 "Args: (matrix &optional stream)
1129 Prints MATRIX to STREAM in a nice form that is still machine readable"
1130 (unless (matrixp a) (error "not a matrix - ~a" a))
1131 (let ((size (min 15 (max (map-elements #'flatsize a)))))
1132 (format stream "#2a(~%")
1133 (dolist (x (row-list a))
1134 (format stream " (")
1135 (let ((n (length x)))
1136 (dotimes (i n)
1137 (let ((y (aref x i)))
1138 (cond
1139 ((integerp y) (format stream "~vd" size y))
1140 ((floatp y) (format stream "~vg" size y))
1141 (t (format stream "~va" size y))))
1142 (if (< i (- n 1)) (format stream " "))))
1143 (format stream ")~%"))
1144 (format stream " )~%")
1145 nil))
1147 (defun solve (a b)
1148 "Args: (a b)
1149 Solves A x = B using LU decomposition and backsolving. B can be a sequence
1150 or a matrix."
1151 (let ((lu (lu-decomp a)))
1152 (if (matrixp b)
1153 (apply #'bind-columns
1154 (mapcar #'(lambda (x) (lu-solve lu x)) (column-list b)))
1155 (lu-solve lu b))))
1157 (defun backsolve (a b)
1158 "Args: (a b)
1159 Solves A x = B by backsolving, assuming A is upper triangular. B must be a
1160 sequence. For use with qr-decomp."
1161 (let* ((n (length b))
1162 (sol (make-array n)))
1163 (dotimes (i n)
1164 (let* ((k (- n i 1))
1165 (val (elt b k)))
1166 (dotimes (j i)
1167 (let ((l (- n j 1)))
1168 (setq val (- val (* (aref sol l) (aref a k l))))))
1169 (setf (aref sol k) (/ val (aref a k k)))))
1170 (if (listp b) (coerce sol 'list) sol)))
1172 (defun eigenvalues (a)
1173 "Args: (a)
1174 Returns list of eigenvalues of square, symmetric matrix A"
1175 (first (eigen a)))
1177 (defun eigenvectors (a)
1178 "Args: (a)
1179 Returns list of eigenvectors of square, symmetric matrix A"
1180 (second (eigen a)))
1182 (defun accumulate (f s)
1183 "Args: (f s)
1184 Accumulates elements of sequence S using binary function F.
1185 (accumulate #'+ x) returns the cumulative sum of x."
1186 (let* ((result (list (elt s 0)))
1187 (tail result))
1188 (flet ((acc (dummy x)
1189 (rplacd tail (list (funcall f (first tail) x)))
1190 (setf tail (cdr tail))))
1191 (reduce #'acc s))
1192 (if (vectorp s) (coerce result 'vector) result)))
1194 (defun cumsum (x)
1195 "Args: (x)
1196 Returns the cumulative sum of X."
1197 (accumulate #'+ x))
1199 (defun combine (&rest args)
1200 "Args (&rest args)
1201 Returns sequence of elements of all arguments."
1202 (copy-seq (element-seq args)))
1204 (defun lowess (x y &key (f .25) (steps 2) (delta -1) sorted)
1205 "Args: (x y &key (f .25) (steps 2) delta sorted)
1206 Returns (list X YS) with YS the LOWESS fit. F is the fraction of data used for
1207 each point, STEPS is the number of robust iterations. Fits for points within
1208 DELTA of each other are interpolated linearly. If the X values setting SORTED
1209 to T speeds up the computation."
1210 (let ((x (if sorted x (sort-data x)))
1211 (y (if sorted y (select y (order x))))
1212 (delta (if (> delta 0.0) delta (/ (- (max x) (min x)) 50))))
1213 (list x)));; (|base-lowess| x y f steps delta))))