diff --git a/src/shogun/mathematics/linalg/LinalgBackendBase.h b/src/shogun/mathematics/linalg/LinalgBackendBase.h index cf2dbec009b..b139fc06d03 100644 --- a/src/shogun/mathematics/linalg/LinalgBackendBase.h +++ b/src/shogun/mathematics/linalg/LinalgBackendBase.h @@ -389,8 +389,8 @@ namespace shogun */ #define BACKEND_GENERIC_IN_PLACE_MATRIX_PROD(Type, Container) \ virtual void matrix_prod( \ - SGMatrix& a, Container& b, Container& result, \ - bool transpose_A, bool transpose_B) const \ + const SGMatrix& a, const Container& b, \ + Container& result, bool transpose_A, bool transpose_B) const \ { \ SG_SNOTIMPLEMENTED; \ } @@ -529,7 +529,7 @@ namespace shogun */ #define BACKEND_GENERIC_IN_PLACE_SCALE(Type, Container) \ virtual void scale( \ - Container& a, Type alpha, Container& result) const \ + const Container& a, Type alpha, Container& result) const \ { \ SG_SNOTIMPLEMENTED; \ } diff --git a/src/shogun/mathematics/linalg/LinalgBackendEigen.h b/src/shogun/mathematics/linalg/LinalgBackendEigen.h index 639ea84caa0..905b0890e38 100644 --- a/src/shogun/mathematics/linalg/LinalgBackendEigen.h +++ b/src/shogun/mathematics/linalg/LinalgBackendEigen.h @@ -199,8 +199,8 @@ namespace shogun /** Implementation of @see LinalgBackendBase::matrix_prod */ #define BACKEND_GENERIC_IN_PLACE_MATRIX_PROD(Type, Container) \ virtual void matrix_prod( \ - SGMatrix& a, Container& b, Container& result, \ - bool transpose_A, bool transpose_B) const; + const SGMatrix& a, const Container& b, \ + Container& result, bool transpose_A, bool transpose_B) const; DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_IN_PLACE_MATRIX_PROD, SGVector) DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_IN_PLACE_MATRIX_PROD, SGMatrix) #undef BACKEND_GENERIC_IN_PLACE_MATRIX_PROD @@ -267,7 +267,7 @@ namespace shogun /** Implementation of @see linalg::scale */ #define BACKEND_GENERIC_IN_PLACE_SCALE(Type, Container) \ virtual void scale( \ - Container& a, Type alpha, Container& result) const; + const Container& a, Type alpha, Container& result) const; DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_IN_PLACE_SCALE, SGVector) DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_IN_PLACE_SCALE, SGMatrix) #undef BACKEND_GENERIC_IN_PLACE_SCALE @@ -531,13 +531,13 @@ namespace shogun /** Eigen3 matrix * vector in-place product method */ template void matrix_prod_impl( - SGMatrix& a, SGVector& b, SGVector& result, bool transpose, - bool transpose_B = false) const; + const SGMatrix& a, const SGVector& b, SGVector& result, + bool transpose, bool transpose_B = false) const; /** Eigen3 matrix in-place product method */ template void matrix_prod_impl( - SGMatrix& a, SGMatrix& b, SGMatrix& result, + const SGMatrix& a, const SGMatrix& b, SGMatrix& result, bool transpose_A, bool transpose_B) const; /** Return the largest element in the vector with Eigen3 library */ @@ -594,11 +594,13 @@ namespace shogun /** Eigen3 vector inplace scale method: result = alpha * A */ template - void scale_impl(SGVector& a, T alpha, SGVector& result) const; + void + scale_impl(const SGVector& a, T alpha, SGVector& result) const; /** Eigen3 matrix inplace scale method: result = alpha * A */ template - void scale_impl(SGMatrix& a, T alpha, SGMatrix& result) const; + void + scale_impl(const SGMatrix& a, T alpha, SGMatrix& result) const; /** Eigen3 set const method */ template class Container> diff --git a/src/shogun/mathematics/linalg/LinalgNamespace.h b/src/shogun/mathematics/linalg/LinalgNamespace.h index 75c76f69ab2..b93cf0f3d2a 100644 --- a/src/shogun/mathematics/linalg/LinalgNamespace.h +++ b/src/shogun/mathematics/linalg/LinalgNamespace.h @@ -818,7 +818,8 @@ namespace shogun */ template void element_prod( - Block>& a, Block>& b, SGMatrix& result) + const Block>& a, const Block>& b, + SGMatrix& result) { REQUIRE( a.m_row_size == b.m_row_size && a.m_col_size == b.m_col_size, @@ -850,7 +851,8 @@ namespace shogun * @return The result of the operation */ template - SGMatrix element_prod(Block>& a, Block>& b) + SGMatrix + element_prod(const Block>& a, const Block>& b) { REQUIRE( a.m_row_size == b.m_row_size && a.m_col_size == b.m_col_size, @@ -877,7 +879,8 @@ namespace shogun * @param result Result matrix */ template - void element_prod(SGMatrix& a, SGMatrix& b, SGMatrix& result) + void element_prod( + const SGMatrix& a, const SGMatrix& b, SGMatrix& result) { REQUIRE( a.num_rows == b.num_rows && a.num_cols == b.num_cols, @@ -912,7 +915,7 @@ namespace shogun * @return The result of the operation */ template - SGMatrix element_prod(SGMatrix& a, SGMatrix& b) + SGMatrix element_prod(const SGMatrix& a, const SGMatrix& b) { REQUIRE( a.num_rows == b.num_rows && a.num_cols == b.num_cols, @@ -1015,7 +1018,7 @@ namespace shogun */ template void matrix_prod( - SGMatrix& A, SGVector& b, SGVector& result, + const SGMatrix& A, const SGVector& b, SGVector& result, bool transpose = false) { if (transpose) @@ -1069,8 +1072,8 @@ namespace shogun * @return result Result vector */ template - SGVector - matrix_prod(SGMatrix& A, SGVector& b, bool transpose = false) + SGVector matrix_prod( + const SGMatrix& A, const SGVector& b, bool transpose = false) { SGVector result; if (transpose) @@ -1113,7 +1116,7 @@ namespace shogun */ template void matrix_prod( - SGMatrix& A, SGMatrix& B, SGMatrix& result, + const SGMatrix& A, const SGMatrix& B, SGMatrix& result, bool transpose_A = false, bool transpose_B = false) { REQUIRE( @@ -1214,8 +1217,8 @@ namespace shogun */ template SGMatrix matrix_prod( - SGMatrix& A, SGMatrix& B, bool transpose_A = false, - bool transpose_B = false) + const SGMatrix& A, const SGMatrix& B, + bool transpose_A = false, bool transpose_B = false) { SGMatrix result; @@ -1278,8 +1281,8 @@ namespace shogun */ template void dgemv( - T alpha, SGMatrix a, bool transpose, SGVector x, T beta, - SGVector& y) + T alpha, const SGMatrix& a, bool transpose, const SGVector& x, + T beta, SGVector& y) { auto temp_vector = matrix_prod(a, x, transpose); add(temp_vector, y, y, alpha, beta); @@ -1302,8 +1305,8 @@ namespace shogun */ template void dgemm( - T alpha, SGMatrix a, SGMatrix b, bool transpose_a, - bool transpose_b, T beta, SGMatrix& c) + T alpha, const SGMatrix& a, const SGMatrix& b, + bool transpose_a, bool transpose_b, T beta, SGMatrix& c) { auto temp_matrix = matrix_prod(a, b, transpose_a, transpose_b); add(temp_matrix, c, c, alpha, beta); @@ -1408,7 +1411,7 @@ namespace shogun * @param result The vector of alpha * a */ template - void scale(SGVector& a, SGVector& result, T alpha = 1) + void scale(const SGVector& a, SGVector& result, T alpha = 1) { REQUIRE( result.vlen == a.vlen, @@ -1428,7 +1431,7 @@ namespace shogun * @param result The matrix of alpha * A */ template - void scale(SGMatrix& A, SGMatrix& result, T alpha = 1) + void scale(const SGMatrix& A, SGMatrix& result, T alpha = 1) { REQUIRE( (A.num_rows == result.num_rows), "Number of rows of matrix A " @@ -1452,7 +1455,7 @@ namespace shogun * @return Vector or matrix of alpha * A */ template class Container> - Container scale(Container& a, T alpha = 1) + Container scale(const Container& a, T alpha = 1) { auto result = a.clone(); scale(a, result, alpha); diff --git a/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp b/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp index 9d2f9a52941..36c7b5b0b64 100644 --- a/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp +++ b/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp @@ -129,8 +129,8 @@ DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_EXPONENT, SGMatrix) #define BACKEND_GENERIC_IN_PLACE_MATRIX_PROD(Type, Container) \ void LinalgBackendEigen::matrix_prod( \ - SGMatrix& a, Container& b, Container& result, \ - bool transpose_A, bool transpose_B) const \ + const SGMatrix& a, const Container& b, \ + Container& result, bool transpose_A, bool transpose_B) const \ { \ matrix_prod_impl(a, b, result, transpose_A, transpose_B); \ } @@ -140,7 +140,7 @@ DEFINE_FOR_ALL_PTYPE(BACKEND_GENERIC_IN_PLACE_MATRIX_PROD, SGMatrix) #define BACKEND_GENERIC_IN_PLACE_SCALE(Type, Container) \ void LinalgBackendEigen::scale( \ - Container& a, Type alpha, Container& result) const \ + const Container& a, Type alpha, Container& result) const \ { \ scale_impl(a, alpha, result); \ } @@ -303,8 +303,8 @@ void LinalgBackendEigen::exponent_impl( template void LinalgBackendEigen::matrix_prod_impl( - SGMatrix& a, SGVector& b, SGVector& result, bool transpose, - bool transpose_B) const + const SGMatrix& a, const SGVector& b, SGVector& result, + bool transpose, bool transpose_B) const { typename SGMatrix::EigenMatrixXtMap a_eig = a; typename SGVector::EigenVectorXtMap b_eig = b; @@ -318,8 +318,8 @@ void LinalgBackendEigen::matrix_prod_impl( template void LinalgBackendEigen::matrix_prod_impl( - SGMatrix& a, SGMatrix& b, SGMatrix& result, bool transpose_A, - bool transpose_B) const + const SGMatrix& a, const SGMatrix& b, SGMatrix& result, + bool transpose_A, bool transpose_B) const { typename SGMatrix::EigenMatrixXtMap a_eig = a; typename SGMatrix::EigenMatrixXtMap b_eig = b; @@ -340,7 +340,7 @@ void LinalgBackendEigen::matrix_prod_impl( template void LinalgBackendEigen::scale_impl( - SGVector& a, T alpha, SGVector& result) const + const SGVector& a, T alpha, SGVector& result) const { typename SGVector::EigenVectorXtMap a_eig = a; typename SGVector::EigenVectorXtMap result_eig = result; @@ -350,7 +350,7 @@ void LinalgBackendEigen::scale_impl( template void LinalgBackendEigen::scale_impl( - SGMatrix& a, T alpha, SGMatrix& result) const + const SGMatrix& a, T alpha, SGMatrix& result) const { typename SGMatrix::EigenMatrixXtMap a_eig = a; typename SGMatrix::EigenMatrixXtMap result_eig = result;