diff --git a/src/shogun/kernel/ExponentialARDKernel.cpp b/src/shogun/kernel/ExponentialARDKernel.cpp index a0064395c08..e3f7d5fa631 100644 --- a/src/shogun/kernel/ExponentialARDKernel.cpp +++ b/src/shogun/kernel/ExponentialARDKernel.cpp @@ -244,7 +244,7 @@ SGMatrix CExponentialARDKernel::get_weighted_vector(SGVector #include +#include #ifndef SWIG // SWIG should skip this part namespace viennacl diff --git a/src/shogun/mathematics/linalg/internal/implementation/Apply.h b/src/shogun/mathematics/linalg/internal/implementation/Apply.h deleted file mode 100644 index 4e9a3c1c10c..00000000000 --- a/src/shogun/mathematics/linalg/internal/implementation/Apply.h +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (c) The Shogun Machine Learning Toolbox - * Written (w) 2015 Soumyajit De - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those - * of the authors and should not be interpreted as representing official policies, - * either expressed or implied, of the Shogun Development Team. - */ - -#ifndef APPLY_IMPL_H_ -#define APPLY_IMPL_H_ - -#include -#include -#include - -#include - -#ifdef HAVE_VIENNACL -#include -#include -#include -#include -#include -#endif // HAVE_VIENNACL - -#include - -namespace shogun -{ - -namespace linalg -{ - -namespace implementation -{ - -/** - * @brief Generic class which is specialized for different backends to perform apply - */ -template -struct apply -{ -}; - -/** - * @brief Partial specialization of apply for the Eigen3 backend - */ -template -struct apply -{ - static_assert(std::is_same::value, - "Different numeric scalar types for matrix and vector not allowed!\n"); - - /** Scalar type */ - typedef typename Matrix::Scalar T; - - /** Eigen matrix type */ - typedef Eigen::Matrix MatrixXt; - - /** Eigen Vector type */ - typedef Eigen::Matrix VectorXt; - - /** Performs the operation of matrix applied to a vector \f$x = Ab\f$. - * - * @param A The matrix - * @param b The vector - * @param transpose Whether to transpose A before applying to b - * @return x Result vector - */ - static SGVector compute(SGMatrix A, SGVector b, bool transpose) - { - SGVector x(A.num_rows); - compute(A, b, x, transpose); - return x; - } - - /** Performs the operation of matrix applied to a vector \f$x = Ab\f$. - * - * @param A The matrix - * @param b The vector - * @param x Result vector - * @param transpose Whether to transpose A before applying to b - */ - static void compute(SGMatrix A, SGVector b, SGVector x, bool transpose) - { - Eigen::Map A_eig=A; - Eigen::Map b_eig=b; - Eigen::Map x_eig=x; - - if (transpose) - x_eig=A_eig.transpose()*b_eig; - else - x_eig=A_eig*b_eig; - } -}; - -#ifdef HAVE_VIENNACL -/** - * @brief Partial specialization of apply for the ViennaCL backend - */ -template -struct apply -{ - static_assert(std::is_same::value, - "Different numeric scalar types for matrix and vector not allowed!\n"); - - /** Scalar type */ - typedef typename Matrix::Scalar T; - - /** Performs the operation of matrix applied to a vector \f$x = Ab\f$. - * - * @param A The matrix - * @param b The vector - * @param transpose Whether to transpose A before applying to b - * @return x Result vector - */ - static CGPUVector compute(CGPUMatrix A, CGPUVector b, bool transpose) - { - CGPUVector x(A.num_rows); - compute(A, b, x, transpose); - return x; - } - - /** Performs the operation of matrix applied to a vector \f$x = Ab\f$. - * - * @param A The matrix - * @param b The vector - * @param x Result vector - * @param transpose Whether to transpose A before applying to b - */ - static void compute(CGPUMatrix A, CGPUVector b, CGPUVector x, bool transpose) - { - if (transpose) - x.vcl_vector()=viennacl::linalg::prod(viennacl::trans(A.vcl_matrix()), b.vcl_vector()); - else - x.vcl_vector()=viennacl::linalg::prod(A.vcl_matrix(), b.vcl_vector()); - } -}; -#endif // HAVE_VIENNACL - -} - -} - -} -#endif // APPLY_IMPL_H_ diff --git a/src/shogun/mathematics/linalg/internal/implementation/ElementwiseProduct.h b/src/shogun/mathematics/linalg/internal/implementation/ElementwiseProduct.h deleted file mode 100644 index 7db4c969411..00000000000 --- a/src/shogun/mathematics/linalg/internal/implementation/ElementwiseProduct.h +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) The Shogun Machine Learning Toolbox - * Written (w) 2014 Khaled Nasr - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those - * of the authors and should not be interpreted as representing official policies, - * either expressed or implied, of the Shogun Development Team. - */ - -#ifndef ELEMENTWISE_PRODUCT_IMPL_H_ -#define ELEMENTWISE_PRODUCT_IMPL_H_ - -#include -#include - -#include - -#ifdef HAVE_VIENNACL -#include -#include -#endif // HAVE_VIENNACL - -namespace shogun -{ - -namespace linalg -{ - -namespace implementation -{ - -/** Generic class which is specialized for different backends to perform - * elementwise multiplication - */ -template -struct elementwise_product -{ - /** Scalar type */ - typedef typename Matrix::Scalar T; - - /** Performs the operation C = A .* B where ".*" denotes elementwise multiplication */ - static void compute(Matrix A, Matrix B, Matrix C); -}; - - -/** Specialization of elementwise_product for the Eigen3 backend */ -template -struct elementwise_product -{ - /** Scalar type */ - typedef typename Matrix::Scalar T; - - /** Return type */ - typedef SGMatrix ReturnType; - - /** Eigen3 matrix type */ - typedef Eigen::Matrix MatrixXt; - - /** Performs the operation C = A .* B where ".*" denotes elementwise multiplication. - * - * This version returns the result in a newly created matrix. If elementwise-product - * is desired that will work irrespective of the backend and the matrix type used, - * then this method should be used. - * - * @param A First matrix - * @param B Second matrix - * @return The result of the operation - */ - static ReturnType compute(SGMatrix A, SGMatrix B) - { - REQUIRE(A.matrix, "Matrix A is not initialized!\n"); - REQUIRE(B.matrix, "Matrix A is not initialized!\n"); - - REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols, - "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n", - A.num_rows, A.num_cols, B.num_rows, B.num_cols); - - ReturnType retMatrix(A.num_rows, A.num_cols); - compute(A, B, retMatrix); - - return retMatrix; - } - - /** Performs the operation C = A .* B where ".*" denotes elementwise multiplication. - * - * This version should be used for backend specific code requirements. For example, - * use this with CGPUMatrix and explicitly set ViennaCL backend, or SGMatrix and - * explicitly set Eigen3 backend. If matrix-type/backend-type independent code is - * desired, use the version that does not support preallocated result matrix but - * returns the result in a newly created matrix instead. - * - * @param A First matrix - * @param B Second matrix - * @param C Result of the operation - */ - static void compute(SGMatrix A, SGMatrix B, SGMatrix C) - { - Eigen::Map A_eig = A; - Eigen::Map B_eig = B; - Eigen::Map C_eig = C; - - C_eig = A_eig.array() * B_eig.array(); - } -}; - -#ifdef HAVE_VIENNACL - -/** Specialization of elementwise_product for the ViennaCL backend */ -template -struct elementwise_product -{ - /** Scalar type */ - typedef typename Matrix::Scalar T; - - /** Return type */ - typedef CGPUMatrix ReturnType; - - /** Performs the operation C = A .* B where ".*" denotes elementwise multiplication. - * - * This version returns the result in a newly created matrix. If elementwise-product - * is desired that will work irrespective of the backend and the matrix type used, - * then this method should be used. - * - * @param A First matrix - * @param B Second matrix - * @return The result of the operation - */ - static ReturnType compute(CGPUMatrix A, CGPUMatrix B) - { - REQUIRE(A.matrix, "Matrix A is not initialized!\n"); - REQUIRE(B.matrix, "Matrix A is not initialized!\n"); - - REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols, - "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n", - A.num_rows, A.num_cols, B.num_rows, B.num_cols); - - ReturnType retMatrix(A.num_rows, A.num_cols); - compute(A, B, retMatrix); - - return retMatrix; - } - - /** Performs the operation C = A .* B where ".*" denotes elementwise multiplication. - * - * This version should be used for backend specific code requirements. For example, - * use this with CGPUMatrix and explicitly set ViennaCL backend, or SGMatrix and - * explicitly set Eigen3 backend. If matrix-type/backend-type independent code is - * desired, use the version that does not support preallocated result matrix but - * returns the result in a newly created matrix instead. - * - * @param A First matrix - * @param B Second matrix - * @param C Result of the operation - */ - static void compute(CGPUMatrix A, CGPUMatrix B, CGPUMatrix C) - { - C.vcl_matrix() = viennacl::linalg::element_prod(A.vcl_matrix(), B.vcl_matrix()); - } -}; - -#endif // HAVE_VIENNACL - -} - -} - -} -#endif // ELEMENTWISE_PRODUCT_IMPL_H_ diff --git a/src/shogun/mathematics/linalg/internal/modules/Core.h b/src/shogun/mathematics/linalg/internal/modules/Core.h index 62136ab15c0..5937aca1030 100644 --- a/src/shogun/mathematics/linalg/internal/modules/Core.h +++ b/src/shogun/mathematics/linalg/internal/modules/Core.h @@ -33,8 +33,6 @@ #define CORE_H_ #include -#include -#include namespace shogun { @@ -43,74 +41,6 @@ namespace linalg { #ifdef HAVE_LINALG_LIB -/** Performs the operation of matrix applied to a vector \f$x = Ab\f$. - * - * This version should be used for backend specific code requirements. For example, - * use this with CGPUMatrix, CGPUVector and explicitly set ViennaCL backend, or - * SGMatrix, SGVector and explicitly set Eigen3 backend. If matrix-type/backend-type - * independent code is desired, use the version that does not support preallocated - * result vector but returns the result in a newly created vector instead. - * - * @param A The matrix - * @param b The vector - * @param x Result vector - */ -template ::backend,class Matrix,class Vector> -void apply(Matrix A, Vector b, Vector x, bool transpose=false) -{ - implementation::apply::compute(A, b, x, transpose); -} - -/** Performs the operation of matrix applied to a vector \f$x = Ab\f$. - * - * This version returns the result in a newly created vector. If apply is desired - * that will work irrespective of the backend and the matrix/vector type used, - * then this method should be used. - * - * @param A The matrix - * @param b The vector - * @param x Result vector - */ -template ::backend,class Matrix,class Vector> -Vector apply(Matrix A, Vector b, bool transpose=false) -{ - return implementation::apply::compute(A, b, transpose); -} - -/** Performs the operation C = A .* B where ".*" denotes elementwise multiplication. - * - * This version should be used for backend specific code requirements. For example, - * use this with CGPUMatrix and explicitly set ViennaCL backend, or SGMatrix and - * explicitly set Eigen3 backend. If matrix-type/backend-type independent code is - * desired, use the version that does not support preallocated result matrix but - * returns the result in a newly created matrix instead. - * - * @param A First matrix - * @param B Second matrix - * @param C Result of the operation - */ -template ::backend,class Matrix> -void elementwise_product(Matrix A, Matrix B, Matrix C) -{ - implementation::elementwise_product::compute(A, B, C); -} - -/** Performs the operation C = A .* B where ".*" denotes elementwise multiplication. - * - * This version returns the result in a newly created matrix. If elementwise-product - * is desired that will work irrespective of the backend and the matrix type used, - * then this method should be used. - * - * @param A First matrix - * @param B Second matrix - * @return The result of the operation - */ -template ::backend,class Matrix> -typename implementation::elementwise_product::ReturnType elementwise_product(Matrix A, Matrix B) -{ - return implementation::elementwise_product::compute(A, B); -} - /** * Wrapper method for internal implementation of square of co-efficients that works * with generic dense matrices. diff --git a/tests/unit/mathematics/linalg/Apply_unittest.cc b/tests/unit/mathematics/linalg/Apply_unittest.cc deleted file mode 100644 index ca95bb73aee..00000000000 --- a/tests/unit/mathematics/linalg/Apply_unittest.cc +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright (c) The Shogun Machine Learning Toolbox - * Written (w) 2015 Soumyajit De - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those - * of the authors and should not be interpreted as representing official policies, - * either expressed or implied, of the Shogun Development Team. - */ - -#include - -#ifdef HAVE_LINALG_LIB -#include -#include -#include -#include - -#include - -#ifdef HAVE_VIENNACL -#include -#include -#endif // HAVE_VIENNACL - -using namespace shogun; - -TEST(Apply, SGMatrix_Eigen3_backend) -{ - const index_t rows=4; - const index_t cols=3; - - SGMatrix A(rows, cols); - SGVector b(cols); - SGVector x(rows); - - for (index_t i=0; i(A, b, x); - - float64_t ref[] = {10,11.5,13,14.5}; - - for (index_t i=0; i A(rows, cols); - SGVector b(rows); - SGVector x(cols); - - for (index_t i=0; i(A, b, x, true); - - float64_t ref[] = {10,11.5,13,14.5}; - - for (index_t i=0; i A(rows, cols); - CGPUVector b(cols); - - for (index_t i=0; i x=linalg::apply(A, b); - - float64_t ref[] = {10,11.5,13,14.5}; - - for (index_t i=0; i A(rows, cols); - CGPUVector b(cols); - CGPUVector x(rows); - - for (index_t i=0; i(A, b, x); - - float64_t ref[] = {10,11.5,13,14.5}; - - for (index_t i=0; i A(rows, cols); - CGPUVector b(rows); - CGPUVector x(cols); - - for (index_t i=0; i(A, b, x, true); - - float64_t ref[] = {10,11.5,13,14.5}; - - for (index_t i=0; i A(rows, cols); - SGVector b(cols); - - for (index_t i=0; i x=linalg::apply(A, b); - - float64_t ref[] = {10,11.5,13,14.5}; - - for (index_t i=0; i - -#ifdef HAVE_LINALG_LIB -#include -#include -#include - -#include - -#ifdef HAVE_VIENNACL -#include -#endif - -using namespace shogun; - -TEST(ElementwiseProduct, SGMatrix_eigen3_backend) -{ - SGMatrix A(3,3); - SGMatrix B(3,3); - SGMatrix C(3,3); - - for (int32_t i=0; i<9; i++) - { - A[i] = i; - B[i] = 0.5*i; - } - - linalg::elementwise_product(A, B, C); - - for (int32_t i=0; i<9; i++) - EXPECT_NEAR(A[i]*B[i], C[i], 1e-15); -} - -#ifdef HAVE_VIENNACL -TEST(ElementwiseProduct, CGPUMatrix_eigen3_backend) -{ - CGPUMatrix A(3,3); - CGPUMatrix B(3,3); - - for (int32_t i=0; i<9; i++) - { - A[i] = i; - B[i] = 0.5*i; - } - - CGPUMatrix C = linalg::elementwise_product(A, B); - - for (int32_t i=0; i<9; i++) - EXPECT_NEAR(A[i]*B[i], C[i], 1e-15); -} - -TEST(ElementwiseProduct, CGPUMatrix_viennacl_backend) -{ - CGPUMatrix A(3,3); - CGPUMatrix B(3,3); - CGPUMatrix C(3,3); - - for (int32_t i=0; i<9; i++) - { - A[i] = i; - B[i] = 0.5*i; - } - - linalg::elementwise_product(A, B, C); - - for (int32_t i=0; i<9; i++) - EXPECT_NEAR(A[i]*B[i], C[i], 1e-15); -} - -TEST(ElementwiseProduct, SGMatrix_viennacl_backend) -{ - SGMatrix A(3,3); - SGMatrix B(3,3); - - for (int32_t i=0; i<9; i++) - { - A[i] = i; - B[i] = 0.5*i; - } - - SGMatrix C = linalg::elementwise_product(A, B); - - for (int32_t i=0; i<9; i++) - EXPECT_NEAR(A[i]*B[i], C[i], 1e-15); -} -#endif // HAVE_VIENNACL - -#endif // HAVE_LINALG_LIB