SHOGUN  6.0.0
shogun::linalg Namespace Reference

implementation

ocl

operations

special_purpose

util

## Classes

struct  Block
Generic class Block which wraps a matrix class and contains block specific information, providing a uniform way to deal with matrix blocks for all supported backend matrices. More...

struct  linalg_traits
General purpose linalg_traits for compile time information about backends set per module (see below). This uses the backend from the modules. To get the backend set globally, use linalg_traits<ModuleName>::backend. More...

## Enumerations

enum  Backend { EIGEN3, NATIVE, DEFAULT = 0 }
All currently supported linear algebra backend libraries, with a default backend, which will be used for all the tasks if any particular backend is not set explicitly via cmake options. A Native backend is also defined to accommodate Shogun's native implementation of some operations. More...

## Functions

template<class Matrix >
Block< Matrix > block (Matrix matrix, index_t row_begin, index_t col_begin, index_t row_size, index_t col_size)

template<class Operand , class UnaryOp >
auto elementwise_compute (Operand operand, UnaryOp unary_op) -> typename Operand::template container_type< decltype(unary_op(operand.data()[0]))>

template<class Operand , class UnaryOp >
void elementwise_compute_inplace (Operand operand, UnaryOp unary_op)

template<Backend backend, class Operand >
Operand::template
container_type< typename
operations::sin< typename
Operand::Scalar >::return_type >
elementwise_sin (Operand operand)

template<Backend backend, class Operand >
void elementwise_sin_inplace (Operand operand)

template<typename T , template< typename > class Container>
LinalgBackendBaseinfer_backend (const Container< T > &a)

template<typename T , template< typename > class Container>
LinalgBackendBaseinfer_backend (const Container< T > &a, const Container< T > &b)

template<typename T >
void to_gpu (SGVector< T > &a, SGVector< T > &b)

template<typename T >
void to_gpu (SGMatrix< T > &a, SGMatrix< T > &b)

template<typename T , template< typename > class Container>
void to_gpu (Container< T > &a)

template<typename T >
void from_gpu (SGVector< T > &a, SGVector< T > &b)

template<typename T >
void from_gpu (SGMatrix< T > &a, SGMatrix< T > &b)

template<typename T , template< typename > class Container>
void from_gpu (Container< T > &a)

template<typename T >
void add (SGVector< T > &a, SGVector< T > &b, SGVector< T > &result, T alpha=1, T beta=1)

template<typename T >
void add (SGMatrix< T > &a, SGMatrix< T > &b, SGMatrix< T > &result, T alpha=1, T beta=1)

template<typename T , template< typename > class Container>
Container< T > add (Container< T > &a, Container< T > &b, T alpha=1, T beta=1)

template<typename T >
SGMatrix< T > cholesky_factor (const SGMatrix< T > &A, const bool lower=true)

template<typename T >
SGVector< T > cholesky_solver (const SGMatrix< T > &L, const SGVector< T > &b, const bool lower=true)

template<typename T >
dot (const SGVector< T > &a, const SGVector< T > &b)

template<typename T >
void element_prod (Block< SGMatrix< T >> &a, Block< SGMatrix< T >> &b, SGMatrix< T > &result)

template<typename T >
SGMatrix< T > element_prod (Block< SGMatrix< T >> &a, Block< SGMatrix< T >> &b)

template<typename T >
void element_prod (SGMatrix< T > &a, SGMatrix< T > &b, SGMatrix< T > &result)

template<typename T >
SGMatrix< T > element_prod (SGMatrix< T > &a, SGMatrix< T > &b)

template<typename T >
void matrix_prod (SGMatrix< T > &A, SGVector< T > &b, SGVector< T > &result, bool transpose=false)

template<typename T >
SGVector< T > matrix_prod (SGMatrix< T > &A, SGVector< T > &b, bool transpose=false)

template<typename T >
void matrix_prod (SGMatrix< T > &A, SGMatrix< T > &B, SGMatrix< T > &result, bool transpose_A=false, bool transpose_B=false)

