Skip to content

Commit

Permalink
Add const qualifiers to input of linalg APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 authored and karlnapf committed Feb 9, 2018
1 parent cfb220c commit a7a79f1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
11 changes: 6 additions & 5 deletions src/shogun/mathematics/linalg/LinalgBackendBase.h
Expand Up @@ -375,7 +375,8 @@ namespace shogun
* @see linalg::logistic
*/
#define BACKEND_GENERIC_LOGISTIC(Type, Container) \
virtual void logistic(Container<Type>& a, Container<Type>& result) const \
virtual void logistic(const Container<Type>& a, Container<Type>& result) \
const \
{ \
SG_SNOTIMPLEMENTED; \
}
Expand Down Expand Up @@ -454,7 +455,7 @@ namespace shogun
*/
#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
virtual void multiply_by_logistic_derivative( \
Container<Type>& a, Container<Type>& result) const \
const Container<Type>& a, Container<Type>& result) const \
{ \
SG_SNOTIMPLEMENTED; \
}
Expand All @@ -469,7 +470,7 @@ namespace shogun
*/
#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
virtual void multiply_by_rectified_linear_derivative( \
Container<Type>& a, Container<Type>& result) const \
const Container<Type>& a, Container<Type>& result) const \
{ \
SG_SNOTIMPLEMENTED; \
}
Expand Down Expand Up @@ -497,8 +498,8 @@ namespace shogun
* @see linalg::rectified_linear
*/
#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
virtual void rectified_linear(Container<Type>& a, Container<Type>& result) \
const \
virtual void rectified_linear( \
const Container<Type>& a, Container<Type>& result) const \
{ \
SG_SNOTIMPLEMENTED; \
}
Expand Down
20 changes: 11 additions & 9 deletions src/shogun/mathematics/linalg/LinalgBackendEigen.h
Expand Up @@ -192,7 +192,8 @@ namespace shogun

/** Implementation of @see LinalgBackendBase::logistic */
#define BACKEND_GENERIC_LOGISTIC(Type, Container) \
virtual void logistic(Container<Type>& a, Container<Type>& result) const;
virtual void logistic(const Container<Type>& a, Container<Type>& result) \
const;
DEFINE_FOR_NUMERIC_PTYPE(BACKEND_GENERIC_LOGISTIC, SGMatrix)
#undef BACKEND_GENERIC_LOGISTIC

Expand Down Expand Up @@ -229,15 +230,15 @@ namespace shogun
/** Implementation of @see linalg::multiply_by_logistic_derivative */
#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
virtual void multiply_by_logistic_derivative( \
Container<Type>& a, Container<Type>& result) const;
const Container<Type>& a, Container<Type>& result) const;
DEFINE_FOR_NUMERIC_PTYPE(
BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV, SGMatrix)
#undef BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV

/** Implementation of @see linalg::multiply_by_rectified_linear_derivative */
#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
virtual void multiply_by_rectified_linear_derivative( \
Container<Type>& a, Container<Type>& result) const;
const Container<Type>& a, Container<Type>& result) const;
DEFINE_FOR_NON_INTEGER_REAL_PTYPE(
BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV, SGMatrix)
#undef BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV
Expand All @@ -259,8 +260,8 @@ namespace shogun

/** Implementation of @see linalg::rectified_linear */
#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
virtual void rectified_linear(Container<Type>& a, Container<Type>& result) \
const;
virtual void rectified_linear( \
const Container<Type>& a, Container<Type>& result) const;
DEFINE_FOR_REAL_PTYPE(BACKEND_GENERIC_RECTIFIED_LINEAR, SGMatrix)
#undef BACKEND_GENERIC_RECTIFIED_LINEAR

Expand Down Expand Up @@ -526,7 +527,7 @@ namespace shogun

