Reduction and Norm#

Reduction#

template<typename E>
inline PartialRedux<E, internal::redux_sum_op, internal::kRowwise> sum(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline PartialRedux<E, internal::redux_sum_op, internal::kColwise> sum(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline E::value_type sum(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline E::value_type sum(const MatrixBase<E> &expr)#

Calculates the sum of the elements in a matrix, vector or expression over a given axis. If axis == none, this applies the operation over the entire expression.

Example:

using namespace mocca;

Matrix<int> mat = {{1, 2, 3},
                {2, 3, 4},
                {5, 5, 5}};

std::cout << "The sum of the elements in each col is: \n";
std::cout << sum(mat, colwise) << "\n";

std::cout << "The sum of all elements is: ";
std::cout << sum(mat) << "\n";

Output:

The sum of the elements in each col is
[ 8 10 12 ]
The sum of all elements is: 30

Complexity: Linear in expr.size()

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

  • axis[in] either no_axis (default), rowwise or colwise (optional)

Returns:

the resulting scalar if no_axis. A expression containing the partial reduction over the specified axis, otherwise.

template<typename E>
inline PartialRedux<E, internal::redux_prod_op, internal::kRowwise> prod(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline PartialRedux<E, internal::redux_prod_op, internal::kColwise> prod(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline E::value_type prod(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline E::value_type prod(const MatrixBase<E> &expr)#

Calculates the product of the elements in a matrix, vector or expression over a given axis. If axis == none, this applies the operation over the entire expression.

Example:

using namespace mocca;

Matrix<int> mat = {{1, 2, 3},
                {2, 3, 4},
                {5, 5, 5}};

std::cout << "The product of the elements in each col is: \n";
std::cout << prod(mat, colwise) << "\n";

std::cout << "The product of all elements is: ";
std::cout << prod(mat) << "\n";

Output:

The product of the elements in each col is
[ 10 30 60 ]
The product of all elements is: 18000

Complexity: Linear in expr.size()

Warning

This routine can easily result in a underflow or overflow if not careful.

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

  • axis[in] either no_axis (default), rowwise or colwise (optional)

Returns:

the resulting scalar if no_axis. A expression containing the partial reduction over the specified axis, otherwise.

template<typename E>
inline PartialRedux<E, internal::redux_max_op, internal::kRowwise> max(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline PartialRedux<E, internal::redux_max_op, internal::kColwise> max(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline E::value_type max(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline E::value_type max(const MatrixBase<E> &expr)#

Finds the maximum value in a matrix, vector or expression over a given axis. If axis == none, this applies the operation over the entire expression.

Example:

using namespace mocca;

Matrix<int> mat = {{1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}};

std::cout << "The maximum value in each row is: \n";
std::cout << max(mat, rowwise) << "\n";

std::cout << "The maximum of the matrix is: ";
std::cout << max(mat) << "\n";

Output:

The maximum value in each row is:
[3
 6
 9]
The maximum of the matrix is: 9

Complexity: Linear in expr.size()

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

  • axis[in] either no_axis (default), rowwise or colwise (optional)

Returns:

the resulting scalar if no_axis. A expression containing the partial reduction over the specified axis, otherwise.

template<typename E>
inline PartialRedux<E, internal::redux_min_op, internal::kRowwise> min(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline PartialRedux<E, internal::redux_min_op, internal::kColwise> min(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline E::value_type min(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline E::value_type min(const MatrixBase<E> &expr)#

Finds the minimum value in a matrix, vector or expression over a given axis. If axis == none, this applies the operation over the entire expression.

Example:

using namespace mocca;

Matrix<int> mat = {{1, 2, 3},
                   {4, 5, 6},
                   {7, 8, 9}};

std::cout << "The minimum value in each row is: \n";
std::cout << min(mat, rowwise) << "\n";

std::cout << "The minimum of the matrix is: ";
std::cout << min(mat) << "\n";

Output:

The minimum value in each row is:
[1
 4
 7]
The minimum of the matrix is: 1

Complexity: Linear in expr.size()

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

  • axis[in] either no_axis (default), rowwise or colwise (optional)

Returns:

the resulting scalar if no_axis. A expression containing the partial reduction over the specified axis, otherwise.

Norm#

template<typename E>
inline PartialRedux<E, internal::norm_l1_op, internal::kRowwise> norm_l1(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline PartialRedux<E, internal::norm_l1_op, internal::kColwise> norm_l1(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline E::value_type norm_l1(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline E::value_type norm_l1(const MatrixBase<E> &expr)#

Calculates the L1 norm of an expression (i.e., \( \|x\|_1 = \|x_1\| + \|x_2\| + ... + \|x_n\|\) with n equal to the size of the expression). If axis == none, this applies the operation over the entire expression.

Example:

using namespace mocca;

Matrix<int> mat = {{1, -2, 3},
                {2, 3, -4},
                {-5, 5, -5}};

std::cout << "The l1 norm of the elements in each col is: \n";
std::cout << norm_l1(mat, colwise) << "\n";

std::cout << "The l1 norm of all elements is: ";
std::cout << norm_l1(mat) << "\n";

Output:

The l1 norm of the elements in each col is
[ 8 10 12 ]
The l1 norm of all elements is: 30

Complexity: Linear in expr.size()

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

  • axis[in] either no_axis (default), rowwise or colwise (optional)

Returns:

the resulting scalar if no_axis. A expression containing the partial reduction over the specified axis, otherwise.

template<typename E>
inline auto norm_l2(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline auto norm_l2(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline double norm_l2(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline double norm_l2(const MatrixBase<E> &expr)#

Calculates the L2 norm of an expression (i.e., \( \|x\|_2 = \sqrt{\|x_1\|^2 + \|x_2\|^2 + ... + \|x_n\|^2}\) with n equal to the size of the expression). If axis == none, this applies the operation over the entire expression. The L2 norm of matrices is also called “Frobenius norm”.

Example:

using namespace mocca;

Matrix<int> mat = {{1, -2, 3},
                  {2, 3, -4},
                  {-5, 5, -5}};

std::cout << "The l2 norm of the elements in each row is: \n";
std::cout << norm_l2(mat, rowwise) << "\n";

std::cout << "The l2 norm of all elements is: ";
std::cout << norm_l2(mat) << "\n";

Output:

The l2 norm of the elements in each col is
[ 3.4641
  5.3852
  8.6603 ]
The l2 norm of all elements is: 10.8628

Complexity: Linear in expr.size()

Parameters:
  • expr[in] vector or matrix expression

  • axis[in] operation axis (optional, matrix expression only)

template<typename E>
inline PartialRedux<E, internal::norm_max_op, internal::kRowwise> norm_max(const MatrixBase<E> &expr, internal::Rowwise)#
template<typename E>
inline PartialRedux<E, internal::norm_max_op, internal::kColwise> norm_max(const MatrixBase<E> &expr, internal::Colwise)#
template<typename E>
inline E::value_type norm_max(const MatrixBase<E> &expr, internal::NoAxis)#
template<typename E>
inline E::value_type norm_max(const MatrixBase<E> &expr)#

Calculates the max norm of an expression (i.e., \( \|x\|_\inf = max(\|x_1\|, \|x_2\|, ..., \|x_n\|)\) with n equal to the size of the expression). If axis == none, this applies the operation over the entire expression.

Example:

using namespace mocca;

Matrix<int> mat = {{1, -2, 3},
                  {2, 3, -8},
                  {-5, 5, -5}};

std::cout << "The l-infinity norm of the elements in each col is: \n";
std::cout << norm_max(mat, colwise) << "\n";

std::cout << "The l-infinity norm of all elements is: ";
std::cout << norm_max(mat) << "\n";

Output:

The l-infinity norm of the elements in each col is
[ 5 5 8 ]
The l-infinity norm of all elements is: 8

Complexity: Linear in expr.size()

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

  • axis[in] either no_axis (default), rowwise or colwise (optional)

Returns:

the resulting scalar if no_axis. A expression containing the partial reduction over the specified axis, otherwise.

Partial Reduction#

template<typename Expr, typename ReduxOp, internal::Axis ReduxAxis>
class PartialRedux : public mocca::MatrixBase<PartialRedux<Expr, ReduxOp, ReduxAxis>>#

An abstract object representing the partial reduction (i.e., the reduction of the elements in a single row or column) of a given matrix.

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 - A matrix or expression

  • ReduxOp - Functor with the reduction operation

  • ReduxAxis - Axis of the partial reduction operation (either kRowwise or kColwise )

Public Functions

inline constexpr index_t rows() const noexcept#

Complexity: Constant

Returns:

the number of rows in the PartialRedux

inline constexpr index_t cols() const noexcept#

Complexity: Constant

Returns:

the number of columns in the PartialRedux

inline constexpr index_t size() const noexcept#

Complexity: Constant

Returns:

the number of entries in the PartialRedux

inline const Expr &expr() const#

Complexity: Constant

Returns:

a reference to the underlying expression

inline const ReduxOp &op() const#

Complexity: Constant

Returns:

a reference to the redux operation