Learning Scientific Programming with Python (2nd edition)

E8.1: The least well-determined physical constants

In this example we will use the scipy.constants.physical_constants dictionary to determine which are the least-accurately known constants. To do this we need the relative uncertainties in the constants' values; the code below uses a structured array to calculate these and outputs the least well-determined constants.

import numpy as np
from scipy.constants import physical_constants


def make_record(k, v):
    """
    Return the record for this constant from the key and value of its entry
    in the physical_constants dictionary.

    """
    name = k
    val, units, abs_unc = v
    # Calculate the relative uncertainty in ppm
    rel_unc = abs_unc / abs(val) * 1.0e6
    return name, val, units, abs_unc, rel_unc


dtype = [
    ("name", "S50"),
    ("val", "f8"),
    ("units", "S20"),
    ("abs_unc", "f8"),
    ("rel_unc", "f8"),
]
constants = np.array(
    [make_record(k, v) for k, v in physical_constants.items()], dtype=dtype
)
constants.sort(order="rel_unc")

# List the 10 constants with the largest relative uncertainties
for rec in constants[-10:]:
    print(
        f"{rec['rel_unc']:.0f} ppm: {rec['name'].decode():s} = {rec['val']:g} {rec['units'].decode():s}"
    )

The output is:

84 ppm: shielding difference of t and p in HT = 2.3945e-08
90 ppm: tau Compton wavelength over 2 pi = 1.11056e-16 m
90 ppm: tau mass energy equivalent in MeV = 1776.82 MeV
127 ppm: deuteron rms charge radius = 2.12778e-15 m
147 ppm: W to Z mass ratio = 0.88145
160 ppm: proton mag. shielding correction = 2.56715e-05
160 ppm: proton magn. shielding correction = 2.56715e-05
761 ppm: proton rms charge radius = 8.4075e-16 m
1031 ppm: weak mixing angle = 0.22305
1251 ppm: alpha particle rms charge radius = 1.6785e-15 m