pyxx.units.Unit#
- class pyxx.units.Unit(unit_system: UnitSystem, base_unit_exps: List[float] | Tuple[float, ...] | ndarray, to_base_function: Callable[[ndarray, float], ndarray], from_base_function: Callable[[ndarray, float], ndarray], identifier: str | None = None, name: str | None = None)#
Bases:
objectBase class for representing a unit
This class can be used to represent an arbitrary unit that is part of a given system of units. The attributes of this class specify how the unit relates to the base units of the system of units.
Examples
For examples, refer to the Units 1: Basics page.
Methods
__init__(unit_system, base_unit_exps, ...[, ...])Creates an instance of the
Unitclassconvert(value, convert_type, unit)Converts a quantity from one unit to another
from_base(value[, exponent])Converts a value or array from base units of the unit system to the given unit
is_convertible(unit)Checks whether a unit can be converted to another unit
to_base(value[, exponent])Converts a value or array from the given unit to the base units of the unit system
Attributes
Defines the mathematical conventions for multiplying or dividing a
Unitobject by a constantA list of exponents relating the given object's units to the base units of
unit_systemA function that transforms a value in the base units of the system of units
unit_systemto the given object's unitsA user-defined string that represents the unit (examples: kg, m, rad)
A user-defined string that describes the unit (examples: kilogram, meter, radian)
A function that transforms a value from the given object's units to the base units of
unit_systemThe system of units to which the unit belongs
- CONSTANT_MATH_CONVENTION = 1#
Defines the mathematical conventions for multiplying or dividing a
Unitobject by a constantConfigures whether
Unitobjects can be generated by multiplying or dividing anotherUnitobject by a constant, and if so what mathematical conventions are adopted. For more detail, refer to theConstantUnitMathConventionsdocumentation.The default is
ConstantUnitMathConventions.DISABLE, which prohibits multiplying or dividingUnitobjects by constants.Warning
This is a class attribute, so changing its value for one class (such as
UnitLinear) will change it for ALL classes that are an instance of or inherit fromUnit.Notes
To select a math convention for multiplying
Unitobjects by constants, include code similar to:>>> from pyxx.units import Unit, ConstantUnitMathConventions >>> Unit.CONSTANT_MATH_CONVENTION = ConstantUnitMathConventions.DISABLE
In general, it is best to set this option at the beginning of your code, as it can get confusing if you set it later in your code and as a result different parts of your code follow different conventions.
- __init__(unit_system: UnitSystem, base_unit_exps: List[float] | Tuple[float, ...] | ndarray, to_base_function: Callable[[ndarray, float], ndarray], from_base_function: Callable[[ndarray, float], ndarray], identifier: str | None = None, name: str | None = None)#
Creates an instance of the
UnitclassDefines an object representing a base or derived unit that is part of a given system of units.
- Parameters:
unit_system (UnitSystem) – The system of units to which the unit belongs
base_unit_exps (list or tuple or np.ndarray) – A 1D list of exponents relating the given object’s unit to the base units of
unit systemto_base_function (Callable) – A function that transforms a value in the given object’s unit to the base units of
unit_systemfrom_base_function (Callable) – A function that transforms a value in the base units of
unit_systemto the given object’s unitidentifier (str, optional) – A short identifier describing the unit (example:
'kg') (default isNone)name (str, optional) – A name describing the unit (example:
'kilogram') (default isNone)
- property base_unit_exps: ndarray#
A list of exponents relating the given object’s units to the base units of
unit_system
- property identifier: str | None#
A user-defined string that represents the unit (examples: kg, m, rad)
- property from_base_function#
A function that transforms a value in the base units of the system of units
unit_systemto the given object’s units
- property name: str | None#
A user-defined string that describes the unit (examples: kilogram, meter, radian)
- property to_base_function#
A function that transforms a value from the given object’s units to the base units of
unit_system
- property unit_system: UnitSystem#
The system of units to which the unit belongs
- convert(value: ndarray | list | tuple | float, convert_type: str, unit: Unit) ndarray#
Converts a quantity from one unit to another
This method performs a unit conversion, converting one or more values from this object’s units to another unit. This conversion can be performed in “either direction” – either from this object’s units to another unit, or from another unit to this object’s units.
- Parameters:
value (np.ndarray or list or tuple or float) – Quantities to convert to a different unit
convert_type (str) – Must be either
'to'or'from'. Describes whether to convertvaluefrom this object’s units to the units specified byunit, or vice versaunit (Unit) – The unit to convert
valueto or from
- Returns:
NumPy array of the same shape as
valuecontaining the quantities after performing the specified unit conversion- Return type:
np.ndarray
- is_convertible(unit: Unit) bool#
Checks whether a unit can be converted to another unit
Checks two units can be converted between each other (i.e., whether they belong to the same system of units and have the same
base_unit_expsrelating them to the base units).- Parameters:
- Returns:
Returns
Trueif this unit instance andunitbelong to the same system of units and have the samebase_unit_expsattribute, andFalseotherwise- Return type:
bool
- from_base(value: ndarray | list | tuple | float, exponent: float = 1.0) ndarray#
Converts a value or array from base units of the unit system to the given unit
- Parameters:
value (np.ndarray or list or tuple or float) – Value(s) to convert to base units
exponent (float, optional) – Exponent to which the unit is raised (default is 1.0)
- Returns:
NumPy array with the same shape as
valuecontaining the value(s) invalueexpressed in base units- Return type:
np.ndarray
Notes
Use the
exponentargument to handle units which are raised to a power. For instance, to convert square kilometers to base units (square meters), setexponentto 2.
- to_base(value: ndarray | list | tuple | float, exponent: float = 1.0) ndarray#
Converts a value or array from the given unit to the base units of the unit system
- Parameters:
value (np.ndarray or list or tuple or float) – Value(s) to convert from base units to the given unit
exponent (float, optional) – Exponent to which the unit is raised (default is 1.0)
- Returns:
NumPy array with the same shape as
valuecontaining the value(s) invalueconverted from base units to the given unit- Return type:
np.ndarray
Notes
Use the
exponentargument to handle units which are raised to a power. For instance, to convert to cubic kilometers from cubic meters (the base unit), setexponentto 3.