/** Eigen3 logistic method. Calculates f(x) = 1/(1+exp(-x)) */
template <typename T>
void logistic_impl(SGMatrix<T>& a, SGMatrix<T>& result) const;
void logistic_impl(const SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 matrix * vector in-place product method */
template <typename T>
Expand Down Expand Up @@ -565,14 +566,14 @@ namespace shogun
*/
template <typename T>
void multiply_by_logistic_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const;
const SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 multiply_by_rectified_linear_derivative method
* Performs the operation C(i,j) = C(i,j) * (A(i,j)!=0) for all i and j
*/
template <typename T>
void multiply_by_rectified_linear_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const;
const SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 vector QR solver. */
template <typename T>
Expand All @@ -590,7 +591,8 @@ namespace shogun

/** Applies the elementwise rectified linear function f(x) = max(0,x) */
template <typename T>
void rectified_linear_impl(SGMatrix<T>& a, SGMatrix<T>& result) const;
void
rectified_linear_impl(const SGMatrix<T>& a, SGMatrix<T>& result) const;

/** Eigen3 vector inplace scale method: result = alpha * A */
template <typename T>
Expand Down
31 changes: 17 additions & 14 deletions src/shogun/mathematics/linalg/LinalgBackendViennaCL.h
Expand Up @@ -118,7 +118,7 @@ namespace shogun

/** Implementation of @see LinalgBackendBase::logistic */
#define BACKEND_GENERIC_LOGISTIC(Type, Container) \
virtual void logistic(Container<Type>& a, Container<Type>& result) const \
virtual void logistic(const Container<Type>& a, Container<Type>& result) const \
{ \
logistic_impl(a, result); \
}
Expand All @@ -127,7 +127,7 @@ namespace shogun

/** Implementation of @see LinalgBackendBase::matrix_prod */
#define BACKEND_GENERIC_IN_PLACE_MATRIX_PROD(Type, Container) \
virtual void matrix_prod(SGMatrix<Type>& a, Container<Type>& b,\
virtual void matrix_prod(const SGMatrix<Type>& a, const Container<Type>& b,\
Container<Type>& result, bool transpose_A, bool transpose_B) const \
{ \
matrix_prod_impl(a, b, result, transpose_A, transpose_B); \
Expand Down Expand Up @@ -158,7 +158,7 @@ namespace shogun

/** Implementation of @see linalg::multiply_by_logistic_derivative */
#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
virtual void multiply_by_logistic_derivative(Container<Type>& a,\
virtual void multiply_by_logistic_derivative(const Container<Type>& a,\
Container<Type>& result) const \
{ \
multiply_by_logistic_derivative_impl(a, result); \
Expand All @@ -168,7 +168,7 @@ namespace shogun

/** Implementation of @see linalg::multiply_by_rectified_linear_derivative */
#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
virtual void multiply_by_rectified_linear_derivative(Container<Type>& a,\
virtual void multiply_by_rectified_linear_derivative(const Container<Type>& a,\
Container<Type>& result) const \
{ \
multiply_by_rectified_linear_derivative_impl(a, result); \
Expand All @@ -178,7 +178,7 @@ namespace shogun

/** Implementation of @see linalg::rectified_linear */
#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
virtual void rectified_linear(Container<Type>& a, Container<Type>& result) const \
virtual void rectified_linear(const Container<Type>& a, Container<Type>& result) const \
{ \
rectified_linear_impl(a, result); \
}
Expand Down Expand Up @@ -378,7 +378,7 @@ namespace shogun

/** ViennaCL logistic method. Calculates f(x) = 1/(1+exp(-x)) */
template <typename T>
void logistic_impl(SGMatrix<T>& a, SGMatrix<T>& result) const
void logistic_impl(const SGMatrix<T>& a, SGMatrix<T>& result) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);
Expand Down Expand Up @@ -409,8 +409,8 @@ namespace shogun
/** ViennaCL matrix * vector in-place product method */
template <typename T>
void matrix_prod_impl(
SGMatrix<T>& a, SGVector<T>& b, SGVector<T>& result, bool transpose,
bool transpose_B = false) const
const SGMatrix<T>& a, const SGVector<T>& b, SGVector<T>& result,
bool transpose, bool transpose_B = false) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* b_gpu = cast_to_viennacl(b);
Expand All @@ -429,7 +429,7 @@ namespace shogun
/** ViennaCL matrices in-place product method */
template <typename T>
void matrix_prod_impl(
SGMatrix<T>& a, SGMatrix<T>& b, SGMatrix<T>& result,
const SGMatrix<T>& a, const SGMatrix<T>& b, SGMatrix<T>& result,
bool transpose_A, bool transpose_B) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
Expand Down Expand Up @@ -503,7 +503,7 @@ namespace shogun
*/
template <typename T>
void multiply_by_logistic_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
const SGMatrix<T>& a, SGMatrix<T>& result) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);
Expand Down Expand Up @@ -539,7 +539,7 @@ namespace shogun
*/
template <typename T>
void multiply_by_rectified_linear_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
const SGMatrix<T>& a, SGMatrix<T>& result) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);
Expand Down Expand Up @@ -571,7 +571,8 @@ namespace shogun

/** Applies the elementwise rectified linear function f(x) = max(0,x) */
template <typename T>
void rectified_linear_impl(SGMatrix<T>& a, SGMatrix<T>& result) const
void
rectified_linear_impl(const SGMatrix<T>& a, SGMatrix<T>& result) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);
Expand Down Expand Up @@ -602,7 +603,8 @@ namespace shogun

