SparsePermutation.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSE_PERMUTATION_H
11 #define EIGEN_SPARSE_PERMUTATION_H
12 
13 // This file implements sparse * permutation products
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename ExpressionType, int Side, bool Transposed>
20 struct permutation_matrix_product<ExpressionType, Side, Transposed, SparseShape>
21 {
24 
26  typedef typename MatrixTypeCleaned::StorageIndex StorageIndex;
27 
28  enum {
29  SrcStorageOrder = MatrixTypeCleaned::Flags&RowMajorBit ? RowMajor : ColMajor,
30  MoveOuter = SrcStorageOrder==RowMajor ? Side==OnTheLeft : Side==OnTheRight
31  };
32 
33  typedef typename internal::conditional<MoveOuter,
36 
37  template<typename Dest,typename PermutationType>
38  static inline void run(Dest& dst, const PermutationType& perm, const ExpressionType& xpr)
39  {
40  MatrixType mat(xpr);
41  if(MoveOuter)
42  {
45  for(Index j=0; j<mat.outerSize(); ++j)
46  {
47  Index jp = perm.indices().coeff(j);
48  sizes[((Side==OnTheLeft) ^ Transposed) ? jp : j] = StorageIndex(mat.innerVector(((Side==OnTheRight) ^ Transposed) ? jp : j).nonZeros());
49  }
50  tmp.reserve(sizes);
51  for(Index j=0; j<mat.outerSize(); ++j)
52  {
53  Index jp = perm.indices().coeff(j);
54  Index jsrc = ((Side==OnTheRight) ^ Transposed) ? jp : j;
55  Index jdst = ((Side==OnTheLeft) ^ Transposed) ? jp : j;
56  for(typename MatrixTypeCleaned::InnerIterator it(mat,jsrc); it; ++it)
57  tmp.insertByOuterInner(jdst,it.index()) = it.value();
58  }
59  dst = tmp;
60  }
61  else
62  {
63  SparseMatrix<Scalar,int(SrcStorageOrder)==RowMajor?ColMajor:RowMajor,StorageIndex> tmp(mat.rows(), mat.cols());
64  Matrix<StorageIndex,Dynamic,1> sizes(tmp.outerSize());
65  sizes.setZero();
67  if((Side==OnTheLeft) ^ Transposed)
68  perm_cpy = perm;
69  else
70  perm_cpy = perm.transpose();
71 
72  for(Index j=0; j<mat.outerSize(); ++j)
73  for(typename MatrixTypeCleaned::InnerIterator it(mat,j); it; ++it)
74  sizes[perm_cpy.indices().coeff(it.index())]++;
75  tmp.reserve(sizes);
76  for(Index j=0; j<mat.outerSize(); ++j)
77  for(typename MatrixTypeCleaned::InnerIterator it(mat,j); it; ++it)
78  tmp.insertByOuterInner(perm_cpy.indices().coeff(it.index()),j) = it.value();
79  dst = tmp;
80  }
81  }
82 };
83 
84 }
85 
86 namespace internal {
87 
88 template <int ProductTag> struct product_promote_storage_type<Sparse, PermutationStorage, ProductTag> { typedef Sparse ret; };
89 template <int ProductTag> struct product_promote_storage_type<PermutationStorage, Sparse, ProductTag> { typedef Sparse ret; };
90 
91 // TODO, the following two overloads are only needed to define the right temporary type through
92 // typename traits<permutation_sparse_matrix_product<Rhs,Lhs,OnTheRight,false> >::ReturnType
93 // whereas it should be correctly handled by traits<Product<> >::PlainObject
94 
95 template<typename Lhs, typename Rhs, int ProductTag>
97  : public evaluator<typename permutation_matrix_product<Rhs,OnTheLeft,false,SparseShape>::ReturnType>
98 {
102 
103  enum {
104  Flags = Base::Flags | EvalBeforeNestingBit
105  };
106 
107  explicit product_evaluator(const XprType& xpr)
108  : m_result(xpr.rows(), xpr.cols())
109  {
110  ::new (static_cast<Base*>(this)) Base(m_result);
112  }
113 
114 protected:
116 };
117 
118 template<typename Lhs, typename Rhs, int ProductTag>
120  : public evaluator<typename permutation_matrix_product<Lhs,OnTheRight,false,SparseShape>::ReturnType>
121 {
125 
126  enum {
127  Flags = Base::Flags | EvalBeforeNestingBit
128  };
129 
130  explicit product_evaluator(const XprType& xpr)
131  : m_result(xpr.rows(), xpr.cols())
132  {
133  ::new (static_cast<Base*>(this)) Base(m_result);
135  }
136 
137 protected:
139 };
140 
141 } // end namespace internal
142 
145 template<typename SparseDerived, typename PermDerived>
148 { return Product<SparseDerived, PermDerived, AliasFreeProduct>(matrix.derived(), perm.derived()); }
149 
152 template<typename SparseDerived, typename PermDerived>
155 { return Product<PermDerived, SparseDerived, AliasFreeProduct>(perm.derived(), matrix.derived()); }
156 
157 
160 template<typename SparseDerived, typename PermutationType>
163 {
165 }
166 
169 template<typename SparseDerived, typename PermutationType>
170 inline const Product<Inverse<PermutationType>, SparseDerived, AliasFreeProduct>
172 {
173  return Product<Inverse<PermutationType>, SparseDerived, AliasFreeProduct>(tperm.derived(), matrix.derived());
174 }
175 
176 } // end namespace Eigen
177 
178 #endif // EIGEN_SPARSE_SELFADJOINTVIEW_H
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
Eigen::internal::Lhs
@ Lhs
Definition: TensorContractionMapper.h:19
perm
idx_t idx_t idx_t idx_t idx_t * perm
Definition: include/metis.h:223
Eigen::internal::product_promote_storage_type< Sparse, PermutationStorage, ProductTag >::ret
Sparse ret
Definition: SparsePermutation.h:88
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::SparseMatrix
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Eigen::internal::permutation_matrix_product< ExpressionType, Side, Transposed, SparseShape >::MatrixTypeCleaned
remove_all< MatrixType >::type MatrixTypeCleaned
Definition: SparsePermutation.h:23
Eigen::EigenBase::derived
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:46
Eigen::PermutationMatrix::indices
const IndicesType & indices() const
Definition: PermutationMatrix.h:360
Eigen::Sparse
Definition: Constants.h:510
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, PermutationShape, SparseShape >::m_result
PlainObject m_result
Definition: SparsePermutation.h:115
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, PermutationShape, SparseShape >::Base
evaluator< PlainObject > Base
Definition: SparsePermutation.h:101
Eigen::internal::permutation_matrix_product< ExpressionType, Side, Transposed, SparseShape >::Scalar
MatrixTypeCleaned::Scalar Scalar
Definition: SparsePermutation.h:25
Eigen::internal::permutation_matrix_product
Definition: ProductEvaluators.h:998
Eigen::RowMajorBit
const unsigned int RowMajorBit
Definition: Constants.h:66
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, PermutationShape, SparseShape >::XprType
Product< Lhs, Rhs, AliasFreeProduct > XprType
Definition: SparsePermutation.h:99
sizes
std::vector< Array2i > sizes
Definition: dense_solvers.cpp:12
type
Definition: pytypes.h:1491
Eigen::RowMajor
@ RowMajor
Definition: Constants.h:321
mat
MatrixXf mat
Definition: Tutorial_AdvancedInitialization_CommaTemporary.cpp:1
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
Eigen::operator*
const EIGEN_DEVICE_FUNC Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:515
Eigen::InverseImpl< PermutationType, PermutationStorage >
Definition: PermutationMatrix.h:536
Eigen::OnTheLeft
@ OnTheLeft
Definition: Constants.h:332
Eigen::internal::generic_product_impl
Definition: ProductEvaluators.h:86
Eigen::internal::true_type
Definition: Meta.h:96
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, SparseShape, PermutationShape >::PlainObject
permutation_matrix_product< Lhs, OnTheRight, false, SparseShape >::ReturnType PlainObject
Definition: SparsePermutation.h:123
Eigen::internal::permutation_matrix_product< ExpressionType, Side, Transposed, SparseShape >::MatrixType
nested_eval< ExpressionType, 1 >::type MatrixType
Definition: SparsePermutation.h:22
Eigen::SparseShape
Definition: Constants.h:537
Eigen::internal::product_evaluator
Definition: ForwardDeclarations.h:164
Eigen::internal::permutation_matrix_product< ExpressionType, Side, Transposed, SparseShape >::StorageIndex
MatrixTypeCleaned::StorageIndex StorageIndex
Definition: SparsePermutation.h:26
Eigen::Product
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Eigen::Triplet< double >
Eigen::internal::product_promote_storage_type
Definition: XprHelper.h:578
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, SparseShape, PermutationShape >::XprType
Product< Lhs, Rhs, AliasFreeProduct > XprType
Definition: SparsePermutation.h:122
Eigen::internal::evaluator
Definition: CoreEvaluators.h:90
Eigen::Product::lhs
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE LhsNestedCleaned & lhs() const
Definition: Product.h:107
Eigen::OnTheRight
@ OnTheRight
Definition: Constants.h:334
Eigen::SparseMatrix::reserve
void reserve(Index reserveSize)
Definition: SparseMatrix.h:264
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, PermutationShape, SparseShape >::product_evaluator
product_evaluator(const XprType &xpr)
Definition: SparsePermutation.h:107
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, SparseShape, PermutationShape >::Base
evaluator< PlainObject > Base
Definition: SparsePermutation.h:124
Eigen::PermutationBase
Base class for permutations.
Definition: PermutationMatrix.h:46
Eigen::internal::product_promote_storage_type< PermutationStorage, Sparse, ProductTag >::ret
Sparse ret
Definition: SparsePermutation.h:89
Eigen::PermutationStorage
Definition: Constants.h:516
Eigen::internal::Rhs
@ Rhs
Definition: TensorContractionMapper.h:18
Eigen::PermutationMatrix< Dynamic, Dynamic, StorageIndex >
Eigen::Product::rhs
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE RhsNestedCleaned & rhs() const
Definition: Product.h:109
Eigen::internal::conditional
Definition: Meta.h:109
Product
ProductLieGroup< Point2, Pose2 > Product
Definition: testLie.cpp:33
Eigen::AliasFreeProduct
@ AliasFreeProduct
Definition: Constants.h:500
Eigen::SparseMatrixBase
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:301
Eigen::internal::permutation_matrix_product< ExpressionType, Side, Transposed, SparseShape >::run
static void run(Dest &dst, const PermutationType &perm, const ExpressionType &xpr)
Definition: SparsePermutation.h:38
Eigen::Matrix< StorageIndex, Dynamic, 1 >
Eigen::SparseMatrix::insertByOuterInner
Scalar & insertByOuterInner(Index j, Index i)
Definition: SparseMatrix.h:460
internal
Definition: BandTriangularSolver.h:13
Eigen::ColMajor
@ ColMajor
Definition: Constants.h:319
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
Eigen::EvalBeforeNestingBit
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:70
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, SparseShape, PermutationShape >::m_result
PlainObject m_result
Definition: SparsePermutation.h:138
Eigen::internal::permutation_matrix_product< ExpressionType, Side, Transposed, SparseShape >::ReturnType
internal::conditional< MoveOuter, SparseMatrix< Scalar, SrcStorageOrder, StorageIndex >, SparseMatrix< Scalar, int(SrcStorageOrder)==RowMajor?ColMajor:RowMajor, StorageIndex > >::type ReturnType
Definition: SparsePermutation.h:35
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, PermutationShape, SparseShape >::PlainObject
permutation_matrix_product< Rhs, OnTheLeft, false, SparseShape >::ReturnType PlainObject
Definition: SparsePermutation.h:100
Eigen::internal::product_evaluator< Product< Lhs, Rhs, AliasFreeProduct >, ProductTag, SparseShape, PermutationShape >::product_evaluator
product_evaluator(const XprType &xpr)
Definition: SparsePermutation.h:130
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::PermutationShape
Definition: Constants.h:535


gtsam
Author(s):
autogenerated on Wed May 15 2024 15:22:15