forked from dbmcclain/vmath
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgesvd.lisp
197 lines (184 loc) · 7.04 KB
/
dgesvd.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(in-package :vm)
(defvar *dgesvd-library*
(translate-logical-pathname
#+:MAC "PROJECTS:DYLIB;libLispNrUtil.dylib"
#+:WIN32 "PROJECTS:DYLIB;Lisp_NrUtil.dll"))
(fli:register-module *dgesvd-library*)
#|
(fli:disconnect-module *dgesvd-library*)
|#
(fli:define-foreign-function (lisp_dgesvd
"lisp_dgesvd" :source)
((a-mat (:pointer :double))
(nrow :int)
(ncol :int)
(u-mat (:pointer :double)) ;; returned values copied in
(w-vec (:pointer :double)) ;; returned values copied in
(vt-mat (:pointer :double))) ;; returned values copied in
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (lisp_dgesvd_bksb
"lisp_dgesvd_bksb" :source)
((u-mat (:pointer :double))
(w-vec (:pointer :double))
(vt-mat (:pointer :double))
(y-vec (:pointer :double))
(x-vec (:pointer :double)) ;; returned values copied in
(nrow :int)
(ncol :int)
(tolerance :double))
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (lisp_dgesvd_solve
"lisp_dgesvd_solve" :source)
((a-mat (:pointer :double))
(y-vec (:pointer :double))
(x-vec (:pointer :double)) ;; returned values copied in
(nrow :int)
(ncol :int)
(tolerance :double))
:result-type :int
:language :ansi-c)
(fli:define-foreign-function (lisp_dgesvd_predict
"lisp_dgesvd_predict" :source)
((v-vec (:pointer :double))
(xv-vec (:pointer :double))
(nel :int)
(ncol :int)
(tol :double)
(v-pred (:pointer :double))
(npred :int)
(ll (:pointer :double))
(hh (:pointer :double))
(sd-pred (:pointer :double)))
:result-type :int
:language :ansi-c)
(defun blit-lisp-to-c (l-vec c-vec nel)
(loop for ix from 0 below nel do
(setf (fli:dereference c-vec :index ix)
(coerce (row-major-aref l-vec ix) 'double-float))))
(defun blit-c-to-lisp (c-vec l-vec nel)
(loop for ix from 0 below nel do
(setf (row-major-aref l-vec ix)
(fli:dereference c-vec :index ix))))
(defun dgesvd (arr)
(destructuring-bind (nrow ncol) (array-dimensions arr)
(fli:with-dynamic-foreign-objects ()
(let ((ca-mat (fli:allocate-dynamic-foreign-object
:type :double
:nelems (* nrow ncol)))
(cu-mat (fli:allocate-dynamic-foreign-object
:type :double
:nelems (* nrow ncol)))
(cw-vec (fli:allocate-dynamic-foreign-object
:type :double
:nelems ncol))
(cv-mat (fli:allocate-dynamic-foreign-object
:type :double
:nelems (* ncol ncol))))
(blit-lisp-to-c arr ca-mat (* nrow ncol))
(let ((err (lisp_dgesvd ca-mat nrow ncol cu-mat cw-vec cv-mat)))
(unless (zerop err)
(error "dgesvd: ~A" err)))
(let ((u-mat (make-array `(,nrow ,ncol)
:element-type 'double-float))
(w-vec (make-array ncol :element-type 'double-float))
(v-mat (make-array `(,ncol ,ncol)
:element-type 'double-float)))
(blit-c-to-lisp ca-mat u-mat (* nrow ncol))
(blit-c-to-lisp cw-vec w-vec ncol)
(blit-c-to-lisp cv-mat v-mat (* ncol ncol))
(list u-mat w-vec v-mat)
)))
))
(defun dgesvd-bksb (u-mat w-vec vt-mat b-vec &key (tolerance 1.0d-8))
(destructuring-bind (nrow ncol) (array-dimensions u-mat)
(fli:with-dynamic-foreign-objects ()
(let ((cu-mat (fli:allocate-dynamic-foreign-object
:type :double
:nelems (* nrow ncol)))
(cw-vec (fli:allocate-dynamic-foreign-object
:type :double
:nelems ncol))
(cvt-mat (fli:allocate-dynamic-foreign-object
:type :double
:nelems (* ncol ncol)))
(cb-vec (fli:allocate-dynamic-foreign-object
:type :double
:nelems nrow))
(cx-vec (fli:allocate-dynamic-foreign-object
:type :double
:nelems ncol)))
(blit-lisp-to-c w-vec cw-vec ncol)
(blit-lisp-to-c b-vec cb-vec nrow)
(blit-lisp-to-c u-mat cu-mat (* nrow ncol))
(blit-lisp-to-c vt-mat cvt-mat (* ncol ncol))
(let ((err (lisp_dgesvd_bksb cu-mat cw-vec cvt-mat
cb-vec cx-vec nrow ncol
(coerce tolerance 'double-float))))
(unless (zerop err)
(error "dgesvd_bksb: ~A" err)))
(let ((x-vec (make-array ncol :element-type 'double-float)))
(blit-c-to-lisp cx-vec x-vec ncol)
x-vec)))
))
(defun dgesvd-solve (arr vec &key (tolerance 1.0d-8))
(destructuring-bind (nrow ncol) (array-dimensions arr)
(fli:with-dynamic-foreign-objects ()
(let ((ca-mat (fli:allocate-dynamic-foreign-object
:type :double
:nelems (* nrow ncol)))
(cv-vec (fli:allocate-dynamic-foreign-object
:type :double
:nelems nrow))
(cx-vec (fli:allocate-dynamic-foreign-object
:type :double
:nelems ncol)))
(blit-lisp-to-c arr ca-mat (* nrow ncol))
(blit-lisp-to-c vec cv-vec nrow)
(let ((err (lisp_dgesvd_solve ca-mat cv-vec cx-vec nrow ncol
(coerce tolerance 'double-float))))
(unless (zerop err)
(error "dgesvd-solve: ~A" err)))
(let ((x-vec (make-array ncol :element-type 'double-float)))
(blit-c-to-lisp cx-vec x-vec ncol)
x-vec
)))
))
(defun dgesvd-predict (vec xvec ndim npred ll hh &key (tolerance 1.0d-8))
(let ((nel (min (length vec) (length xvec))))
(fli:with-dynamic-foreign-objects ()
(let ((cvec (fli:allocate-dynamic-foreign-object
:type :double
:nelems nel))
(cxvec (fli:allocate-dynamic-foreign-object
:type :double
:nelems nel))
(cpred (fli:allocate-dynamic-foreign-object
:type :double
:nelems npred))
(csd (fli:allocate-dynamic-foreign-object
:type :double
:nelems npred))
(chh (fli:allocate-dynamic-foreign-object
:type :double
:nelems nel))
(cll (fli:allocate-dynamic-foreign-object
:type :double
:nelems nel)))
(blit-lisp-to-c vec cvec nel)
(blit-lisp-to-c xvec cxvec nel)
(blit-lisp-to-c hh chh nel)
(blit-lisp-to-c ll cll nel)
(let ((err (lisp_dgesvd_predict cvec cxvec nel ndim
(coerce tolerance 'double-float)
cpred npred cll chh csd)))
(unless (zerop err)
(error "svd-predict: ~A" err)))
(let ((pred (make-array npred :element-type 'double-float))
(sd (make-array npred :element-type 'double-float)))
(blit-c-to-lisp cpred pred npred)
(blit-c-to-lisp csd sd npred)
(list pred sd)
)))
))