qpOASES  3.0.1
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-2014 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 #ifdef __USE_SINGLE_PRECISION__
00046 
00048     #define GEMM sgemm_
00049 
00050     #define SYR ssyr_
00051 
00052     #define SYR2 ssyr2_
00053 
00054     #define POTRF spotrf_
00055 
00056 #else
00057 
00059     #define GEMM dgemm_
00060 
00061     #define SYR  dsyr_
00062 
00063     #define SYR2 dsyr2_
00064 
00065     #define POTRF dpotrf_
00066 
00067 #endif /* __USE_SINGLE_PRECISION__ */
00068 
00069 
00070 extern "C"
00071 {
00073     void dgemm_ ( const char*, const char*, const unsigned long*, const unsigned long*, const unsigned long*,
00074             const double*, const double*, const unsigned long*, const double*, const unsigned long*,
00075             const double*, double*, const unsigned long* );
00077     void sgemm_ ( const char*, const char*, const unsigned long*, const unsigned long*, const unsigned long*,
00078             const float*, const float*, const unsigned long*, const float*, const unsigned long*,
00079             const float*, float*, const unsigned long* );
00080 
00082     void dsyr_ ( const char *, const unsigned long *, const double *, const double *,
00083                  const unsigned long *, double *, const unsigned long *);
00085     void ssyr_ ( const char *, const unsigned long *, const float *, const float *,
00086                  const unsigned long *, float *, const unsigned long *);
00087 
00089     void dsyr2_ ( const char *, const unsigned long *, const double *, const double *,
00090                   const unsigned long *, const double *, const unsigned long *, double *, const unsigned long *);
00092     void ssyr2_ ( const char *, const unsigned long *, const float *, const float *,
00093                   const unsigned long *, const float *, const unsigned long *, float *, const unsigned long *);
00094 
00096     void dpotrf_ ( const char *, const unsigned long *, double *, const unsigned long *, long * );
00098     void spotrf_ ( const char *, const unsigned long *, float *, const unsigned long *, long * );
00099 }
00100 
00101 
00102 BEGIN_NAMESPACE_QPOASES
00103 
00110 typedef int sparse_int_t;
00111 
00112 
00124 class Matrix
00125 {
00126     public:
00128         Matrix( ) { doNotFreeMemory(); };
00129 
00131         virtual ~Matrix( ) { };
00132 
00134         virtual void free( ) = 0;
00135 
00138         virtual Matrix* duplicate( ) const = 0;
00139 
00142         virtual real_t diag(    int i           
00143                                 ) const = 0;
00144 
00148         virtual BooleanType isDiag( ) const = 0;
00149 
00153         virtual real_t getNorm( int type = 2    
00154                                 ) const = 0;
00155 
00159         virtual real_t getRowNorm(  int rNum,       
00160                                     int type = 2    
00161                                     ) const = 0;
00162 
00165         virtual returnValue getRow( int rNum,                       
00166                                     const Indexlist* const icols,   
00167                                     real_t alpha,                   
00168                                     real_t *row                     
00169                                     ) const = 0;
00170 
00173         virtual returnValue getCol( int cNum,                       
00174                                     const Indexlist* const irows,   
00175                                     real_t alpha,                   
00176                                     real_t *col                     
00177                                     ) const = 0;
00178 
00181         virtual returnValue times ( int xN,                 
00182                                     real_t alpha,           
00183                                     const real_t *x,        
00184                                     int xLD,                
00185                                     real_t beta,            
00186                                     real_t *y,              
00187                                     int yLD                 
00188                                     ) const = 0;
00189 
00192         virtual returnValue transTimes (    int xN,             
00193                                             real_t alpha,       
00194                                             const real_t *x,    
00195                                             int xLD,            
00196                                             real_t beta,        
00197                                             real_t *y,          
00198                                             int yLD             
00199                                             ) const = 0;
00200 
00203         virtual returnValue times ( const Indexlist* const irows,   
00204                                     const Indexlist* const icols,   
00205                                     int xN,                         
00206                                     real_t alpha,                   
00207                                     const real_t *x,                
00208                                     int xLD,                        
00209                                     real_t beta,                    
00210                                     real_t *y,                      
00211                                     int yLD,                        
00212                                     BooleanType yCompr = BT_TRUE    
00213                                     ) const = 0;
00214 
00217         virtual returnValue transTimes (    const Indexlist* const irows,   
00218                                             const Indexlist* const icols,   
00219                                             int xN,                         
00220                                             real_t alpha,                   
00221                                             const real_t *x,                
00222                                             int xLD,                        
00223                                             real_t beta,                    
00224                                             real_t *y,                      
00225                                             int yLD                         
00226                                             ) const = 0;
00227 
00231         virtual returnValue addToDiag(  real_t alpha        
00232                                         ) = 0;
00233 
00240         virtual real_t* full() const = 0;
00241 
00242 
00245         virtual returnValue print(  const char* name = 0    
00246                                     ) const = 0;
00247 
00248 
00252         BooleanType needToFreeMemory( ) const { return freeMemory; };
00253 
00255         void doFreeMemory( ) { freeMemory = BT_TRUE; };
00256 
00258         void doNotFreeMemory( ) { freeMemory = BT_FALSE; };
00259 
00260 
00261     protected:
00262             BooleanType freeMemory;             
00264 };
00265 
00266 
00277 class SymmetricMatrix : public virtual Matrix
00278 {
00279     public:
00280 
00283         virtual SymmetricMatrix* duplicateSym( ) const = 0;
00284 
00285 
00288         virtual returnValue bilinear(   const Indexlist* const icols,   
00289                                         int xN,                         
00290                                         const real_t *x,                
00291                                         int xLD,                        
00292                                         real_t *y,                      
00293                                         int yLD                         
00294                                         ) const = 0;
00295 
00296 };
00297 
00298 
00308 class DenseMatrix : public virtual Matrix
00309 {
00310     public:
00312         DenseMatrix( ) : nRows(0), nCols(0), leaDim(0), val(0) {};
00313 
00317         DenseMatrix(    int m,          
00318                         int n,          
00319                         int lD,         
00320                         real_t *v       
00321                         ) : nRows(m), nCols(n), leaDim(lD), val(v) {}
00322 
00323 
00325         virtual ~DenseMatrix();
00326 
00328         virtual void free( );
00329 
00332         virtual Matrix *duplicate( ) const;
00333 
00336         virtual real_t diag(    int i           
00337                                 ) const;
00338 
00342         virtual BooleanType isDiag( ) const;
00343 
00347         virtual real_t getNorm( int type = 2    
00348                                 ) const;
00349         
00353         virtual real_t getRowNorm(  int rNum,       
00354                                     int type = 2    
00355                                     ) const;
00356 
00359         virtual returnValue getRow(
00360                 int rNum,                       
00361                 const Indexlist* const icols,   
00362                 real_t alpha,                   
00363                 real_t *row                     
00364                 ) const;
00365 
00368         virtual returnValue getCol(
00369                 int cNum,                       
00370                 const Indexlist* const irows,   
00371                 real_t alpha,                   
00372                 real_t *col                     
00373                 ) const;
00374 
00377         returnValue times ( int xN,                 
00378                             real_t alpha,           
00379                             const real_t *x,        
00380                             int xLD,                
00381                             real_t beta,            
00382                             real_t *y,              
00383                             int yLD                 
00384                             ) const;
00385 
00388         returnValue transTimes (    int xN,             
00389                                     real_t alpha,       
00390                                     const real_t *x,    
00391                                     int xLD,            
00392                                     real_t beta,        
00393                                     real_t *y,          
00394                                     int yLD             
00395                                     ) const;
00396 
00399         virtual returnValue times ( const Indexlist* const irows,   
00400                                     const Indexlist* const icols,   
00401                                     int xN,                         
00402                                     real_t alpha,                   
00403                                     const real_t *x,                
00404                                     int xLD,                        
00405                                     real_t beta,                    
00406                                     real_t *y,                      
00407                                     int yLD,                        
00408                                     BooleanType yCompr = BT_TRUE    
00409                                     ) const;
00410 
00413         virtual returnValue transTimes (    const Indexlist* const irows,   
00414                                             const Indexlist* const icols,   
00415                                             int xN,                         
00416                                             real_t alpha,                   
00417                                             const real_t *x,                
00418                                             int xLD,                        
00419                                             real_t beta,                    
00420                                             real_t *y,                      
00421                                             int yLD                         
00422                                             ) const;
00423 
00427         virtual returnValue addToDiag(  real_t alpha        
00428                                         );
00429 
00430 
00437         virtual real_t* full() const;
00438 
00439 
00442         virtual returnValue print(  const char* name = 0    
00443                                     ) const;
00444 
00445 
00446     protected:
00447         int nRows;          
00448         int nCols;          
00449         int leaDim;         
00450         real_t *val;        
00451 };
00452 
00453 
00463 class SymDenseMat : public DenseMatrix, public SymmetricMatrix
00464 {
00465     public:
00467         SymDenseMat() : DenseMatrix() {}
00468 
00470         SymDenseMat(    int m,          
00471                         int n,          
00472                         int lD,         
00473                         real_t *v       
00474                         ) : DenseMatrix(m, n, lD, v) {}
00475 
00478         virtual Matrix *duplicate( ) const;
00479 
00482         virtual SymmetricMatrix* duplicateSym( ) const;
00483 
00484 
00487         virtual returnValue bilinear(   const Indexlist* const icols,   
00488                                         int xN,                         
00489                                         const real_t *x,                
00490                                         int xLD,                        
00491                                         real_t *y,                      
00492                                         int yLD                         
00493                                         ) const;
00494 };
00495 
00496 
00506 class SparseMatrix : public virtual Matrix
00507 {
00508     public:
00510         SparseMatrix();
00511 
00513         SparseMatrix(   int nr,             
00514                         int nc,             
00515                         sparse_int_t *r,    
00516                         sparse_int_t *c,    
00517                         real_t *v           
00518                         );
00519 
00521         SparseMatrix(   int nr,                 
00522                         int nc,                 
00523                         int ld,                 
00524                         const real_t * const v  
00525                         );
00526 
00528         virtual ~SparseMatrix();
00529 
00531         virtual void free( );
00532 
00535         virtual Matrix *duplicate( ) const;
00536 
00539         virtual real_t diag(    int i           
00540                                 ) const;
00541 
00545         virtual BooleanType isDiag( ) const;
00546 
00547 
00551         virtual real_t getNorm( int type = 2    
00552                                 ) const;
00553 
00557         virtual real_t getRowNorm(  int rNum,       
00558                                     int type = 2    
00559                                     ) const;
00560 
00562         virtual returnValue getRow( int rNum,                       
00563                                     const Indexlist* const icols,   
00564                                     real_t alpha,                   
00565                                     real_t *row                     
00566                                     ) const;
00567 
00569         virtual returnValue getCol( int cNum,                       
00570                                     const Indexlist* const irows,   
00571                                     real_t alpha,                   
00572                                     real_t *col                     
00573                                     ) const;
00574 
00576         virtual returnValue times ( int xN,                 
00577                                     real_t alpha,           
00578                                     const real_t *x,        
00579                                     int xLD,                
00580                                     real_t beta,            
00581                                     real_t *y,              
00582                                     int yLD                 
00583                                     ) const;
00584 
00586         virtual returnValue transTimes (    int xN,             
00587                                             real_t alpha,       
00588                                             const real_t *x,    
00589                                             int xLD,            
00590                                             real_t beta,        
00591                                             real_t *y,          
00592                                             int yLD             
00593                                             ) const;
00594 
00596         virtual returnValue times ( const Indexlist* const irows,   
00597                                     const Indexlist* const icols,   
00598                                     int xN,                         
00599                                     real_t alpha,                   
00600                                     const real_t *x,                
00601                                     int xLD,                        
00602                                     real_t beta,                    
00603                                     real_t *y,                      
00604                                     int yLD,                        
00605                                     BooleanType yCompr = BT_TRUE    
00606                                     ) const;
00607 
00609         virtual returnValue transTimes (    const Indexlist* const irows,   
00610                                             const Indexlist* const icols,   
00611                                             int xN,                         
00612                                             real_t alpha,                   
00613                                             const real_t *x,                
00614                                             int xLD,                        
00615                                             real_t beta,                    
00616                                             real_t *y,                      
00617                                             int yLD                         
00618                                             ) const;
00619 
00623         virtual returnValue addToDiag(  real_t alpha        
00624                                         );
00625 
00628         sparse_int_t *createDiagInfo();
00629 
00636         virtual real_t* full() const;
00637 
00640         virtual returnValue print(  const char* name = 0    
00641                                     ) const;
00642 
00643 
00644     protected:
00645         int nRows;          
00646         int nCols;          
00647         sparse_int_t *ir;   
00648         sparse_int_t *jc;   
00649         sparse_int_t *jd;   
00650         real_t *val;        
00651 };
00652 
00653 
00663 class SparseMatrixRow : public virtual Matrix
00664 {
00665     public:
00667         SparseMatrixRow();
00668 
00670         SparseMatrixRow(    int nr,             
00671                             int nc,             
00672                             sparse_int_t *r,    
00673                             sparse_int_t *c,    
00674                             real_t *v           
00675                             );
00676 
00678         SparseMatrixRow(    int nr,                 
00679                             int nc,                 
00680                             int ld,                 
00681                             const real_t * const v  
00682                             );
00683 
00685         virtual ~SparseMatrixRow();
00686 
00688         virtual void free( );
00689 
00692         virtual Matrix *duplicate( ) const;
00693 
00696         virtual real_t diag(    int i           
00697                                 ) const;
00698 
00702         virtual BooleanType isDiag( ) const;
00703 
00704         
00708         virtual real_t getNorm( int type = 2    
00709                                 ) const;
00710 
00714         virtual real_t getRowNorm(  int rNum,       
00715                                     int type = 2    
00716                                     ) const;
00717 
00719         virtual returnValue getRow (
00720                 int rNum,                       
00721                 const Indexlist* const icols,   
00722                 real_t alpha,                   
00723                 real_t *row                     
00724                 ) const;
00725 
00727         virtual returnValue getCol (
00728                 int cNum,                       
00729                 const Indexlist* const irows,   
00730                 real_t alpha,                   
00731                 real_t *col                     
00732                 ) const;
00733 
00735         virtual returnValue times ( int xN,                 
00736                                     real_t alpha,           
00737                                     const real_t *x,        
00738                                     int xLD,                
00739                                     real_t beta,            
00740                                     real_t *y,              
00741                                     int yLD                 
00742                                     ) const;
00743 
00745         virtual returnValue transTimes (    int xN,             
00746                                             real_t alpha,       
00747                                             const real_t *x,    
00748                                             int xLD,            
00749                                             real_t beta,        
00750                                             real_t *y,          
00751                                             int yLD             
00752                                             ) const;
00753 
00755         virtual returnValue times ( const Indexlist* const irows,   
00756                                     const Indexlist* const icols,   
00757                                     int xN,                         
00758                                     real_t alpha,                   
00759                                     const real_t *x,                
00760                                     int xLD,                        
00761                                     real_t beta,                    
00762                                     real_t *y,                      
00763                                     int yLD,                        
00764                                     BooleanType yCompr = BT_TRUE    
00765                                     ) const;
00766 
00768         virtual returnValue transTimes (    const Indexlist* const irows,   
00769                                             const Indexlist* const icols,   
00770                                             int xN,                         
00771                                             real_t alpha,                   
00772                                             const real_t *x,                
00773                                             int xLD,                        
00774                                             real_t beta,                    
00775                                             real_t *y,                      
00776                                             int yLD                         
00777                                             ) const;
00778 
00782         virtual returnValue addToDiag(  real_t alpha        
00783                                         );
00784 
00787         sparse_int_t *createDiagInfo();
00788 
00795         virtual real_t* full() const;
00796 
00799         virtual returnValue print(  const char* name = 0    
00800                                     ) const;
00801 
00802 
00803     protected:
00804         int nRows;          
00805         int nCols;          
00806         sparse_int_t *jr;   
00807         sparse_int_t *ic;   
00808         sparse_int_t *jd;   
00809         real_t *val;        
00810 };
00811 
00812 
00822 class SymSparseMat : public SymmetricMatrix, public SparseMatrix
00823 {
00824     public:
00826         SymSparseMat() : SparseMatrix() {}
00827 
00829         SymSparseMat(   int nr,             
00830                         int nc,             
00831                         sparse_int_t *r,    
00832                         sparse_int_t *c,    
00833                         real_t *v           
00834                         ) : SparseMatrix(nr, nc, r, c, v) {}
00835 
00837         SymSparseMat(   int nr,                 
00838                         int nc,                 
00839                         int ld,                 
00840                         const real_t * const v  
00841                         ) : SparseMatrix(nr, nc, ld, v) {}
00842 
00845         virtual Matrix *duplicate( ) const;
00846 
00849         virtual SymmetricMatrix* duplicateSym( ) const;
00850 
00851 
00854         virtual returnValue bilinear(   const Indexlist* const icols,   
00855                                         int xN,                         
00856                                         const real_t *x,                
00857                                         int xLD,                        
00858                                         real_t *y,                      
00859                                         int yLD                         
00860                                         ) const;
00861 };
00862 
00863 
00864 END_NAMESPACE_QPOASES
00865 
00866 
00867 #endif  /* QPOASES_MATRICES_HPP */