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

/data/development/ViennaCL/ViennaCL-1.1.2/viennacl/linalg/norm_2.hpp

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

Generated on Sat May 21 2011 20:36:50 for ViennaCL - The Vienna Computing Library by  doxygen 1.7.1