ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkMath.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkMath_h
29 #define itkMath_h
30 
31 #include "itkMathDetail.h"
32 #include "itkConceptChecking.h"
33 #include <vnl/vnl_math.h>
34 
35 /* Only maintain backwards compatibility with old versions
36  * of VXL back to the point where vnl_math:: was introduced
37  * versions of VXL where only vnl_math_ was available are not
38  * supported.
39  */
40 #include <vxl_version.h>
41 #if VXL_VERSION_DATE_FULL <= 20121114
42 # error "VXL version must support vnl_math:: namespace versions of functions"
43 #endif
44 
45 namespace itk
46 {
47 namespace Math
48 {
49 // These constants originate from VXL's vnl_math.h. They have been
50 // moved here to improve visibility, and to ensure that the constants
51 // are available during compile time ( as opposed to static ITK_CONSTEXPR
52 // member vaiables ).
53 
54 
56 static ITK_CONSTEXPR_VAR double e = vnl_math::e;
58 static ITK_CONSTEXPR_VAR double log2e = vnl_math::log2e;
60 static ITK_CONSTEXPR_VAR double log10e = vnl_math::log10e;
62 static ITK_CONSTEXPR_VAR double ln2 = vnl_math::ln2;
64 static ITK_CONSTEXPR_VAR double ln10 = vnl_math::ln10;
66 static ITK_CONSTEXPR_VAR double pi = vnl_math::pi;
68 static ITK_CONSTEXPR_VAR double twopi = vnl_math::twopi;
70 static ITK_CONSTEXPR_VAR double pi_over_2 = vnl_math::pi_over_2;
72 static ITK_CONSTEXPR_VAR double pi_over_4 = vnl_math::pi_over_4;
74 static ITK_CONSTEXPR_VAR double pi_over_180 = vnl_math::pi_over_180;
76 static ITK_CONSTEXPR_VAR double one_over_pi = vnl_math::one_over_pi;
78 static ITK_CONSTEXPR_VAR double two_over_pi = vnl_math::two_over_pi;
80 static ITK_CONSTEXPR_VAR double deg_per_rad = vnl_math::deg_per_rad;
82 static ITK_CONSTEXPR_VAR double sqrt2pi = vnl_math::sqrt2pi;
84 static ITK_CONSTEXPR_VAR double two_over_sqrtpi = vnl_math::two_over_sqrtpi;
86 static ITK_CONSTEXPR_VAR double one_over_sqrt2pi = vnl_math::one_over_sqrt2pi;
88 static ITK_CONSTEXPR_VAR double sqrt2 = vnl_math::sqrt2;
90 static ITK_CONSTEXPR_VAR double sqrt1_2 = vnl_math::sqrt1_2;
92 static ITK_CONSTEXPR_VAR double sqrt1_3 = vnl_math::sqrt1_3;
94 static ITK_CONSTEXPR_VAR double euler = vnl_math::euler;
95 
96 //: IEEE double machine precision
97 static ITK_CONSTEXPR_VAR double eps = vnl_math::eps;
98 static ITK_CONSTEXPR_VAR double sqrteps = vnl_math::sqrteps;
99 //: IEEE single machine precision
100 static ITK_CONSTEXPR_VAR float float_eps = vnl_math::float_eps;
101 static ITK_CONSTEXPR_VAR float float_sqrteps = vnl_math::float_sqrteps;
102 
106 #define itkTemplateFloatingToIntegerMacro(name) \
107  template< typename TReturn, typename TInput > \
108  inline TReturn name(TInput x) \
109  { \
110  \
111  if ( sizeof( TReturn ) <= 4 ) \
112  { \
113  return static_cast< TReturn >( Detail::name##_32(x) ); \
114  } \
115  else if ( sizeof( TReturn ) <= 8 ) \
116  { \
117  return static_cast< TReturn >( Detail::name##_64(x) ); \
118  } \
119  else \
120  { \
121  return static_cast< TReturn >( Detail::name##_base< TReturn, TInput >(x) ); \
122  } \
123  }
124 
125 
146 
170 
178 template< typename TReturn, typename TInput >
179 inline TReturn Round(TInput x) { return RoundHalfIntegerUp< TReturn, TInput >(x); }
180 
194 
206 
207 #undef itkTemplateFloatingToIntegerMacro
208 
209 template< typename TReturn, typename TInput >
210 inline TReturn CastWithRangeCheck(TInput x)
211 {
212 #ifdef ITK_USE_CONCEPT_CHECKING
213  itkConceptMacro( OnlyDefinedForIntegerTypes1, ( itk::Concept::IsInteger< TReturn > ) );
214  itkConceptMacro( OnlyDefinedForIntegerTypes2, ( itk::Concept::IsInteger< TInput > ) );
215 #endif // ITK_USE_CONCEPT_CHECKING
216 
217  TReturn ret = static_cast< TReturn >( x );
218  if ( sizeof( TReturn ) > sizeof( TInput )
220  {
221  // if the output type is bigger and we are not converting a signed
222  // integer to an unsigned integer then we have no problems
223  return ret;
224  }
225  else if ( sizeof( TReturn ) >= sizeof( TInput ) )
226  {
228  {
229  itk::RangeError _e(__FILE__, __LINE__);
230  throw _e;
231  }
232  }
233  else if ( static_cast< TInput >( ret ) != x
235  {
236  itk::RangeError _e(__FILE__, __LINE__);
237  throw _e;
238  }
239  return ret;
240 }
241 
249 template <typename T>
250 inline typename Detail::FloatIEEE<T>::IntType
251 FloatDifferenceULP( T x1, T x2 )
252 {
253  Detail::FloatIEEE<T> x1f(x1);
254  Detail::FloatIEEE<T> x2f(x2);
255  return x1f.AsULP() - x2f.AsULP();
256 }
257 
265 template <typename T>
266 inline T
268 {
269  Detail::FloatIEEE<T> representInput( x );
270  Detail::FloatIEEE<T> representOutput( representInput.asInt + ulps );
271  return representOutput.asFloat;
272 }
273 
304 template <typename T>
305 inline bool
306 FloatAlmostEqual( T x1, T x2,
307  typename Detail::FloatIEEE<T>::IntType maxUlps = 4,
308  typename Detail::FloatIEEE<T>::FloatType maxAbsoluteDifference = 0.1*itk::NumericTraits<T>::epsilon() )
309 {
310  // Check if the numbers are really close -- needed
311  // when comparing numbers near zero.
312  const T absDifference = std::abs(x1 - x2);
313  if ( absDifference <= maxAbsoluteDifference )
314  {
315  return true;
316  }
317 
318 #if defined(__APPLE__) && (__clang_major__ == 3) && (__clang_minor__ == 0) && defined(NDEBUG) && defined(__x86_64__)
319  Detail::FloatIEEE<T> x1f(x1);
320  Detail::FloatIEEE<T> x2f(x2);
321  double x1fAsULP = static_cast<double>(x1f.AsULP());
322  double x2fAsULP = static_cast<double>(x2f.AsULP());
323  double ulps = x1fAsULP - x2fAsULP;
324  if(ulps < 0)
325  {
326  ulps = -ulps;
327  }
328  return ulps <= static_cast<double>(maxUlps);
329 #else
331  ulps = FloatDifferenceULP(x1, x2);
332  if(ulps < 0)
333  {
334  ulps = -ulps;
335  }
336  return ulps <= maxUlps;
337 #endif
338 }
339 
340 // The following code cannot be moved to the itkMathDetail.h file without introducing circular dependencies
341 namespace Detail // The Detail namespace holds the templates used by AlmostEquals
342 {
343 // The following structs and templates are used to choose
344 // which version of the AlmostEquals function
345 // should be implemented base on input parameter types
346 
347 // Structs for choosing AlmostEquals function
348 
350 {
351  template <typename TFloatType1, typename TFloatType2>
352  static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
353  {
354  return FloatAlmostEqual<double>(x1, x2);
355  }
356 
357  template <typename TFloatType1, typename TFloatType2>
358  static bool
359  AlmostEqualsFunction(double x1, double x2)
360  {
361  return FloatAlmostEqual<double>(x1, x2);
362  }
363 
364  template <typename TFloatType1, typename TFloatType2>
365  static bool
366  AlmostEqualsFunction(double x1, float x2)
367  {
368  return FloatAlmostEqual<float>(x1, x2);
369  }
370 
371  template <typename TFloatType1, typename TFloatType2>
372  static bool
373  AlmostEqualsFunction(float x1, double x2)
374  {
375  return FloatAlmostEqual<float>(x1, x2);
376  }
377 
378  template <typename TFloatType1, typename TFloatType2>
379  static bool
380  AlmostEqualsFunction(float x1, float x2)
381  {
382  return FloatAlmostEqual<float>(x1, x2);
383  }
384 };
385 
387 {
388  template <typename TFloatType, typename TIntType>
389  static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
390  {
391  return FloatAlmostEqual<TFloatType> (floatingVariable, integerVariable);
392  }
393 };
394 
396 {
397  template <typename TIntType, typename TFloatType>
398  static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
399  {
400  return AlmostEqualsFloatVsInteger::AlmostEqualsFunction(floatingVariable, integerVariable);
401  }
402 };
403 
405 {
406  template <typename TSignedInt, typename TUnsignedInt>
407  static bool AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
408  {
409  if(signedVariable < 0) return false;
410  if( unsignedVariable > static_cast< size_t >(itk::NumericTraits<TSignedInt>::max()) ) return false;
411  return signedVariable == static_cast< TSignedInt >(unsignedVariable);
412  }
413 };
414 
416 {
417  template <typename TUnsignedInt, typename TSignedInt>
418  static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
419  {
420  return AlmostEqualsSignedVsUnsigned::AlmostEqualsFunction(signedVariable, unsignedVariable);
421  }
422 };
423 
425 {
426  template <typename TIntegerType1, typename TIntegerType2>
427  static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
428  {
429  return x1 == x2;
430  }
431 };
432 // end of structs that choose the specific AlmostEquals function
433 
434 // Selector structs, these select the correct case based on its types
435 // input1 is int? input 1 is signed? input2 is int? input 2 is signed?
436 template<bool TInput1IsIntger, bool TInput1IsSigned, bool TInput2IsInteger, bool TInput2IsSigned>
438 { // default case
440 };
441 
443 template<>
444 struct AlmostEqualsFunctionSelector < false, true, false, true>
445 // floating type v floating type
446 {
448 };
449 
450 template<>
451 struct AlmostEqualsFunctionSelector <false, true, true, true>
452 // float vs signed int
453 {
454  typedef AlmostEqualsFloatVsInteger SelectedVersion;
455 };
456 
457 template<>
458 struct AlmostEqualsFunctionSelector <false, true, true,false>
459 // float vs unsigned int
460 {
461  typedef AlmostEqualsFloatVsInteger SelectedVersion;
462 };
463 
464 template<>
465 struct AlmostEqualsFunctionSelector <true, false, false, true>
466 // unsigned int vs float
467 {
468  typedef AlmostEqualsIntegerVsFloat SelectedVersion;
469 };
470 
471 template<>
472 struct AlmostEqualsFunctionSelector <true, true, false, true>
473 // signed int vs float
474 {
475  typedef AlmostEqualsIntegerVsFloat SelectedVersion;
476 };
477 
478 template<>
479 struct AlmostEqualsFunctionSelector<true, true, true, false>
480 // signed vs unsigned
481 {
482  typedef AlmostEqualsSignedVsUnsigned SelectedVersion;
483 };
484 
485 template<>
486 struct AlmostEqualsFunctionSelector<true, false, true, true>
487 // unsigned vs signed
488 {
489  typedef AlmostEqualsUnsignedVsSigned SelectedVersion;
490 };
491 
492 template<>
493 struct AlmostEqualsFunctionSelector<true, true, true, true>
494 // signed vs signed
495 {
496  typedef AlmostEqualsPlainOldEquals SelectedVersion;
497 };
498 
499 template<>
500 struct AlmostEqualsFunctionSelector<true, false, true, false>
501 // unsigned vs unsigned
502 {
503  typedef AlmostEqualsPlainOldEquals SelectedVersion;
504 };
505 // end of AlmostEqualsFunctionSelector structs
506 
507  // The implementor tells the selector what to do
508 template<typename TInputType1, typename TInputType2>
509 struct AlmostEqualsScalarImplementer
510 {
511  static ITK_CONSTEXPR_VAR bool TInputType1IsInteger = itk::NumericTraits<TInputType1>::IsInteger;
512  static ITK_CONSTEXPR_VAR bool TInputType1IsSigned = itk::NumericTraits<TInputType1>::IsSigned;
513  static ITK_CONSTEXPR_VAR bool TInputType2IsInteger = itk::NumericTraits<TInputType2>::IsInteger;
514  static ITK_CONSTEXPR_VAR bool TInputType2IsSigned = itk::NumericTraits<TInputType2>::IsSigned;
515 
516  typedef typename AlmostEqualsFunctionSelector< TInputType1IsInteger, TInputType1IsSigned,
517  TInputType2IsInteger, TInputType2IsSigned >::SelectedVersion SelectedVersion;
518 };
519 
520 // The AlmostEqualsScalarComparer returns the result of an
521 // approximate comparison between two scalar values of
522 // potentially different data types.
523 template <typename TScalarType1, typename TScalarType2>
524 inline bool
525 AlmostEqualsScalarComparer( TScalarType1 x1, TScalarType2 x2 )
526 {
527  return AlmostEqualsScalarImplementer<TScalarType1, TScalarType2>::SelectedVersion:: template AlmostEqualsFunction<TScalarType1, TScalarType2>(x1, x2);
528 }
529 
530 // The following structs are used to evaluate approximate comparisons between
531 // complex and scalar values of potentially different types.
532 
533 // Comparisons between scalar types use the AlmostEqualsScalarComparer function.
534 struct AlmostEqualsScalarVsScalar
535 {
536  template <typename TScalarType1, typename TScalarType2>
537  static bool
538  AlmostEqualsFunction(TScalarType1 x1, TScalarType2 x2)
539  {
540  return AlmostEqualsScalarComparer(x1, x2);
541  }
542 };
543 
544 // Comparisons between two complex values compare the real and imaginary components
545 // separately with the AlmostEqualsScalarComparer function.
546 struct AlmostEqualsComplexVsComplex
547 {
548  template <typename TComplexType1, typename TComplexType2>
549  static bool
550  AlmostEqualsFunction(TComplexType1 x1, TComplexType2 x2)
551  {
552  return AlmostEqualsScalarComparer(x1.real(), x2.real()) && AlmostEqualsScalarComparer( x1.imag(), x2.imag() );
553  }
554 };
555 
556 // Comparisons between complex and scalar values first check to see if the imaginary component
557 // of the complex value is approximately 0. Then a ScalarComparison is done between the real
558 // part of the complex number and the scalar value.
559 struct AlmostEqualsScalarVsComplex
560 {
561  template <typename TScalarType, typename TComplexType>
562  static bool
563  AlmostEqualsFunction(TScalarType scalarVariable, TComplexType complexVariable)
564  {
565  if( !AlmostEqualsScalarComparer( complexVariable.imag(), itk::NumericTraits< typename itk::NumericTraits< TComplexType >::ValueType >::ZeroValue() ) )
566  {
567  return false;
568  }
569  return AlmostEqualsScalarComparer(scalarVariable, complexVariable.real());
570  }
571 };
572 
573 struct AlmostEqualsComplexVsScalar
574 {
575  template <typename TComplexType, typename TScalarType>
576  static bool
577  AlmostEqualsFunction(TComplexType complexVariable, TScalarType scalarVariable)
578  {
579  return AlmostEqualsScalarVsComplex::AlmostEqualsFunction(scalarVariable, complexVariable);
580  }
581 };
582 
583 // The AlmostEqualsComplexChooser structs choose the correct case
584 // from the input parameter types' IsComplex property
585 // The default case is scalar vs scalar
586 template < bool T1IsComplex, bool T2IsComplex > //Default is false, false
587 struct AlmostEqualsComplexChooser
588 {
589  typedef AlmostEqualsScalarVsScalar ChosenVersion;
590 };
591 
592 template <>
593 struct AlmostEqualsComplexChooser< true, true >
594 {
595  typedef AlmostEqualsComplexVsComplex ChosenVersion;
596 };
597 
598 template <>
599 struct AlmostEqualsComplexChooser< false, true >
600 {
601  typedef AlmostEqualsScalarVsComplex ChosenVersion;
602 };
603 
604 template <>
605 struct AlmostEqualsComplexChooser< true, false>
606 {
607  typedef AlmostEqualsComplexVsScalar ChosenVersion;
608 };
609 // End of AlmostEqualsComplexChooser structs.
610 
611 // The AlmostEqualsComplexImplementer determines which of the input
612 // parameters are complex and which are real, and sends that information
613 // to the AlmostEqualsComplexChooser structs to determine the proper
614 // type of evaluation.
615 template <typename T1, typename T2>
616 struct AlmostEqualsComplexImplementer
617 {
618  static ITK_CONSTEXPR_VAR bool T1IsComplex = NumericTraits< T1 >::IsComplex;
619  static ITK_CONSTEXPR_VAR bool T2IsComplex = NumericTraits< T2 >::IsComplex;
620 
621  typedef typename AlmostEqualsComplexChooser< T1IsComplex, T2IsComplex >::ChosenVersion ChosenVersion;
622 };
624 
625 } // end namespace Detail
626 
669 // The AlmostEquals function
670 template <typename T1, typename T2>
671 inline bool
672 AlmostEquals( T1 x1, T2 x2 )
673 {
674  return Detail::AlmostEqualsComplexImplementer<T1,T2>::ChosenVersion::AlmostEqualsFunction(x1, x2);
675 }
676 
677 // The NotAlmostEquals function
678 template <typename T1, typename T2>
679 inline bool
680 NotAlmostEquals( T1 x1, T2 x2 )
681 {
682  return ! AlmostEquals( x1, x2 );
683 }
684 
685 
707 // The ExactlyEquals function
708 template <typename TInput1, typename TInput2>
709 inline bool
710 ExactlyEquals( const TInput1 & x1, const TInput2 & x2 )
711 {
712 CLANG_PRAGMA_PUSH
713 CLANG_SUPPRESS_Wfloat_equal
714  return x1 == x2;
715 CLANG_PRAGMA_POP
716 }
717 
718 //The NotExactlyEquals function
719 template <typename TInput1, typename TInput2>
720 inline bool
721 NotExactlyEquals( const TInput1 & x1, const TInput2 & x2 )
722 {
723  return !ExactlyEquals(x1, x2);
724 }
725 
726 
731 ITKCommon_EXPORT bool IsPrime( unsigned short n );
732 ITKCommon_EXPORT bool IsPrime( unsigned int n );
733 ITKCommon_EXPORT bool IsPrime( unsigned long n );
734 ITKCommon_EXPORT bool IsPrime( unsigned long long n );
736 
737 
739 ITKCommon_EXPORT unsigned short GreatestPrimeFactor( unsigned short n );
740 ITKCommon_EXPORT unsigned int GreatestPrimeFactor( unsigned int n );
741 ITKCommon_EXPORT unsigned long GreatestPrimeFactor( unsigned long n );
742 ITKCommon_EXPORT unsigned long long GreatestPrimeFactor( unsigned long long n );
744 
745 
746 /*==========================================
747  * Alias the vnl_math functions in the itk::Math
748  * namespace. If possible, use the std:: equivalents
749  */
750 #if ITK_COMPILED_CXX_STANDARD_VERSION >= 201103L
751 
756 #define ITK_PERFECT_FORWARD_MACRO(new_name, old_name) \
757  template <typename... TArgs> \
758  auto new_name(TArgs&&... args) -> decltype(old_name(std::forward<TArgs>(args)...)) { \
759  return old_name(std::forward<TArgs>(args)...); \
760  }
761 
762  // Prefer to use perfect forwarding to the std library if C++11 features are available and consistent with vnl
763 ITK_PERFECT_FORWARD_MACRO(isnan,std::isnan);
764 ITK_PERFECT_FORWARD_MACRO(isinf,std::isinf);
765 ITK_PERFECT_FORWARD_MACRO(isfinite,std::isfinite);
766 ITK_PERFECT_FORWARD_MACRO(isnormal,std::isnormal);
767 ITK_PERFECT_FORWARD_MACRO(cbrt,std::cbrt);
768 ITK_PERFECT_FORWARD_MACRO(hypot,std::hypot);
769 // Pefect forwarding to vnl specializations
770 ITK_PERFECT_FORWARD_MACRO(angle_0_to_2pi,vnl_math::angle_0_to_2pi);
771 ITK_PERFECT_FORWARD_MACRO(angle_minuspi_to_pi,vnl_math::angle_minuspi_to_pi);
772 ITK_PERFECT_FORWARD_MACRO(rnd_halfinttoeven,vnl_math::rnd_halfinttoeven);
773 ITK_PERFECT_FORWARD_MACRO(rnd_halfintup,vnl_math::rnd_halfintup);
774 ITK_PERFECT_FORWARD_MACRO(rnd,vnl_math::rnd);
775 ITK_PERFECT_FORWARD_MACRO(floor,vnl_math::floor);
776 ITK_PERFECT_FORWARD_MACRO(ceil,vnl_math::ceil);
777 ITK_PERFECT_FORWARD_MACRO(sgn,vnl_math::sgn);
778 ITK_PERFECT_FORWARD_MACRO(sgn0,vnl_math::sgn0);
779 ITK_PERFECT_FORWARD_MACRO(remainder_truncated,vnl_math::remainder_truncated);
780 ITK_PERFECT_FORWARD_MACRO(remainder_floored,vnl_math::remainder_floored);
781 ITK_PERFECT_FORWARD_MACRO(abs,vnl_math::abs);
782 ITK_PERFECT_FORWARD_MACRO(sqr,vnl_math::sqr);
783 ITK_PERFECT_FORWARD_MACRO(cube,vnl_math::cube);
784 ITK_PERFECT_FORWARD_MACRO(squared_magnitude,vnl_math::squared_magnitude);
785 
786 #undef ITK_PERFECT_FORWARD_MACRO
787 
788 #else
789 template<typename T> bool isnan(const T value) { return vnl_math::isnan(value); }
790 template<typename T> bool isinf(const T value) { return vnl_math::isinf(value); }
791 template<typename T> bool isfinite(const T value) { return vnl_math::isfinite(value); }
792 template<typename T> T cbrt(const T value) { return vnl_math::cuberoot(value); }
793 template<typename T> T hypot(const T value1, const T value2) { return vnl_math::hypot(value1,value2); }
794 template<typename T> T angle_0_to_2pi(const T angle) { return vnl_math::angle_0_to_2pi(angle); }
795 template<typename T> T angle_minuspi_to_pi(const T angle) { return vnl_math::angle_minuspi_to_pi(angle); }
796 template<typename T> inline int rnd_halfinttoeven(const T x) {return vnl_math::rnd_halfinttoeven(x); }
797 template<typename T> inline int rnd_halfintup(const T x) { return vnl_math::rnd_halfintup(x); }
798 template<typename T> inline int rnd(const T x) { return vnl_math::rnd(x); }
799 template<typename T> inline int floor(const T x) { return vnl_math::floor(x); }
800 template<typename T> inline int ceil(const T x) { return vnl_math::ceil(x); }
801 template<typename T> int sgn(const T x) { return vnl_math::sgn(x); }
802 template<typename T> int sgn0(const T x) { return vnl_math::sgn0(x); }
803 template<typename T> T remainder_truncated(const T x, const T y) { return vnl_math::remainder_truncated(x,y); }
804 template<typename T> T remainder_floored(const T x, const T y) { return vnl_math::remainder_floored(x,y); }
805 
806 inline bool abs(const bool x) { return x; }
807 inline unsigned char abs(const unsigned char x) { return x; }
808 inline unsigned char abs(const signed char x) { return vnl_math::abs(x); }
809 inline unsigned char abs(const char x) { return vnl_math::abs(x); }
810 inline unsigned short abs(const short x) { return vnl_math::abs(x); }
811 inline unsigned short abs(const unsigned short x) { return x; }
812 inline unsigned int abs(const int x) { return vnl_math::abs(x); }
813 inline unsigned int abs(const unsigned int x) { return x; }
814 inline unsigned long abs(const long x) { return vnl_math::abs(x); }
815 inline unsigned long abs(const unsigned long x) { return x; }
816 #if VCL_HAS_LONG_LONG
817 inline unsigned long long abs(const long long x) { return vnl_math::abs(x); }
818 inline unsigned long long abs(const unsigned long long x) { return x; }
819 #endif
820 inline float abs(const float x) { return std::abs(x); }
821 inline double abs(const double x) { return std::abs(x); }
822 inline long double abs(const long double x) { return std::abs(x); }
823 
824 inline bool sqr(const bool x) { return vnl_math::sqr(x); }
825 inline int sqr(const int x) { return vnl_math::sqr(x); }
826 inline unsigned int sqr(const unsigned int x) { return vnl_math::sqr(x); }
827 inline long sqr(const long x) { return vnl_math::sqr(x); }
828 inline unsigned long sqr(const unsigned long x) { return vnl_math::sqr(x); }
829 #if VCL_HAS_LONG_LONG
830 inline long long sqr(const long long x) { return vnl_math::sqr(x); }
831 inline unsigned long long sqr(const unsigned long long x) { return vnl_math::sqr(x); }
832 #endif
833 inline float sqr(const float x) { return vnl_math::sqr(x); }
834 inline double sqr(const double x) { return vnl_math::sqr(x); }
835 
836 inline bool cube(const bool x) { return vnl_math::cube(x); }
837 inline int cube(const int x) { return vnl_math::cube(x); }
838 inline unsigned int cube(const unsigned int x) { return vnl_math::cube(x); }
839 inline long cube(const long x) { return vnl_math::cube(x); }
840 inline unsigned long cube(const unsigned long x) { return vnl_math::cube(x); }
841 #if VCL_HAS_LONG_LONG
842 inline long long cube(const long long x) { return vnl_math::cube(x); }
843 inline unsigned long long cube(const unsigned long long x) { return vnl_math::cube(x); }
844 #endif
845 inline float cube(const float x) { return vnl_math::cube(x); }
846 inline double cube(const double x) { return vnl_math::cube(x); }
847 
848 inline unsigned int squared_magnitude(const char x) { return vnl_math::squared_magnitude(x); }
849 inline unsigned int squared_magnitude(const unsigned char x) { return vnl_math::squared_magnitude(x); }
850 inline unsigned int squared_magnitude(const int x) { return vnl_math::squared_magnitude(x); }
851 inline unsigned int squared_magnitude(const unsigned int x) { return vnl_math::squared_magnitude(x); }
852 inline unsigned long squared_magnitude(const long x) { return vnl_math::squared_magnitude(x); }
853 inline unsigned long squared_magnitude(const unsigned long x) { return vnl_math::squared_magnitude(x); }
854 #if VCL_HAS_LONG_LONG
855 inline unsigned long long squared_magnitude(const long long x) { return vnl_math::squared_magnitude(x); }
856 inline unsigned long long squared_magnitude(const unsigned long long x) { return vnl_math::squared_magnitude(x); }
857 #endif
858 inline float squared_magnitude(const float x) { return vnl_math::squared_magnitude(x); }
859 inline double squared_magnitude(const double x) { return vnl_math::squared_magnitude(x); }
860 inline long double squared_magnitude(const long double x) { return vnl_math::squared_magnitude(x); }
861 
862 #endif //If not C++11 features
863 
864 } // end namespace Math
865 } // end namespace itk
866 
867 #endif // end of itkMath.h
static bool AlmostEqualsFunction(double x1, float x2)
Definition: itkMath.h:366
static bool AlmostEqualsFunction(TSignedInt signedVariable, TUnsignedInt unsignedVariable)
Definition: itkMath.h:407
static bool AlmostEqualsFunction(float x1, double x2)
Definition: itkMath.h:373
static ITK_CONSTEXPR_VAR double sqrt2
Definition: itkMath.h:88
TReturn Round(TInput x)
Round towards nearest integer (This is a synonym for RoundHalfIntegerUp)
Definition: itkMath.h:179
RoundHalfIntegerUp(TInput x)
Round towards nearest integer.
bool isnan(const T value)
Definition: itkMath.h:789
static ITK_CONSTEXPR_VAR double one_over_pi
Definition: itkMath.h:76
int sgn0(const T x)
Definition: itkMath.h:802
static ITK_CONSTEXPR_VAR double pi_over_180
Definition: itkMath.h:74
static ITK_CONSTEXPR_VAR double one_over_sqrt2pi
Definition: itkMath.h:86
T remainder_truncated(const T x, const T y)
Definition: itkMath.h:803
static bool AlmostEqualsFunction(TIntType integerVariable, TFloatType floatingVariable)
Definition: itkMath.h:398
static ITK_CONSTEXPR_VAR double pi
Definition: itkMath.h:66
Floor(TInput x)
Round towards minus infinity.
bool isinf(const T value)
Definition: itkMath.h:790
static ITK_CONSTEXPR_VAR double log10e
Definition: itkMath.h:60
static ITK_CONSTEXPR_VAR double ln2
Definition: itkMath.h:62
static ITK_CONSTEXPR_VAR float float_eps
Definition: itkMath.h:100
bool ExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Return the result of an exact comparison between two scalar values of potetially different types...
Definition: itkMath.h:710
TReturn CastWithRangeCheck(TInput x)
Definition: itkMath.h:210
ITKCommon_EXPORT unsigned short GreatestPrimeFactor(unsigned short n)
static bool IsPositive(T val)
static ITK_CONSTEXPR_VAR double two_over_pi
Definition: itkMath.h:78
static ITK_CONSTEXPR_VAR double deg_per_rad
Definition: itkMath.h:80
static bool AlmostEqualsFunction(TUnsignedInt unsignedVariable, TSignedInt signedVariable)
Definition: itkMath.h:418
static ITK_CONSTEXPR_VAR double ln10
Definition: itkMath.h:64
static ITK_CONSTEXPR_VAR double pi_over_4
Definition: itkMath.h:72
static bool AlmostEqualsFunction(TFloatType floatingVariable, TIntType integerVariable)
Definition: itkMath.h:389
T angle_minuspi_to_pi(const T angle)
Definition: itkMath.h:795
Detail::FloatIEEE< T >::IntType FloatDifferenceULP(T x1, T x2)
Return the signed distance in ULPs (units in the last place) between two floats.
Definition: itkMath.h:251
int rnd_halfinttoeven(const T x)
Definition: itkMath.h:796
#define itkTemplateFloatingToIntegerMacro(name)
Definition: itkMath.h:106
T FloatAddULP(T x, typename Detail::FloatIEEE< T >::IntType ulps)
Add the given ULPs (units in the last place) to a float.
Definition: itkMath.h:267
bool sqr(const bool x)
Definition: itkMath.h:824
T remainder_floored(const T x, const T y)
Definition: itkMath.h:804
static ITK_CONSTEXPR_VAR double log2e
Definition: itkMath.h:58
FloatIEEETraits< T >::IntType IntType
static bool AlmostEqualsFunction(TIntegerType1 x1, TIntegerType2 x2)
Definition: itkMath.h:427
int sgn(const T x)
Definition: itkMath.h:801
static ITK_CONSTEXPR_VAR double sqrteps
Definition: itkMath.h:98
bool NotExactlyEquals(const TInput1 &x1, const TInput2 &x2)
Definition: itkMath.h:721
int floor(const T x)
Definition: itkMath.h:799
int rnd(const T x)
Definition: itkMath.h:798
static ITK_CONSTEXPR_VAR double sqrt1_2
Definition: itkMath.h:90
Ceil(TInput x)
Round towards plus infinity.
static bool AlmostEqualsFunction(float x1, float x2)
Definition: itkMath.h:380
T angle_0_to_2pi(const T angle)
Definition: itkMath.h:794
bool AlmostEquals(T1 x1, T2 x2)
Provide consistent equality checks between values of potentially different scalar or complex types...
Definition: itkMath.h:672
unsigned int squared_magnitude(const char x)
Definition: itkMath.h:848
bool FloatAlmostEqual(T x1, T x2, typename Detail::FloatIEEE< T >::IntType maxUlps=4, typename Detail::FloatIEEE< T >::FloatType maxAbsoluteDifference=0.1 *itk::NumericTraits< T >::epsilon())
Compare two floats and return if they are effectively equal.
Definition: itkMath.h:306
static ITK_CONSTEXPR_VAR double two_over_sqrtpi
Definition: itkMath.h:84
AlmostEqualsPlainOldEquals SelectedVersion
Definition: itkMath.h:439
static bool AlmostEqualsFunction(TFloatType1 x1, TFloatType2 x2)
Definition: itkMath.h:352
bool cube(const bool x)
Definition: itkMath.h:836
static ITK_CONSTEXPR_VAR double sqrt1_3
Definition: itkMath.h:92
static ITK_CONSTEXPR_VAR double sqrt2pi
Definition: itkMath.h:82
Define additional traits for native types such as int or float.
bool NotAlmostEquals(T1 x1, T2 x2)
Definition: itkMath.h:680
T hypot(const T value1, const T value2)
Definition: itkMath.h:793
int ceil(const T x)
Definition: itkMath.h:800
static bool AlmostEqualsFunction(double x1, double x2)
Definition: itkMath.h:359
static ITK_CONSTEXPR_VAR float float_sqrteps
Definition: itkMath.h:101
static ITK_CONSTEXPR_VAR double eps
Definition: itkMath.h:97
static ITK_CONSTEXPR_VAR double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:56
RoundHalfIntegerToEven(TInput x)
Round towards nearest integer.
#define itkConceptMacro(name, concept)
static ITK_CONSTEXPR_VAR double pi_over_2
Definition: itkMath.h:70
ITKCommon_EXPORT bool IsPrime(unsigned short n)
bool isfinite(const T value)
Definition: itkMath.h:791
static ITK_CONSTEXPR_VAR double twopi
Definition: itkMath.h:68
bool abs(const bool x)
Definition: itkMath.h:806
T cbrt(const T value)
Definition: itkMath.h:792
int rnd_halfintup(const T x)
Definition: itkMath.h:797
static ITK_CONSTEXPR_VAR double euler
euler constant
Definition: itkMath.h:94