Linear Algebra  arduino
Accessible implementations of linear algebra algorithms
Public Types | Protected Attributes | Private Types | Related Functions | List of all members
PermutationMatrix Class Reference

#include <include/linalg/PermutationMatrix.hpp>

Detailed Description

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.

+ Collaboration diagram for PermutationMatrix:

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...
 
PermutationMatrixoperator= (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...
 
PermutationMatrixoperator= (const PermutationMatrix &)=default
 Default copy assignment. More...
 
PermutationMatrixoperator= (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...
 

Member Typedef Documentation

◆ storage_t

using storage_t = util::storage_t<size_t>
private

Container to store the elements of the permutation matrix internally.

Definition at line 14 of file PermutationMatrix.hpp.

◆ Permutation

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.

Member Enumeration Documentation

◆ Type

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.

Constructor & Destructor Documentation

◆ PermutationMatrix() [1/6]

PermutationMatrix ( )
default

Default constructor.

◆ PermutationMatrix() [2/6]

PermutationMatrix ( Type  type)
inline

Create an empty permutation matrix with the given type.

Definition at line 31 of file PermutationMatrix.hpp.

◆ PermutationMatrix() [3/6]

PermutationMatrix ( size_t  rows,
Type  type = Unspecified 
)
inline

Create a permutation matrix without permutations.

Definition at line 34 of file PermutationMatrix.hpp.

◆ PermutationMatrix() [4/6]

PermutationMatrix ( std::initializer_list< size_t >  init,
Type  type = Unspecified 
)
inline

Create a permutation matrix with the given permutation.

Definition at line 389 of file PermutationMatrix.hpp.

◆ PermutationMatrix() [5/6]

PermutationMatrix ( const PermutationMatrix )
default

Default copy constructor.

◆ PermutationMatrix() [6/6]

PermutationMatrix ( PermutationMatrix &&  other)
inline

Move constructor.

Definition at line 374 of file PermutationMatrix.hpp.

Member Function Documentation

◆ operator=() [1/3]

PermutationMatrix & operator= ( std::initializer_list< size_t >  init)
inline

Assign the given permutation to the matrix.

Definition at line 431 of file PermutationMatrix.hpp.

◆ operator=() [2/3]

PermutationMatrix& operator= ( const PermutationMatrix )
default

Default copy assignment.

◆ operator=() [3/3]

PermutationMatrix & operator= ( PermutationMatrix &&  other)
inline

Move assignment.

Definition at line 379 of file PermutationMatrix.hpp.

◆ size()

size_t size ( ) const
inline

Get the size of the permutation matrix.

Definition at line 62 of file PermutationMatrix.hpp.

◆ rows()

size_t rows ( ) const
inline

Get the number of rows of the permutation matrix.

Definition at line 64 of file PermutationMatrix.hpp.

◆ cols()

size_t cols ( ) const
inline

Get the number of columns of the permutation matrix.

Definition at line 66 of file PermutationMatrix.hpp.

◆ num_elems()

size_t num_elems ( ) const
inline

Get the number of elements in the matrix:

Definition at line 68 of file PermutationMatrix.hpp.

◆ resize()

void resize ( size_t  size)
inline

Resize the permutation matrix.

Definition at line 70 of file PermutationMatrix.hpp.

◆ operator()() [1/2]

size_t& operator() ( size_t  index)
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.

◆ operator()() [2/2]

const size_t& operator() ( size_t  index) const
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.

◆ reverse()

void reverse ( )
inline

Reverse the order of the permutations.

Definition at line 92 of file PermutationMatrix.hpp.

◆ transpose_inplace()

void transpose_inplace ( )
inline

Transpose or invert the permutation matrix.

Definition at line 95 of file PermutationMatrix.hpp.

◆ is_reversed()

bool is_reversed ( ) const
inline

Check if the permutation should be applied in reverse.

Definition at line 98 of file PermutationMatrix.hpp.

◆ get_type()

Type get_type ( ) const
inline

Get the type of permutation matrix (whether it permutes rows or columns, or unspecified).

Definition at line 102 of file PermutationMatrix.hpp.

◆ set_type()

void set_type ( Type  type)
inline

Set the type of permutation matrix (whether it permutes rows or columns, or unspecified).

Definition at line 105 of file PermutationMatrix.hpp.

◆ to_matrix()

SquareMatrix to_matrix ( Type  type = Unspecified) const
inline

Convert a permutation matrix into a full matrix.

Definition at line 395 of file PermutationMatrix.hpp.

◆ to_permutation()

PermutationMatrix::Permutation to_permutation ( ) const
inline

Convert a permutation matrix into a mathematical permutation.

Definition at line 413 of file PermutationMatrix.hpp.

◆ permute_columns()

void permute_columns ( Matrix A) const
inline

Apply the permutation to the columns of matrix A.

Definition at line 439 of file PermutationMatrix.hpp.

◆ permute_rows()

void permute_rows ( Matrix A) const
inline

Apply the permutation to the rows of matrix A.

Definition at line 456 of file PermutationMatrix.hpp.

◆ clear_and_deallocate()

void clear_and_deallocate ( )
inline

Set the size to zero, and deallocate the storage.

Definition at line 141 of file PermutationMatrix.hpp.

◆ random_permutation()

PermutationMatrix::Permutation random_permutation ( size_t  length,
std::default_random_engine::result_type  seed = std::default_random_engine::default_seed 
)
static

Return a random permutation of the integers 0 through length-1.

Definition at line 8 of file PermutationMatrix.cpp.

◆ identity_permutation()

PermutationMatrix::Permutation identity_permutation ( size_t  length)
static

Return the identity permutation (0, 1, 2, 3, ..., length-1).

Definition at line 25 of file PermutationMatrix.cpp.

◆ fill_identity()

void fill_identity ( )
inline

Fill the matrix as an identity permutation.

Definition at line 170 of file PermutationMatrix.hpp.

◆ fill_from_permutation()

void fill_from_permutation ( Permutation  permutation)

Create a permutation matrix from the given permutation.

Note
This isn't a very fast method, it's mainly used for tests. Internally, the permutation matrix is represented by a sequence of swap operations. Converting from this representation to a mathematical permutation is fast, but the other way around requires O(n²) operations (with the naive implementation used here, anyway).

Definition at line 31 of file PermutationMatrix.cpp.

◆ fill_random()

void fill_random ( std::default_random_engine::result_type  seed = std::default_random_engine::default_seed)
inline

Fill the matrix with a random permutation.

Note
This isn't a very fast method, it's mainly used for tests.

Definition at line 184 of file PermutationMatrix.hpp.

◆ identity()

static PermutationMatrix identity ( size_t  rows,
Type  type = Unspecified 
)
inlinestatic

Create an identity permutation matrix.

Definition at line 197 of file PermutationMatrix.hpp.

◆ from_permutation()

static PermutationMatrix from_permutation ( Permutation  permutation,
Type  type = Unspecified 
)
inlinestatic

Create a permutation matrix from the given permutation.

Note
This isn't a very fast method, it's mainly used for tests. Internally, the permutation matrix is represented by a sequence of swap operations. Converting from this representation to a mathematical permutation is fast, but the other way around requires O(n²) operations (with the naive implementation used here, anyway).

Definition at line 203 of file PermutationMatrix.hpp.

◆ random()

static PermutationMatrix random ( size_t  rows,
Type  type = Unspecified,
std::default_random_engine::result_type  seed = std::default_random_engine::default_seed 
)
inlinestatic

Create a random permutation matrix.

Note
This isn't a very fast method, it's mainly used for tests.

Definition at line 214 of file PermutationMatrix.hpp.

◆ begin() [1/2]

storage_t::iterator begin ( )
inline

Get the iterator to the first element of the swapping sequence.

Definition at line 230 of file PermutationMatrix.hpp.

◆ begin() [2/2]

storage_t::const_iterator begin ( ) const
inline

Get the iterator to the first element of the swapping sequence.

Definition at line 232 of file PermutationMatrix.hpp.

◆ cbegin()

storage_t::const_iterator cbegin ( ) const
inline

Get the iterator to the first element of the swapping sequence.

Definition at line 234 of file PermutationMatrix.hpp.

◆ end() [1/2]

storage_t::iterator end ( )
inline

Get the iterator to the element past the end of the swapping sequence.

Definition at line 237 of file PermutationMatrix.hpp.

◆ end() [2/2]

storage_t::const_iterator end ( ) const
inline

Get the iterator to the element past the end of the swapping sequence.

Definition at line 239 of file PermutationMatrix.hpp.

◆ cend()

storage_t::const_iterator cend ( ) const
inline

Get the iterator to the element past the end of the swapping sequence.

Definition at line 241 of file PermutationMatrix.hpp.

◆ print()

void print ( std::ostream &  os,
uint8_t  precision = 0,
uint8_t  width = 0 
) const

Print a permutation matrix.

Parameters
osThe stream to print to.
precisionThe number of significant figures to print. (0 = auto)
widthThe width of each element (number of characters). (0 = auto)

Definition at line 68 of file PermutationMatrix.cpp.

Friends And Related Function Documentation

◆ operator<<()

std::ostream & operator<< ( std::ostream &  os,
const PermutationMatrix M 
)
related

Print a permutation matrix.

Definition at line 81 of file PermutationMatrix.cpp.

Member Data Documentation

◆ storage

storage_t storage
protected

Definition at line 264 of file PermutationMatrix.hpp.

◆ reverse_

bool reverse_ = false
protected

Definition at line 265 of file PermutationMatrix.hpp.

◆ type

Type type = Unspecified
protected

Definition at line 266 of file PermutationMatrix.hpp.


The documentation for this class was generated from the following files: