• Main Page
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

/data/development/ViennaCL/dev/viennacl/linalg/norm_2.hpp

Go to the documentation of this file.
00001 #ifndef VIENNACL_LINALG_NORM_2_HPP_
00002 #define VIENNACL_LINALG_NORM_2_HPP_
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2011, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008 
00009                             -----------------
00010                   ViennaCL - The Vienna Computing Library
00011                             -----------------
00012 
00013    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00014                
00015    (A list of authors and contributors can be found in the PDF manual)
00016 
00017    License:         MIT (X11), see file LICENSE in the base directory
00018 ============================================================================= */
00019 
00024 #include <math.h>    //for sqrt()
00025 #include "viennacl/forwards.h"
00026 #include "viennacl/tools/tools.hpp"
00027 #include "viennacl/meta/enable_if.hpp"
00028 #include "viennacl/meta/tag_of.hpp"
00029 
00030 namespace viennacl
00031 {
00032   //
00033   // generic norm_2 function
00034   //   uses tag dispatch to identify which algorithm
00035   //   should be called 
00036   //
00037   namespace linalg 
00038   {
00039     #ifdef VIENNACL_HAVE_MTL4
00040     // ----------------------------------------------------
00041     // MTL4
00042     //
00043       #if defined(_MSC_VER) && _MSC_VER < 1500        //Visual Studio 2005 needs special treatment
00044       template <typename ScalarType>
00045       ScalarType norm_2(mtl::dense_vector<ScalarType> const & v)
00046       {
00047         // std::cout << "mtl4 .. " << std::endl;
00048         return mtl::two_norm(v);
00049       }
00050       
00051       #else
00052       template< typename VectorT >
00053       typename VectorT::value_type
00054       norm_2(VectorT const& v, 
00055           typename viennacl::enable_if< viennacl::is_mtl4< typename viennacl::traits::tag_of< VectorT >::type >::value
00056                                       >::type* dummy = 0)
00057       {
00058         // std::cout << "mtl4 .. " << std::endl;
00059         return mtl::two_norm(v);
00060       }
00061       #endif
00062     #endif
00063     
00064     
00065     #ifdef VIENNACL_HAVE_EIGEN
00066     // ----------------------------------------------------
00067     // EIGEN
00068     //
00069       #if defined(_MSC_VER) && _MSC_VER < 1500        //Visual Studio 2005 needs special treatment
00070       float norm_2(Eigen::VectorXf const & v)
00071       {
00072         // std::cout << "eigen .. " << std::endl;
00073         return v.norm();
00074       }
00075       
00076       double norm_2(Eigen::VectorXd const & v)
00077       {
00078         // std::cout << "eigen .. " << std::endl;
00079         return v.norm();
00080       }
00081       
00082       #else
00083       template< typename VectorT >
00084       typename VectorT::RealScalar
00085       norm_2(VectorT const& v, 
00086           typename viennacl::enable_if< viennacl::is_eigen< typename viennacl::traits::tag_of< VectorT >::type >::value
00087                                       >::type* dummy = 0)
00088       {
00089         // std::cout << "ublas .. " << std::endl;
00090         return v.norm();
00091       }
00092       #endif
00093     #endif
00094     
00095     
00096     #ifdef VIENNACL_HAVE_UBLAS
00097     // ----------------------------------------------------
00098     // UBLAS
00099     //
00100       #if defined(_MSC_VER) && _MSC_VER < 1500        //Visual Studio 2005 needs special treatment
00101       template< typename ScalarType >
00102       ScalarType
00103       norm_2(boost::numeric::ublas::vector<ScalarType> const & v)
00104       {
00105         // std::cout << "ublas .. " << std::endl;
00106         return boost::numeric::ublas::norm_2(v);
00107       }
00108       #else
00109       template< typename VectorT >
00110       typename VectorT::value_type
00111       norm_2(VectorT const& v, 
00112           typename viennacl::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< VectorT >::type >::value
00113                                       >::type* dummy = 0)
00114       {
00115         // std::cout << "ublas .. " << std::endl;
00116         return boost::numeric::ublas::norm_2(v);
00117       }
00118       #endif
00119     #endif
00120     
00121     
00122     // ----------------------------------------------------
00123     // STL
00124     //
00125     template< typename VectorT>
00126     typename VectorT::value_type
00127     norm_2(VectorT const& v1,
00128          typename viennacl::enable_if< viennacl::is_stl< typename viennacl::traits::tag_of< VectorT >::type >::value
00129                                      >::type* dummy = 0)
00130     {
00131       //std::cout << "stl .. " << std::endl;
00132       typename VectorT::value_type result = 0;
00133       for (typename VectorT::size_type i=0; i<v1.size(); ++i)
00134         result += v1[i] * v1[i];
00135       
00136       return sqrt(result);
00137     }
00138     
00139     // ----------------------------------------------------
00140     // VIENNACL
00141     //
00142     template< typename ScalarType, unsigned int alignment >
00143     viennacl::scalar_expression< const viennacl::vector<ScalarType, alignment>, 
00144                                  const viennacl::vector<ScalarType, alignment>,
00145                                  viennacl::op_norm_2 >
00146     norm_2(viennacl::vector<ScalarType, alignment> const & v, 
00147          typename viennacl::enable_if< viennacl::is_viennacl< typename viennacl::traits::tag_of< viennacl::vector<ScalarType, alignment> >::type >::value
00148                                      >::type* dummy = 0)
00149     {
00150        //std::cout << "viennacl .. " << std::endl;
00151       return viennacl::scalar_expression< const viennacl::vector<ScalarType, alignment>, 
00152                                           const viennacl::vector<ScalarType, alignment>,
00153                                           viennacl::op_norm_2 >(v, v);
00154     }
00155 
00156   } // end namespace linalg
00157 } // end namespace viennacl
00158 #endif
00159 
00160 
00161 
00162 
00163 

Generated on Fri Dec 30 2011 23:20:43 for ViennaCL - The Vienna Computing Library by  doxygen 1.7.1