idamnjanovic@51: /************************************************************************** idamnjanovic@51: * idamnjanovic@51: * File name: myblas.h idamnjanovic@51: * idamnjanovic@51: * Ron Rubinstein idamnjanovic@51: * Computer Science Department idamnjanovic@51: * Technion, Haifa 32000 Israel idamnjanovic@51: * ronrubin@cs idamnjanovic@51: * idamnjanovic@51: * Version: 1.1 idamnjanovic@51: * Last updated: 17.8.2009 idamnjanovic@51: * idamnjanovic@51: * A collection of basic linear algebra functions, in the spirit of the idamnjanovic@51: * BLAS/LAPACK libraries. idamnjanovic@51: * idamnjanovic@51: *************************************************************************/ idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: #ifndef __MY_BLAS_H__ idamnjanovic@51: #define __MY_BLAS_H__ idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: #include "mex.h" idamnjanovic@51: #include idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Squared value. idamnjanovic@51: **************************************************************************/ idamnjanovic@51: #define SQR(X) ((X)*(X)) idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A*x idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * x - vector of length m idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void mat_vec(double alpha, double A[], double x[], double y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix-transpose-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A'*x idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * x - vector of length n idamnjanovic@51: * y - output vector of length m idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void matT_vec(double alpha, double A[], double x[], double y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Sparse-matrix-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A*x idamnjanovic@51: * idamnjanovic@51: * where A is a sparse matrix. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * pr,ir,jc - sparse representation of the matrix A, of size n x m idamnjanovic@51: * x - vector of length m idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void mat_sp_vec(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double x[], double y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Sparse-matrix-transpose-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A'*x idamnjanovic@51: * idamnjanovic@51: * where A is a sparse matrix. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * pr,ir,jc - sparse representation of the matrix A, of size n x m idamnjanovic@51: * x - vector of length m idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void matT_sp_vec(double alpha, double pr[], mwIndex ir[], mwIndex jc[], double x[], double y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix-sparse-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A*x idamnjanovic@51: * idamnjanovic@51: * where A is a matrix and x is a sparse vector. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * pr,ir,jc - sparse representation of the vector x, of length m idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void mat_vec_sp(double alpha, double A[], double pr[], mwIndex ir[], mwIndex jc[], double y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix-transpose-sparse-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A'*x idamnjanovic@51: * idamnjanovic@51: * where A is a matrix and x is a sparse vector. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * pr,ir,jc - sparse representation of the vector x, of length n idamnjanovic@51: * y - output vector of length m idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void matT_vec_sp(double alpha, double A[], double pr[], mwIndex ir[], mwIndex jc[], double y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Sparse-matrix-sparse-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A*x idamnjanovic@51: * idamnjanovic@51: * where A is a sparse matrix and x is a sparse vector. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * pr,ir,jc - sparse representation of the matrix A, of size n x m idamnjanovic@51: * prx,irx,jcx - sparse representation of the vector x (of length m) idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: 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); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Sparse-matrix-transpose-sparse-vector multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*A'*x idamnjanovic@51: * idamnjanovic@51: * where A is a sparse matrix and x is a sparse vector. idamnjanovic@51: * idamnjanovic@51: * Importnant note: this function is provided for completeness, but is NOT efficient. idamnjanovic@51: * If possible, convert x to non-sparse representation and use matT_vec_sp instead. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * pr,ir,jc - sparse representation of the matrix A, of size n x m idamnjanovic@51: * prx,irx,jcx - sparse representation of the vector x (of length n) idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: 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); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix-matrix multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * X := alpha*A*B idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * B - matrix of size m X k idamnjanovic@51: * X - output matrix of size n X k idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m, k - dimensions of A, B idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of X. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void mat_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix-transpose-matrix multiplication. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * X := alpha*A*B idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * B - matrix of size m X k idamnjanovic@51: * X - output matrix of size n X k idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m, k - dimensions of A, B idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of X. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void matT_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Tensor-matrix multiplication. idamnjanovic@51: * idamnjanovic@51: * This function accepts a 3-D tensor A of size n X m X k idamnjanovic@51: * and a 2-D matrix B of size l X k. idamnjanovic@51: * The function computes the 3-D tensor X of size n X m X l, where idamnjanovic@51: * idamnjanovic@51: * X(i,j,:) = B*A(i,j,:) idamnjanovic@51: * idamnjanovic@51: * for all i,j. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - tensor of size n X m X k idamnjanovic@51: * B - matrix of size l X k idamnjanovic@51: * X - output tensor of size n X m X l idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m, k, l - dimensions of A, B idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of X. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void tens_mat(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k, mwSize l); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Tensor-matrix-transpose multiplication. idamnjanovic@51: * idamnjanovic@51: * This function accepts a 3-D tensor A of size n X m X k idamnjanovic@51: * and a 2-D matrix B of size k X l. idamnjanovic@51: * The function computes the 3-D tensor X of size n X m X l, where idamnjanovic@51: * idamnjanovic@51: * X(i,j,:) = B'*A(i,j,:) idamnjanovic@51: * idamnjanovic@51: * for all i,j. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - tensor of size n X m X k idamnjanovic@51: * B - matrix of size k X l idamnjanovic@51: * X - output tensor of size n X m X l idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n, m, k, l - dimensions of A, B idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of X. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void tens_matT(double alpha, double A[], double B[], double X[], mwSize n, mwSize m, mwSize k, mwSize l); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Vector-vector sum. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * y := alpha*x + y idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * x - vector of length n idamnjanovic@51: * y - output vector of length n idamnjanovic@51: * alpha - real constant idamnjanovic@51: * n - length of x,y idamnjanovic@51: * idamnjanovic@51: * Note: This function re-writes the contents of y. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void vec_sum(double alpha, double x[], double y[], mwSize n); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Triangular back substitution. idamnjanovic@51: * idamnjanovic@51: * Solve the set of linear equations idamnjanovic@51: * idamnjanovic@51: * T*x = b idamnjanovic@51: * idamnjanovic@51: * where T is lower or upper triangular. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * ul - 'U' for upper triangular, 'L' for lower triangular idamnjanovic@51: * A - matrix of size n x m containing T idamnjanovic@51: * b - vector of length k idamnjanovic@51: * x - output vector of length k idamnjanovic@51: * n - size of first dimension of A idamnjanovic@51: * k - the size of the equation set, k<=n,m idamnjanovic@51: * idamnjanovic@51: * Note: idamnjanovic@51: * The matrix A can be of any size n X m, as long as n,m >= k. idamnjanovic@51: * Only the lower/upper triangle of the submatrix A(1:k,1:k) defines the idamnjanovic@51: * matrix T (depending on the parameter ul). idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void backsubst(char ul, double A[], double b[], double x[], mwSize n, mwSize k); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Solve a set of equations using a Cholesky decomposition. idamnjanovic@51: * idamnjanovic@51: * Solve the set of linear equations idamnjanovic@51: * idamnjanovic@51: * M*x = b idamnjanovic@51: * idamnjanovic@51: * where M is positive definite with a known Cholesky decomposition: idamnjanovic@51: * either M=L*L' (L lower triangular) or M=U'*U (U upper triangular). idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * ul - 'U' for upper triangular, 'L' for lower triangular decomposition idamnjanovic@51: * A - matrix of size n x m with the Cholesky decomposition of M idamnjanovic@51: * b - vector of length k idamnjanovic@51: * x - output vector of length k idamnjanovic@51: * n - size of first dimension of A idamnjanovic@51: * k - the size of the equation set, k<=n,m idamnjanovic@51: * idamnjanovic@51: * Note: idamnjanovic@51: * The matrix A can be of any size n X m, as long as n,m >= k. idamnjanovic@51: * Only the lower/upper triangle of the submatrix A(1:k,1:k) is used as idamnjanovic@51: * the Cholesky decomposition of M (depending on the parameter ul). idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void cholsolve(char ul, double A[], double b[], double x[], mwSize n, mwSize k); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Maximum absolute value. idamnjanovic@51: * idamnjanovic@51: * Returns the index of the coefficient with maximal absolute value in a vector. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * x - vector of length n idamnjanovic@51: * n - length of x idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: mwIndex maxabs(double x[], mwSize n); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Maximum vector element. idamnjanovic@51: * idamnjanovic@51: * Returns the index of the maximal coefficient in a vector. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * x - vector of length n idamnjanovic@51: * n - length of x idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: mwIndex maxpos(double x[], mwSize n); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Vector-vector dot product. idamnjanovic@51: * idamnjanovic@51: * Computes an operation of the form: idamnjanovic@51: * idamnjanovic@51: * c = a'*b idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * a, b - vectors of length n idamnjanovic@51: * n - length of a,b idamnjanovic@51: * idamnjanovic@51: * Returns: The dot product c. idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: double dotprod(double a[], double b[], mwSize n); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Indexed vector assignment. idamnjanovic@51: * idamnjanovic@51: * Perform a permutation assignment of the form idamnjanovic@51: * idamnjanovic@51: * y = x(ind) idamnjanovic@51: * idamnjanovic@51: * where ind is an array of indices to x. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * y - output vector of length k idamnjanovic@51: * x - input vector of arbitrary length idamnjanovic@51: * ind - array of indices into x (indices begin at 0) idamnjanovic@51: * k - length of the array ind idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void vec_assign(double y[], double x[], mwIndex ind[], mwSize k); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Matrix transpose. idamnjanovic@51: * idamnjanovic@51: * Computes Y := X' idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * X - input matrix of size n X m idamnjanovic@51: * Y - output matrix of size m X n idamnjanovic@51: * n, m - dimensions of X idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void transpose(double X[], double Y[], mwSize n, mwSize m); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Print a matrix. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - matrix of size n X m idamnjanovic@51: * n, m - dimensions of A idamnjanovic@51: * matname - name of matrix to display idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void printmat(double A[], int n, int m, char* matname); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: /************************************************************************** idamnjanovic@51: * Print a sparse matrix. idamnjanovic@51: * idamnjanovic@51: * Parameters: idamnjanovic@51: * A - sparse matrix of type double idamnjanovic@51: * matname - name of matrix to display idamnjanovic@51: * idamnjanovic@51: **************************************************************************/ idamnjanovic@51: void printspmat(mxArray *A, char* matname); idamnjanovic@51: idamnjanovic@51: idamnjanovic@51: #endif idamnjanovic@51: