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 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 namespace internal {
20 
21 template<typename ExpressionType, typename PlainObjectType, bool NeedEval = !is_same<ExpressionType, PlainObjectType>::value>
22 struct XprHelper
23 {
24  XprHelper(const ExpressionType& xpr) : m_xpr(xpr) {}
25  inline const PlainObjectType& xpr() const { return m_xpr; }
26  // this is a new PlainObjectType initialized by xpr
27  const PlainObjectType m_xpr;
28 };
29 template<typename ExpressionType, typename PlainObjectType>
30 struct XprHelper<ExpressionType, PlainObjectType, false>
31 {
32  XprHelper(const ExpressionType& xpr) : m_xpr(xpr) {}
33  inline const PlainObjectType& xpr() const { return m_xpr; }
34  // this is a reference to xpr
35  const PlainObjectType& m_xpr;
36 };
37 
38 template<typename PermDerived, bool NeedInverseEval>
39 struct PermHelper
40 {
41  using IndicesType = typename PermDerived::IndicesType;
42  using PermutationIndex = typename IndicesType::Scalar;
43  using type = PermutationMatrix<IndicesType::SizeAtCompileTime, IndicesType::MaxSizeAtCompileTime, PermutationIndex>;
44  PermHelper(const PermDerived& perm) : m_perm(perm.inverse()) {}
45  inline const type& perm() const { return m_perm; }
46  // this is a new PermutationMatrix initialized by perm.inverse()
47  const type m_perm;
48 };
49 template<typename PermDerived>
50 struct PermHelper<PermDerived, false>
51 {
52  using type = PermDerived;
53  PermHelper(const PermDerived& perm) : m_perm(perm) {}
54  inline const type& perm() const { return m_perm; }
55  // this is a reference to perm
56  const type& m_perm;
57 };
58 
59 template<typename ExpressionType, int Side, bool Transposed>
60 struct permutation_matrix_product<ExpressionType, Side, Transposed, SparseShape>
61 {
62  using MatrixType = typename nested_eval<ExpressionType, 1>::type;
63  using MatrixTypeCleaned = remove_all_t<MatrixType>;
64 
65  using Scalar = typename MatrixTypeCleaned::Scalar;
66  using StorageIndex = typename MatrixTypeCleaned::StorageIndex;
67 
68  // the actual "return type" is `Dest`. this is a temporary type
69  using ReturnType = SparseMatrix<Scalar, MatrixTypeCleaned::IsRowMajor ? RowMajor : ColMajor, StorageIndex>;
70  using TmpHelper = XprHelper<ExpressionType, ReturnType>;
71 
72  static constexpr bool NeedOuterPermutation = ExpressionType::IsRowMajor ? Side == OnTheLeft : Side == OnTheRight;
73  static constexpr bool NeedInversePermutation = Transposed ? Side == OnTheLeft : Side == OnTheRight;
74 
75  template <typename Dest, typename PermutationType>
76  static inline void permute_outer(Dest& dst, const PermutationType& perm, const ExpressionType& xpr) {
77 
78  // if ExpressionType is not ReturnType, evaluate `xpr` (allocation)
79  // otherwise, just reference `xpr`
80  // TODO: handle trivial expressions such as CwiseBinaryOp without temporary
81  const TmpHelper tmpHelper(xpr);
82  const ReturnType& tmp = tmpHelper.xpr();
83 
84  ReturnType result(tmp.rows(), tmp.cols());
85 
86  for (Index j = 0; j < tmp.outerSize(); j++) {
87  Index jp = perm.indices().coeff(j);
88  Index jsrc = NeedInversePermutation ? jp : j;
89  Index jdst = NeedInversePermutation ? j : jp;
90  Index begin = tmp.outerIndexPtr()[jsrc];
91  Index end = tmp.isCompressed() ? tmp.outerIndexPtr()[jsrc + 1] : begin + tmp.innerNonZeroPtr()[jsrc];
92  result.outerIndexPtr()[jdst + 1] += end - begin;
93  }
94 
95  std::partial_sum(result.outerIndexPtr(), result.outerIndexPtr() + result.outerSize() + 1,
96  result.outerIndexPtr());
97  result.resizeNonZeros(result.nonZeros());
98 
99  for (Index j = 0; j < tmp.outerSize(); j++) {
100  Index jp = perm.indices().coeff(j);
101  Index jsrc = NeedInversePermutation ? jp : j;
102  Index jdst = NeedInversePermutation ? j : jp;
103  Index begin = tmp.outerIndexPtr()[jsrc];
104  Index end = tmp.isCompressed() ? tmp.outerIndexPtr()[jsrc + 1] : begin + tmp.innerNonZeroPtr()[jsrc];
105  Index target = result.outerIndexPtr()[jdst];
106  smart_copy(tmp.innerIndexPtr() + begin, tmp.innerIndexPtr() + end, result.innerIndexPtr() + target);
107  smart_copy(tmp.valuePtr() + begin, tmp.valuePtr() + end, result.valuePtr() + target);
108  }
109  dst = std::move(result);
110  }
111 
112  template <typename Dest, typename PermutationType>
113  static inline void permute_inner(Dest& dst, const PermutationType& perm, const ExpressionType& xpr) {
114  using InnerPermHelper = PermHelper<PermutationType, NeedInversePermutation>;
115  using InnerPermType = typename InnerPermHelper::type;
116 
117  // if ExpressionType is not ReturnType, evaluate `xpr` (allocation)
118  // otherwise, just reference `xpr`
119  // TODO: handle trivial expressions such as CwiseBinaryOp without temporary
120  const TmpHelper tmpHelper(xpr);
121  const ReturnType& tmp = tmpHelper.xpr();
122 
123  // if inverse permutation of inner indices is requested, calculate perm.inverse() (allocation)
124  // otherwise, just reference `perm`
125  const InnerPermHelper permHelper(perm);
126  const InnerPermType& innerPerm = permHelper.perm();
127 
128  ReturnType result(tmp.rows(), tmp.cols());
129 
130  for (Index j = 0; j < tmp.outerSize(); j++) {
131  Index begin = tmp.outerIndexPtr()[j];
132  Index end = tmp.isCompressed() ? tmp.outerIndexPtr()[j + 1] : begin + tmp.innerNonZeroPtr()[j];
133  result.outerIndexPtr()[j + 1] += end - begin;
134  }
135 
136  std::partial_sum(result.outerIndexPtr(), result.outerIndexPtr() + result.outerSize() + 1, result.outerIndexPtr());
137  result.resizeNonZeros(result.nonZeros());
138 
139  for (Index j = 0; j < tmp.outerSize(); j++) {
140  Index begin = tmp.outerIndexPtr()[j];
141  Index end = tmp.isCompressed() ? tmp.outerIndexPtr()[j + 1] : begin + tmp.innerNonZeroPtr()[j];
142  Index target = result.outerIndexPtr()[j];
143  std::transform(tmp.innerIndexPtr() + begin, tmp.innerIndexPtr() + end, result.innerIndexPtr() + target,
144  [&innerPerm](StorageIndex i) { return innerPerm.indices().coeff(i); });
145  smart_copy(tmp.valuePtr() + begin, tmp.valuePtr() + end, result.valuePtr() + target);
146  }
147  // the inner indices were permuted, and must be sorted
148  result.sortInnerIndices();
149  dst = std::move(result);
150  }
151 
152  template <typename Dest, typename PermutationType, bool DoOuter = NeedOuterPermutation, std::enable_if_t<DoOuter, int> = 0>
153  static inline void run(Dest& dst, const PermutationType& perm, const ExpressionType& xpr) { permute_outer(dst, perm, xpr); }
154 
155  template <typename Dest, typename PermutationType, bool DoOuter = NeedOuterPermutation, std::enable_if_t<!DoOuter, int> = 0>
156  static inline void run(Dest& dst, const PermutationType& perm, const ExpressionType& xpr) { permute_inner(dst, perm, xpr); }
157 };
158 
159 }
160 
161 namespace internal {
162 
163 template <int ProductTag> struct product_promote_storage_type<Sparse, PermutationStorage, ProductTag> { typedef Sparse ret; };
164 template <int ProductTag> struct product_promote_storage_type<PermutationStorage, Sparse, ProductTag> { typedef Sparse ret; };
165 
166 // TODO, the following two overloads are only needed to define the right temporary type through
167 // typename traits<permutation_sparse_matrix_product<Rhs,Lhs,OnTheRight,false> >::ReturnType
168 // whereas it should be correctly handled by traits<Product<> >::PlainObject
169 
170 template<typename Lhs, typename Rhs, int ProductTag>
171 struct product_evaluator<Product<Lhs, Rhs, AliasFreeProduct>, ProductTag, PermutationShape, SparseShape>
172  : public evaluator<typename permutation_matrix_product<Rhs,OnTheLeft,false,SparseShape>::ReturnType>
173 {
174  typedef Product<Lhs, Rhs, AliasFreeProduct> XprType;
175  typedef typename permutation_matrix_product<Rhs,OnTheLeft,false,SparseShape>::ReturnType PlainObject;
176  typedef evaluator<PlainObject> Base;
177 
178  enum {
179  Flags = Base::Flags | EvalBeforeNestingBit
180  };
181 
182  explicit product_evaluator(const XprType& xpr)
183  : m_result(xpr.rows(), xpr.cols())
184  {
185  internal::construct_at<Base>(this, m_result);
186  generic_product_impl<Lhs, Rhs, PermutationShape, SparseShape, ProductTag>::evalTo(m_result, xpr.lhs(), xpr.rhs());
187  }
188 
189 protected:
190  PlainObject m_result;
191 };
192 
193 template<typename Lhs, typename Rhs, int ProductTag>
194 struct product_evaluator<Product<Lhs, Rhs, AliasFreeProduct>, ProductTag, SparseShape, PermutationShape >
195  : public evaluator<typename permutation_matrix_product<Lhs,OnTheRight,false,SparseShape>::ReturnType>
196 {
197  typedef Product<Lhs, Rhs, AliasFreeProduct> XprType;
198  typedef typename permutation_matrix_product<Lhs,OnTheRight,false,SparseShape>::ReturnType PlainObject;
199  typedef evaluator<PlainObject> Base;
200 
201  enum {
202  Flags = Base::Flags | EvalBeforeNestingBit
203  };
204 
205  explicit product_evaluator(const XprType& xpr)
206  : m_result(xpr.rows(), xpr.cols())
207  {
208  ::new (static_cast<Base*>(this)) Base(m_result);
209  generic_product_impl<Lhs, Rhs, SparseShape, PermutationShape, ProductTag>::evalTo(m_result, xpr.lhs(), xpr.rhs());
210  }
211 
212 protected:
213  PlainObject m_result;
214 };
215 
216 } // end namespace internal
217 
220 template <typename SparseDerived, typename PermDerived>
221 inline const Product<SparseDerived, PermDerived, AliasFreeProduct> operator*(
222  const SparseMatrixBase<SparseDerived>& matrix, const PermutationBase<PermDerived>& perm) {
223  return Product<SparseDerived, PermDerived, AliasFreeProduct>(matrix.derived(), perm.derived());
224 }
225 
228 template <typename SparseDerived, typename PermDerived>
232 }
233 
236 template <typename SparseDerived, typename PermutationType>
240 }
241 
244 template <typename SparseDerived, typename PermutationType>
247  return Product<Inverse<PermutationType>, SparseDerived, AliasFreeProduct>(tperm.derived(), matrix.derived());
248 }
249 
250 } // end namespace Eigen
251 
252 #endif // EIGEN_SPARSE_SELFADJOINTVIEW_H
Matrix< float, 1, Dynamic > MatrixType
Base class for permutations.
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
Base class of any sparse matrices or sparse expressions.
static const lastp1_t end
@ OnTheLeft
Definition: Constants.h:334
@ OnTheRight
Definition: Constants.h:336
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:72
void smart_copy(const T *start, const T *end, T *target)
Definition: Memory.h:601
: InteropHeaders
Definition: Core:139
@ AliasFreeProduct
Definition: Constants.h:504
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_inverse_op< typename Derived::Scalar >, const Derived > inverse(const Eigen::ArrayBase< Derived > &x)
Derived & derived()
Definition: EigenBase.h:48
std::ptrdiff_t j