SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ElementwiseOperations.h
Go to the documentation of this file.
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 ELEMENTWISE_OPERATIONS_H_
32 #define ELEMENTWISE_OPERATIONS_H_
33 
38 
39 namespace shogun
40 {
41 
42 namespace linalg
43 {
44 
55 template <class Operand, class UnaryOp>
56 auto elementwise_compute(Operand operand, UnaryOp unary_op)
57 -> typename Operand::template container_type<decltype(unary_op(operand.data()[0]))>
58 {
59  typedef decltype(unary_op(operand.data()[0])) ST;
60  typedef typename Operand::template container_type<ST> ReturnType;
61 
62  ReturnType result=util::allocate_result<Operand,ReturnType>::alloc(operand);
63 
64  implementation::elementwise_unary_operation<Backend::NATIVE, Operand,
65  ReturnType, UnaryOp>::compute(operand, result, unary_op);
66 
67  return result;
68 }
69 
79 template <class Operand, class UnaryOp>
80 void elementwise_compute_inplace(Operand operand, UnaryOp unary_op)
81 {
82  typedef typename Operand::Scalar T;
83  typedef decltype(unary_op(operand.data()[0])) ST;
84  static_assert(std::is_same<T,ST>::value, "Scalar type mismatch!\n");
85 
86  implementation::elementwise_unary_operation<Backend::NATIVE, Operand,
87  Operand, UnaryOp>::compute(operand, operand, unary_op);
88 }
89 
90 #ifdef HAVE_VIENNACL
91 
101 template <class Operand>
102 Operand elementwise_compute(Operand operand, std::string unary_op)
103 {
104  Operand result=util::allocate_result<Operand,Operand>::alloc(operand);
105  operations::ocl_operation operation(unary_op);
106 
107  implementation::elementwise_unary_operation<Backend::VIENNACL, Operand,
108  Operand, operations::ocl_operation>::compute(operand, result, operation);
109 
110  return result;
111 }
112 
122 template <class Operand>
123 void elementwise_compute_inplace(Operand operand, std::string unary_op)
124 {
125  operations::ocl_operation operation(unary_op);
126  implementation::elementwise_unary_operation<Backend::VIENNACL, Operand,
127  Operand, operations::ocl_operation>::compute(operand, operand, operation);
128 }
129 #endif // HAVE_VIENNACL
130 
139 template <Backend backend, class Operand>
140 typename Operand::template container_type<typename operations::sin<typename Operand::Scalar>::return_type>
141 elementwise_sin(Operand operand)
142 {
143  typedef typename Operand::Scalar T;
144  typedef typename operations::sin<T>::return_type ST;
145  typedef typename Operand::template container_type<ST> ReturnType;
146 
147  ReturnType result=util::allocate_result<Operand,ReturnType>::alloc(operand);
148 
149  operations::sin<T> operation;
151  ReturnType, operations::sin<T>>::compute(operand, result, operation);
152 
153  return result;
154 }
155 
164 template <Backend backend, class Operand>
165 void elementwise_sin_inplace(Operand operand)
166 {
167  typedef typename Operand::Scalar T;
168  typedef typename operations::sin<T>::return_type ST;
169  static_assert(std::is_same<T,ST>::value, "Scalar type mismatch!\n");
170 
171  operations::sin<T> operation;
173  Operand, operations::sin<T>>::compute(operand, operand, operation);
174 }
175 
176 }
177 
178 }
179 #endif // ELEMENTWISE_OPERATIONS_H_
void elementwise_sin_inplace(Operand operand)
auto elementwise_compute(Operand operand, UnaryOp unary_op) -> typename Operand::template container_type< decltype(unary_op(operand.data()[0]))>
Template struct elementwise_unary_operation. This struct is specialized for computing element-wise op...
Operand::template container_type< typename operations::sin< typename Operand::Scalar >::return_type > elementwise_sin(Operand operand)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static ReturnType alloc(Operand op)
void elementwise_compute_inplace(Operand operand, UnaryOp unary_op)

SHOGUN Machine Learning Toolbox - Documentation