📄 pmatlib.h
字号:
/******************************** matrix library using pointers ** This library implements matrix arithmetic** Storage is based on the array of pointers idea for* flexible (and fast) indexing.* * Todd K. Moon* Utah State University* Started: 4 September 1991*********************************//* Copyright 2004 by Todd K. Moon Permission is granted to use this program/data for educational/research only*/#ifndef _PMATLIB_H_#define _PMATLIB_H_#include <stdio.h> /* to define NULL */#define THRESH 10e-30 /* An error threshold. How close can two numbers be */ /* and still not be equal, > THRESH.*/#define ONESVEC(mat,type,num) {int ionesvec; \ mat = (type *)calloc(num,sizeof(type)); \ if(mat==NULL) { \ fprintf(stderr,"Cannot allocate space for vector %s\n",#mat);\ exit(-1); } \ for(ionesvec = 0; ionesvec < num; ionesvec++) \ mat[ionesvec] = 1; }#define ONESMAT(mat,type,m,n) {int ionesmat,jonesmat; \ mat = (type **)pcalloc_matrix2(m,n,sizeof(type)); \ if(mat==NULL) { \ fprintf(stderr,"Cannot allocate space for matrix %s\n",#mat);\ exit(-1); } \ for(ionesmat = 0; ionesmat < m; ionesmat++) \ for(jonesmat = 0; jonesmat < n; jonesmat++) \ mat[ionesmat][jonesmat] = 1;}/* allocate a vector of arbitrary type (even typdef'ed types) */#define VECTOR(name,type,num) type *name; CALLOCVECTOR(name,type,num);#define MATRIX(name,type,n1,n2) type **name; CALLOCMATRIX(name,type,n1,n2);#define TENSOR(nam,type,n1,n2,n3)type ***nam;CALLOCTENSOR(nam,type,n1,n2,n3);#define CALLOCVECTOR(name,type,num) {name = \ (type *)pcalloc_vector2(num,sizeof(type)); \ if(name == NULL) { \ fprintf(stderr,"Cannot allocate space for vector %s\n",#name);\ exit(-1); }}/* allocate a matrix of arbitrary type (even typdef'ed types) */#define CALLOCMATRIX(name,type,rows,cols) {name = \ (type **)pcalloc_matrix2(rows,cols,sizeof(type)); \ if(name == NULL) \ { fprintf(stderr,"Cannot allocate space for matrix %s\n",#name);\ exit(-1); }}/* allocate a tensor of arbitrary type (even typdef'ed types) */#define CALLOCTENSOR(name,type,nmat,rows,cols) {name = \ (type ***)pcalloc_tensor2(nmat,rows,cols,sizeof(type)); \ if(name == NULL) \ { fprintf(stderr,"Cannot allocate space for tensor %s\n",#name);\ exit(-1); }}/* declare a matrix of size (nr x nc) */void **pcalloc_matrix(int type,int nr,int nc,char *name);/* allocate a matrix of size nr x nc, with elements of given size *//* used by macro CALLOCMATRIX */void **pcalloc_matrix2(int length,int width, int sizeoftype);/* declare a vector of size (length) */void *pcalloc_vector(int type,int length,char *name);/* allocate a matrix of size length with elements of a given size *//* used by macro CALLOCVECTOR */void *pcalloc_vector2(int length, int sizeoftype);/* allocate a tensor of size nmat x nr x nc, with elements of given size *//* used by macro CALLOCMATRIX */void ***pcalloc_tensor2(int nmat, int length,int width, int sizeoftype);/* free a matrix of size (nr x nc) */void pfree_matrix(void ***array);/* free an array of size (length) */void pfree_vector(void **vector);/* alloc a nstack x nrow x ncol tensor */void ***pcalloc_tensor(int type,int nstack,int nrow,int ncol,char *s);void pfree_tensor(void ****m,int nstack);/* set a vector to 0 */void pveczerod(int *v,int dim);void pveczerof(float *v,int dim);void pveczerolf(double *v,int dim);/* set a matrix to 0 */void pmatzerof(float **m, int r, int c);void pmatzerolf(double **m, int r, int c);/* set vector to a constant */void pvecconstf(float *v, float c, int dim);void pvecconstlf(double *v, double c, int dim);/* set matrix to a constant */void pmatconstf(float **mat, float c, int m, int n);void pmatconstlf(double **mat, double c, int m, int n);/* Add two vectors: c= a + b */void pvecaddf(float *a, float *b, float *c, int n);void pvecaddlf(double *a, double *b, double *c, int n);/* Subtract two vectors: c = a-b;*/void pvecsubf(float *a, float *b, float *c, int n);void pvecsublf(double *a, double *b, double *c, int n);/* Multiply a vector times a scalar: c = b*a */void pvecscalef(float *a,float b, float *c, int n);void pvecscalelf(double *a, double b, double *c, int n);/* Inner product: c = a'b */float pvecinnerf(float *a, float *b, int n);double pvecinnerlf(double *a, double *b, int n);/* Multiply two vectors element by element */void pvecvecmultf(float *a, float *b, float *c, int n);void pvecvecmultlf(double *a, double *b, double *c, int n);/* Copy vectors: a --> b */void pveccopylf(double *a,double *b,int m);void pveccopyf(float *a,float *b,int m);void pveccopyd(int *a,int *b,int m);void pveccopyc(char *a,char *b,int m);/* Vector norms: */double pvecnorm2lf(double *, int);float pvecnorm2f(float *, int);/********************************************************************//* convolve the sequence in1 (of n1 points) with the sequence in2 (of n2 points) into out This does not do "fast" convolution (e.g., using FFTs). Return value is the length of the new sequence, n1+n2-1*/int pconvlf(double *in1, int n1, double *in2, int n2, double *out);/********************************************************************//* return index of min (resp. max) of vectors */int pminveclf(double *v, int n);int pmaxveclf(double *v, int n);int pminvecf(float *v, int n);int pminvecf(float *v, int n);/********************************************************************//* Add matrices of size (m x n): c = a + b */void pmataddf(float **a,float **b,float ** c,int m,int n);void pmataddlf(double **a,double **b,double ** c,int m,int n);/* subtract matrices of size (m x n): c = a - b */void pmatsubf(float **a,float **b,float ** c,int m,int n);void pmatsublf(double **a,double **b,double ** c,int m,int n);/* Multiply matrices element by element */void pmatmatmultf(float **a,float **b,float ** c,int m,int n);void pmatmatmultlf(double **a,double **b,double ** c,int m,int n);/* Add and scale: c = d*(a+b), where d is a scalar constant */void pmataddandscalelf(double **a,double **b,double d,double ** c,int m,int n);void pmataddandscalef(float **a,float **b,float d,float ** c,int m,int n);/********************************************************************//* Copy matrix: a --> b */void pmatcopylf(double **a, double **b, int m, int n);void pmatcopyf(float **a, float **b, int m, int n);/* Multiply a matrix times a scalar: c = b*a, a is nxm */void pmatscalef(float **a, float b, float **c, int n, int m);void pmatscalelf(double **a, double b, double **c, int n, int m);/* transpose a matrix (m x n) into b matrix, assumed to be declared as *//* (n x m) */void pmattposef(float **a, float **b, int m, int n);void pmattpoself(double **a, double **b, int m, int n);/* Multiply two matrices: c= a*b (no transposes are assumed, so it avoids the overhead of the switch)*/void pmatmultlf(double **a, double **b, double **c, int n, int m, int q);void pmatmultf(float **a, float **b, float **c, int n, int m, int q);/* Matrix multiplication with transposes: Compute one of the following forms, depending on the value of type: type 0: c = a*b a is n x m b is m x q c is n x q type 1: c = a*b' a is n x m b is q x m c is n x q type 2: c = a'*b a is m x n b is m x q c is n x q type 3: c = a'*b' a is m x n b is q x m c is n x q*/void pmatmultTf(float **a,float **b,float **c,int n,int m,int q,int type);void pmatmultTlf(double **a,double **b,double **c,int n,int m,int q,int type);/* Multiply matrix x vector: a is nxm, b is mx1, c is nx1 */void pmatvecmultf(float **a, float *b,float *c, int n, int m);void pmatvecmultlf(double **a, double *b,double *c, int n, int m);/* Multiply a*b', where a is mx1 and b is nx1, to obtain the mxn outer product c */void pvecouterlf(double *a, double *b, double **c, int m, int n);void pvecouterf(float *a, float *b, float **c, int m, int n);/* Multiply a vector (transpose) times a matrix: c = a'*b *//* a is (nx1), b is (nxm), c is (1xm) (a vector) */void pvecmatmultlf(double *a,double **b, double *c,int n,int m);void pvecmatmultf(float *a,float **b, float *c,int n,int m);/* compute v'Mw, and return the result. Generalized vector inner product */double pvecinnerMlf(double *v, double **M, double *w,int n);float pvecinnerMf(float *v, float **M, float *w, int n);/* Multiply a vector (transpose) times a matrix transpose: c = a'*b' *//* a is (nx1), b is (mxn), c is (1xm) (a vector) */void pvecmatTmultlf(double *a,double **b, double *c,int n,int m);void pvecmatTmultf(float *a,float **b, float *c,int n,int m);/* Multiply a matrix transpose times a vector: c = A'*b, A is (nxm), b is(nx1), c is (mx1) */void pmatTvecmultf(float **A, float *b,float *c, int n, int m);void pmatTvecmultlf(double **A, double *b,double *c, int n, int m);/* form the product c = a * diag(d), where a is mxn and d is nx1 */void pmatdiagmultf(float **a, float *d, float **c, int m, int n);void pmatdiagmultlf(double **a, double *d, double **c, int m, int n);/* Find the trace of a matrix. */float pmattracef(float **A, int M);double pmattracelf(double **A, int M);/* compute the fobenius norm of a matrix */double pfrobeniuslf( double **a, int rows, int cols );float pfrobeniusf( float **a, int rows, int cols );/* Copy tensors (space already allocated) a ---> b*/void ptencopylf(double ***a, double ***b, int nstack, int r, int c);void ptencopyf(float ***a, float ***b, int nstack, int r, int c);/* Printing routines: */void pmatprint(char *name,void **mat, int m, int n,int type);void pmatprints(char *name,char **mat,int n);void pmatprintsc(char *name,signed char **mat,int n,int m);void pmatprintuc(char *name,unsigned char **mat,int n,int m);void pmatprinthd(char *name,short int **mat, int m, int n);void pmatprintd(char *name,int **mat, int m, int n);void pmatprintx(char *name,int **mat,int n,int m);void pmatprintf(char *name,float **mat, int m, int n);void pmatprintlf(char *name,double **mat, int m, int n);void pmatprintld(char *name,long int **mat, int m, int n);/* control if a matrix is printed in a row or in matrix form *//* default is to print in matrix form */void setprintinrow();void clearprintinrow();/* set the format of integer printing for printf. E.g., %4.3d is obtained with setintprintwidths(4,3); (default is 0.0)*/void setintprintwidths(int w1,int w2);/* set the format of float printing for printf. E.g., %4.3d is obtained with setintprintwidths(4,3); (default is 7.4)*/void setfloatprintwidths(int w1,int w2);void setmatdelim(char *leftdelim, char *rightdelim);void setmatrowdelim(char *rowdelim);void setprintnamewnl(int p);/* set output file stream */void pprinttofile(FILE *fout);void pprinttostdout();void pvecprint(char *name,void *v,int n,int type);void pvecprintd(char *name, int *v, int n);void pvecprintx(char *name,int *v,int n);void pvecprinthd(char *name, short int *v, int n);void pvecprintld(char *name, long int *v, int n);void pvecprintsc(char *name, signed char *v, int n);void pvecprintuc(char *name, unsigned char *v, int n);void pvecprints(char *name,char *v);void vecprints(char *name,char *v);void pvecprintf(char *name, float *v, int n);void pvecprintlf(char *name, double *v, int n);void ptenprint(char *name, void ***ten,int nstack,int n, int m, int type);void ptenprints(char *name,char ***ten,int nstack,int m);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -