Mercurial > hg > smallbox
comparison Problems/private/myblas.h @ 51:217a33ac374e
(none)
author | idamnjanovic |
---|---|
date | Mon, 14 Mar 2011 16:52:27 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
50:d5155eaa3f68 | 51:217a33ac374e |
---|---|
1 /************************************************************************** | |
2 * | |
3 * File name: myblas.h | |
4 * | |
5 * Ron Rubinstein | |
6 * Computer Science Department | |
7 * Technion, Haifa 32000 Israel | |
8 * ronrubin@cs | |
9 * | |
10 * Version: 1.1 | |
11 * Last updated: 17.8.2009 | |
12 * | |
13 * A collection of basic linear algebra functions, in the spirit of the | |
14 * BLAS/LAPACK libraries. | |
15 * | |
16 *************************************************************************/ | |
17 | |
18 | |
19 | |
20 #ifndef __MY_BLAS_H__ | |
21 #define __MY_BLAS_H__ | |
22 | |
23 | |
24 #include "mex.h" | |
25 #include <math.h> | |
26 | |
27 | |
28 | |
29 /************************************************************************** | |
30 * Squared value. | |
31 **************************************************************************/ | |
32 #define SQR(X) ((X)*(X)) | |
33 | |
34 | |
35 | |
36 /************************************************************************** | |
37 * Matrix-vector multiplication. | |
38 * | |
39 * Computes an operation of the form: | |
40 * | |
41 * y := alpha*A*x | |
42 * | |
43 * Parameters: | |
44 * A - matrix of size n X m | |
45 * x - vector of length m | |
46 * y - output vector of length n | |
47 * alpha - real constant | |
48 * n, m - dimensions of A | |
49 * | |
50 * Note: This function re-writes the contents of y. | |
51 * | |
52 **************************************************************************/ | |
53 void mat_vec(double alpha, double A[], double x[], double y[], mwSize n, mwSize m); | |
54 | |
55 | |
56 | |
57 /************************************************************************** | |
58 * Matrix-transpose-vector multiplication. | |
59 * | |
60 * Computes an operation of the form: | |
61 * | |
62 * y := alpha*A'*x | |
63 * | |
64 * Parameters: | |
65 * A - matrix of size n X m | |
66 * x - vector of length n | |
67 * y - output vector of length m | |
68 * alpha - real constant | |
69 * n, m - dimensions of A | |
70 * | |
71 * Note: This function re-writes the contents of y. | |
72 * | |
73 **************************************************************************/ | |
74 void matT_vec(double alpha, double A[], double x[], double y[], mwSize n, mwSize m); | |
75 | |
76 | |
77 | |
78 /************************************************************************** | |
79 * Sparse-matrix-vector multiplication. | |
80 * | |
81 * Computes an operation of the form: | |
82 * | |
83 * y := alpha*A*x | |
84 * | |
85 * where A is a sparse matrix. | |
86 * | |
87 * Parameters: | |
88 * pr,ir,jc - sparse representation of the matrix A, of size n x m | |
89 * x - vector of length m | |
90 * y - output vector of length n | |
91 * alpha - real constant | |
92 * n, m - dimensions of A | |
93 * | |
94 * Note: This function re-writes the contents of y. | |
95 * | |
96 **************************************************************************/ | |
97 void mat_sp_vec(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double x[], double y[], mwSize n, mwSize m); | |
98 | |
99 | |
100 | |
101 /************************************************************************** | |
102 * Sparse-matrix-transpose-vector multiplication. | |
103 * | |
104 * Computes an operation of the form: | |
105 * | |
106 * y := alpha*A'*x | |
107 * | |
108 * where A is a sparse matrix. | |
109 * | |
110 * Parameters: | |
111 * pr,ir,jc - sparse representation of the matrix A, of size n x m | |
112 * x - vector of length m | |
113 * y - output vector of length n | |
114 * alpha - real constant | |
115 * n, m - dimensions of A | |
116 * | |
117 * Note: This function re-writes the contents of y. | |
118 * | |
119 **************************************************************************/ | |
120 void matT_sp_vec(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double x[], double y[], mwSize n, mwSize m); | |
121 | |
122 | |
123 | |
124 /************************************************************************** | |
125 * Matrix-sparse-vector multiplication. | |
126 * | |
127 * Computes an operation of the form: | |
128 * | |
129 * y := alpha*A*x | |
130 * | |
131 * where A is a matrix and x is a sparse vector. | |
132 * | |
133 * Parameters: | |
134 * A - matrix of size n X m | |
135 * pr,ir,jc - sparse representation of the vector x, of length m | |
136 * y - output vector of length n | |
137 * alpha - real constant | |
138 * n, m - dimensions of A | |
139 * | |
140 * Note: This function re-writes the contents of y. | |
141 * | |
142 **************************************************************************/ | |
143 void mat_vec_sp(double alpha, double A[], double pr[], mwIndex ir[], mwIndex jc[], double y[], mwSize n, mwSize m); | |
144 | |
145 | |
146 | |
147 /************************************************************************** | |
148 * Matrix-transpose-sparse-vector multiplication. | |
149 * | |
150 * Computes an operation of the form: | |
151 * | |
152 * y := alpha*A'*x | |
153 * | |
154 * where A is a matrix and x is a sparse vector. | |
155 * | |
156 * Parameters: | |
157 * A - matrix of size n X m | |
158 * pr,ir,jc - sparse representation of the vector x, of length n | |
159 * y - output vector of length m | |
160 * alpha - real constant | |
161 * n, m - dimensions of A | |
162 * | |
163 * Note: This function re-writes the contents of y. | |
164 * | |
165 **************************************************************************/ | |
166 void matT_vec_sp(double alpha, double A[], double pr[], mwIndex ir[], mwIndex jc[], double y[], mwSize n, mwSize m); | |
167 | |
168 | |
169 | |
170 /************************************************************************** | |
171 * Sparse-matrix-sparse-vector multiplication. | |
172 * | |
173 * Computes an operation of the form: | |
174 * | |
175 * y := alpha*A*x | |
176 * | |
177 * where A is a sparse matrix and x is a sparse vector. | |
178 * | |
179 * Parameters: | |
180 * pr,ir,jc - sparse representation of the matrix A, of size n x m | |
181 * prx,irx,jcx - sparse representation of the vector x (of length m) | |
182 * y - output vector of length n | |
183 * alpha - real constant | |
184 * n, m - dimensions of A | |
185 * | |
186 * Note: This function re-writes the contents of y. | |
187 * | |
188 **************************************************************************/ | |
189 void mat_sp_vec_sp(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double prx[], mwIndex irx[], mwIndex jcx[], double y[], mwSize n, mwSize m); | |
190 | |
191 | |
192 | |
193 /************************************************************************** | |
194 * Sparse-matrix-transpose-sparse-vector multiplication. | |
195 * | |
196 * Computes an operation of the form: | |
197 * | |
198 * y := alpha*A'*x | |
199 * | |
200 * where A is a sparse matrix and x is a sparse vector. | |
201 * | |
202 * Importnant note: this function is provided for completeness, but is NOT efficient. | |
203 * If possible, convert x to non-sparse representation and use matT_vec_sp instead. | |
204 * | |
205 * Parameters: | |
206 * pr,ir,jc - sparse representation of the matrix A, of size n x m | |
207 * prx,irx,jcx - sparse representation of the vector x (of length n) | |
208 * y - output vector of length n | |
209 * alpha - real constant | |
210 * n, m - dimensions of A | |
211 * | |
212 * Note: This function re-writes the contents of y. | |
213 * | |
214 **************************************************************************/ | |
215 void matT_sp_vec_sp(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double prx[], mwIndex irx[], mwIndex jcx[], double y[], mwSize n, mwSize m); | |
216 | |
217 | |
218 | |
219 /************************************************************************** | |
220 * Matrix-matrix multiplication. | |
221 * | |
222 * Computes an operation of the form: | |
223 * | |
224 * X := alpha*A*B | |
225 * | |
226 * Parameters: | |
227 * A - matrix of size n X m | |
228 * B - matrix of size m X k | |
229 * X - output matrix of size n X k | |
230 * alpha - real constant | |
231 * n, m, k - dimensions of A, B | |
232 * | |
233 * Note: This function re-writes the contents of X. | |
234 * | |
235 **************************************************************************/ | |
236 void mat_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k); | |
237 | |
238 | |
239 | |
240 /************************************************************************** | |
241 * Matrix-transpose-matrix multiplication. | |
242 * | |
243 * Computes an operation of the form: | |
244 * | |
245 * X := alpha*A*B | |
246 * | |
247 * Parameters: | |
248 * A - matrix of size n X m | |
249 * B - matrix of size m X k | |
250 * X - output matrix of size n X k | |
251 * alpha - real constant | |
252 * n, m, k - dimensions of A, B | |
253 * | |
254 * Note: This function re-writes the contents of X. | |
255 * | |
256 **************************************************************************/ | |
257 void matT_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k); | |
258 | |
259 | |
260 | |
261 /************************************************************************** | |
262 * Tensor-matrix multiplication. | |
263 * | |
264 * This function accepts a 3-D tensor A of size n X m X k | |
265 * and a 2-D matrix B of size l X k. | |
266 * The function computes the 3-D tensor X of size n X m X l, where | |
267 * | |
268 * X(i,j,:) = B*A(i,j,:) | |
269 * | |
270 * for all i,j. | |
271 * | |
272 * Parameters: | |
273 * A - tensor of size n X m X k | |
274 * B - matrix of size l X k | |
275 * X - output tensor of size n X m X l | |
276 * alpha - real constant | |
277 * n, m, k, l - dimensions of A, B | |
278 * | |
279 * Note: This function re-writes the contents of X. | |
280 * | |
281 **************************************************************************/ | |
282 void tens_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k, mwSize l); | |
283 | |
284 | |
285 | |
286 /************************************************************************** | |
287 * Tensor-matrix-transpose multiplication. | |
288 * | |
289 * This function accepts a 3-D tensor A of size n X m X k | |
290 * and a 2-D matrix B of size k X l. | |
291 * The function computes the 3-D tensor X of size n X m X l, where | |
292 * | |
293 * X(i,j,:) = B'*A(i,j,:) | |
294 * | |
295 * for all i,j. | |
296 * | |
297 * Parameters: | |
298 * A - tensor of size n X m X k | |
299 * B - matrix of size k X l | |
300 * X - output tensor of size n X m X l | |
301 * alpha - real constant | |
302 * n, m, k, l - dimensions of A, B | |
303 * | |
304 * Note: This function re-writes the contents of X. | |
305 * | |
306 **************************************************************************/ | |
307 void tens_matT(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k, mwSize l); | |
308 | |
309 | |
310 | |
311 /************************************************************************** | |
312 * Vector-vector sum. | |
313 * | |
314 * Computes an operation of the form: | |
315 * | |
316 * y := alpha*x + y | |
317 * | |
318 * Parameters: | |
319 * x - vector of length n | |
320 * y - output vector of length n | |
321 * alpha - real constant | |
322 * n - length of x,y | |
323 * | |
324 * Note: This function re-writes the contents of y. | |
325 * | |
326 **************************************************************************/ | |
327 void vec_sum(double alpha, double x[], double y[], mwSize n); | |
328 | |
329 | |
330 | |
331 /************************************************************************** | |
332 * Triangular back substitution. | |
333 * | |
334 * Solve the set of linear equations | |
335 * | |
336 * T*x = b | |
337 * | |
338 * where T is lower or upper triangular. | |
339 * | |
340 * Parameters: | |
341 * ul - 'U' for upper triangular, 'L' for lower triangular | |
342 * A - matrix of size n x m containing T | |
343 * b - vector of length k | |
344 * x - output vector of length k | |
345 * n - size of first dimension of A | |
346 * k - the size of the equation set, k<=n,m | |
347 * | |
348 * Note: | |
349 * The matrix A can be of any size n X m, as long as n,m >= k. | |
350 * Only the lower/upper triangle of the submatrix A(1:k,1:k) defines the | |
351 * matrix T (depending on the parameter ul). | |
352 * | |
353 **************************************************************************/ | |
354 void backsubst(char ul, double A[], double b[], double x[], mwSize n, mwSize k); | |
355 | |
356 | |
357 | |
358 /************************************************************************** | |
359 * Solve a set of equations using a Cholesky decomposition. | |
360 * | |
361 * Solve the set of linear equations | |
362 * | |
363 * M*x = b | |
364 * | |
365 * where M is positive definite with a known Cholesky decomposition: | |
366 * either M=L*L' (L lower triangular) or M=U'*U (U upper triangular). | |
367 * | |
368 * Parameters: | |
369 * ul - 'U' for upper triangular, 'L' for lower triangular decomposition | |
370 * A - matrix of size n x m with the Cholesky decomposition of M | |
371 * b - vector of length k | |
372 * x - output vector of length k | |
373 * n - size of first dimension of A | |
374 * k - the size of the equation set, k<=n,m | |
375 * | |
376 * Note: | |
377 * The matrix A can be of any size n X m, as long as n,m >= k. | |
378 * Only the lower/upper triangle of the submatrix A(1:k,1:k) is used as | |
379 * the Cholesky decomposition of M (depending on the parameter ul). | |
380 * | |
381 **************************************************************************/ | |
382 void cholsolve(char ul, double A[], double b[], double x[], mwSize n, mwSize k); | |
383 | |
384 | |
385 | |
386 /************************************************************************** | |
387 * Maximum absolute value. | |
388 * | |
389 * Returns the index of the coefficient with maximal absolute value in a vector. | |
390 * | |
391 * Parameters: | |
392 * x - vector of length n | |
393 * n - length of x | |
394 * | |
395 **************************************************************************/ | |
396 mwIndex maxabs(double x[], mwSize n); | |
397 | |
398 | |
399 | |
400 /************************************************************************** | |
401 * Maximum vector element. | |
402 * | |
403 * Returns the index of the maximal coefficient in a vector. | |
404 * | |
405 * Parameters: | |
406 * x - vector of length n | |
407 * n - length of x | |
408 * | |
409 **************************************************************************/ | |
410 mwIndex maxpos(double x[], mwSize n); | |
411 | |
412 | |
413 | |
414 /************************************************************************** | |
415 * Vector-vector dot product. | |
416 * | |
417 * Computes an operation of the form: | |
418 * | |
419 * c = a'*b | |
420 * | |
421 * Parameters: | |
422 * a, b - vectors of length n | |
423 * n - length of a,b | |
424 * | |
425 * Returns: The dot product c. | |
426 * | |
427 **************************************************************************/ | |
428 double dotprod(double a[], double b[], mwSize n); | |
429 | |
430 | |
431 | |
432 /************************************************************************** | |
433 * Indexed vector assignment. | |
434 * | |
435 * Perform a permutation assignment of the form | |
436 * | |
437 * y = x(ind) | |
438 * | |
439 * where ind is an array of indices to x. | |
440 * | |
441 * Parameters: | |
442 * y - output vector of length k | |
443 * x - input vector of arbitrary length | |
444 * ind - array of indices into x (indices begin at 0) | |
445 * k - length of the array ind | |
446 * | |
447 **************************************************************************/ | |
448 void vec_assign(double y[], double x[], mwIndex ind[], mwSize k); | |
449 | |
450 | |
451 | |
452 /************************************************************************** | |
453 * Matrix transpose. | |
454 * | |
455 * Computes Y := X' | |
456 * | |
457 * Parameters: | |
458 * X - input matrix of size n X m | |
459 * Y - output matrix of size m X n | |
460 * n, m - dimensions of X | |
461 * | |
462 **************************************************************************/ | |
463 void transpose(double X[], double Y[], mwSize n, mwSize m); | |
464 | |
465 | |
466 | |
467 /************************************************************************** | |
468 * Print a matrix. | |
469 * | |
470 * Parameters: | |
471 * A - matrix of size n X m | |
472 * n, m - dimensions of A | |
473 * matname - name of matrix to display | |
474 * | |
475 **************************************************************************/ | |
476 void printmat(double A[], int n, int m, char* matname); | |
477 | |
478 | |
479 | |
480 /************************************************************************** | |
481 * Print a sparse matrix. | |
482 * | |
483 * Parameters: | |
484 * A - sparse matrix of type double | |
485 * matname - name of matrix to display | |
486 * | |
487 **************************************************************************/ | |
488 void printspmat(mxArray *A, char* matname); | |
489 | |
490 | |
491 #endif | |
492 |