brainunit module#

Data Structures#

Quantity

A numerical value paired with a physical unit.

Unit

A physical unit.

Dimension

Store the exponents of the 7 basic SI unit dimensions.

Errors#

UnitMismatchError

Exception for operations with incompatible physical units.

DimensionMismatchError

Exception for operations with incompatible physical dimensions.

Constants#

DIMENSIONLESS

Store the exponents of the 7 basic SI unit dimensions.

UNITLESS

A physical unit.

Getters and Checkers#

is_dimensionless

Test if a value is dimensionless or not.

is_unitless

Test if a value is unitless or not.

is_scalar_type

Test whether obj is a scalar (0-d) numeric type.

get_dim

Return the dimension of any object that has them.

get_unit

Return the unit of any object that has them.

get_mantissa

Return the mantissa of a Quantity or a number.

get_magnitude

Return the mantissa of a Quantity or a number.

display_in_unit

Display a value in a certain unit with a given precision.

split_mantissa_unit

Split a Quantity into its mantissa and unit.

maybe_decimal

Convert a quantity to a plain number if it is dimensionless.

fail_for_dimension_mismatch

Compare the dimensions of two objects.

fail_for_unit_mismatch

Compare the units of two objects.

assert_quantity

Assert that a Quantity has a certain mantissa and unit.

have_same_dim

Test if two values have the same dimensions.

has_same_unit

Check whether two objects have the same unit.

unit_scale_align_to_first

Align the units of all arguments to the unit of the first argument.

array_with_unit

Create a new Quantity with the given unit.

Decorators#

check_dims

Decorator to check dimensions of arguments passed to a function

check_units

Decorator to check units of arguments passed to a function

assign_units

Decorator to transform units of arguments passed to a function and optionally assign units to the return value.

Dimension Utilities#

get_or_create_dimension

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

get_dim_for_display

Return a string representation of a dimension for display purposes.

add_standard_unit

Register a unit as a standard unit for display purposes.

Temperature Conversion#

celsius2kelvin

Convert a Celsius value to a kelvin Quantity.

kelvin2celsius

Convert a kelvin Quantity to a Celsius value.

Customized Array Interfaces#

Mathematical functions provided in brainunit are aware of CustomArray. You can define your own array-like class by inheriting from CustomArray and implementing the required methods. These customized arrays can seamlessly interact with the unit-aware mathematical functions.

CustomArray

A custom array wrapper providing comprehensive array operations and cross-framework compatibility.

Equinox Compatibility#

compatible_with_equinox

Enable or disable compatibility with the Equinox library.