gsl_vector_matrix_ops.h

Go to the documentation of this file.
00001 
00002 
00003 //
00013 
00014 //--------------------------------------------------------------------------
00015 //--------------------------------------------------------------------------
00016 //  
00017 //  libMoM Library - a library for stochastic simulations in engineering.
00018 // 
00019 //  Copyright (C) 2010 Rochan R. Upadhyay
00020 // 
00021 //  This library is free software; you can redistribute it and/or
00022 //  modify it under the terms of the Version 2.1 GNU Lesser General
00023 //  Public License as published by the Free Software Foundation.
00024 // 
00025 //  This library is distributed in the hope that it will be useful,
00026 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00028 //  Lesser General Public License for more details.
00029 // 
00030 //  You should have received a copy of the GNU Lesser General Public
00031 //  License along with this library; if not, write to the Free Software
00032 //  Foundation, Inc. 51 Franklin Street, Fifth Floor, 
00033 //  Boston, MA  02110-1301  USA
00034 // 
00035 //--------------------------------------------------------------------------
00036 
00037 // Some functions are taken from the gslwrap project whose Copyright notice is below.
00038 
00039 //  This matrix class is a C++ wrapper for the GNU Scientific Library
00040 //  Copyright (C)  ULP-IPB Strasbourg
00041 
00042 //  This program is free software; you can redistribute it and/or modify
00043 //  it under the terms of the GNU General Public License as published by
00044 //  the Free Software Foundation; either version 2 of the License, or
00045 //  (at your option) any later version.
00046 
00047 //  This program is distributed in the hope that it will be useful,
00048 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00049 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00050 //  GNU General Public License for more details.
00051 
00052 //  You should have received a copy of the GNU General Public License
00053 //  along with this program; if not, write to the Free Software
00054 //  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00055 
00056 
00057 #ifndef _GSL_VECTOR_MATRIX_OPS_H
00058 #define _GSL_VECTOR_MATRIX_OPS_H
00059 
00060 #include <gsl/gsl_linalg.h>
00061 #include <gsl/gsl_blas.h>
00062 #include <gsl/gsl_eigen.h>
00063 #include "gsl_vector_double.h"
00064 #include "gsl_vector_integer.h"
00065 #include "gsl_matrix_double.h"
00066 #include "gsl_matrix_integer.h"
00067 #include "gsl_permutation.h"
00068 #include <glpk.h>
00069 
00071 //
00077 //
00078 
00079 inline 
00080 vector operator*(const matrix& m, const vector& v)
00081 {
00082         vector y(m.get_rows());
00083         gsl_blas_dgemv(CblasNoTrans, 1.0, m.gslobj(), v.gslobj(), 0.0, y.gslobj());
00084         return y;
00085 }
00086 
00088 //
00095 //
00096 
00097 inline
00098 vector LU_solve (const matrix &m, const vector &v)
00099 {
00100    permutation p;
00101    vector y(m.get_cols());
00102    matrix m_lud(m.get_rows(), m.get_cols());
00103    m_lud = m.LU_decomp(&p);
00104    gsl_linalg_LU_solve(m_lud.gslobj(), p.gslperm_obj(), v.gslobj(), y.gslobj()); 
00105    return y;
00106 }
00107 
00109 //
00115 //
00116 
00117 inline
00118 vector singular_values (const matrix &m)
00119 {
00120    vector s(m.get_cols());
00121    vector work(m.get_cols());
00122    matrix v(m.get_cols(),m.get_cols());
00123    matrix a(m.get_rows(),m.get_cols());
00124    a=m;
00125    gsl_linalg_SV_decomp(a.gslobj(), v.gslobj(), s.gslobj(), work.gslobj());
00126    return s;
00127 }
00128 
00130 //
00138 //
00139 
00140 inline
00141 double condition_number (const matrix &m)
00142 {
00143    vector s(m.get_cols());
00144    double smax,smin,cond_number;
00145    s = singular_values(m);
00146    smax = s.max();
00147    smin = s.min();
00148    if (smin > 0 || smin < 0) {
00149    cond_number = smax/smin;
00150    return cond_number;}
00151    else return 1.e300; 
00152 }
00153 
00155 //
00162 //
00163 
00164 inline
00165 matrix_int addrow(const size_t &irow, const size_t &itot, const vector_int &v)
00166 {
00167    matrix_int y(itot, v.size());  
00168    for(size_t j = 0; j < v.size(); j++ ) {
00169       y.set_element( irow, j, v.get(j) );}
00170    return y;
00171 }
00172 
00174 //
00181 //
00182 
00183 inline
00184 matrix addrow(const size_t &irow, const size_t &itot, const vector &v)
00185 {
00186    matrix y(itot, v.size());  
00187    for(size_t j = 0; j < v.size(); j++ ) {
00188       y.set_element( irow, j, v.get(j) );}
00189    return y;
00190 }
00191 
00193 //
00199 //
00200 
00201 inline
00202 matrix extract_cols(const matrix &m, const vector_int &v)
00203 {
00204   size_t num_rows = m.get_rows();
00205   size_t num_cols = v.size();
00206   matrix y(num_rows,num_cols);
00207   for(size_t i = 0; i < num_rows; i++){
00208     for(size_t j = 0; j < num_cols; j++){
00209       y.set_element(i,j,m.get_element(i,v.get(j)));}}
00210   return y;
00211 }  
00212 
00214 //
00223 //
00224 
00225 inline
00226 matrix tridiag_eigen_solve (const vector &v1, const vector &v2)
00227 {
00228   size_t msize = v1.size();
00229   matrix m( msize, msize );
00230   vector eval( msize );
00231   matrix evec( msize, msize );
00232   matrix eigvalvec( msize+1, msize);
00233   vector colvec;
00234   m.set_tridiag(v1, v2, v2);
00235   gsl_eigen_symmv_workspace * w = gsl_eigen_symmv_alloc ( msize );
00236   gsl_eigen_symmv ( m.gslobj(), eval.gslobj(), evec.gslobj(), w );   
00237   gsl_eigen_symmv_free ( w );
00238   gsl_eigen_symmv_sort ( eval.gslobj(), evec.gslobj(), GSL_EIGEN_SORT_ABS_ASC );
00239   eigvalvec = addrow(0, msize+1, eval);
00240   for(size_t i=0; i < msize; i++){
00241     colvec = evec.row(i);
00242     eigvalvec += addrow(i+1, msize+1, colvec);}
00243   return ( eigvalvec );
00244 }
00245 
00247 //
00253 //
00254 
00255 inline
00256 vector subvector_elements (const vector &v, const vector_int &index)
00257 {
00258   vector y(index.size()); 
00259   for (size_t i = 0; i < index.size(); i++){
00260     y.set(i,v.get(index.get(i)));}
00261   return y;  
00262 } 
00263 
00265 //
00272 //
00273 
00274 inline
00275 vector simplex_method (const matrix &A, const vector &b, const vector &c)
00276 {
00277   size_t m = A.get_rows();
00278   size_t n = A.get_cols();
00279   vector xsimp(n);
00280   int* ia = NULL;
00281   int *ja = NULL;
00282   double *ar = NULL;
00283   ia = new int[1+m*n];
00284   ja = new int[1+m*n];
00285   ar = new double[1+m*n];
00286   for (int ir=0; ir<m*n+1; ir++) {
00287     ia[ir] = 0;
00288     ja[ir] = 0;
00289     ar[ir] = 0.0;}
00290   glp_prob *lp;
00291   glp_smcp parm;
00292   lp = glp_create_prob();
00293   glp_init_smcp(&parm);
00294   parm.msg_lev = GLP_MSG_ERR;
00295   parm.it_lim = 50000;
00296   parm.meth = GLP_DUALP;
00297   parm.tol_bnd = 1.0e-7;
00298   parm.tol_dj = 1.0e-7;
00299   glp_set_obj_dir(lp, GLP_MIN);
00300   glp_add_rows(lp, m);
00301   glp_add_cols(lp, n);
00302   for (size_t ir = 1; ir<m+1; ir++){
00303     glp_set_row_bnds(lp, ir, GLP_FX, b.get(ir-1), b.get(ir-1));}
00304   for (size_t ic = 1; ic<n+1; ic++){
00305      glp_set_col_bnds(lp, ic, GLP_LO, 0.0, 0.0);
00306      glp_set_obj_coef(lp, ic, c.get(ic-1));}
00307   size_t ircnt = 1;
00308   for (size_t ira = 1; ira<m+1; ira++){
00309     for (size_t jca = 1; jca<n+1; jca++){
00310        ia[ircnt]=ira;
00311        ja[ircnt]=jca;
00312        ar[ircnt]=A.get_element(ira-1,jca-1);
00313        ircnt++;}}
00314   glp_load_matrix(lp, m*n, ia, ja, ar);
00315   glp_simplex(lp, &parm);
00316   for (size_t ic = 1; ic<n+1; ic++){
00317     xsimp.set(ic-1,glp_get_col_prim(lp, ic));}
00318   glp_delete_prob(lp);
00319   delete [] ia;
00320   ia = NULL;
00321   delete [] ja;
00322   ja = NULL;
00323   delete [] ar;
00324   ar = NULL;
00325   return xsimp;
00326 }
00327 
00329 //
00336 //
00337 
00338 inline
00339 matrix_int glex_dtuples( size_t dim, size_t total)
00340 {
00341   matrix_int g(total,dim);
00342   vector_int v(dim);
00343   for(size_t j = 0; j < dim; j++){
00344     v.set(j,0);}
00345   for(size_t i = 0; i < total; i++){
00346     for(size_t j = 0; j < dim; j++){
00347       g.set_element(i,j,v.get(j));}
00348     if (dim==1) v.set(0,i+1);  
00349     if (dim>1) v=v.glex();}
00350   return g;
00351 }
00352 
00353 #endif // GSL_VECTOR_MATRIX_OPS_H
All Classes Files Functions Variables Typedefs Friends Defines