Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:06:04

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_BLOCK_H
0012 #define EIGEN_BLOCK_H
0013 
0014 namespace RivetEigen {
0015 
0016 namespace internal {
0017 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
0018 struct traits<Block<XprType, BlockRows, BlockCols, InnerPanel> > : traits<XprType>
0019 {
0020   typedef typename traits<XprType>::Scalar Scalar;
0021   typedef typename traits<XprType>::StorageKind StorageKind;
0022   typedef typename traits<XprType>::XprKind XprKind;
0023   typedef typename ref_selector<XprType>::type XprTypeNested;
0024   typedef typename remove_reference<XprTypeNested>::type _XprTypeNested;
0025   enum{
0026     MatrixRows = traits<XprType>::RowsAtCompileTime,
0027     MatrixCols = traits<XprType>::ColsAtCompileTime,
0028     RowsAtCompileTime = MatrixRows == 0 ? 0 : BlockRows,
0029     ColsAtCompileTime = MatrixCols == 0 ? 0 : BlockCols,
0030     MaxRowsAtCompileTime = BlockRows==0 ? 0
0031                          : RowsAtCompileTime != Dynamic ? int(RowsAtCompileTime)
0032                          : int(traits<XprType>::MaxRowsAtCompileTime),
0033     MaxColsAtCompileTime = BlockCols==0 ? 0
0034                          : ColsAtCompileTime != Dynamic ? int(ColsAtCompileTime)
0035                          : int(traits<XprType>::MaxColsAtCompileTime),
0036 
0037     XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0,
0038     IsRowMajor = (MaxRowsAtCompileTime==1&&MaxColsAtCompileTime!=1) ? 1
0039                : (MaxColsAtCompileTime==1&&MaxRowsAtCompileTime!=1) ? 0
0040                : XprTypeIsRowMajor,
0041     HasSameStorageOrderAsXprType = (IsRowMajor == XprTypeIsRowMajor),
0042     InnerSize = IsRowMajor ? int(ColsAtCompileTime) : int(RowsAtCompileTime),
0043     InnerStrideAtCompileTime = HasSameStorageOrderAsXprType
0044                              ? int(inner_stride_at_compile_time<XprType>::ret)
0045                              : int(outer_stride_at_compile_time<XprType>::ret),
0046     OuterStrideAtCompileTime = HasSameStorageOrderAsXprType
0047                              ? int(outer_stride_at_compile_time<XprType>::ret)
0048                              : int(inner_stride_at_compile_time<XprType>::ret),
0049 
0050     // FIXME, this traits is rather specialized for dense object and it needs to be cleaned further
0051     FlagsLvalueBit = is_lvalue<XprType>::value ? LvalueBit : 0,
0052     FlagsRowMajorBit = IsRowMajor ? RowMajorBit : 0,
0053     Flags = (traits<XprType>::Flags & (DirectAccessBit | (InnerPanel?CompressedAccessBit:0))) | FlagsLvalueBit | FlagsRowMajorBit,
0054     // FIXME DirectAccessBit should not be handled by expressions
0055     //
0056     // Alignment is needed by MapBase's assertions
0057     // We can sefely set it to false here. Internal alignment errors will be detected by an eigen_internal_assert in the respective evaluator
0058     Alignment = 0
0059   };
0060 };
0061 
0062 template<typename XprType, int BlockRows=Dynamic, int BlockCols=Dynamic, bool InnerPanel = false,
0063          bool HasDirectAccess = internal::has_direct_access<XprType>::ret> class BlockImpl_dense;
0064 
0065 } // end namespace internal
0066 
0067 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, typename StorageKind> class BlockImpl;
0068 
0069 /** \class Block
0070   * \ingroup Core_Module
0071   *
0072   * \brief Expression of a fixed-size or dynamic-size block
0073   *
0074   * \tparam XprType the type of the expression in which we are taking a block
0075   * \tparam BlockRows the number of rows of the block we are taking at compile time (optional)
0076   * \tparam BlockCols the number of columns of the block we are taking at compile time (optional)
0077   * \tparam InnerPanel is true, if the block maps to a set of rows of a row major matrix or
0078   *         to set of columns of a column major matrix (optional). The parameter allows to determine
0079   *         at compile time whether aligned access is possible on the block expression.
0080   *
0081   * This class represents an expression of either a fixed-size or dynamic-size block. It is the return
0082   * type of DenseBase::block(Index,Index,Index,Index) and DenseBase::block<int,int>(Index,Index) and
0083   * most of the time this is the only way it is used.
0084   *
0085   * However, if you want to directly maniputate block expressions,
0086   * for instance if you want to write a function returning such an expression, you
0087   * will need to use this class.
0088   *
0089   * Here is an example illustrating the dynamic case:
0090   * \include class_Block.cpp
0091   * Output: \verbinclude class_Block.out
0092   *
0093   * \note Even though this expression has dynamic size, in the case where \a XprType
0094   * has fixed size, this expression inherits a fixed maximal size which means that evaluating
0095   * it does not cause a dynamic memory allocation.
0096   *
0097   * Here is an example illustrating the fixed-size case:
0098   * \include class_FixedBlock.cpp
0099   * Output: \verbinclude class_FixedBlock.out
0100   *
0101   * \sa DenseBase::block(Index,Index,Index,Index), DenseBase::block(Index,Index), class VectorBlock
0102   */
0103 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel> class Block
0104   : public BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind>
0105 {
0106     typedef BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, typename internal::traits<XprType>::StorageKind> Impl;
0107   public:
0108     //typedef typename Impl::Base Base;
0109     typedef Impl Base;
0110     EIGEN_GENERIC_PUBLIC_INTERFACE(Block)
0111     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Block)
0112 
0113     typedef typename internal::remove_all<XprType>::type NestedExpression;
0114 
0115     /** Column or Row constructor
0116       */
0117     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0118     Block(XprType& xpr, Index i) : Impl(xpr,i)
0119     {
0120       eigen_assert( (i>=0) && (
0121           ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && i<xpr.rows())
0122         ||((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && i<xpr.cols())));
0123     }
0124 
0125     /** Fixed-size constructor
0126       */
0127     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0128     Block(XprType& xpr, Index startRow, Index startCol)
0129       : Impl(xpr, startRow, startCol)
0130     {
0131       EIGEN_STATIC_ASSERT(RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic,THIS_METHOD_IS_ONLY_FOR_FIXED_SIZE)
0132       eigen_assert(startRow >= 0 && BlockRows >= 0 && startRow + BlockRows <= xpr.rows()
0133              && startCol >= 0 && BlockCols >= 0 && startCol + BlockCols <= xpr.cols());
0134     }
0135 
0136     /** Dynamic-size constructor
0137       */
0138     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0139     Block(XprType& xpr,
0140           Index startRow, Index startCol,
0141           Index blockRows, Index blockCols)
0142       : Impl(xpr, startRow, startCol, blockRows, blockCols)
0143     {
0144       eigen_assert((RowsAtCompileTime==Dynamic || RowsAtCompileTime==blockRows)
0145           && (ColsAtCompileTime==Dynamic || ColsAtCompileTime==blockCols));
0146       eigen_assert(startRow >= 0 && blockRows >= 0 && startRow  <= xpr.rows() - blockRows
0147           && startCol >= 0 && blockCols >= 0 && startCol <= xpr.cols() - blockCols);
0148     }
0149 };
0150 
0151 // The generic default implementation for dense block simplu forward to the internal::BlockImpl_dense
0152 // that must be specialized for direct and non-direct access...
0153 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
0154 class BlockImpl<XprType, BlockRows, BlockCols, InnerPanel, Dense>
0155   : public internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel>
0156 {
0157     typedef internal::BlockImpl_dense<XprType, BlockRows, BlockCols, InnerPanel> Impl;
0158     typedef typename XprType::StorageIndex StorageIndex;
0159   public:
0160     typedef Impl Base;
0161     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl)
0162     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index i) : Impl(xpr,i) {}
0163     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index startRow, Index startCol) : Impl(xpr, startRow, startCol) {}
0164     EIGEN_DEVICE_FUNC
0165     EIGEN_STRONG_INLINE BlockImpl(XprType& xpr, Index startRow, Index startCol, Index blockRows, Index blockCols)
0166       : Impl(xpr, startRow, startCol, blockRows, blockCols) {}
0167 };
0168 
0169 namespace internal {
0170 
0171 /** \internal Internal implementation of dense Blocks in the general case. */
0172 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel, bool HasDirectAccess> class BlockImpl_dense
0173   : public internal::dense_xpr_base<Block<XprType, BlockRows, BlockCols, InnerPanel> >::type
0174 {
0175     typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
0176     typedef typename internal::ref_selector<XprType>::non_const_type XprTypeNested;
0177   public:
0178 
0179     typedef typename internal::dense_xpr_base<BlockType>::type Base;
0180     EIGEN_DENSE_PUBLIC_INTERFACE(BlockType)
0181     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense)
0182 
0183     // class InnerIterator; // FIXME apparently never used
0184 
0185     /** Column or Row constructor
0186       */
0187     EIGEN_DEVICE_FUNC
0188     inline BlockImpl_dense(XprType& xpr, Index i)
0189       : m_xpr(xpr),
0190         // It is a row if and only if BlockRows==1 and BlockCols==XprType::ColsAtCompileTime,
0191         // and it is a column if and only if BlockRows==XprType::RowsAtCompileTime and BlockCols==1,
0192         // all other cases are invalid.
0193         // The case a 1x1 matrix seems ambiguous, but the result is the same anyway.
0194         m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
0195         m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0),
0196         m_blockRows(BlockRows==1 ? 1 : xpr.rows()),
0197         m_blockCols(BlockCols==1 ? 1 : xpr.cols())
0198     {}
0199 
0200     /** Fixed-size constructor
0201       */
0202     EIGEN_DEVICE_FUNC
0203     inline BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
0204       : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
0205                     m_blockRows(BlockRows), m_blockCols(BlockCols)
0206     {}
0207 
0208     /** Dynamic-size constructor
0209       */
0210     EIGEN_DEVICE_FUNC
0211     inline BlockImpl_dense(XprType& xpr,
0212           Index startRow, Index startCol,
0213           Index blockRows, Index blockCols)
0214       : m_xpr(xpr), m_startRow(startRow), m_startCol(startCol),
0215                     m_blockRows(blockRows), m_blockCols(blockCols)
0216     {}
0217 
0218     EIGEN_DEVICE_FUNC inline Index rows() const { return m_blockRows.value(); }
0219     EIGEN_DEVICE_FUNC inline Index cols() const { return m_blockCols.value(); }
0220 
0221     EIGEN_DEVICE_FUNC
0222     inline Scalar& coeffRef(Index rowId, Index colId)
0223     {
0224       EIGEN_STATIC_ASSERT_LVALUE(XprType)
0225       return m_xpr.coeffRef(rowId + m_startRow.value(), colId + m_startCol.value());
0226     }
0227 
0228     EIGEN_DEVICE_FUNC
0229     inline const Scalar& coeffRef(Index rowId, Index colId) const
0230     {
0231       return m_xpr.derived().coeffRef(rowId + m_startRow.value(), colId + m_startCol.value());
0232     }
0233 
0234     EIGEN_DEVICE_FUNC
0235     EIGEN_STRONG_INLINE const CoeffReturnType coeff(Index rowId, Index colId) const
0236     {
0237       return m_xpr.coeff(rowId + m_startRow.value(), colId + m_startCol.value());
0238     }
0239 
0240     EIGEN_DEVICE_FUNC
0241     inline Scalar& coeffRef(Index index)
0242     {
0243       EIGEN_STATIC_ASSERT_LVALUE(XprType)
0244       return m_xpr.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
0245                             m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
0246     }
0247 
0248     EIGEN_DEVICE_FUNC
0249     inline const Scalar& coeffRef(Index index) const
0250     {
0251       return m_xpr.coeffRef(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
0252                             m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
0253     }
0254 
0255     EIGEN_DEVICE_FUNC
0256     inline const CoeffReturnType coeff(Index index) const
0257     {
0258       return m_xpr.coeff(m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
0259                          m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
0260     }
0261 
0262     template<int LoadMode>
0263     inline PacketScalar packet(Index rowId, Index colId) const
0264     {
0265       return m_xpr.template packet<Unaligned>(rowId + m_startRow.value(), colId + m_startCol.value());
0266     }
0267 
0268     template<int LoadMode>
0269     inline void writePacket(Index rowId, Index colId, const PacketScalar& val)
0270     {
0271       m_xpr.template writePacket<Unaligned>(rowId + m_startRow.value(), colId + m_startCol.value(), val);
0272     }
0273 
0274     template<int LoadMode>
0275     inline PacketScalar packet(Index index) const
0276     {
0277       return m_xpr.template packet<Unaligned>
0278               (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
0279                m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0));
0280     }
0281 
0282     template<int LoadMode>
0283     inline void writePacket(Index index, const PacketScalar& val)
0284     {
0285       m_xpr.template writePacket<Unaligned>
0286          (m_startRow.value() + (RowsAtCompileTime == 1 ? 0 : index),
0287           m_startCol.value() + (RowsAtCompileTime == 1 ? index : 0), val);
0288     }
0289 
0290     #ifdef EIGEN_PARSED_BY_DOXYGEN
0291     /** \sa MapBase::data() */
0292     EIGEN_DEVICE_FUNC inline const Scalar* data() const;
0293     EIGEN_DEVICE_FUNC inline Index innerStride() const;
0294     EIGEN_DEVICE_FUNC inline Index outerStride() const;
0295     #endif
0296 
0297     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0298     const typename internal::remove_all<XprTypeNested>::type& nestedExpression() const
0299     {
0300       return m_xpr;
0301     }
0302 
0303     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0304     XprType& nestedExpression() { return m_xpr; }
0305 
0306     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0307     StorageIndex startRow() const EIGEN_NOEXCEPT
0308     {
0309       return m_startRow.value();
0310     }
0311 
0312     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0313     StorageIndex startCol() const EIGEN_NOEXCEPT
0314     {
0315       return m_startCol.value();
0316     }
0317 
0318   protected:
0319 
0320     XprTypeNested m_xpr;
0321     const internal::variable_if_dynamic<StorageIndex, (XprType::RowsAtCompileTime == 1 && BlockRows==1) ? 0 : Dynamic> m_startRow;
0322     const internal::variable_if_dynamic<StorageIndex, (XprType::ColsAtCompileTime == 1 && BlockCols==1) ? 0 : Dynamic> m_startCol;
0323     const internal::variable_if_dynamic<StorageIndex, RowsAtCompileTime> m_blockRows;
0324     const internal::variable_if_dynamic<StorageIndex, ColsAtCompileTime> m_blockCols;
0325 };
0326 
0327 /** \internal Internal implementation of dense Blocks in the direct access case.*/
0328 template<typename XprType, int BlockRows, int BlockCols, bool InnerPanel>
0329 class BlockImpl_dense<XprType,BlockRows,BlockCols, InnerPanel,true>
0330   : public MapBase<Block<XprType, BlockRows, BlockCols, InnerPanel> >
0331 {
0332     typedef Block<XprType, BlockRows, BlockCols, InnerPanel> BlockType;
0333     typedef typename internal::ref_selector<XprType>::non_const_type XprTypeNested;
0334     enum {
0335       XprTypeIsRowMajor = (int(traits<XprType>::Flags)&RowMajorBit) != 0
0336     };
0337   public:
0338 
0339     typedef MapBase<BlockType> Base;
0340     EIGEN_DENSE_PUBLIC_INTERFACE(BlockType)
0341     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(BlockImpl_dense)
0342 
0343     /** Column or Row constructor
0344       */
0345     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0346     BlockImpl_dense(XprType& xpr, Index i)
0347       : Base(xpr.data() + i * (    ((BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) && (!XprTypeIsRowMajor))
0348                                 || ((BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) && ( XprTypeIsRowMajor)) ? xpr.innerStride() : xpr.outerStride()),
0349              BlockRows==1 ? 1 : xpr.rows(),
0350              BlockCols==1 ? 1 : xpr.cols()),
0351         m_xpr(xpr),
0352         m_startRow( (BlockRows==1) && (BlockCols==XprType::ColsAtCompileTime) ? i : 0),
0353         m_startCol( (BlockRows==XprType::RowsAtCompileTime) && (BlockCols==1) ? i : 0)
0354     {
0355       init();
0356     }
0357 
0358     /** Fixed-size constructor
0359       */
0360     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0361     BlockImpl_dense(XprType& xpr, Index startRow, Index startCol)
0362       : Base(xpr.data()+xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol)),
0363         m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
0364     {
0365       init();
0366     }
0367 
0368     /** Dynamic-size constructor
0369       */
0370     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0371     BlockImpl_dense(XprType& xpr,
0372           Index startRow, Index startCol,
0373           Index blockRows, Index blockCols)
0374       : Base(xpr.data()+xpr.innerStride()*(XprTypeIsRowMajor?startCol:startRow) + xpr.outerStride()*(XprTypeIsRowMajor?startRow:startCol), blockRows, blockCols),
0375         m_xpr(xpr), m_startRow(startRow), m_startCol(startCol)
0376     {
0377       init();
0378     }
0379 
0380     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0381     const typename internal::remove_all<XprTypeNested>::type& nestedExpression() const EIGEN_NOEXCEPT
0382     {
0383       return m_xpr;
0384     }
0385 
0386     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0387     XprType& nestedExpression() { return m_xpr; }
0388 
0389     /** \sa MapBase::innerStride() */
0390     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0391     Index innerStride() const EIGEN_NOEXCEPT
0392     {
0393       return internal::traits<BlockType>::HasSameStorageOrderAsXprType
0394              ? m_xpr.innerStride()
0395              : m_xpr.outerStride();
0396     }
0397 
0398     /** \sa MapBase::outerStride() */
0399     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0400     Index outerStride() const EIGEN_NOEXCEPT
0401     {
0402       return internal::traits<BlockType>::HasSameStorageOrderAsXprType
0403                     ? m_xpr.outerStride()
0404                     : m_xpr.innerStride();
0405     }
0406 
0407     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0408     StorageIndex startRow() const EIGEN_NOEXCEPT { return m_startRow.value(); }
0409 
0410     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR
0411     StorageIndex startCol() const EIGEN_NOEXCEPT { return m_startCol.value(); }
0412 
0413   #ifndef __SUNPRO_CC
0414   // FIXME sunstudio is not friendly with the above friend...
0415   // META-FIXME there is no 'friend' keyword around here. Is this obsolete?
0416   protected:
0417   #endif
0418 
0419     #ifndef EIGEN_PARSED_BY_DOXYGEN
0420     /** \internal used by allowAligned() */
0421     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0422     BlockImpl_dense(XprType& xpr, const Scalar* data, Index blockRows, Index blockCols)
0423       : Base(data, blockRows, blockCols), m_xpr(xpr)
0424     {
0425       init();
0426     }
0427     #endif
0428 
0429   protected:
0430     EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0431     void init()
0432     {
0433       m_outerStride = internal::traits<BlockType>::HasSameStorageOrderAsXprType
0434                     ? m_xpr.outerStride()
0435                     : m_xpr.innerStride();
0436     }
0437 
0438     XprTypeNested m_xpr;
0439     const internal::variable_if_dynamic<StorageIndex, (XprType::RowsAtCompileTime == 1 && BlockRows==1) ? 0 : Dynamic> m_startRow;
0440     const internal::variable_if_dynamic<StorageIndex, (XprType::ColsAtCompileTime == 1 && BlockCols==1) ? 0 : Dynamic> m_startCol;
0441     Index m_outerStride;
0442 };
0443 
0444 } // end namespace internal
0445 
0446 } // end namespace RivetEigen
0447 
0448 #endif // EIGEN_BLOCK_H