Skip to content

Commit

Permalink
Merge pull request #3843 from micmn/feature/linalg-ops2
Browse files Browse the repository at this point in the history
Add linalg methods needed by FisherLDA and KernelPCA (CPU-only)
  • Loading branch information
vigsterkr committed Jun 26, 2017
2 parents 5a2d6e1 + 333cf71 commit 191bfa8
Show file tree
Hide file tree
Showing 8 changed files with 1,034 additions and 0 deletions.
125 changes: 125 additions & 0 deletions src/shogun/mathematics/linalg/LinalgBackendBase.h
Expand Up @@ -39,6 +39,7 @@
#include <shogun/lib/SGVector.h>
#include <shogun/lib/common.h>
#include <shogun/lib/config.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/GPUMemoryBase.h>
#include <shogun/mathematics/linalg/internal/Block.h>

Expand Down Expand Up @@ -121,6 +122,33 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_ADD_COL_VEC, SGMatrix)
#undef BACKEND_GENERIC_ADD_COL_VEC

/**
* Wrapper method of add scalar operation.
*
* @see linalg::add_scalar
*/
#define BACKEND_GENERIC_ADD_SCALAR(Type, Container) \
virtual void add_scalar(Container<Type>& a, Type b) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_ADD_SCALAR, SGVector)
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_ADD_SCALAR, SGMatrix)
#undef BACKEND_GENERIC_ADD_SCALAR

/**
* Wrapper method of center matrix operation.
*
* @see linalg::center_matrix
*/
#define BACKEND_GENERIC_CENTER_MATRIX(Type, Container) \
virtual void center_matrix(Container<Type>& A) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_CENTER_MATRIX, SGMatrix)
#undef BACKEND_GENERIC_CENTER_MATRIX

/**
* Wrapper method of Cholesky decomposition.
*
Expand Down Expand Up @@ -166,6 +194,38 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_DOT, SGVector)
#undef BACKEND_GENERIC_DOT

/**
* Wrapper method of eigenvalues and eigenvectors computation.
*
* @see linalg::eigen_solver
*/
#define BACKEND_GENERIC_EIGEN_SOLVER(Type, Container) \
virtual void eigen_solver( \
const Container<Type>& A, SGVector<Type>& eigenvalues, \
SGMatrix<Type>& eigenvectors) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_EIGEN_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_EIGEN_SOLVER

/**
* Wrapper method of eigenvalues and eigenvectors computation
* for symmetric matrices.
*
* @see linalg::eigen_solver_symmetric
*/
#define BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC(Type, Container) \
virtual void eigen_solver_symmetric( \
const Container<Type>& A, SGVector<Type>& eigenvalues, \
SGMatrix<Type>& eigenvectors) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_NON_INTEGER_PTYPE(
BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC, SGMatrix)
#undef BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC

/**
* Wrapper method of in-place matrix elementwise product.
*
Expand Down Expand Up @@ -301,6 +361,23 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_RANGE_FILL, SGMatrix)
#undef BACKEND_GENERIC_RANGE_FILL

/**
* Wrapper method that solves a system of linear equations
* using QR decomposition.
*
* @see linalg::qr_solver
*/
#define BACKEND_GENERIC_QR_SOLVER(Type, Container) \
virtual Container<Type> qr_solver( \
const SGMatrix<Type>& A, const Container<Type>& b) const \
{ \
SG_SNOTIMPLEMENTED; \
return 0; \
}
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_QR_SOLVER, SGVector)
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_QR_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_QR_SOLVER

/**
* Wrapper method of scale operation the operation result = alpha*A.
*
Expand Down Expand Up @@ -449,6 +526,21 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_BLOCK_ROWWISE_SUM, SGMatrix)
#undef BACKEND_GENERIC_BLOCK_ROWWISE_SUM

/**
* Wrapper method of svd computation.
*
* @see linalg::svd
*/
#define BACKEND_GENERIC_SVD(Type, Container) \
virtual void svd( \
const Container<Type>& A, SGVector<Type> s, SGMatrix<Type> U, \
bool thin_U) const \
{ \
SG_SNOTIMPLEMENTED; \
}
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_SVD, SGMatrix)
#undef BACKEND_GENERIC_SVD

/**
* Wrapper method of trace computation.
*
Expand All @@ -463,6 +555,39 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TRACE, SGMatrix)
#undef BACKEND_GENERIC_TRACE

/**
* Wrapper method of trace computation.
*
* @see linalg::transpose_matrix
*/
#define BACKEND_GENERIC_TRANSPOSE_MATRIX(Type, Container) \
virtual Container<Type> transpose_matrix(const Container<Type>& A) const \
{ \
SG_SNOTIMPLEMENTED; \
return 0; \
}
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TRANSPOSE_MATRIX, SGMatrix)
#undef BACKEND_GENERIC_TRANSPOSE_MATRIX

/**
* Wrapper method of triangular solver.
*
* @see linalg::triangular_solver
*/
#define BACKEND_GENERIC_TRIANGULAR_SOLVER(Type, Container) \
virtual Container<Type> triangular_solver( \
const SGMatrix<Type>& L, const Container<Type>& b, \
const bool lower = true) const \
{ \
SG_SNOTIMPLEMENTED; \
return 0; \
}
DEFINE_FOR_NON_INTEGER_PTYPE(
BACKEND_GENERIC_TRIANGULAR_SOLVER, SGVector)
DEFINE_FOR_NON_INTEGER_PTYPE(
BACKEND_GENERIC_TRIANGULAR_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_TRIANGULAR_SOLVER

/**
* Wrapper method of set vector or matrix to zero.
*
Expand Down
126 changes: 126 additions & 0 deletions src/shogun/mathematics/linalg/LinalgBackendEigen.h
Expand Up @@ -63,6 +63,19 @@ namespace shogun
DEFINE_FOR_NUMERIC_PTYPE(BACKEND_GENERIC_ADD_COL_VEC, SGMatrix)
#undef BACKEND_GENERIC_ADD_COL_VEC

/** Implementation of @see LinalgBackendBase::add_scalar */
#define BACKEND_GENERIC_ADD_SCALAR(Type, Container) \
virtual void add_scalar(Container<Type>& a, Type b) const;
DEFINE_FOR_NUMERIC_PTYPE(BACKEND_GENERIC_ADD_SCALAR, SGVector)
DEFINE_FOR_NUMERIC_PTYPE(BACKEND_GENERIC_ADD_SCALAR, SGMatrix)
#undef BACKEND_GENERIC_ADD_SCALAR

/** Implementation of @see LinalgBackendBase::center_matrix */
#define BACKEND_GENERIC_CENTER_MATRIX(Type, Container) \
virtual void center_matrix(Container<Type>& A) const;
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_CENTER_MATRIX, SGMatrix)
#undef BACKEND_GENERIC_CENTER_MATRIX

/** Implementation of @see LinalgBackendBase::cholesky_factor */
#define BACKEND_GENERIC_CHOLESKY_FACTOR(Type, Container) \
virtual Container<Type> cholesky_factor( \
Expand All @@ -84,6 +97,23 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_DOT, SGVector)
#undef BACKEND_GENERIC_DOT

/** Implementation of @see LinalgBackendBase::eigen_solver */
#define BACKEND_GENERIC_EIGEN_SOLVER(Type, Container) \
virtual void eigen_solver( \
const Container<Type>& A, SGVector<Type>& eigenvalues, \
SGMatrix<Type>& eigenvectors) const;
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_EIGEN_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_EIGEN_SOLVER

/** Implementation of @see LinalgBackendBase::eigen_solver_symmetric */
#define BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC(Type, Container) \
virtual void eigen_solver_symmetric( \
const Container<Type>& A, SGVector<Type>& eigenvalues, \
SGMatrix<Type>& eigenvectors) const;
DEFINE_FOR_NON_INTEGER_PTYPE(
BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC, SGMatrix)
#undef BACKEND_GENERIC_EIGEN_SOLVER_SYMMETRIC

/** Implementation of @see LinalgBackendBase::element_prod */
#define BACKEND_GENERIC_IN_PLACE_ELEMENT_PROD(Type, Container) \
virtual void element_prod( \
Expand Down Expand Up @@ -143,6 +173,14 @@ namespace shogun
BACKEND_GENERIC_COMPLEX_MEAN(SGMatrix)
#undef BACKEND_GENERIC_COMPLEX_MEAN

/** Implementation of @see LinalgBackendBase::qr_solver */
#define BACKEND_GENERIC_QR_SOLVER(Type, Container) \
virtual Container<Type> qr_solver( \
const SGMatrix<Type>& A, const Container<Type>& b) const;
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_QR_SOLVER, SGVector)
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_QR_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_QR_SOLVER

/** Implementation of @see LinalgBackendBase::range_fill */
#define BACKEND_GENERIC_RANGE_FILL(Type, Container) \
virtual void range_fill(Container<Type>& a, const Type start) const;
Expand Down Expand Up @@ -220,12 +258,37 @@ namespace shogun
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_BLOCK_ROWWISE_SUM, SGMatrix)
#undef BACKEND_GENERIC_BLOCK_ROWWISE_SUM

/** Implementation of @see LinalgBackendBase::svd */
#define BACKEND_GENERIC_SVD(Type, Container) \
virtual void svd( \
const Container<Type>& A, SGVector<Type> s, Container<Type> U, \
bool thin_U) const;
DEFINE_FOR_NON_INTEGER_PTYPE(BACKEND_GENERIC_SVD, SGMatrix)
#undef BACKEND_GENERIC_SVD

/** Implementation of @see LinalgBackendBase::trace */
#define BACKEND_GENERIC_TRACE(Type, Container) \
virtual Type trace(const Container<Type>& A) const;
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TRACE, SGMatrix)
#undef BACKEND_GENERIC_TRACE

