SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
ElementwiseProduct.h
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Khaled Nasr
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
31 #ifndef ELEMENTWISE_PRODUCT_IMPL_H_
32 #define ELEMENTWISE_PRODUCT_IMPL_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
36 
37 #ifdef HAVE_EIGEN3
39 #endif // HAVE_EIGEN3
40 
41 #ifdef HAVE_VIENNACL
42 #include <shogun/lib/GPUMatrix.h>
43 #include <viennacl/linalg/matrix_operations.hpp>
44 #endif // HAVE_VIENNACL
45 
46 namespace shogun
47 {
48 
49 namespace linalg
50 {
51 
52 namespace implementation
53 {
54 
58 template <enum Backend, class Matrix>
60 {
62  typedef typename Matrix::Scalar T;
63 
65  static void compute(Matrix A, Matrix B, Matrix C);
66 };
67 
68 #ifdef HAVE_EIGEN3
69 
71 template <class Matrix>
72 struct elementwise_product<Backend::EIGEN3, Matrix>
73 {
75  typedef typename Matrix::Scalar T;
76 
79 
82 
93  static ReturnType compute(SGMatrix<T> A, SGMatrix<T> B)
94  {
95  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
96  REQUIRE(B.matrix, "Matrix A is not initialized!\n");
97 
98  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
99  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
100  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
101 
102  ReturnType retMatrix(A.num_rows, A.num_cols);
103  compute(A, B, retMatrix);
104 
105  return retMatrix;
106  }
107 
121  {
122  Eigen::Map<MatrixXt> A_eig = A;
123  Eigen::Map<MatrixXt> B_eig = B;
124  Eigen::Map<MatrixXt> C_eig = C;
125 
126  C_eig = A_eig.array() * B_eig.array();
127  }
128 };
129 #endif // HAVE_EIGEN3
130 
131 #ifdef HAVE_VIENNACL
132 
134 template <class Matrix>
135 struct elementwise_product<Backend::VIENNACL, Matrix>
136 {
138  typedef typename Matrix::Scalar T;
139 
141  typedef CGPUMatrix<T> ReturnType;
142 
153  static ReturnType compute(CGPUMatrix<T> A, CGPUMatrix<T> B)
154  {
155  REQUIRE(A.matrix, "Matrix A is not initialized!\n");
156  REQUIRE(B.matrix, "Matrix A is not initialized!\n");
157 
158  REQUIRE(A.num_rows == B.num_rows && A.num_cols == B.num_cols,
159  "Dimension mismatch! A(%d x %d) vs B(%d x %d)\n",
160  A.num_rows, A.num_cols, B.num_rows, B.num_cols);
161 
162  ReturnType retMatrix(A.num_rows, A.num_cols);
163  compute(A, B, retMatrix);
164 
165  return retMatrix;
166  }
167 
180  static void compute(CGPUMatrix<T> A, CGPUMatrix<T> B, CGPUMatrix<T> C)
181  {
182  C.vcl_matrix() = viennacl::linalg::element_prod(A.vcl_matrix(), B.vcl_matrix());
183  }
184 };
185 
186 #endif // HAVE_VIENNACL
187 
188 }
189 
190 }
191 
192 }
193 #endif // ELEMENTWISE_PRODUCT_IMPL_H_
static void compute(Matrix A, Matrix B, Matrix C)
#define REQUIRE(x,...)
Definition: SGIO.h:206
index_t num_cols
Definition: SGMatrix.h:378
index_t num_rows
Definition: SGMatrix.h:376
shogun matrix
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static void compute(SGMatrix< T > A, SGMatrix< T > B, SGMatrix< T > C)

SHOGUN 机器学习工具包 - 项目文档