Class matrix¶
The castor framework implements its own templatized class matrix<T>
where T
can be for example a float
, int
, std::complex<double>
, etc. At its core, it is built over a std::vector<T>
which holds the values. The class matrix
itself provides many useful functions and operators (addition, multiplication, indexing, etc.). It is designed such that it should feel like using Matlab or Numpy arrays. The user will find here all the available constructors. Specific builders (ones, eye, etc.) may be found at Builders.
-
template<typename T = double>
class castor::matrix¶ Array with element of type T (double by default).
Matrix values are stored in std::vector<T> object, with row-major layout. C++ numbering [0,n( is used for linear indexing A(l) and bi-linear A(i,j). Dimensions and indexing are of type ‘std::size_t’, and boolean are given in ‘logical’ type (which is ‘std::uint8_t’), instead of standard ‘bool’.
matrix<logical> s = true; matrix<int> X = {1, 2, 3, 4}; matrix<double> A = {{1, 2, 3, 4}, {5, 6, 7, 8}}; auto B = A;
Constructors
-
matrix()¶
Default constructor.
Builds an empty matrix.
matrix<logical> A; disp(A);
-
matrix(T v)¶
Builds a (1x1) matrix from fundamental type constant or variable.
matrix<int> A(M_PI); disp(A);
-
matrix(std::initializer_list<T> const &v)¶
Builds a row matrix from initializer list.
matrix<float> A({0,1,2,M_PI}); disp(A);
-
matrix(std::initializer_list<std::vector<T>> const &v)¶
Builds a matrix from nested initializer list.
matrix<double> A({{0,1,2,M_PI},{4,5,6,7},{8,9,10,11}}); disp(A);
-
matrix(std::size_t m, std::size_t n, T v = 0)¶
Builds a (nxm) matrix whose all coefficients are equal to value of v.
matrix<std::complex<double>> A(2,3,std::complex<double>(1,M_PI)); disp(A);
-
template<typename S>
matrix(std::vector<S> const &v)¶ Builds a row matrix from vector of standard library.
matrix<double> A(std::vector<int>({0, 1, 2, 3})); disp(A);
-
template<typename S = double>
matrix(std::size_t m, std::size_t n, std::vector<S> const &v)¶ Builds a (nxm) matrix from vector of standard library or initializer list.
matrix<> A(2,2,{0, 1, 2, 3}); disp(A);
Internal operators
-
template<typename S>
explicit operator S() const¶ Converts a (1x1) to fundamental type variable.
matrix<int> A(3); a = double(A); disp(a);
-
matrix<T> &operator+=(matrix<T> const &A)¶
Performs addition assignment.
If A is fundamental type variable, add value of A to each element of the matrix If A is a matrix, add each element of A to each element of the matrix, in this case matrix dimensions must agree.
matrix<float> V({0,1,2,3}); V += 1; disp(V); V += {1,1,1,1}; disp(V);
-
matrix<T> &operator-=(matrix<T> const &A)¶
Performs substraction assignment.
If A is fundamental type variable, substract value of A to each element of the matrix.
If A is a matrix, substract each element of A to each element of the matrix, in this case matrix dimensions must agree.matrix<float> V({0,1,2,3}); V -= 1; disp(V); V -= matrix<int>(1,4,1); disp(V);
-
matrix<T> &operator*=(matrix<T> const &A)¶
Performs multiplication assignment.
If A is fundamental type variable, multiply value of A to each element of the matrix.
If A is a matrix, multiply each element of A to each element of the matrix, in this case matrix dimensions must agree.matrix<float> V({0,1,2,3}); V *= 2; disp(V); V *= matrix<int>(1,4,2); disp(V);
-
matrix<T> &operator/=(matrix<T> const &A)¶
Performs division assignment.
If A is fundamental type variable, divide value of A to each element of the matrix.
If A is a matrix, divide each element of A to each element of the matrix, in this case matrix dimensions must agree.matrix<float> V({0,1,2,3}); V /= 2; disp(V); V /= matrix<int>(1,4,2); disp(V);
-
const T &operator()(std::size_t l) const¶
Returns an element of the matrix by giving the linear indexing of element (const accessor).
Remark : matrix values are stored in std::vector<T> object, with row-major layout.
const matrix<float> A = eye(3,4); // access to last element float coef = A(11); disp(coef);
-
T &operator()(std::size_t l)¶
Returns an element of the matrix by giving the linear indexing of element (non-const accessor).
Remark : matrix values are stored in std::vector<T> object, with row-major layout.
matrix<float> A = eye(3,4); // access to last element A(11) = 10; disp(A(11));
-
const cview<T> operator()(matrix<std::size_t> const &L) const¶
Returns a reference to a submatrix corresponding to linear indexing L (const accessor).
const matrix<float> A = eye(3,4); // access to element equal to 1 auto B = A({0,5,10}); disp(eval(B));
-
view<T> operator()(matrix<std::size_t> const &L)¶
Returns a reference to a submatrix corresponding to linear indexing L (non-const accessor).
matrix<float> A = eye(3,4); // access to element equal to 1 auto B = A({0,5,10}); disp(eval(B));
-
const T &operator()(std::size_t i, std::size_t j) const¶
Returns an element of the matrix by giving the bi-linear indexing of element (const accessor).
const matrix<int> A = eye(3,4); int coef = A(2,3); disp(coef);
-
T &operator()(std::size_t i, std::size_t j)¶
Returns an element of the matrix by giving the bi-linear indexing of element (non-const accessor).
matrix<int> A = eye(3,4); A(2,3) = 5; disp(A(2,3));
Internal tools
-
void clear()¶
Removes all values of matrix A and fix size to 0x0.
Clear should be used to free memory without deleting matrix object.
matrix<> X = {{1,2,3},{4,5,6}}; disp(X); X.clear; disp(X);
-
void reshape(std::size_t m, std::size_t n)¶
Modifies the shape of the matrix without changing its values.
matrix<> A = eye(3,2); A.reshape(2,3); disp(A);
-
void resize(std::size_t m, std::size_t n, T v = (T)NAN)¶
Resizes and replaces the values of the matrix, eventually filled with the values v specified (by default v is Nan).
matrix<> A = eye(3,2); A.resize(2,3,0); disp(A);
-
std::size_t size(int dim = 0) const¶
If dim=0 (default value) returns the number elements of the matrix else returns the lengths of the specified dimensions dim.
matrix<> A = {{1,2,3},{4,5,6}}; disp(A.size(0)); disp(A.size(1)); disp(A.size(2));
-
matrix()¶