SUNphi  1.0
Partition.hpp
Go to the documentation of this file.
1 #ifndef _PARTITION_HPP
2 #define _PARTITION_HPP
3 
4 /// \file Partition.hpp
5 ///
6 /// \brief Factorizes numbers into partitions
7 
8 #include <math/Factorize.hpp>
9 #include <utility/Combinatorial.hpp>
10 
11 namespace SUNphi
12 {
13  /////////////////////////////////////////////////////////////////
14 
15  namespace Impl
16  {
17  /// Loop on all factorizing partition of n
18  ///
19  /// Internal implementation, proceeds recursively calling the
20  /// function until the position is the end
21  template <typename I,
22  typename Fun>
23  I loopOnAllFactorizingPartitioning(Vector<I>& partition, ///< Progressively built partition
24  const I& n, ///< Numbr to factorize
25  const Fun& fun, ///< Function to be called
26  const int& pos=0) ///< Position filled at this iteration
27  {
28  /// Number of partition found
29  I nPart=
30  0;
31 
32  if(pos+1<partition.size())
33  loopOnAllSubmultiplesOf(n,[&partition,&n,&pos,&fun,&nPart](const I& factor)
34  {
35  partition[pos]=
36  factor;
37 
38  nPart+=
39  loopOnAllFactorizingPartitioning(partition,n/factor,fun,pos+1);
40  });
41  else
42  {
43  partition[pos]=
44  n;
45 
46  fun(partition);
47 
48  nPart=
49  1;
50  }
51 
52  return
53  nPart;
54  }
55  }
56 
57  /// Loop on all factorizing partition of n
58  ///
59  /// Returns the number of partitions obtained
60  template <typename I,
61  typename Fun>
62  I loopOnAllFactorizingPartitioning(const I& n, ///< Number to factorize
63  const Fun& fun, ///< Function to be invocated at each turn
64  const int& nFacts) ///< Size of the partition
65  {
66  /// Built partition
67  Vector<I> partition(nFacts);
68 
69  return
70  Impl::loopOnAllFactorizingPartitioning(partition,n,fun);
71  }
72 
73  /// Gets the list of all factorizing partition of n
74  template <typename I>
75  Vector<Vector<I>> listAllFactorizingPartitioning(const I& n, ///< Number to factorize
76  const int& nFacts) ///< Size of the partition
77  {
78  Vector<Vector<I>> list;
79 
80  loopOnAllFactorizingPartitioning(n,
81  [&list](const Vector<I>& partition)
82  {
83  list.push_back(partition);
84  },
85  nFacts);
86 
87  return
88  list;
89  }
90 }
91 
92 #endif
Vector< Vector< I > > listAllFactorizingPartitioning(const I &n, const int &nFacts)
Gets the list of all factorizing partition of n.
Definition: Partition.hpp:75
I loopOnAllFactorizingPartitioning(const I &n, const Fun &fun, const int &nFacts)
Definition: Partition.hpp:62
void divWithMod(Vector< TOut > &quotient, Vector< TOut > &remainder, const Vector &divisor) const
Returns the result and remainder of the division.
Definition: Vector.hpp:310
I loopOnAllFactorizingPartitioning(Vector< I > &partition, const I &n, const Fun &fun, const int &pos=0)
Definition: Partition.hpp:23