Linear Algebra  master
Accessible implementations of linear algebra algorithms
Protected Member Functions | Protected Attributes | Private Types | Related Functions | List of all members
Matrix Class Reference

#include <linalg/Matrix.hpp>

Detailed Description

General matrix class.

Examples
Basics.cpp, and QR-PerfTest.cpp.

Definition at line 25 of file Matrix.hpp.

+ Inheritance diagram for Matrix:
+ Collaboration diagram for Matrix:

Constructors and assignment

 Matrix ()=default
 Default constructor. More...
 
 Matrix (size_t rows, size_t cols)
 Create a matrix of zeros with the given dimensions. More...
 
 Matrix (std::initializer_list< std::initializer_list< double >> init)
 Create a matrix with the given values. More...
 
Matrixoperator= (std::initializer_list< std::initializer_list< double >> init)
 Assign the given values to the matrix. More...
 
 Matrix (const Matrix &)=default
 Default copy constructor. More...
 
 Matrix (Matrix &&)
 Move constructor. More...
 
Matrixoperator= (const Matrix &)=default
 Default copy assignment. More...
 
Matrixoperator= (Matrix &&)
 Move assignment. More...
 

Matrix size

size_t rows () const
 Get the number of rows of the matrix. More...
 
size_t cols () const
 Get the number of columns of the matrix. More...
 
size_t num_elems () const
 Get the number of elements in the matrix: More...
 
void reshape (size_t newrows, size_t newcols)
 Reshape the matrix. More...
 
Matrix reshaped (size_t newrows, size_t newcols) const
 Create a reshaped copy of the matrix. More...
 

Element access

double & operator() (size_t row, size_t col)
 Get the element at the given position in the matrix. More...
 
const double & operator() (size_t row, size_t col) const
 Get the element at the given position in the matrix. More...
 
double & operator() (size_t index)
 Get the element at the given position in the linearized matrix. More...
 
const double & operator() (size_t index) const
 Get the element at the given position in the linearized matrix. More...
 

Memory management

void clear_and_deallocate ()
 Set the number of rows and columns to zero, and deallocate the storage. More...
 

Filling matrices

void fill (double value)
 Fill the matrix with a constant value. More...
 
void fill_identity ()
 Fill the matrix as an identity matrix (all zeros except the diagonal which is one). More...
 
void fill_random (double min=0, double max=1, std::default_random_engine::result_type seed=std::default_random_engine::default_seed)
 Fill the matrix with uniformly distributed random values. More...
 

Swapping rows and columns

void swap_rows (size_t a, size_t b)
 Swap two rows of the matrix. More...
 
void swap_columns (size_t a, size_t b)
 Swap two columns of the matrix. More...
 

Comparison

bool operator== (const Matrix &other) const
 Check for equality of two matrices. More...
 
bool operator!= (const Matrix &other) const
 Check for inequality of two matrices. More...
 

Matrix norms

double normFro () const &
 Compute the Frobenius norm of the matrix. More...
 
double normFro () &&
 Compute the Frobenius norm of the matrix. More...
 

Iterators

storage_t::iterator begin ()
 Get the iterator to the first element of the matrix. More...
 
storage_t::const_iterator begin () const
 Get the iterator to the first element of the matrix. More...
 
storage_t::const_iterator cbegin () const
 Get the iterator to the first element of the matrix. More...
 
storage_t::iterator end ()
 Get the iterator to the element past the end of the matrix. More...
 
storage_t::const_iterator end () const
 Get the iterator to the element past the end of the matrix. More...
 
storage_t::const_iterator cend () const
 Get the iterator to the element past the end of the matrix. More...
 

Printing

void print (std::ostream &os, uint8_t precision=0, uint8_t width=0) const
 Print a matrix. More...
 

Create special matrices

static Matrix ones (size_t rows, size_t cols)
 Create a matrix filled with ones. More...
 
static Matrix zeros (size_t rows, size_t cols)
 Create a matrix filled with zeros. More...
 
static Matrix constant (size_t rows, size_t cols, double value)
 Create a matrix filled with a constant value. More...
 
static Matrix identity (size_t rows, size_t cols)
 Create an identity matrix. More...
 
static Matrix identity (size_t rows)
 Create a square identity matrix. More...
 
static Matrix random (size_t rows, size_t cols, double min=0, double max=1, std::default_random_engine::result_type seed=std::default_random_engine::default_seed)
 Create a matrix with uniformly distributed random values. More...
 

Protected Member Functions

 Matrix (storage_t &&storage, size_t rows, size_t cols)
 Convert raw storage to a matrix. More...
 
 Matrix (const storage_t &storage, size_t rows, size_t cols)
 Convert raw storage to a matrix. More...
 

Protected Attributes

size_t rows_ = 0
 
size_t cols_ = 0
 
storage_t storage
 

Private Types

using storage_t = util::storage_t< double >
 Container to store the elements of the matrix internally. More...
 

Related Functions

(Note that these are not member functions.)

std::ostream & operator<< (std::ostream &os, const Matrix &M)
 Print a matrix. More...
 

Member Typedef Documentation

◆ storage_t

using storage_t = util::storage_t<double>
private

Container to store the elements of the matrix internally.

Definition at line 28 of file Matrix.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/7]

Matrix ( storage_t &&  storage,
size_t  rows,
size_t  cols 
)
explicitprotected

Convert raw storage to a matrix.

Definition at line 5 of file Matrix.cpp.

◆ Matrix() [2/7]

Matrix ( const storage_t storage,
size_t  rows,
size_t  cols 
)
explicitprotected

Convert raw storage to a matrix.

Definition at line 10 of file Matrix.cpp.

◆ Matrix() [3/7]

Matrix ( )
default

Default constructor.

◆ Matrix() [4/7]

Matrix ( size_t  rows,
size_t  cols 
)

Create a matrix of zeros with the given dimensions.

Definition at line 15 of file Matrix.cpp.

◆ Matrix() [5/7]

Matrix ( std::initializer_list< std::initializer_list< double >>  init)

Create a matrix with the given values.

Definition at line 22 of file Matrix.cpp.

◆ Matrix() [6/7]

Matrix ( const Matrix )
default

Default copy constructor.

◆ Matrix() [7/7]

Matrix ( Matrix &&  other)

Move constructor.

Definition at line 20 of file Matrix.cpp.

Member Function Documentation

◆ operator=() [1/3]

Matrix & operator= ( std::initializer_list< std::initializer_list< double >>  init)

Assign the given values to the matrix.

Definition at line 41 of file Matrix.cpp.

◆ operator=() [2/3]

Matrix& operator= ( const Matrix )
default

Default copy assignment.

◆ operator=() [3/3]

Matrix & operator= ( Matrix &&  other)

Move assignment.

Definition at line 30 of file Matrix.cpp.

◆ rows()

size_t rows ( ) const
inline

Get the number of rows of the matrix.

Examples
Basics.cpp.

Definition at line 69 of file Matrix.hpp.

◆ cols()

size_t cols ( ) const
inline

Get the number of columns of the matrix.

Examples
Basics.cpp.

Definition at line 71 of file Matrix.hpp.

◆ num_elems()

size_t num_elems ( ) const
inline

Get the number of elements in the matrix:

Examples
Basics.cpp.

Definition at line 73 of file Matrix.hpp.

◆ reshape()

void reshape ( size_t  newrows,
size_t  newcols 
)

Reshape the matrix.

The new size must have the same number of elements, and the result depends on the storage order (column major order or row major order).

Definition at line 74 of file Matrix.cpp.

◆ reshaped()

Matrix reshaped ( size_t  newrows,
size_t  newcols 
) const

Create a reshaped copy of the matrix.

See also
reshape

Definition at line 80 of file Matrix.cpp.

◆ operator()() [1/4]

double & operator() ( size_t  row,
size_t  col 
)

Get the element at the given position in the matrix.

Definition at line 90 of file Matrix.cpp.

◆ operator()() [2/4]

const double & operator() ( size_t  row,
size_t  col 
) const

Get the element at the given position in the matrix.

Definition at line 98 of file Matrix.cpp.

◆ operator()() [3/4]

double& operator() ( size_t  index)
inline

Get the element at the given position in the linearized matrix.

Definition at line 95 of file Matrix.hpp.

◆ operator()() [4/4]

const double& operator() ( size_t  index) const
inline

Get the element at the given position in the linearized matrix.

Definition at line 97 of file Matrix.hpp.

◆ clear_and_deallocate()

void clear_and_deallocate ( )

Set the number of rows and columns to zero, and deallocate the storage.

Definition at line 110 of file Matrix.cpp.

◆ fill()

void fill ( double  value)

Fill the matrix with a constant value.

Definition at line 121 of file Matrix.cpp.

◆ fill_identity()

void fill_identity ( )

Fill the matrix as an identity matrix (all zeros except the diagonal which is one).

Definition at line 125 of file Matrix.cpp.

◆ fill_random()

void fill_random ( double  min = 0,
double  max = 1,
std::default_random_engine::result_type  seed = std::default_random_engine::default_seed 
)

Fill the matrix with uniformly distributed random values.

Definition at line 131 of file Matrix.cpp.

◆ ones()

Matrix ones ( size_t  rows,
size_t  cols 
)
static

