qpOASES-3.2.0/include/qpOASES/Matrices.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of qpOASES.
3  *
4  * qpOASES -- An Implementation of the Online Active Set Strategy.
5  * Copyright (C) 2007-2015 by Hans Joachim Ferreau, Andreas Potschka,
6  * Christian Kirches et al. All rights reserved.
7  *
8  * qpOASES is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * qpOASES is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with qpOASES; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  */
23 
24 
37 #ifndef QPOASES_MATRICES_HPP
38 #define QPOASES_MATRICES_HPP
39 
40 
41 #include <qpOASES/Utils.hpp>
42 #include <qpOASES/Indexlist.hpp>
43 
44 
45 #ifdef __USE_SINGLE_PRECISION__
46 
48  #define GEMM sgemm_
49 
50  #define SYR ssyr_
51 
52  #define SYR2 ssyr2_
53 
54  #define POTRF spotrf_
55 
56 #else
57 
59  #define GEMM dgemm_
60 
61  #define SYR dsyr_
62 
63  #define SYR2 dsyr2_
64 
65  #define POTRF dpotrf_
66 
67 #endif /* __USE_SINGLE_PRECISION__ */
68 
69 
70 extern "C"
71 {
73  void dgemm_ ( const char*, const char*, const unsigned long*, const unsigned long*, const unsigned long*,
74  const double*, const double*, const unsigned long*, const double*, const unsigned long*,
75  const double*, double*, const unsigned long* );
77  void sgemm_ ( const char*, const char*, const unsigned long*, const unsigned long*, const unsigned long*,
78  const float*, const float*, const unsigned long*, const float*, const unsigned long*,
79  const float*, float*, const unsigned long* );
80 
82  void dsyr_ ( const char *, const unsigned long *, const double *, const double *,
83  const unsigned long *, double *, const unsigned long *);
85  void ssyr_ ( const char *, const unsigned long *, const float *, const float *,
86  const unsigned long *, float *, const unsigned long *);
87 
89  void dsyr2_ ( const char *, const unsigned long *, const double *, const double *,
90  const unsigned long *, const double *, const unsigned long *, double *, const unsigned long *);
92  void ssyr2_ ( const char *, const unsigned long *, const float *, const float *,
93  const unsigned long *, const float *, const unsigned long *, float *, const unsigned long *);
94 
96  void dpotrf_ ( const char *, const unsigned long *, double *, const unsigned long *, long * );
98  void spotrf_ ( const char *, const unsigned long *, float *, const unsigned long *, long * );
99 }
100 
101 
103 
104 
116 class Matrix
117 {
118  public:
121 
123  virtual ~Matrix( ) { };
124 
126  virtual void free( ) = 0;
127 
130  virtual Matrix* duplicate( ) const = 0;
131 
134  virtual real_t diag( int_t i
135  ) const = 0;
136 
140  virtual BooleanType isDiag( ) const = 0;
141 
145  virtual real_t getNorm( int_t type = 2
146  ) const = 0;
147 
151  virtual real_t getRowNorm( int_t rNum,
152  int_t type = 2
153  ) const = 0;
154 
157  virtual returnValue getRow( int_t rNum,
158  const Indexlist* const icols,
159  real_t alpha,
160  real_t *row
161  ) const = 0;
162 
165  virtual returnValue getCol( int_t cNum,
166  const Indexlist* const irows,
167  real_t alpha,
168  real_t *col
169  ) const = 0;
170 
177  const Indexlist* const irows,
178  const Indexlist* const icols,
179  int_t rowoffset,
180  int_t coloffset,
181  int_t& numNonzeros,
182  int_t* irn,
183  int_t* jcn,
184  real_t* avals,
185  BooleanType only_lower_triangular = BT_FALSE
186  ) const;
187 
195  const Indexlist* const irows,
196  int_t idx_icol,
197  int_t rowoffset,
198  int_t coloffset,
199  int_t& numNonzeros,
200  int_t* irn,
201  int_t* jcn,
202  real_t* avals,
203  BooleanType only_lower_triangular = BT_FALSE
204  ) const;
205 
212  int_t idx_row,
213  const Indexlist* const icols,
214  int_t rowoffset,
215  int_t coloffset,
216  int_t& numNonzeros,
217  int_t* irn,
218  int_t* jcn,
219  real_t* avals,
220  BooleanType only_lower_triangular = BT_FALSE
221  ) const;
222 
229  int_t irowsLength,
230  const int_t* const irowsNumber,
231  int_t icolsLength,
232  const int_t* const icolsNumber,
233  int_t rowoffset,
234  int_t coloffset,
235  int_t& numNonzeros,
236  int_t* irn,
237  int_t* jcn,
238  real_t* avals,
239  BooleanType only_lower_triangular = BT_FALSE
240  ) const = 0;
241 
244  virtual returnValue times ( int_t xN,
245  real_t alpha,
246  const real_t *x,
247  int_t xLD,
248  real_t beta,
249  real_t *y,
250  int_t yLD
251  ) const = 0;
252 
255  virtual returnValue transTimes ( int_t xN,
256  real_t alpha,
257  const real_t *x,
258  int_t xLD,
259  real_t beta,
260  real_t *y,
261  int_t yLD
262  ) const = 0;
263 
266  virtual returnValue times ( const Indexlist* const irows,
267  const Indexlist* const icols,
268  int_t xN,
269  real_t alpha,
270  const real_t *x,
271  int_t xLD,
272  real_t beta,
273  real_t *y,
274  int_t yLD,
275  BooleanType yCompr = BT_TRUE
276  ) const = 0;
277 
280  virtual returnValue transTimes ( const Indexlist* const irows,
281  const Indexlist* const icols,
282  int_t xN,
283  real_t alpha,
284  const real_t *x,
285  int_t xLD,
286  real_t beta,
287  real_t *y,
288  int_t yLD
289  ) const = 0;
290 
294  virtual returnValue addToDiag( real_t alpha
295  ) = 0;
296 
303  virtual real_t* full() const = 0;
304 
305 
308  virtual returnValue print( const char* name = 0
309  ) const = 0;
310 
313  virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const = 0;
314 
319 
322 
325 
326 
327  protected:
330 };
331 
332 
343 class SymmetricMatrix : public virtual Matrix
344 {
345  public:
348 
350  virtual ~SymmetricMatrix( ) { };
351 
354  virtual SymmetricMatrix* duplicateSym( ) const = 0;
355 
356 
359  virtual returnValue bilinear( const Indexlist* const icols,
360  int_t xN,
361  const real_t *x,
362  int_t xLD,
363  real_t *y,
364  int_t yLD
365  ) const = 0;
366 
367 };
368 
369 
379 class DenseMatrix : public virtual Matrix
380 {
381  public:
383  DenseMatrix( ) : nRows(0), nCols(0), leaDim(0), val(0) { };
384 
389  int_t n,
390  int_t lD,
391  real_t *v
392  ) : nRows(m), nCols(n), leaDim(lD), val(v) {}
393 
394 
396  virtual ~DenseMatrix( );
397 
399  virtual void free( );
400 
403  virtual Matrix *duplicate( ) const;
404 
407  virtual real_t diag( int_t i
408  ) const;
409 
413  virtual BooleanType isDiag( ) const;
414 
418  virtual real_t getNorm( int_t type = 2
419  ) const;
420 
424  virtual real_t getRowNorm( int_t rNum,
425  int_t type = 2
426  ) const;
427 
430  virtual returnValue getRow( int_t rNum,
431  const Indexlist* const icols,
432  real_t alpha,
433  real_t *row
434  ) const;
435 
438  virtual returnValue getCol(
439  int_t cNum,
440  const Indexlist* const irows,
441  real_t alpha,
442  real_t *col
443  ) const;
444 
451  int_t irowsLength,
452  const int_t* const irowsNumber,
453  int_t icolsLength,
454  const int_t* const icolsNumber,
455  int_t rowoffset,
456  int_t coloffset,
457  int_t& numNonzeros,
458  int_t* irn,
459  int_t* jcn,
460  real_t* avals,
461  BooleanType only_lower_triangular = BT_FALSE
462  ) const;
463 
464 
467  virtual returnValue times( int_t xN,
468  real_t alpha,
469  const real_t *x,
470  int_t xLD,
471  real_t beta,
472  real_t *y,
473  int_t yLD
474  ) const;
475 
478  virtual returnValue transTimes( int_t xN,
479  real_t alpha,
480  const real_t *x,
481  int_t xLD,
482  real_t beta,
483  real_t *y,
484  int_t yLD
485  ) const;
486 
489  virtual returnValue times( const Indexlist* const irows,
490  const Indexlist* const icols,
491  int_t xN,
492  real_t alpha,
493  const real_t *x,
494  int_t xLD,
495  real_t beta,
496  real_t *y,
497  int_t yLD,
498  BooleanType yCompr = BT_TRUE
499  ) const;
500 
503  virtual returnValue transTimes( const Indexlist* const irows,
504  const Indexlist* const icols,
505  int_t xN,
506  real_t alpha,
507  const real_t *x,
508  int_t xLD,
509  real_t beta,
510  real_t *y,
511  int_t yLD
512  ) const;
513 
517  virtual returnValue addToDiag( real_t alpha
518  );
519 
520 
527  virtual real_t* full() const;
528 
529 
532  virtual returnValue print( const char* name = 0
533  ) const;
534 
537  virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const;
538 
539  protected:
543  real_t *val;
544 };
545 
546 
556 class SymDenseMat : public DenseMatrix, public SymmetricMatrix
557 {
558  public:
561 
564  int_t n,
565  int_t lD,
566  real_t *v
567  ) : DenseMatrix(m, n, lD, v) { };
568 
570  virtual ~SymDenseMat() { };
571 
574  virtual Matrix *duplicate( ) const;
575 
578  virtual SymmetricMatrix* duplicateSym( ) const;
579 
580 
583  virtual returnValue bilinear( const Indexlist* const icols,
584  int_t xN,
585  const real_t *x,
586  int_t xLD,
587  real_t *y,
588  int_t yLD
589  ) const;
590 };
591 
592 
602 class SparseMatrix : public virtual Matrix
603 {
604  public:
606  SparseMatrix( );
607 
609  SparseMatrix( int_t nr,
610  int_t nc,
611  sparse_int_t *r,
612  sparse_int_t *c,
613  real_t *v
614  );
615 
617  SparseMatrix( int_t nr,
618  int_t nc,
619  int_t ld,
620  const real_t * const v
621  );
622 
624  virtual ~SparseMatrix( );
625 
627  virtual void free( );
628 
631  virtual Matrix *duplicate( ) const;
632 
635  virtual real_t diag( int_t i
636  ) const;
637 
641  virtual BooleanType isDiag( ) const;
642 
643 
647  virtual real_t getNorm( int_t type = 2
648  ) const;
649 
653  virtual real_t getRowNorm( int_t rNum,
654  int_t type = 2
655  ) const;
656 
658  virtual returnValue getRow( int_t rNum,
659  const Indexlist* const icols,
660  real_t alpha,
661  real_t *row
662  ) const;
663 
665  virtual returnValue getCol( int_t cNum,
666  const Indexlist* const irows,
667  real_t alpha,
668  real_t *col
669  ) const;
670 
677  int_t irowsLength,
678  const int_t* const irowsNumber,
679  int_t icolsLength,
680  const int_t* const icolsNumber,
681  int_t rowoffset,
682  int_t coloffset,
683  int_t& numNonzeros,
684  int_t* irn,
685  int_t* jcn,
686  real_t* avals,
687  BooleanType only_lower_triangular = BT_FALSE
688  ) const;
689 
691  virtual returnValue times ( int_t xN,
692  real_t alpha,
693  const real_t *x,
694  int_t xLD,
695  real_t beta,
696  real_t *y,
697  int_t yLD
698  ) const;
699 
701  virtual returnValue transTimes ( int_t xN,
702  real_t alpha,
703  const real_t *x,
704  int_t xLD,
705  real_t beta,
706  real_t *y,
707  int_t yLD
708  ) const;
709 
711  virtual returnValue times ( const Indexlist* const irows,
712  const Indexlist* const icols,
713  int_t xN,
714  real_t alpha,
715  const real_t *x,
716  int_t xLD,
717  real_t beta,
718  real_t *y,
719  int_t yLD,
720  BooleanType yCompr = BT_TRUE
721  ) const;
722 
724  virtual returnValue transTimes ( const Indexlist* const irows,
725  const Indexlist* const icols,
726  int_t xN,
727  real_t alpha,
728  const real_t *x,
729  int_t xLD,
730  real_t beta,
731  real_t *y,
732  int_t yLD
733  ) const;
734 
738  virtual returnValue addToDiag( real_t alpha
739  );
740 
743  sparse_int_t *createDiagInfo();
744 
751  virtual real_t* full() const;
752 
755  virtual returnValue print( const char* name = 0
756  ) const;
757 
760  virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const;
761 
762 
763  protected:
769  real_t *val;
770 };
771 
772 
782 class SparseMatrixRow : public virtual Matrix
783 {
784  public:
786  SparseMatrixRow( );
787 
789  SparseMatrixRow( int_t nr,
790  int_t nc,
791  sparse_int_t *r,
792  sparse_int_t *c,
793  real_t *v
794  );
795 
797  SparseMatrixRow( int_t nr,
798  int_t nc,
799  int_t ld,
800  const real_t * const v
801  );
802 
804  virtual ~SparseMatrixRow( );
805 
807  virtual void free( );
808 
811  virtual Matrix *duplicate( ) const;
812 
815  virtual real_t diag( int_t i
816  ) const;
817 
821  virtual BooleanType isDiag( ) const;
822 
823 
827  virtual real_t getNorm( int_t type = 2
828  ) const;
829 
833  virtual real_t getRowNorm( int_t rNum,
834  int_t type = 2
835  ) const;
836 
838  virtual returnValue getRow ( int_t rNum,
839  const Indexlist* const icols,
840  real_t alpha,
841  real_t *row
842  ) const;
843 
845  virtual returnValue getCol ( int_t cNum,
846  const Indexlist* const irows,
847  real_t alpha,
848  real_t *col
849  ) const;
850 
857  int_t irowsLength,
858  const int_t* const irowsNumber,
859  int_t icolsLength,
860  const int_t* const icolsNumber,
861  int_t rowoffset,
862  int_t coloffset,
863  int_t& numNonzeros,
864  int_t* irn,
865  int_t* jcn,
866  real_t* avals,
867  BooleanType only_lower_triangular = BT_FALSE
868  ) const;
869 
871  virtual returnValue times( int_t xN,
872  real_t alpha,
873  const real_t *x,
874  int_t xLD,
875  real_t beta,
876  real_t *y,
877  int_t yLD
878  ) const;
879 
881  virtual returnValue transTimes( int_t xN,
882  real_t alpha,
883  const real_t *x,
884  int_t xLD,
885  real_t beta,
886  real_t *y,
887  int_t yLD
888  ) const;
889 
891  virtual returnValue times( const Indexlist* const irows,
892  const Indexlist* const icols,
893  int_t xN,
894  real_t alpha,
895  const real_t *x,
896  int_t xLD,
897  real_t beta,
898  real_t *y,
899  int_t yLD,
900  BooleanType yCompr = BT_TRUE
901  ) const;
902 
904  virtual returnValue transTimes( const Indexlist* const irows,
905  const Indexlist* const icols,
906  int_t xN,
907  real_t alpha,
908  const real_t *x,
909  int_t xLD,
910  real_t beta,
911  real_t *y,
912  int_t yLD
913  ) const;
914 
918  virtual returnValue addToDiag( real_t alpha
919  );
920 
923  sparse_int_t *createDiagInfo();
924 
931  virtual real_t* full() const;
932 
935  virtual returnValue print( const char* name = 0
936  ) const;
937 
940  virtual returnValue writeToFile( FILE* output_file, const char* prefix ) const;
941 
942  protected:
949 };
950 
951 
961 class SymSparseMat : public SymmetricMatrix, public SparseMatrix
962 {
963  public:
966 
969  int_t nc,
970  sparse_int_t *r,
971  sparse_int_t *c,
972  real_t *v
973  ) : SparseMatrix(nr, nc, r, c, v) { };
974 
977  int_t nc,
978  int_t ld,
979  const real_t * const v
980  ) : SparseMatrix(nr, nc, ld, v) { };
981 
983  virtual ~SymSparseMat( ) { };
984 
987  virtual Matrix *duplicate( ) const;
988 
991  virtual SymmetricMatrix* duplicateSym( ) const;
992 
993 
996  virtual returnValue bilinear( const Indexlist* const icols,
997  int_t xN,
998  const real_t *x,
999  int_t xLD,
1000  real_t *y,
1001  int_t yLD
1002  ) const;
1003 };
1004 
1005 
1007 
1008 
1009 #endif /* QPOASES_MATRICES_HPP */
virtual returnValue getCol(int cNum, const Indexlist *const irows, real_t alpha, real_t *col) const =0
virtual real_t getNorm(int_t type=2) const =0
Interfaces matrix-vector operations tailored to symmetric sparse matrices.
virtual real_t * full() const =0
virtual returnValue print() const =0
virtual Matrix * duplicate() const =0
SymDenseMat(int_t m, int_t n, int_t lD, real_t *v)
void dpotrf_(const char *, const unsigned long *, double *, const unsigned long *, long *)
virtual BooleanType isDiag() const =0
Allows to pass back messages to the calling function.
Interfaces matrix-vector operations tailored to symmetric dense matrices.
void dgemm_(const char *, const char *, const unsigned long *, const unsigned long *, const unsigned long *, const double *, const double *, const unsigned long *, const double *, const unsigned long *, const double *, double *, const unsigned long *)
virtual returnValue transTimes(int xN, real_t alpha, const real_t *x, int xLD, real_t beta, real_t *y, int yLD) const =0
virtual returnValue getSparseSubmatrix(const Indexlist *const irows, const Indexlist *const icols, int_t rowoffset, int_t coloffset, int_t &numNonzeros, int_t *irn, int_t *jcn, real_t *avals, BooleanType only_lower_triangular=BT_FALSE) const
void ssyr_(const char *, const unsigned long *, const float *, const float *, const unsigned long *, float *, const unsigned long *)
void ssyr2_(const char *, const unsigned long *, const float *, const float *, const unsigned long *, const float *, const unsigned long *, float *, const unsigned long *)
Interfaces matrix-vector operations tailored to general sparse matrices.
void dsyr_(const char *, const unsigned long *, const double *, const double *, const unsigned long *, double *, const unsigned long *)
Interfaces matrix-vector operations tailored to general dense matrices.
virtual real_t getRowNorm(int_t rNum, int_t type=2) const =0
virtual returnValue writeToFile(FILE *output_file, const char *prefix) const =0
virtual returnValue times(int xN, real_t alpha, const real_t *x, int xLD, real_t beta, real_t *y, int yLD) const =0
Interfaces matrix-vector operations tailored to general sparse matrices.
void dsyr2_(const char *, const unsigned long *, const double *, const double *, const unsigned long *, const double *, const unsigned long *, double *, const unsigned long *)
void sgemm_(const char *, const char *, const unsigned long *, const unsigned long *, const unsigned long *, const float *, const float *, const unsigned long *, const float *, const unsigned long *, const float *, float *, const unsigned long *)
virtual void free()=0
Abstract base class for interfacing tailored matrix-vector operations.
void spotrf_(const char *, const unsigned long *, float *, const unsigned long *, long *)
#define v
BooleanType needToFreeMemory() const
DenseMatrix(int_t m, int_t n, int_t lD, real_t *v)
#define BT_TRUE
Definition: acado_types.hpp:47
virtual real_t diag(int i) const =0
RowXpr row(Index i)
Definition: BlockMethods.h:725
SymSparseMat(int_t nr, int_t nc, int_t ld, const real_t *const v)
virtual returnValue addToDiag(real_t alpha)=0
#define BT_FALSE
Definition: acado_types.hpp:49
ColXpr col(Index i)
Definition: BlockMethods.h:708
double real_t
Definition: AD_test.c:10
virtual returnValue getRow(int rNum, const Indexlist *const icols, real_t alpha, real_t *row) const =0
SymSparseMat(int_t nr, int_t nc, sparse_int_t *r, sparse_int_t *c, real_t *v)
Abstract base class for interfacing matrix-vector operations tailored to symmetric matrices...


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:51