template<typename T >
SGMatrix< T > matrix_prod (SGMatrix< T > &A, SGMatrix< T > &B, bool transpose_A=false, bool transpose_B=false)

template<typename T , template< typename > class Container>
max (const Container< T > &a)

template<typename T , template< typename > class Container>
std::enable_if<!std::is_same
< T, complex128_t >::value,
float64_t >::type
mean (const Container< T > &a)

template<template< typename > class Container>
complex128_t mean (const Container< complex128_t > &a)

template<typename T , template< typename > class Container>
void range_fill (Container< T > &a, const T start=0)

template<typename T >
void scale (SGVector< T > &a, SGVector< T > &result, T alpha=1)

template<typename T >
void scale (SGMatrix< T > &A, SGMatrix< T > &result, T alpha=1)

template<typename T , template< typename > class Container>
Container< T > scale (Container< T > &a, T alpha=1)

template<typename T , template< typename > class Container>
void set_const (Container< T > &a, T value)

template<typename T , template< typename > class Container>
sum (const Container< T > &a, bool no_diag=false)

template<typename T >
sum (const Block< SGMatrix< T >> &a, bool no_diag=false)

template<typename T >
sum_symmetric (const SGMatrix< T > &a, bool no_diag=false)

template<typename T >
sum_symmetric (const Block< SGMatrix< T >> &a, bool no_diag=false)

template<typename T >
SGVector< T > colwise_sum (const SGMatrix< T > &mat, bool no_diag=false)

template<typename T >
SGVector< T > colwise_sum (const Block< SGMatrix< T >> &a, bool no_diag=false)

template<typename T >
SGVector< T > rowwise_sum (const SGMatrix< T > &mat, bool no_diag=false)

template<typename T >
SGVector< T > rowwise_sum (const Block< SGMatrix< T >> &a, bool no_diag=false)

template<typename T >
void logistic (SGMatrix< T > &a, SGMatrix< T > &result)

## Detailed Description

This namespace contains all linear algebra specific modules and operations for which we (may) rely on multiple implementation, either native or making use of some supported third party external linear algebra libraries.

## Enumeration Type Documentation

 enum Backend
strong

All currently supported linear algebra backend libraries, with a default backend, which will be used for all the tasks if any particular backend is not set explicitly via cmake options. A Native backend is also defined to accommodate Shogun's native implementation of some operations.

Developer's Note :

• Changing the default backend would just require to change it in the Backend enum
• Please refer to the developer's wiki (link) for the design and structure of internal linalg module The enum defines these backends in order of priority as default backend, as in, first defined one will be used as default.

Note - Currently EIGEN3 is the default (if it is available).

Enumerator
EIGEN3
NATIVE
DEFAULT

Definition at line 74 of file linalg.h.

## Function Documentation

 void shogun::linalg::add ( SGVector< T > & a, SGVector< T > & b, SGVector< T > & result, T alpha = 1, T beta = 1 )

Performs the operation result = alpha * a + beta * b on vectors. This version returns the result in-place. User should pass an appropriately pre-allocated memory matrix Or pass one of the operands arguments (A or B) as a result

Parameters
 a First vector b Second vector result The vector that saves the result alpha Constant to be multiplied by the first vector beta Constant to be multiplied by the second vector

Definition at line 284 of file LinalgNamespace.h.

 void shogun::linalg::add ( SGMatrix< T > & a, SGMatrix< T > & b, SGMatrix< T > & result, T alpha = 1, T beta = 1 )

Performs the operation result = alpha * a + beta * b on matrices. This version returns the result in-place. User should pass an appropriately pre-allocated memory matrix Or pass one of the operands arguments (A or B) as a result

Parameters
 a First matrix b Second matrix result The matrix that saves the result alpha Constant to be multiplied by the first matrix beta Constant to be multiplied by the second matrix

Definition at line 315 of file LinalgNamespace.h.

 Container shogun::linalg::add ( Container< T > & a, Container< T > & b, T alpha = 1, T beta = 1 )

Performs the operation C = alpha * A + beta * B. This version returns the result in a newly created vector or matrix.

Parameters
 A First vector or matrix B Second vector or matrix alpha Constant to be multiplied by the first vector or matrix beta Constant to be multiplied by the second vector or matrix
Returns
The result vector or matrix

Definition at line 345 of file LinalgNamespace.h.

 Block shogun::linalg::block ( Matrix matrix, index_t row_begin, index_t col_begin, index_t row_size, index_t col_size )

Method that returns a block object. Suited for Eigen3/SGMatrix

Parameters
 matrix the matrix on which the block is defined row_begin the row index at which the block starts col_begin the col index at which the block starts row_size the number of rows in the block col_size the number of cols in the block
Returns
a block object on this matrix

Definition at line 105 of file mathematics/linalg/internal/Block.h.

 SGMatrix shogun::linalg::cholesky_factor ( const SGMatrix< T > & A, const bool lower = true )

Compute the cholesky decomposition $$A = L L^{*}$$ or $$A = U^{*} U$$ of a Hermitian positive definite matrix

Parameters
 A The matrix whose cholesky decomposition is to be computed lower Whether to compute the upper or lower triangular Cholesky factorization (default: lower)
Returns
The upper or lower triangular Cholesky factorization

Definition at line 362 of file LinalgNamespace.h.

 SGVector shogun::linalg::cholesky_solver ( const SGMatrix< T > & L, const SGVector< T > & b, const bool lower = true )

Solve the linear equations $$Ax=b$$, given the Cholesky factorization of A, where $$A$$ is a Hermitian positive definite matrix

Parameters
 L Triangular matrix, Cholesky factorization of A b Right-hand side array lower Whether to use L as the upper or lower triangular Cholesky factorization (default:lower)
Returns
$$\x$$

Definition at line 378 of file LinalgNamespace.h.

 SGVector shogun::linalg::colwise_sum ( const SGMatrix< T > & mat, bool no_diag = false )

Method that computes colwise sum of co-efficients of a dense matrix

Parameters
 Mat a matrix whose colwise sum has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
The colwise sum of co-efficients computed as $$s_j=\sum_{i}b_{i,j}$$

Definition at line 873 of file LinalgNamespace.h.

 SGVector shogun::linalg::colwise_sum ( const Block< SGMatrix< T >> & a, bool no_diag = false )

Method that computes the colwise sum of matrix blocks This operation works with CPU backends only.

Parameters
 a the matrix-block whose colwise sum of co-efficients has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
the colwise sum of co-efficients computed as $$s_j=\sum_{i}b_{i,j}$$

Definition at line 887 of file LinalgNamespace.h.

 T shogun::linalg::dot ( const SGVector< T > & a, const SGVector< T > & b )

Vector dot-product that works with generic vectors.

Parameters
 a First vector b Second vector
Returns
The dot product of $$\mathbf{a}$$ and $$\mathbf{b}$$, represented as $$\sum_i a_i b_i$$

Definition at line 393 of file LinalgNamespace.h.

 void shogun::linalg::element_prod ( Block< SGMatrix< T >> & a, Block< SGMatrix< T >> & b, SGMatrix< T > & result )

Performs the operation C = A .* B where ".*" denotes elementwise multiplication on matrix blocks.

This version returns the result in-place. User should pass an appropriately pre-allocated memory matrix.

This operation works with CPU backends only.

Parameters
 a First matrix block b Second matrix block c Result matrix

Definition at line 413 of file LinalgNamespace.h.

 SGMatrix shogun::linalg::element_prod ( Block< SGMatrix< T >> & a, Block< SGMatrix< T >> & b )

Performs the operation C = A .* B where ".*" denotes elementwise multiplication on matrix blocks.

This version returns the result in a newly created matrix.

Parameters
 A First matrix block B Second matrix block
Returns
The result of the operation

Definition at line 438 of file LinalgNamespace.h.

 void shogun::linalg::element_prod ( SGMatrix< T > & a, SGMatrix< T > & b, SGMatrix< T > & result )

Performs the operation C = A .* B where ".*" denotes elementwise multiplication.

This version returns the result in-place. User should pass an appropriately pre-allocated memory matrix Or pass one of the operands arguments (A or B) as a result

Parameters
 a First matrix b Second matrix result Result matrix

Definition at line 463 of file LinalgNamespace.h.

 SGMatrix shogun::linalg::element_prod ( SGMatrix< T > & a, SGMatrix< T > & b )

Performs the operation C = A .* B where ".*" denotes elementwise multiplication.

This version returns the result in a newly created matrix.

Parameters
 A First matrix B Second matrix
Returns
The result of the operation

Definition at line 491 of file LinalgNamespace.h.

 auto shogun::linalg::elementwise_compute ( Operand operand, UnaryOp unary_op ) -> typename Operand::template container_type

Template method for computing custom unary operations element-wise for matrices and vectors using NATIVE backend. Works for SGMatrix/SGVector.

This method returns the result in a newly allocated matrix/vector.

Parameters
 operand The operand on which the element-wise operation has to be performed unary_op The custom unary operator
Returns
The result of the unary operator applied element-wise on the operand

Definition at line 56 of file ElementwiseOperations.h.

 void shogun::linalg::elementwise_compute_inplace ( Operand operand, UnaryOp unary_op )

Template method for computing custom unary operations element-wise for matrices and vectors using NATIVE backend. Works for SGMatrix/SGVector.

This method computes the result in-place.

Parameters
 operand The operand on which the element-wise operation has to be performed unary_op The custom unary operator

Definition at line 80 of file ElementwiseOperations.h.

 Operand::template container_type::return_type> shogun::linalg::elementwise_sin ( Operand operand )

Template method for computing element-wise sin for matrices and vectors.

This method returns the result in a newly allocated matrix/vector.

Parameters
 operand The operand on which the element-wise operation has to be performed
Returns
The result of the unary operator applied element-wise on the operand

Definition at line 141 of file ElementwiseOperations.h.

 void shogun::linalg::elementwise_sin_inplace ( Operand operand )

Template method for computing element-wise sin for matrices and vectors.

This method computes the result in-place.

Parameters
 operand The operand on which the element-wise operation has to be performed
Returns
The result of the unary operator applied element-wise on the operand

Definition at line 165 of file ElementwiseOperations.h.

 void shogun::linalg::from_gpu ( SGVector< T > & a, SGVector< T > & b )

Fetches data from GPU memory. Transfer vectors to CPU if GPU backend is still available.

Parameters
 a SGVector to be transferred b SGVector to be set

Definition at line 193 of file LinalgNamespace.h.

 void shogun::linalg::from_gpu ( SGMatrix< T > & a, SGMatrix< T > & b )

Fetches data from GPU memory. Transfer matrices to CPU if GPU backend is still available.

Parameters
 a SGMatrix to be transferred b SGMatrix to be set

Definition at line 230 of file LinalgNamespace.h.

 void shogun::linalg::from_gpu ( Container< T > & a )

Fetches data from GPU memory. Transfer vector or matrix to CPU if GPU backend is still available.

Parameters
 a SGVector or SGMatrix to be transferred

Definition at line 266 of file LinalgNamespace.h.

 LinalgBackendBase* shogun::linalg::infer_backend ( const Container< T > & a )

Infer the appropriate backend for linalg operations from the input SGVector or SGMatrix (Container).

Parameters
 a SGVector or SGMatrix
Returns
LinalgBackendBase pointer

Definition at line 52 of file LinalgNamespace.h.

 LinalgBackendBase* shogun::linalg::infer_backend ( const Container< T > & a, const Container< T > & b )

Infer the appropriate backend for linalg operations from the input SGVector or SGMatrix (Container). Raise error if the backends of the two Containers conflict.

Parameters
 a The first SGVector/SGMatrix b The second SGVector/SGMatrix
Returns
LinalgBackendBase pointer

Definition at line 79 of file LinalgNamespace.h.

 void shogun::linalg::logistic ( SGMatrix< T > & a, SGMatrix< T > & result )

