Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
numeric.functions.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/numeric.functions.hh
11 /// @brief Numeric functions
12 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
13 
14 
15 #ifndef INCLUDED_numeric_numeric_functions_hh
16 #define INCLUDED_numeric_numeric_functions_hh
17 
18 
19 #include <numeric/types.hh>
20 
21 // Platform headers
22 #include <platform/types.hh>
23 
24 // C++ headers
25 #include <algorithm>
26 #include <utility/assert.hh>
27 #include <cmath>
28 #include <limits>
29 
30 
31 namespace numeric {
32 
33 
34 // min functions: Specializations for built-in types pass by value for speed
35 
36 #if (defined min) && (defined WIN32) // Workaround for MSVC and windows.h include which used #define min
37 #undef min
38 #endif
39 
40 #if (defined max) && (defined WIN32) // Workaround for MSVC and windows.h include which used #define max
41 #undef max
42 #endif
43 
44 /// @brief min( short int, short int )
45 inline
46 short int
47 min( short int const a, short int const b )
48 {
49  return ( a < b ? a : b );
50 }
51 
52 
53 /// @brief min( int, int )
54 inline
55 int
56 min( int const a, int const b )
57 {
58  return ( a < b ? a : b );
59 }
60 
61 
62 /// @brief min( long int, long int )
63 inline
64 long int
65 min( long int const a, long int const b )
66 {
67  return ( a < b ? a : b );
68 }
69 
70 
71 /// @brief min( unsigned short int, unsigned short int )
72 inline
73 unsigned short int
74 min( unsigned short int const a, unsigned short int const b )
75 {
76  return ( a < b ? a : b );
77 }
78 
79 
80 /// @brief min( unsigned int, unsigned int )
81 inline
82 unsigned int
83 min( unsigned int const a, unsigned int const b )
84 {
85  return ( a < b ? a : b );
86 }
87 
88 
89 /// @brief min( unsigned long int, unsigned long int )
90 inline
91 unsigned long int
92 min( unsigned long int const a, unsigned long int const b )
93 {
94  return ( a < b ? a : b );
95 }
96 
97 
98 /// @brief min( float, float )
99 inline
100 float
101 min( float const a, float const b )
102 {
103  return ( a < b ? a : b );
104 }
105 
106 
107 /// @brief min( double, double )
108 inline
109 double
110 min( double const a, double const b )
111 {
112  return ( a < b ? a : b );
113 }
114 
115 
116 /// @brief min( long double, long double )
117 inline
118 long double
119 min( long double const a, long double const b )
120 {
121  return ( a < b ? a : b );
122 }
123 
124 
125 /// @brief Use std::min for 2 arguments
126 using std::min;
127 
128 
129 /// @brief min( a, b, c )
130 template< typename T >
131 inline
132 T const &
133 min( T const & a, T const & b, T const & c )
134 {
135  return ( a < b ? ( a < c ? a : c ) : ( b < c ? b : c ) );
136 }
137 
138 
139 /// @brief min( a, b, c, d )
140 template< typename T >
141 inline
142 T const &
143 min( T const & a, T const & b, T const & c, T const & d )
144 {
145  return std::min( std::min( a, b ), std::min( c, d ) );
146 }
147 
148 
149 /// @brief min( a, b, c, d, e )
150 template< typename T >
151 inline
152 T const &
153 min( T const & a, T const & b, T const & c, T const & d, T const & e )
154 {
155  return min( std::min( a, b ), std::min( c, d ), e );
156 }
157 
158 
159 /// @brief min( a, b, c, d, e, f )
160 template< typename T >
161 inline
162 T const &
163 min( T const & a, T const & b, T const & c, T const & d, T const & e, T const & f )
164 {
165  return min( std::min( a, b ), std::min( c, d ), std::min( e, f ) );
166 }
167 
168 
169 // max functions: Specializations for built-in types pass by value for speed
170 
171 
172 /// @brief max( short int, short int )
173 inline
174 short int
175 max( short int const a, short int const b )
176 {
177  return ( a < b ? b : a );
178 }
179 
180 
181 /// @brief max( int, int )
182 inline
183 int
184 max( int const a, int const b )
185 {
186  return ( a < b ? b : a );
187 }
188 
189 
190 /// @brief max( long int, long int )
191 inline
192 long int
193 max( long int const a, long int const b )
194 {
195  return ( a < b ? b : a );
196 }
197 
198 
199 /// @brief max( unsigned short int, unsigned short int )
200 inline
201 unsigned short int
202 max( unsigned short int const a, unsigned short int const b )
203 {
204  return ( a < b ? b : a );
205 }
206 
207 
208 /// @brief max( unsigned int, unsigned int )
209 inline
210 unsigned int
211 max( unsigned int const a, unsigned int const b )
212 {
213  return ( a < b ? b : a );
214 }
215 
216 
217 /// @brief max( unsigned long int, unsigned long int )
218 inline
219 unsigned long int
220 max( unsigned long int const a, unsigned long int const b )
221 {
222  return ( a < b ? b : a );
223 }
224 
225 
226 /// @brief max( float, float )
227 inline
228 float
229 max( float const a, float const b )
230 {
231  return ( a < b ? b : a );
232 }
233 
234 
235 /// @brief max( double, double )
236 inline
237 double
238 max( double const a, double const b )
239 {
240  return ( a < b ? b : a );
241 }
242 
243 
244 /// @brief max( long double, long double )
245 inline
246 long double
247 max( long double const a, long double const b )
248 {
249  return ( a < b ? b : a );
250 }
251 
252 
253 /// @brief Use std::max for 2 arguments
254 using std::max;
255 
256 
257 /// @brief max( a, b, c )
258 template< typename T >
259 inline
260 T const &
261 max( T const & a, T const & b, T const & c )
262 {
263  return ( a < b ? ( b < c ? c : b ) : ( a < c ? c : a ) );
264 }
265 
266 
267 /// @brief max( a, b, c, d )
268 template< typename T >
269 inline
270 T const &
271 max( T const & a, T const & b, T const & c, T const & d )
272 {
273  return std::max( std::max( a, b ), std::max( c, d ) );
274 }
275 
276 
277 /// @brief max( a, b, c, d, e )
278 template< typename T >
279 inline
280 T const &
281 max( T const & a, T const & b, T const & c, T const & d, T const & e )
282 {
283  return max( std::max( a, b ), std::max( c, d ), e );
284 }
285 
286 
287 /// @brief max( a, b, c, d, e, f )
288 template< typename T >
289 inline
290 T const &
291 max( T const & a, T const & b, T const & c, T const & d, T const & e, T const & f )
292 {
293  return max( std::max( a, b ), std::max( c, d ), std::max( e, f ) );
294 }
295 
296 
297 // General numeric functions
298 
299 
300 /// @brief square( x ) == x^2
301 template< typename T >
302 inline
303 T
304 square( T const & x )
305 {
306  return x * x;
307 }
308 
309 
310 /// @brief cube( x ) == x^3
311 template< typename T >
312 inline
313 T
314 cube( T const & x )
315 {
316  return x * x * x;
317 }
318 
319 
320 /// @brief sign( x )
321 template< typename T >
322 inline
323 int
324 sign( T const & x )
325 {
326  return ( x >= T( 0 ) ? +1 : -1 );
327 }
328 
329 
330 /// @brief Sign transfered value
331 template< typename S, typename T >
332 inline
333 T
334 sign_transfered( S const & sigma, T const & x )
335 {
336  return ( sigma >= S( 0 ) ? std::abs( x ) : -std::abs( x ) );
337 }
338 
339 
340 /// @brief Absolute difference
341 template< typename T >
342 inline
343 T
344 abs_difference( T const & a, T const & b )
345 {
346  return max( a, b ) - min( a, b ); // Assure positive result even for unsigned types
347 }
348 
349 
350 /// @brief Nearest function selector class for R non-integer or T integer
351 template< typename R, typename T, bool >
353 {
354  inline
355  static
356  R
357  nearest( T const & x )
358  {
359  return R( x );
360  }
361 };
362 
363 
364 /// @brief Nearest function selector class for R integer and T non-integer
365 template< typename R, typename T >
366 struct NearestSelector< R, T, true >
367 {
368  inline
369  static
370  R
371  nearest( T const & x )
372  {
373  return R( x + ( sign( x ) * T( 0.5 ) ) );
374  }
375 };
376 
377 
378 /// @brief nearest< R >( x ): Nearest R
379 template< typename R, typename T >
380 inline
381 R
382 nearest( T const & x )
383 {
384  return NearestSelector< R, T, ( ( std::numeric_limits< R >::is_integer ) && ( ! std::numeric_limits< T >::is_integer ) ) >::nearest( x );
385 }
386 
387 
388 /// @brief nearest_size( x ): Nearest std::size_t
389 template< typename T >
390 inline
391 std::size_t
392 nearest_size( T const & x )
393 {
394  return std::size_t( x > T( 0 ) ? x + ( sign( x ) * T( 0.5 ) ) : 0 );
395 }
396 
397 
398 /// @brief nearest_ssize( x ): Nearest SSize
399 template< typename T >
400 inline
401 SSize
402 nearest_ssize( T const & x )
403 {
404  return SSize( x + ( sign( x ) * T( 0.5 ) ) );
405 }
406 
407 
408 /// @brief nearest_int( x ): Nearest int
409 template< typename T >
410 inline
411 int
412 nearest_int( T const & x )
413 {
414  return static_cast< int >( x + ( sign( x ) * T( 0.5 ) ) );
415 }
416 
417 
418 /// @brief nint( x ): Nearest int
419 template< typename T >
420 inline
421 int
422 nint( T const & x )
423 {
424  return static_cast< int >( x + ( sign( x ) * T( 0.5 ) ) );
425 }
426 
427 
428 /// @brief Mod function selector class for non-integer types
429 template< typename T, bool >
431 {
432  inline
433  static
434  T
435  mod( T const & x, T const & y )
436  {
437  return ( y != T( 0 ) ? x - ( T( static_cast< SSize >( x / y ) ) * y ) : T( 0 ) );
438  }
439 };
440 
441 
442 /// @brief Mod function selector class for integer types
443 /// @note When used with negative integer arguments this assumes integer division
444 /// rounds towards zero (de facto and future official standard)
445 template< typename T >
446 struct ModSelector< T, true >
447 {
448  inline
449  static
450  T
451  mod( T const & x, T const & y )
452  {
453  return ( y != T( 0 ) ? x - ( ( x / y ) * y ) : T( 0 ) );
454  }
455 };
456 
457 
458 /// @brief x(mod y) computational modulo returning magnitude < | y | and sign of x
459 /// @note When used with negative integer arguments this assumes integer division
460 /// rounds towards zero (de facto and future official standard)
461 template< typename T >
462 inline
463 T
464 mod( T const & x, T const & y )
465 {
467 }
468 
469 
470 /// @brief Modulo function selector class for non-integer types
471 template< typename T, bool >
473 {
474  inline
475  static
476  T
477  modulo( T const & x, T const & y )
478  {
479  return ( y != T( 0 ) ? x - ( std::floor( x / y ) * y ) : T( 0 ) );
480  }
481 };
482 
483 
484 /// @brief Modulo function selector class for integer types
485 template< typename T >
486 struct ModuloSelector< T, true >
487 {
488  inline
489  static
490  T
491  modulo( T const & x, T const & y )
492  {
493  return ( y != T( 0 ) ? x - ( T( std::floor( static_cast< long double >( x ) / y ) ) * y ) : T( 0 ) );
494  }
495 };
496 
497 
498 /// @brief x(mod y) mathematical modulo returning magnitude < | y | and sign of y
499 template< typename T >
500 inline
501 T
502 modulo( T const & x, T const & y )
503 {
505 }
506 
507 
508 /// @brief Remainder function selector class for non-integer types
509 template< typename T, bool >
511 {
512  inline
513  static
514  T
515  remainder( T const & x, T const & y )
516  {
517  if ( y == T( 0 ) ) { // Degenerate y == 0 case
518  return T( 0 );
519  } else { // Normal y != 0 case
520  T const x_over_y( x / y );
521  SSize n( nearest_ssize( x_over_y ) );
522  if ( mod( n, SSize( 2 ) ) == 1 ) { // Odd: Check for ( n - ( x / y ) ) == .5
523  T const n_minus_x_over_y( T( n ) - x_over_y );
524  if ( n_minus_x_over_y == T( 0.5 ) ) {
525  --n;
526  } else if ( n_minus_x_over_y == T( -0.5 ) ) { // Never happens if .5 rounds up
527  ++n;
528  }
529  }
530  return ( x - ( n * y ) );
531  }
532  }
533 };
534 
535 
536 /// @brief Remainder function selector class for integer types
537 template< typename T >
538 struct RemainderSelector< T, true >
539 {
540  inline
541  static
542  T
543  remainder( T const & x, T const & y )
544  {
545  if ( y == T( 0 ) ) { // Degenerate y == 0 case
546  return T( 0 );
547  } else { // Normal y != 0 case
548  long double const x_over_y( static_cast< long double >( x ) / y );
549  SSize n( nearest_ssize( x_over_y ) );
550  if ( mod( n, SSize( 2 ) ) == 1 ) { // Odd: Check for ( n - ( x / y ) ) == .5
551  long double const n_minus_x_over_y( static_cast< long double >( n ) - x_over_y );
552  if ( n_minus_x_over_y == 0.5l ) {
553  --n;
554  } else if ( n_minus_x_over_y == -0.5l ) { // Never happens if .5 rounds up
555  ++n;
556  }
557  }
558  return ( x - ( n * y ) );
559  }
560  }
561 };
562 
563 
564 /// @brief Remainder of x with respect to division by y that is of smallest magnitude
565 /// @note Emulates the C99 remainder function but also supports integer arguments
566 /// @note Returns zero if y is zero
567 /// @note Return value has magnitude <= | y / 2 |
568 /// @note If | n - ( x / y ) | == .5 the nearest even n is used
569 template< typename T >
570 inline
571 T
572 remainder( T const & x, T const & y )
573 {
575 }
576 
577 
578 /// @brief Fast remainder function selector class for non-integer types
579 template< typename T, bool >
581 {
582  inline
583  static
584  T
585  fast_remainder( T const & x, T const & y )
586  {
587  return ( y != T( 0 ) ? x - ( nearest_ssize( x / y ) * y ) : T( 0 ) );
588  }
589 };
590 
591 
592 /// @brief Fast remainder function selector class for integer types
593 template< typename T >
594 struct FastRemainderSelector< T, true >
595 {
596  inline
597  static
598  T
599  fast_remainder( T const & x, T const & y )
600  {
601  return ( y != T( 0 ) ? x - ( nearest_ssize( static_cast< long double >( x ) / y ) * y ) : T( 0 ) );
602  }
603 };
604 
605 
606 /// @brief Remainder of x with respect to division by y that is of smallest magnitude
607 /// @note Emulates the C99 remainder function except for rounding halfway values to even multiples
608 /// @note Returns zero if y is zero
609 /// @note Return value has magnitude <= | y / 2 |
610 template< typename T >
611 inline
612 T
613 fast_remainder( T const & x, T const & y )
614 {
616 }
617 
618 
619 /// @brief Remainder and result of conversion to a different type
620 template< typename T, typename S >
621 inline
622 T
623 remainder_conversion( T const & t, S & s )
624 {
625  s = S( t );
626  return t - s;
627 }
628 
629 
630 /// @brief Greatest common divisor
631 template< typename T >
632 inline
633 T
634 gcd( T const & m, T const & n )
635 {
636  T lo = min( m, n );
637  T hi = max( m, n );
638  while ( lo > T( 0 ) ) {
639  T const rem = mod( hi, lo );
640  hi = lo;
641  lo = rem;
642  }
643  return hi;
644 }
645 
646 
647 /// @brief Equal within specified relative and absolute tolerances?
648 template< typename T >
649 inline
650 bool
651 eq_tol( T const & x, T const & y, T const & r_tol, T const & a_tol )
652 {
653  using std::abs; // Can use std::abs or user-defined abs
654  assert( r_tol >= T( 0 ) );
655  assert( a_tol >= T( 0 ) );
656  return ( std::abs( x - y ) <= min( r_tol * max( std::abs( x ), std::abs( y ) ), a_tol ) );
657 }
658 
659 
660 /// @brief Less than within specified relative and absolute tolerances?
661 template< typename T >
662 inline
663 bool
664 lt_tol( T const & x, T const & y, T const & r_tol, T const & a_tol )
665 {
666  using std::abs; // Can use std::abs or user-defined abs
667  assert( r_tol >= T( 0 ) );
668  assert( a_tol >= T( 0 ) );
669  return ( x < y + min( r_tol * max( std::abs( x ), std::abs( y ) ), a_tol ) );
670 }
671 
672 
673 /// @brief Less than or equal within specified relative and absolute tolerances?
674 template< typename T >
675 inline
676 bool
677 le_tol( T const & x, T const & y, T const & r_tol, T const & a_tol )
678 {
679  using std::abs; // Can use std::abs or user-defined abs
680  assert( r_tol >= T( 0 ) );
681  assert( a_tol >= T( 0 ) );
682  return ( x <= y + min( r_tol * max( std::abs( x ), std::abs( y ) ), a_tol ) );
683 }
684 
685 
686 /// @brief Greater than or equal within specified relative and absolute tolerances?
687 template< typename T >
688 inline
689 bool
690 ge_tol( T const & x, T const & y, T const & r_tol, T const & a_tol )
691 {
692  using std::abs; // Can use std::abs or user-defined abs
693  assert( r_tol >= T( 0 ) );
694  assert( a_tol >= T( 0 ) );
695  return ( x >= y - min( r_tol * max( std::abs( x ), std::abs( y ) ), a_tol ) );
696 }
697 
698 
699 /// @brief Greater than within specified relative and absolute tolerances?
700 template< typename T >
701 inline
702 bool
703 gt_tol( T const & x, T const & y, T const & r_tol, T const & a_tol )
704 {
705  using std::abs; // Can use std::abs or user-defined abs
706  assert( r_tol >= T( 0 ) );
707  assert( a_tol >= T( 0 ) );
708  return ( x > y - min( r_tol * max( std::abs( x ), std::abs( y ) ), a_tol ) );
709 }
710 
711 
712 // You must supply 1.0 and 0.0 as arguments a and b.
713 // and no you can't short cut this, because the compiler will optimize it away!
714 inline
715 bool
716 is_a_finitenumber( double s, double a, double b ){
717  if ( (a*s) != (s*cos(b)) ) return false; // NAN!
718  if ( s * 100.0 == s * 1000.00 ) return false; // INF!
719  return true;
720 }
721 
722 
723 } // namespace numeric
724 
725 
726 #endif // INCLUDED_numeric_numeric_functions_HH
static T min(T x, T y)
Definition: Svm.cc:16
int nearest_int(T const &x)
nearest_int( x ): Nearest int
Modulo function selector class for non-integer types.
bool eq_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Equal within specified relative and absolute tolerances?
bool lt_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Less than within specified relative and absolute tolerances?
T remainder(T const &x, T const &y)
Remainder of x with respect to division by y that is of smallest magnitude.
def x
std::size_t nearest_size(T const &x)
nearest_size( x ): Nearest std::size_t
T mod(T const &x, T const &y)
x(mod y) computational modulo returning magnitude < | y | and sign of x
static T fast_remainder(T const &x, T const &y)
static T remainder(T const &x, T const &y)
T fast_remainder(T const &x, T const &y)
Remainder of x with respect to division by y that is of smallest magnitude.
T modulo(T const &x, T const &y)
x(mod y) mathematical modulo returning magnitude < | y | and sign of y
T gcd(T const &m, T const &n)
Greatest common divisor.
short int min(short int const a, short int const b)
min( short int, short int )
T abs_difference(T const &a, T const &b)
Absolute difference.
short int max(short int const a, short int const b)
max( short int, short int )
T cube(T const &x)
cube( x ) == x^3
static R nearest(T const &x)
R nearest(T const &x)
nearest< R >( x ): Nearest R
static T mod(T const &x, T const &y)
T square(T const &x)
square( x ) == x^2
T abs(T const &x)
std::abs( x ) == | x |
Definition: Fmath.hh:295
Mod function selector class for non-integer types.
T remainder_conversion(T const &t, S &s)
Remainder and result of conversion to a different type.
bool is_a_finitenumber(double s, double a, double b)
static T remainder(T const &x, T const &y)
bool ge_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Greater than or equal within specified relative and absolute tolerances?
rosetta project type declarations. Should be kept updated with core/types.hh. This exists because num...
SSize nearest_ssize(T const &x)
nearest_ssize( x ): Nearest SSize
Nearest function selector class for R non-integer or T integer.
platform::SSize SSize
Definition: types.hh:43
static T modulo(T const &x, T const &y)
bool gt_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Greater than within specified relative and absolute tolerances?
static T fast_remainder(T const &x, T const &y)
static T mod(T const &x, T const &y)
Remainder function selector class for non-integer types.
int nint(T const &x)
nint( x ): Nearest int
static T max(T x, T y)
Definition: Svm.cc:19
T sign_transfered(S const &sigma, T const &x)
Sign transfered value.
bool le_tol(T const &x, T const &y, T const &r_tol, T const &a_tol)
Less than or equal within specified relative and absolute tolerances?
int sign(T const &x)
sign( x )
static T modulo(T const &x, T const &y)
Fast remainder function selector class for non-integer types.
def y