Sign in

    #Dimension Core

    core/dimension defines the runtime dimension model used by LunarUnits. Dimensions are multiplicative symbolic values built on top of core/algebra.

    This package models dimension identity only. Unit scale factors, symbols and quantity values live in core/unit and core/quantity.

    #Main API

    • SI base dimensions: length, mass, time, electric_current, temperature, amount_of_substance, luminous_intensity.
    • dimensionless() for scalar quantities.
    • custom(symbol) for extension dimensions such as angle, information, currency and count.
    • mul, div and pow for derived dimensions.
    • is_same and is_dimensionless for runtime checks.

    let velocity = @dimension.Dimension::length() / @dimension.Dimension::time()
    let force = @dimension.Dimension::mass() * velocity / @dimension.Dimension::time()

    Use custom in extension packages rather than adding new base dimensions to the core.

    Dimension

    A physical dimension expressed over the seven SI base dimensions: length (L), mass (M), time (T), electric current (I), thermodynamic temperature (Θ), amount of substance (N) and luminous intensity (J).

    A Dimension reuses the algebra layer's Monomial: the symbols are the base-dimension tags and the coefficient is always 1 (dimensions carry no scale). Two dimensions are equal exactly when their exponent vectors match, which is the foundation of dimensional checking in the quantity layer.

    Example

    test {
    // Velocity has dimension length / time.
    let velocity = Dimension::length().div(Dimension::time())
    let acceleration = velocity.div(Dimension::time())
    assert_eq(acceleration, Dimension::length().div(Dimension::time().pow(2)))
    }
    impl Div for Dimension
    impl Mul for Dimension
    impl Show for Dimension

    Dimension::amount_of_substance

    fn Dimension::amount_of_substance() -> Dimension

    The amount-of-substance dimension (N).

    Example

    test {
    assert_false(Dimension::amount_of_substance() == Dimension::length())
    }

    Dimension::custom

    fn Dimension::custom(symbol : String) -> Dimension

    Builds an extension dimension from a caller-owned symbol.

    This is intended for domain packages that need dimensions outside the seven SI base dimensions, while keeping the core dimension package independent from those domains.

    Example

    test {
    let information = Dimension::custom("Info")
    assert_true(information.is_same(Dimension::custom("Info")))
    assert_false(information.is_same(Dimension::length()))
    }

    Dimension::dimensionless

    fn Dimension::dimensionless() -> Dimension

    The dimensionless quantity (no base dimensions).

    Example

    test {
    assert_true(Dimension::dimensionless().is_dimensionless())
    // A ratio of like dimensions is dimensionless.
    assert_eq(
    Dimension::length().div(Dimension::length()),
    Dimension::dimensionless(),
    )
    }

    Dimension::div

    fn Dimension::div(self : Dimension, other : Dimension) -> Dimension

    Divides one dimension by another (subtracts exponent vectors).

    Example

    test {
    // Velocity is length / time; multiplying back by time gives length.
    let v = Dimension::length().div(Dimension::time())
    assert_eq(v.mul(Dimension::time()), Dimension::length())
    }

    Dimension::electric_current

    fn Dimension::electric_current() -> Dimension

    The electric current dimension (I).

    Example

    test {
    // Electric charge has dimension current * time.
    let charge = Dimension::electric_current().mul(Dimension::time())
    assert_false(charge.is_dimensionless())
    }

    Dimension::is_dimensionless

    fn Dimension::is_dimensionless(self : Dimension) -> Bool

    Returns whether this is the dimensionless quantity.

    Example

    test {
    assert_true(Dimension::length().div(Dimension::length()).is_dimensionless())
    assert_false(Dimension::length().is_dimensionless())
    }

    Dimension::is_same

    fn Dimension::is_same(self : Dimension, other : Dimension) -> Bool

    Returns whether two dimensions are the same physical dimension. This is the check the quantity layer uses to allow addition and subtraction.

    Example

    test {
    // Two ways of building the force dimension agree.
    let f1 = Dimension::mass()
    .mul(Dimension::length())
    .div(Dimension::time().pow(2))
    let f2 = Dimension::mass().mul(
    Dimension::length().div(Dimension::time().pow(2)),
    )
    assert_true(f1.is_same(f2))
    }

    Dimension::length

    fn Dimension::length() -> Dimension

    The length dimension (L).

    Example

    test {
    assert_false(Dimension::length() == Dimension::mass())
    }

    Dimension::luminous_intensity

    fn Dimension::luminous_intensity() -> Dimension

    The luminous-intensity dimension (J).

    Example

    test {
    assert_false(Dimension::luminous_intensity() == Dimension::time())
    }

    Dimension::mass

    fn Dimension::mass() -> Dimension

    The mass dimension (M).

    Example

    test {
    // Momentum has dimension mass * length / time.
    let momentum = Dimension::mass()
    .mul(Dimension::length())
    .div(Dimension::time())
    assert_false(momentum.is_dimensionless())
    }

    Dimension::mul

    fn Dimension::mul(self : Dimension, other : Dimension) -> Dimension

    Multiplies two dimensions (adds their exponent vectors).

    Example

    test {
    // Area is length * length.
    assert_eq(
    Dimension::length().mul(Dimension::length()),
    Dimension::length().pow(2),
    )
    }

    Dimension::pow

    fn Dimension::pow(self : Dimension, n : Int) -> Dimension

    Raises a dimension to an integer power.

    Example

    test {
    // Volume is length^3.
    let volume = Dimension::length().pow(3)
    assert_eq(
    volume,
    Dimension::length().mul(Dimension::length()).mul(Dimension::length()),
    )
    }

    Dimension::temperature

    fn Dimension::temperature() -> Dimension

    The thermodynamic temperature dimension (Θ).

    Example

    test {
    assert_false(Dimension::temperature() == Dimension::mass())
    }

    Dimension::time

    fn Dimension::time() -> Dimension

    The time dimension (T).

    Example

    test {
    // Frequency has dimension 1 / time.
    let frequency = Dimension::dimensionless().div(Dimension::time())
    assert_eq(frequency, Dimension::time().pow(-1))
    }

    Source Files