annotate solvers/SMALL_ompGabor/myblas.h @ 174:dc2f0fa21310 danieleb

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