get_or_create_dimension

get_or_create_dimension#

class saiunit.math.get_or_create_dimension(*args, **kwds)#

Create a new Dimension object or get a reference to an existing one.

This function maintains a singleton cache so that only one Dimension instance exists for each unique combination of exponents. This allows very efficient dimensionality checks using is.

Parameters:
  • args (sequence of float) – A sequence of 7 floats specifying the exponents of the SI base dimensions.

  • kwds (keyword arguments) – Keyword-value pairs where keywords are SI dimension names (e.g., length, mass, time) or SI unit names (e.g., m, kg, s).

Returns:

The (possibly cached) Dimension object.

Return type:

Dimension

Raises:

TypeError – If the positional argument is not a sequence of exactly 7 items, or if more than one positional argument is given.

Notes

The 7 SI base dimensions, in order, are:

  1. Length (m, metre)

  2. Mass (kg, kilogram)

  3. Time (s, second)

  4. Electric Current (A, ampere)

  5. Temperature (K, kelvin)

  6. Amount of Substance (mol, mole)

  7. Luminous Intensity (cd, candle)

Examples

The following are all equivalent definitions of the dimension of force:

>>> import saiunit as u
>>> u.get_or_create_dimension(length=1, mass=1, time=-2)
metre * kilogram * second ** -2
>>> u.get_or_create_dimension(m=1, kg=1, s=-2)
metre * kilogram * second ** -2
>>> u.get_or_create_dimension([1, 1, -2, 0, 0, 0, 0])
metre * kilogram * second ** -2