Linear Algebra
master
Accessible implementations of linear algebra algorithms
|
#include <linalg/PermutationMatrix.hpp>
Class that represents matrices that permute the rows or columns of other matrices.
Stored in an efficient manner with O(n) memory requirements.
Definition at line 11 of file PermutationMatrix.hpp.
Conversion to a full matrix or a permutation | |
using | Permutation = storage_t |
Type that represents a permutation (in the mathematical sense, a permutation of the integers 0 through n-1). More... | |
SquareMatrix | to_matrix (Type type=Unspecified) const |
Convert a permutation matrix into a full matrix. More... | |
Permutation | to_permutation () const |
Convert a permutation matrix into a mathematical permutation. More... | |
Constructors and assignment | |
PermutationMatrix ()=default | |
Default constructor. More... | |
PermutationMatrix (Type type) | |
Create an empty permutation matrix with the given type. More... | |
PermutationMatrix (size_t rows, Type type=Unspecified) | |
Create a permutation matrix without permutations. More... | |
PermutationMatrix (std::initializer_list< size_t > init, Type type=Unspecified) | |
Create a permutation matrix with the given permutation. More... | |
PermutationMatrix & | operator= (std::initializer_list< size_t > init) |
Assign the given permutation to the matrix. More... | |
PermutationMatrix (const PermutationMatrix &)=default | |
Default copy constructor. More... | |
PermutationMatrix (PermutationMatrix &&) | |
Move constructor. More... | |
PermutationMatrix & | operator= (const PermutationMatrix &)=default |
Default copy assignment. More... | |
PermutationMatrix & | operator= (PermutationMatrix &&) |
Move assignment. More... | |
Matrix size | |
size_t | size () const |
Get the size of the permutation matrix. More... | |
size_t | rows () const |
Get the number of rows of the permutation matrix. More... | |
size_t | cols () const |
Get the number of columns of the permutation matrix. More... | |
size_t | num_elems () const |
Get the number of elements in the matrix: More... | |
void | resize (size_t size) |
Resize the permutation matrix. More... | |
Element access | |
size_t & | operator() (size_t index) |
Get the element at the given position in the swap sequence. More... | |
const size_t & | operator() (size_t index) const |
Get the element at the given position in the swap sequence. More... | |
Transposition | |
void | reverse () |
Reverse the order of the permutations. More... | |
void | transpose_inplace () |
Transpose or invert the permutation matrix. More... | |
bool | is_reversed () const |
Check if the permutation should be applied in reverse. More... | |
Type | get_type () const |
Get the type of permutation matrix (whether it permutes rows or columns, or unspecified). More... | |
void | set_type (Type type) |
Set the type of permutation matrix (whether it permutes rows or columns, or unspecified). More... | |
Applying the permutation to matrices | |
void | permute_columns (Matrix &A) const |
Apply the permutation to the columns of matrix A. More... | |
void | permute_rows (Matrix &A) const |
Apply the permutation to the rows of matrix A. More... | |
Memory management | |
void | clear_and_deallocate () |
Set the size to zero, and deallocate the storage. More... | |
Filling matrices | |
void | fill_identity () |
Fill the matrix as an identity permutation. More... | |
void | fill_from_permutation (Permutation permutation) |
Create a permutation matrix from the given permutation. More... | |
void | fill_random (std::default_random_engine::result_type seed=std::default_random_engine::default_seed) |
Fill the matrix with a random permutation. More... | |
Iterators | |
storage_t::iterator | begin () |
Get the iterator to the first element of the swapping sequence. More... | |
storage_t::const_iterator | begin () const |
Get the iterator to the first element of the swapping sequence. More... | |
storage_t::const_iterator | cbegin () const |
Get the iterator to the first element of the swapping sequence. More... | |
storage_t::iterator | end () |
Get the iterator to the element past the end of the swapping sequence. More... | |
storage_t::const_iterator | end () const |
Get the iterator to the element past the end of the swapping sequence. More... | |
storage_t::const_iterator | cend () const |
Get the iterator to the element past the end of the swapping sequence. More... | |
Printing | |
void | print (std::ostream &os, uint8_t precision=0, uint8_t width=0) const |
Print a permutation matrix. More... | |
Generating permutations | |
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. More... | |
static Permutation | identity_permutation (size_t length) |
Return the identity permutation (0, 1, 2, 3, ..., length-1). More... | |
Create special matrices | |
static PermutationMatrix | identity (size_t rows, Type type=Unspecified) |
Create an identity permutation matrix. More... | |
static PermutationMatrix | from_permutation (Permutation permutation, Type type=Unspecified) |
Create a permutation matrix from the given permutation. More... | |
static PermutationMatrix | random (size_t rows, Type type=Unspecified, std::default_random_engine::result_type seed=std::default_random_engine::default_seed) |
Create a random permutation matrix. More... | |
Public Types | |
enum | Type { Unspecified = 0 , RowPermutation = 1 , ColumnPermutation = 2 } |
Protected Attributes | |
storage_t | storage |
bool | reverse_ = false |
Type | type = Unspecified |
Private Types | |
using | storage_t = util::storage_t< size_t > |
Container to store the elements of the permutation matrix internally. More... | |
Related Functions | |
(Note that these are not member functions.) | |
std::ostream & | operator<< (std::ostream &os, const PermutationMatrix &M) |
Print a permutation matrix. More... | |
|
private |
Container to store the elements of the permutation matrix internally.
Definition at line 14 of file PermutationMatrix.hpp.
using Permutation = storage_t |
Type that represents a permutation (in the mathematical sense, a permutation of the integers 0 through n-1).
Definition at line 118 of file PermutationMatrix.hpp.
enum Type |
Enumerator | |
---|---|
Unspecified | Can be used for permuting rows or columns. |
RowPermutation | Can be used for permuting rows only. |
ColumnPermutation | Can be used for permuting columns only. |
Definition at line 17 of file PermutationMatrix.hpp.
|
default |
Default constructor.
|
inline |
Create an empty permutation matrix with the given type.
Definition at line 31 of file PermutationMatrix.hpp.
|
inline |
Create a permutation matrix without permutations.
Definition at line 34 of file PermutationMatrix.hpp.
|
inline |
Create a permutation matrix with the given permutation.
Definition at line 383 of file PermutationMatrix.hpp.
|
default |
Default copy constructor.
|
inline |
Move constructor.
Definition at line 368 of file PermutationMatrix.hpp.
|
inline |
Assign the given permutation to the matrix.
Definition at line 425 of file PermutationMatrix.hpp.
|
default |
Default copy assignment.
|
inline |
Move assignment.
Definition at line 373 of file PermutationMatrix.hpp.
|
inline |
Get the size of the permutation matrix.
Definition at line 62 of file PermutationMatrix.hpp.
|
inline |
Get the number of rows of the permutation matrix.
Definition at line 64 of file PermutationMatrix.hpp.
|
inline |
Get the number of columns of the permutation matrix.
Definition at line 66 of file PermutationMatrix.hpp.
|
inline |
Get the number of elements in the matrix:
Definition at line 68 of file PermutationMatrix.hpp.
|
inline |
Resize the permutation matrix.
Definition at line 70 of file PermutationMatrix.hpp.
|
inline |
Get the element at the given position in the swap sequence.
If the k-th element is i, that is P(k) == i
, this means that the k-th step of the swapping algorithm will swap i
and k
.
Definition at line 81 of file PermutationMatrix.hpp.
|
inline |
Get the element at the given position in the swap sequence.
If the k-th element is i, that is P(k) == i
, this means that the k-th step of the swapping algorithm will swap i
and k
.
Definition at line 83 of file PermutationMatrix.hpp.
|
inline |
Reverse the order of the permutations.
Definition at line 92 of file PermutationMatrix.hpp.
|
inline |
Transpose or invert the permutation matrix.
Definition at line 95 of file PermutationMatrix.hpp.
|
inline |
Check if the permutation should be applied in reverse.
Definition at line 98 of file PermutationMatrix.hpp.
|
inline |
Get the type of permutation matrix (whether it permutes rows or columns, or unspecified).
Definition at line 102 of file PermutationMatrix.hpp.
|
inline |
Set the type of permutation matrix (whether it permutes rows or columns, or unspecified).
Definition at line 105 of file PermutationMatrix.hpp.
|
inline |
Convert a permutation matrix into a full matrix.
Definition at line 389 of file PermutationMatrix.hpp.
|
inline |
Convert a permutation matrix into a mathematical permutation.
Definition at line 407 of file PermutationMatrix.hpp.
|
inline |
Apply the permutation to the columns of matrix A.
Definition at line 433 of file PermutationMatrix.hpp.
|
inline |
Apply the permutation to the rows of matrix A.
Definition at line 450 of file PermutationMatrix.hpp.
|
inline |
Set the size to zero, and deallocate the storage.
Definition at line 141 of file PermutationMatrix.hpp.
|
static |
Return a random permutation of the integers 0 through length-1.
Definition at line 6 of file PermutationMatrix.cpp.
|
static |
Return the identity permutation (0, 1, 2, 3, ..., length-1).
Definition at line 22 of file PermutationMatrix.cpp.
|
inline |
Fill the matrix as an identity permutation.
Definition at line 168 of file PermutationMatrix.hpp.
void fill_from_permutation | ( | Permutation | permutation | ) |
Create a permutation matrix from the given permutation.
Definition at line 28 of file PermutationMatrix.cpp.
|
inline |
Fill the matrix with a random permutation.
Definition at line 181 of file PermutationMatrix.hpp.
|
inlinestatic |
Create an identity permutation matrix.
Definition at line 193 of file PermutationMatrix.hpp.
|
inlinestatic |
Create a permutation matrix from the given permutation.
Definition at line 199 of file PermutationMatrix.hpp.
|
inlinestatic |
Create a random permutation matrix.
Definition at line 209 of file PermutationMatrix.hpp.
|
inline |
Get the iterator to the first element of the swapping sequence.
Definition at line 224 of file PermutationMatrix.hpp.
|
inline |
Get the iterator to the first element of the swapping sequence.
Definition at line 226 of file PermutationMatrix.hpp.
|
inline |
Get the iterator to the first element of the swapping sequence.
Definition at line 228 of file PermutationMatrix.hpp.
|
inline |
Get the iterator to the element past the end of the swapping sequence.
Definition at line 231 of file PermutationMatrix.hpp.
|
inline |
Get the iterator to the element past the end of the swapping sequence.
Definition at line 233 of file PermutationMatrix.hpp.
|
inline |
Get the iterator to the element past the end of the swapping sequence.
Definition at line 235 of file PermutationMatrix.hpp.
void print | ( | std::ostream & | os, |
uint8_t | precision = 0 , |
||
uint8_t | width = 0 |
||
) | const |
Print a permutation matrix.
os | The stream to print to. |
precision | The number of significant figures to print. (0 = auto) |
width | The width of each element (number of characters). (0 = auto) |
Definition at line 60 of file PermutationMatrix.cpp.
|
related |
Print a permutation matrix.
Definition at line 73 of file PermutationMatrix.cpp.
|
protected |
Definition at line 258 of file PermutationMatrix.hpp.
|
protected |
Definition at line 259 of file PermutationMatrix.hpp.
|
protected |
Definition at line 260 of file PermutationMatrix.hpp.