Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef _VIENNACL_NORM_2_HPP_
00016 #define _VIENNACL_NORM_2_HPP_
00017
00022 #include <math.h>
00023 #include "viennacl/forwards.h"
00024 #include "tag_of.hpp"
00025
00026 namespace viennacl
00027 {
00028
00029
00030
00031
00032
00033 namespace linalg
00034 {
00035 #ifdef VIENNACL_HAVE_MTL4
00036
00037
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
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
00055 return mtl::two_norm(v);
00056 }
00057 #endif
00058 #endif
00059
00060
00061 #ifdef VIENNACL_HAVE_EIGEN
00062
00063
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
00069 return v.norm();
00070 }
00071
00072 double norm_2(Eigen::VectorXd const & v)
00073 {
00074
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
00086 return v.norm();
00087 }
00088 #endif
00089 #endif
00090
00091
00092 #ifdef VIENNACL_HAVE_UBLAS
00093
00094
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
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
00112 return boost::numeric::ublas::norm_2(v);
00113 }
00114 #endif
00115 #endif
00116
00117
00118
00119
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
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
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
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 }
00153 }
00154 #endif
00155
00156
00157
00158
00159