Combining and Defining Unit#

Colab Open in Kaggle

Combining Example#

Units and quantities can be combined together using the regular Python numeric operators:

import brainunit as u
volt = u.meter2 * u.kilogram / (u.second3 * u.ampere)
volt == u.volt
True

Defining Units#

Users are free to define new units, either fundamental or compound, using the Unit.create and Unit.create_scaled_unit function:

Creating Basic Units#

First, we create some basic units, such as meters (metre) and seconds (second):

# Creating a basic unit: metre
metre = u.Unit.create(u.get_or_create_dimension(m=1), "metre", "m", base=10.)

# Creating a basic unit: second
second = u.Unit.create(u.get_or_create_dimension(s=1), "second", "s", base=10.)

metre, second
(Unit("m"), Unit("s"))

Creating Compound Units#

Next, we create a compound unit, such as volt(metre ^ 2 * kilogram / (second ^ 3 * ampere)):

volt = u.Unit.create(u.get_or_create_dimension(m=2, kg=1, s=-3, A=-1), "volt", "V")

volt
Unit("V")

In this example, we define the dimensions for the compound unit and create a new unit named “volt” with the specified dimensions.

Creating Scaled Units#

Finally, we create a scaled version of a basic unit, such as kilometers (kilometre):

kilometre = u.Unit.create_scaled_unit(metre, "k")

kilometre
Unit("km")
1 * kilometre / (1 * metre)
1000.0

Here, create_scaled_unit creates a new unit named “kilometre” by scaling the base unit “metre” with a scale factor of “k” (kilo).

The scale factor determines the prefix used for the unit, allowing for easy conversion between different scales of the same unit.