|
Boost : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2005-08-19 08:02:26
Should we add functions to evaluate the various norms of a (numeric)
sequence?
//=======================================================================
template < typename Number, typename InputIterator >
Number norm_1( InputIterator b, InputIterator e )
{
using std::abs;
return std::accumulate( b, e, Number(), abs(_1) + abs(_2) );
}
template < typename Number, typename InputIterator >
Number norm_2( InputIterator b, InputIterator e )
{
using std::sqrt;
return sqrt( std::inner_product( b, e, b, Number() ) );
}
template < typename Number, typename InputIterator >
Number norm_infinity( InputIterator b, InputIterator e )
{
using std::abs;
return abs( *std::max_element( b, e, abs(_1) < abs(_2) ) );
}
template < typename Number, typename InputIterator >
Number norm_x( int n, InputIterator b, InputIterator e )
{
switch ( n )
{
default:
{
using std::pow;
using std::abs;
Number r = Number();
for ( ; b != e ; ++b )
{
r += pow( abs(*b), n );
}
return root( r, n );
}
case 2:
return norm_2<Number>( b, e );
case 1:
return norm_1<Number>( b, e );
case 0:
// You can't really have a zero-norm
// And you can't pass in infinity as a parameter
// So we have 0 -> infinity
return norm_infinity<Number>( b, e );
}
}
//=======================================================================
I just wrote that here, so I don't know if it can work. I don't know if I
used the bind/lambda stuff correctly, and there's still the problem that the
"root" function doesn't exist.
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk