ViennaCL - The Vienna Computing Library
1.5.2
|
00001 #ifndef VIENNACL_LINALG_NORM_1_HPP_ 00002 #define VIENNACL_LINALG_NORM_1_HPP_ 00003 00004 /* ========================================================================= 00005 Copyright (c) 2010-2014, Institute for Microelectronics, 00006 Institute for Analysis and Scientific Computing, 00007 TU Wien. 00008 Portions of this software are copyright by UChicago Argonne, LLC. 00009 00010 ----------------- 00011 ViennaCL - The Vienna Computing Library 00012 ----------------- 00013 00014 Project Head: Karl Rupp rupp@iue.tuwien.ac.at 00015 00016 (A list of authors and contributors can be found in the PDF manual) 00017 00018 License: MIT (X11), see file LICENSE in the base directory 00019 ============================================================================= */ 00020 00025 #include <cmath> 00026 #include "viennacl/forwards.h" 00027 #include "viennacl/tools/tools.hpp" 00028 #include "viennacl/meta/enable_if.hpp" 00029 #include "viennacl/meta/tag_of.hpp" 00030 00031 namespace viennacl 00032 { 00033 // 00034 // generic norm_1 function 00035 // uses tag dispatch to identify which algorithm 00036 // should be called 00037 // 00038 namespace linalg 00039 { 00040 00041 #ifdef VIENNACL_WITH_UBLAS 00042 // ---------------------------------------------------- 00043 // UBLAS 00044 // 00045 template< typename VectorT > 00046 typename viennacl::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< VectorT >::type >::value, 00047 typename VectorT::value_type 00048 >::type 00049 norm_1(VectorT const& vector) 00050 { 00051 // std::cout << "ublas .. " << std::endl; 00052 return boost::numeric::ublas::norm_1(vector); 00053 } 00054 #endif 00055 00056 00057 // ---------------------------------------------------- 00058 // STL 00059 // 00060 template< typename T, typename A > 00061 T norm_1(std::vector<T, A> const & v1) 00062 { 00063 //std::cout << "stl .. " << std::endl; 00064 T result = 0; 00065 for (typename std::vector<T, A>::size_type i=0; i<v1.size(); ++i) 00066 result += std::fabs(v1[i]); 00067 00068 return result; 00069 } 00070 00071 // ---------------------------------------------------- 00072 // VIENNACL 00073 // 00074 template< typename ScalarType> 00075 viennacl::scalar_expression< const viennacl::vector_base<ScalarType>, 00076 const viennacl::vector_base<ScalarType>, 00077 viennacl::op_norm_1 > 00078 norm_1(viennacl::vector_base<ScalarType> const & vector) 00079 { 00080 return viennacl::scalar_expression< const viennacl::vector_base<ScalarType>, 00081 const viennacl::vector_base<ScalarType>, 00082 viennacl::op_norm_1 >(vector, vector); 00083 } 00084 00085 // with vector expression: 00086 template <typename LHS, typename RHS, typename OP> 00087 viennacl::scalar_expression<const viennacl::vector_expression<const LHS, const RHS, OP>, 00088 const viennacl::vector_expression<const LHS, const RHS, OP>, 00089 viennacl::op_norm_1> 00090 norm_1(viennacl::vector_expression<const LHS, const RHS, OP> const & vector) 00091 { 00092 return viennacl::scalar_expression< const viennacl::vector_expression<const LHS, const RHS, OP>, 00093 const viennacl::vector_expression<const LHS, const RHS, OP>, 00094 viennacl::op_norm_1 >(vector, vector); 00095 } 00096 00097 // with matrix 00098 /*template<typename NumericT, typename F> 00099 scalar_expression< const matrix_base<NumericT, F>, const matrix_base<NumericT, F>, op_norm_1> 00100 norm_1(const matrix<NumericT, F> & A) 00101 { 00102 return scalar_expression< const matrix_base<NumericT, F>, const matrix_base<NumericT, F>, op_norm_1>(A, A); 00103 }*/ 00104 00105 } // end namespace linalg 00106 } // end namespace viennacl 00107 #endif 00108 00109 00110 00111 00112