Home | History | Annotate | Download | only in plugins
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      5 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #ifndef EIGEN_BLOCKMETHODS_H
     12 #define EIGEN_BLOCKMETHODS_H
     13 
     14 #ifndef EIGEN_PARSED_BY_DOXYGEN
     15 
     16 /** \internal expression type of a column */
     17 typedef Block<Derived, internal::traits<Derived>::RowsAtCompileTime, 1, !IsRowMajor> ColXpr;
     18 typedef const Block<const Derived, internal::traits<Derived>::RowsAtCompileTime, 1, !IsRowMajor> ConstColXpr;
     19 /** \internal expression type of a row */
     20 typedef Block<Derived, 1, internal::traits<Derived>::ColsAtCompileTime, IsRowMajor> RowXpr;
     21 typedef const Block<const Derived, 1, internal::traits<Derived>::ColsAtCompileTime, IsRowMajor> ConstRowXpr;
     22 /** \internal expression type of a block of whole columns */
     23 typedef Block<Derived, internal::traits<Derived>::RowsAtCompileTime, Dynamic, !IsRowMajor> ColsBlockXpr;
     24 typedef const Block<const Derived, internal::traits<Derived>::RowsAtCompileTime, Dynamic, !IsRowMajor> ConstColsBlockXpr;
     25 /** \internal expression type of a block of whole rows */
     26 typedef Block<Derived, Dynamic, internal::traits<Derived>::ColsAtCompileTime, IsRowMajor> RowsBlockXpr;
     27 typedef const Block<const Derived, Dynamic, internal::traits<Derived>::ColsAtCompileTime, IsRowMajor> ConstRowsBlockXpr;
     28 /** \internal expression type of a block of whole columns */
     29 template<int N> struct NColsBlockXpr { typedef Block<Derived, internal::traits<Derived>::RowsAtCompileTime, N, !IsRowMajor> Type; };
     30 template<int N> struct ConstNColsBlockXpr { typedef const Block<const Derived, internal::traits<Derived>::RowsAtCompileTime, N, !IsRowMajor> Type; };
     31 /** \internal expression type of a block of whole rows */
     32 template<int N> struct NRowsBlockXpr { typedef Block<Derived, N, internal::traits<Derived>::ColsAtCompileTime, IsRowMajor> Type; };
     33 template<int N> struct ConstNRowsBlockXpr { typedef const Block<const Derived, N, internal::traits<Derived>::ColsAtCompileTime, IsRowMajor> Type; };
     34 
     35 
     36 #endif // not EIGEN_PARSED_BY_DOXYGEN
     37 
     38 /** \returns a dynamic-size expression of a block in *this.
     39   *
     40   * \param startRow the first row in the block
     41   * \param startCol the first column in the block
     42   * \param blockRows the number of rows in the block
     43   * \param blockCols the number of columns in the block
     44   *
     45   * Example: \include MatrixBase_block_int_int_int_int.cpp
     46   * Output: \verbinclude MatrixBase_block_int_int_int_int.out
     47   *
     48   * \note Even though the returned expression has dynamic size, in the case
     49   * when it is applied to a fixed-size matrix, it inherits a fixed maximal size,
     50   * which means that evaluating it does not cause a dynamic memory allocation.
     51   *
     52   * \sa class Block, block(Index,Index)
     53   */
     54 inline Block<Derived> block(Index startRow, Index startCol, Index blockRows, Index blockCols)
     55 {
     56   return Block<Derived>(derived(), startRow, startCol, blockRows, blockCols);
     57 }
     58 
     59 /** This is the const version of block(Index,Index,Index,Index). */
     60 inline const Block<const Derived> block(Index startRow, Index startCol, Index blockRows, Index blockCols) const
     61 {
     62   return Block<const Derived>(derived(), startRow, startCol, blockRows, blockCols);
     63 }
     64 
     65 
     66 
     67 
     68 /** \returns a dynamic-size expression of a top-right corner of *this.
     69   *
     70   * \param cRows the number of rows in the corner
     71   * \param cCols the number of columns in the corner
     72   *
     73   * Example: \include MatrixBase_topRightCorner_int_int.cpp
     74   * Output: \verbinclude MatrixBase_topRightCorner_int_int.out
     75   *
     76   * \sa class Block, block(Index,Index,Index,Index)
     77   */
     78 inline Block<Derived> topRightCorner(Index cRows, Index cCols)
     79 {
     80   return Block<Derived>(derived(), 0, cols() - cCols, cRows, cCols);
     81 }
     82 
     83 /** This is the const version of topRightCorner(Index, Index).*/
     84 inline const Block<const Derived> topRightCorner(Index cRows, Index cCols) const
     85 {
     86   return Block<const Derived>(derived(), 0, cols() - cCols, cRows, cCols);
     87 }
     88 
     89 /** \returns an expression of a fixed-size top-right corner of *this.
     90   *
     91   * The template parameters CRows and CCols are the number of rows and columns in the corner.
     92   *
     93   * Example: \include MatrixBase_template_int_int_topRightCorner.cpp
     94   * Output: \verbinclude MatrixBase_template_int_int_topRightCorner.out
     95   *
     96   * \sa class Block, block(Index,Index,Index,Index)
     97   */
     98 template<int CRows, int CCols>
     99 inline Block<Derived, CRows, CCols> topRightCorner()
    100 {
    101   return Block<Derived, CRows, CCols>(derived(), 0, cols() - CCols);
    102 }
    103 
    104 /** This is the const version of topRightCorner<int, int>().*/
    105 template<int CRows, int CCols>
    106 inline const Block<const Derived, CRows, CCols> topRightCorner() const
    107 {
    108   return Block<const Derived, CRows, CCols>(derived(), 0, cols() - CCols);
    109 }
    110 
    111 
    112 
    113 
    114 /** \returns a dynamic-size expression of a top-left corner of *this.
    115   *
    116   * \param cRows the number of rows in the corner
    117   * \param cCols the number of columns in the corner
    118   *
    119   * Example: \include MatrixBase_topLeftCorner_int_int.cpp
    120   * Output: \verbinclude MatrixBase_topLeftCorner_int_int.out
    121   *
    122   * \sa class Block, block(Index,Index,Index,Index)
    123   */
    124 inline Block<Derived> topLeftCorner(Index cRows, Index cCols)
    125 {
    126   return Block<Derived>(derived(), 0, 0, cRows, cCols);
    127 }
    128 
    129 /** This is the const version of topLeftCorner(Index, Index).*/
    130 inline const Block<const Derived> topLeftCorner(Index cRows, Index cCols) const
    131 {
    132   return Block<const Derived>(derived(), 0, 0, cRows, cCols);
    133 }
    134 
    135 /** \returns an expression of a fixed-size top-left corner of *this.
    136   *
    137   * The template parameters CRows and CCols are the number of rows and columns in the corner.
    138   *
    139   * Example: \include MatrixBase_template_int_int_topLeftCorner.cpp
    140   * Output: \verbinclude MatrixBase_template_int_int_topLeftCorner.out
    141   *
    142   * \sa class Block, block(Index,Index,Index,Index)
    143   */
    144 template<int CRows, int CCols>
    145 inline Block<Derived, CRows, CCols> topLeftCorner()
    146 {
    147   return Block<Derived, CRows, CCols>(derived(), 0, 0);
    148 }
    149 
    150 /** This is the const version of topLeftCorner<int, int>().*/
    151 template<int CRows, int CCols>
    152 inline const Block<const Derived, CRows, CCols> topLeftCorner() const
    153 {
    154   return Block<const Derived, CRows, CCols>(derived(), 0, 0);
    155 }
    156 
    157 
    158 
    159 /** \returns a dynamic-size expression of a bottom-right corner of *this.
    160   *
    161   * \param cRows the number of rows in the corner
    162   * \param cCols the number of columns in the corner
    163   *
    164   * Example: \include MatrixBase_bottomRightCorner_int_int.cpp
    165   * Output: \verbinclude MatrixBase_bottomRightCorner_int_int.out
    166   *
    167   * \sa class Block, block(Index,Index,Index,Index)
    168   */
    169 inline Block<Derived> bottomRightCorner(Index cRows, Index cCols)
    170 {
    171   return Block<Derived>(derived(), rows() - cRows, cols() - cCols, cRows, cCols);
    172 }
    173 
    174 /** This is the const version of bottomRightCorner(Index, Index).*/
    175 inline const Block<const Derived> bottomRightCorner(Index cRows, Index cCols) const
    176 {
    177   return Block<const Derived>(derived(), rows() - cRows, cols() - cCols, cRows, cCols);
    178 }
    179 
    180 /** \returns an expression of a fixed-size bottom-right corner of *this.
    181   *
    182   * The template parameters CRows and CCols are the number of rows and columns in the corner.
    183   *
    184   * Example: \include MatrixBase_template_int_int_bottomRightCorner.cpp
    185   * Output: \verbinclude MatrixBase_template_int_int_bottomRightCorner.out
    186   *
    187   * \sa class Block, block(Index,Index,Index,Index)
    188   */
    189 template<int CRows, int CCols>
    190 inline Block<Derived, CRows, CCols> bottomRightCorner()
    191 {
    192   return Block<Derived, CRows, CCols>(derived(), rows() - CRows, cols() - CCols);
    193 }
    194 
    195 /** This is the const version of bottomRightCorner<int, int>().*/
    196 template<int CRows, int CCols>
    197 inline const Block<const Derived, CRows, CCols> bottomRightCorner() const
    198 {
    199   return Block<const Derived, CRows, CCols>(derived(), rows() - CRows, cols() - CCols);
    200 }
    201 
    202 
    203 
    204 /** \returns a dynamic-size expression of a bottom-left corner of *this.
    205   *
    206   * \param cRows the number of rows in the corner
    207   * \param cCols the number of columns in the corner
    208   *
    209   * Example: \include MatrixBase_bottomLeftCorner_int_int.cpp
    210   * Output: \verbinclude MatrixBase_bottomLeftCorner_int_int.out
    211   *
    212   * \sa class Block, block(Index,Index,Index,Index)
    213   */
    214 inline Block<Derived> bottomLeftCorner(Index cRows, Index cCols)
    215 {
    216   return Block<Derived>(derived(), rows() - cRows, 0, cRows, cCols);
    217 }
    218 
    219 /** This is the const version of bottomLeftCorner(Index, Index).*/
    220 inline const Block<const Derived> bottomLeftCorner(Index cRows, Index cCols) const
    221 {
    222   return Block<const Derived>(derived(), rows() - cRows, 0, cRows, cCols);
    223 }
    224 
    225 /** \returns an expression of a fixed-size bottom-left corner of *this.
    226   *
    227   * The template parameters CRows and CCols are the number of rows and columns in the corner.
    228   *
    229   * Example: \include MatrixBase_template_int_int_bottomLeftCorner.cpp
    230   * Output: \verbinclude MatrixBase_template_int_int_bottomLeftCorner.out
    231   *
    232   * \sa class Block, block(Index,Index,Index,Index)
    233   */
    234 template<int CRows, int CCols>
    235 inline Block<Derived, CRows, CCols> bottomLeftCorner()
    236 {
    237   return Block<Derived, CRows, CCols>(derived(), rows() - CRows, 0);
    238 }
    239 
    240 /** This is the const version of bottomLeftCorner<int, int>().*/
    241 template<int CRows, int CCols>
    242 inline const Block<const Derived, CRows, CCols> bottomLeftCorner() const
    243 {
    244   return Block<const Derived, CRows, CCols>(derived(), rows() - CRows, 0);
    245 }
    246 
    247 
    248 
    249 /** \returns a block consisting of the top rows of *this.
    250   *
    251   * \param n the number of rows in the block
    252   *
    253   * Example: \include MatrixBase_topRows_int.cpp
    254   * Output: \verbinclude MatrixBase_topRows_int.out
    255   *
    256   * \sa class Block, block(Index,Index,Index,Index)
    257   */
    258 inline RowsBlockXpr topRows(Index n)
    259 {
    260   return RowsBlockXpr(derived(), 0, 0, n, cols());
    261 }
    262 
    263 /** This is the const version of topRows(Index).*/
    264 inline ConstRowsBlockXpr topRows(Index n) const
    265 {
    266   return ConstRowsBlockXpr(derived(), 0, 0, n, cols());
    267 }
    268 
    269 /** \returns a block consisting of the top rows of *this.
    270   *
    271   * \tparam N the number of rows in the block
    272   *
    273   * Example: \include MatrixBase_template_int_topRows.cpp
    274   * Output: \verbinclude MatrixBase_template_int_topRows.out
    275   *
    276   * \sa class Block, block(Index,Index,Index,Index)
    277   */
    278 template<int N>
    279 inline typename NRowsBlockXpr<N>::Type topRows()
    280 {
    281   return typename NRowsBlockXpr<N>::Type(derived(), 0, 0, N, cols());
    282 }
    283 
    284 /** This is the const version of topRows<int>().*/
    285 template<int N>
    286 inline typename ConstNRowsBlockXpr<N>::Type topRows() const
    287 {
    288   return typename ConstNRowsBlockXpr<N>::Type(derived(), 0, 0, N, cols());
    289 }
    290 
    291 
    292 
    293 /** \returns a block consisting of the bottom rows of *this.
    294   *
    295   * \param n the number of rows in the block
    296   *
    297   * Example: \include MatrixBase_bottomRows_int.cpp
    298   * Output: \verbinclude MatrixBase_bottomRows_int.out
    299   *
    300   * \sa class Block, block(Index,Index,Index,Index)
    301   */
    302 inline RowsBlockXpr bottomRows(Index n)
    303 {
    304   return RowsBlockXpr(derived(), rows() - n, 0, n, cols());
    305 }
    306 
    307 /** This is the const version of bottomRows(Index).*/
    308 inline ConstRowsBlockXpr bottomRows(Index n) const
    309 {
    310   return ConstRowsBlockXpr(derived(), rows() - n, 0, n, cols());
    311 }
    312 
    313 /** \returns a block consisting of the bottom rows of *this.
    314   *
    315   * \tparam N the number of rows in the block
    316   *
    317   * Example: \include MatrixBase_template_int_bottomRows.cpp
    318   * Output: \verbinclude MatrixBase_template_int_bottomRows.out
    319   *
    320   * \sa class Block, block(Index,Index,Index,Index)
    321   */
    322 template<int N>
    323 inline typename NRowsBlockXpr<N>::Type bottomRows()
    324 {
    325   return typename NRowsBlockXpr<N>::Type(derived(), rows() - N, 0, N, cols());
    326 }
    327 
    328 /** This is the const version of bottomRows<int>().*/
    329 template<int N>
    330 inline typename ConstNRowsBlockXpr<N>::Type bottomRows() const
    331 {
    332   return typename ConstNRowsBlockXpr<N>::Type(derived(), rows() - N, 0, N, cols());
    333 }
    334 
    335 
    336 
    337 /** \returns a block consisting of a range of rows of *this.
    338   *
    339   * \param startRow the index of the first row in the block
    340   * \param numRows the number of rows in the block
    341   *
    342   * Example: \include DenseBase_middleRows_int.cpp
    343   * Output: \verbinclude DenseBase_middleRows_int.out
    344   *
    345   * \sa class Block, block(Index,Index,Index,Index)
    346   */
    347 inline RowsBlockXpr middleRows(Index startRow, Index numRows)
    348 {
    349   return RowsBlockXpr(derived(), startRow, 0, numRows, cols());
    350 }
    351 
    352 /** This is the const version of middleRows(Index,Index).*/
    353 inline ConstRowsBlockXpr middleRows(Index startRow, Index numRows) const
    354 {
    355   return ConstRowsBlockXpr(derived(), startRow, 0, numRows, cols());
    356 }
    357 
    358 /** \returns a block consisting of a range of rows of *this.
    359   *
    360   * \tparam N the number of rows in the block
    361   * \param startRow the index of the first row in the block
    362   *
    363   * Example: \include DenseBase_template_int_middleRows.cpp
    364   * Output: \verbinclude DenseBase_template_int_middleRows.out
    365   *
    366   * \sa class Block, block(Index,Index,Index,Index)
    367   */
    368 template<int N>
    369 inline typename NRowsBlockXpr<N>::Type middleRows(Index startRow)
    370 {
    371   return typename NRowsBlockXpr<N>::Type(derived(), startRow, 0, N, cols());
    372 }
    373 
    374 /** This is the const version of middleRows<int>().*/
    375 template<int N>
    376 inline typename ConstNRowsBlockXpr<N>::Type middleRows(Index startRow) const
    377 {
    378   return typename ConstNRowsBlockXpr<N>::Type(derived(), startRow, 0, N, cols());
    379 }
    380 
    381 
    382 
    383 /** \returns a block consisting of the left columns of *this.
    384   *
    385   * \param n the number of columns in the block
    386   *
    387   * Example: \include MatrixBase_leftCols_int.cpp
    388   * Output: \verbinclude MatrixBase_leftCols_int.out
    389   *
    390   * \sa class Block, block(Index,Index,Index,Index)
    391   */
    392 inline ColsBlockXpr leftCols(Index n)
    393 {
    394   return ColsBlockXpr(derived(), 0, 0, rows(), n);
    395 }
    396 
    397 /** This is the const version of leftCols(Index).*/
    398 inline ConstColsBlockXpr leftCols(Index n) const
    399 {
    400   return ConstColsBlockXpr(derived(), 0, 0, rows(), n);
    401 }
    402 
    403 /** \returns a block consisting of the left columns of *this.
    404   *
    405   * \tparam N the number of columns in the block
    406   *
    407   * Example: \include MatrixBase_template_int_leftCols.cpp
    408   * Output: \verbinclude MatrixBase_template_int_leftCols.out
    409   *
    410   * \sa class Block, block(Index,Index,Index,Index)
    411   */
    412 template<int N>
    413 inline typename NColsBlockXpr<N>::Type leftCols()
    414 {
    415   return typename NColsBlockXpr<N>::Type(derived(), 0, 0, rows(), N);
    416 }
    417 
    418 /** This is the const version of leftCols<int>().*/
    419 template<int N>
    420 inline typename ConstNColsBlockXpr<N>::Type leftCols() const
    421 {
    422   return typename ConstNColsBlockXpr<N>::Type(derived(), 0, 0, rows(), N);
    423 }
    424 
    425 
    426 
    427 /** \returns a block consisting of the right columns of *this.
    428   *
    429   * \param n the number of columns in the block
    430   *
    431   * Example: \include MatrixBase_rightCols_int.cpp
    432   * Output: \verbinclude MatrixBase_rightCols_int.out
    433   *
    434   * \sa class Block, block(Index,Index,Index,Index)
    435   */
    436 inline ColsBlockXpr rightCols(Index n)
    437 {
    438   return ColsBlockXpr(derived(), 0, cols() - n, rows(), n);
    439 }
    440 
    441 /** This is the const version of rightCols(Index).*/
    442 inline ConstColsBlockXpr rightCols(Index n) const
    443 {
    444   return ConstColsBlockXpr(derived(), 0, cols() - n, rows(), n);
    445 }
    446 
    447 /** \returns a block consisting of the right columns of *this.
    448   *
    449   * \tparam N the number of columns in the block
    450   *
    451   * Example: \include MatrixBase_template_int_rightCols.cpp
    452   * Output: \verbinclude MatrixBase_template_int_rightCols.out
    453   *
    454   * \sa class Block, block(Index,Index,Index,Index)
    455   */
    456 template<int N>
    457 inline typename NColsBlockXpr<N>::Type rightCols()
    458 {
    459   return typename NColsBlockXpr<N>::Type(derived(), 0, cols() - N, rows(), N);
    460 }
    461 
    462 /** This is the const version of rightCols<int>().*/
    463 template<int N>
    464 inline typename ConstNColsBlockXpr<N>::Type rightCols() const
    465 {
    466   return typename ConstNColsBlockXpr<N>::Type(derived(), 0, cols() - N, rows(), N);
    467 }
    468 
    469 
    470 
    471 /** \returns a block consisting of a range of columns of *this.
    472   *
    473   * \param startCol the index of the first column in the block
    474   * \param numCols the number of columns in the block
    475   *
    476   * Example: \include DenseBase_middleCols_int.cpp
    477   * Output: \verbinclude DenseBase_middleCols_int.out
    478   *
    479   * \sa class Block, block(Index,Index,Index,Index)
    480   */
    481 inline ColsBlockXpr middleCols(Index startCol, Index numCols)
    482 {
    483   return ColsBlockXpr(derived(), 0, startCol, rows(), numCols);
    484 }
    485 
    486 /** This is the const version of middleCols(Index,Index).*/
    487 inline ConstColsBlockXpr middleCols(Index startCol, Index numCols) const
    488 {
    489   return ConstColsBlockXpr(derived(), 0, startCol, rows(), numCols);
    490 }
    491 
    492 /** \returns a block consisting of a range of columns of *this.
    493   *
    494   * \tparam N the number of columns in the block
    495   * \param startCol the index of the first column in the block
    496   *
    497   * Example: \include DenseBase_template_int_middleCols.cpp
    498   * Output: \verbinclude DenseBase_template_int_middleCols.out
    499   *
    500   * \sa class Block, block(Index,Index,Index,Index)
    501   */
    502 template<int N>
    503 inline typename NColsBlockXpr<N>::Type middleCols(Index startCol)
    504 {
    505   return typename NColsBlockXpr<N>::Type(derived(), 0, startCol, rows(), N);
    506 }
    507 
    508 /** This is the const version of middleCols<int>().*/
    509 template<int N>
    510 inline typename ConstNColsBlockXpr<N>::Type middleCols(Index startCol) const
    511 {
    512   return typename ConstNColsBlockXpr<N>::Type(derived(), 0, startCol, rows(), N);
    513 }
    514 
    515 
    516 
    517 /** \returns a fixed-size expression of a block in *this.
    518   *
    519   * The template parameters \a BlockRows and \a BlockCols are the number of
    520   * rows and columns in the block.
    521   *
    522   * \param startRow the first row in the block
    523   * \param startCol the first column in the block
    524   *
    525   * Example: \include MatrixBase_block_int_int.cpp
    526   * Output: \verbinclude MatrixBase_block_int_int.out
    527   *
    528   * \note since block is a templated member, the keyword template has to be used
    529   * if the matrix type is also a template parameter: \code m.template block<3,3>(1,1); \endcode
    530   *
    531   * \sa class Block, block(Index,Index,Index,Index)
    532   */
    533 template<int BlockRows, int BlockCols>
    534 inline Block<Derived, BlockRows, BlockCols> block(Index startRow, Index startCol)
    535 {
    536   return Block<Derived, BlockRows, BlockCols>(derived(), startRow, startCol);
    537 }
    538 
    539 /** This is the const version of block<>(Index, Index). */
    540 template<int BlockRows, int BlockCols>
    541 inline const Block<const Derived, BlockRows, BlockCols> block(Index startRow, Index startCol) const
    542 {
    543   return Block<const Derived, BlockRows, BlockCols>(derived(), startRow, startCol);
    544 }
    545 
    546 /** \returns an expression of the \a i-th column of *this. Note that the numbering starts at 0.
    547   *
    548   * Example: \include MatrixBase_col.cpp
    549   * Output: \verbinclude MatrixBase_col.out
    550   *
    551   * \sa row(), class Block */
    552 inline ColXpr col(Index i)
    553 {
    554   return ColXpr(derived(), i);
    555 }
    556 
    557 /** This is the const version of col(). */
    558 inline ConstColXpr col(Index i) const
    559 {
    560   return ConstColXpr(derived(), i);
    561 }
    562 
    563 /** \returns an expression of the \a i-th row of *this. Note that the numbering starts at 0.
    564   *
    565   * Example: \include MatrixBase_row.cpp
    566   * Output: \verbinclude MatrixBase_row.out
    567   *
    568   * \sa col(), class Block */
    569 inline RowXpr row(Index i)
    570 {
    571   return RowXpr(derived(), i);
    572 }
    573 
    574 /** This is the const version of row(). */
    575 inline ConstRowXpr row(Index i) const
    576 {
    577   return ConstRowXpr(derived(), i);
    578 }
    579 
    580 #endif // EIGEN_BLOCKMETHODS_H
    581