# delegation to other functions involved (as for __mul__, etc.). aff_func = to_affine_scalar(x) (frac_part, int_part) = math.modf(aff_func.nominal_value) if aff_func.derivatives: # The derivative of the fractional part is simply 1: the # derivatives of modf(x)[0] are the derivatives of x: return (AffineScalarFunc(frac_part, aff_func.derivatives), int_part) else: # This function was not called with an AffineScalarFunc # argument: there is no need to return numbers with uncertainties: return (frac_part, int_part) modf = uncertainties.set_doc(math.modf.__doc__)(modf) many_scalars_to_scalar_funcs.append('modf') def ldexp(x, y): # The code below is inspired by uncertainties.wrap(). It is # simpler because only 1 argument is given, and there is no # delegation to other functions involved (as for __mul__, etc.). # Another approach would be to add an additional argument to # uncertainties.wrap() so that some arguments are automatically # considered as constants. aff_func = to_affine_scalar(x) # y must be an integer, for math.ldexp if aff_func.derivatives: factor = 2**y
def wrap_array_func(func): """ Returns a version of the function func() that works even when func() is given a NumPy array that contains numbers with uncertainties. func() is supposed to return a NumPy array. This wrapper is similar to uncertainties.wrap(), except that it handles an array argument instead of float arguments. func -- version that takes and returns a single NumPy array. """ def wrapped_func(arr, *args): # Nominal value: arr_nominal_value = nominal_values(arr) func_nominal_value = func(arr_nominal_value, *args) # The algorithm consists in numerically calculating the derivatives # of func: # Variables on which the array depends are collected: variables = set() for element in arr.flat: # floats, etc. might be present if isinstance(element, uncertainties.AffineScalarFunc): variables |= set(element.derivatives.iterkeys()) # If the matrix has no variables, then the function value can be # directly returned: if not variables: return func_nominal_value # Calculation of the derivatives of each element with respect # to the variables. Each element must be independent of the # others. The derivatives have the same shape as the output # array (which might differ from the shape of the input array, # in the case of the pseudo-inverse). derivatives = numpy.vectorize(lambda _: {})(func_nominal_value) for var in variables: # A basic assumption of this package is that the user # guarantees that uncertainties cover a zone where # evaluated functions are linear enough. Thus, numerical # estimates of the derivative should be good over the # standard deviation interval. This is true for the # common case of a non-zero standard deviation of var. If # the standard deviation of var is zero, then var has no # impact on the uncertainty of the function func being # calculated: an incorrect derivative has no impact. One # scenario can give incorrect results, however, but it # should be extremely uncommon: the user defines a # variable x with 0 standard deviation, sets y = func(x) # through this routine, changes the standard deviation of # x, and prints y; in this case, the uncertainty on y # might be incorrect, because this program had no idea of # the scale on which func() is linear, when it calculated # the numerical derivative. # The standard deviation might be numerically too small # for the evaluation of the derivative, though: we set the # minimum variable shift. shift_var = max(var._std_dev / 1e5, 1e-8 * abs(var._nominal_value)) # An exceptional case is that of var being exactly zero. # In this case, an arbitrary shift is used for the # numerical calculation of the derivative. The resulting # derivative value might be quite incorrect, but this does # not matter as long as the uncertainty of var remains 0, # since it is, in this case, a constant. if not shift_var: shift_var = 1e-8 # Shift of all the elements of arr when var changes by shift_var: shift_arr = array_derivative(arr, var) * shift_var # Origin value of array arr when var is shifted by shift_var: shifted_arr_values = arr_nominal_value + shift_arr func_shifted = func(shifted_arr_values, *args) numerical_deriv = (func_shifted - func_nominal_value) / shift_var # Update of the list of variables and associated # derivatives, for each element: for (derivative_dict, derivative_value) in zip(derivatives.flat, numerical_deriv.flat): if derivative_value: derivative_dict[var] = derivative_value # numbers with uncertainties are build from the result: return numpy.vectorize(uncertainties.AffineScalarFunc)(func_nominal_value, derivatives) wrapped_func = uncertainties.set_doc( """\ Version of %s(...) that works even when its first argument is a NumPy array that contains numbers with uncertainties. Warning: elements of the first argument array that are not AffineScalarFunc objects must not depend on uncertainties.Variable objects in any way. Otherwise, the dependence of the result in uncertainties.Variable objects will be incorrect. Original documentation: %s""" % (func.__name__, func.__doc__) )(wrapped_func) # It is easier to work with wrapped_func, which represents a # wrapped version of 'func', when it bears the same name as # 'func' (the name is used by repr(wrapped_func)). wrapped_func.__name__ = func.__name__ return wrapped_func
_pinv_with_uncert = func_with_deriv_to_uncert_func(pinv_with_derivatives) def _pinv(array_like, rcond=_pinv_default): return _pinv_with_uncert(array_like, rcond) _pinv = uncertainties.set_doc( """ Version of numpy.linalg.pinv that works with array-like objects that contain numbers with uncertainties. The result is a unumpy.matrix if numpy.linalg.pinv would return a matrix for the array of nominal values. Analytical formulas are used. Original documentation: %s """ % numpy.linalg.pinv.__doc__ )(_pinv) ########## Matrix class class matrix(numpy.matrix): # The name of this class is the same as NumPy's, which is why it # does not follow PEP 8. """
aff_func = to_affine_scalar(x) (frac_part, int_part) = math.modf(aff_func.nominal_value) if aff_func.derivatives: # The derivative of the fractional part is simply 1: the # derivatives of modf(x)[0] are the derivatives of x: return (AffineScalarFunc(frac_part, aff_func.derivatives), int_part) else: # This function was not called with an AffineScalarFunc # argument: there is no need to return numbers with uncertainties: return (frac_part, int_part) modf = uncertainties.set_doc(math.modf.__doc__)(modf) many_scalars_to_scalar_funcs.append('modf') def ldexp(x, y): # The code below is inspired by uncertainties.wrap(). It is # simpler because only 1 argument is given, and there is no # delegation to other functions involved (as for __mul__, etc.). # Another approach would be to add an additional argument to # uncertainties.wrap() so that some arguments are automatically # considered as constants. aff_func = to_affine_scalar(x) # y must be an integer, for math.ldexp if aff_func.derivatives:
def wrap_array_func(func): """ Returns a version of the function func() that works even when func() is given a NumPy array that contains numbers with uncertainties. This wrapper is similar to uncertainties.wrap(), except that it handles an array argument instead of float arguments. However, the returned function is more restricted: the array argument cannot be given as a keyword argument with the name in the original function (it is not a drop-in replacement). func -- function whose first argument takes a single NumPy array, and which returns a NumPy array. """ @uncertainties.set_doc("""\ Version of %s(...) that works even when its first argument is a NumPy array that contains numbers with uncertainties. Warning: elements of the first argument array that are not AffineScalarFunc objects must not depend on uncertainties.Variable objects in any way. Otherwise, the dependence of the result in uncertainties.Variable objects will be incorrect. Original documentation: %s""" % (func.__name__, func.__doc__)) def wrapped_func(arr, *args, **kwargs): # Nominal value: arr_nominal_value = nominal_values(arr) func_nominal_value = func(arr_nominal_value, *args, **kwargs) # The algorithm consists in numerically calculating the derivatives # of func: # Variables on which the array depends are collected: variables = set() for element in arr.flat: # floats, etc. might be present if isinstance(element, uncertainties.AffineScalarFunc): variables |= set(element.derivatives.iterkeys()) # If the matrix has no variables, then the function value can be # directly returned: if not variables: return func_nominal_value # Calculation of the derivatives of each element with respect # to the variables. Each element must be independent of the # others. The derivatives have the same shape as the output # array (which might differ from the shape of the input array, # in the case of the pseudo-inverse). derivatives = numpy.vectorize(lambda _: {})(func_nominal_value) for var in variables: # A basic assumption of this package is that the user # guarantees that uncertainties cover a zone where # evaluated functions are linear enough. Thus, numerical # estimates of the derivative should be good over the # standard deviation interval. This is true for the # common case of a non-zero standard deviation of var. If # the standard deviation of var is zero, then var has no # impact on the uncertainty of the function func being # calculated: an incorrect derivative has no impact. One # scenario can give incorrect results, however, but it # should be extremely uncommon: the user defines a # variable x with 0 standard deviation, sets y = func(x) # through this routine, changes the standard deviation of # x, and prints y; in this case, the uncertainty on y # might be incorrect, because this program had no idea of # the scale on which func() is linear, when it calculated # the numerical derivative. # The standard deviation might be numerically too small # for the evaluation of the derivative, though: we set the # minimum variable shift. shift_var = max(var._std_dev / 1e5, 1e-8 * abs(var._nominal_value)) # An exceptional case is that of var being exactly zero. # In this case, an arbitrary shift is used for the # numerical calculation of the derivative. The resulting # derivative value might be quite incorrect, but this does # not matter as long as the uncertainty of var remains 0, # since it is, in this case, a constant. if not shift_var: shift_var = 1e-8 # Shift of all the elements of arr when var changes by shift_var: shift_arr = array_derivative(arr, var) * shift_var # Origin value of array arr when var is shifted by shift_var: shifted_arr_values = arr_nominal_value + shift_arr func_shifted = func(shifted_arr_values, *args, **kwargs) numerical_deriv = (func_shifted - func_nominal_value) / shift_var # Update of the list of variables and associated # derivatives, for each element: for (derivative_dict, derivative_value) in (zip(derivatives.flat, numerical_deriv.flat)): if derivative_value: derivative_dict[var] = derivative_value # numbers with uncertainties are built from the result: return numpy.vectorize(uncertainties.AffineScalarFunc)( func_nominal_value, derivatives) wrapped_func = uncertainties.set_doc("""\ Version of %s(...) that works even when its first argument is a NumPy array that contains numbers with uncertainties. Warning: elements of the first argument array that are not AffineScalarFunc objects must not depend on uncertainties.Variable objects in any way. Otherwise, the dependence of the result in uncertainties.Variable objects will be incorrect. Original documentation: %s""" % (func.__name__, func.__doc__))(wrapped_func) # It is easier to work with wrapped_func, which represents a # wrapped version of 'func', when it bears the same name as # 'func' (the name is used by repr(wrapped_func)). wrapped_func.__name__ = func.__name__ return wrapped_func
# Default rcond argument for the generalization of numpy.linalg.pinv: pinv_default = numpy.linalg.pinv.__defaults__[0] # Python 1, 2.6+: pinv_with_uncert = func_with_deriv_to_uncert_func(pinv_with_derivatives) def pinv(array_like, rcond=pinv_default): return pinv_with_uncert(array_like, rcond) pinv = uncertainties.set_doc(""" Version of numpy.linalg.pinv that works with array-like objects that contain numbers with uncertainties. The result is a unumpy.matrix if numpy.linalg.pinv would return a matrix for the array of nominal values. Analytical formulas are used. Original documentation: %s """ % numpy.linalg.pinv.__doc__)(pinv) ########## Matrix class class matrix(numpy.matrix): # The name of this class is the same as NumPy's, which is why it # does not follow PEP 8. """ Class equivalent to numpy.matrix, but that behaves better when the matrix contains numbers with uncertainties.