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 ;;;; ladata -- Data handling functions for linear algebra interface
8 ;;;; Copyright (c) 1991, by Luke Tierney. Permission is granted for
15 (in-package #:lisp-stat-basics
)
17 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19 ;;;; Data Mode Functions
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;;; These constants need to be redefined if IN, RE or CX in linalg.h
28 ;;; FIXME:AJR: This is how Luke got around having appropriate
29 ;;; approaches for Linear Algebra. We want to cheat and instead use
30 ;;; CLEM or MATLISP as the underlying linear algebra package.
32 (defparameter +mode-in
+ 0)
33 (defparameter +mode-re
+ 1)
34 (defparameter +mode-cx
+ 2)
43 (defun la-data-mode (data)
44 (let ((data (compound-data-seq data
))
48 (let ((n (length data
)))
52 (setf mode
(max mode
(mode-of (aref data i
)))))))
53 ((consp data
) (dolist (x data mode
) (setf mode
(max mode
(mode-of x
)))))
54 (t (error "bad sequence - ~s" data
)))))
57 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 ;;;; Internal Allocation Funcitons
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
63 (defvar *la-allocations
* nil
)
65 (defun la-allocate (n m
)
66 (let ((p (la-base-allocate n m
)))
67 (if (null-ptr-p p
) (error "allocation failed"))
68 (if (member p
*la-allocations
* :test
#'ptr-eq
)
69 (error "pointer is already on the list"))
70 (push p
*la-allocations
*)
74 (when (and (not (null-ptr-p p
)) (member p
*la-allocations
* :test
#'ptr-eq
))
75 (setf *la-allocations
* (delete p
*la-allocations
* :test
#'ptr-eq
))
78 (defun la-cleanup-allocations ()
79 (let ((allocs (copy-list *la-allocations
*)))
80 (dolist (p allocs
) (la-free p
))))
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 ;;;; C Vector and Array Allocation
86 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
88 (defun la-vector(n mode
) (la-allocate n
(la-mode-size mode
)))
89 (defun la-free-vector (v) (la-free v
))
91 (defun la-matrix (n m mode
)
92 (let ((matrix (la-allocate n
(la-mode-size +mode-in
+))))
94 (la-put-pointer matrix i
(la-allocate m
(la-mode-size mode
))))
97 (defun la-free-matrix (matrix n
)
99 (la-free (la-get-pointer matrix i
)))
103 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
105 ;;;; C to/from Lisp Data Conversion
107 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
109 (defun la-data-to-vector (data mode
)
110 (check-sequence data
)
111 (let* ((n (length data
))
112 (vec (la-vector n mode
))
113 (d (make-next-element data
)))
119 (la-put-integer vec i
(get-next-element d i
))))
123 (la-put-double vec i
(get-next-element d i
))))
127 (let ((x (get-next-element d i
)))
128 (la-put-complex vec i
(realpart x
) (imagpart x
))))))
131 (defun la-data-to-matrix (data mode
)
133 (let* ((n (num-rows data
))
135 (mat (la-matrix n m mode
)))
136 (declare (fixnum n m
))
141 (let ((vec (la-get-pointer mat i
)))
143 (la-put-integer vec j
(aref data i j
))))))
147 (let ((vec (la-get-pointer mat i
)))
149 (la-put-double vec j
(aref data i j
))))))
153 (let ((vec (la-get-pointer mat i
)))
155 (let ((x (aref data i j
)))
156 (la-put-complex vec i
(realpart x
) (imagpart x
))))))))
159 (defun la-vector-to-data (vec n mode data
)
161 (check-sequence data
)
162 (let ((d (make-next-element data
))
164 ((= mode
+mode-in
+) #'la-get-integer
)
165 ((= mode
+mode-re
+) #'la-get-double
)
166 ((= mode
+mode-cx
+) #'la-get-complex
))))
169 (set-next-element d i
(funcall gf vec i
))))
172 (defun la-matrix-to-data (mat n m mode result
)
173 (declare (fixnum n m
))
174 (check-matrix result
)
176 ((= mode
+mode-in
+) #'la-get-integer
)
177 ((= mode
+mode-re
+) #'la-get-double
)
178 ((= mode
+mode-cx
+) #'la-get-complex
))))
181 (let ((vec (la-get-pointer mat i
)))
184 (setf (aref result i j
) (funcall gf vec j
))))))