00001
00002 #include<gslwrap/matrix_double.h>
00003 #include<gslwrap/matrix_#type#.h>
00004 #include<gslwrap/vector_#type#.h>
00005 #include<iomanip.h>
00006
00007 #define type_is#typeext#
00008 #ifdef type_is
00009 #define type_is_double
00010 #endif
00011
00012 namespace gsl
00013 {
00014
00082 matrix#typeext#::matrix#typeext#():m(NULL)
00083 {
00084 }
00085
00086 matrix#typeext#::matrix#typeext#( size_t rows, size_t cols , bool clear)
00087 {
00088 if(clear){m = gsl_matrix#typeext#_calloc( rows, cols );}
00089 else {m = gsl_matrix#typeext#_alloc ( rows, cols );}
00090 }
00091
00092
00093 matrix#typeext#::~matrix#typeext#()
00094 {
00095 if ( m ) {gsl_matrix#typeext#_free( m );}
00096 }
00097
00098
00099
00100
00101
00102
00103 void matrix#typeext#::dimensions( size_t *num_rows, size_t *num_cols ) const
00104 {
00105 *num_rows = m->size1;
00106 *num_cols = m->size2;
00107 }
00108
00109
00110 void matrix#typeext#::set_elements( const #type# & new_value )
00111 {
00112 gsl_matrix#typeext#_set_all( m, new_value );
00113 }
00114
00115 void matrix#typeext#::set_dimensions( size_t new_rows, size_t new_cols )
00116 {
00117
00118 if ( m && (get_rows() != new_rows || get_cols() != new_cols )) {
00119 gsl_matrix#typeext#_free( m );
00120 }
00121
00122
00123 m = gsl_matrix#typeext#_calloc( new_rows, new_cols );
00124 }
00125
00126
00127 void matrix#typeext#::load( const char *filename )
00128 {
00129 FILE * f = fopen( filename, "r" ) ;
00130 matrix temp;
00131 gsl_matrix_fread ( f, temp.m );
00132 fclose (f);
00133 *this = temp;
00134 }
00135
00136 void matrix#typeext#::save( const char *filename ) const
00137 {
00138 FILE * f = fopen( filename, "w" ) ;
00139 matrix temp = *this;
00140 gsl_matrix_fwrite ( f, temp.m );
00141 fclose ( f );
00142 }
00143
00144 ostream& operator<< ( ostream& os, const matrix#typeext# & m )
00145 {
00146 size_t i, j;
00147
00148 os.setf( ios::fixed );
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 for ( i = 0; i < m.get_rows(); i++ ) {
00159 for ( j = 0; j < m.get_cols() - 1; j++ ) {
00160 os << m.get_element( i, j ) << " ";
00161 }
00162 os << m.get_element( i, j ) << endl;
00163 }
00164
00165 return os;
00166 }
00167
00168
00169
00170 void matrix#typeext#::load_binary( const char *filename )
00171 {
00172 ;
00173 }
00174
00175 void matrix#typeext#::save_binary( const char *filename ) const
00176 {
00177 ;
00178 }
00179
00180 bool matrix#typeext#::operator==( const matrix#typeext# &other ) const
00181 {
00182 size_t i, j;
00183
00184
00185 if ( size1() != other.size1() || size2() != other.size2() )
00186 {
00187 return false;
00188 }
00189
00190 for ( i = 0; i < size1(); i++ ) {
00191 for ( j = 0; j < size2(); j++ ) {
00192 if ( this->get_element( i, j ) != other.get_element( i, j ) ) {
00193 return false;
00194 }
00195 }
00196 }
00197
00198 return true;
00199 }
00200
00201 matrix#typeext# matrix#typeext#::operator+( const matrix#typeext# &other ) const
00202 {
00203 matrix#typeext# result(*this);
00204 gsl_matrix#typeext#_add( result.m, other.m );
00205 return result;
00206 }
00207
00208 matrix#typeext# matrix#typeext#::operator+( const #type# &f ) const
00209 {
00210 matrix#typeext# result( *this );
00211 gsl_matrix#typeext#_add_constant( result.m, f );
00212
00213 return( result );
00214 }
00215
00216 matrix#typeext# operator+( const #type# &f, const matrix#typeext# &other )
00217 {
00218 matrix#typeext# result( other );
00219 gsl_matrix#typeext#_add_constant( result.m, f );
00220
00221 return( result );
00222 }
00223
00224 matrix#typeext# &matrix#typeext#::operator+=( const #type# &f )
00225 {
00226 gsl_matrix#typeext#_add_constant( m, f );
00227
00228 return( *this );
00229 }
00230
00231 matrix#typeext# &matrix#typeext#::operator+=( const matrix#typeext# &other )
00232 {
00233 gsl_matrix#typeext#_add( m, other.m );
00234
00235 return( *this );
00236 }
00237
00238 matrix#typeext# matrix#typeext#::operator-( const matrix#typeext# &other ) const
00239 {
00240 matrix#typeext# result( *this );
00241 gsl_matrix#typeext#_sub( result.m, other.m );
00242
00243 return( result );
00244 }
00245
00246 matrix#typeext# matrix#typeext#::operator-( const #type# &f ) const
00247 {
00248 matrix#typeext# result( *this );
00249 gsl_matrix#typeext#_add_constant( result.m, -f );
00250
00251 return( result );
00252 }
00253
00254 matrix#typeext# operator-( const #type# &f, const matrix#typeext# &other )
00255 {
00256 matrix#typeext# result( -1 * other );
00257 gsl_matrix#typeext#_add_constant( result.m, f );
00258
00259 return( result );
00260 }
00261
00262 matrix#typeext# &matrix#typeext#::operator-=( const #type# &f )
00263 {
00264 gsl_matrix#typeext#_add_constant( m, -f );
00265
00266 return( *this );
00267 }
00268
00269 matrix#typeext# &matrix#typeext#::operator-=( const matrix#typeext# &other )
00270 {
00271 gsl_matrix#typeext#_sub( m, other.m );
00272
00273 return( *this );
00274 }
00275
00276
00277 matrix#typeext# matrix#typeext#::operator*( const matrix#typeext# &other ) const
00278 {
00279 matrix result( get_rows(), other.get_cols() );
00280 #ifdef type_is_double
00281 gsl_linalg_matmult(m,other.m,result.m);
00282 #else //type_is_double
00283 matrix a=*this;
00284 matrix b=other;
00285 gsl_linalg_matmult(a.m,b.m,result.m);
00286 #endif //type_is_double
00287 return result ;
00288 }
00289
00290 matrix#typeext# matrix#typeext#::operator*( const #type# &f ) const
00291 {
00292 matrix#typeext# result( *this );
00293 gsl_matrix#typeext#_scale( result.m, f );
00294 return( result );
00295 }
00296
00297 matrix#typeext# operator*( const #type# &f, const matrix#typeext# &other )
00298 {
00299 matrix#typeext# result( other );
00300 gsl_matrix#typeext#_scale( result.m, f );
00301
00302 return( result );
00303 }
00304
00305 matrix#typeext# &matrix#typeext#::operator*=( const #type# &f )
00306 {
00307 gsl_matrix#typeext#_scale( m, f );
00308
00309 return( *this );
00310 }
00311
00312 matrix#typeext# &matrix#typeext#::operator*=( const matrix#typeext# &other )
00313 {
00314 *this=(*this)*other;
00315 return( *this );
00316 }
00317
00318 matrix#typeext# matrix#typeext#::operator/( const #type# &f ) const
00319 {
00320 matrix#typeext# result( *this );
00321
00322 if ( f != 0.0 ) {
00323 gsl_matrix#typeext#_scale( result.m, 1.0 / f );
00324 } else {
00325 cout << "e_r_r_o_r: division by zero." << endl;
00326 return( result );
00327 }
00328
00329 return( result );
00330 }
00331
00332 matrix#typeext# &matrix#typeext#::operator/=( const #type# &f )
00333 {
00334 if ( f != 0.0 ) {
00335 gsl_matrix#typeext#_scale( m, 1.0 / f );
00336 } else {
00337 cout << "e_rr_or: division by zero." << endl;
00338 return( *this );
00339 }
00340
00341 return( *this );
00342 }
00343
00344 matrix#typeext# matrix#typeext#::transpose() const
00345 {
00346 int i, j;
00347 matrix#typeext# result( get_cols(), get_rows() );
00348
00349 for ( i = 0; i < get_rows(); i++ ) {
00350 for ( j = 0; j < get_cols(); j++ ) {
00351 gsl_matrix#typeext#_set( result.m, j, i, gsl_matrix#typeext#_get( m, i, j ) );
00352 }
00353 }
00354
00355 return( result );
00356 }
00357
00358
00359 matrix#typeext# matrix#typeext#::LU_decomp(gsl::permutation *perm,int *psign) const
00360 {
00361 bool retPerm=perm!=NULL;
00362 if(!perm){perm = new permutation();}
00363 int sign;
00364 perm->resize( get_rows() );
00365 matrix result=*this;
00366 gsl_linalg_LU_decomp( result.m, perm->gsldata, &sign );
00367
00368 if(!retPerm){delete perm;}
00369 if(psign){*psign=sign;}
00370
00371 return result;
00372 }
00373
00374 matrix#typeext# matrix#typeext#::LU_invert() const
00375 {
00376 int i, j;
00377 permutation p;
00378 matrix a=*this;
00379 a=a.LU_decomp(&p);
00380 matrix inverse(size1(),size2());
00381 gsl_linalg_LU_invert( a.m, p.gsldata, inverse.m );
00382 return inverse;
00383 }
00384
00385
00386
00387 #type# matrix#typeext#::sum() const
00388 {
00389 int i, j;
00390 #type# sum = 0;
00391
00392 for ( i = 0; i < get_rows(); i++ ) {
00393 for ( j = 0; j < get_cols(); j++ ) {
00394 sum += gsl_matrix#typeext#_get( m, i, j );
00395 }
00396 }
00397
00398 return( sum );
00399 }
00400
00401
00402 double matrix#typeext#::LU_lndet() const
00403 {
00404 matrix a=*this;
00405 a=a.LU_decomp();
00406
00407
00408 return gsl_linalg_LU_lndet( a.m );
00409 }
00410
00411 vector#typeext#_view
00412 matrix#typeext#::row( size_t rowindex )
00413 {
00414 gsl_vector#typeext#_view view=gsl_matrix#typeext#_row(m, rowindex);
00415 return vector#typeext#_view::create_vector_view(view);
00416 }
00417
00418 const
00419 vector#typeext#_view
00420 matrix#typeext#::row( size_t rowindex ) const
00421 {
00422 gsl_vector#typeext#_view view=gsl_matrix#typeext#_row(m, rowindex);
00423 return vector#typeext#_view::create_vector_view(view);
00424 }
00425
00426 vector#typeext#_view
00427 matrix#typeext#::column( size_t colindex )
00428 {
00429 gsl_vector#typeext#_view view=gsl_matrix#typeext#_column(m, colindex);
00430 return vector#typeext#_view::create_vector_view(view);
00431 }
00432
00433 const
00434 vector#typeext#_view
00435 matrix#typeext#::column( size_t colindex ) const
00436 {
00437 gsl_vector#typeext#_view view=gsl_matrix#typeext#_column(m, colindex);
00438 return vector#typeext#_view::create_vector_view(view);
00439 }
00440
00442 matrix#typeext# matrix#typeext#::get_row( size_t rowindex ) const
00443 {
00444 matrix#typeext# rowmatrix( 1, get_cols() );
00445 gsl_vector#typeext# *tempvector = gsl_vector#typeext#_calloc( get_cols() );
00446
00447 if ( rowindex < 0 || rowindex >= get_rows() )
00448 {
00449 cerr << "row index must be in range 0 to " << get_rows() - 1 << endl;
00450 exit( 1 );
00451 }
00452
00453 gsl_matrix#typeext#_get_row( tempvector, m, rowindex );
00454 gsl_matrix#typeext#_set_row( rowmatrix.m, 0, tempvector );
00455
00456
00457 gsl_vector#typeext#_free( tempvector );
00458
00459 return( rowmatrix );
00460 }
00461
00463 matrix#typeext# matrix#typeext#::get_col( size_t colindex ) const
00464 {
00465 matrix#typeext# columnmatrix( get_rows(), 1 );
00466 gsl_vector#typeext# *tempvector = gsl_vector#typeext#_calloc( get_rows() );
00467
00468 if ( colindex < 0 || colindex >= get_cols() )
00469 {
00470 cerr << "column index must be in range 0 to " << get_cols() - 1 << endl;
00471 exit( 1 );
00472 }
00473
00474 gsl_matrix#typeext#_get_col( tempvector, m, colindex );
00475 gsl_matrix#typeext#_set_col( columnmatrix.m, 0, tempvector );
00476 for ( int i = 0; i < get_rows(); i++ )
00477 cout << gsl_vector#typeext#_get( tempvector, i ) << endl;
00478
00479
00480 gsl_vector#typeext#_free( tempvector );
00481
00482 return( columnmatrix );
00483 }
00484
00486 matrix#typeext# matrix#typeext#::row_sum() const
00487 {
00488 int i;
00489 matrix#typeext# sum( get_rows(), 1 );
00490
00491 sum.set_zero();
00492 for ( i = 0; i < get_rows(); i++ ) {
00493 sum.set_element( i, 0, get_row( i ).sum() );
00494 }
00495
00496 return( sum );
00497 }
00498
00500 matrix#typeext# matrix#typeext#::column_sum() const
00501 {
00502 int i;
00503 matrix#typeext# sum( 1, get_cols() );
00504
00505 sum.set_zero( );
00506 for ( i = 0; i < get_cols(); i++ ) {
00507 sum.set_element( 0, i, get_col( i ).sum() );
00508 }
00509
00510 return( sum );
00511 }
00512
00514 double matrix#typeext#::trace() const
00515 {
00516 int i;
00517 double sum = 0.0;
00518
00519 if ( get_rows() != get_cols() ) {
00520 cerr << "e_r_r_o_r: cannot calculate trace of non-square matrix.";
00521 cerr << endl;
00522 exit( 1 );
00523 }
00524
00525
00526 for ( i = 0; i < get_rows(); i++ ) {
00527 sum += get_element( i, i );
00528 }
00529
00530 return( sum );
00531 }
00532
00534
00535 int matrix#typeext#::cholesky_decomp( matrix#typeext# &a ) const
00536 {
00537 int i, j;
00538 int error;
00539 matrix result=*this;
00540
00541 error = gsl_linalg_cholesky_decomp( result.m );
00542 a=result;
00543 return error;
00544 }
00545
00547 void matrix#typeext#::identity( size_t k )
00548 {
00549 set_dimensions( k, k );
00550 set_zero();
00551 set_diagonal( 1 );
00552 }
00553
00555 void matrix#typeext#::set_diagonal( #type# f )
00556 {
00557 size_t i;
00558 int mn=(get_rows()<get_cols() ? get_rows() : get_cols());
00559 for ( i = 0; i < mn; i++ )
00560 set_element( i, i, f );
00561 }
00562
00564 bool matrix#typeext#::is_square() const
00565 {
00566 if ( get_rows() == get_cols() )
00567 return true;
00568 return false;
00569 }
00570
00572 double matrix#typeext#::norm( double n ) const
00573 {
00574 int i, j;
00575 double sum = 0.0;
00576
00577 for ( i = 0; i < get_rows(); i++ ) {
00578 for ( j = 0; j < get_cols(); j++ ) {
00579 sum += pow( get_element( i, j ), n );
00580 }
00581 }
00582
00583 return sum;
00584 }
00585
00586 }