7 size_t length, std::default_random_engine::result_type seed) {
9 std::default_random_engine gen(seed);
14 permutation.begin(), permutation.end(), [&gen](
size_t n) {
15 std::uniform_int_distribution<size_t> dist(0, n ? n - 1 : 0);
24 std::iota(permutation.begin(), permutation.end(),
size_t(0));
29 resize(permutation.size());
38 for (
size_t i =
size(); i-- > 0;) {
41 auto unsorted_begin = permutation.begin();
42 auto unsorted_end = permutation.begin() + i + 1;
44 auto next_element = std::find(unsorted_begin, unsorted_end, i);
47 assert(next_element != unsorted_end &&
"Invalid permutation");
49 auto swap_idx = next_element - permutation.begin();
52 std::swap(permutation[i], *next_element);
54 (*this)(i) = swap_idx;
61 uint8_t width)
const {
62 int backup_precision = os.precision();
63 precision = precision > 0 ? precision : backup_precision;
64 width = width > 0 ? width : precision + 9;
65 os.precision(precision);
67 for (
size_t i = 0; i <
size(); ++i) {
68 os << std::setw(width) << permutation[i];
70 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.