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