ViennaCL - The Vienna Computing Library: viennacl/linalg/inner_prod.hpp Source File

ViennaCL - The Vienna Computing Library 1.7.1 Free open-source GPU-accelerated linear algebra and solver library. |

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inner_prod.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_INNER_PROD_HPP_
2 #define VIENNACL_LINALG_INNER_PROD_HPP_
3
4 /* =========================================================================
5 Copyright (c) 2010-2016, Institute for Microelectronics,
6 Institute for Analysis and Scientific Computing,
7 TU Wien.
8 Portions of this software are copyright by UChicago Argonne, LLC.
9
10 -----------------
11 ViennaCL - The Vienna Computing Library
12 -----------------
13
14 Project Head: Karl Rupp rupp@iue.tuwien.ac.at
15
16 (A list of authors and contributors can be found in the manual)
17
18 License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20
25 #include "viennacl/forwards.h"
26 #include "viennacl/tools/tools.hpp"
27 #include "viennacl/meta/enable_if.hpp"
28 #include "viennacl/meta/tag_of.hpp"
29 #include "viennacl/meta/result_of.hpp"
30
31 namespace viennacl
32 {
33 //
34 // generic inner_prod function
35 // uses tag dispatch to identify which algorithm
36 // should be called
37 //
38 namespace linalg
39 {
40
41 #ifdef VIENNACL_WITH_ARMADILLO
42 // ----------------------------------------------------
43 // Armadillo
44 //
45 template<typename NumericT>
46 NumericT inner_prod(arma::Col<NumericT> const& v1, arma::Col<NumericT> const& v2)
47 {
48 return dot(v1, v2);
49 }
50 #endif
51
52 #ifdef VIENNACL_WITH_EIGEN
53 // ----------------------------------------------------
54 // EIGEN
55 //
56 template<typename VectorT1, typename VectorT2>
57 typename viennacl::enable_if< viennacl::is_eigen< typename viennacl::traits::tag_of< VectorT1 >::type >::value,
58 typename VectorT1::RealScalar>::type
59 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
60 {
61 //std::cout << "eigen .. " << std::endl;
62 return v1.dot(v2);
63 }
64 #endif
65
66 #ifdef VIENNACL_WITH_MTL4
67 // ----------------------------------------------------
68 // MTL4
69 //
70 template<typename VectorT1, typename VectorT2>
71 typename viennacl::enable_if< viennacl::is_mtl4< typename viennacl::traits::tag_of< VectorT1 >::type >::value,
72 typename VectorT1::value_type>::type
73 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
74 {
75 //std::cout << "mtl4 .. " << std::endl;
76 return mtl::dot(v1, v2);
77 }
78 #endif
79
80 #ifdef VIENNACL_WITH_UBLAS
81 // ----------------------------------------------------
82 // UBLAS
83 //
84 template<typename VectorT1, typename VectorT2>
85 typename viennacl::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< VectorT1 >::type >::value,
86 typename VectorT1::value_type>::type
87 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
88 {
89 //std::cout << "ublas .. " << std::endl;
90 return boost::numeric::ublas::inner_prod(v1, v2);
91 }
92 #endif
93
94 // ----------------------------------------------------
95 // STL
96 //
97 template<typename VectorT1, typename VectorT2>
98 typename viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value,
99 typename VectorT1::value_type>::type
100 inner_prod(VectorT1 const & v1, VectorT2 const & v2)
101 {
102 assert(v1.size() == v2.size() && bool("Vector sizes mismatch"));
103 //std::cout << "stl .. " << std::endl;
104 typename VectorT1::value_type result = 0;
105 for (typename VectorT1::size_type i=0; i<v1.size(); ++i)
106 result += v1[i] * v2[i];
107
108 return result;
109 }
110
111 // ----------------------------------------------------
112 // VIENNACL
113 //
114 template<typename NumericT>
115 viennacl::scalar_expression< const vector_base<NumericT>, const vector_base<NumericT>, viennacl::op_inner_prod >
116 inner_prod(vector_base<NumericT> const & vector1,
117 vector_base<NumericT> const & vector2)
118 {
119 //std::cout << "viennacl .. " << std::endl;
120 return viennacl::scalar_expression< const vector_base<NumericT>,
121 const vector_base<NumericT>,
122 viennacl::op_inner_prod >(vector1, vector2);
123 }
124
125
126 // expression on lhs:
127 template< typename LHS, typename RHS, typename OP, typename NumericT>
128 viennacl::scalar_expression< const viennacl::vector_expression<LHS, RHS, OP>,
129 const vector_base<NumericT>,
130 viennacl::op_inner_prod >
131 inner_prod(viennacl::vector_expression<LHS, RHS, OP> const & vector1,
132 vector_base<NumericT> const & vector2)
133 {
134 //std::cout << "viennacl .. " << std::endl;
135 return viennacl::scalar_expression< const viennacl::vector_expression<LHS, RHS, OP>,
136 const vector_base<NumericT>,
137 viennacl::op_inner_prod >(vector1, vector2);
138 }
139
140 // expression on rhs:
141 template<typename NumericT, typename LHS, typename RHS, typename OP>
142 viennacl::scalar_expression< const vector_base<NumericT>,
143 const viennacl::vector_expression<LHS, RHS, OP>,
144 viennacl::op_inner_prod >
145 inner_prod(vector_base<NumericT> const & vector1,
146 viennacl::vector_expression<LHS, RHS, OP> const & vector2)
147 {
148 //std::cout << "viennacl .. " << std::endl;
149 return viennacl::scalar_expression< const vector_base<NumericT>,
150 const viennacl::vector_expression<LHS, RHS, OP>,
151 viennacl::op_inner_prod >(vector1, vector2);
152 }
153
154 // expression on lhs and rhs:
155 template<typename LHS1, typename RHS1, typename OP1,
156 typename LHS2, typename RHS2, typename OP2>
157 viennacl::scalar_expression< const viennacl::vector_expression<LHS1, RHS1, OP1>,
158 const viennacl::vector_expression<LHS2, RHS2, OP2>,
159 viennacl::op_inner_prod >
160 inner_prod(viennacl::vector_expression<LHS1, RHS1, OP1> const & vector1,
161 viennacl::vector_expression<LHS2, RHS2, OP2> const & vector2)
162 {
163 //std::cout << "viennacl .. " << std::endl;
164 return viennacl::scalar_expression< const viennacl::vector_expression<LHS1, RHS1, OP1>,
165 const viennacl::vector_expression<LHS2, RHS2, OP2>,
166 viennacl::op_inner_prod >(vector1, vector2);
167 }
168
169
170 // Multiple inner products:
171 template<typename NumericT>
172 viennacl::vector_expression< const vector_base<NumericT>, const vector_tuple<NumericT>, viennacl::op_inner_prod >
173 inner_prod(vector_base<NumericT> const & x,
174 vector_tuple<NumericT> const & y_tuple)
175 {
176 return viennacl::vector_expression< const vector_base<NumericT>,
177 const vector_tuple<NumericT>,
178 viennacl::op_inner_prod >(x, y_tuple);
179 }
180
181
182 } // end namespace linalg
183 } // end namespace viennacl
184 #endif
185
186
viennacl::enable_if
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
tag_of.hpp
Dispatch facility for distinguishing between ublas, STL and ViennaCL types.
tools.hpp
Various little tools used here and there in ViennaCL.
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::linalg::inner_prod
viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT1 >::type >::value, typename VectorT1::value_type >::type inner_prod(VectorT1 const &v1, VectorT2 const &v2)
Definition: inner_prod.hpp:100
viennacl::linalg::inner_prod
viennacl::vector_expression< const vector_base< NumericT >, const vector_tuple< NumericT >, viennacl::op_inner_prod > inner_prod(vector_base< NumericT > const &x, vector_tuple< NumericT > const &y_tuple)
Definition: inner_prod.hpp:173
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
NumericT
float NumericT
Definition: bisect.cpp:40
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
viennacl::vector_tuple
Tuple class holding pointers to multiple vectors. Mainly used as a temporary object returned from vie...
Definition: forwards.h:269
viennacl::vector_base< NumericT >
v2
viennacl::vector< int > v2
Definition: global_variables.cpp:61
viennacl::op_inner_prod
A tag class representing inner products of two vectors.
Definition: forwards.h:198
result_of.hpp
A collection of compile time type deductions.
enable_if.hpp
Simple enable-if variant that uses the SFINAE pattern.