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 |
|
|
Convert and inspect |
|
|
Use a unit system |
|
Quantities interpreted in that system |
Work with arrays |
|
|
Propagate uncertainty |
|
|
Discover units |
|
Text or JSON listing |
Run a CLI-equivalent command |
|
Formatted |
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, andmaximum, 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"), andQuantity(value, uncertainty, "m")for scalar values.Quantity("3.048*m")for a PUEL expression.Quantity(numpy_array, "m")andQuantity(numpy_array, uncertainty_array, "m")for array data.Arithmetic and comparison operators, including
**.
Conversion and unit systems
Quantity.convert(units, system=None, quantity="")andQuantity.convert(Dformat, system=None).Quantity.unit_system(),Quantity.rebase_prefixes(), andQuantity.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)andQuantity.uncertainty(numpy=False).Quantity.units,Quantity.size(),Quantity.shape(), andQuantity.to_numpy().Quantity.to_string(format=None),Quantity.info(),str(quantity), andrepr(quantity).formats.Dformat.MKS,MGS,CGS, andFPS.
Uncertainty propagation
Quantity.set_propagation_mode("standard")or"interval".Quantity.correlate(left, right, coefficient)andQuantity.correlate_many(quantities, covariance).Quantity.monte_carlo(model, inputs, distributions=None, samples=100000, seed=0, coverage=0.95)and the returnedMonteCarloResultfieldsquantity,lower,upper,coverage, andsamples.
Evaluation and discovery
Calculator(expression),sqrt(quantity),minimum(left, right), andmaximum(left, right).listsfunctionsprefixes,base_units,derived_units,logarithmic_units,temperature_units,constants,quantities, andunit_systems.
Application command facade
scinumtools3.api.puq.PUQConvert(expression, target_units)withargument_input_system,argument_output_system,argument_output_quantity, andexecute() -> str.PUQEval(expression)with the conversion options plusargument_output_unitsandexecute() -> str.PUQInfo(expression)withargument_input_systemandexecute() -> str.PUQList(category="")withargument_systemandexecute() -> str.