7 #ifndef NO_RANDOM_SUPPORT
9 size_t length, std::default_random_engine::result_type seed) {
11 std::default_random_engine gen(seed);
16 permutation.begin(), permutation.end(), [&gen](
size_t n) {
17 std::uniform_int_distribution<size_t> dist(0, n ? n - 1 : 0);
27 std::iota(permutation.begin(), permutation.end(),
size_t(0));
32 resize(permutation.size());
41 for (
size_t i =
size(); i-- > 0;) {
44 auto unsorted_begin = permutation.begin();
45 auto unsorted_end = permutation.begin() + i + 1;
47 auto next_element = std::find(unsorted_begin, unsorted_end, i);
50 assert(next_element != unsorted_end &&
"Invalid permutation");
52 auto swap_idx = next_element - permutation.begin();
55 std::swap(permutation[i], *next_element);
57 (*this)(i) = swap_idx;
61 #ifndef NO_IOSTREAM_SUPPORT
69 uint8_t width)
const {
70 int backup_precision = os.precision();
71 precision = precision > 0 ? precision : backup_precision;
72 width = width > 0 ? width : precision + 9;
73 os.precision(precision);
75 for (
size_t i = 0; i <
size(); ++i) {
76 os << std::setw(width) << permutation[i];
78 os.precision(backup_precision);
std::ostream & operator<<(std::ostream &os, const HouseholderQR &qr)
Print the Q and R matrices of a HouseholderQR object.
Class that represents matrices that permute the rows or columns of other matrices.
size_t size() const
Get the size of the permutation matrix.
Permutation to_permutation() const
Convert a permutation matrix into a mathematical permutation.
void resize(size_t size)
Resize the permutation matrix.
void fill_from_permutation(Permutation permutation)
Create a permutation matrix from the given permutation.
storage_t Permutation
Type that represents a permutation (in the mathematical sense, a permutation of the integers 0 throug...
static Permutation identity_permutation(size_t length)
Return the identity permutation (0, 1, 2, 3, ..., length-1).
void print(std::ostream &os, uint8_t precision=0, uint8_t width=0) const
Print a permutation matrix.
static Permutation random_permutation(size_t length, std::default_random_engine::result_type seed=std::default_random_engine::default_seed)
Return a random permutation of the integers 0 through length-1.