ViennaCL - The Vienna Computing Library  1.2.0
norm_2.hpp
Go to the documentation of this file.
1 #ifndef VIENNACL_LINALG_NORM_2_HPP_
2 #define VIENNACL_LINALG_NORM_2_HPP_
3 
4 /* =========================================================================
5  Copyright (c) 2010-2011, Institute for Microelectronics,
6  Institute for Analysis and Scientific Computing,
7  TU Wien.
8 
9  -----------------
10  ViennaCL - The Vienna Computing Library
11  -----------------
12 
13  Project Head: Karl Rupp rupp@iue.tuwien.ac.at
14 
15  (A list of authors and contributors can be found in the PDF manual)
16 
17  License: MIT (X11), see file LICENSE in the base directory
18 ============================================================================= */
19 
24 #include <math.h> //for sqrt()
25 #include "viennacl/forwards.h"
26 #include "viennacl/tools/tools.hpp"
28 #include "viennacl/meta/tag_of.hpp"
29 
30 namespace viennacl
31 {
32  //
33  // generic norm_2 function
34  // uses tag dispatch to identify which algorithm
35  // should be called
36  //
37  namespace linalg
38  {
39  #ifdef VIENNACL_HAVE_MTL4
40  // ----------------------------------------------------
41  // MTL4
42  //
43  #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
44  template <typename ScalarType>
45  ScalarType norm_2(mtl::dense_vector<ScalarType> const & v)
46  {
47  // std::cout << "mtl4 .. " << std::endl;
48  return mtl::two_norm(v);
49  }
50 
51  #else
52  template< typename VectorT >
53  typename VectorT::value_type
54  norm_2(VectorT const& v,
56  >::type* dummy = 0)
57  {
58  // std::cout << "mtl4 .. " << std::endl;
59  return mtl::two_norm(v);
60  }
61  #endif
62  #endif
63 
64 
65  #ifdef VIENNACL_HAVE_EIGEN
66  // ----------------------------------------------------
67  // EIGEN
68  //
69  #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
70  float norm_2(Eigen::VectorXf const & v)
71  {
72  // std::cout << "eigen .. " << std::endl;
73  return v.norm();
74  }
75 
76  double norm_2(Eigen::VectorXd const & v)
77  {
78  // std::cout << "eigen .. " << std::endl;
79  return v.norm();
80  }
81 
82  #else
83  template< typename VectorT >
84  typename VectorT::RealScalar
85  norm_2(VectorT const& v,
87  >::type* dummy = 0)
88  {
89  // std::cout << "ublas .. " << std::endl;
90  return v.norm();
91  }
92  #endif
93  #endif
94 
95 
96  #ifdef VIENNACL_HAVE_UBLAS
97  // ----------------------------------------------------
98  // UBLAS
99  //
100  #if defined(_MSC_VER) && _MSC_VER < 1500 //Visual Studio 2005 needs special treatment
101  template< typename ScalarType >
102  ScalarType
103  norm_2(boost::numeric::ublas::vector<ScalarType> const & v)
104  {
105  // std::cout << "ublas .. " << std::endl;
107  }
108  #else
109  template< typename VectorT >
110  typename VectorT::value_type
111  norm_2(VectorT const& v,
113  >::type* dummy = 0)
114  {
115  // std::cout << "ublas .. " << std::endl;
117  }
118  #endif
119  #endif
120 
121 
122  // ----------------------------------------------------
123  // STL
124  //
125  template< typename VectorT>
126  typename VectorT::value_type
127  norm_2(VectorT const& v1,
129  >::type* dummy = 0)
130  {
131  //std::cout << "stl .. " << std::endl;
132  typename VectorT::value_type result = 0;
133  for (typename VectorT::size_type i=0; i<v1.size(); ++i)
134  result += v1[i] * v1[i];
135 
136  return sqrt(result);
137  }
138 
139  // ----------------------------------------------------
140  // VIENNACL
141  //
142  template< typename ScalarType, unsigned int alignment >
145  viennacl::op_norm_2 >
148  >::type* dummy = 0)
149  {
150  //std::cout << "viennacl .. " << std::endl;
153  viennacl::op_norm_2 >(v, v);
154  }
155 
156  } // end namespace linalg
157 } // end namespace viennacl
158 #endif
159 
160 
161 
162 
163