Applies the elementwise logistic function f(x) = 1/(1+exp(-x)) to a matrix This method returns the result in-place.

Parameters
 a The input matrix result The output matrix

Definition at line 51 of file LinalgSpecialPurposes.h.

 void shogun::linalg::matrix_prod ( SGMatrix< T > & A, SGVector< T > & b, SGVector< T > & result, bool transpose = false )

Performs the operation of a matrix multiplies a vector $$x = Ab$$.

This version returns the result in-place. User should pass an appropriately allocated memory matrix.

Parameters
 A The matrix b The vector transpose Whether to transpose the matrix. Default false result Result vector

Definition at line 516 of file LinalgNamespace.h.

 SGVector shogun::linalg::matrix_prod ( SGMatrix< T > & A, SGVector< T > & b, bool transpose = false )

Performs the operation of matrix multiply a vector $$x = Ab$$. This version returns the result in a newly created vector.

Parameters
 A The matrix b The vector transpose Whether to transpose a matrix. Default:false
Returns
result Result vector

Definition at line 552 of file LinalgNamespace.h.

 void shogun::linalg::matrix_prod ( SGMatrix< T > & A, SGMatrix< T > & B, SGMatrix< T > & result, bool transpose_A = false, bool transpose_B = false )

Performs the operation C = A * B where "*" denotes matrix multiplication.

This version returns the result in-place. User should pass an appropriately allocated memory matrix

Parameters
 A First matrix B Second matrix result Result matrix transpose_A whether to transpose matrix A transpose_B whether to transpose matrix B

Definition at line 587 of file LinalgNamespace.h.

 SGMatrix shogun::linalg::matrix_prod ( SGMatrix< T > & A, SGMatrix< T > & B, bool transpose_A = false, bool transpose_B = false )

Performs the operation C = A * B where "*" denotes matrix multiplication.

This version returns the result in a newly created matrix.

Parameters
 A First matrix B Second matrix transpose_A whether to transpose matrix A transpose_B whether to transpose matrix B
Returns
The result of the operation

Definition at line 655 of file LinalgNamespace.h.

 T shogun::linalg::max ( const Container< T > & a )

Returns the largest element in a vector or matrix

Parameters
 a Input vector or matrix
Returns
The largest value in the vector or matrix

Definition at line 700 of file LinalgNamespace.h.

 std::enable_if::value, float64_t>::type shogun::linalg::mean ( const Container< T > & a )

Method that computes the mean of vectors or matrices composed of real numbers.

Parameters
 a SGVector or SGMatrix
Returns
The vector mean $$\bar{a}_i$$ or matrix mean $$\bar{m}_{i,j}$$

Definition at line 713 of file LinalgNamespace.h.

 complex128_t shogun::linalg::mean ( const Container< complex128_t > & a )

Method that computes the mean of vectors or matrices composed of complex numbers.

Parameters
 a SGVector or SGMatrix
Returns
The vector mean $$\bar{a}_i$$ or matrix mean $$\bar{m}_{i,j}$$

Definition at line 726 of file LinalgNamespace.h.

 void shogun::linalg::range_fill ( Container< T > & a, const T start = 0 )

Range fill a vector or matrix with start...start+len-1

Parameters
 a The vector or matrix to be filled start Value to be assigned to the first element of vector or matrix

Definition at line 739 of file LinalgNamespace.h.

 SGVector shogun::linalg::rowwise_sum ( const SGMatrix< T > & mat, bool no_diag = false )

Method that computes rowwise sum of co-efficients of a dense matrix

Parameters
 mat a matrix whose rowwise sum has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
the rowwise sum of co-efficients computed as $$s_i=\sum_{j}m_{i,j}$$

Definition at line 900 of file LinalgNamespace.h.

 SGVector shogun::linalg::rowwise_sum ( const Block< SGMatrix< T >> & a, bool no_diag = false )

Method that computes the rowwise sum of matrix blocks This operation works with CPU backends only.

Parameters
 a the matrix-block whose rowwise sum of co-efficients has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
