00001 #ifndef FUNCTIONS_H
00002 #define FUNCTIONS_H
00003
00004 #include <boost/array.hpp>
00005 #include <cmath>
00006 #include <complex>
00007 #include <functional>
00008 #include "mimasexception.h"
00009
00010 namespace mimas {
00011
00012 #ifndef sgn
00013 #define sgn(x) ((x<0)?-1:((x>0)?1:0))
00014 #endif
00015
00017 template< typename T >
00018 struct _abs: public std::unary_function< T, T >
00019 {
00023 T operator()( const T &x ) const { return std::abs( x ); }
00024 };
00025
00029 template< typename T >
00030 struct _fastsqr: public std::unary_function< T, int >
00031 {
00033 _fastsqr(void) {
00034 for ( int i=0; i<(signed)table.size(); i++ )
00035 table[i] = i * i;
00036 }
00038 int operator()( const T &x ) const {
00039 assert( x > -(signed)table.size() && x < (signed)table.size() );
00040 return table[ x < 0 ? -x : x ];
00041 }
00043 boost::array< int, 512 > table;
00044 };
00045
00047 template< typename T >
00048 struct _sqr: public std::unary_function< T, T >
00049 {
00050 T operator()( const T &x ) const { return x * x; }
00051 };
00052
00054 template<typename T>
00055 struct _sqrt: public std::unary_function< T, T >
00056 {
00060 T operator()( const T &x ) const { return (T)std::sqrt( (float)x ); }
00061 };
00062
00064 template< typename T >
00065 struct _threshold: public std::binary_function< T, T, T >
00066 {
00072 T operator()( const T &x, const T &y ) const { return x < y ? T() : x; }
00073 };
00074
00076 template< typename T >
00077 struct _tobinary: public std::binary_function< T, T, bool >
00078 {
00084 bool operator()( const T &x, const T &y ) const { return x >= y; }
00085 };
00086
00087
00089 template< typename T >
00090 struct _bilevel: public std::binary_function< T, T, T > {
00091 _bilevel( T _val1, T _val2 ): val1(_val1), val2(_val2) {}
00096 T operator()( const T &x, const T &y ) const { return x >= y ? val2 : val1; }
00097 T val1;
00098 T val2;
00099 };
00100
00102 template< typename T >
00103 struct _bilevel_double: public std::unary_function< T, T> {
00104 T val1, val2, min, max;
00105 _bilevel_double( T _val1, T _val2, T _min, T _max ): val1(_val1), val2(_val2), min(_min), max(_max){}
00110 T operator()( const T &x ) const { return (x >= min && x <= max) ? val2 : val1;
00111 }
00112 };
00113
00115 template< typename T >
00116 struct _normalise: public std::unary_function< T, T >
00117 {
00119 _normalise( T _minval, T _maxval, T _val1, T _val2 ) {
00120 if ( _maxval > _minval ) {
00121 factor = (double)( _val2 - _val1 ) / ( _maxval - _minval );
00122 offset = _val1 - _minval * factor;
00123 } else {
00124 factor = 0.0;
00125 offset = _val1;
00126 };
00127 }
00128 T operator()( const T &x ) const {
00129
00130
00131 return (T)( x * factor + offset );
00132 }
00133 double factor;
00134 double offset;
00135 };
00136
00138 template< typename T1, typename T2 >
00139 struct _norm: public std::unary_function< T2, T1 >
00140 {
00141 T1 operator()( const T2 &x ) const { return std::norm( x ); }
00142 };
00143
00145 template< typename T1, typename T2 >
00146 struct _arg: public std::unary_function< T2, T1 >
00147 {
00148 T1 operator()( const T2 &x ) const { return std::arg( x ); }
00149 };
00150
00152 template< typename T >
00153 struct _conj: public std::unary_function< T, T >
00154 {
00155 T operator()( const T &x ) const { return std::conj( x ); }
00156 };
00157
00159 template< typename T >
00160 struct _log: public std::unary_function< T, T >
00161 {
00162 T operator()( const T &x ) const { return log( x ); }
00163 };
00164
00169 template< typename T >
00170 struct _sumsquares: public std::binary_function< T, T, T > {
00175 T operator()( const T &x, const T &y ) const { return x * x + y * y; }
00176 };
00177
00179 template< typename T >
00180 struct _orientation: public std::binary_function< T, T, T > {
00185 T operator()( const T &y, const T &x ) const { return atan2( y, x ); }
00186 };
00187
00188 };
00189
00190 #endif