ViennaCL - The Vienna Computing Library  1.5.2
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-2014, 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 PDF manual)
17 
18  License: MIT (X11), see file LICENSE in the base directory
19 ============================================================================= */
20 
25 #include <cmath>
26 #include "viennacl/forwards.h"
27 #include "viennacl/tools/tools.hpp"
29 #include "viennacl/meta/tag_of.hpp"
30 
31 namespace viennacl
32 {
33  //
34  // generic norm_2 function
35  // uses tag dispatch to identify which algorithm
36  // should be called
37  //
38  namespace linalg
39  {
40  #ifdef VIENNACL_WITH_MTL4
41  // ----------------------------------------------------
42  // MTL4
43  //
44  template< typename VectorT >
46  typename VectorT::value_type>::type
47  norm_2(VectorT const & v)
48  {
49  return mtl::two_norm(v);
50  }
51  #endif
52 
53 
54  #ifdef VIENNACL_WITH_EIGEN
55  // ----------------------------------------------------
56  // EIGEN
57  //
58  template< typename VectorT >
60  typename VectorT::RealScalar>::type
61  norm_2(VectorT const & v)
62  {
63  return v.norm();
64  }
65  #endif
66 
67 
68  #ifdef VIENNACL_WITH_UBLAS
69  // ----------------------------------------------------
70  // UBLAS
71  //
72  template< typename VectorT >
74  typename VectorT::value_type>::type
75  norm_2(VectorT const & v)
76  {
78  }
79  #endif
80 
81 
82  // ----------------------------------------------------
83  // STL
84  //
85  template< typename T, typename A >
86  T norm_2(std::vector<T, A> const & v1)
87  {
88  T result = 0;
89  for (typename std::vector<T, A>::size_type i=0; i<v1.size(); ++i)
90  result += v1[i] * v1[i];
91 
92  return std::sqrt(result);
93  }
94 
95  // ----------------------------------------------------
96  // VIENNACL
97  //
98  template< typename ScalarType>
102  norm_2(viennacl::vector_base<ScalarType> const & v)
103  {
104  //std::cout << "viennacl .. " << std::endl;
106  const viennacl::vector_base<ScalarType>,
107  viennacl::op_norm_2 >(v, v);
108  }
109 
110  // with vector expression:
111  template <typename LHS, typename RHS, typename OP>
115  norm_2(viennacl::vector_expression<const LHS, const RHS, OP> const & vector)
116  {
118  const viennacl::vector_expression<const LHS, const RHS, OP>,
119  viennacl::op_norm_2>(vector, vector);
120  }
121 
122 
123  } // end namespace linalg
124 } // end namespace viennacl
125 #endif
126 
127 
128 
129 
130 
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:29
Dispatch facility for distinguishing between ublas, STL and ViennaCL types.
T norm_2(std::vector< T, A > const &v1)
Definition: norm_2.hpp:86
Various little tools used here and there in ViennaCL.
This file provides the forward declarations for the main types used within ViennaCL.
Main namespace in ViennaCL. Holds all the basic types such as vector, matrix, etc. and defines operations upon them.
Definition: cpu_ram.hpp:29
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:181
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:175
viennacl::scalar_expression< const viennacl::vector_expression< const LHS, const RHS, OP >, const viennacl::vector_expression< const LHS, const RHS, OP >, viennacl::op_norm_2 > norm_2(viennacl::vector_expression< const LHS, const RHS, OP > const &vector)
Definition: norm_2.hpp:115
A vector class representing a linear memory sequence on the GPU. Inspired by boost::numeric::ublas::v...
Definition: forwards.h:208
A tag class representing the 2-norm of a vector.
Definition: forwards.h:156
Simple enable-if variant that uses the SFINAE pattern.