def test_math_module():
    "Operations with the math module"

    x = uncertainties.ufloat((-1.5, 0.1))
    
    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info[:2] >= (2, 6):
    
        # factorial() must not be "damaged" by the umath module, so as 
        # to help make it a drop-in replacement for math (even though 
        # factorial() does not work on numbers with uncertainties 
        # because it is restricted to integers, as for 
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3
Esempio n. 2
0
def test_math_module():
    "Operations with the math module"

    x = uncertainties.ufloat((-1.5, 0.1))

    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info[:2] >= (2, 6):

        # factorial() must not be "damaged" by the umath module, so as
        # to help make it a drop-in replacement for math (even though
        # factorial() does not work on numbers with uncertainties
        # because it is restricted to integers, as for
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3
Esempio n. 3
0
def test_math_module():
    "Operations with the math module"

    x = ufloat(-1.5, 0.1)
    
    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info >= (2, 6):
    
        # factorial() must not be "damaged" by the umath module, so as 
        # to help make it a drop-in replacement for math (even though 
        # factorial() does not work on numbers with uncertainties 
        # because it is restricted to integers, as for 
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3

    # The same exceptions should be generated when numbers with uncertainties
    # are used:

    ## !! The Nose testing framework seems to catch an exception when
    ## it is aliased: "exc = OverflowError; ... except exc:..."
    ## surprisingly catches OverflowError. So, tests are written in a
    ## version-specific manner (until the Nose issue is resolved).

    if sys.version_info < (2, 6):
            
        try:
            math.log(0)
        except OverflowError, err_math:  # "as", for Python 2.6+
            pass
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(0)
        except OverflowError, err_ufloat:  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
Esempio n. 4
0
def test_math_module():
    "Operations with the math module"

    x = ufloat(-1.5, 0.1)

    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info >= (2, 6):

        # factorial() must not be "damaged" by the umath module, so as
        # to help make it a drop-in replacement for math (even though
        # factorial() does not work on numbers with uncertainties
        # because it is restricted to integers, as for
        # math.factorial()):
        assert umath.factorial(4) == 24

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3

    # Functions that give locally constant results are tested: they
    # should give the same result as their float equivalent:
    for name in umath.locally_cst_funcs:

        try:
            func = getattr(umath, name)
        except AttributeError:
            continue  # Not in the math module, so not in umath either

        assert func(x) == func(x.nominal_value)
        # The type should be left untouched. For example, isnan()
        # should always give a boolean:
        assert type(func(x)) == type(func(x.nominal_value))

    # The same exceptions should be generated when numbers with uncertainties
    # are used:

    ## !! The Nose testing framework seems to catch an exception when
    ## it is aliased: "exc = OverflowError; ... except exc:..."
    ## surprisingly catches OverflowError. So, tests are written in a
    ## version-specific manner (until the Nose issue is resolved).

    if sys.version_info < (2, 6):

        try:
            math.log(0)
        except OverflowError, err_math:  # "as", for Python 2.6+
            pass
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(0)
        except OverflowError, err_ufloat:  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
Esempio n. 5
0
def test_math_module():
    "Operations with the math module"

    x = ufloat(-1.5, 0.1)
    
    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # factorial() must not be "damaged" by the umath module, so as 
    # to help make it a drop-in replacement for math (even though 
    # factorial() does not work on numbers with uncertainties 
    # because it is restricted to integers, as for 
    # math.factorial()):
    assert umath.factorial(4) == 24

    # fsum is special because it does not take a fixed number of
    # variables:
    assert umath.fsum([x, x]).nominal_value == -3

    # Functions that give locally constant results are tested: they
    # should give the same result as their float equivalent:
    for name in umath.locally_cst_funcs:

        try:
            func = getattr(umath, name)
        except AttributeError:
            continue  # Not in the math module, so not in umath either
        
        assert func(x) == func(x.nominal_value)
        # The type should be left untouched. For example, isnan()
        # should always give a boolean:
        assert type(func(x)) == type(func(x.nominal_value))

    # The same exceptions should be generated when numbers with uncertainties
    # are used:

    ## !! The Nose testing framework seems to catch an exception when
    ## it is aliased: "exc = OverflowError; ... except exc:..."
    ## surprisingly catches OverflowError. So, tests are written in a
    ## version-specific manner (until the Nose issue is resolved).

    try:
        math.log(0)
    except ValueError as err_math:
        # Python 3 does not make exceptions local variables: they are
        # restricted to their except block:
        err_math_args = err_math.args
    else:
        raise Exception('ValueError exception expected')

    try:
        umath.log(0)
    except ValueError as err_ufloat:
        assert err_math_args == err_ufloat.args
    else:
        raise Exception('ValueError exception expected')
    try:
        umath.log(ufloat(0, 0))
    except ValueError as err_ufloat:
        assert err_math_args == err_ufloat.args
    else:
        raise Exception('ValueError exception expected')
    try:
        umath.log(ufloat(0, 1))
    except ValueError as err_ufloat:
        assert err_math_args == err_ufloat.args
    else:
        raise Exception('ValueError exception expected')
Esempio n. 6
0
def test_math_module():
    "Operations with the math module"

    x = uncertainties.ufloat((-1.5, 0.1))

    # The exponent must not be differentiated, when calculating the
    # following (the partial derivative with respect to the exponent
    # is not defined):
    assert (x**2).nominal_value == 2.25

    # Regular operations are chosen to be unchanged:
    assert isinstance(umath.sin(3), float)

    # Python >=2.6 functions:

    if sys.version_info >= (2, 6):

        # factorial() must not be "damaged" by the umath module, so as
        # to help make it a drop-in replacement for math (even though
        # factorial() does not work on numbers with uncertainties
        # because it is restricted to integers, as for
        # math.factorial()):
        assert umath.factorial(4) == 24

        # Boolean functions:
        assert not umath.isinf(x)

        # Comparison, possibly between an AffineScalarFunc object and a
        # boolean, which makes things more difficult for this code:
        assert umath.isinf(x) == False

        # fsum is special because it does not take a fixed number of
        # variables:
        assert umath.fsum([x, x]).nominal_value == -3

    # The same exceptions should be generated when numbers with uncertainties
    # are used:

    ## !! The Nose testing framework seems to catch an exception when
    ## it is aliased: "exc = OverflowError; ... except exc:..."
    ## surprisingly catches OverflowError. So, tests are written in a
    ## version-specific manner (until the Nose issue is resolved).

    if sys.version_info < (2, 6):

        try:
            math.log(0)
        except OverflowError(err_math):  # "as", for Python 2.6+
            pass
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(0)
        except OverflowError(err_ufloat):  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 0)))
        except OverflowError( err_ufloat):  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('OverflowError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 1)))
        except OverflowError( err_ufloat):  # "as", for Python 2.6+
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('OverflowError exception expected')

    elif sys.version_info < (3,):

        try:
            math.log(0)
        except ValueError( err_math):
            pass
        else:
            raise Exception('ValueError exception expected')
        try:
            umath.log(0)
        except ValueError( err_ufloat):
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('ValueError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 0)))
        except ValueError( err_ufloat):
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('ValueError exception expected')
        try:
            umath.log(uncertainties.ufloat((0, 1)))
        except ValueError( err_ufloat):
            assert err_math.args == err_ufloat.args
        else:
            raise Exception('ValueError exception expected')

    else:  # Python 3+

        # !!! The tests should be made to work with Python 3 too!
        pass