unxt.unitsystems#
Systems of units.
A unit system is a collection of units that are used together. In a unit system there are base units and derived units. Base units are the fundamental units of the system and derived units are constructed from the base units. For example, in the SI system, the base units are the meter, kilogram, second, ampere, kelvin, mole, and candela. Derived units are constructed from these base units, for example, the newton is a derived unit of force.
unxt provides powerful tools for defining and working with unit systems. Unit systems can be statically defined (useful for many tools and development environments) or dynamically defined (useful for interactive environments and Python’s general dynamism). Unit systems can be extended, compared, and used for decomposing units on quantities. There are many more features and tools for working with unit systems in unxt.
- unxt.unitsystems.unitsystem(usys: AbstractUnitSystem, /)#
Convert a UnitSystem to a UnitSystem.
Examples
>>> from unxt.unitsystems import unitsystem >>> usys = unitsystem("kpc", "Myr", "Msun", "radian") >>> usys unitsystem(kpc, Myr, solMass, rad)
>>> unitsystem(usys) is usys True
- unxt.unitsystems.unitsystem(seq: Sequence[Any], /) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Convert a UnitSystem or tuple of arguments to a UnitSystem.
Examples
>>> import unxt as u
>>> u.unitsystem(()) DimensionlessUnitSystem()
>>> u.unitsystem(("kpc", "Myr", "Msun", "radian")) unitsystem(kpc, Myr, solMass, rad)
>>> u.unitsystem(["kpc", "Myr", "Msun", "radian"]) unitsystem(kpc, Myr, solMass, rad)
- unxt.unitsystems.unitsystem(_: NoneType, /) DimensionlessUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Dimensionless unit system from None.
Examples
>>> from unxt.unitsystems import unitsystem >>> unitsystem(None) DimensionlessUnitSystem()
- unxt.unitsystems.unitsystem(*args: Any) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Convert a set of arguments to a UnitSystem.
Examples
>>> from unxt.unitsystems import unitsystem
>>> unitsystem("kpc", "Myr", "Msun", "radian") unitsystem(kpc, Myr, solMass, rad)
- unxt.unitsystems.unitsystem(name: str, /) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Return unit system from name.
Examples
>>> from unxt.unitsystems import unitsystem >>> unitsystem("galactic") unitsystem(kpc, Myr, solMass, rad)
>>> unitsystem("solarsystem") unitsystem(AU, yr, solMass, rad)
>>> unitsystem("dimensionless") DimensionlessUnitSystem()
- unxt.unitsystems.unitsystem(usys: AbstractUnitSystem, *args: Any) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Create a unit system from an existing unit system and additional units.
Examples
We can add a new unit definition to an existing unit system:
>>> from unxt.unitsystems import unitsystem >>> usys = unitsystem("galactic") >>> unitsystem(usys, "km/s") LengthTimeMassAngleSpeedUnitSystem(length=Unit("kpc"), time=Unit("Myr"), mass=Unit("solMass"), angle=Unit("rad"), speed=Unit("km / s"))
We can also override the base unit of an existing unit system:
>>> new_usys = unitsystem(usys, "pc") >>> new_usys TimeMassAngleLengthUnitSystem(time=Unit("Myr"), mass=Unit("solMass"), angle=Unit("rad"), length=Unit("pc"))
- unxt.unitsystems.unitsystem(flag: type[AbstractUSysFlag], *_: Any) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Raise an exception since the flag is abstract.
- unxt.unitsystems.unitsystem(flag: type[StandardUSysFlag], *args: Any) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Create a standard unit system using the inputted units.
Examples
>>> from unxt import unitsystem, unitsystems >>> unitsystem(unitsystems.StandardUSysFlag, "kpc", "Myr", "Msun") LengthTimeMassUnitSystem(length=Unit("kpc"), time=Unit("Myr"), mass=Unit("solMass"))
- unxt.unitsystems.unitsystem(flag: type[DynamicalSimUSysFlag], *args: Any, G: float | int = 1.0) AbstractUnitSystem
- Parameters:
usys (AbstractUnitSystem)
- Return type:
Make a dynamical unit system.
Examples
>>> from unxt.unitsystems import unitsystem, DynamicalSimUSysFlag
>>> unitsystem(DynamicalSimUSysFlag, "m", "kg") LengthMassTimeUnitSystem(length=Unit("m"), mass=Unit("kg"), time=Unit("122404 s"))
- Parameters:
usys (
AbstractUnitSystem)- Return type:
- unxt.unitsystems.unitsystem_of(obj: Any, /)#
Return the unit system of an object.
- unxt.unitsystems.unitsystem_of(obj: Any, /) DimensionlessUnitSystem
Return the unit system of the object.
Examples
>>> from unxt.unitsystems import unitsystem_of
>>> unitsystem_of(1) DimensionlessUnitSystem()
- unxt.unitsystems.unitsystem_of(obj: AbstractUnitSystem, /) AbstractUnitSystem
Return the unit system from the unit system.
Examples
>>> from unxt.unitsystems import galactic, unitsystem_of
>>> unitsystem_of(galactic) is galactic True
- class unxt.unitsystems.AbstractUnitSystem#
Bases:
objectRepresents a system of units.
This class behaves like a dictionary with keys set by physical types (i.e. “length”, “velocity”, “energy”, etc.). If a unit for a particular physical type is not specified on creation, a composite unit will be created with the base units. See the examples below for some demonstrations.
Examples
If only base units are specified, any physical type specified as a key to this object will be composed out of the base units:
>>> from unxt import unitsystem >>> usys = unitsystem("m", "s", "kg", "radian") >>> usys unitsystem(m, s, kg, rad)
>>> usys["velocity"] Unit("m / s")
This unit system defines energy:
>>> usys = unitsystem("m", "s", "kg", "radian", "erg") >>> usys["energy"] Unit("erg")
This is useful for Galactic dynamics where lengths and times are usually given in terms of
kpcandMyr, but velocities are often specified inkm/s:>>> usys = unitsystem("kpc", "Myr", "Msun", "radian", "km / s") >>> usys["velocity"] Unit("km / s")
Unit systems can be hashed:
>>> isinstance(hash(usys), int) True
And iterated over:
>>> [x for x in usys] [Unit("kpc"), Unit("Myr"), Unit("solMass"), Unit("rad"), Unit("km / s")]
With length equal to the number of base units
>>> len(usys) 5
- property base_dimensions: tuple[PhysicalType, ...]#
Dimensions required for the unit system.
- property base_units: tuple[Unit | UnitBase | CompositeUnit, ...]#
List of core units.
- class unxt.unitsystems.DimensionlessUnitSystem(dimensionless: Annotated[UnitBase, PhysicalType('dimensionless')] = Unit(dimensionless))#
Bases:
SingletonMixin,AbstractUnitSystemA unit system with only dimensionless units.
This is a singleton class.
Examples
>>> from unxt.unitsystems import DimensionlessUnitSystem >>> dims1 = DimensionlessUnitSystem() >>> dims2 = DimensionlessUnitSystem() >>> dims1 is dims2 True
- Parameters:
dimensionless (
UnitBase)
- property base_dimensions: tuple[PhysicalType, ...]#
Dimensions required for the unit system.
- property base_units: tuple[Unit | UnitBase | CompositeUnit, ...]#
List of core units.
- class unxt.unitsystems.LTMAUnitSystem(length: Annotated[UnitBase, PhysicalType('length')], time: Annotated[UnitBase, PhysicalType('time')], mass: Annotated[UnitBase, PhysicalType('mass')], angle: Annotated[UnitBase, PhysicalType('angle')])#
Bases:
AbstractUnitSystemLength, time, mass, angle unit system.
- property base_dimensions: tuple[PhysicalType, ...]#
Dimensions required for the unit system.
- property base_units: tuple[Unit | UnitBase | CompositeUnit, ...]#
List of core units.
- class unxt.unitsystems.AbstractUSysFlag(*_, **__)#
Bases:
objectAbstract class for unit system flags to provide dispatch control.
Unit system flags are used to indicate the type of unit system being defined. They are not intended to be instantiated and are used to provide dispatch control for defining unit systems.
- Raises:
ValueError – If an attempt is made to instantiate a unit system flag class.
- Parameters:
See also
NoneFunction to define a unit system. The AbstractUSysFlag can be provided as the first argument to indicate the type of unit system being defined. This is useful for multiple-dispatching to the correct constructor.
Examples
For this example we will use the unxt.unitsystems.StandardUSysFlag, which indicates the standard construction of a unit system directly from the set of base units. Flags like unxt.unitsystems.DynamicalSimUSysFlag can be used to create a unit system where the gravitational constant
Gis unit-less and units are defined accordingly.>>> import unxt
Define a unit system with the standard flag:
>>> unxt.unitsystem(unxt.unitsystems.StandardUSysFlag, "m", "kg", "s") LengthMassTimeUnitSystem(length=Unit("m"), mass=Unit("kg"), time=Unit("s"))
- class unxt.unitsystems.StandardUSysFlag(*_, **__)#
Bases:
AbstractUSysFlagFlag to indicate a standard unit system with no additional arguments.
Examples
>>> from unxt.unitsystems import unitsystem, StandardUSysFlag
Define a unit system with the standard flag:
>>> unitsystem(StandardUSysFlag, "m", "kg", "s") LengthMassTimeUnitSystem(length=Unit("m"), mass=Unit("kg"), time=Unit("s"))
Further examples may be found in the
unitsystemdocs.
- class unxt.unitsystems.DynamicalSimUSysFlag(*_, **__)#
Bases:
AbstractUSysFlagFlag to indicate a unit system with optional definition of G.
Examples
>>> from unxt.unitsystems import unitsystem, DynamicalSimUSysFlag
Define a unit system with the dynamical simulation flag:
>>> unitsystem(DynamicalSimUSysFlag, "m", "kg") LengthMassTimeUnitSystem(length=Unit("m"), mass=Unit("kg"), time=Unit("122404 s"))
Further examples may be found in the
unitsystemdocs.
- unxt.unitsystems.equivalent(a: AbstractUnitSystem, b: AbstractUnitSystem, /)#
Check if two unit systems are equivalent.
- Parameters:
- Return type: