Natural Unit

A natural unit represents a product of terms, each involving a distinct basis dimension

Context

_images/ditaa-853ff047fc0382fc817d8c1e8c10a1b1118dedc8.png

Introduction

#include <xo/unit/natural_unit.hpp>

Representation for the unit associated with a xquantity

  • represents a cartesian product of basis units.

  • constexpr implementation

  • limited support for fractional dimensions such as time^-1/2

object newton<<natural_unit>>
newton : n_bpu = 3
newton : bpu_v[]

object kg<<bpu>>
kg : native_dim = dim::mass
kg : scalefactor = 1000/1
kg : power = 1/1

object m<<bpu>>
m : native_dim = dim::distance
m : scalefactor = 1/1
m : power = 1/1

object s2<<bpu>>
s2 : native_dim = dim::time
s2 : scalefactor = 1/1
s2 : power = -2/1

newton o-- kg
newton o-- m
newton o-- s2

natural unit for a Newton (unit of force)

Class

template<typename Int>
class natural_unit

an array representing the cartesian product of distinct basis-power-units

  1. Quantities are represented as a multiple of a natural unit

  2. Each bpu in the array represents a power of a basis dimension, e.g. “meter” or “second^2”.

  3. Each bpu in an array has a different dimension id. For example dim::time, if present, appears once.

  4. Basis dimensions can appear in any order. Order used for constructing abbreviations: will get "kg.m" or "m.kg" depending on the ordering of dim::distance and dim::mass in bpu_v_

Int supplies representation for numerator and denominator in basis-unit scale factors.

Member Variables

group Natural-unit-instance-vars

Variables

std::size_t n_bpu_

the number of occupied slots in bpu_v_

bpu<Int> bpu_v_[n_dim]

storage for basis power units

Type Traits

group natural unit type traits

Typedefs

using ratio_int_type = Int

representation for numerator and denominator of scalefactor ratios

Constructors

group Natural-unit-ctors

Functions

inline constexpr natural_unit()

construct dimensionless unit

static inline natural_unit from_bu(basis_unit bu, power_ratio_type power = power_ratio_type(1))

construct unit representing basis unit bu with exponent power

Access Methods

group Natural-unit-access-methods

Functions

inline bool is_natural() const

always true. Provided for symmetry with xo::qty::scaled_unit::is_natural

inline std::size_t n_bpu() const

get member n_bpu

inline bool is_dimensionless() const

true if this unit has no dimension

inline bpu<Int> *bpu_v() const

get address of member bpu_v

inline bpu<Int> lookup_dim(dimension d) const

get bpu for dimension d. if d isn’t present, construct bpu with 0 power

inline bpu<Int> &operator[](std::size_t i)

get element i of bpu_v_

inline const bpu<Int> &operator[](std::size_t i) const

get element i of bpu_v_ (const version)

General Methods

group Natural-unit-methods

Functions

inline natural_unit reciprocal() const

construct reciprocal of this unit.

For example reciprocal of a newton (abbreviation "kg.m.s^-2") is a unit with abbreviation "kg^-1.m^-1.s^2"

inline nu_abbrev_type abbrev() const

abbreviation for this unit.

Apply as suffix when printing quantities involving this unit.

For example "mm" for millimeters, or "ns" for nanoseconds

inline void remove_bpu(size_t p)

remove bpu at position p

inline void push_back(const bpu<Int> &bpu)

append bpu to this unit in-place

Require bpu.native_dim does not match any existing member of bpu_v_

Conversion

group Natural-unit-conversion-methods

Functions

template<typename Int2>
inline natural_unit<Int2> to_repr() const

convert to equivalent unit using scalefactor representation Int2 instead of ratio_int_type

Comparison Functions

group natural-unit comparison functions

Functions

template<typename Int>
bool operator==(const natural_unit<Int> &x, const natural_unit<Int> &y)

compare natural units x, y for equality.

template<typename Int>
bool operator!=(const natural_unit<Int> &x, const natural_unit<Int> &y)

compare natural units x, y for inequality