include/it/mat.h

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /*
00021   Matrices functions
00022   Copyright (C) 2005-2007 Vivien Chappelier, Herve Jegou, Francois Cayre
00023 */
00024 
00025 #ifndef __it_mat_h
00026 #define __it_mat_h
00027 
00028 #include <stddef.h>
00029 #include <stdlib.h>
00030 #include <string.h>
00031 
00032 #include <it/types.h>
00033 #include <it/vec.h>
00034 
00035 /*---------------------------------------------------------------------------*/
00036 /*! \defgroup mat Matrices                                                   */
00037 /* @{                                                                        */
00038 /*---------------------------------------------------------------------------*/
00039 
00040 /* A matrix is a vector of row vectors. The pointer corresponding to a
00041    given matrix therefore points to the first row vector ([0]).
00042    M being a matrix, the indexation convention is M[row][col]. 
00043    As the vector length, the number of rows is stored before the components.  */
00044 
00045 typedef Vec *Mat;
00046 typedef vec *mat;
00047 typedef ivec *imat;
00048 typedef bvec *bmat;
00049 typedef cvec *cmat;
00050 
00051 
00052 #define Mat_element_size(m) Vec_element_size(m[0])
00053 
00054 /*---------------------------------------------------------------------------*/
00055 /** @name Basic vector allocation, initialization and deletion               */
00056 /*---------------------------------------------------------------------------*/
00057 
00058 Mat __Mat_new_alloc (size_t elem_size, idx_t h, idx_t w,
00059            idx_t hmax, idx_t wmax);
00060 
00061 /* Allocate a matrix with explicit allocation of the maximum height and width */
00062 #define Mat_new_alloc( type_t, h, w, hmax, wmax ) \
00063           ((type_t **) __Mat_new_alloc( sizeof(type_t), h, w, hmax, wmax ))
00064 mat mat_new_alloc (idx_t h, idx_t w, idx_t hmax, idx_t wmax);
00065 imat imat_new_alloc (idx_t h, idx_t w, idx_t hmax, idx_t wmax);
00066 bmat bmat_new_alloc (idx_t h, idx_t w, idx_t hmax, idx_t wmax);
00067 cmat cmat_new_alloc (idx_t h, idx_t w, idx_t hmax, idx_t wmax);
00068 
00069 
00070 Mat __Mat_new (size_t elem_size, idx_t h, idx_t w);
00071 
00072 /* Allocate a matrix. This function do not return the matrix pointer.
00073    For this purpose, use specialized matrix instead                          */
00074 #define Mat_new( type_t, h, w ) ((type_t **) __Mat_new( sizeof(type_t), h, w ))
00075 mat mat_new (idx_t h, idx_t w);
00076 imat imat_new (idx_t h, idx_t w);
00077 bmat bmat_new (idx_t h, idx_t w);
00078 cmat cmat_new (idx_t h, idx_t w);
00079 
00080 
00081 
00082 /* Free the vector */
00083 void __Mat_delete (Mat m);
00084 
00085 #define Mat_delete( m ) __Mat_delete((Mat) m)
00086 void mat_delete (mat m);
00087 void imat_delete (imat m);
00088 void bmat_delete (bmat m);
00089 void cmat_delete (cmat m);
00090 
00091 
00092 /*---------------------------------------------------------------------------*/
00093 /** @name Width and Height functions                                         */
00094 /*---------------------------------------------------------------------------*/
00095 
00096 /* Return the length of the vector and the number of allocated elements */
00097 #define Mat_height(m)       Vec_length(m)
00098 idx_t mat_height (mat m);
00099 idx_t imat_height (imat m);
00100 idx_t bmat_height (bmat m);
00101 idx_t cmat_height (cmat m);
00102 
00103 
00104 #define Mat_height_max(m)   Vec_length_max(m)
00105 idx_t mat_height_max (mat m);
00106 idx_t imat_height_max (imat m);
00107 idx_t bmat_height_max (bmat m);
00108 idx_t cmat_height_max (cmat m);
00109 
00110 
00111 #define Mat_width(m)        Vec_length(m[0])
00112 idx_t mat_width (mat m);
00113 idx_t imat_width (imat m);
00114 idx_t bmat_width (bmat m);
00115 idx_t cmat_width (cmat m);
00116 
00117 
00118 #define Mat_width_max(m)    Vec_length_max(m[0])
00119 idx_t mat_width_max (mat m);
00120 idx_t imat_width_max (imat m);
00121 idx_t bmat_width_max (bmat m);
00122 idx_t cmat_width_max (cmat m);
00123 
00124 
00125 
00126 #define Mat_set_height_max(m, hmax) do {                                    \
00127     idx_t i, oldhmax = Mat_height_max(m), nhmax;            \
00128     assert( hmax >= Vec_length(m) );                                          \
00129     nhmax = ( hmax > 0 ? hmax : 1 );                          \
00130     if( nhmax > Mat_height(m) ) {                                             \
00131       Vec_set_length_max((m),nhmax);                      \
00132       for( i = oldhmax ; i < nhmax ; i++ ) {                                  \
00133         void ** pm = (void **) &(m)[i]; /* keep both C/C++ compilers happy */ \
00134         *pm = __Vec_new(Mat_element_size(m), Mat_width(m) );                  \
00135       }                                                                       \
00136     }                                                                         \
00137   } while(0)
00138 
00139 mat _mat_set_height_max (mat m, idx_t hmax);
00140 imat _imat_set_height_max (imat m, idx_t hmax);
00141 bmat _bmat_set_height_max (bmat m, idx_t hmax);
00142 cmat _cmat_set_height_max (cmat m, idx_t hmax);
00143 
00144 #define mat_set_height_max( m, h ) do { m = _mat_set_height_max(m, h); } while(0)
00145 #define imat_set_height_max( m, h ) do { m = _imat_set_height_max(m, h); } while(0)
00146 #define bmat_set_height_max( m, h ) do { m = _bmat_set_height_max(m, h); } while(0)
00147 #define cmat_set_height_max( m, h ) do { m = _cmat_set_height_max(m, h); } while(0)
00148 
00149 
00150 /* Modify the height of the matrix (but never reduce the allocated memory)      */
00151 #define Mat_set_height( m, h )     do {                                       \
00152                                       if( h > Mat_height( m ) ) {             \
00153                                         Mat_set_height_max( m, h );           \
00154                                         Vec_length(m) = (h);                  \
00155                                       }                                       \
00156                                       else                                    \
00157                                         Vec_length(m) = (h);                  \
00158                                    } while(0)
00159 
00160 mat _mat_set_height (mat m, idx_t h);
00161 imat _imat_set_height (imat m, idx_t h);
00162 bmat _bmat_set_height (bmat m, idx_t h);
00163 cmat _cmat_set_height (cmat m, idx_t h);
00164 
00165 #define mat_set_height( m, h ) do { m = _mat_set_height(m, h); } while(0)
00166 #define imat_set_height( m, h ) do { m = _imat_set_height(m, h); } while(0)
00167 #define bmat_set_height( m, h ) do { m = _bmat_set_height(m, h); } while(0)
00168 #define cmat_set_height( m, h ) do { m = _cmat_set_height(m, h); } while(0)
00169 
00170 
00171 /* to use the entity end in a mat function, this macro is called */
00172 #define MAT_END_COL_PARAM( v, param ) do { if( param == end ) param = Mat_width( v ) - 1; } while(0)
00173 #define MAT_END_ROW_PARAM( v, param ) do { if( param == end ) param = Mat_height( v ) - 1; } while(0)
00174 
00175 
00176 /*---------------------------------------------------------------------------*/
00177 /** @name Basic matrix initialization/modification                           */
00178 /*---------------------------------------------------------------------------*/
00179 
00180 mat _mat_init (mat m, double *buf, idx_t w, idx_t h);
00181 imat _imat_init (imat m, int *buf, idx_t w, idx_t h);
00182 bmat _bmat_init (bmat m, byte * buf, idx_t w, idx_t h);
00183 cmat _cmat_init (cmat m, cplx * buf, idx_t w, idx_t h);
00184 
00185 /* Initialize the matrix from a buffer of the same type */
00186 #define Mat_init( m, buf, w, h ) do {                                         \
00187                                     idx_t i;                                  \
00188                                     Mat_set_height(m, h);                     \
00189                                     for( i=0 ; i < h ; i++ )                  \
00190                                       Vec_init( m[i], buf+(w)*(i), w );       \
00191                                  } while(0)
00192 
00193 #define mat_init(m, buf, w, h) _mat_init(m, buf, w, h)
00194 #define imat_init(m, buf, w, h) _imat_init(m, buf, w, h)
00195 #define bmat_init(m, buf, w, h) _bmat_init(m, buf, w, h)
00196 #define cmat_init(m, buf, w, h) _cmat_init(m, buf, w, h)
00197 
00198 
00199 /* Set all the elements of the matrix to value val */
00200 #define Mat_set( m, val ) do {                                                \
00201                               idx_t i,j;                                      \
00202                               assert( m );                                    \
00203                               for( i = 0 ; i < Mat_height(m) ; i++ )          \
00204                                 for( j = 0 ; j < Mat_width(m) ; j++ )         \
00205                                   m[i][j] = val;                              \
00206                              } while(0)
00207 
00208 mat mat_set (mat m, double val);
00209 imat imat_set (imat m, int val);
00210 bmat bmat_set (bmat m, byte val);
00211 cmat cmat_set (cmat m, cplx val);
00212 
00213 mat  mat_new_set (double val, idx_t h, idx_t w);
00214 imat imat_new_set (int val, idx_t h, idx_t w);
00215 bmat bmat_new_set (byte val, idx_t h, idx_t w);
00216 cmat cmat_new_set (cplx val, idx_t h, idx_t w);
00217 
00218 
00219 /* Set some elements of the matrix to value val                              */
00220 #define Mat_set_between( m, r1, c1, r2, c2, val ) do {                        \
00221                             idx_t ce = c2;                                    \
00222                             idx_t re = r2;                                    \
00223                             idx_t x, y;                                       \
00224                             assert( m );                                      \
00225                             MAT_END_COL_PARAM( m, ce );                       \
00226                             MAT_END_ROW_PARAM( m, re );                       \
00227                             for( y = r1 ; y <= re ; y++ )                     \
00228                               for( x = c1 ; x <= ce ; x++ )                   \
00229                                 m[ y ][ x ] = val;                            \
00230 } while( 0 )
00231 
00232 mat mat_set_between (mat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2, double val);
00233 imat imat_set_between (imat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2, int val);
00234 bmat bmat_set_between (bmat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2, byte val);
00235 cmat cmat_set_between (cmat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2, cplx val);
00236 
00237 void mat_void (mat m);
00238 void imat_void (imat m);
00239 void bmat_void (bmat m);
00240 void cmat_void (cmat m);
00241 
00242 mat  mat_new_void ();
00243 imat imat_new_void ();
00244 bmat bmat_new_void ();
00245 cmat cmat_new_void ();
00246 
00247 void mat_zeros (mat m);
00248 void imat_zeros (imat m);
00249 void bmat_zeros (bmat m);
00250 void cmat_zeros (cmat m);
00251 
00252 mat mat_new_zeros (idx_t h, idx_t w);
00253 imat imat_new_zeros (idx_t h, idx_t w);
00254 bmat bmat_new_zeros (idx_t h, idx_t w);
00255 cmat cmat_new_zeros (idx_t h, idx_t w);
00256 
00257 /* Set a matrix to identity. This function also works if the matrix is not square */
00258 void mat_eye (mat m);
00259 void imat_eye (imat m);
00260 void bmat_eye (bmat m);
00261 void cmat_eye (cmat m);
00262 
00263 mat  mat_new_eye (idx_t n);
00264 imat imat_new_eye (idx_t n);
00265 bmat bmat_new_eye (idx_t n);
00266 cmat cmat_new_eye (idx_t n);
00267 
00268 void mat_ones (mat m);
00269 void imat_ones (imat m);
00270 void bmat_ones (bmat m);
00271 void cmat_ones (cmat m);
00272 
00273 mat mat_new_ones (idx_t h, idx_t w);
00274 imat imat_new_ones (idx_t h, idx_t w);
00275 bmat bmat_new_ones (idx_t h, idx_t w);
00276 cmat cmat_new_ones (idx_t h, idx_t w);
00277 
00278 mat  mat_new_diag (vec v);
00279 imat imat_new_diag (ivec v);
00280 bmat bmat_new_diag (bvec v);
00281 
00282 
00283 /*------------------------------------------------------------------------------*/
00284 /** @name Copy and Conversions Functions                                        */
00285 /*------------------------------------------------------------------------------*/
00286 
00287 #define Mat_copy( m1, m2 ) __Mat_copy( (Mat) m1, (Mat) m2 )
00288 Mat __Mat_copy (Mat m1, Mat m2);
00289 
00290 void mat_copy (mat dest, mat orig);
00291 void imat_copy (imat dest, imat orig);
00292 void bmat_copy (bmat dest, bmat orig);
00293 void cmat_copy (cmat dest, cmat orig);
00294 
00295 Mat  Mat_clone (Mat m);
00296 mat  mat_clone (mat m);
00297 imat imat_clone (imat m);
00298 bmat bmat_clone (bmat m);
00299 cmat cmat_clone (cmat m);
00300 
00301 
00302 /* Vectorize the matrix into a vector                               */
00303 vec  mat_to_vec (mat m);
00304 ivec imat_to_ivec (imat m);
00305 bvec bmat_to_bvec (bmat m);
00306 cvec cmat_to_cvec (cmat m);
00307 
00308 /* Transform a vector into a matrix, assuming a given matrix width  */
00309 mat  vec_to_mat (vec v, idx_t width);
00310 imat ivec_to_imat (ivec v, idx_t width);
00311 bmat bvec_to_bmat (bvec v, idx_t width);
00312 cmat cvec_to_cmat (cvec v, idx_t width);
00313 
00314 mat  imat_to_mat (imat m);
00315 mat  bmat_to_mat (bmat m);
00316 imat mat_to_imat (mat m);
00317 imat bmat_to_imat (bmat m);
00318 
00319 
00320 /*---------------------------------------------------------------------------*/
00321 /** @name Comparison functions                                               */
00322 /*---------------------------------------------------------------------------*/
00323 
00324 int  Mat_eq (Mat m1, Mat m2);
00325 int  mat_eq (mat m1, mat m2);
00326 int  imat_eq (imat m, imat m2);
00327 int  bmat_eq (bmat m, bmat m2);
00328 int  cmat_eq (cmat m, cmat m2);
00329 
00330 int  mat_is_void (mat m);
00331 int  imat_is_void (imat m);
00332 int  bmat_is_void (bmat m);
00333 int  cmat_is_void (cmat m);
00334 
00335 
00336 /*---------------------------------------------------------------------------*/
00337 /** @name Row/column manipulation                                            */
00338 /*---------------------------------------------------------------------------*/
00339 
00340 
00341 /* Delete a matrix row (without freeing memory)  */
00342 #define Mat_del_row( m, pos ) do {                                          \
00343                                idx_t h = Mat_height(m)-1;                   \
00344                                idx_t i;                                     \
00345                                assert(m);                                   \
00346                                assert((pos) <= h);                          \
00347                                for( i = (pos) + 1 ; i <= h ; i++ )          \
00348                                  (m)[ i - 1 ] = (m)[ i ];                   \
00349                                Mat_height(m)--;                             \
00350                              } while( 0 )
00351 
00352 /* Insert a matrix row */
00353 /* The matrix max height is increased according to a geometric allocation   */
00354 #define Mat_ins_row( m, pos, v ) do {                                         \
00355                                idx_t h = Mat_height(m);                       \
00356                                idx_t i;                                       \
00357                                assert(m);                                     \
00358                                assert((pos) <= h);                            \
00359                                if(h) assert(Vec_length(v) == Mat_width(m));   \
00360                                if( h+1 > Mat_height_max(m) )                  \
00361                                  Mat_set_height_max( m, DYN_ALLOC_RULE( h )); \
00362                                for( i = h ; i > (pos) ; i-- )                 \
00363                                  (m)[i] = (m)[ i-1 ];                         \
00364                                (m)[(pos)] = (v);                              \
00365                                Mat_height(m)++;                               \
00366                              } while( 0 )
00367 
00368 /* Add a vector at the end row of the matrix (Use matrix as a vector stack)  */
00369 #define Mat_push_row( m, v ) Mat_ins_row( m, Mat_height(m), v )
00370 
00371 /* Retrieve the last element at the end of the vector (Use vector as a stack)*/
00372 #define Mat_pop_row( m ) Mat_del_row( m, Mat_height(m)-1 )
00373 
00374 /* Return the last element of vector                                         */
00375 #define Mat_last_row( m ) ((m)[Mat_height(m)-1])
00376 
00377 
00378 /* Remove a row from a matrix */
00379 mat __mat_del_row (mat m, idx_t pos);
00380 imat __imat_del_row (imat m, idx_t pos);
00381 bmat __bmat_del_row (bmat m, idx_t pos);
00382 cmat __cmat_del_row (cmat m, idx_t pos);
00383 
00384 #define mat_del_row( m, pos )  do { m = __mat_del_row( m, pos ); } while (0)
00385 #define imat_del_row( m, pos ) do { m = __imat_del_row( m, pos ); } while (0)
00386 #define bmat_del_row( m, pos ) do { m = __bmat_del_row( m, pos ); } while (0)
00387 #define cmat_del_row( m, pos ) do { m = __cmat_del_row( m, pos ); } while (0)
00388 
00389 /* Insert a row */
00390 
00391 mat __mat_ins_row (mat m, idx_t pos, vec v);
00392 imat __imat_ins_row (imat m, idx_t pos, ivec v);
00393 bmat __bmat_ins_row (bmat m, idx_t pos, bvec v);
00394 cmat __cmat_ins_row (cmat m, idx_t pos, cvec v);
00395 
00396 #define mat_ins_row( m, pos, v )  do { m = __mat_ins_row( m, pos, v ); } while (0)
00397 #define imat_ins_row( m, pos, v ) do { m = __imat_ins_row( m, pos, v ); } while (0)
00398 #define bmat_ins_row( m, pos, v ) do { m = __bmat_ins_row( m, pos, v ); } while (0)
00399 #define cmat_ins_row( m, pos, v ) do { m = __cmat_ins_row( m, pos, v ); } while (0)
00400 
00401 /* Add a vector at the end row of the matrix (Use matrix as a vector stack)  */
00402 mat __mat_push_row (mat m, vec v);
00403 imat __imat_push_row (imat m, ivec v);
00404 bmat __bmat_push_row (bmat m, bvec v);
00405 cmat __cmat_push_row (cmat m, cvec v);
00406 
00407 #define mat_push_row( m, v )  do { m = __mat_push_row( m, v ); } while (0)
00408 #define imat_push_row( m, v ) do { m = __imat_push_row( m, v ); } while (0)
00409 #define bmat_push_row( m, v ) do { m = __bmat_push_row( m, v ); } while (0)
00410 #define cmat_push_row( m, v ) do { m = __cmat_push_row( m, v ); } while (0)
00411 
00412 
00413 /* Retrieve the last element at the end of the vector (Use vector as a stack)*/
00414 mat __mat_pop_row (mat m);
00415 imat __imat_pop_row (imat m);
00416 bmat __bmat_pop_row (bmat m);
00417 cmat __cmat_pop_row (cmat m);
00418 
00419 #define mat_pop_row( m )  do { m = __mat_pop_row( m ); } while (0)
00420 #define imat_pop_row( m ) do { m = __imat_pop_row( m ); } while (0)
00421 #define bmat_pop_row( m ) do { m = __bmat_pop_row( m ); } while (0)
00422 #define cmat_pop_row( m ) do { m = __cmat_pop_row( m ); } while (0)
00423 
00424 
00425 /* Swap lines or columns */
00426 void mat_swap_rows (mat m, idx_t i, idx_t j);
00427 void mat_swap_cols (mat m, idx_t i, idx_t j);
00428 void imat_swap_rows (imat m, idx_t i, idx_t j);
00429 void imat_swap_cols (imat m, idx_t i, idx_t j);
00430 void bmat_swap_rows (bmat m, idx_t i, idx_t j);
00431 void bmat_swap_cols (bmat m, idx_t i, idx_t j);
00432 void cmat_swap_rows (cmat m, idx_t i, idx_t j);
00433 void cmat_swap_cols (cmat m, idx_t i, idx_t j);
00434 
00435 
00436 /* Transposition of the matrix. Note: the functions _mat_transpose 
00437    and _imat_transpose can be called directly only for symmetric matrices. 
00438    We advise to use only the macros instead (work in the general case). */
00439 mat  _mat_transpose (mat m);
00440 imat _imat_transpose (imat m);
00441 bmat _bmat_transpose (bmat m);
00442 
00443 #define mat_transpose( m ) do {     \
00444   if( mat_height( m ) == mat_width( m ) ) \
00445     m = _mat_transpose( m );      \
00446   else {          \
00447     mat t = mat_new_transpose( m );   \
00448     mat_delete( m );        \
00449     m = t;          \
00450   } } while(0)
00451 
00452 #define imat_transpose( m ) do {    \
00453   if( imat_height( m ) == imat_width( m ) ) \
00454     m = _imat_transpose( m );     \
00455   else {          \
00456     imat t = imat_new_transpose( m );   \
00457     imat_delete( m );       \
00458     m = t;          \
00459   } } while(0)
00460 
00461 #define bmat_transpose( m ) do {    \
00462   if( bmat_height( m ) == bmat_width( m ) ) \
00463     m = _bmat_transpose( m );     \
00464   else {          \
00465     bmat t = bmat_new_transpose( m );   \
00466     bmat_delete( m );       \
00467     m = t;          \
00468   } } while(0)
00469 
00470 
00471 mat  mat_new_transpose (mat m);
00472 imat imat_new_transpose (imat m);
00473 bmat bmat_new_transpose (bmat m);
00474 
00475 
00476 /*------------------------------------------------------------------------------*/
00477 /** @name Mean, median, min, max, sum and elementary statistic indicators       */
00478 /*------------------------------------------------------------------------------*/
00479 
00480 double mat_sum (mat m);
00481 int  imat_sum (imat m);
00482 cplx cmat_sum (cmat m);
00483 
00484 double mat_row_sum (mat m, idx_t c);
00485 int  imat_row_sum (imat m, idx_t c);
00486 cplx cmat_row_sum (cmat m, idx_t c);
00487 
00488 double mat_col_sum (mat m, idx_t c);
00489 int  imat_col_sum (imat m, idx_t c);
00490 cplx cmat_col_sum (cmat m, idx_t c);
00491 
00492 vec  mat_rows_sum (mat m);
00493 ivec imat_rows_sum (imat m);
00494 cvec cmat_rows_sum (cmat m);
00495 
00496 vec  mat_cols_sum (mat m);
00497 ivec imat_cols_sum (imat m);
00498 cvec cmat_cols_sum (cmat m);
00499 
00500 double mat_diag_sum (mat m); 
00501 int imat_diag_sum (imat m);
00502 int bmat_diag_sum (bmat m);
00503 cplx cmat_diag_sum (cmat m);
00504   
00505 
00506 double mat_mean (mat m);
00507 double imat_mean (imat m);
00508 cplx cmat_mean (cmat m);
00509 
00510 double mat_max_index_submatrix (mat m, idx_t rmin, idx_t rmax, idx_t cmin,
00511         idx_t cmax, idx_t * r, idx_t * c);
00512 double mat_min_index_submatrix (mat m, idx_t rmin, idx_t rmax, idx_t cmin,
00513         idx_t cmax, idx_t * r, idx_t * c);
00514 int  imat_max_index_submatrix (imat m, idx_t rmin, idx_t rmax, idx_t cmin,
00515              idx_t cmax, idx_t * r, idx_t * c);
00516 int  imat_min_index_submatrix (imat m, idx_t rmin, idx_t rmax, idx_t cmin,
00517              idx_t cmax, idx_t * r, idx_t * c);
00518 
00519 double mat_max (mat m);
00520 int  imat_max (imat m);
00521 
00522 double mat_min (mat m);
00523 int  imat_min (imat m);
00524 
00525 double mat_max_index (mat m, idx_t * r, idx_t * c);
00526 double mat_max_col_index (mat m, idx_t c, idx_t * r);
00527 double mat_max_row_index (mat m, idx_t r, idx_t * c);
00528 double mat_min_index (mat m, idx_t * r, idx_t * c);
00529 double mat_min_col_index (mat m, idx_t c, idx_t * r);
00530 double mat_min_row_index (mat m, idx_t r, idx_t * c);
00531 
00532 int  imat_max_index (imat m, idx_t * r, idx_t * c);
00533 int  imat_max_col_index (imat m, idx_t c, idx_t * r);
00534 int  imat_max_row_index (imat m, idx_t r, idx_t * c);
00535 int  imat_min_index (imat m, idx_t * r, idx_t * c);
00536 int  imat_min_col_index (imat m, idx_t c, idx_t * r);
00537 int  imat_min_row_index (imat m, idx_t r, idx_t * c);
00538 
00539 double mat_variance (mat m);
00540 void mat_normalize (mat m);
00541 
00542 mat mat_cov (mat m); 
00543 
00544 
00545 /* Column/rows normalization */
00546 void mat_rows_normalize (mat m, double nr);
00547 void mat_cols_normalize (mat m, double nr);
00548 
00549 /* Matrice norms */
00550 double mat_norm_1 (mat m);
00551 double mat_norm_inf (mat m);
00552 
00553 
00554 /*------------------------------------------------------------------------------*/
00555 /** @name Basic arithmetic operations                                           */
00556 /*------------------------------------------------------------------------------*/
00557 
00558 /* Operations with a scalar value                                               */
00559 void mat_incr (mat m, double a);  /* add a to the components              */
00560 void mat_decr (mat m, double a);  /* substract a to the components        */
00561 void mat_mul_by (mat m, double a);  /* multiply the components per a        */
00562 void mat_div_by (mat m, double a);  /* divide the components per a          */
00563 
00564 void imat_incr (imat m, int a); /* add a to the components              */
00565 void imat_decr (imat m, int a); /* substract a to the components        */
00566 void imat_mul_by (imat m, int a); /* multiply the components per a        */
00567 void imat_div_by (imat m, int a); /* divide the components per a          */
00568 
00569 void cmat_incr (cmat m, cplx a);  /* add a to the components              */
00570 void cmat_decr (cmat m, cplx a);  /* substract a to the components        */
00571 void cmat_mul_by (cmat m, cplx a);  /* multiply the components per a        */
00572 void cmat_div_by (cmat m, cplx a);  /* divide the components per a          */
00573 
00574 
00575 /*------------------------------------------------------------------------------*/
00576 /** @name Basic arithmetic operations on rows/columns                           */
00577 /*------------------------------------------------------------------------------*/
00578 
00579 void mat_col_set (mat m, idx_t col, double a);
00580 void mat_col_incr (mat m, idx_t col, double a);
00581 void mat_col_decr (mat m, idx_t col, double a);
00582 void mat_col_mul_by (mat m, idx_t col, double a);
00583 void mat_col_div_by (mat m, idx_t col, double a);
00584 
00585 void imat_col_set (imat m, idx_t col, int a);
00586 void imat_col_incr (imat m, idx_t col, int a);
00587 void imat_col_decr (imat m, idx_t col, int a);
00588 void imat_col_mul_by (imat m, idx_t col, int a);
00589 void imat_col_div_by (imat m, idx_t col, int a);
00590 
00591 void mat_row_incr (mat m, idx_t row, double a);
00592 void mat_row_decr (mat m, idx_t row, double a);
00593 void mat_row_mul_by (mat m, idx_t row, double a);
00594 void mat_row_div_by (mat m, idx_t row, double a);
00595 
00596 void imat_row_incr (imat m, idx_t row, int a);
00597 void imat_row_decr (imat m, idx_t row, int a);
00598 void imat_row_mul_by (imat m, idx_t row, int a);
00599 void imat_row_div_by (imat m, idx_t row, int a);
00600 
00601 
00602 /*------------------------------------------------------------------------------*/
00603 /** @name Component per component operations                                    */
00604 /*------------------------------------------------------------------------------*/
00605 
00606 /* Components per components operations (vectors must be of same size).
00607    The only difference between mat_add and mat_elem_add is that the former 
00608    allocate a new matrix, where in the latter return the address of m1          */
00609 void mat_elem_add (mat m1, mat m2);
00610 void mat_elem_sub (mat m1, mat m2);
00611 void mat_elem_mul (mat m1, mat m2);
00612 void mat_elem_div (mat m1, mat m2);
00613 void mat_elem_sqr (mat m);
00614 void mat_elem_abs (mat m);
00615 void mat_elem_exp (mat m);
00616 void mat_elem_sqrt (mat m);
00617 void mat_elem_pow (mat m, double p);
00618 
00619 void imat_elem_add (imat m1, imat m2);
00620 void imat_elem_sub (imat m1, imat m2);
00621 void imat_elem_mul (imat m1, imat m2);
00622 void imat_elem_div (imat m1, imat m2);
00623 void imat_elem_sqr (imat m);
00624 
00625 void cmat_elem_add (cmat m1, cmat m2);
00626 void cmat_elem_sub (cmat m1, cmat m2);
00627 void cmat_elem_mul (cmat m1, cmat m2);
00628 void cmat_elem_div (cmat m1, cmat m2);
00629 
00630 
00631 /*------------------------------------------------------------------------------*/
00632 /** @name Matrix arithmetic                                                     */
00633 /*------------------------------------------------------------------------------*/
00634 
00635 /* Matrix to matrix, matrix to vector and vector to matrix 
00636    addition, substraction and multiplication                        */
00637 void mat_add (mat m1, mat m2);
00638 void imat_add (imat m1, imat m2);
00639 void cmat_add (cmat m1, cmat m2);
00640 
00641 void mat_sub (mat m1, mat m2);
00642 void imat_sub (imat m1, imat m2);
00643 void cmat_sub (cmat m1, cmat m2);
00644 
00645 mat  mat_new_add (mat m1, mat m2);
00646 imat imat_new_add (imat m1, imat m2);
00647 cmat cmat_new_add (cmat m1, cmat m2);
00648 
00649 mat  mat_new_sub (mat m1, mat m2);
00650 imat imat_new_sub (imat m1, imat m2);
00651 cmat cmat_new_sub (cmat m1, cmat m2);
00652 
00653 mat  mat_new_mul (mat m1, mat m2);
00654 imat imat_new_mul (imat m1, imat m2);
00655 cmat cmat_new_mul (cmat m1, cmat m2);
00656 
00657 /* inplace multiplication. Can multiply with transposition of the operands before */
00658 void mat_mul (mat out, mat m1, mat m2);
00659 void imat_mul (imat out, imat m1, imat m2);
00660 
00661 void mat_mul_transpose_left (mat out, mat m1, mat m2);
00662 void mat_mul_transpose_right (mat out, mat m1, mat m2);
00663 void mat_mul_transpose_leftright (mat out, mat m1, mat m2);
00664 
00665 vec  mat_vec_mul (mat m, vec v);
00666 vec  mat_ivec_mul (mat m, ivec v);
00667 vec  imat_vec_mul (imat m, vec v);
00668 ivec imat_bvec_mul (imat m, bvec v);
00669 ivec imat_ivec_mul (imat m, ivec v);
00670 cvec cmat_vec_mul (cmat m, vec v);
00671 cvec cmat_cvec_mul (cmat m, cvec v);
00672 
00673 vec  vec_mat_mul (vec v, mat m);
00674 ivec ivec_imat_mul (ivec v, imat m);
00675 
00676 
00677 /*------------------------------------------------------------------------------*/
00678 /** @name Submatrices operations                                                */
00679 /*------------------------------------------------------------------------------*/
00680 
00681 mat  mat_get_submatrix (mat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2);
00682 imat imat_get_submatrix (imat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2);
00683 bmat bmat_get_submatrix (bmat m, idx_t r1, idx_t c1, idx_t r2, idx_t c2);
00684 
00685 mat  mat_set_submatrix (mat m, mat s, idx_t r, idx_t c);
00686 imat imat_set_submatrix (imat m, imat s, idx_t r, idx_t c);
00687 bmat bmat_set_submatrix (bmat m, bmat s, idx_t r, idx_t c);
00688 
00689 void mat_copy_row (vec v, mat m, idx_t r);
00690 void imat_copy_row (ivec v, imat m, idx_t r);
00691 void bmat_copy_row (bvec v, bmat m, idx_t r);
00692 void cmat_copy_row (cvec v, cmat m, idx_t r);
00693 
00694 void mat_copy_col (vec v, mat m, idx_t c);
00695 void imat_copy_col (ivec v, imat m, idx_t c);
00696 void bmat_copy_col (bvec v, bmat m, idx_t c);
00697 void cmat_copy_col (cvec v, cmat m, idx_t c);
00698 
00699 vec mat_get_row (mat m, idx_t r);
00700 ivec imat_get_row (imat m, idx_t r);
00701 bvec bmat_get_row (bmat m, idx_t r);
00702 cvec cmat_get_row (cmat m, idx_t r);
00703 
00704 vec mat_get_col (mat m, idx_t c);
00705 ivec imat_get_col (imat m, idx_t c);
00706 bvec bmat_get_col (bmat m, idx_t c);
00707 cvec cmat_get_col (cmat m, idx_t c);
00708 
00709 void Mat_set_col (Mat m, idx_t c, Vec v);
00710 void mat_set_col (mat m, idx_t c, vec v);
00711 void imat_set_col (imat m, idx_t c, ivec v);
00712 void bmat_set_col (bmat m, idx_t c, bvec v);
00713 void cmat_set_col (cmat m, idx_t c, cvec v);
00714 
00715 #define Mat_set_row( m, r, v ) Vec_copy((m)[r], (v))
00716 #define mat_set_row( m, r, v ) vec_copy((m)[r], (v))
00717 #define imat_set_row( m, r, v ) ivec_copy((m)[r], (v))
00718 #define bmat_set_row( m, r, v ) bvec_copy((m)[r], (v))
00719 #define cmat_set_row( m, r, v ) cvec_copy((m)[r], (v))
00720 
00721 vec mat_get_diag (mat m); 
00722 ivec imat_get_diag (imat m);
00723 bvec bmat_get_diag (bmat m);
00724 cvec cmat_get_diag (cmat m);
00725 
00726 /* Set a matrix to diagonal matrix */
00727 void mat_diag (mat m, vec v);
00728 void imat_diag (imat m, ivec v);
00729 void bmat_diag (bmat m, bvec v);
00730 void cmat_diag (cmat m, cvec v);
00731 
00732 /* Set the diagonal only (not the other values) */
00733 void mat_set_diag (mat m, vec v);
00734 void imat_set_diag (imat m, ivec v);
00735 void bmat_set_diag (bmat m, bvec v);
00736 void cmat_set_diag (cmat m, cvec v);
00737 
00738 
00739 /*----------------------------------------------------------------------------*/
00740 /** @name Component per component arbitrar function                           */
00741 /*----------------------------------------------------------------------------*/
00742 
00743 void mat_apply_function (mat v, it_function_t function, it_args_t args);
00744 mat  mat_new_apply_function (mat v, it_function_t function, it_args_t args);
00745 #define mat_eval(v, f, a) mat_apply_function(v, f, a)
00746 #define mat_new_eval(v, f, a) mat_new_apply_function(v, f, a)
00747 
00748 void imat_apply_function (imat v, it_ifunction_t function, it_args_t args);
00749 imat imat_new_apply_function (imat v, it_ifunction_t function,
00750             it_args_t args);
00751 #define imat_eval(v, f, a) imat_apply_function(v, f, a)
00752 #define imat_new_eval(v, f, a) imat_new_apply_function(v, f, a)
00753 
00754 
00755 /*----------------------------------------------------------------------------*/
00756 /** @name Random matrices                                                     */
00757 /*----------------------------------------------------------------------------*/
00758 
00759 void mat_rand (mat m);
00760 void mat_randn (mat m);
00761 mat  mat_new_rand (idx_t h, idx_t w);
00762 mat  mat_new_randn (idx_t h, idx_t w);
00763 
00764 /* @} */
00765 
00766 #endif

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau