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

#include <include/linalg/Matrix.hpp>

Detailed Description

A row vector (1×n matrix).

Examples
Basic-matrix-operations-no-iostream.ino, Basic-matrix-operations.ino, and Basics.cpp.

Definition at line 437 of file Matrix.hpp.

+ Inheritance diagram for RowVector:
+ Collaboration diagram for RowVector:

Constructors and assignment

 RowVector ()=default
 Default constructor. More...
 
 RowVector (size_t size)
 Create a row vector of the given size. More...
 
 RowVector (std::initializer_list< double > init)
 Create a row vector from the given list of values. More...
 
RowVectoroperator= (std::initializer_list< double > init)
 Assign a list of values to the column vector. More...
 
 RowVector (const Matrix &matrix)
 Convert an m×n matrix to a mn row vector. More...
 
 RowVector (Matrix &&matrix)
 Convert an m×n matrix to a mn row vector. More...
 

Vector size

void resize (size_t size)
 Resize the vector. More...
 
size_t size () const
 Get the number of elements in the vector. More...
 
void reshape (size_t, size_t)=delete
 Reshaping a vector to a matrix requires an explicit cast. More...
 
Matrix reshaped (size_t, size_t)=delete
 Reshaping a vector to a matrix requires an explicit cast. More...
 

Dot products

double dot (const RowVector &b) const &
 Compute the dot product of this vector with another vector. More...
 
double dot (const RowVector &b) &&
 Compute the dot product of this vector with another vector. More...
 
double dot (RowVector &&b) const &
 Compute the dot product of this vector with another vector. More...
 
double dot (RowVector &&b) &&
 Compute the dot product of this vector with another vector. More...
 
static double dot (const RowVector &a, const RowVector &b)
 Compute the dot product of two vectors. More...
 
static double dot (RowVector &&a, const RowVector &b)
 Compute the dot product of two vectors. More...
 
static double dot (const RowVector &a, RowVector &&b)
 Compute the dot product of two vectors. More...
 
static double dot (RowVector &&a, RowVector &&b)
 Compute the dot product of two vectors. More...
 

Cross products

RowVector cross (const RowVector &b) const &
 Compute the cross product of this 3-vector with another 3-vector. More...
 
RowVector && cross (const RowVector &b) &&
 Compute the cross product of this 3-vector with another 3-vector,. More...
 
RowVector && cross (RowVector &&b) const &
 Compute the cross product of this 3-vector with another 3-vector,. More...
 
RowVector && cross (RowVector &&b) &&
 Compute the cross product of this 3-vector with another 3-vector,. More...
 
static void cross_inplace (RowVector &a, const RowVector &b)
 Compute the cross product of two 3-vectors, overwriting the first vector with the result. More...
 
static void cross_inplace (RowVector &a, RowVector &&b)
 Compute the cross product of two 3-vectors, overwriting the first vector with the result. More...
 
static void cross_inplace_neg (RowVector &a, const RowVector &b)
 Compute the opposite of the cross product of two 3-vectors, overwriting the first vector with the result. More...
 
static void cross_inplace_neg (RowVector &a, RowVector &&b)
 Compute the opposite of the cross product of two 3-vectors, overwriting the first vector with the result. More...
 
static RowVector cross (const RowVector &a, const RowVector &b)
 Compute the cross product of two 3-vectors. More...
 
static RowVector && cross (RowVector &&a, const RowVector &b)
 Compute the cross product of two 3-vectors. More...
 
static RowVector && cross (const RowVector &a, RowVector &&b)
 Compute the cross product of two 3-vectors. More...
 
static RowVector && cross (RowVector &&a, RowVector &&b)
 Compute the cross product of two 3-vectors. More...
 

Vector norms

double norm2 () const &
 Compute the 2-norm of the vector. More...
 
double norm2 () &&
 Compute the 2-norm of the vector. 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...
 
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...
 
void print (Print &print, uint8_t precision=0, uint8_t width=0) const
 Print a matrix. More...
 
size_t printTo (Print &print) const override
 Implements the Arduino Printable interface. More...
 

Creating special row vectors

static RowVector ones (size_t size)
 Create a row vector filled with ones. More...
 
static RowVector zeros (size_t size)
 Create a row vector filled with zeros. More...
 
static RowVector constant (size_t size, double value)
 Create a row vector filled with a constant value. More...
 
static RowVector random (size_t size, double min=0, double max=1, std::default_random_engine::result_type seed=std::default_random_engine::default_seed)
 Create a row vector with uniformly distributed random values. 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 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...
 
Print & operator<< (Print &p, const Matrix &M)
 Print a matrix. More...
 

Member Typedef Documentation

◆ storage_t

using storage_t = util::storage_t<double>
privateinherited

Container to store the elements of the matrix internally.

Definition at line 40 of file Matrix.hpp.

Constructor & Destructor Documentation

◆ RowVector() [1/5]

RowVector ( )
default

Default constructor.

◆ RowVector() [2/5]

RowVector ( size_t  size)
inline

Create a row vector of the given size.

Definition at line 446 of file Matrix.hpp.

◆ RowVector() [3/5]

RowVector ( std::initializer_list< double >  init)
inline

Create a row vector from the given list of values.

Definition at line 449 of file Matrix.hpp.

◆ RowVector() [4/5]

RowVector ( const Matrix matrix)
explicit

Convert an m×n matrix to a mn row vector.

Definition at line 433 of file Matrix.cpp.

◆ RowVector() [5/5]

RowVector ( Matrix &&  matrix)
explicit

Convert an m×n matrix to a mn row vector.

Definition at line 436 of file Matrix.cpp.

Member Function Documentation

◆ operator=()

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

Assign a list of values to the column vector.

Definition at line 439 of file Matrix.cpp.

◆ resize()

void resize ( size_t  size)
inline

Resize the vector.

Definition at line 466 of file Matrix.hpp.

◆ size()

size_t size ( ) const
inline

Get the number of elements in the vector.

Definition at line 469 of file Matrix.hpp.

◆ reshape()

void reshape ( size_t  ,
size_t   
)
delete

Reshaping a vector to a matrix requires an explicit cast.

◆ reshaped() [1/2]

Matrix reshaped ( size_t  ,
size_t   
)
delete

Reshaping a vector to a matrix requires an explicit cast.

◆ ones() [1/2]

RowVector ones ( size_t  size)
static

Create a row vector filled with ones.

Definition at line 448 of file Matrix.cpp.

◆ zeros() [1/2]

RowVector zeros ( size_t  size)
static

Create a row vector filled with zeros.

Definition at line 452 of file Matrix.cpp.

◆ constant() [1/2]

RowVector constant ( size_t  size,
double  value 
)
static

Create a row vector filled with a constant value.

Definition at line 456 of file Matrix.cpp.

◆ random() [1/2]

RowVector random ( size_t  size,
double  min = 0,
double  max = 1,
std::default_random_engine::result_type  seed = std::default_random_engine::default_seed 
)
static

Create a row vector with uniformly distributed random values.

Definition at line 461 of file Matrix.cpp.

◆ dot() [1/8]

double dot ( const RowVector a,
const RowVector b 
)
static

Compute the dot product of two vectors.

Examples
Basic-matrix-operations-no-iostream.ino, Basic-matrix-operations.ino, and Basics.cpp.

Definition at line 471 of file Matrix.cpp.

◆ dot() [2/8]

double dot ( RowVector &&  a,
const RowVector b 
)
static

Compute the dot product of two vectors.

Definition at line 475 of file Matrix.cpp.

◆ dot() [3/8]

double dot ( const RowVector a,
RowVector &&  b 
)
static

Compute the dot product of two vectors.

Definition at line 479 of file Matrix.cpp.

◆ dot() [4/8]

double dot ( RowVector &&  a,
RowVector &&  b 
)
static

Compute the dot product of two vectors.

Definition at line 483 of file Matrix.cpp.

◆ dot() [5/8]

double dot ( const RowVector b) const &
inline

Compute the dot product of this vector with another vector.

Definition at line 512 of file Matrix.hpp.

◆ dot() [6/8]

double dot ( const RowVector b) &&
inline

Compute the dot product of this vector with another vector.

Definition at line 514 of file Matrix.hpp.

◆ dot() [7/8]

double dot ( RowVector &&  b) const &
inline

Compute the dot product of this vector with another vector.

Definition at line 516 of file Matrix.hpp.

◆ dot() [8/8]

double dot ( RowVector &&  b) &&
inline

Compute the dot product of this vector with another vector.

Definition at line 518 of file Matrix.hpp.

◆ cross_inplace() [1/2]

void cross_inplace ( RowVector a,
const RowVector b 
)
static

Compute the cross product of two 3-vectors, overwriting the first vector with the result.

Definition at line 491 of file Matrix.cpp.

◆ cross_inplace() [2/2]

void cross_inplace ( RowVector a,
RowVector &&  b 
)
static

Compute the cross product of two 3-vectors, overwriting the first vector with the result.

Definition at line 494 of file Matrix.cpp.

◆ cross_inplace_neg() [1/2]

void cross_inplace_neg ( RowVector a,
const RowVector b 
)
static

Compute the opposite of the cross product of two 3-vectors, overwriting the first vector with the result.

Definition at line 498 of file Matrix.cpp.

◆ cross_inplace_neg() [2/2]

void cross_inplace_neg ( RowVector a,
RowVector &&  b 
)
static

Compute the opposite of the cross product of two 3-vectors, overwriting the first vector with the result.

Definition at line 501 of file Matrix.cpp.

◆ cross() [1/8]

RowVector cross ( const RowVector a,
const RowVector b 
)
static

Compute the cross product of two 3-vectors.

Examples
Basic-matrix-operations-no-iostream.ino, Basic-matrix-operations.ino, and Basics.cpp.

Definition at line 506 of file Matrix.cpp.

◆ cross() [2/8]

RowVector && cross ( RowVector &&  a,
const RowVector b 
)
static

Compute the cross product of two 3-vectors.

Definition at line 511 of file Matrix.cpp.

◆ cross() [3/8]

RowVector && cross ( const RowVector a,
RowVector &&  b 
)
static

Compute the cross product of two 3-vectors.

Definition at line 515 of file Matrix.cpp.

◆ cross() [4/8]

RowVector && cross ( RowVector &&  a,
RowVector &&  b 
)
static

Compute the cross product of two 3-vectors.

Definition at line 519 of file Matrix.cpp.

◆ cross() [5/8]

RowVector cross ( const RowVector b) const &

Compute the cross product of this 3-vector with another 3-vector.

Definition at line 524 of file Matrix.cpp.

◆ cross() [6/8]

RowVector && cross ( const RowVector b) &&

Compute the cross product of this 3-vector with another 3-vector,.

Definition at line 527 of file Matrix.cpp.

◆ cross() [7/8]

RowVector && cross ( RowVector &&  b) const &

Compute the cross product of this 3-vector with another 3-vector,.

Definition at line 530 of file Matrix.cpp.

◆ cross() [8/8]

RowVector && cross ( RowVector &&  b) &&

Compute the cross product of this 3-vector with another 3-vector,.

Definition at line 533 of file Matrix.cpp.

◆ norm2() [1/2]

double norm2 ( ) const &

Compute the 2-norm of the vector.

Definition at line 541 of file Matrix.cpp.

◆ norm2() [2/2]

double norm2 ( ) &&

Compute the 2-norm of the vector.

Definition at line 543 of file Matrix.cpp.

◆ rows()

size_t rows ( ) const
inlineinherited

Get the number of rows of the matrix.

Examples
Basics.cpp.

Definition at line 81 of file Matrix.hpp.

◆ cols()

size_t cols ( ) const
inlineinherited

Get the number of columns of the matrix.

Examples
Basics.cpp.

Definition at line 83 of file Matrix.hpp.

◆ num_elems()

size_t num_elems ( ) const
inlineinherited

Get the number of elements in the matrix:

Examples
Basics.cpp.

Definition at line 85 of file Matrix.hpp.

◆ reshaped() [2/2]

Matrix reshaped ( size_t  newrows,
size_t  newcols 
) const
inherited

Create a reshaped copy of the matrix.

See also
reshape

Definition at line 84 of file Matrix.cpp.

◆ operator()() [1/4]

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

Get the element at the given position in the matrix.

Definition at line 94 of file Matrix.cpp.

◆ operator()() [2/4]

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

Get the element at the given position in the matrix.

Definition at line 102 of file Matrix.cpp.

◆ operator()() [3/4]

double& operator() ( size_t  index)
inlineinherited

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

Definition at line 107 of file Matrix.hpp.

◆ operator()() [4/4]

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

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

Definition at line 109 of file Matrix.hpp.

◆ clear_and_deallocate()

void clear_and_deallocate ( )
inherited

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

Definition at line 114 of file Matrix.cpp.

◆ fill()

void fill ( double  value)
inherited

Fill the matrix with a constant value.

Definition at line 125 of file Matrix.cpp.

◆ fill_identity()

void fill_identity ( )
inherited

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

Definition at line 129 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 
)
inherited

Fill the matrix with uniformly distributed random values.

Definition at line 136 of file Matrix.cpp.

◆ ones() [2/2]

Matrix ones ( size_t  rows,
size_t  cols 
)
staticinherited

Create a matrix filled with ones.

Examples
Basics.cpp.

Definition at line 148 of file Matrix.cpp.

◆ zeros() [2/2]

Matrix zeros ( size_t  rows,
size_t  cols 
)
staticinherited

Create a matrix filled with zeros.

Examples
Basics.cpp.

Definition at line 152 of file Matrix.cpp.

◆ constant() [2/2]

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

Create a matrix filled with a constant value.

Examples
Basics.cpp.

Definition at line 157 of file Matrix.cpp.

◆ identity() [1/2]

Matrix identity ( size_t  rows,
size_t  cols 
)
staticinherited

Create an identity matrix.

Examples
Basics.cpp.

Definition at line 163 of file Matrix.cpp.

◆ identity() [2/2]

Matrix identity ( size_t  rows)
staticinherited

Create a square identity matrix.

Definition at line 169 of file Matrix.cpp.

◆ random() [2/2]

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 
)
staticinherited

Create a matrix with uniformly distributed random values.

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

Definition at line 172 of file Matrix.cpp.

◆ swap_rows()

void swap_rows ( size_t  a,
size_t  b 
)
inherited

Swap two rows of the matrix.

Definition at line 189 of file Matrix.cpp.

◆ swap_columns()

void swap_columns ( size_t  a,
size_t  b 
)
inherited

Swap two columns of the matrix.

Definition at line 184 of file Matrix.cpp.

◆ operator==()

bool operator== ( const Matrix other) const
inherited

Check for equality of two matrices.

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

Definition at line 198 of file Matrix.cpp.

◆ operator!=()

bool operator!= ( const Matrix other) const
inlineinherited

Check for inequality of two matrices.

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

Definition at line 193 of file Matrix.hpp.

◆ normFro() [1/2]

double normFro ( ) const &
inherited

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:219
static double dot_unchecked(const Matrix &a, const Matrix &b)
Compute the dot product of two vectors.
Definition: Matrix.cpp:306

Definition at line 219 of file Matrix.cpp.

◆ normFro() [2/2]

double normFro ( ) &&
inherited

Compute the Frobenius norm of the matrix.

Definition at line 228 of file Matrix.cpp.

◆ begin() [1/2]

storage_t::iterator begin ( )
inlineinherited

Get the iterator to the first element of the matrix.

Examples
QR-PerfTest.cpp.

Definition at line 213 of file Matrix.hpp.

◆ begin() [2/2]

storage_t::const_iterator begin ( ) const
inlineinherited

Get the iterator to the first element of the matrix.

Definition at line 215 of file Matrix.hpp.

◆ cbegin()

storage_t::const_iterator cbegin ( ) const
inlineinherited

Get the iterator to the first element of the matrix.

Definition at line 217 of file Matrix.hpp.

◆ end() [1/2]

storage_t::iterator end ( )
inlineinherited

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

Examples
QR-PerfTest.cpp.

Definition at line 220 of file Matrix.hpp.

◆ end() [2/2]

storage_t::const_iterator end ( ) const
inlineinherited

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

Definition at line 222 of file Matrix.hpp.

◆ cend()

storage_t::const_iterator cend ( ) const
inlineinherited

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

Definition at line 224 of file Matrix.hpp.

◆ print() [1/2]

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

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 242 of file Matrix.cpp.

◆ print() [2/2]

void print ( Print &  print,
uint8_t  precision = 0,
uint8_t  width = 0 
) const
inherited

Print a matrix.

Parameters
printThe printer 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 48 of file ArduinoMatrixPrinter.cpp.

◆ printTo()

size_t printTo ( Print &  print) const
inlineoverrideinherited

Implements the Arduino Printable interface.

Definition at line 259 of file Matrix.hpp.

Friends And Related Function Documentation

◆ operator<<() [1/2]

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

Print a matrix.

Definition at line 255 of file Matrix.cpp.

◆ operator<<() [2/2]

Print & operator<< ( Print &  p,
const Matrix M 
)
related

Print a matrix.

Definition at line 62 of file ArduinoMatrixPrinter.cpp.

Member Data Documentation

◆ rows_

size_t rows_ = 0
protectedinherited

Definition at line 268 of file Matrix.hpp.

◆ cols_

size_t cols_ = 0
protectedinherited

Definition at line 268 of file Matrix.hpp.

◆ storage

storage_t storage
protectedinherited

Definition at line 269 of file Matrix.hpp.


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