qpOASES  3.1.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-2015 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:
00281         SymmetricMatrix( ) { };
00282 
00284         virtual ~SymmetricMatrix( ) { };
00285 
00288         virtual SymmetricMatrix* duplicateSym( ) const = 0;
00289 
00290 
00293         virtual returnValue bilinear(   const Indexlist* const icols,   
00294                                         int xN,                         
00295                                         const real_t *x,                
00296                                         int xLD,                        
00297                                         real_t *y,                      
00298                                         int yLD                         
00299                                         ) const = 0;
00300 
00301 };
00302 
00303 
00313 class DenseMatrix : public virtual Matrix
00314 {
00315     public:
00317         DenseMatrix( ) : nRows(0), nCols(0), leaDim(0), val(0) { };
00318 
00322         DenseMatrix(    int m,          
00323                         int n,          
00324                         int lD,         
00325                         real_t *v       
00326                         ) : nRows(m), nCols(n), leaDim(lD), val(v) {}
00327 
00328 
00330         virtual ~DenseMatrix( );
00331 
00333         virtual void free( );
00334 
00337         virtual Matrix *duplicate( ) const;
00338 
00341         virtual real_t diag(    int i           
00342                                 ) const;
00343 
00347         virtual BooleanType isDiag( ) const;
00348 
00352         virtual real_t getNorm( int type = 2    
00353                                 ) const;
00354         
00358         virtual real_t getRowNorm(  int rNum,       
00359                                     int type = 2    
00360                                     ) const;
00361 
00364         virtual returnValue getRow( int rNum,                       
00365                                     const Indexlist* const icols,   
00366                                     real_t alpha,                   
00367                                     real_t *row                     
00368                                     ) const;
00369 
00372         virtual returnValue getCol( int cNum,                       
00373                                     const Indexlist* const irows,   
00374                                     real_t alpha,                   
00375                                     real_t *col                     
00376                                     ) const;
00377 
00380         virtual returnValue times(  int xN,                 
00381                                     real_t alpha,           
00382                                     const real_t *x,        
00383                                     int xLD,                
00384                                     real_t beta,            
00385                                     real_t *y,              
00386                                     int yLD                 
00387                                     ) const;
00388 
00391         virtual returnValue transTimes( int xN,             
00392                                         real_t alpha,       
00393                                         const real_t *x,    
00394                                         int xLD,            
00395                                         real_t beta,        
00396                                         real_t *y,          
00397                                         int yLD             
00398                                         ) const;
00399 
00402         virtual returnValue times(  const Indexlist* const irows,   
00403                                     const Indexlist* const icols,   
00404                                     int xN,                         
00405                                     real_t alpha,                   
00406                                     const real_t *x,                
00407                                     int xLD,                        
00408                                     real_t beta,                    
00409                                     real_t *y,                      
00410                                     int yLD,                        
00411                                     BooleanType yCompr = BT_TRUE    
00412                                     ) const;
00413 
00416         virtual returnValue transTimes( const Indexlist* const irows,   
00417                                         const Indexlist* const icols,   
00418                                         int xN,                         
00419                                         real_t alpha,                   
00420                                         const real_t *x,                
00421                                         int xLD,                        
00422                                         real_t beta,                    
00423                                         real_t *y,                      
00424                                         int yLD                         
00425                                         ) const;
00426 
00430         virtual returnValue addToDiag(  real_t alpha        
00431                                         );
00432 
00433 
00440         virtual real_t* full() const;
00441 
00442 
00445         virtual returnValue print(  const char* name = 0    
00446                                     ) const;
00447 
00448 
00449     protected:
00450         int nRows;          
00451         int nCols;          
00452         int leaDim;         
00453         real_t *val;        
00454 };
00455 
00456 
00466 class SymDenseMat : public DenseMatrix, public SymmetricMatrix
00467 {
00468     public:
00470         SymDenseMat() : DenseMatrix() { };
00471 
00473         SymDenseMat(    int m,          
00474                         int n,          
00475                         int lD,         
00476                         real_t *v       
00477                         ) : DenseMatrix(m, n, lD, v) { };
00478 
00480         virtual ~SymDenseMat() { };
00481 
00484         virtual Matrix *duplicate( ) const;
00485 
00488         virtual SymmetricMatrix* duplicateSym( ) const;
00489 
00490 
00493         virtual returnValue bilinear(   const Indexlist* const icols,   
00494                                         int xN,                         
00495                                         const real_t *x,                
00496                                         int xLD,                        
00497                                         real_t *y,                      
00498                                         int yLD                         
00499                                         ) const;
00500 };
00501 
00502 
00512 class SparseMatrix : public virtual Matrix
00513 {
00514     public:
00516         SparseMatrix( );
00517 
00519         SparseMatrix(   int nr,             
00520                         int nc,             
00521                         sparse_int_t *r,    
00522                         sparse_int_t *c,    
00523                         real_t *v           
00524                         );
00525 
00527         SparseMatrix(   int nr,                 
00528                         int nc,                 
00529                         int ld,                 
00530                         const real_t * const v  
00531                         );
00532 
00534         virtual ~SparseMatrix( );
00535 
00537         virtual void free( );
00538 
00541         virtual Matrix *duplicate( ) const;
00542 
00545         virtual real_t diag(    int i           
00546                                 ) const;
00547 
00551         virtual BooleanType isDiag( ) const;
00552 
00553 
00557         virtual real_t getNorm( int type = 2    
00558                                 ) const;
00559 
00563         virtual real_t getRowNorm(  int rNum,       
00564                                     int type = 2    
00565                                     ) const;
00566 
00568         virtual returnValue getRow( int rNum,                       
00569                                     const Indexlist* const icols,   
00570                                     real_t alpha,                   
00571                                     real_t *row                     
00572                                     ) const;
00573 
00575         virtual returnValue getCol( int cNum,                       
00576                                     const Indexlist* const irows,   
00577                                     real_t alpha,                   
00578                                     real_t *col                     
00579                                     ) const;
00580 
00582         virtual returnValue times ( int xN,                 
00583                                     real_t alpha,           
00584                                     const real_t *x,        
00585                                     int xLD,                
00586                                     real_t beta,            
00587                                     real_t *y,              
00588                                     int yLD                 
00589                                     ) const;
00590 
00592         virtual returnValue transTimes (    int xN,             
00593                                             real_t alpha,       
00594                                             const real_t *x,    
00595                                             int xLD,            
00596                                             real_t beta,        
00597                                             real_t *y,          
00598                                             int yLD             
00599                                             ) const;
00600 
00602         virtual returnValue times ( const Indexlist* const irows,   
00603                                     const Indexlist* const icols,   
00604                                     int xN,                         
00605                                     real_t alpha,                   
00606                                     const real_t *x,                
00607                                     int xLD,                        
00608                                     real_t beta,                    
00609                                     real_t *y,                      
00610                                     int yLD,                        
00611                                     BooleanType yCompr = BT_TRUE    
00612                                     ) const;
00613 
00615         virtual returnValue transTimes (    const Indexlist* const irows,   
00616                                             const Indexlist* const icols,   
00617                                             int xN,                         
00618                                             real_t alpha,                   
00619                                             const real_t *x,                
00620                                             int xLD,                        
00621                                             real_t beta,                    
00622                                             real_t *y,                      
00623                                             int yLD                         
00624                                             ) const;
00625 
00629         virtual returnValue addToDiag(  real_t alpha        
00630                                         );
00631 
00634         sparse_int_t *createDiagInfo();
00635 
00642         virtual real_t* full() const;
00643 
00646         virtual returnValue print(  const char* name = 0    
00647                                     ) const;
00648 
00649 
00650     protected:
00651         int nRows;          
00652         int nCols;          
00653         sparse_int_t *ir;   
00654         sparse_int_t *jc;   
00655         sparse_int_t *jd;   
00656         real_t *val;        
00657 };
00658 
00659 
00669 class SparseMatrixRow : public virtual Matrix
00670 {
00671     public:
00673         SparseMatrixRow( );
00674 
00676         SparseMatrixRow(    int nr,             
00677                             int nc,             
00678                             sparse_int_t *r,    
00679                             sparse_int_t *c,    
00680                             real_t *v           
00681                             );
00682 
00684         SparseMatrixRow(    int nr,                 
00685                             int nc,                 
00686                             int ld,                 
00687                             const real_t * const v  
00688                             );
00689 
00691         virtual ~SparseMatrixRow( );
00692 
00694         virtual void free( );
00695 
00698         virtual Matrix *duplicate( ) const;
00699 
00702         virtual real_t diag(    int i           
00703                                 ) const;
00704 
00708         virtual BooleanType isDiag( ) const;
00709 
00710         
00714         virtual real_t getNorm( int type = 2    
00715                                 ) const;
00716 
00720         virtual real_t getRowNorm(  int rNum,       
00721                                     int type = 2    
00722                                     ) const;
00723 
00725         virtual returnValue getRow (    int rNum,                       
00726                                         const Indexlist* const icols,   
00727                                         real_t alpha,                   
00728                                         real_t *row                     
00729                                         ) const;
00730 
00732         virtual returnValue getCol (    int cNum,                       
00733                                         const Indexlist* const irows,   
00734                                         real_t alpha,                   
00735                                         real_t *col                     
00736                                         ) const;
00737 
00739         virtual returnValue times(  int xN,                 
00740                                     real_t alpha,           
00741                                     const real_t *x,        
00742                                     int xLD,                
00743                                     real_t beta,            
00744                                     real_t *y,              
00745                                     int yLD                 
00746                                     ) const;
00747 
00749         virtual returnValue transTimes( int xN,             
00750                                         real_t alpha,       
00751                                         const real_t *x,    
00752                                         int xLD,            
00753                                         real_t beta,        
00754                                         real_t *y,          
00755                                         int yLD             
00756                                         ) const;
00757 
00759         virtual returnValue times(  const Indexlist* const irows,   
00760                                     const Indexlist* const icols,   
00761                                     int xN,                         
00762                                     real_t alpha,                   
00763                                     const real_t *x,                
00764                                     int xLD,                        
00765                                     real_t beta,                    
00766                                     real_t *y,                      
00767                                     int yLD,                        
00768                                     BooleanType yCompr = BT_TRUE    
00769                                     ) const;
00770 
00772         virtual returnValue transTimes( const Indexlist* const irows,   
00773                                         const Indexlist* const icols,   
00774                                         int xN,                         
00775                                         real_t alpha,                   
00776                                         const real_t *x,                
00777                                         int xLD,                        
00778                                         real_t beta,                    
00779                                         real_t *y,                      
00780                                         int yLD                         
00781                                         ) const;
00782 
00786         virtual returnValue addToDiag(  real_t alpha        
00787                                         );
00788 
00791         sparse_int_t *createDiagInfo();
00792 
00799         virtual real_t* full() const;
00800 
00803         virtual returnValue print(  const char* name = 0    
00804                                     ) const;
00805 
00806 
00807     protected:
00808         int nRows;          
00809         int nCols;          
00810         sparse_int_t *jr;   
00811         sparse_int_t *ic;   
00812         sparse_int_t *jd;   
00813         real_t *val;        
00814 };
00815 
00816 
00826 class SymSparseMat : public SymmetricMatrix, public SparseMatrix
00827 {
00828     public:
00830         SymSparseMat( ) : SparseMatrix( ) { };
00831 
00833         SymSparseMat(   int nr,             
00834                         int nc,             
00835                         sparse_int_t *r,    
00836                         sparse_int_t *c,    
00837                         real_t *v           
00838                         ) : SparseMatrix(nr, nc, r, c, v) { };
00839 
00841         SymSparseMat(   int nr,                 
00842                         int nc,                 
00843                         int ld,                 
00844                         const real_t * const v  
00845                         ) : SparseMatrix(nr, nc, ld, v) { };
00846 
00848         virtual ~SymSparseMat( ) { };
00849 
00852         virtual Matrix *duplicate( ) const;
00853 
00856         virtual SymmetricMatrix* duplicateSym( ) const;
00857 
00858 
00861         virtual returnValue bilinear(   const Indexlist* const icols,   
00862                                         int xN,                         
00863                                         const real_t *x,                
00864                                         int xLD,                        
00865                                         real_t *y,                      
00866                                         int yLD                         
00867                                         ) const;
00868 };
00869 
00870 
00871 END_NAMESPACE_QPOASES
00872 
00873 
00874 #endif  /* QPOASES_MATRICES_HPP */