Python

The scinumtools3.puq module exposes the native PUQ object model. Quantities keep their units and optional uncertainty through arithmetic and conversion.

Basic examples

Convert a measured length to a display unit:

from scinumtools3.puq import Quantity

length = Quantity(2.5, 0.01, "m")
print(length.convert("cm"))  # 2.500(10)e2*cm

Evaluate a calculation from text:

from scinumtools3.puq import Calculator

speed = Calculator("12*km / 3*h")
print(speed.convert("m/s"))  # 1.11111*m*s-1

Common workflows

Task

Call

Result

Evaluate an expression

Calculator(expression)

Quantity

Convert and inspect

quantity.convert(units); value(), units, uncertainty()

Quantity; scalar or text

Use a unit system

with UnitSystem(system): ...

Quantities interpreted in that system

Work with arrays

Quantity(array, uncertainty, units).to_numpy()

numpy.ndarray

Propagate uncertainty

Quantity.monte_carlo(model, inputs, ...)

MonteCarloResult

Discover units

lists.derived_units() or another lists function

Text or JSON listing

Run a CLI-equivalent command

scinumtools3.api.puq.PUQConvert(...).execute()

Formatted str

from scinumtools3.api.puq import PUQConvert
from scinumtools3.puq import Calculator, Quantity, UnitSystem
from scinumtools3.puq.systems import US

length = Calculator("3.048*m")             # Quantity
length_cm = length.convert("cm")            # Quantity
print(length_cm.value(), length_cm.units)   # scalar, text

with UnitSystem(US):
    speed = Quantity("60*mi/h").convert("km/h")  # Quantity
print(speed.to_string())

print(PUQConvert("2.5*m", "cm").execute())      # "250*cm"

Unit-system context

Use a system constant from scinumtools3.puq.systems when a unit or conversion needs an explicit interpretation:

from scinumtools3.puq import Quantity, UnitSystem
from scinumtools3.puq.systems import US

with UnitSystem(US):
    speed = Quantity("60*mi/h").convert("km/h")
    print(speed.to_string())

Arrays and uncertainties

Quantities may carry scalar or array values and an optional uncertainty. Use to_numpy() when passing data to NumPy-based code:

from scinumtools3.puq import Quantity

values = Quantity([1.0, 2.0, 3.0], "m")
print(values.to_numpy())

Less common entry points

Evaluation, uncertainty sampling, and unit listings use the same public API:

from scinumtools3.puq import Calculator, Quantity, lists

energy = Calculator("23*kg*m2/s2")
result = Quantity.monte_carlo(
    lambda length, width: length * width,
    [Quantity(2.0, 0.1, "m"), Quantity(3.0, 0.2, "m")],
    seed=42,
)
print(result.quantity, result.lower, result.upper)
print(lists.derived_units())

See the Python binding compatibility guide for shared setup and additional examples, and PUQ Python API for complete generated API documentation.

Current limitations and differences

Compared with the canonical C++ implementation:

  • Python values, lists, and NumPy arrays replace C++ VAL storage types. to_numpy() extracts numerical estimates and drops units; implicit conversion to a plain NumPy array is disabled.

  • The exported mathematical helpers are sqrt, minimum, and maximum, together with supported NumPy operations. The complete C++ mathematical and expression-solver extension APIs are not wrapped.

  • Monte Carlo inputs must be scalar, as in C++. See the native quantity guide for distribution and correlation restrictions; Python does not relax them.

Binding reference

This is a lookup index, not a sequence to read from top to bottom. Use Common workflows above to choose a task. The groups below then give the exact Python names for that task. Names in monospace are callable names or properties; parentheses show arguments, and -> shows a result where it is not obvious. The linked API reference documents complete signatures and fields.

Construction and arithmetic

  • Quantity(value), Quantity(value, "m"), and Quantity(value, uncertainty, "m") for scalar values.

  • Quantity("3.048*m") for a PUEL expression.

  • Quantity(numpy_array, "m") and Quantity(numpy_array, uncertainty_array, "m") for array data.

  • Arithmetic and comparison operators, including **.

Conversion and unit systems

  • Quantity.convert(units, system=None, quantity="") and Quantity.convert(Dformat, system=None).

  • Quantity.unit_system(), Quantity.rebase_prefixes(), and Quantity.rebase_dimensions().

  • UnitSystem(system) as a context manager; systems.SI, systems.US, systems.IU, and the other exported system constants.

Values, arrays, and formatting

  • Quantity.value(numpy=False) and Quantity.uncertainty(numpy=False).

  • Quantity.units, Quantity.size(), Quantity.shape(), and Quantity.to_numpy().

  • Quantity.to_string(format=None), Quantity.info(), str(quantity), and repr(quantity).

  • formats.Dformat.MKS, MGS, CGS, and FPS.

Uncertainty propagation

  • Quantity.set_propagation_mode("standard") or "interval".

  • Quantity.correlate(left, right, coefficient) and Quantity.correlate_many(quantities, covariance).

  • Quantity.monte_carlo(model, inputs, distributions=None, samples=100000, seed=0, coverage=0.95) and the returned MonteCarloResult fields quantity, lower, upper, coverage, and samples.

Evaluation and discovery

  • Calculator(expression), sqrt(quantity), minimum(left, right), and maximum(left, right).

  • lists functions prefixes, base_units, derived_units, logarithmic_units, temperature_units, constants, quantities, and unit_systems.

Application command facade

  • scinumtools3.api.puq.PUQConvert(expression, target_units) with argument_input_system, argument_output_system, argument_output_quantity, and execute() -> str.

  • PUQEval(expression) with the conversion options plus argument_output_units and execute() -> str.

  • PUQInfo(expression) with argument_input_system and execute() -> str.

  • PUQList(category="") with argument_system and execute() -> str.