Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

# This file is part of the pyMOR project (http://www.pymor.org). 

# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler 

# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) 

 

from __future__ import absolute_import, division, print_function 

 

import numpy as np 

 

from pymor import defaults 

 

 

def float_cmp(x, y, rtol=None, atol=None): 

    '''Compare x and y component-wise for almost equality. 

 

    For scalars we define almost equality as :: 

 

       float_cmp(x,y) <=> |x - y| <= atol + |y|*rtol 

 

    .. note:: 

       Numpy's :meth:`~numpy.allclose` method uses the same definition but 

       treats arrays containing infinities as close if the infinities are 

       at the same places and all other entries are close. 

       In our definition, arrays containing infinities can never be close 

       which seems more appropriate in most cases. 

 

    Parameters 

    ---------- 

    x, y 

        |NumPy arrays| to be compared. Have to be broadcastable to the same shape. 

    rtol 

        The relative tolerance. If `None`, it is set to `float_cmp_tol` 

        |default| value. 

    atol 

        The absolute tolerance. If `None`, it is set to `rtol`. 

    ''' 

 

    rtol = rtol or defaults.float_cmp_tol 

    atol = atol or rtol 

    return np.abs(x - y) <= atol + np.abs(y) * rtol 

 

 

def float_cmp_all(x, y, rtol=None, atol=None): 

    '''Compare x and y for almost equality. 

 

    Returns `True` if all components of `x` are almost equal to the corresponding 

    components of `y`. 

 

    See :meth:`float_cmp`. 

    ''' 

    return np.all(float_cmp(x, y, rtol, atol))