Skip to main content

Uncertainty Calculator

Python 3.12+ CI Status codecov Code style: ruff Checked with mypy pre-commit License: MIT Open in Colab

"Help you get rid of the Physical Chemistry Experiment course in CCME at PKU!"

A Python tool for automated error propagation in physical experiments. This calculator uses symbolic differentiation to compute partial derivatives and propagate uncertainties, generating detailed LaTeX output for your reports.


📑 Table of Contents

✨ Features

  • Symbolic Differentiation: Automatically calculates partial derivatives using sympy.
  • Automated Error Propagation: Computes the final uncertainty based on the standard error propagation formula.
  • LaTeX Output: Generates ready-to-use LaTeX code for equations, derivatives, and substitution steps.
  • Customizable: Configurable decimal precision, units, and output formatting.

🚀 Installation

Requirements

  • Python: 3.12+
  • Dependencies: sympy

Install via uv

We recommend using uv for fast environment management:

uv venv --python 3.12
source .venv/bin/activate
uv pip install -e .[dev]

📖 Usage

The calculator is designed to be used as a Python module. Below is a complete example of how to configure and run calculation.

Code layout

The core logic is organized by responsibility:

  • calculator.py: orchestrates the pipeline
  • parsers.py: builds symbols/mappings from inputs
  • compute.py: performs derivatives and numeric propagation
  • render.py: produces LaTeX output
  • _types.py: input dataclasses and type aliases
  • format.py / validation.py: shared helpers

1. Define the Equation

The equation can be defined using the Equation class. latex_name is the rendered result symbol, while expression is the internal symbolic formula written with variable names.

from uncertainty_calculator import Equation

# Define equation
equation = Equation(
    latex_name=r"\zeta",
    expression=r"(K*pi*eta*u*l)/(4*pi*phi*e_0*e_r)",
)

2. Define Variables

Variables can be defined using the Variable class for structured input.

from uncertainty_calculator import Variable

# Define variables
variables = [
    Variable(name="K", value=4.0, uncertainty=0.0, latex_name="K"),
    Variable(name="eta", value=0.9358e-3, uncertainty=5.773502691896258e-05, latex_name=r"\eta"),
    Variable(name="u", value=3.68e-5, uncertainty=0.11e-5, latex_name="u"),
    Variable(name="l", value=0.2256, uncertainty=0.0019, latex_name="l"),
    Variable(name="phi", value=100.0, uncertainty=0.5773502691896257, latex_name=r"\varphi"),
    Variable(name="e_0", value=8.8541878128e-12, uncertainty=0.0, latex_name=r"\varepsilon_0"),
    Variable(
        name="e_r", value=78.7, uncertainty=0.057735026918962574, latex_name=r"\varepsilon_\text{r}"
    ),
]

3. Configuration

Configure the output format, precision, and units.

from uncertainty_calculator import Digits

# Set digits of results
digits = Digits(mu=3, sigma=3)

# Set units of results
last_unit = r"\text{V}"  # Use None if dimensionless
# last_unit = None

# Print separately or integrally
separate = False

# Insert numbers or not
insert = False

# Include equation number or not
include_equation_number = True

4. Run the Calculator

Import the UncertaintyCalculator and execute the calculation.

from uncertainty_calculator import UncertaintyCalculator

calculator = UncertaintyCalculator(
    digits=digits,
    last_unit=last_unit,
    separate=separate,
    insert=insert,
    include_equation_number=include_equation_number,
)

latex_string = calculator.run(equation=equation, variables=variables)

print(latex_string)

5. Render the LaTeX String

from IPython.display import Latex

Latex(latex_string)

📊 Output Example

The tool generates LaTeX code that renders to standard physical chemistry calculation steps:

\begin{equation}
\begin{aligned}
\zeta&=\frac{K \eta l u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=0.111\ \text{V}\\
\\
\frac{\partial \zeta }{\partial \eta }&=\frac{K l u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=1.2 \times 10^{2}\\
\frac{\partial \zeta }{\partial u }&=\frac{K \eta l}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=3.0 \times 10^{3}\\
\frac{\partial \zeta }{\partial l }&=\frac{K \eta u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi}=0.49\\
\frac{\partial \zeta }{\partial \varphi }&=- \frac{K \eta l u}{4 \varepsilon_0 \varepsilon_\text{r} \varphi^{2}}=-0.0011\\
\frac{\partial \zeta }{\partial \varepsilon_\text{r} }&=- \frac{K \eta l u}{4 \varepsilon_0 \varepsilon_\text{r}^{2} \varphi}=-0.0014\\
\\
\sigma_{\zeta}&=\sqrt{\left(\frac{\partial \zeta }{\partial \eta } \sigma_{\eta}\right)^2+\left(\frac{\partial \zeta }{\partial u } \sigma_{u}\right)^2+\left(\frac{\partial \zeta }{\partial l } \sigma_{l}\right)^2+\left(\frac{\partial \zeta }{\partial \varphi } \sigma_{\varphi}\right)^2+\left(\frac{\partial \zeta }{\partial \varepsilon_\text{r} } \sigma_{\varepsilon_\text{r}}\right)^2}\\
&=\sqrt{\left(0.0069\right)^2+\left(0.0033\right)^2+\left(0.00094\right)^2+\left(-0.00064\right)^2+\left(-8.2 \times 10^{-5}\right)^2}\\
&=0.00773\ \text{V}\\
\\
\zeta&=\left (0.111 \pm 0.00773 \right )\ \text{V}
\end{aligned}
\end{equation}

Rendered results:

Rendered results

🙌 Acknowledgements

📄 License

This project is licensed under the MIT License.

Metadata

Release files for uncertainty-calculator 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for uncertainty-calculator 0.2.0
File Size Uploaded
uncertainty_calculator-0.2.0.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for uncertainty-calculator 0.2.0
File Interpreter ABI Platform
uncertainty_calculator-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.4 kB

Release files / uncertainty_calculator-0.2.0.tar.gz

Download URL uncertainty_calculator-0.2.0.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
1f45844ad1e2e080b899c1784fe229eac075d95030907cde91c55a0d3c0dd909
BLAKE2b-256 checksum
How to use checksums
dbaca873a49616b1fd8cbf4a5e186565f1b2d5a4f8a744a7dcb7eb86353f15e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 16, 2026.

Transparency log

Release files / uncertainty_calculator-0.2.0-py3-none-any.whl

Download URL uncertainty_calculator-0.2.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b82ce889f4389c17e9e9b0fc4b6ebd8507eecc338b7dc0fc02524ad90ea9431d
BLAKE2b-256 checksum
How to use checksums
f4f85d07df7ef2519553357b82a789be7b79d551eb4ec22237561caddbae4164
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 16, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page