Matrix Operations

Operators and Functions

template<typename L, typename R>
EWiseBinaryOp<L, internal::element_add, R> operator+(const MatrixBase<L> &lhs, const MatrixBase<R> &rhs)

Element-wise Addition.

Complexity: Constant

Parameters:
  • lhs[in] left hand side

  • rhs[in] right hand side

Returns:

an abstract object representing the element-wise sum of lhs and rhs

template<typename L, typename R>
EWiseBinaryOp<L, internal::element_sub, R> operator-(const MatrixBase<L> &lhs, const MatrixBase<R> &rhs)

Element-wise Subtraction.

Complexity: Constant

Parameters:
  • lhs[in] left hand side

  • rhs[in] right hand side

Returns:

an abstract object representing the element-wise subtraction of lhs by rhs

template<typename L, typename R>
EWiseBinaryOp<L, internal::element_mult, R> operator*(const MatrixBase<L> &lhs, const MatrixBase<R> &rhs)

Schur/Hadamard Product (Element-wise Multiplication).

Complexity: Constant

Parameters:
  • lhs[in] left hand side

  • rhs[in] right hand side

Returns:

an abstract object representing the element-wise multiplication of lhs and rhs

template<typename L, typename R>
EWiseBinaryOp<L, internal::element_div, R> operator/(const MatrixBase<L> &lhs, const MatrixBase<R> &rhs)

Element-wise Division.

Complexity: Constant

Parameters:
  • lhs[in] left hand side

  • rhs[in] right hand side

Returns:

an abstract object representing the element-wise division of lhs by rhs

template<typename E>
EWiseUnaryOp<internal::scalar_mult<typename E::value_type>, E> operator-(const MatrixBase<E> &expr)

Unary Minus. Modifies the sign of the expression.

Complexity: Constant

Parameters:

a[in] matrix, vector or expression

Returns:

an abstract object representing the expression with the opposite sign.

template<typename E>
EWiseUnaryOp<internal::scalar_add<typename E::value_type>, E> operator+(const MatrixBase<E> &expr, typename MatrixBase<E>::value_type scalar)
template<typename E>
EWiseUnaryOp<internal::scalar_add<typename E::value_type>, E> operator+(typename MatrixBase<E>::value_type scalar, const MatrixBase<E> &expr)

Scalar Addition. Adds a scalar to all elements in the expression.

Complexity: Constant

Parameters:
  • expr[in] a matrix, vector or expression

  • scalar[in] value to multiply the expr with

Returns:

an abstract object representing the multiplication of expr by a scalar

template<typename E>
EWiseUnaryOp<internal::scalar_right_sub<typename E::value_type>, E> operator-(const MatrixBase<E> &expr, typename MatrixBase<E>::value_type scalar)
template<typename E>
EWiseUnaryOp<internal::scalar_left_sub<typename E::value_type>, E> operator-(typename MatrixBase<E>::value_type scalar, const MatrixBase<E> &expr)

Scalar Subtraction. Subtracts a scalar from all elements in the expression.

Complexity: Constant

Parameters:
  • expr[in] a matrix, vector or expression

  • scalar[in] value to multiply the expr with

Returns:

an abstract object representing the multiplication of expr by a scalar

template<typename E>
EWiseUnaryOp<internal::scalar_mult<typename E::value_type>, E> operator*(const MatrixBase<E> &expr, typename MatrixBase<E>::value_type scalar)
template<typename E>
EWiseUnaryOp<internal::scalar_mult<typename E::value_type>, E> operator*(typename MatrixBase<E>::value_type scalar, const MatrixBase<E> &expr)

Scalar Multiplication. Multiplies all elements in the expression by a scalar.

Complexity: Constant

Parameters:
  • expr[in] a matrix, vector or expression

  • scalar[in] value to multiply the expr with

Returns:

an abstract object representing the multiplication of expr by a scalar

template<typename E>
EWiseUnaryOp<internal::scalar_right_div<typename E::value_type>, E> operator/(const MatrixBase<E> &expr, typename MatrixBase<E>::value_type scalar)
template<typename E>
EWiseUnaryOp<internal::scalar_left_div<typename E::value_type>, E> operator/(typename MatrixBase<E>::value_type scalar, const MatrixBase<E> &expr)

Scalar Division. Divides all elements in the expression by a scalar.

Complexity: Constant

Parameters:
  • expr[in] a matrix, vector or expression

  • scalar[in] value to divide the expr with

Returns:

an abstract object representing the division of expr by a scalar

template<typename E>
EWiseUnaryOp<internal::element_abs, E> abs(const MatrixBase<E> &expr)

Calculates the absolute value of all elements in the expression.

Complexity: Constant

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise absolute value of expr

template<typename E>
EWiseUnaryOp<internal::element_sqrt, E> sqrt(const MatrixBase<E> &expr)

Calculates the square root of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise square root of expr

template<typename E>
EWiseUnaryOp<internal::element_exp, E> exp(const MatrixBase<E> &expr)

Calculates the exponential of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise exponential of expr

template<typename E>
EWiseUnaryOp<internal::element_sin, E> sin(const MatrixBase<E> &expr)

Calculates the sine of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise exponential of expr

template<typename E>
EWiseUnaryOp<internal::element_cos, E> cos(const MatrixBase<E> &expr)

Calculates the cosine of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise exponential of expr

template<typename E>
EWiseUnaryOp<internal::element_tan, E> tan(const MatrixBase<E> &expr)

Calculates the tangent of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise exponential of expr

template<typename E>
EWiseUnaryOp<internal::element_floor, E> floor(const MatrixBase<E> &expr)

Calculates the floor of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise exponential of expr

template<typename E>
EWiseUnaryOp<internal::element_ceil, E> ceil(const MatrixBase<E> &expr)

Calculates the ceil of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise exponential of expr

template<typename E>
EWiseUnaryOp<internal::element_pow<typename E::value_type>, E> pow(const MatrixBase<E> &expr, typename E::value_type y)

Calculates the y-th power of all elements in the expression. Complexity: Constant

Note

Only floating-point expressions are supported.

Parameters:

expr[in] a matrix, vector or expression

Returns:

an abstract object representing the element-wise pow of expr

template<typename E>
DiagonalOp<E, 1> diag(const MatrixBase<E> &expr)

Extracts the diagonal of a matrix expression.

Complexity: Constant

Parameters:

expr[in] a matrix or matrix expression

Returns:

an abstract object containing the diagonal of expr

template<typename E>
DiagonalOp<E, 2> diag_mat(const MatrixBase<E> &expr)

Constructs a diagonal matrix from a vector expression.

Complexity: Constant

Parameters:

expr[in] a vector expression

Returns:

a diagonal matrix containing the diagonal of expr

template<typename E>
MatrixBase<E>::value_type trace(const MatrixBase<E> &expr)

Calculates the trace (i.e., the sum of all diagonal elements) of a matrix expression. Complexity: Linear in expr.size()

Parameters:

expr[in] a matrix or matrix expression

Returns:

the result of the operation

template<typename E>
internal::result_traits<E>::type eval(const MatrixBase<E> &expr)

Force the evaluation of a matrix or vector expression.

Complexity: Linear in expr.size()

Parameters:

expr[in] expression to be evaluated

Returns:

the result of the expression

template<typename E, typename Op>
EWiseUnaryOp<Op, E> unary_op(const MatrixBase<E> &expr, Op op)

Apply a unary operation op over the entries of the expression. See @rstinline :ref:custom_op @rstend in @rstinline :ref:matrix_operations @rstend for more information.

Parameters:
  • expr[in] a vector, matrix or expression

  • op[in] an element-wise unary operation

Returns:

an abstract object representing the element-wise unary operation op over expr.

template<typename LHS, typename Op, typename RHS>
EWiseBinaryOp<LHS, Op, RHS> binary_op(const MatrixBase<LHS> &lhs, const MatrixBase<RHS> &rhs, Op op)

Apply a element-wise binary operation op between the expressions lhs and rhs. See @rstinline :ref:custom_op @rstend in @rstinline :ref:matrix_operations @rstend for more information.

Parameters:
  • lhs[in] left hand side

  • rhs[in] right hand side

  • op[in] an element-wise binary operation

Returns:

an abstract object representing the element-wise binary operation op between lhs and rhs.

Special Matrices

template<typename T>
EWiseNullaryOp<T, internal::constant_matrix_op<T>, 2> zeros(index_t rows, index_t cols)

Creates a zeros matrix (i.e., all elements in the matrix is equal to0).

Complexity: Constant

Parameters:
  • rows[in] number of rows in the matrix

  • cols[in] number of columns in the matrix

Template Parameters:

type – Numeric type of the matrix

template<typename T>
EWiseNullaryOp<T, internal::constant_matrix_op<T>, 2> ones(index_t rows, index_t cols)

Creates a Ones matrix (i.e., all elements in the matrix is equal to 1).

Complexity: Constant

