TestCenter Reference
Math.py
Go to the documentation of this file.
2# Copyright 2010, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11
13
14# -- system imports ----------------------------------------------------------------------------{{{-
15from numbers import Number
16# ----------------------------------------------------------------------------------------------}}}-
17
18
20 return ( isinstance( a, Number ) and isinstance( b, Number ) )
21
23 return ( ( type( a ) in ( list, tuple ) ) and ( type( b ) in ( list, tuple ) ))
24
25def _haveEqualLength( a, b ):
26 return ( len( a ) == len( b ) )
27
28
29def _areEqual( a, b, epsilon ):
30 return ( abs( a - b ) < epsilon )
31
32def _isLessThan( a, b ):
33 return ( a < b )
34
35
36# -- def compareFloatEqual ---------------------------------------------------------------------{{{-
37
42def compareFloatEqual ( a, b, epsilon = 0.0001 ):
43 result = False
44 if ( _areInstancesOfNumber( a, b ) ):
45 result = _areEqual( a, b, epsilon )
46
47 elif ( _areOfTypeListOrTuple( a, b ) ):
48 if ( _haveEqualLength( a, b ) ):
49 result = True
50 for i in range( 0, len( a ) ):
51 result = compareFloatEqual ( a[i], b[i], epsilon )
52 if ( not result ):
53 break
54
55 else:
56 Logging_error( "Only lists or tuples of equal length can be compared!" )
57
58 else:
59 Logging_error( "Only numbers can be compared!" )
60
61 return result
62
63# ----------------------------------------------------------------------------------------------}}}-
64
65# -- def compareFloatLessThan ------------------------------------------------------------------{{{-
66
71 result = False
72 if ( _areInstancesOfNumber( a, b ) ):
73 result = _isLessThan( a, b )
74
75 elif ( _areOfTypeListOrTuple( a, b ) ):
76 if ( _haveEqualLength( a, b ) ):
77 for i in range( 0, len( a ) ):
78 result = compareFloatLessThan ( a[i], b[i] )
79 if ( not result ):
80 break
81
82 else:
83 Logging_error( "Only lists or tuples of equal length can be compared!" )
84
85 else:
86 Logging_error( "Only numbers can be compared!" )
87
88 return result
89
90# ----------------------------------------------------------------------------------------------}}}-
91
92# -- def compareFloatLessThanOrEqual -----------------------------------------------------------{{{-
93
99def compareFloatLessThanOrEqual ( a, b, epsilon = 0.0001 ):
100 return ( compareFloatLessThan( a, b ) or compareFloatEqual( a, b, epsilon ) )
101
102# ----------------------------------------------------------------------------------------------}}}-
103
104from .Logging import error as Logging_error, warning as Logging_warning, info as Logging_info
_areOfTypeListOrTuple(a, b)
Definition Math.py:22
_isLessThan(a, b)
Definition Math.py:32
compareFloatLessThanOrEqual(a, b, epsilon=0.0001)
Compare the two given float values if the first is less than or equal to the second.
Definition Math.py:99
compareFloatLessThan(a, b)
Compare the two given float values if the first is less than the second.
Definition Math.py:70
compareFloatEqual(a, b, epsilon=0.0001)
Compare the given float values or lists of float values for equality.
Definition Math.py:42
_haveEqualLength(a, b)
Definition Math.py:25
_areInstancesOfNumber(a, b)
Definition Math.py:19
_areEqual(a, b, epsilon)
Definition Math.py:29