### Install Uncertainties to Custom Directory with setup.py Source: https://pythonhosted.org/uncertainties/index This command installs the uncertainties package into a specified custom directory ('my_directory') using the setup.py script. Ensure 'my_directory' is a valid path and Python can import from it. ```bash python setup.py install --install-lib my_directory ``` -------------------------------- ### Manual Install Uncertainties with setup.py Source: https://pythonhosted.org/uncertainties/index This command installs the uncertainties package manually by running its setup.py script. Navigate to the unpacked package directory first. Replace 'python' with the specific Python interpreter if needed (e.g., 'python3', 'python3.3'). ```bash python setup.py install ``` -------------------------------- ### User-Specific Install Uncertainties with setup.py Source: https://pythonhosted.org/uncertainties/index This command installs the uncertainties package into the user's Python library, which does not require additional system access rights. It uses the setup.py script within the unpacked package directory. ```bash python setup.py install --user ``` -------------------------------- ### Untitled No description -------------------------------- ### Install Uncertainties with easy_install Source: https://pythonhosted.org/uncertainties/index This command automatically installs or upgrades the uncertainties package using easy_install, part of the setuptools package. While less common now than pip, it's another method for Python package installation. ```bash easy_install --upgrade uncertainties ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Install Uncertainties with Root Privileges Source: https://pythonhosted.org/uncertainties/index This command installs the uncertainties package using 'sudo' for elevated privileges. This is typically required on Unix-like systems when installing to system-wide Python directories and needs to be run from the unpacked package directory. ```bash sudo python setup.py install ``` -------------------------------- ### Install Uncertainties with pip Source: https://pythonhosted.org/uncertainties/index This command installs or upgrades the uncertainties package to the latest version using pip, a standard Python package installer. It's a common and straightforward method for package management. ```bash pip install --upgrade uncertainties ``` -------------------------------- ### Untitled No description -------------------------------- ### Import Uncertainties Module Source: https://pythonhosted.org/uncertainties/user_guide This snippet shows how to import the necessary functions from the uncertainties package for basic usage. It's the first step to enable calculations with uncertainties. ```python from uncertainties import ufloat ``` ```python import uncertainties ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Get Nominal Value and Standard Deviation Uniformly (Python) Source: https://pythonhosted.org/uncertainties/user_guide Provides utility functions `uncertainties.nominal_value()` and `uncertainties.std_dev()` to access the nominal value and standard deviation of numbers, whether they are simple floats or objects with uncertainties. This allows for uniform handling of mixed data types. ```python >>> print uncertainties.nominal_value(x) 0.2 >>> print uncertainties.std_dev(x) 0.01 >>> uncertainties.nominal_value(3) 3 >>> uncertainties.std_dev(3) 0.0 ``` -------------------------------- ### Displaying All Digits with repr() Source: https://pythonhosted.org/uncertainties/user_guide Shows how the `repr()` function provides a representation of the number with uncertainty that includes all its digits, unlike the default `print` output which is formatted for readability. ```python >>> y = ufloat(1.23456789012345, 0.123456789) >>> print y 1.23+/-0.12 ``` ```python >>> print repr(y) 1.23456789012345+/-0.123456789 ``` ```python >>> y 1.23456789012345+/-0.123456789 ``` -------------------------------- ### Formatting Zero Uncertainties Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates how uncertainties that are exactly zero are always formatted as integers, simplifying the output when a value has no associated uncertainty. ```python >>> print ufloat(3.1415, 0) 3.1415+/-0 ``` ```python >>> print ufloat(3.1415e10, 0) (3.1415+/-0)e+10 ``` ```python >>> print ufloat(3.1415, 0.0005) 3.1415+/-0.0005 ``` ```python >>> print '{:.2f}'.format(ufloat(3.14, 0.001)) 3.14+/-0.00 ``` ```python >>> print '{:.2f}'.format(ufloat(3.14, 0.00)) 3.14+/-0 ``` -------------------------------- ### Greater Than Comparison for Uncertain Numbers Source: https://pythonhosted.org/uncertainties/tech_guide This example demonstrates the greater than comparison between two `ufloat` objects. The result is `True` if the nominal value of the first is greater than the second, assuming their probability distributions largely do not overlap, aligning with practical expectations. ```python >>> x = ufloat(3.14, 0.01) >>> y = ufloat(3.00, 0.01) >>> x > y True ``` -------------------------------- ### Install Uncertainties on Mac OS X with MacPorts Source: https://pythonhosted.org/uncertainties/index For Mac OS X users with the MacPorts package manager, this command installs the uncertainties package. Replace '**' with your desired Python version (e.g., '27' for Python 2.7, '33' for Python 3.3). ```bash sudo port install py**-uncertainties ``` ```bash sudo port upgrade py**-uncertainties ``` -------------------------------- ### Generating Correlation Matrix in Python (with NumPy) Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates how to generate the correlation matrix for a list of uncertain quantities when the NumPy package is available, using `uncertainties.correlation_matrix()`. ```python from uncertainties import ufloat from uncertainties import correlation_matrix import numpy as np # Assuming numpy is available u = ufloat(1, 0.1) v = ufloat(10, 0.1) sum_value = u + 2 * v corr_matrix = correlation_matrix([u, v, sum_value]) print(corr_matrix) ``` -------------------------------- ### Untitled No description -------------------------------- ### Tracking Random Variables with Uncertainties Source: https://pythonhosted.org/uncertainties/tech_guide Demonstrates how the 'uncertainties' package tracks random variables and their dependencies to perform transparent calculations. It shows how uncertainties are updated on the fly and how object dependencies are maintained even after redefinition. ```python >>> x = ufloat(2, 0.1) >>> a = 42 >>> poly = x**2 + a >>> poly 46.0+/-0.4 >>> poly - x*x 42.0+/-0.0 >>> a = 123 >>> print poly 46.0+/-0.4 >>> x.std_dev = 0 >>> print poly 46.0+/-0.0 >>> x = 10000 >>> print poly 46.0+/-0.0 ``` -------------------------------- ### Basic Math Operations with Uncertainties Source: https://pythonhosted.org/uncertainties/user_guide Illustrates how standard mathematical operations like exponentiation can be directly applied to numbers with uncertainties. The results automatically incorporate the uncertainty propagation. ```python square = x**2 print square ``` -------------------------------- ### Untitled No description -------------------------------- ### Basic Usage with Uncertainties in Python Source: https://pythonhosted.org/uncertainties/index Demonstrates how to use the `uncertainties` package for basic arithmetic operations and mathematical functions with numbers having uncertainties. It showcases automatic error propagation and correlation handling. Requires the `uncertainties` package to be installed. ```python >>> from uncertainties import ufloat >>> from uncertainties.umath import * # sin(), etc. >>> x = ufloat(1, 0.1) # x = 1+/-0.1 >>> print 2*x 2.00+/-0.20 >>> sin(2*x) # In a Python shell, "print" is optional 0.9092974268256817+/-0.08322936730942848 ``` -------------------------------- ### Custom Formatting for Uncertain Numbers in Python Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates how to create a custom string formatter in Python that can handle the shorthand notation for numbers with uncertainties, using the string.Formatter class and the 'S' format specifier. ```python import string import uncertainties from uncertainties import ufloat class ShorthandFormatter(string.Formatter): def format_field(self, value, format_spec): if isinstance(value, uncertainties.UFloat): return value.format(format_spec+'S') # Shorthand option added # Special formatting for other types can be added here (floats, etc.) else: # Usual formatting: return super(ShorthandFormatter, self).format_field( value, format_spec) frmtr = ShorthandFormatter() x = ufloat(0.2, 0.001) print(frmtr.format("Result = {0:.1S}", x)) # 1-digit uncertainty ``` -------------------------------- ### Create Numbers with Uncertainties Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates various methods to create numbers with uncertainties using the `ufloat` and `ufloat_fromstr` functions. This includes direct numerical input, string parsing with explicit uncertainty, shorthand notation, and automatic uncertainty determination. ```python x = ufloat(0.20, 0.01) # x = 0.20+/-0.01 ``` ```python from uncertainties import ufloat_fromstr x = ufloat_fromstr("0.20+/-0.01") x = ufloat_fromstr("(2+/-0.1)e-01") # Factored exponent x = ufloat_fromstr("0.20(1)") # Short-hand notation x = ufloat_fromstr("20(1)e-2") # Exponent notation x = ufloat_fromstr(u"0.20±0.01") # Pretty-print form x = ufloat_fromstr("0.20") # Automatic uncertainty of +/-1 on last digit ``` -------------------------------- ### Basic Printing of Uncertainties Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates the default way to print numbers with uncertainties. The output shows the nominal value and the uncertainty with the same precision. This format can often be parsed back using `ufloat_fromstr()`. ```python >>> print x 0.200+/-0.010 ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Handling Correlated Variables Source: https://pythonhosted.org/uncertainties/user_guide Explains and demonstrates how the uncertainties package automatically handles correlations between variables during calculations. This ensures accurate uncertainty propagation even in complex scenarios. ```python square = x**2 print square square - x*x y = x*x + 1 y - square ``` -------------------------------- ### Generating Covariance Matrix in Python Source: https://pythonhosted.org/uncertainties/user_guide Shows how to compute the covariance matrix for a list of uncertain quantities using the `uncertainties.covariance_matrix()` function. This matrix indicates the relationships and dependencies between variables. ```python from uncertainties import ufloat from uncertainties import covariance_matrix u = ufloat(1, 0.1) v = ufloat(10, 0.1) sum_value = u + 2 * v cov_matrix = covariance_matrix([u, v, sum_value]) print(cov_matrix) ``` -------------------------------- ### Calculate Derivatives of Expressions (Python) Source: https://pythonhosted.org/uncertainties/user_guide Shows how the `uncertainties` package automatically calculates derivatives of expressions with respect to their constituent uncertain variables. This is fundamental for error propagation and is achieved through a fast differentiation algorithm. ```python >>> u = ufloat(1, 0.1) >>> v = ufloat(10, 0.1) >>> sum_value = u+2*v >>> print sum_value.derivatives[u] 1.0 >>> print sum_value.derivatives[v] 2.0 ``` -------------------------------- ### Creating Variables with Uncertainties Source: https://pythonhosted.org/uncertainties/tech_guide Shows how to create independent random variables using the `ufloat()` factory function from the 'uncertainties' package. It also illustrates the type of object returned, which is a `Variable` instance. ```python >>> x = ufloat(1, 0.1) >>> type(x) ``` -------------------------------- ### Accessing Nominal Value and Standard Deviation in Python Source: https://pythonhosted.org/uncertainties/user_guide Shows how to access the core numerical value (nominal_value) and the associated uncertainty (std_dev) of an uncertain number object in Python. ```python from uncertainties import ufloat x = ufloat(0.2, 0.001) square = x**2 print(square) print(square.nominal_value) print(square.n) # Abbreviation print(square.std_dev) print(square.s) # Abbreviation ``` -------------------------------- ### Extracting Error Components for Uncertain Numbers in Python Source: https://pythonhosted.org/uncertainties/user_guide Illustrates how to obtain individual contributions to the total uncertainty of a calculated quantity using the `error_components()` method. It shows how to use tags to identify the source of each error component. ```python from uncertainties import ufloat import math u = ufloat(1, 0.1, "u variable") # Tag v = ufloat(10, 0.1, "v variable") sum_value = u + 2 * v # Print individual contributions for (var, error) in sum_value.error_components().items(): print("{}: {}".format(var.tag, error)) # Example: calculating uncertainty from systematic errors # Assume 'result' is a calculated ufloat object with tagged components # syst_error = math.sqrt(sum( # error**2 # for (var, error) in result.error_components().items() # if var.tag == "systematic")) # other_error = math.sqrt(result.std_dev**2 - syst_error**2) ``` -------------------------------- ### Untitled No description -------------------------------- ### Advanced Math Functions with Uncertainties Source: https://pythonhosted.org/uncertainties/user_guide Shows how to use extended mathematical functions (like sin, cos, etc.) from the `uncertainties.umath` module. These functions are generalized to handle numbers with uncertainties. ```python from uncertainties.umath import * sin(x**2) ``` -------------------------------- ### Comparison Operators for Uncertain Numbers in Python Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates the behavior of comparison operators (>, ==) when applied to uncertain numbers. It highlights that two uncertain numbers with the same nominal value and standard deviation are considered distinct. ```python from uncertainties import ufloat x = ufloat(0.2, 0.001) y = x + 0.0001 print(x > y) print(y > 0) y_same = ufloat(1, 0.1) z_same = ufloat(1, 0.1) print(y_same == y_same) print(y_same == z_same) ``` -------------------------------- ### Handle Correlated Variables with Covariance Matrix (Python) Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates how to create variables with correlations using a covariance matrix. It shows how to create correlated values and then recover the covariance matrix from these values. This is useful when dealing with dependent measurements or parameters. ```python >>> (u2, v2, sum2) = uncertainties.correlated_values([1, 10, 21], cov_matrix) >>> print sum2 21.0+/-0.223606797749979 >>> uncertainties.covariance_matrix([u2, v2, sum2]) ``` -------------------------------- ### Factored Common Exponent Formatting Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates how the library automatically calculates and factors a common exponent for both the nominal value and the uncertainty when needed for legibility. This is typically seen when the numbers span several orders of magnitude. ```python >>> print x*1e7 (2.00+/-0.10)e+06 ``` -------------------------------- ### Unfactored Common Exponent with Format Width Source: https://pythonhosted.org/uncertainties/user_guide Explains that using a format width (e.g., in `'{:10.1e}'`) prevents the common exponent from being factored, leading to potentially less readable but more aligned output for specific use cases. ```python >>> print 'Result = {:10.1e}'.format(x*1e-10) Result = 2.0e-11+/- 0.1e-11 ``` -------------------------------- ### Untitled No description -------------------------------- ### Arrays and Matrices with Uncertainties Source: https://pythonhosted.org/uncertainties/user_guide Demonstrates the use of NumPy arrays with numbers containing uncertainties. Standard NumPy operations like multiplication and summation are transparently handled, propagating uncertainties correctly. ```python import numpy arr = numpy.array([ufloat(1, 0.01), ufloat(2, 0.1)]) 2*arr arr.sum() ``` -------------------------------- ### Handling Correlations with Uncertainties in Python Source: https://pythonhosted.org/uncertainties/index Illustrates the correct handling of correlations between variables with uncertainties using the `uncertainties` package. This example shows that subtracting a variable from itself correctly results in zero uncertainty, unlike other methods that might wrongly assume independence. ```python >>> x-x 0.0+/-0 ``` -------------------------------- ### Untitled No description -------------------------------- ### Equality Comparison for Independent Random Variables Source: https://pythonhosted.org/uncertainties/tech_guide This example illustrates that comparing two independent `ufloat` objects for equality, even if they have the same nominal value and uncertainty, returns `True` because a sample from a probability distribution is always equal to itself. ```python >>> x = ufloat(3.14, 0.01) >>> x == x True ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Handling Not-a-Number (NaN) Uncertainties in Python Source: https://pythonhosted.org/uncertainties/tech_guide Illustrates how the 'uncertainties' package handles cases where linear error propagation theory is not applicable, resulting in a 'nan' value for the derivative. It shows an example using `umath.sqrt` with an input uncertainty where the derivative is undefined at zero, leading to a 'nan' in the result. It also contrasts this with the correct handling of precise numbers. ```python >>> import uncertainties.umath as umath >>> from uncertainties import ufloat >>> umath.sqrt(ufloat(0, 1)) 0.0+/-nan >>> umath.sqrt(ufloat(0, 0)) 0.0+/-0 ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Update a Single Python File from Uncertainties v1 to v2 Source: https://pythonhosted.org/uncertainties/index This command specifically targets a single Python file ('example.py') and updates it from uncertainties version 1 syntax to version 2 syntax. The '-w' flag indicates that the changes should be written back to the file. ```bash python -m uncertainties.1to2 -w example.py ``` -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Untitled No description -------------------------------- ### Adapt Uncertainties Code to Python 3 Source: https://pythonhosted.org/uncertainties/index For Python 3 users who have manually moved the uncertainties package directory, this command uses '2to3' to automatically adapt the code to Python 3. Run this command from within the 'uncertainties-py*' directory. ```bash 2to3 -w . ``` -------------------------------- ### Representing Functions with Uncertainties Source: https://pythonhosted.org/uncertainties/tech_guide Illustrates how mathematical expressions involving numbers with uncertainties are represented as `AffineScalarFunc` objects in the 'uncertainties' package. These objects store the variables they depend on. ```python >>> import uncertainties.umath as umath >>> x = ufloat(1, 0.1) >>> type(umath.sin(x)) ``` -------------------------------- ### Calculating Derivatives with Uncertainties in Python Source: https://pythonhosted.org/uncertainties/index Shows how to obtain the derivatives of an expression involving numbers with uncertainties using the `uncertainties` package. This feature allows for easy calculation of how changes in input variables affect the output. ```python >>> (2*x+1000).derivatives[x] 2.0 ``` -------------------------------- ### Untitled No description