Doxygen Source Code Documentation
matrix.h
Go to the documentation of this file.00001 /***************************************************************************** 00002 Major portions of this software are copyrighted by the Medical College 00003 of Wisconsin, 1994-2000, and are released under the Gnu General Public 00004 License, Version 2. See the file README.Copyright for details. 00005 ******************************************************************************/ 00006 00007 /* 00008 This is the header file for matrix.c. 00009 00010 File: matrix.h 00011 Author: B. Douglas Ward 00012 Date: 23 April 1997 00013 00014 Mod: Added routines matrix_file_write and matrix_file_read. 00015 Date: 02 July 1999 00016 00017 Mod: Added routine for calculating square root of matrix. 00018 Date: 30 September 1999 00019 00020 Mod: Added routines matrix_sprint and vector_sprint. 00021 Date: 04 October 1999 00022 00023 Mod: Modified matrix_file_read to use mri_read_ascii routine. 00024 Date: 12 January 2000 00025 00026 Mod: Changed return type of vector_dot from float to double. 00027 Date: 13 April 2000 00028 00029 Mod: Added functions column_to_vector and matrix_extract_rows. 00030 Date: 21 April 2000 00031 00032 Mod: Added functions vector_dotself() and vector_multiply_subtract() -- RWCox. 00033 Date: 28 Dec 2002 00034 00035 Mod: Use one array instead of array of arrays for matrix -- RWCox. 00036 Date: 04 Mar 2005 00037 00038 */ 00039 00040 /*---------------------------------------------------------------------------*/ 00041 /* 00042 Define matrix and vector data structures. 00043 */ 00044 00045 #include "machdep.h" /* 07 Mar 2005: to get DONT_USE_MATRIX_MAT */ 00046 00047 typedef struct matrix 00048 { 00049 int rows; 00050 int cols; 00051 double ** elts; 00052 #ifndef DONT_USE_MATRIX_MAT 00053 double * mat ; /* 04 Mar 2005 */ 00054 #endif 00055 } matrix; 00056 00057 00058 typedef struct vector 00059 { 00060 int dim; 00061 double * elts; 00062 } vector; 00063 00064 /*---------------------------------------------------------------------------*/ 00065 /* 00066 Routine to print an error message and stop. 00067 */ 00068 00069 void matrix_error (char * message); 00070 00071 00072 /*---------------------------------------------------------------------------*/ 00073 /* 00074 Initialize matrix data structure. 00075 */ 00076 00077 void matrix_initialize (matrix * m); 00078 00079 00080 /*---------------------------------------------------------------------------*/ 00081 /* 00082 Destroy matrix data structure by deallocating memory. 00083 */ 00084 00085 void matrix_destroy (matrix * m); 00086 00087 00088 /*---------------------------------------------------------------------------*/ 00089 /* 00090 Create matrix data structure by allocating memory and initializing values. 00091 */ 00092 00093 void matrix_create (int rows, int cols, matrix * m); 00094 00095 00096 /*---------------------------------------------------------------------------*/ 00097 /* 00098 Print contents of matrix m. 00099 */ 00100 00101 void matrix_print (matrix m); 00102 00103 00104 /*---------------------------------------------------------------------------*/ 00105 /* 00106 Print label and contents of matrix m. 00107 */ 00108 00109 void matrix_sprint (char * s, matrix m); 00110 00111 00112 /*---------------------------------------------------------------------------*/ 00113 /* 00114 Print contents of matrix m to specified file. 00115 */ 00116 00117 void matrix_file_write (char * filename, matrix m); 00118 00119 00120 /*---------------------------------------------------------------------------*/ 00121 /* 00122 Manual entry of matrix data. 00123 */ 00124 00125 void matrix_enter (matrix * m); 00126 00127 00128 /*---------------------------------------------------------------------------*/ 00129 /* 00130 Read contents of matrix m from specified file. 00131 If unable to read matrix from file, or matrix has wrong dimensions: 00132 If error_exit flag is set, then print error message and exit. 00133 Otherwise, return null matrix. 00134 */ 00135 00136 void matrix_file_read (char * filename, int rows, int cols, matrix * m, 00137 int error_exit); 00138 00139 00140 /*---------------------------------------------------------------------------*/ 00141 /* 00142 Convert simple array to matrix structure. 00143 */ 00144 00145 void array_to_matrix (int rows, int cols, float ** f, matrix * m); 00146 00147 00148 /*---------------------------------------------------------------------------*/ 00149 /* 00150 Make a copy of the first matrix, return copy as the second matrix. 00151 */ 00152 00153 void matrix_equate (matrix a, matrix * b); 00154 00155 00156 /*---------------------------------------------------------------------------*/ 00157 /* 00158 Extract p columns (specified by list) from matrix a. Result is matrix b. 00159 */ 00160 00161 void matrix_extract (matrix a, int p, int * list, matrix * b); 00162 00163 00164 /*---------------------------------------------------------------------------*/ 00165 /* 00166 Extract p rows (specified by list) from matrix a. Result is matrix b. 00167 */ 00168 00169 void matrix_extract_rows (matrix a, int p, int * list, matrix * b); 00170 00171 00172 /*---------------------------------------------------------------------------*/ 00173 /* 00174 Create n x n identity matrix. 00175 */ 00176 00177 void matrix_identity (int n, matrix * m); 00178 00179 00180 /*---------------------------------------------------------------------------*/ 00181 /* 00182 Add matrix a to matrix b. Result is matrix c. 00183 */ 00184 00185 void matrix_add (matrix a, matrix b, matrix * c); 00186 00187 00188 /*---------------------------------------------------------------------------*/ 00189 /* 00190 Subtract matrix b from matrix a. Result is matrix c. 00191 */ 00192 00193 void matrix_subtract (matrix a, matrix b, matrix * c); 00194 00195 00196 /*---------------------------------------------------------------------------*/ 00197 /* 00198 Multiply matrix a by matrix b. Result is matrix c. 00199 */ 00200 00201 void matrix_multiply (matrix a, matrix b, matrix * c); 00202 00203 00204 /*---------------------------------------------------------------------------*/ 00205 /* 00206 Multiply matrix a by scalar constant k. Result is matrix c. 00207 */ 00208 00209 void matrix_scale (double k, matrix a, matrix * c); 00210 00211 00212 /*---------------------------------------------------------------------------*/ 00213 /* 00214 Take transpose of matrix a. Result is matrix t. 00215 */ 00216 00217 void matrix_transpose (matrix a, matrix * t); 00218 00219 00220 /*---------------------------------------------------------------------------*/ 00221 /* 00222 Use Gaussian elimination to calculate inverse of matrix a. Result is 00223 matrix ainv. 00224 */ 00225 00226 int matrix_inverse (matrix a, matrix * ainv); 00227 00228 int matrix_inverse_dsc (matrix a, matrix * ainv); /* 15 Jul 2004 */ 00229 00230 00231 /*---------------------------------------------------------------------------*/ 00232 /* 00233 Calculate square root of symmetric positive definite matrix a. 00234 Result is matrix s. 00235 */ 00236 00237 int matrix_sqrt (matrix a, matrix * s); 00238 00239 00240 /*---------------------------------------------------------------------------*/ 00241 /* 00242 Initialize vector data structure. 00243 */ 00244 00245 void vector_initialize (vector * v); 00246 00247 00248 /*---------------------------------------------------------------------------*/ 00249 /* 00250 Destroy vector data structure by deallocating memory. 00251 */ 00252 00253 void vector_destroy (vector * v); 00254 00255 00256 /*---------------------------------------------------------------------------*/ 00257 /* 00258 Create vector v by allocating memory and initializing values. 00259 */ 00260 00261 void vector_create (int dim, vector * v); 00262 00263 00264 /*---------------------------------------------------------------------------*/ 00265 /* 00266 Print contents of vector v. 00267 */ 00268 00269 void vector_print (vector v); 00270 00271 00272 /*---------------------------------------------------------------------------*/ 00273 /* 00274 Print label and contents of vector v. 00275 */ 00276 00277 void vector_sprint (char * s, vector v); 00278 00279 00280 /*---------------------------------------------------------------------------*/ 00281 /* 00282 Copy vector a. Result is vector b. 00283 */ 00284 00285 void vector_equate (vector a, vector * b); 00286 00287 00288 /*---------------------------------------------------------------------------*/ 00289 /* 00290 Convert simple array f into vector v. 00291 */ 00292 00293 void array_to_vector (int dim, float * f, vector * v); 00294 00295 00296 /*---------------------------------------------------------------------------*/ 00297 /* 00298 Convert column c of matrix m into vector v. 00299 */ 00300 00301 void column_to_vector (matrix m, int c, vector * v); 00302 00303 00304 /*---------------------------------------------------------------------------*/ 00305 /* 00306 Convert vector v into array f. 00307 */ 00308 00309 void vector_to_array (vector v, float * f); 00310 00311 00312 /*---------------------------------------------------------------------------*/ 00313 /* 00314 Add vector a to vector b. Result is vector c. 00315 */ 00316 00317 void vector_add (vector a, vector b, vector * c); 00318 00319 00320 /*---------------------------------------------------------------------------*/ 00321 /* 00322 Subtract vector b from vector a. Result is vector c. 00323 */ 00324 00325 void vector_subtract (vector a, vector b, vector * c); 00326 00327 00328 /*---------------------------------------------------------------------------*/ 00329 /* 00330 Right multiply matrix a by vector b. Result is vector c. 00331 */ 00332 00333 void vector_multiply (matrix a, vector b, vector * c); 00334 00335 /*---------------------------------------------------------------------------*/ 00336 /* 00337 Right multiply matrix a by vector b, then subtract c. Result is vector d. 00338 Also returns sum of squares of elements of d. 00339 */ 00340 00341 double vector_multiply_subtract (matrix a, vector b, vector c, vector * d) ; 00342 00343 /*---------------------------------------------------------------------------*/ 00344 /* 00345 Calculate dot product of vector a with vector b. 00346 */ 00347 00348 double vector_dot (vector a, vector b); 00349 00350 double vector_dotself (vector a); /* 28 Dec 2002: RWCox */ 00351 00352 /*---------------------------------------------------------------------------*/ 00353 00354 double matrix_norm( matrix a ) ; /* 03 Mar 2003: RWCox */ 00355 00356 int * matrix_check_columns( matrix a , double eps ) ; /* 14 Jul 2004: RWCox */ 00357 00358 double * matrix_singvals( matrix X ) ; /* 14 Jul 2004 */ 00359 00360 void matrix_psinv( matrix X , matrix *XtXinv , matrix *XtXinvXt ) ; /* 19 Jul 2004 */ 00361 00362 double get_matrix_flops(void) ; 00363 double get_matrix_dotlen(void) ;