SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
Apply.h
浏览该文件的文档.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Soumyajit De
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 APPLY_IMPL_H_
32 #define APPLY_IMPL_H_
33 
34 #include <shogun/lib/config.h>
35 #include <shogun/lib/SGMatrix.h>
36 #include <shogun/lib/SGVector.h>
37 
38 #ifdef HAVE_EIGEN3
40 #endif // HAVE_EIGEN3
41 
42 #ifdef HAVE_VIENNACL
43 #include <shogun/lib/GPUMatrix.h>
44 #include <shogun/lib/GPUVector.h>
45 #include <viennacl/linalg/matrix_operations.hpp>
46 #include <viennacl/linalg/vector_operations.hpp>
47 #endif // HAVE_VIENNACL
48 
49 #include <type_traits>
50 
51 namespace shogun
52 {
53 
54 namespace linalg
55 {
56 
57 namespace implementation
58 {
59 
63 template <enum Backend, class Matrix, class Vector>
64 struct apply
65 {
66 };
67 
68 #ifdef HAVE_EIGEN3
69 
72 template <class Matrix, class Vector>
73 struct apply<Backend::EIGEN3, Matrix, Vector>
74 {
75  static_assert(std::is_same<typename Matrix::Scalar,typename Vector::Scalar>::value,
76  "Different numeric scalar types for matrix and vector not allowed!\n");
77 
79  typedef typename Matrix::Scalar T;
80 
83 
85  typedef Eigen::Matrix<T,Eigen::Dynamic,1> VectorXt;
86 
94  static SGVector<T> compute(SGMatrix<T> A, SGVector<T> b, bool transpose)
95  {
96  SGVector<T> x(A.num_rows);
97  compute(A, b, x, transpose);
98  return x;
99  }
100 
108  static void compute(SGMatrix<T> A, SGVector<T> b, SGVector<T> x, bool transpose)
109  {
110  Eigen::Map<MatrixXt> A_eig=A;
111  Eigen::Map<VectorXt> b_eig=b;
112  Eigen::Map<VectorXt> x_eig=x;
113 
114  if (transpose)
115  x_eig=A_eig.transpose()*b_eig;
116  else
117  x_eig=A_eig*b_eig;
118  }
119 };
120 #endif // HAVE_EIGEN3
121 
122 #ifdef HAVE_VIENNACL
123 
126 template <class Matrix, class Vector>
127 struct apply<Backend::VIENNACL, Matrix, Vector>
128 {
129  static_assert(std::is_same<typename Matrix::Scalar,typename Vector::Scalar>::value,
130  "Different numeric scalar types for matrix and vector not allowed!\n");
131 
133  typedef typename Matrix::Scalar T;
134 
142  static CGPUVector<T> compute(CGPUMatrix<T> A, CGPUVector<T> b, bool transpose)
143  {
144  CGPUVector<T> x(A.num_rows);
145  compute(A, b, x, transpose);
146  return x;
147  }
148 
156  static void compute(CGPUMatrix<T> A, CGPUVector<T> b, CGPUVector<T> x, bool transpose)
157  {
158  if (transpose)
159  x.vcl_vector()=viennacl::linalg::prod(viennacl::trans(A.vcl_matrix()), b.vcl_vector());
160  else
161  x.vcl_vector()=viennacl::linalg::prod(A.vcl_matrix(), b.vcl_vector());
162  }
163 };
164 #endif // HAVE_VIENNACL
165 
166 }
167 
168 }
169 
170 }
171 #endif // APPLY_IMPL_H_
Generic class which is specialized for different backends to perform apply.
Definition: Apply.h:64
index_t num_rows
Definition: SGMatrix.h:376
shogun matrix
shogun vector
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

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