mi32/matrix.h

Go to the documentation of this file.
00001 /**
00002  * \file matrix.h <mi32/matrix.h>
00003  * \brief Definitions for matrix manipulation functions
00004  *
00005  * \if NODOC
00006  * $Id: matrix.h_v 1.23 2005/03/31 16:57:15 fileserver!dwilliss Exp $
00007  *
00008  * $Log: matrix.h_v $
00009  * Revision 1.23  2005/03/31 16:57:15  fileserver!dwilliss
00010  * Rename one of our types to MIUNICODE because it conflicted with a Microsoft #define
00011  *
00012  * Revision 1.22  2003/09/15 13:49:56  fileserver!dwilliss
00013  * Doxygen
00014  *
00015  * Revision 1.21  2002/06/12 13:13:25  mju
00016  * nc.
00017  *
00018  * Revision 1.20  2000/12/22 17:50:23  dwilliss
00019  * Principle Componants function now takes real pointer for type-safety
00020  * instead of (void*)
00021  *
00022  * Revision 1.19  2000/06/20 17:56:01  sparsons
00023  * Genitor documentation.
00024  *
00025  * Revision 1.18  2000/06/14 13:26:27  mju
00026  * Include stddefns so compiles in isolation.
00027  *
00028  * \endif
00029 **/
00030 
00031 #ifndef  INC_MI32_MATRIX_H
00032 #define  INC_MI32_MATRIX_H
00033 
00034 #ifndef INC_MI32_STDDEFNS_H
00035 #include <mi32/stddefns.h>
00036 #endif
00037 
00038 #if defined(__cplusplus)
00039 extern "C" {
00040 #endif
00041 
00042 
00043 struct PRINCOMPPARMS;   //!< For a prototype later...
00044 
00045 typedef struct _MATRIX {
00046    double **matrix;                    //!<  Matrix data 
00047    UINT16 rows;                        //!<  Number of rows 
00048    UINT16 cols;                        //!<  Number of columns 
00049    UINT32 flags;
00050    } *MATRIX;
00051 
00052 #define  MATFLAG_Symmetric       0x00000001        //!<  Matrix is symmetric 
00053 #define  MATFLAG_NoDiagonal      0x00000002        //!<  Don't allocate space for diagonal (symmetric only) 
00054 
00055 //! Retrieve value from matrix.
00056 #define  MatGetValue(m,i,j)      ((m)->matrix[i][j])
00057 //! Set single matrix entry to a specified value.
00058 #define  MatSetValue(m,i,j,v)    ((m)->matrix[i][j]=(v))
00059 //! Determine if "symmetric" flag set for a matrix.
00060 #define  MatIsSymmetric(m)       ((m)->flags&MATFLAG_Symmetric)
00061 
00062 //! Add one matrix to another of the same size.
00063 int MatAddMatrix (
00064    MATRIX outmatrix,                   //!< Output matrix, may be the same as either input matrix
00065    MATRIX matrixA,                     //!< First input matrix
00066    MATRIX matrixB                      //!< Second input matrix, if NULL will use outmatrix
00067    );
00068 
00069 //! Add scalar value to a matrix.
00070 void MatAddScalar (
00071    MATRIX matrix,                      //!< Matrix to use 
00072    double value                        //!< Value to add to all matrix elements
00073    );
00074 
00075 //! Copy one matrix to another.
00076 //!
00077 //! The output matrix will be resized if it exists and is not the same size as the input matrix.
00078 int MatCopy (
00079    MATRIX *outmatrix,                  //!< Matrix to copy to, will be created if NULL
00080    MATRIX inmatrix                     //!< Matrix to copy from
00081    );
00082 
00083 //! Create a new matrix.
00084 //!
00085 //! Flags:
00086 //!      MATFLAG_Symmetric    Matrix is symmetric, don't allocate space for elements above diagonal
00087 //!      MATFLAG_NoDiagonal   Don't allocate space for diagonal elements (symmetric only)
00088 //!
00089 //!   All matrix elements will be initialized to 0.
00090 int MatCreate (
00091    MATRIX *matrix,                     //!< Matrix returned
00092    UINT16 rows,                        //!< Number of rows in matrix
00093    UINT16 cols,                        //!< Number of columns in matrix
00094    UINT32 flags                        //!< Flags
00095    );
00096 
00097 //! Destroy a matrix.
00098 void MatDestroy (
00099    MATRIX matrix                       //!< Matrix to be destroyed
00100    );
00101 
00102 //! Compute determinate of a square matrix.
00103 int MatDeterminant (
00104    MATRIX matrix,                      //!< Matrix to compute determinant for
00105    double *detvalue                    //!< Determinant returned
00106    );
00107 
00108 //! Create or extract diagonals.
00109 //!
00110 //! If input is a vector with N components MatDiag(input,K) is a square matrix
00111 //!   of order N + ABS(K) with elements of input on the K-th diagonal.
00112 //!   K = 0 is a main diagonal, K > 0 is above main diagonal , K < 0 below.
00113 //!   If input is a matrix, MatDiag(input,K) is a column vector formed from the
00114 //!   element of K-th diagonal.
00115 int MatDiag (
00116    MATRIX input,                       //!< Input matrix
00117    int k,                              //!< Diagonal number
00118    MATRIX *output                      //!< Output matrix (or vector) created 
00119    );
00120 
00121 //! Compute eigenvectors and eigenvalues of the SYMMETRIC matrix.
00122 //!
00123 //! WARNING: This function computes eigen-stuff for SYMMETRIC matrices ONLY.
00124 //!   Example: correlation /covariation matrix. 
00125 //!   NO CHECKING IS PERFORMED ON INPUT TO MAKE SURE IT'S SYMMETRIC.
00126 int MatEigenvectors (
00127    MATRIX matrix,                      //!< Matrix to use
00128    MATRIX *vectors,                    //!< Matrix returned, that contain eigenvectors as columns
00129    MATRIX *values                      //!< Matrix returned (vector), that contains eigenvalues
00130    );
00131 
00132 //! Get a "condition number" of the matrix.
00133 //!
00134 //! @return 0.0 instead of INFINITY just to simplify handling of the result.
00135 //!
00136 //! EXPLANATION:
00137 //!   The condition number of a matrix is defined as the ratio of the largest
00138 //!   (in magnitude) of the elements of matrix W to the smallest one.
00139 //!   Where W is one of the results of SVD defined as:
00140 //!                      T
00141 //!         A = U * W * V  (see MatSVD function).              
00142 //!   A matrix is singular if it's condition number approaches infinity or
00143 //!   just very large. 
00144 double MatGetConditionNumber (
00145    MATRIX matrix
00146    );
00147 
00148 //! Retrieve value from matrix with range check.
00149 //!
00150 //! @return Value in matrix. 
00151 //! This function will return 0.0 for values outside the matrix.  It also handles symmetric matrices.
00152 double MatGetValueTest (
00153    MATRIX matrix,                      //!< Matrix to retrieve value from
00154    UINT16 row,                         //!< Row in matrix (0 - numrows-1)
00155    UINT16 col                          //!< Column in matrix (0 - numcols-1)
00156    );
00157 
00158 //! Compute inverse of a square matrix.
00159 int MatInverse (
00160    MATRIX outmatrix,                   //!< Inverse matrix computed
00161    MATRIX inmatrix,                    //!< Matrix to compute inverse of
00162    int maxiterations,                  //!< Maximum iterations for convergence (0 for default)
00163    double maxerror                     //!< Maximum error, (0.0 for default)
00164    );
00165 
00166 //! Determine if matrix has at least one non-zero on diagonal and zeros elsewhere.
00167 int MatIsDiagonal (
00168    MATRIX matrix,                      //!< Matrix to use
00169    double tolerance                    //!< Define minimal value, everything less than tolerance will be treated as zero.
00170    );
00171 
00172 //! Determine if matrix A equals to matrix B.
00173 //!
00174 //! This function checks if 2 matrices have similar size and all 
00175 //!   corresponding values are equal within tolerance.
00176 int MatIsEqual (
00177    MATRIX A,                           //!< Matrix to use
00178    MATRIX B,                           //!< Matrix to use
00179    double tolerance                    //!< Define minimal value, everything less than tolerance will be treated as zero.
00180    );
00181 
00182 //! Determine if matrix is singular.
00183 //!
00184 //! NOTE: This function computes SVD, so it's no a fast check!
00185 //!   This function calls MatGetConditionNumber() with some default tolerances.
00186 int MatIsSingular (
00187    MATRIX matrix                       //!< Matrix to use
00188    );
00189 
00190 //! Determine if matrix has all elements equal to 0.0.
00191 int MatIsZero (
00192    MATRIX matrix,                      //!< Matrix to use 
00193    DOUBLE tolerance                    //!< Define minimal value, everything less than tolerance will be treated as zero.
00194    );
00195 
00196 //! Multiply one matrix by another.
00197 int MatMultMatrix (
00198    MATRIX outmatrix,                   //!< Output matrix, may be the same as either input matrix
00199    MATRIX matrixA,                     //!< Left matrix, will use outmatrix if NULL
00200    MATRIX matrixB                      //!< Right matrix, will use outmatrix if NULL
00201    );
00202 
00203 //! Multiply a matrix by a scalar.
00204 void MatMultScalar (
00205    MATRIX matrix,                      //!< Matrix to use
00206    double value                        //!< Value to multiply all matrix elements by
00207    );
00208 
00209 //! Compute orthonormal basis for set of vectors.
00210 //! 
00211 //! Suppose that you have N vectors in an M-dimensional vector space,
00212 //!   with N <= M. Then N vectors span some subspace of the full vector space.
00213 //!   The right way to construct an orthonormal basis for a subspace is by SVD.
00214 //!   Form M x N matrix A whose N columns are your vectors. Run the matrix
00215 //!   through MatSVD. The columns of the matrix U are your desired orthonormal basis vectors.
00216 int MatOrthoBasis (
00217    MATRIX *vectors,                    //!< Array of vectors (number of columns = 1)
00218    int numvectors,                     //!< Number of input vectors
00219    MATRIX *basis                       //!< Matrix returned, columns are orthonormal basis vectors
00220    );
00221 
00222 //! Compute pseudoinverse of the matrix using SVD  
00223 //!
00224 //! If Input matrix is not singular pseudoinverse is equal to
00225 //!   normal inverse of the matrix. 
00226 //! EXPLANATION:
00227 //!   Pseudoinverse is such matrix X that satisfy the following condition:
00228 //!      A * X * A = A, if matrix A is singular X still exists.
00229 int MatPseudoInverse (
00230    MATRIX inverse,                     //!< Output pseudoinverse
00231    MATRIX input,                       //!< Input matrix
00232    double tolerance                    //!< Tolerance to use, if in doubt pass 0.0 and function will use built-in defaults
00233    );
00234 
00235 //! Resize a matrix.
00236 //!
00237 //! Newly created elements will be initialized to 0.
00238 int MatResize (
00239    MATRIX matrix,                      //!< Matrix to be resized
00240    UINT16 newrows,                     //!< New number of rows
00241    UINT16 newcols                      //!< New number of columns
00242    );
00243 
00244 //! Determine if two matrices are the same size.
00245 //!
00246 //! @return 1 if matrices have the same number of rows/columns, 0 if not.
00247 int MatSameSize (
00248    MATRIX matrix1, 
00249    MATRIX matrix2
00250    );
00251 
00252 //! Set all entries in a matrix to a specified scalar value.
00253 void MatSetScalar (
00254    MATRIX matrix,                      //!< Matrix to use
00255    double value                        //!< Value to set all matrix elements to
00256    );
00257 
00258 //! Set matrix to a identity.
00259 int MatSetIdentity (
00260    MATRIX matrix                       //!< Matrix to use
00261    );
00262 
00263 //! Compute Singular Value Decomposition (SVD).
00264 //!
00265 //! This function computes SVD as A = U * W * V ;
00266 //!   Output matrix U has the same dimension as input matrix A. 
00267 //!   The diagonal matrix of singular values W is output as a vector.
00268 //!   The matrix V (not transpose of V) is output as a matrix.
00269 int MatSVD (
00270    MATRIX A,                           //!< Input matrix
00271    MATRIX *U,                          //!< Returned
00272    MATRIX *W,                          //!< Returned
00273    MATRIX *V                           //!< Returned
00274    );
00275 
00276 //! Stable Solution of system of linear equation using SVD algorithm.
00277 //!
00278 //! A * X = B where A(M x N) matrix and X (N) vector. B(M) is also a vector.
00279 //!   X is the output solution vector.
00280 //!   Matrix A is decomposed into U(M x N), W(N) and V(N x N) matrices using SVD operation.
00281 int MatSVDSolve (
00282    MATRIX A,                           //!< Input matrix of equations
00283    MATRIX B,                           //!< Input vector that contains right side of equations
00284                                        //! NOTE: you can pass ANY type of vector: numrows = 1 or numcols = 1
00285    MATRIX X,                           //!< Solution returned
00286    double tolerance                    //!< Tolerance to use, if in doubt pass 0.0 for defaults
00287    );
00288 
00289 //! 
00290 int MatSVDSolveAutoTolerance (
00291    MATRIX A, 
00292    MATRIX B, 
00293    MATRIX X                            //!< Size of A->cols x 1
00294    );
00295 
00296 //! 
00297 int MatSVDSolveAutoToleranceFast (
00298    MATRIX A, 
00299    MATRIX U, 
00300    MATRIX W, 
00301    MATRIX V, 
00302    MATRIX B, 
00303    MATRIX X                            //!< Size of A->cols x 1
00304    );
00305 
00306 //! Subtract one matrix from another.
00307 int MatSubMatrix (
00308    MATRIX outmatrix,                   //!< Output matrix, may be the same as either input matrix
00309    MATRIX matrixA,                     //!< Matrix to subtract FROM
00310    MATRIX matrixB                      //!< Second to subtract, if NULL will subtract matrixA from outmatrix
00311    );
00312 
00313 //! Compute trace of the matrix: sum of it's diagonal elements.
00314 double MatTrace (
00315    MATRIX matrix                       //!< Matrix to use
00316    );
00317 
00318 //! Transpose a matrix.
00319 int MatTranspose (
00320    MATRIX outmatrix,                   //!< Output matrix (may be the same as the input matrix)
00321    MATRIX inmatrix                     //!< Input matrix
00322    );
00323 
00324 //! Computes forward and inverse principal components transformation.
00325 int RasterLinearCombComputePrinComp (
00326    FNAMEINODEUC *fnameinode,           //!< List of input rasters (must be the same size)
00327    MIUNICODE *maskfilename,               //!< NULL if not needed
00328    INT32 maskinode,                    //!< Mask object inode
00329    INT32 colsample,                    //!< Sampling rates for analysis (1 - default)
00330    INT32 linsample,                    
00331    MATRIX *forward,                    //!< Forward PC transformation
00332    MATRIX *inverse,                    //!< Inverse PC transformation
00333    PRINCOMPPARMS *parm,                //!< See mi32p/princomp.h, NULL if not needed
00334    UINT32 flags                        //!< Flags
00335    );
00336 
00337 //! Computes coefficients of multilinear regression equations.
00338 int RasterLinearCombMultRegression (
00339    FNAMEINODEUC *fnameinode,           //!< List of input rasters (must be the same size) 
00340    MIUNICODE *maskfilename,               //!< NULL if not needed
00341    INT32 maskinode,                    //!< Mask object inode
00342    INT32 colsample,                    //!< Sampling rates for analysis (1 - default)
00343    INT32 linsample,                    
00344    MATRIX *MatMLR,                     //!< Matrix of MLR coefficients returned: 
00345                                        //!      matrix(numrasts,numrasts+1), first column - offsets;
00346    UINT32 flags                        //!< Flags
00347    );
00348 
00349 #if defined(__cplusplus)
00350 }
00351 #endif
00352 
00353 #endif   //!<  #ifdef INC_MI32_MATRIX_H 
00354 

Generated on Thu Apr 26 04:45:02 2007 for TNTsdk by  doxygen 1.5.2