17 #ifndef BLOCK_EIGENBINDINGS_HPP
18 #define BLOCK_EIGENBINDINGS_HPP
22 #ifndef BOGUS_BLOCK_WITHOUT_EIGEN_SPARSE
23 #include "SparseHeader.hpp"
26 #include "../Block/BlockMatrixBase.hpp"
27 #include "../Block/Expressions.hpp"
29 #ifndef BOGUS_BLOCK_WITHOUT_LINEAR_SOLVERS
30 #include "EigenLinearSolvers.hpp"
31 #ifndef BOGUS_BLOCK_WITHOUT_EIGEN_SPARSE
32 #include "EigenSparseLinearSolvers.hpp"
36 #include "../Utils/CppTools.hpp"
38 #define BOGUS_EIGEN_NEW_EXPRESSIONS EIGEN_VERSION_AT_LEAST(3,2,90)
45 template<
typename EigenDerived >
46 inline bool is_zero (
const Eigen::MatrixBase< EigenDerived >& block,
47 typename EigenDerived::Scalar precision )
49 return block.isZero( precision ) ;
52 template<
typename EigenDerived >
53 inline void set_zero ( Eigen::MatrixBase< EigenDerived >& block )
55 block.derived().setZero( ) ;
58 template<
typename EigenDerived >
59 inline void set_identity ( Eigen::MatrixBase< EigenDerived >& block )
61 block.derived().setIdentity( ) ;
64 template<
typename EigenDerived >
65 inline void resize ( Eigen::MatrixBase< EigenDerived >& block,
int rows,
int cols )
67 block.derived().resize( rows, cols ) ;
70 template<
typename EigenDerived >
71 inline const typename EigenDerived::Scalar* data_pointer (
const Eigen::MatrixBase< EigenDerived >& block )
73 return block.derived().data() ;
76 #ifndef BOGUS_BLOCK_WITHOUT_EIGEN_SPARSE
78 #if !BOGUS_EIGEN_NEW_EXPRESSIONS
79 template <
typename BlockT >
81 typedef const Eigen::Transpose< const BlockT > ReturnType ;
83 template<
typename _Scalar,
int _Flags,
typename _Index>
85 :
public BlockTransposeTraits< Eigen::SparseMatrixBase< Eigen::SparseMatrix < _Scalar, _Flags, _Index > > >
87 template<
typename _Scalar,
int _Flags,
typename _Index>
89 :
public BlockTransposeTraits< Eigen::SparseMatrixBase< Eigen::SparseVector < _Scalar, _Flags, _Index > > >
91 template<
typename _Scalar,
int _Flags,
typename _Index>
93 :
public BlockTransposeTraits< Eigen::SparseMatrixBase< Eigen::MappedSparseMatrix < _Scalar, _Flags, _Index > > >
96 template<
typename EigenDerived >
97 inline const Eigen::Transpose< const EigenDerived >
100 return block.transpose() ;
104 template<
typename EigenDerived >
105 inline bool is_zero (
const Eigen::SparseMatrixBase< EigenDerived >& block,
106 typename EigenDerived::Scalar precision )
108 return block.isZero( precision ) ;
111 template <
typename Scalar,
int Options,
typename Index >
112 inline void set_identity ( Eigen::SparseMatrix< Scalar, Options, Index >& block )
114 return block.setIdentity( ) ;
117 template <
typename Scalar,
int Options,
typename Index >
118 inline void resize ( Eigen::SparseMatrix< Scalar, Options, Index >& block, Index rows, Index cols )
120 block.resize( rows, cols ) ;
127 template<
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols >
128 struct BlockTraits < Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
130 typedef _Scalar Scalar ;
141 typedef Eigen::Matrix< _Scalar, _Cols, _Rows,
142 ( _Options | ((_Cols==1&&_Rows!=1)?Eigen::RowMajor:0)) & ~((_Rows==1&&_Cols!=1)?Eigen::RowMajor:0),
144 TransposeStorageType ;
151 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols,
152 typename _Scalar2,
int _Rows2,
int _Cols2,
int _Options2,
int _MaxRows2,
int _MaxCols2,
153 bool TransposeLhs,
bool TransposeRhs >
155 Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>,
156 Eigen::Matrix<_Scalar2, _Rows2, _Cols2, _Options2, _MaxRows2, _MaxCols2>,
157 TransposeLhs, TransposeRhs >
159 typedef Eigen::Matrix< _Scalar,
168 template<
typename Derived >
169 inline typename Eigen::internal::plain_matrix_type<Derived>::type
170 get_mutable_vector(
const Eigen::MatrixBase< Derived > & )
172 return typename Eigen::internal::plain_matrix_type<Derived>::type() ;
183 template<
typename Derived >
189 typedef Eigen::internal::traits< EigenBlockWrapper< Derived > > Traits ;
190 typedef typename Traits::Scalar Scalar ;
191 typedef typename Traits::Index Index ;
193 enum { Flags = Traits::Flags, IsVectorAtCompileTime = 0,
194 MaxRowsAtCompileTime = Traits::MaxRowsAtCompileTime,
195 MaxColsAtCompileTime = Traits::MaxColsAtCompileTime
199 : obj( obj_ ), scaling(s)
202 Index rows()
const {
return obj.rows() ; }
203 Index cols()
const {
return obj.cols() ; }
208 operator*(
const Scalar& scalar)
const
217 #if BOGUS_EIGEN_NEW_EXPRESSIONS
218 template <
typename EigenDerived >
219 inline Eigen::Product< EigenBlockWrapper, EigenDerived > operator* (
220 const EigenDerived &rhs )
const
222 return Eigen::Product< EigenBlockWrapper, EigenDerived > (*
this, rhs) ;
224 template <
typename EigenDerived >
225 friend inline Eigen::Product< EigenDerived, EigenBlockWrapper > operator* (
228 return Eigen::Product< EigenDerived, EigenBlockWrapper > (lhs, matrix) ;
233 const Scalar scaling ;
238 template<
typename Lhs,
typename Rhs >
241 #if BOGUS_EIGEN_NEW_EXPRESSIONS
242 template <
typename Lhs,
typename Rhs >
244 :
public Eigen::Product< Lhs, Rhs >
246 typedef Eigen::Product< Lhs, Rhs > Base ;
254 template <
typename Lhs,
typename Rhs >
256 :
public Eigen::ProductBase< BlockEigenProduct< Lhs, Rhs>, Lhs, Rhs >
258 typedef Eigen::ProductBase< BlockEigenProduct, Lhs, Rhs > Base ;
269 template<
typename Dest>
inline void evalTo(Dest& dst)
const
271 product_impl::evalTo( dst, m_lhs, this->m_rhs ) ;
273 template<
typename Dest>
inline void scaleAndAddTo(Dest& dst, Scalar alpha)
const
275 product_impl::scaleAndAddTo( dst, m_lhs, m_rhs, alpha ) ;
280 template<
typename Derived,
typename EigenDerived >
282 const Derived &matrix,
const EigenDerived &vector,
typename Derived::Scalar scaling = 1 )
287 template<
typename Derived,
typename EigenDerived >
288 BlockEigenProduct< EigenDerived, EigenBlockWrapper< Derived > > eigen_block_product(
289 const EigenDerived &vector,
const Derived &matrix,
typename Derived::Scalar scaling = 1 )
291 return BlockEigenProduct< EigenDerived, EigenBlockWrapper< Derived > > ( vector, EigenBlockWrapper< Derived >(matrix, scaling) ) ;
294 template<
typename Derived,
typename EigenDerived >
298 typedef EigenDerived Rhs ;
299 typedef typename Derived::Scalar Scalar;
301 template<
typename Dst>
302 static void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
304 lhs.obj.template multiply< false >( rhs.derived(), dst, lhs.scaling, 0 ) ;
306 template<
typename Dst>
307 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
309 lhs.obj.template multiply< false >( rhs.derived(), dst, alpha*lhs.scaling, 1 ) ;
313 template<
typename Derived,
typename EigenDerived >
316 typedef EigenDerived Lhs ;
318 typedef typename Derived::Scalar Scalar;
320 template<
typename Dst>
321 static void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
323 Eigen::Transpose< Dst > transposed( dst.transpose() ) ;
324 rhs.obj.template multiply< true >( lhs.transpose(), transposed, rhs.scaling, 0 ) ;
326 template<
typename Dst>
327 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
329 Eigen::Transpose< Dst > transposed( dst.transpose() ) ;
330 rhs.obj.template multiply< true >( lhs.transpose(), transposed, alpha * rhs.scaling, 1 ) ;
338 #if BOGUS_EIGEN_NEW_EXPRESSIONS
342 template<
typename Derived,
typename EigenDerived,
int ProductType >
343 struct generic_product_impl< bogus::mv_impl::EigenBlockWrapper<Derived>, EigenDerived, SparseShape, DenseShape, ProductType>
344 :
public generic_product_impl_base <
345 bogus::mv_impl::EigenBlockWrapper<Derived>, EigenDerived,
346 generic_product_impl< bogus::mv_impl::EigenBlockWrapper<Derived>, EigenDerived, SparseShape, DenseShape, ProductType > >
349 typedef EigenDerived Rhs ;
350 typedef typename Derived::Scalar Scalar;
352 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
355 template<
typename Dst>
356 static void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
358 RhsNested rhsNested( rhs ) ;
359 product_impl::evalTo( dst, lhs, rhsNested ) ;
361 template<
typename Dst>
362 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs, Scalar alpha)
364 RhsNested rhsNested( rhs ) ;
365 product_impl::scaleAndAddTo( dst, lhs, rhsNested, alpha ) ;
369 template<
typename Derived,
typename EigenDerived,
int ProductType >
370 struct generic_product_impl< EigenDerived, bogus::mv_impl::EigenBlockWrapper<Derived>, DenseShape, SparseShape, ProductType >
371 :
public generic_product_impl_base <
372 EigenDerived, bogus::mv_impl::EigenBlockWrapper<Derived>,
373 generic_product_impl< EigenDerived, bogus::mv_impl::EigenBlockWrapper<Derived>, DenseShape, SparseShape, ProductType > >
375 typedef EigenDerived Lhs ;
377 typedef typename Derived::Scalar Scalar;
379 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
382 template<
typename Dst>
383 static void evalTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs)
385 LhsNested lhsNested(lhs);
386 product_impl::evalTo( dst, lhsNested, rhs) ;
388 template<
typename Dst>
389 static void scaleAndAddTo(Dst& dst,
const Lhs& lhs,
const Rhs& rhs,
const Scalar& alpha)
391 LhsNested lhsNested(lhs);
392 product_impl::scaleAndAddTo( dst, lhsNested, rhs, alpha ) ;
397 template<
typename Derived,
typename Rhs,
typename Scalar>
398 struct evaluator<CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, const Product<bogus::mv_impl::EigenBlockWrapper<Derived>, Rhs, DefaultProduct> > >
399 :
public evaluator<Product<bogus::mv_impl::EigenBlockWrapper<Derived>, Rhs, DefaultProduct> >
401 typedef CwiseUnaryOp<internal::scalar_multiple_op<Scalar>,
const Product<bogus::mv_impl::EigenBlockWrapper<Derived>, Rhs, DefaultProduct> > XprType;
402 typedef evaluator<Product<bogus::mv_impl::EigenBlockWrapper<Derived>, Rhs, DefaultProduct> > Base;
404 EIGEN_DEVICE_FUNC
explicit evaluator(
const XprType& xpr)
405 : Base( ( xpr.functor().m_other * xpr.nestedExpression().lhs() ) * xpr.nestedExpression().rhs() )
419 template <
typename Lhs,
typename Rhs >
420 struct traits< bogus::mv_impl::BlockEigenProduct< Lhs, Rhs > >
421 #if BOGUS_EIGEN_NEW_EXPRESSIONS
422 :
public traits< typename bogus::mv_impl::BlockEigenProduct< Lhs, Rhs >::Base >
424 :
public traits< ProductBase< bogus::mv_impl::BlockEigenProduct< Lhs, Rhs >, Lhs, Rhs > >
427 typedef Dense StorageKind;
430 template<
typename Derived>
431 struct traits<bogus::mv_impl::EigenBlockWrapper<Derived> >
433 typedef typename Derived::Scalar Scalar;
434 typedef typename Derived::Index Index;
435 typedef typename Derived::Index StorageIndex;
436 #if BOGUS_EIGEN_NEW_EXPRESSIONS
437 typedef Sparse StorageKind;
439 typedef Dense StorageKind;
441 typedef MatrixXpr XprKind;
443 RowsAtCompileTime = Dynamic,
444 ColsAtCompileTime = Dynamic,
445 MaxRowsAtCompileTime = Dynamic,
446 MaxColsAtCompileTime = Dynamic,
459 template <
typename Derived,
typename EigenDerived >
460 mv_impl::BlockEigenProduct< mv_impl::EigenBlockWrapper<Derived>, EigenDerived > operator* (
461 const BlockObjectBase< Derived >& lhs,
462 const Eigen::MatrixBase< EigenDerived > &rhs )
464 assert( rhs.rows() == lhs.cols() ) ;
465 return mv_impl::block_eigen_product ( lhs.derived(), rhs.derived() ) ;
468 template <
typename Derived,
typename EigenDerived >
469 mv_impl::BlockEigenProduct< mv_impl::EigenBlockWrapper<Derived>, EigenDerived > operator* (
470 const Scaling< Derived >& lhs,
471 const Eigen::MatrixBase< EigenDerived > &rhs )
473 assert( rhs.rows() == lhs.cols() ) ;
474 return mv_impl::block_eigen_product ( lhs.operand.object, rhs.derived(), lhs.operand.scaling ) ;
477 template <
typename Derived,
typename EigenDerived >
478 mv_impl::BlockEigenProduct< EigenDerived, mv_impl::EigenBlockWrapper<Derived> > operator* (
479 const Eigen::MatrixBase< EigenDerived > &lhs,
480 const BlockObjectBase< Derived >& rhs )
482 assert( lhs.cols() == rhs.rows() ) ;
483 return mv_impl::eigen_block_product ( lhs.derived(), rhs.derived() ) ;
486 template <
typename Derived,
typename EigenDerived >
487 mv_impl::BlockEigenProduct< EigenDerived, mv_impl::EigenBlockWrapper<Derived> > operator* (
488 const Eigen::MatrixBase< EigenDerived > &lhs,
489 const Scaling< Derived >& rhs )
491 assert( lhs.cols() == rhs.rows() ) ;
492 return mv_impl::eigen_block_product ( lhs.derived(), rhs.operand.object, rhs.operand.scaling ) ;
498 #endif // EIGENBINDINGS_HPP
SparseBlockMatrix / Dense producty expression.
Definition: BlockBindings.hpp:239
Definition: BlockBindings.hpp:255
EnableIf< BlockTraits< SelfTransposeT >::is_self_transpose, const SelfTransposeT & >::ReturnType transpose_block(const SelfTransposeT &block)
Specialization of transpose_block() for self-adjoint types.
Definition: Access.hpp:25
Number of rows spanned by a block at compile time ; useful for efficient segmentation.
Definition: Traits.hpp:37
Defines the return type of the product of two blocks potentially transposed.
Definition: Traits.hpp:64
Whether this block is equal to its transpose.
Definition: Traits.hpp:46
Definition: Traits.hpp:29
Ordering inside the block ; only useful uses_plain_array_storage is true.
Definition: Traits.hpp:44
Can be set to true if "const Scalar* data_pointer( const BlockType& )" exist.
Definition: Traits.hpp:42
Number of cols spanned by a block at compile time ; useful for efficient segmentation.
Definition: Traits.hpp:39
Definition: CppTools.hpp:49
Wrapper so our SparseBlockMatrix can be used inside Eigen expressions.
Definition: BlockBindings.hpp:184
Defines the return type of an associated transpose_block( const BlockType& ) function.
Definition: Traits.hpp:55