/** Implementation of @see LinalgBackendBase::transpose_matrix */
#define BACKEND_GENERIC_TRANSPOSE_MATRIX(Type, Container) \
virtual Container<Type> transpose_matrix(const Container<Type>& A) const;
DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_TRANSPOSE_MATRIX, SGMatrix)
#undef BACKEND_GENERIC_TRANSPOSE_MATRIX

/** Implementation of @see LinalgBackendBase::triangular_solver */
#define BACKEND_GENERIC_TRIANGULAR_SOLVER(Type, Container) \
virtual Container<Type> triangular_solver( \
const SGMatrix<Type>& L, const Container<Type>& b, const bool lower) \
const;
DEFINE_FOR_NON_INTEGER_PTYPE(
BACKEND_GENERIC_TRIANGULAR_SOLVER, SGVector)
DEFINE_FOR_NON_INTEGER_PTYPE(
BACKEND_GENERIC_TRIANGULAR_SOLVER, SGMatrix)
#undef BACKEND_GENERIC_TRIANGULAR_SOLVER

/** Implementation of @see LinalgBackendBase::zero */
#define BACKEND_GENERIC_ZERO(Type, Container) \
virtual void zero(Container<Type>& a) const;
Expand Down Expand Up @@ -263,6 +326,18 @@ namespace shogun
const SGMatrix<T>& A, index_t i, const SGVector<T>& b,
SGVector<T>& result, T alpha, T beta) const;

/** Eigen3 vector add scalar method */
template <typename T>
void add_scalar_impl(SGVector<T>& a, T b) const;

/** Eigen3 matrix add scalar method */
template <typename T>
void add_scalar_impl(SGMatrix<T>& a, T b) const;

/** Eigen3 center matrix method */
template <typename T>
void center_matrix_impl(SGMatrix<T>& A) const;

/** Eigen3 Cholesky decomposition */
template <typename T>
SGMatrix<T>
Expand All @@ -277,6 +352,27 @@ namespace shogun
template <typename T>
T dot_impl(const SGVector<T>& a, const SGVector<T>& b) const;

/** Eigen3 eigenvalues and eigenvectors computation for real matrices.
*/
template <typename T>
void eigen_solver_impl(
const SGMatrix<T>& A, SGVector<T>& eigenvalues,
SGMatrix<T>& eigenvectors) const;

/** Eigen3 eigenvalues and eigenvectors computation for complex
* matrices. */
void eigen_solver_impl(
const SGMatrix<complex128_t>& A,
SGVector<complex128_t>& eigenvalues,
SGMatrix<complex128_t>& eigenvectors) const;

/** Eigen3 eigenvalues and eigenvectors computation of symmetric
* matrices */
template <typename T>
void eigen_solver_symmetric_impl(
const SGMatrix<T>& A, SGVector<T>& eigenvalues,
SGMatrix<T>& eigenvectors) const;

/** Eigen3 matrix in-place elementwise product method */
template <typename T>
void element_prod_impl(
Expand Down Expand Up @@ -326,6 +422,16 @@ namespace shogun
template <template <typename> class Container>
complex128_t mean_impl(const Container<complex128_t>& a) const;

/** Eigen3 vector QR solver. */
template <typename T>
SGVector<T>
qr_solver_impl(const SGMatrix<T>& A, const SGVector<T>& b) const;

/** Eigen3 matrix QR solver. */
template <typename T>
SGMatrix<T>
qr_solver_impl(const SGMatrix<T>& A, const SGMatrix<T> b) const;

/** Range fill a vector or matrix with start...start+len-1. */
template <typename T, template <typename> class Container>
void range_fill_impl(Container<T>& a, const T start) const;
Expand Down Expand Up @@ -385,10 +491,30 @@ namespace shogun
SGVector<T> rowwise_sum_impl(
const linalg::Block<SGMatrix<T>>& mat, bool no_diag) const;

/** Eigen3 compute svd method */
template <typename T>
void svd_impl(
const SGMatrix<T>& A, SGVector<T>& s, SGMatrix<T>& U,
bool thin_U) const;

/** Eigen3 compute trace method */
template <typename T>
T trace_impl(const SGMatrix<T>& A) const;

/** Eigen3 transpose matrix method */
template <typename T>
SGMatrix<T> transpose_matrix_impl(const SGMatrix<T>& A) const;

/** Eigen3 triangular solver method */
template <typename T>
SGMatrix<T> triangular_solver_impl(
const SGMatrix<T>& L, const SGMatrix<T>& b, const bool lower) const;

/** Eigen3 triangular solver method */
template <typename T>
SGVector<T> triangular_solver_impl(
const SGMatrix<T>& L, const SGVector<T>& b, const bool lower) const;

/** Eigen3 set vector to zero method */
template <typename T>
void zero_impl(SGVector<T>& a) const;
Expand Down

0 comments on commit 191bfa8

Please sign in to comment.