/** ViennaCL vector inplace scale method: result = alpha * A */
template <typename T>
void scale_impl(SGVector<T>& a, SGVector<T>& result, T alpha) const
void
scale_impl(const SGVector<T>& a, SGVector<T>& result, T alpha) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);
Expand All @@ -613,7 +615,8 @@ namespace shogun

/** ViennaCL vector inplace scale method: result = alpha * A */
template <typename T>
void scale_impl(SGMatrix<T>& a, SGMatrix<T>& result, T alpha) const
void
scale_impl(const SGMatrix<T>& a, SGMatrix<T>& result, T alpha) const
{
GPUMemoryViennaCL<T>* a_gpu = cast_to_viennacl(a);
GPUMemoryViennaCL<T>* result_gpu = cast_to_viennacl(result);
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/mathematics/linalg/LinalgNamespace.h
Expand Up @@ -154,7 +154,7 @@ namespace shogun
* @param b SGVector to be set
*/
template <typename T>
void to_gpu(SGVector<T>& a, SGVector<T>& b)
void to_gpu(const SGVector<T>& a, SGVector<T>& b)
{
sg_linalg->m_gpu_transfer.lock();

Expand Down Expand Up @@ -190,7 +190,7 @@ namespace shogun
* @param b SGMatrix to be set
*/
template <typename T>
void to_gpu(SGMatrix<T>& a, SGMatrix<T>& b)
void to_gpu(const SGMatrix<T>& a, SGMatrix<T>& b)
{
sg_linalg->m_gpu_transfer.lock();

Expand Down
10 changes: 5 additions & 5 deletions src/shogun/mathematics/linalg/LinalgSpecialPurposes.h
Expand Up @@ -49,7 +49,7 @@ namespace shogun
* @param result The output matrix
*/
template <typename T>
void logistic(SGMatrix<T>& a, SGMatrix<T>& result)
void logistic(const SGMatrix<T>& a, SGMatrix<T>& result)
{
REQUIRE(
(a.num_rows == result.num_rows), "Number of rows of matrix a "
Expand All @@ -74,8 +74,8 @@ namespace shogun
* @param result The output matrix
*/
template <typename T>
void
multiply_by_logistic_derivative(SGMatrix<T>& a, SGMatrix<T>& result)
void multiply_by_logistic_derivative(
const SGMatrix<T>& a, SGMatrix<T>& result)
{
REQUIRE(
(a.num_rows == result.num_rows), "Number of rows of matrix a "
Expand All @@ -100,7 +100,7 @@ namespace shogun
*/
template <typename T>
void multiply_by_rectified_linear_derivative(
SGMatrix<T>& a, SGMatrix<T>& result)
const SGMatrix<T>& a, SGMatrix<T>& result)
{
REQUIRE(
(a.num_rows == result.num_rows), "Number of rows of matrix a "
Expand All @@ -125,7 +125,7 @@ namespace shogun
* @param result The output matrix
*/
template <typename T>
void rectified_linear(SGMatrix<T>& a, SGMatrix<T>& result)
void rectified_linear(const SGMatrix<T>& a, SGMatrix<T>& result)
{
REQUIRE(
(a.num_rows == result.num_rows), "Number of rows of matrix a "
Expand Down
16 changes: 8 additions & 8 deletions src/shogun/mathematics/linalg/backend/eigen/SpecialPurposes.cpp
Expand Up @@ -46,7 +46,7 @@ DEFINE_FOR_NON_INTEGER_REAL_PTYPE(BACKEND_GENERIC_CROSS_ENTROPY, SGMatrix)

#define BACKEND_GENERIC_LOGISTIC(Type, Container) \
void LinalgBackendEigen::logistic( \
Container<Type>& a, Container<Type>& result) const \
const Container<Type>& a, Container<Type>& result) const \
{ \
logistic_impl(a, result); \
}
Expand All @@ -55,7 +55,7 @@ DEFINE_FOR_NUMERIC_PTYPE(BACKEND_GENERIC_LOGISTIC, SGMatrix)

#define BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV(Type, Container) \
void LinalgBackendEigen::multiply_by_logistic_derivative( \
Container<Type>& a, Container<Type>& result) const \
const Container<Type>& a, Container<Type>& result) const \
{ \
multiply_by_logistic_derivative_impl(a, result); \
}
Expand All @@ -64,7 +64,7 @@ DEFINE_FOR_NUMERIC_PTYPE(BACKEND_GENERIC_MULTIPLY_BY_LOGISTIC_DERIV, SGMatrix)

#define BACKEND_GENERIC_MULTIPLY_BY_RECTIFIED_LINEAR_DERIV(Type, Container) \
void LinalgBackendEigen::multiply_by_rectified_linear_derivative( \
Container<Type>& a, Container<Type>& result) const \
const Container<Type>& a, Container<Type>& result) const \
{ \
multiply_by_rectified_linear_derivative_impl(a, result); \
}
Expand All @@ -74,7 +74,7 @@ DEFINE_FOR_NON_INTEGER_REAL_PTYPE(

#define BACKEND_GENERIC_RECTIFIED_LINEAR(Type, Container) \
void LinalgBackendEigen::rectified_linear( \
Container<Type>& a, Container<Type>& result) const \
const Container<Type>& a, Container<Type>& result) const \
{ \
rectified_linear_impl(a, result); \
}
Expand Down Expand Up @@ -115,7 +115,7 @@ T LinalgBackendEigen::cross_entropy_impl(

template <typename T>
void LinalgBackendEigen::logistic_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
const SGMatrix<T>& a, SGMatrix<T>& result) const
{
typename SGMatrix<T>::EigenMatrixXtMap a_eig = a;
typename SGMatrix<T>::EigenMatrixXtMap result_eig = result;
Expand All @@ -125,7 +125,7 @@ void LinalgBackendEigen::logistic_impl(

template <typename T>
void LinalgBackendEigen::multiply_by_logistic_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
const SGMatrix<T>& a, SGMatrix<T>& result) const
{
typename SGMatrix<T>::EigenMatrixXtMap a_eig = a;
typename SGMatrix<T>::EigenMatrixXtMap result_eig = result;
Expand All @@ -135,7 +135,7 @@ void LinalgBackendEigen::multiply_by_logistic_derivative_impl(

template <typename T>
void LinalgBackendEigen::multiply_by_rectified_linear_derivative_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
const SGMatrix<T>& a, SGMatrix<T>& result) const
{
typename SGMatrix<T>::EigenMatrixXtMap a_eig = a;
typename SGMatrix<T>::EigenMatrixXtMap result_eig = result;
Expand All @@ -147,7 +147,7 @@ void LinalgBackendEigen::multiply_by_rectified_linear_derivative_impl(

template <typename T>
void LinalgBackendEigen::rectified_linear_impl(
SGMatrix<T>& a, SGMatrix<T>& result) const
const SGMatrix<T>& a, SGMatrix<T>& result) const
{
typename SGMatrix<T>::EigenMatrixXtMap a_eig = a;
typename SGMatrix<T>::EigenMatrixXtMap result_eig = result;
Expand Down

0 comments on commit a7a79f1

Please sign in to comment.