Create a matrix filled with ones.

Examples
Basics.cpp.

Definition at line 142 of file Matrix.cpp.

◆ zeros()

Matrix zeros ( size_t  rows,
size_t  cols 
)
static

Create a matrix filled with zeros.

Examples
Basics.cpp.

Definition at line 146 of file Matrix.cpp.

◆ constant()

Matrix constant ( size_t  rows,
size_t  cols,
double  value 
)
static

Create a matrix filled with a constant value.

Examples
Basics.cpp.

Definition at line 151 of file Matrix.cpp.

◆ identity() [1/2]

Matrix identity ( size_t  rows,
size_t  cols 
)
static

Create an identity matrix.

Examples
Basics.cpp.

Definition at line 157 of file Matrix.cpp.

◆ identity() [2/2]

Matrix identity ( size_t  rows)
static

Create a square identity matrix.

Definition at line 163 of file Matrix.cpp.

◆ random()

Matrix random ( size_t  rows,
size_t  cols,
double  min = 0,
double  max = 1,
std::default_random_engine::result_type  seed = std::default_random_engine::default_seed 
)
static

Create a matrix with uniformly distributed random values.

Examples
Basics.cpp, and QR-PerfTest.cpp.

Definition at line 165 of file Matrix.cpp.

◆ swap_rows()

void swap_rows ( size_t  a,
size_t  b 
)

Swap two rows of the matrix.

Definition at line 181 of file Matrix.cpp.

◆ swap_columns()

void swap_columns ( size_t  a,
size_t  b 
)

Swap two columns of the matrix.

Definition at line 176 of file Matrix.cpp.

◆ operator==()

bool operator== ( const Matrix other) const

Check for equality of two matrices.

Warning
Uses exact comparison, which is often not appropriate for floating point numbers.

Definition at line 190 of file Matrix.cpp.

◆ operator!=()

bool operator!= ( const Matrix other) const
inline

Check for inequality of two matrices.

Warning
Uses exact comparison, which is often not appropriate for floating point numbers.

Definition at line 177 of file Matrix.hpp.

◆ normFro() [1/2]

double normFro ( ) const &

Compute the Frobenius norm of the matrix.

Implementation

double Matrix::normFro() const & {
// Reinterpret the matrix as one big vector, and compute the dot product
// with itself. This is the 2-norm of the vector squared, so the Frobenius
// norm of the matrix is the square root of this dot product.
// ‖A‖f = ‖vec(A)‖₂ = √(vec(A)ᵀvec(A))
return std::sqrt(Vector::dot_unchecked(*this, *this));
}
double normFro() const &
Compute the Frobenius norm of the matrix.
Definition: Matrix.cpp:211
static double dot_unchecked(const Matrix &a, const Matrix &b)
Compute the dot product of two vectors.
Definition: Matrix.cpp:292

Definition at line 211 of file Matrix.cpp.

◆ normFro() [2/2]

double normFro ( ) &&

Compute the Frobenius norm of the matrix.

Definition at line 220 of file Matrix.cpp.

◆ begin() [1/2]

storage_t::iterator begin ( )
inline

Get the iterator to the first element of the matrix.

Examples
QR-PerfTest.cpp.

Definition at line 197 of file Matrix.hpp.

◆ begin() [2/2]

storage_t::const_iterator begin ( ) const
inline

Get the iterator to the first element of the matrix.

Definition at line 199 of file Matrix.hpp.

◆ cbegin()

storage_t::const_iterator cbegin ( ) const
inline

Get the iterator to the first element of the matrix.

Definition at line 201 of file Matrix.hpp.

◆ end() [1/2]

storage_t::iterator end ( )
inline

Get the iterator to the element past the end of the matrix.

Examples
QR-PerfTest.cpp.

Definition at line 204 of file Matrix.hpp.

◆ end() [2/2]

storage_t::const_iterator end ( ) const
inline

Get the iterator to the element past the end of the matrix.

Definition at line 206 of file Matrix.hpp.

◆ cend()

storage_t::const_iterator cend ( ) const
inline

Get the iterator to the element past the end of the matrix.

Definition at line 208 of file Matrix.hpp.

◆ print()

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

Print a 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 232 of file Matrix.cpp.

Friends And Related Function Documentation

◆ operator<<()

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

Print a matrix.

Definition at line 245 of file Matrix.cpp.

Member Data Documentation

◆ rows_

size_t rows_ = 0
protected

Definition at line 231 of file Matrix.hpp.

◆ cols_

size_t cols_ = 0
protected

Definition at line 231 of file Matrix.hpp.

◆ storage

storage_t storage
protected

Definition at line 232 of file Matrix.hpp.


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