All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
norm_1.hpp
Go to the documentation of this file.
#ifndef VIENNACL_LINALG_NORM_1_HPP_
#define VIENNACL_LINALG_NORM_1_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <cmath>
#include "viennacl/forwards.h"
#include "viennacl/tools/tools.hpp"
#include "viennacl/meta/enable_if.hpp"
#include "viennacl/meta/tag_of.hpp"
namespace viennacl
{
//
// generic norm_1 function
// uses tag dispatch to identify which algorithm
// should be called
//
namespace linalg
{
#ifdef VIENNACL_WITH_UBLAS
// ----------------------------------------------------
// UBLAS
//
template< typename VectorT >
typename viennacl::enable_if< viennacl::is_ublas< typename viennacl::traits::tag_of< VectorT >::type >::value,
typename VectorT::value_type
>::type
norm_1(VectorT const& vector)
{
// std::cout << "ublas .. " << std::endl;
return boost::numeric::ublas::norm_1(vector);
}
#endif
// ----------------------------------------------------
// STL
//
template< typename T, typename A >
T norm_1(std::vector<T, A> const & v1)
{
//std::cout << "stl .. " << std::endl;
T result = 0;
for (typename std::vector<T, A>::size_type i=0; i<v1.size(); ++i)
result += std::fabs(v1[i]);
return result;
}
// ----------------------------------------------------
// VIENNACL
//
template< typename ScalarType>
viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
const viennacl::vector_base<ScalarType>,
viennacl::op_norm_1 >
norm_1(viennacl::vector_base<ScalarType> const & vector)
{
return viennacl::scalar_expression< const viennacl::vector_base<ScalarType>,
const viennacl::vector_base<ScalarType>,
viennacl::op_norm_1 >(vector, vector);
}
// with vector expression:
template<typename LHS, typename RHS, typename OP>
viennacl::scalar_expression<const viennacl::vector_expression<const LHS, const RHS, OP>,
const viennacl::vector_expression<const LHS, const RHS, OP>,
viennacl::op_norm_1>
norm_1(viennacl::vector_expression<const LHS, const RHS, OP> const & vector)
{
return viennacl::scalar_expression< const viennacl::vector_expression<const LHS, const RHS, OP>,
const viennacl::vector_expression<const LHS, const RHS, OP>,
viennacl::op_norm_1 >(vector, vector);
}
} // end namespace linalg
} // end namespace viennacl
#endif
viennacl::enable_if
Simple enable-if variant that uses the SFINAE pattern.
Definition: enable_if.hpp:30
tag_of.hpp
Dispatch facility for distinguishing between ublas, STL and ViennaCL types.
tools.hpp
Various little tools used here and there in ViennaCL.
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::vector_expression
An expression template class that represents a binary operation that yields a vector.
Definition: forwards.h:239
v1
viennacl::vector< float > v1
Definition: global_variables.cpp:60
viennacl::linalg::norm_1
viennacl::scalar_expression< const viennacl::vector_expression< const LHS, const RHS, OP >, const viennacl::vector_expression< const LHS, const RHS, OP >, viennacl::op_norm_1 > norm_1(viennacl::vector_expression< const LHS, const RHS, OP > const &vector)
Definition: norm_1.hpp:90
viennacl::vector_base
Common base class for dense vectors, vector ranges, and vector slices.
Definition: vector_def.hpp:104
viennacl::vector
Definition: forwards.h:266
viennacl::op_norm_1
A tag class representing the 1-norm of a vector.
Definition: forwards.h:201
viennacl::linalg::norm_1
T norm_1(std::vector< T, A > const &v1)
Definition: norm_1.hpp:61
enable_if.hpp
Simple enable-if variant that uses the SFINAE pattern.