ucon.integrations.numpy¶
NumPy array support via NumberArray. Install with pip install ucon[numpy].
NumPy array support for ucon.
This module provides NumberArray, a collection type for operating on multiple quantities of a given unit simultaneously.
Requires: pip install ucon[numpy]
Example
from ucon import units from ucon.numpy import NumberArray heights = NumberArray([1.7, 1.8, 1.9], unit=units.meter) heights.to(units.foot) <[5.577, 5.905, 6.233] ft>
NumberArray
¶
A collection of quantities with a shared unit.
Combines a numpy array of magnitudes with a unit, enabling vectorized arithmetic and conversion.
Parameters¶
quantities : array-like The numeric values (will be converted to numpy array). unit : Unit or UnitProduct, optional The unit for all quantities. Defaults to dimensionless. uncertainty : float or array-like, optional Uncertainty value(s). If scalar, applies uniformly to all elements. If array-like, must match the shape of quantities.
Examples¶
from ucon import units from ucon.numpy import NumberArray
Create from list:
heights = NumberArray([1.7, 1.8, 1.9], unit=units.meter) len(heights) 3
Vectorized conversion:
heights_ft = heights.to(units.foot) heights_ft[0].quantity # doctest: +ELLIPSIS 5.577...
With uniform uncertainty:
temps = NumberArray([20, 21, 22], unit=units.celsius, uncertainty=0.5)
With per-element uncertainty:
measurements = NumberArray([1.0, 2.0, 3.0], unit=units.meter, ... uncertainty=[0.01, 0.02, 0.015])
quantities
property
¶
The array of numeric values.
unit
property
¶
The unit shared by all quantities.
uncertainty
property
¶
The uncertainty (scalar or per-element array).
shape
property
¶
Shape of the quantities array.
ndim
property
¶
Number of dimensions.
dtype
property
¶
Data type of the quantities array.
dimension
property
¶
The physical dimension of the quantities.
__len__()
¶
Return the number of elements.
__getitem__(key)
¶
Index or slice the array.
Returns Number for scalar index, NumberArray for slice.
__iter__()
¶
Iterate as Number instances.
__repr__()
¶
String representation with truncation for large arrays.
__mul__(other)
¶
Multiply by scalar, Number, or NumberArray.
__rmul__(other)
¶
Right multiplication.
__truediv__(other)
¶
Divide by scalar, Number, or NumberArray.
__rtruediv__(other)
¶
Right division (other / self).
__add__(other)
¶
Add NumberArray or Number (same unit required).
__radd__(other)
¶
Right addition.
__sub__(other)
¶
Subtract NumberArray or Number (same unit required).
__rsub__(other)
¶
Right subtraction (other - self).
__neg__()
¶
Negation.
__pos__()
¶
Unary positive (returns copy).
__abs__()
¶
Absolute value.
__eq__(other)
¶
Element-wise equality comparison. Returns boolean array.
__ne__(other)
¶
Element-wise inequality comparison. Returns boolean array.
__lt__(other)
¶
Element-wise less-than comparison. Returns boolean array.
__le__(other)
¶
Element-wise less-than-or-equal comparison. Returns boolean array.
__gt__(other)
¶
Element-wise greater-than comparison. Returns boolean array.
__ge__(other)
¶
Element-wise greater-than-or-equal comparison. Returns boolean array.
to(target, graph=None)
¶
Convert all quantities to a different unit.
Parameters¶
target : Unit or UnitProduct The target unit to convert to. graph : ConversionGraph, optional The conversion graph to use. Defaults to the global default graph.
Returns¶
NumberArray A new NumberArray with converted quantities.
Examples¶
from ucon import units heights = NumberArray([1, 2, 3], unit=units.meter) heights_ft = heights.to(units.foot)
__array__(dtype=None)
¶
Support np.asarray(number_array).
sum()
¶
Sum all quantities.
mean()
¶
Compute the mean.
std(ddof=0)
¶
Compute the standard deviation.
min()
¶
Return the minimum value.
max()
¶
Return the maximum value.