Attachment 'matrix.h'

Download

   1 #ifndef MATRIX_H
   2 #define MATRIX_H
   3 
   4 #ifdef X
   5 #undef X
   6 #endif
   7 
   8 #include <stdio.h>
   9 
  10 /* matrices and vectors are the same data type, vector will only
  11    have one column (by default) or one row.
  12 */
  13 typedef struct
  14 {
  15   short   type ;
  16   int     rows ;
  17   int     cols ;
  18   float **rptr;    /* pointer to an array of rows */
  19   float *data;     /* pointer to base of data */
  20   FILE *mmapfile;
  21 } MATRIX, VECTOR ;
  22 
  23 typedef struct
  24 {
  25   float  real ;
  26   float  imag ;
  27 } COMPLEX_FLOAT, *CPTR ;
  28 
  29 #define MATRIX_CELT(m,r,c)      (((COMPLEX_FLOAT **)m->rptr)[r]+c)
  30 #define MATRIX_RELT(m,r,c)      (m->rptr[r]+c)
  31 #define MATRIX_ELT(m,r,c)       (m->type == MATRIX_REAL ? \
  32                                   *MATRIX_RELT(m,r,c) : \
  33                                   *MATRIX_CELT(m,r,c))
  34 #define MATRIX_PTR(m,r,c)       (m->type == MATRIX_REAL ? \
  35                                   MATRIX_RELT(m,r,c) : \
  36                                   (float *)MATRIX_CELT(m,r,c))
  37 
  38 #define MATRIX_CELT_REAL(m,r,c)  (MATRIX_CELT(m,r,c)->real)
  39 #define MATRIX_CELT_IMAG(m,r,c)  (MATRIX_CELT(m,r,c)->imag)
  40 
  41 #define MATRIX_REAL        1
  42 #define MATRIX_COMPLEX     2
  43 #define MATRIX_SYM    0
  44 #define MATRIX_UPPER  1
  45 #define MATRIX_LOWER  2
  46 
  47 MATRIX  *MatrixReshape(MATRIX *m_src, MATRIX *m_dst, int rows, int cols) ;
  48 int     MatrixCheck(MATRIX *m) ;
  49 MATRIX  *MatrixInverse(MATRIX *mIn, MATRIX *mOut) ;
  50 MATRIX  *MatrixPseudoInverse(MATRIX *m, MATRIX *m_pseudo_inv) ;
  51 MATRIX  *MatrixSVDPseudoInverse(MATRIX *m, MATRIX *m_pseudo_inv) ;
  52 MATRIX  *MatrixRightPseudoInverse(MATRIX *m, MATRIX *m_pseudo_inv) ;
  53 #define MatrixLeftPseudoInverse MatrixPseudoInverse
  54 MATRIX  *MatrixAlloc(int rows, int cols, int type) ;
  55 int     MatrixFree(MATRIX **pmat) ;
  56 MATRIX  *MatrixMultiply(MATRIX *m1, MATRIX *m2, MATRIX *m3) ;
  57 MATRIX  *MatrixCopy(MATRIX *mIn, MATRIX *mOut) ;
  58 int     MatrixWriteTxt(char *fname, MATRIX *mat) ;
  59 MATRIX  *MatrixReadTxt(char *fname, MATRIX *mat) ;
  60 MATRIX  *MatrixRead(char *fname) ;
  61 int     MatrixWrite(MATRIX *mIn, char *fname, char *name) ;
  62 MATRIX  *MatrixIdentity(int n, MATRIX *mI) ;
  63 int     MatrixPrint(FILE *fp, MATRIX *mat) ;
  64 int     MatrixPrintOneLine(FILE *fp, MATRIX *mat) ;
  65 int     MatrixPrintTranspose(FILE *fp, MATRIX *mat) ;
  66 MATRIX  *MatrixTranspose(MATRIX *mIn, MATRIX *mOut) ;
  67 MATRIX  *MatrixAdd(MATRIX *m1, MATRIX *m2, MATRIX *mOut) ;
  68 MATRIX  *MatrixSubtract(MATRIX *m1, MATRIX *m2, MATRIX *mOut) ;
  69 MATRIX  *MatrixScalarMul(MATRIX *mIn, float val, MATRIX *mOut) ;
  70 MATRIX  *MatrixClear(MATRIX *mat) ;
  71 MATRIX  *MatrixSquareElts(MATRIX *mIn, MATRIX *mOut) ;
  72 MATRIX  *MatrixSignedSquareElts(MATRIX *mIn, MATRIX *mOut) ;
  73 MATRIX  *MatrixSqrtElts(MATRIX *mIn, MATRIX *mOut) ;
  74 MATRIX  *MatrixDiag(MATRIX *mDiag, MATRIX *mOut) ;
  75 MATRIX  *MatrixMakeDiagonal(MATRIX *mSrc, MATRIX *mDst) ;
  76 MATRIX  *MatrixCopyRegion(MATRIX *mSrc, MATRIX *mDst, int start_row,
  77                           int start_col, int rows, int cols,
  78                           int dest_row, int dest_col) ;
  79 MATRIX  *MatrixCopyRealRegion(MATRIX *mSrc, MATRIX *mDst,int start_row,
  80                               int start_col, int rows, int cols,
  81                               int dest_row, int dest_col) ;
  82 MATRIX  *MatrixCopyImagRegion(MATRIX *mSrc, MATRIX *mDst, int start_row,
  83                               int start_col, int rows, int cols,
  84                               int dest_row, int dest_col) ;
  85 MATRIX  *MatrixSetRegion(MATRIX *mSrc, MATRIX *mDst, int start_row,
  86                          int start_col, int rows, int cols, float val);
  87 MATRIX *MatrixRealToComplex(MATRIX *mReal, MATRIX *mImag, MATRIX *mOut);
  88 MATRIX *MatrixRegularize(MATRIX *mIn, MATRIX *mOut) ;
  89 int    MatrixSingular(MATRIX *m) ;
  90 MATRIX *MatrixToeplitz(VECTOR *v, MATRIX *T, int Type);
  91 
  92 /* determinants and eigenvectors */
  93 float  MatrixDeterminant(MATRIX *m) ;
  94 MATRIX *MatrixEigenSystem(MATRIX *m, float *evalues, MATRIX *m_dst) ;
  95 MATRIX *MatrixSVD(MATRIX *mA, VECTOR *v_z, MATRIX *mV) ;
  96 MATRIX *MatrixSVDInverse(MATRIX *m, MATRIX *m_inverse) ;
  97 float  MatrixNSConditionNumber(MATRIX *m);
  98 float  MatrixConditionNumber(MATRIX *m) ;
  99 float  MatrixSVDEigenValues(MATRIX *m, float *evalues) ;
 100 MATRIX *MatrixFactorSqrSVD(MATRIX *M, int Invert, MATRIX *D);
 101 
 102 /* statistical stuff */
 103 MATRIX *MatrixCovariance(MATRIX *mInputs, MATRIX *mCov, VECTOR *mMeans) ;
 104 MATRIX *MatrixUpdateCovariance(MATRIX *mInputs, MATRIX *mCov, VECTOR *mMeans);
 105 MATRIX *MatrixUpdateMeans(MATRIX *mInputs, VECTOR *mMeans, VECTOR *mNobs) ;
 106 MATRIX *MatrixFinalMeans(VECTOR *mMeans, VECTOR *mNobs) ;
 107 MATRIX *MatrixFinalCovariance(MATRIX *mInputs, MATRIX *mCov, VECTOR *mNobs);
 108 
 109 /* misc. I/O functions */
 110 int    MatrixAsciiWriteInto(FILE *fp, MATRIX *m) ;
 111 MATRIX *MatrixAsciiReadFrom(FILE *fp, MATRIX *m) ;
 112 int    MatrixAsciiWrite(char *fname, MATRIX *m) ;
 113 MATRIX *MatrixAsciiRead(char *fname, MATRIX *m) ;
 114 
 115 #define VectorAlloc(n, type)       MatrixAlloc(n, 1, type)
 116 #define RVectorAlloc(n, type)      MatrixAlloc(1, n, type)
 117 #define VectorFree(pm)             MatrixFree(pm)
 118 #define VectorAdd(v1, v2, v3)      MatrixAdd(v1, v2, v3)
 119 #define VectorSubtract(v1,v2,v3)   MatrixSubtract(v1, v2, v3)
 120 #define VectorScalarMul(v1,val,v2) MatrixScalarMul(v1, val, v2)
 121 #define VectorCopy(v1, v2)         MatrixCopy(v1, v2)
 122 #define VectorClear(v)             MatrixClear(v)
 123 #define VectorTranspose(vsrc,vdst) MatrixTranspose(vsrc, vdst)
 124 #define VectorAsciiWriteInto       MatrixAsciiWriteInto
 125 #define VectorAsciiReadFrom        MatrixAsciiReadFrom
 126 
 127 #define VECTOR_ELT(v,i)            ((v)->rptr[i][1])
 128 #define RVECTOR_ELT(v,i)            ((v)->rptr[1][i])
 129 #define VECTOR3_LOAD(v,x,y,z)    (VECTOR_ELT(v,1)=x, VECTOR_ELT(v,2)=y, \
 130                                   VECTOR_ELT(v,3)=z) ;
 131 #define VECTOR_LOAD   VECTOR3_LOAD
 132 #define V3_LOAD       VECTOR3_LOAD
 133 
 134 double Vector3Angle(VECTOR *v1, VECTOR *v2) ;
 135 float  VectorLen(VECTOR *v) ;
 136 float  VectorAngle(VECTOR *v1, VECTOR *v2) ;
 137 float  VectorDot(VECTOR *v1, VECTOR *v2) ;
 138 float  VectorNormalizedDot(VECTOR *v1, VECTOR *v2) ;
 139 float  VectorDistance(VECTOR *v1, VECTOR *v2) ;
 140 VECTOR *MatrixColumn(MATRIX *m, VECTOR *v, int col) ;
 141 MATRIX *VectorOuterProduct(VECTOR *v1, VECTOR *v2, MATRIX *m) ;
 142 VECTOR *VectorCrossProduct(VECTOR *v1, VECTOR *v2, VECTOR *vdst) ;
 143 float  VectorTripleProduct(VECTOR *v1, VECTOR *v2, VECTOR *v3) ;
 144 VECTOR *VectorNormalize(VECTOR *vin, VECTOR *vout) ;
 145 MATRIX *MatrixNormalizeCol(MATRIX *m, MATRIX *mcnorm);
 146 
 147 /* these are macro versions that work on 3-d vectors */
 148 #define RV3_X(v)     (RVECTOR_ELT(v,1))
 149 #define RV3_Y(v)     (RVECTOR_ELT(v,2))
 150 #define RV3_Z(v)     (RVECTOR_ELT(v,3))
 151 #define V3_X(v)      (VECTOR_ELT(v,1))
 152 #define V3_Y(v)      (VECTOR_ELT(v,2))
 153 #define V3_Z(v)      (VECTOR_ELT(v,3))
 154 #define V3_LEN(v)    (sqrt(V3_X(v)*V3_X(v)+V3_Y(v)*V3_Y(v)+V3_Z(v)*V3_Z(v)))
 155 #define V3_LEN_IS_ZERO(v)    (DZERO(V3_LEN_SQ(v)))
 156 #define V3_LEN_SQ(v) (V3_X(v)*V3_X(v)+V3_Y(v)*V3_Y(v)+V3_Z(v)*V3_Z(v))
 157 #define V3_CROSS_PRODUCT(va,vb,vc) \
 158                  V3_X(vc) = V3_Y(va)*V3_Z(vb)- V3_Z(va)*V3_Y(vb),  \
 159                  V3_Y(vc) = V3_Z(va)*V3_X(vb)- V3_X(va)*V3_Z(vb),  \
 160                  V3_Z(vc) = V3_X(va)*V3_Y(vb)- V3_Y(va)*V3_X(vb) ;
 161 #define V3_TRIPLE_PRODUCT(va,vb,vc) \
 162                 (V3_X(vc) * (V3_Y(va)*V3_Z(vb)- V3_Z(va)*V3_Y(vb)) + \
 163                  V3_Y(vc) * (V3_Z(va)*V3_X(vb)- V3_X(va)*V3_Z(vb)) + \
 164                  V3_Z(vc) * (V3_X(va)*V3_Y(vb)- V3_Y(va)*V3_X(vb)))
 165 #define V3_ADD(va,vb,vc) \
 166                  V3_X(vc) = V3_X(va)+V3_X(vb), \
 167                  V3_Y(vc) = V3_Y(va)+V3_Y(vb), \
 168                  V3_Z(vc) = V3_Z(va)+V3_Z(vb) ;
 169 #define V3_SUBTRACT(va,vb,vc) \
 170                  V3_X(vc) = V3_X(va)-V3_X(vb), \
 171                  V3_Y(vc) = V3_Y(va)-V3_Y(vb), \
 172                  V3_Z(vc) = V3_Z(va)-V3_Z(vb) ;
 173 #define V3_DOT(va,vb) (V3_X(va)*V3_X(vb)+V3_Y(va)*V3_Y(vb)+V3_Z(va)*V3_Z(vb))
 174 #define V3_SCALAR_MUL(va,s,vb)  (V3_X(vb)=V3_X(va)*s,\
 175                                  V3_Y(vb)=V3_Y(va)*s,\
 176                                  V3_Z(vb)=V3_Z(va)*s)
 177 
 178 #define V3_NORMALIZE(va,vb)  { float len = (V3_LEN(va)) ; \
 179                                   if (FZERO(len)) len = 1.0f ; \
 180                                   else len = 1.0f / len ; \
 181                                   V3_SCALAR_MUL(va,len,vb) ; }
 182 #define V3_CLEAR(v)          (V3_X(v) = 0, V3_Y(v) = 0, V3_Z(v) = 0)
 183 
 184 #define X_ROTATION   0
 185 #define Y_ROTATION   1
 186 #define Z_ROTATION   2
 187 
 188 MATRIX *MatrixAllocRotation(int n, float angle, int which) ;
 189 MATRIX *MatrixReallocRotation(int n, float angle, int which, MATRIX *m) ;
 190 MATRIX *MatrixAllocTranslation(int n, double *trans) ;
 191 #define MatrixClone(mat)   MatrixCopy(mat, NULL)
 192 #define VectorClone        MatrixClone
 193 
 194 float MatrixTrace(MATRIX *M);
 195 MATRIX *MatrixVertCat(MATRIX *m1, MATRIX *m2, MATRIX *mcat);
 196 MATRIX *MatrixHorCat(MATRIX *m1, MATRIX *m2, MATRIX *mcat);
 197 
 198 MATRIX *MatrixConstVal(float val, int rows, int cols, MATRIX *X);
 199 MATRIX *MatrixZero(int rows, int cols, MATRIX *X);
 200 MATRIX *MatrixSum(MATRIX *m, int dim, MATRIX *msum);
 201 MATRIX *MatrixDRand48(int rows, int cols, MATRIX *m);
 202 MATRIX *MatrixSimilarityTransform(MATRIX *m_src, MATRIX *m_mul, MATRIX *m_dst);
 203 
 204 double VectorSum(MATRIX *v);
 205 double VectorMean(MATRIX *v);
 206 double VectorVar(MATRIX *v, double *pMean);
 207 double VectorStdDev(MATRIX *v, double *pMean);
 208 double VectorRange(MATRIX *v, double *pVmin, double *pVmax);
 209 
 210 MATRIX *GaussianMatrix(int len, float std, int norm, MATRIX *G);
 211 MATRIX *GaussianVector(int len, float mean, float std, int norm, MATRIX *g);
 212 MATRIX *MatrixReorderRows(MATRIX *X, int *NewRowOrder, MATRIX *XRO);
 213 int MatrixRandPermRows(MATRIX *X);
 214 
 215 #endif
 216 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-01-26 22:35:32, 12.9 KB) [[attachment:fio.c]]
  • [get | view] (2009-01-26 22:35:32, 1.3 KB) [[attachment:fio.h]]
  • [get | view] (2009-01-26 22:35:32, 5.9 KB) [[attachment:load_mgh.m]]
  • [get | view] (2009-01-26 22:35:32, 9.3 KB) [[attachment:matrix.h]]
  • [get | view] (2009-01-26 22:35:32, 390.2 KB) [[attachment:mri.c]]
  • [get | view] (2009-01-26 22:35:32, 48.5 KB) [[attachment:mri.h]]
  • [get | view] (2009-01-26 22:35:32, 94.4 KB) [[attachment:mri_convert.c]]
  • [get | view] (2009-01-26 22:35:32, 14.1 KB) [[attachment:mri_info.c]]
  • [get | view] (2009-01-26 22:35:32, 395.4 KB) [[attachment:mriio.c]]
  • [get | view] (2009-01-26 22:35:32, 2.4 KB) [[attachment:save_mgh.m]]
  • [get | view] (2009-01-26 22:35:32, 2.2 KB) [[attachment:tags.c]]
  • [get | view] (2009-01-26 22:35:32, 0.9 KB) [[attachment:tags.h]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.