Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
def cos_plain(angle):
return math.cos(angle)
def cos_kwargs(angle, **kwargs):
return math.cos(angle)
def use_kwargs(angle, cos=True):
if cos:
return math.cos(angle)
else:
return math.sin(angle)
# wrappings of these functions
wrap_cos_plain = uncertainties.wrap(cos_plain)
wrap_cos_wderiv = uncertainties.wrap(cos_plain, [math.cos])
wrap_cos_kwargs = uncertainties.wrap(cos_kwargs)
wrap_use_kwargs = uncertainties.wrap(use_kwargs)
umath_cos = umath.cos
umath_sin = umath.sin
# now test that the wrapped functions give the same results
# as the umath versions for a variety of input values
for a in (ufloat((0.2, 0.01)), ufloat((0.7, 0.00001)),
#ufloat((0.9, 0.3)), ufloat((1.e-4, 0.3)),
#ufloat((200.0, 0.3)), ufloat((1.e5, 0.3)),
#0, 2, 1.25, 0.0, 1.e-5, 0.707, 1.5708
):
ucos = umath_cos(a)
usin = umath_sin(a)
assert _numbers_close(ucos, wrap_cos_plain(a))
assert _numbers_close(ucos, wrap_cos_wderiv(a))
def cos_plain(angle):
return math.cos(angle)
def cos_kwargs(angle, **kwargs):
return math.cos(angle)
def use_kwargs(angle, cos=True):
if cos:
return math.cos(angle)
else:
return math.sin(angle)
# wrappings of these functions
wrap_cos_plain = uncertainties.wrap(cos_plain)
wrap_cos_wderiv = uncertainties.wrap(cos_plain, [math.cos])
wrap_cos_kwargs = uncertainties.wrap(cos_kwargs)
wrap_use_kwargs = uncertainties.wrap(use_kwargs)
umath_cos = umath.cos
umath_sin = umath.sin
# now test that the wrapped functions give the same results
# as the umath versions for a variety of input values
for a in (ufloat((0.2, 0.01)), ufloat((0.7, 0.00001)),
#ufloat((0.9, 0.3)), ufloat((1.e-4, 0.3)),
#ufloat((200.0, 0.3)), ufloat((1.e5, 0.3)),
#0, 2, 1.25, 0.0, 1.e-5, 0.707, 1.5708
):
ucos = umath_cos(a)
usin = umath_sin(a)
assert _numbers_close(ucos, wrap_cos_plain(a))
assert _numbers_close(ucos, wrap_cos_wderiv(a))
assert _numbers_close(ucos, wrap_cos_kwargs(a))
Test wrapped functions with keyword args
"""
def cos_plain(angle):
return math.cos(angle)
def cos_kwargs(angle, **kwargs):
return math.cos(angle)
def use_kwargs(angle, cos=True):
if cos:
return math.cos(angle)
else:
return math.sin(angle)
# wrappings of these functions
wrap_cos_plain = uncertainties.wrap(cos_plain)
wrap_cos_wderiv = uncertainties.wrap(cos_plain, [math.cos])
wrap_cos_kwargs = uncertainties.wrap(cos_kwargs)
wrap_use_kwargs = uncertainties.wrap(use_kwargs)
umath_cos = umath.cos
umath_sin = umath.sin
# now test that the wrapped functions give the same results
# as the umath versions for a variety of input values
for a in (ufloat((0.2, 0.01)), ufloat((0.7, 0.00001)),
#ufloat((0.9, 0.3)), ufloat((1.e-4, 0.3)),
#ufloat((200.0, 0.3)), ufloat((1.e5, 0.3)),
#0, 2, 1.25, 0.0, 1.e-5, 0.707, 1.5708
):
ucos = umath_cos(a)
usin = umath_sin(a)
assert _numbers_close(ucos, wrap_cos_plain(a))
return math.cos(angle)
def cos_kwargs(angle, **kwargs):
return math.cos(angle)
def use_kwargs(angle, cos=True):
if cos:
return math.cos(angle)
else:
return math.sin(angle)
# wrappings of these functions
wrap_cos_plain = uncertainties.wrap(cos_plain)
wrap_cos_wderiv = uncertainties.wrap(cos_plain, [math.cos])
wrap_cos_kwargs = uncertainties.wrap(cos_kwargs)
wrap_use_kwargs = uncertainties.wrap(use_kwargs)
umath_cos = umath.cos
umath_sin = umath.sin
# now test that the wrapped functions give the same results
# as the umath versions for a variety of input values
for a in (ufloat((0.2, 0.01)), ufloat((0.7, 0.00001)),
#ufloat((0.9, 0.3)), ufloat((1.e-4, 0.3)),
#ufloat((200.0, 0.3)), ufloat((1.e5, 0.3)),
#0, 2, 1.25, 0.0, 1.e-5, 0.707, 1.5708
):
ucos = umath_cos(a)
usin = umath_sin(a)
assert _numbers_close(ucos, wrap_cos_plain(a))
assert _numbers_close(ucos, wrap_cos_wderiv(a))
assert _numbers_close(ucos, wrap_cos_kwargs(a))
assert _numbers_close(ucos, wrap_cos_kwargs(a, opt=None))
def wrapped_fsum():
"""
Returns an uncertainty-aware version of math.fsum, which must
be contained in _original_func.
"""
# The fsum function is flattened, in order to use the
# wrap() wrapper:
flat_fsum = lambda *args: original_func(args)
flat_fsum_wrap = uncertainties.wrap(
flat_fsum, itertools.repeat(lambda *args: 1))
return wraps(lambda arg_list: flat_fsum_wrap(*arg_list),
original_func)
uncertainty in an object even with a complicated expression.
"""
_obj = kwargs.get('_obj', None)
_pars = kwargs.get('_pars', None)
_names = kwargs.get('_names', None)
_asteval = _pars._asteval
if (_obj is None or _pars is None or _names is None or
_asteval is None or _obj._expr_ast is None):
return 0
for val, name in zip(vals, _names):
_asteval.symtable[name] = val
return _asteval.eval(_obj._expr_ast)
wrap_ueval = uncertainties.wrap(asteval_with_uncertainties)
def eval_stderr(obj, uvars, _names, _pars):
"""Evaluate uncertainty and set .stderr for a parameter `obj`.
Given the uncertain values `uvars` (a list of uncertainties.ufloats), a
list of parameter names that matches uvars, and a dict of param objects,
keyed by name.
This uses the uncertainties package wrapped function to evaluate the
uncertainty for an arbitrary expression (in obj._expr_ast) of parameters.
"""
if not isinstance(obj, Parameter) or getattr(obj, '_expr_ast', None) is None:
return
uval = wrap_ueval(*uvars, _obj=obj, _names=_names, _pars=_pars)