Parameters:
  • rows[in] number of rows in the matrix

  • cols[in] number of columns in the matrix

Template Parameters:

type – Numeric type of the matrix

template<typename T>
EWiseNullaryOp<T, internal::constant_matrix_op<T>, 2> const_matrix(index_t rows, index_t cols, T scalar)

Creates a Constant matrix (i.e., all elements in the matrix is equal to a constant value).

Complexity: Constant

Parameters:
  • rows[in] number of rows in the matrix

  • cols[in] number of columns in the matrix

  • scalar[in] constant value that all element will be equal to

Template Parameters:

type – Numeric type of the matrix

template<typename T>
EWiseNullaryOp<T, internal::eye_matrix_op<T>, 2> eye(index_t rows, index_t cols)

Creates a eye matrix (i.e., all elements in the diagonal are equal to 1, while the other are equal to 0).

Complexity: Constant

Note

The elements of this matrix can only be accessed with (row, col) operator.

Parameters:
  • rows[in] number of rows in the matrix

  • cols[in] number of columns in the matrix

Template Parameters:

type – Numeric type of the matrix

template<typename T>
EWiseNullaryOp<T, internal::arange_op<T>, 1> arange(T start, T end, T step = 1)

Creates a evenly-spaced sequence of number in the interval [start, end[.

Parameters:
  • start[in] first value of the interval

  • end[in] last value of the interval

  • step[in] spacing between values (optional, default: 1)

template<typename T>
EWiseNullaryOp<T, internal::arange_op<T>, 1> linspace(T start, T end, index_t num = 50, bool end_point = true)

Creates a evenly-spaced sequence of number between [start, end].

Parameters:
  • start[in] first value of the interval

  • end[in] last value of the interval

  • num[in] number of elements in the sequence (optional, default: 50)

  • end_point[in] Indicate if end is included in the sequence or not (optional, default: true)

template<typename T, typename Op>
EWiseNullaryOp<T, Op, 2> init_matrix(index_t nrows, index_t ncols, Op op)

Initializes a matrix based on the operation op.

Parameters:
  • nrows[in] number of rows

  • ncols[in] number of columns

  • op[in] a functor that receives the position (row, col) and returns its value.

template<typename T, typename Op>
EWiseNullaryOp<T, Op, 1> init_vector(index_t n, Op op)

Initializes a vector based on the operation op.

Parameters:
  • nrows[in] number of rows

  • ncols[in] number of columns

  • op[in] a functor that receives the position (i) and returns its value.

EWiseNullaryOp

template<typename T, typename NullaryOp, int NumDim>
class EWiseNullaryOp : public mocca::MatrixBase<EWiseNullaryOp<T, NullaryOp, NumDim>>

Abstract object representing a vector or matrix that have the value of its elements defined by a mathematical function (e.g., the eye matrix and zeros matrix).

Template Parameters:

  • type - Numeric type

  • NullaryOp - Functor containing the mathematical function

  • NumDim - Number of dimensions

Note

This class do not create an array in memory and cannot be converted to normal Matrix or Vector object.

Public Functions

inline constexpr EWiseNullaryOp(index_t rows, index_t cols, const NullaryOp &op)

EWiseNullaryOp Constructor.

Parameters:
  • rows[in] number of rows in the matrix

  • cols[in] number of columns in the matrix

  • op[in] functor containing the mathematical function Complexity: Constant

inline constexpr index_t size() const noexcept
Returns:

the expression size Complexity: Constant

inline constexpr index_t cols() const noexcept
Returns:

the number of columns of the expression Complexity: Constant

inline constexpr index_t rows() const noexcept
Returns:

the number of rows of the expression Complexity: Constant

inline constexpr const NullaryOp &op() const noexcept
Returns:

a reference to the nullary operator Complexity: Constant

EWiseUnaryOp

template<typename UnaryOp, typename Expr>
class EWiseUnaryOp : public mocca::MatrixBase<EWiseUnaryOp<UnaryOp, Expr>>

Abstract object representing an element-wise unary operation over a matrix, vector or expression. This object is returned by all methods that takes a single input expression, regardless of the presence of other input such as scalars. For example, 5 * mat is considered unary since only the right hand side is a expression and it will return a specialisation of EWiseUnaryOp.

In most cases, this object only purpose is to hold the operation until it is evaluated. Therefore, you typically do not need to use this class explicitly.

Template Parameters:

  • UnaryOp - Functor containing the operation

  • Expr - A matrix or expression type

Public Functions

inline constexpr index_t size() const
Returns:

the expression size Complexity: Constant

inline constexpr index_t cols() const
Returns:

the number of columns of the expression Complexity: Constant

inline constexpr index_t rows() const
Returns:

the number of rows of the expression Complexity: Constant

inline const Expr &expr() const
Returns:

a reference to the mat Complexity: Constant

inline const UnaryOp &op() const
Returns:

a reference to the unary operator Complexity: Constant

EWiseBinaryOp

template<typename LHS, typename BinaryOp, typename RHS>
class EWiseBinaryOp : public mocca::MatrixBase<EWiseBinaryOp<LHS, BinaryOp, RHS>>

Abstract object representing an element-wise binary operation over a matrix, vector or expression. This object is returned by all methods that have matrix, vector or expression in both its right and left hand sides. For example, mat1 + ma2 will return a specialisation of EWiseBinaryOp.

In most cases, this object only purpose is to hold the operation until it is evaluated. Therefore, you typically do not need to use this class explicitly.

Template Parameters:

  • LHS - A expression or expression type in the left hand side

  • BinaryOp - Functor containing the operation

  • RHS - A expression or expression type in the right hand side

Note

If the dimensions of the right and left hand side do not match (e.g., a vector and a matrix), MOCCA will try to broadcast one or both operands to correct dimensions (see Broadcast in the Matrix and Vector Operations for more information). In case this is not possible, it will throw a std::length_error.

Public Functions

inline index_t size() const
Returns:

the expression size Complexity: Constant

inline index_t cols() const
Returns:

the number of columns of the expression Complexity: Constant

inline index_t rows() const
Returns:

the number of rows of the expression Complexity: Constant

inline const internal::BinaryOperand<LHS, traits::broadcastLHS> &lhs() const
Returns:

a reference to the matrix, vector or expression in the left hand side Complexity: Constant

inline const internal::BinaryOperand<RHS, traits::broadcastRHS> &rhs() const
Returns:

a reference to the matrix, vector or expression in the right hand side Complexity: Constant

inline const BinaryOp &op() const
Returns:

a reference to the binary operator Complexity: Constant

Transpose

template<typename Expr>
class TransposeOp : public mocca::MatrixBase<TransposeOp<Expr>>

Abstract object representing the transpose of a matrix or expression. This object is returned when calling transpose() or T() in any matrix, vector or expression.

In most cases, this object only purpose is to hold the operation still it is evaluated. Therefore, you typically do not need to use this class explicitly.

Template Parameters:

  • Expr - A matrix, vector or expression object

Note

TransposeOp do not change how the matrix is stored.

Warning

If you want to replace the matrix by its transpose, do not use:

mat = mat.transpose(); 
Instead, do
mat.tranpose_inplace() 
to correctly transpose the matrix and allows MOCCA to optimize the operation.

Public Functions

inline const Expr &expr() const
Returns:

a reference to the matrix or expression Complexity: Constant

DiagonalOp

template<typename Expr, int NumDim>
class DiagonalOp : public mocca::MatrixBase<DiagonalOp<Expr, NumDim>>

Abstract object representing the diagonal of a matrix or expression. The diag() method returns a instance of this class with the diagonal of the expression, while diag_mat() returns an object with the diagonal matrix construct from the vector expression.

In most cases, this object only purpose is to hold the operation until it is evaluated. Therefore, you typically do not need to use this class explicitly.

Template Parameters:

  • Expr - Expression Type

  • NumDim - Indicate if it is a diagonal (NumDim == 1) or a diagonal matrix (NumDim == 2)

Public Functions

inline constexpr index_t size() const
Returns:

the expression size Complexity: Constant

inline constexpr index_t cols() const
Returns:

the number of columns of the expression Complexity: Constant

inline constexpr index_t rows() const
Returns:

the number of rows of the expression Complexity: Constant

inline const Expr &expr() const
Returns:

a reference to the mat Complexity: Constant

Vector Operations

namespace mocca

Functions

template<typename L, typename R>
inline auto dot(const MatrixBase<L> &lhs, const MatrixBase<R> &rhs)

Calculates the scalar (or dot) product between lhs and rhs vector expressions.

Parameters:
  • lhs[in] left side hand side

  • rhs[in] right side hand side

Returns:

the result of the scalar product

template<typename L, typename R>
inline auto outer(const MatrixBase<L> &lhs, const MatrixBase<R> &rhs)

Calculates the outer product between lhs and rhs vector expressions.

Parameters:
  • lhs[in] left side hand side

  • rhs[in] right side hand side

Returns:

the result of the outer product