the rowwise sum of co-efficients computed as $$s_i=\sum_{j}m_{i,j}$$

Definition at line 914 of file LinalgNamespace.h.

 void shogun::linalg::scale ( SGVector< T > & a, SGVector< T > & result, T alpha = 1 )

Performs the operation result = alpha * a on vectors This version returns the result in-place. User should pass an appropriately pre-allocated memory matrix Or pass the operands argument a as a result

Parameters
 a First vector alpha Scale factor result The vector of alpha * a

Definition at line 755 of file LinalgNamespace.h.

 void shogun::linalg::scale ( SGMatrix< T > & A, SGMatrix< T > & result, T alpha = 1 )

Performs the operation result = alpha * A on matrices This version returns the result in-place. User should pass an appropriately pre-allocated memory matrix Or pass the operands argument A as a result

Parameters
 A First matrix alpha Scale factor result The matrix of alpha * A

Definition at line 772 of file LinalgNamespace.h.

 Container shogun::linalg::scale ( Container< T > & a, T alpha = 1 )

Performs the operation B = alpha * A on vectors or matrices This version returns the result in a newly created vector or matrix.

Parameters
 a First vector/matrix alpha Scale factor
Returns
Vector or matrix of alpha * A

Definition at line 790 of file LinalgNamespace.h.

 void shogun::linalg::set_const ( Container< T > & a, T value )

Set const value to vectors or matrices

Parameters
 a Vector or matrix to be set value The value to set the vector or matrix

Definition at line 804 of file LinalgNamespace.h.

 T shogun::linalg::sum ( const Container< T > & a, bool no_diag = false )

Method that computes the sum of vectors or matrices

Parameters
 a The vector or matrix whose sum has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
The vector sum $$\sum_i a_i$$ or matrix sum $$\sum_{i,j}b_{i,j}$$

Definition at line 817 of file LinalgNamespace.h.

 T shogun::linalg::sum ( const Block< SGMatrix< T >> & a, bool no_diag = false )

Method that computes the sum of matrix blocks This operation works with CPU backends only.

Parameters
 a The matrix-block whose sum of co-efficients has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
Matrix-block sum $$\sum_{i,j}b_{i,j}$$

Definition at line 831 of file LinalgNamespace.h.

 T shogun::linalg::sum_symmetric ( const SGMatrix< T > & a, bool no_diag = false )

Method that computes the sum of symmetric matrices

Parameters
 a The symmetric matrix whose sum has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
The matrix sum $$\sum_{i,j}b_{i,j}$$

Definition at line 844 of file LinalgNamespace.h.

 T shogun::linalg::sum_symmetric ( const Block< SGMatrix< T >> & a, bool no_diag = false )

Method that computes the sum of symmetric matrix blocks This operation works with CPU backends only.

Parameters
 a The symmetric matrix-block whose sum has to be computed no_diag If true, diagonal entries are excluded from the sum. Default: false
Returns
Symmetric matrix-block sum $$\sum_{i,j}b_{i,j}$$

Definition at line 859 of file LinalgNamespace.h.

 void shogun::linalg::to_gpu ( SGVector< T > & a, SGVector< T > & b )

Transfers data to GPU memory. Shallow-copy of SGVector with vector on CPU if GPU backend not available

Parameters
 a SGVector to be transferred b SGVector to be set

Definition at line 112 of file LinalgNamespace.h.

 void shogun::linalg::to_gpu ( SGMatrix< T > & a, SGMatrix< T > & b )

Transfers data to GPU memory. Does nothing if no GPU backend registered. Shallow-copy SGMatrix on CPU if GPU backend not available

Parameters
 a SGMatrix to be transferred b SGMatrix to be set

Definition at line 147 of file LinalgNamespace.h.

 void shogun::linalg::to_gpu ( Container< T > & a )

Transfers data to GPU memory in-place.

Parameters
 a SGVector or SGMatrix to be transferred

Definition at line 180 of file LinalgNamespace.h.

SHOGUN Machine Learning Toolbox - Documentation