jmath.units package¶
Submodules¶
jmath.units.annotation module¶
Provides functions to allow for annotation of functions to enforce units.
- jmath.units.annotation.annotate(func)¶
Converts the units of a function to those specified in function annotations.
- Parameters
func (Callable) – The wrapped function to process.
- Return type
Callable
Examples
>>> from jmath import Unit, units >>> from jmath.units import si >>> # Setup custom units >>> gram = Unit("g") >>> units.define_conversion(si.kilogram, gram, 1000) >>> # Example function >>> # Expects mass in kilograms, returns energy in joules as annotated >>> @units.annotate ... def calc_energy(mass: si.kilogram) -> si.joules: ... print(mass) ... return mass/1e15 >>> input_mass = 3 * gram >>> print(input_mass) 3 [g] >>> energy = calc_energy(input_mass) 0.003 [kg] >>> print(energy) 0.27 [J]
jmath.units.conversion module¶
Unit Spaces. For management of unit conversions and aliases.
- class jmath.units.conversion.UnitSpace¶
Bases:
object
Management for units and unit conversions.
- alias(unit)¶
Produces the alias of a unit.
- convert(from_unit, to_unit)¶
Converts a unit to another unit if possible.
- Parameters
- Raises
NoConversion – Raises the no conversion error if there is no way to conver the unit.
- Return type
Optional[Unit]
- define_alias(base_unit, end_unit)¶
Defines an alias from unit to another.
- define_conversion(from_unit, to_unit, factor)¶
Defines the conversion between two units
- Parameters
from_unit (Unit) – The unit to convert from.
to_unit (Unit) – The unit to convert to.
factor (Union[float, Unit, Callable[[float], float]]) – The factor to multiply by or a function to produce the difference. If a factor is passed then the reverse conversion is added by default. If a function is passed then the reverse conversion is not added by default.
- jmath.units.conversion.define_alias(base_unit, end_unit)¶
Defines an alias from unit to another.
- jmath.units.conversion.define_conversion(from_unit, to_unit, factor)¶
Defines the conversion between two units
- Parameters
from_unit (Unit) – The unit to convert from.
to_unit (Unit) – The unit to convert to.
factor (Union[float, Unit, Callable[[float], float]]) – The factor to multiply by or a function to produce the difference. If a factor is passed then the reverse conversion is added by default. If a function is passed then the reverse conversion is not added by default.
jmath.units.other module¶
Defines a set of other useful units.
jmath.units.si module¶
Defines a set of SI units and their conversion factors.
Examples
>>> from jmath.units import si
>>> voltage_drop = Uncertainty(3, 0.2) * si.volt
>>> current = Uncertainty(0.1, 0.02) * si.ampere
>>> resistance = voltage_drop/current
>>> print(resistance)
(30 ± 8) [Ω]
jmath.units.units module¶
Defines the unit class and its behaviour
- class jmath.units.units.Unit(unit=None, unit_space=None)¶
Bases:
object
A value with an associated unit.
- Parameters
unit (str) – String identifier of unit e.g. “m”
unit_space (jmath.units.conversion.UnitSpace) – The unit space the unit exists in.
Examples
>>> print(3 * Unit("m")/Unit("s")) 3 [ms^(-1)]
- convert_to(other)¶
Converts the current unit to the given unit.
- Parameters
other (jmath.units.units.Unit) – The type of unit to convert to.
- copy(value=None, flip_powers=False)¶
Produces a copy of the unit.
- Parameters
value (Optional[float]) – Optional overwrite of value.
flip_powers – Optional flipping of powers.
- Return type
- union(other)¶
Constructs the union of two units. Ignores values.
- Parameters
other (jmath.units.units.Unit) – The other set to construct the union with.
- Return type
- property unit_space¶
The Unitspace in which the Unit Exists.
- unit_str()¶
Produces a string describing the unit.
- Return type
str
Module contents¶
Maths with Units