qpOASES  3.2.1
An Implementation of the Online Active Set Strategy
include/qpOASES/Matrices.hpp
Go to the documentation of this file.
00001 /*
00002  *  This file is part of qpOASES.
00003  *
00004  *  qpOASES -- An Implementation of the Online Active Set Strategy.
00005  *  Copyright (C) 2007-2017 by Hans Joachim Ferreau, Andreas Potschka,
00006  *  Christian Kirches et al. All rights reserved.
00007  *
00008  *  qpOASES is free software; you can redistribute it and/or
00009  *  modify it under the terms of the GNU Lesser General Public
00010  *  License as published by the Free Software Foundation; either
00011  *  version 2.1 of the License, or (at your option) any later version.
00012  *
00013  *  qpOASES is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  *  See the GNU Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with qpOASES; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  *
00022  */
00023 
00024 
00037 #ifndef QPOASES_MATRICES_HPP
00038 #define QPOASES_MATRICES_HPP
00039 
00040 
00041 #include <qpOASES/Utils.hpp>
00042 #include <qpOASES/Indexlist.hpp>
00043 
00044 
00045 BEGIN_NAMESPACE_QPOASES
00046 
00047 
00059 class Matrix
00060 {
00061     public:
00063         Matrix( ) { doNotFreeMemory(); };
00064 
00066         virtual ~Matrix( ) { };
00067 
00069         virtual void free( ) = 0;
00070 
00073         virtual Matrix* duplicate( ) const = 0;
00074 
00077         virtual real_t diag(    int_t i         
00078                                 ) const = 0;
00079 
00083         virtual BooleanType isDiag( ) const = 0;
00084 
00088         virtual real_t getNorm( int_t type = 2  
00089                                 ) const = 0;
00090 
00094         virtual real_t getRowNorm(  int_t rNum,     
00095                                     int_t type = 2  
00096                                     ) const = 0;
00097 
00100         virtual returnValue getRowNorm(  real_t* norm,   
00101                                          int_t type = 2  
00102                                          ) const = 0;
00103 
00106         virtual returnValue getRow( int_t rNum,                     
00107                                     const Indexlist* const icols,   
00108                                     real_t alpha,                   
00109                                     real_t* row                     
00110                                     ) const = 0;
00111 
00114         virtual returnValue getCol( int_t cNum,                     
00115                                     const Indexlist* const irows,   
00116                                     real_t alpha,                   
00117                                     real_t* col                     
00118                                     ) const = 0;
00119 
00125         virtual returnValue getSparseSubmatrix(
00126                 const Indexlist* const irows,   
00127                 const Indexlist* const icols,   
00128                 int_t rowoffset,                
00129                 int_t coloffset,                
00130                 int_t& numNonzeros,             
00131                 int_t* irn,                     
00132                 int_t* jcn,                     
00133                 real_t* avals,                  
00134                 BooleanType only_lower_triangular = BT_FALSE 
00135                 ) const;
00136 
00143         virtual returnValue getSparseSubmatrix(
00144                 const Indexlist* const irows,   
00145                 int_t idx_icol,                 
00146                 int_t rowoffset,                
00147                 int_t coloffset,                
00148                 int_t& numNonzeros,             
00149                 int_t* irn,                     
00150                 int_t* jcn,                     
00151                 real_t* avals,                  
00152                 BooleanType only_lower_triangular = BT_FALSE 
00153                 ) const;
00154 
00160         virtual returnValue getSparseSubmatrix(
00161                 int_t idx_row,                  
00162                 const Indexlist* const icols,   
00163                 int_t rowoffset,                
00164                 int_t coloffset,                
00165                 int_t& numNonzeros,             
00166                 int_t* irn,                     
00167                 int_t* jcn,                     
00168                 real_t* avals,                  
00169                 BooleanType only_lower_triangular = BT_FALSE 
00170                 ) const;
00171 
00177         virtual returnValue getSparseSubmatrix(
00178                 int_t irowsLength,              
00179                 const int_t* const irowsNumber, 
00180                 int_t icolsLength,              
00181                 const int_t* const icolsNumber, 
00182                 int_t rowoffset,                
00183                 int_t coloffset,                
00184                 int_t& numNonzeros,             
00185                 int_t* irn,                     
00186                 int_t* jcn,                     
00187                 real_t* avals,                  
00188                 BooleanType only_lower_triangular = BT_FALSE 
00189                 ) const = 0;
00190 
00193         virtual returnValue times ( int_t xN,               
00194                                     real_t alpha,           
00195                                     const real_t* x,        
00196                                     int_t xLD,              
00197                                     real_t beta,            
00198                                     real_t* y,              
00199                                     int_t yLD               
00200                                     ) const = 0;
00201 
00204         virtual returnValue transTimes (    int_t xN,           
00205                                             real_t alpha,       
00206                                             const real_t* x,    
00207                                             int_t xLD,          
00208                                             real_t beta,        
00209                                             real_t* y,          
00210                                             int_t yLD           
00211                                             ) const = 0;
00212 
00215         virtual returnValue times ( const Indexlist* const irows,   
00216                                     const Indexlist* const icols,   
00217                                     int_t xN,                       
00218                                     real_t alpha,                   
00219                                     const real_t* x,                
00220                                     int_t xLD,                      
00221                                     real_t beta,                    
00222                                     real_t* y,                      
00223                                     int_t yLD,                      
00224                                     BooleanType yCompr = BT_TRUE    
00225                                     ) const = 0;
00226 
00229         virtual returnValue transTimes (    const Indexlist* const irows,   
00230                                             const Indexlist* const icols,   
00231                                             int_t xN,                       
00232                                             real_t alpha,                   
00233                                             const real_t* x,                
00234                                             int_t xLD,                      
00235                                             real_t beta,                    
00236                                             real_t* y,                      
00237                                             int_t yLD                       
00238                                             ) const = 0;
00239 
00243         virtual returnValue addToDiag(  real_t alpha        
00244                                         ) = 0;
00245 
00252         virtual real_t* full() const = 0;
00253 
00254 
00257         virtual returnValue print(  const char* name = 0    
00258                                     ) const = 0;
00259 
00262         virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const = 0;
00263 
00267         BooleanType needToFreeMemory( ) const { return freeMemory; };
00268 
00270         void doFreeMemory( ) { freeMemory = BT_TRUE; };
00271 
00273         void doNotFreeMemory( ) { freeMemory = BT_FALSE; };
00274 
00275 
00276     protected:
00277             BooleanType freeMemory;             
00279 };
00280 
00281 
00292 class SymmetricMatrix : public virtual Matrix
00293 {
00294     public:
00296         SymmetricMatrix( ) { };
00297 
00299         virtual ~SymmetricMatrix( ) { };
00300 
00303         virtual SymmetricMatrix* duplicateSym( ) const = 0;
00304 
00305 
00308         virtual returnValue bilinear(   const Indexlist* const icols,   
00309                                         int_t xN,                       
00310                                         const real_t* x,                
00311                                         int_t xLD,                      
00312                                         real_t* y,                      
00313                                         int_t yLD                       
00314                                         ) const = 0;
00315 
00316 };
00317 
00318 
00328 class DenseMatrix : public virtual Matrix
00329 {
00330     public:
00332         DenseMatrix( ) : nRows(0), nCols(0), leaDim(0), val(0) { };
00333 
00337         DenseMatrix(    int_t m,        
00338                         int_t n,        
00339                         int_t lD,       
00340                         real_t* v       
00341                         ) : nRows(m), nCols(n), leaDim(lD), val(v) {}
00342 
00343 
00345         virtual ~DenseMatrix( );
00346 
00348         virtual void free( );
00349 
00352         virtual Matrix *duplicate( ) const;
00353 
00356         virtual real_t diag(    int_t i         
00357                                 ) const;
00358 
00362         virtual BooleanType isDiag( ) const;
00363 
00367         virtual real_t getNorm( int_t type = 2  
00368                                 ) const;
00369         
00373         virtual real_t getRowNorm(  int_t rNum,     
00374                                     int_t type = 2  
00375                                     ) const;
00376 
00379         virtual returnValue getRowNorm(  real_t* norm,   
00380                                          int_t type = 2  
00381                                          ) const;
00382 
00385         virtual returnValue getRow( int_t rNum,                     
00386                                     const Indexlist* const icols,   
00387                                     real_t alpha,                   
00388                                     real_t* row                     
00389                                     ) const;
00390 
00393         virtual returnValue getCol(
00394                 int_t cNum,                     
00395                 const Indexlist* const irows,   
00396                 real_t alpha,                   
00397                 real_t* col                     
00398                 ) const;
00399 
00405         virtual returnValue getSparseSubmatrix(
00406                 int_t irowsLength,              
00407                 const int_t* const irowsNumber, 
00408                 int_t icolsLength,              
00409                 const int_t* const icolsNumber, 
00410                 int_t rowoffset,                
00411                 int_t coloffset,                
00412                 int_t& numNonzeros,             
00413                 int_t* irn,                     
00414                 int_t* jcn,                     
00415                 real_t* avals,                  
00416                 BooleanType only_lower_triangular = BT_FALSE 
00417                 ) const;
00418 
00419 
00422         virtual returnValue times(  int_t xN,               
00423                                     real_t alpha,           
00424                                     const real_t* x,        
00425                                     int_t xLD,              
00426                                     real_t beta,            
00427                                     real_t* y,              
00428                                     int_t yLD               
00429                                     ) const;
00430 
00433         virtual returnValue transTimes( int_t xN,           
00434                                         real_t alpha,       
00435                                         const real_t* x,    
00436                                         int_t xLD,          
00437                                         real_t beta,        
00438                                         real_t* y,          
00439                                         int_t yLD           
00440                                         ) const;
00441 
00444         virtual returnValue times(  const Indexlist* const irows,   
00445                                     const Indexlist* const icols,   
00446                                     int_t xN,                       
00447                                     real_t alpha,                   
00448                                     const real_t* x,                
00449                                     int_t xLD,                      
00450                                     real_t beta,                    
00451                                     real_t* y,                      
00452                                     int_t yLD,                      
00453                                     BooleanType yCompr = BT_TRUE    
00454                                     ) const;
00455 
00458         virtual returnValue transTimes( const Indexlist* const irows,   
00459                                         const Indexlist* const icols,   
00460                                         int_t xN,                       
00461                                         real_t alpha,                   
00462                                         const real_t* x,                
00463                                         int_t xLD,                      
00464                                         real_t beta,                    
00465                                         real_t* y,                      
00466                                         int_t yLD                       
00467                                         ) const;
00468 
00472         virtual returnValue addToDiag(  real_t alpha        
00473                                         );
00474 
00475 
00482         virtual real_t* full() const;
00483 
00484 
00487         virtual returnValue print(  const char* name = 0    
00488                                     ) const;
00489 
00492         virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const;
00493 
00494     protected:
00495         int_t nRows;        
00496         int_t nCols;        
00497         int_t leaDim;       
00498         real_t* val;        
00499 };
00500 
00501 
00511 class SymDenseMat : public DenseMatrix, public SymmetricMatrix
00512 {
00513     public:
00515         SymDenseMat() : DenseMatrix() { };
00516 
00518         SymDenseMat(    int_t m,        
00519                         int_t n,        
00520                         int_t lD,       
00521                         real_t* v       
00522                         ) : DenseMatrix(m, n, lD, v) { };
00523 
00525         virtual ~SymDenseMat() { };
00526 
00529         virtual Matrix *duplicate( ) const;
00530 
00533         virtual SymmetricMatrix* duplicateSym( ) const;
00534 
00535 
00538         virtual returnValue bilinear(   const Indexlist* const icols,   
00539                                         int_t xN,                       
00540                                         const real_t* x,                
00541                                         int_t xLD,                      
00542                                         real_t* y,                      
00543                                         int_t yLD                       
00544                                         ) const;
00545 };
00546 
00547 
00557 class SparseMatrix : public virtual Matrix
00558 {
00559     public:
00561         SparseMatrix( );
00562 
00564         SparseMatrix(   int_t nr,           
00565                         int_t nc,           
00566                         sparse_int_t* r,    
00567                         sparse_int_t* c,    
00568                         real_t* v           
00569                         );
00570 
00572         SparseMatrix(   int_t nr,               
00573                         int_t nc,               
00574                         int_t ld,               
00575                         const real_t*  const v  
00576                         );
00577 
00579         virtual ~SparseMatrix( );
00580 
00582         virtual void free( );
00583 
00586         virtual Matrix *duplicate( ) const;
00587 
00588 
00593         virtual void setVal(    const real_t* newVal    
00594                                 );
00595 
00598         virtual real_t diag(    int_t i         
00599                                 ) const;
00600 
00604         virtual BooleanType isDiag( ) const;
00605 
00606 
00610         virtual real_t getNorm( int_t type = 2  
00611                                 ) const;
00612 
00616         virtual real_t getRowNorm(  int_t rNum,     
00617                                     int_t type = 2  
00618                                     ) const;
00619 
00622         virtual returnValue getRowNorm(  real_t* norm,   
00623                                          int_t type = 2  
00624                                          ) const;
00625 
00627         virtual returnValue getRow( int_t rNum,                     
00628                                     const Indexlist* const icols,   
00629                                     real_t alpha,                   
00630                                     real_t* row                     
00631                                     ) const;
00632 
00634         virtual returnValue getCol( int_t cNum,                     
00635                                     const Indexlist* const irows,   
00636                                     real_t alpha,                   
00637                                     real_t* col                     
00638                                     ) const;
00639 
00645         virtual returnValue getSparseSubmatrix(
00646                 int_t irowsLength,              
00647                 const int_t* const irowsNumber, 
00648                 int_t icolsLength,              
00649                 const int_t* const icolsNumber, 
00650                 int_t rowoffset,                
00651                 int_t coloffset,                
00652                 int_t& numNonzeros,             
00653                 int_t* irn,                     
00654                 int_t* jcn,                     
00655                 real_t* avals,                  
00656                 BooleanType only_lower_triangular = BT_FALSE 
00657                 ) const;
00658 
00660         virtual returnValue times ( int_t xN,               
00661                                     real_t alpha,           
00662                                     const real_t* x,        
00663                                     int_t xLD,              
00664                                     real_t beta,            
00665                                     real_t* y,              
00666                                     int_t yLD               
00667                                     ) const;
00668 
00670         virtual returnValue transTimes (    int_t xN,           
00671                                             real_t alpha,       
00672                                             const real_t* x,    
00673                                             int_t xLD,          
00674                                             real_t beta,        
00675                                             real_t* y,          
00676                                             int_t yLD           
00677                                             ) const;
00678 
00680         virtual returnValue times ( const Indexlist* const irows,   
00681                                     const Indexlist* const icols,   
00682                                     int_t xN,                       
00683                                     real_t alpha,                   
00684                                     const real_t* x,                
00685                                     int_t xLD,                      
00686                                     real_t beta,                    
00687                                     real_t* y,                      
00688                                     int_t yLD,                      
00689                                     BooleanType yCompr = BT_TRUE    
00690                                     ) const;
00691 
00693         virtual returnValue transTimes (    const Indexlist* const irows,   
00694                                             const Indexlist* const icols,   
00695                                             int_t xN,                       
00696                                             real_t alpha,                   
00697                                             const real_t* x,                
00698                                             int_t xLD,                      
00699                                             real_t beta,                    
00700                                             real_t* y,                      
00701                                             int_t yLD                       
00702                                             ) const;
00703 
00707         virtual returnValue addToDiag(  real_t alpha        
00708                                         );
00709 
00712         sparse_int_t* createDiagInfo();
00713 
00720         virtual real_t* full() const;
00721 
00724         virtual returnValue print(  const char* name = 0    
00725                                     ) const;
00726 
00729         virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const;
00730 
00731 
00732     protected:
00733         int_t nRows;        
00734         int_t nCols;        
00735         sparse_int_t* ir;   
00736         sparse_int_t* jc;   
00737         sparse_int_t* jd;   
00738         real_t* val;        
00739 };
00740 
00741 
00751 class SparseMatrixRow : public virtual Matrix
00752 {
00753     public:
00755         SparseMatrixRow( );
00756 
00758         SparseMatrixRow(    int_t nr,           
00759                             int_t nc,           
00760                             sparse_int_t* r,    
00761                             sparse_int_t* c,    
00762                             real_t* v           
00763                             );
00764 
00766         SparseMatrixRow(    int_t nr,               
00767                             int_t nc,               
00768                             int_t ld,               
00769                             const real_t*  const v  
00770                             );
00771 
00773         virtual ~SparseMatrixRow( );
00774 
00776         virtual void free( );
00777 
00780         virtual Matrix *duplicate( ) const;
00781 
00784         virtual real_t diag(    int_t i         
00785                                 ) const;
00786 
00790         virtual BooleanType isDiag( ) const;
00791 
00792         
00796         virtual real_t getNorm( int_t type = 2  
00797                                 ) const;
00798 
00802         virtual real_t getRowNorm(  int_t rNum,     
00803                                     int_t type = 2  
00804                                     ) const;
00805 
00808         virtual returnValue getRowNorm(  real_t* norm,   
00809                                          int_t type = 2  
00810                                          ) const;
00811 
00813         virtual returnValue getRow (    int_t rNum,                     
00814                                         const Indexlist* const icols,   
00815                                         real_t alpha,                   
00816                                         real_t* row                     
00817                                         ) const;
00818 
00820         virtual returnValue getCol (    int_t cNum,                     
00821                                         const Indexlist* const irows,   
00822                                         real_t alpha,                   
00823                                         real_t* col                     
00824                                         ) const;
00825 
00831         virtual returnValue getSparseSubmatrix(
00832                 int_t irowsLength,              
00833                 const int_t* const irowsNumber, 
00834                 int_t icolsLength,              
00835                 const int_t* const icolsNumber, 
00836                 int_t rowoffset,                
00837                 int_t coloffset,                
00838                 int_t& numNonzeros,             
00839                 int_t* irn,                     
00840                 int_t* jcn,                     
00841                 real_t* avals,                  
00842                 BooleanType only_lower_triangular = BT_FALSE 
00843                 ) const;
00844 
00846         virtual returnValue times(  int_t xN,               
00847                                     real_t alpha,           
00848                                     const real_t* x,        
00849                                     int_t xLD,              
00850                                     real_t beta,            
00851                                     real_t* y,              
00852                                     int_t yLD               
00853                                     ) const;
00854 
00856         virtual returnValue transTimes( int_t xN,           
00857                                         real_t alpha,       
00858                                         const real_t* x,    
00859                                         int_t xLD,          
00860                                         real_t beta,        
00861                                         real_t* y,          
00862                                         int_t yLD           
00863                                         ) const;
00864 
00866         virtual returnValue times(  const Indexlist* const irows,   
00867                                     const Indexlist* const icols,   
00868                                     int_t xN,                       
00869                                     real_t alpha,                   
00870                                     const real_t* x,                
00871                                     int_t xLD,                      
00872                                     real_t beta,                    
00873                                     real_t* y,                      
00874                                     int_t yLD,                      
00875                                     BooleanType yCompr = BT_TRUE    
00876                                     ) const;
00877 
00879         virtual returnValue transTimes( const Indexlist* const irows,   
00880                                         const Indexlist* const icols,   
00881                                         int_t xN,                       
00882                                         real_t alpha,                   
00883                                         const real_t* x,                
00884                                         int_t xLD,                      
00885                                         real_t beta,                    
00886                                         real_t* y,                      
00887                                         int_t yLD                       
00888                                         ) const;
00889 
00893         virtual returnValue addToDiag(  real_t alpha        
00894                                         );
00895 
00898         sparse_int_t* createDiagInfo();
00899 
00906         virtual real_t* full() const;
00907 
00910         virtual returnValue print(  const char* name = 0    
00911                                     ) const;
00912 
00915         virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const;
00916 
00917     protected:
00918         int_t nRows;        
00919         int_t nCols;        
00920         sparse_int_t* jr;   
00921         sparse_int_t* ic;   
00922         sparse_int_t* jd;   
00923         real_t* val;        
00924 };
00925 
00926 
00936 class SymSparseMat : public SymmetricMatrix, public SparseMatrix
00937 {
00938     public:
00940         SymSparseMat( ) : SparseMatrix( ) { };
00941 
00943         SymSparseMat(   int_t nr,           
00944                         int_t nc,           
00945                         sparse_int_t* r,    
00946                         sparse_int_t* c,    
00947                         real_t* v           
00948                         ) : SparseMatrix(nr, nc, r, c, v) { };
00949 
00951         SymSparseMat(   int_t nr,               
00952                         int_t nc,               
00953                         int_t ld,               
00954                         const real_t*  const v  
00955                         ) : SparseMatrix(nr, nc, ld, v) { };
00956 
00958         virtual ~SymSparseMat( ) { };
00959 
00962         virtual Matrix *duplicate( ) const;
00963 
00966         virtual SymmetricMatrix* duplicateSym( ) const;
00967 
00968 
00971         virtual returnValue bilinear(   const Indexlist* const icols,   
00972                                         int_t xN,                       
00973                                         const real_t* x,                
00974                                         int_t xLD,                      
00975                                         real_t* y,                      
00976                                         int_t yLD                       
00977                                         ) const;
00978 };
00979 
00980 
00981 END_NAMESPACE_QPOASES
00982 
00983 
00984 #endif  /* QPOASES_MATRICES_HPP */