Skip to content

Commit

Permalink
rename to nutils_SI
Browse files Browse the repository at this point in the history
  • Loading branch information
gertjanvanzwieten committed Jan 19, 2023
1 parent d824e72 commit a2d6d7e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ The SI module is most conveniently installed using Python's pip installer:
Alternatively the package can be installed from source by calling `python -m
pip install .` (note the dot) from inside the source directory.

The module will be installed in the nutils namespace, alongside other
components of the Nutils suite, from where it should be imortable as `SI`:
After installation the module is imortable as `nutils_SI`:

>>> from nutils import SI
>>> from nutils_SI import parse, Velocity, Length, Time, Force


## Usage
Expand All @@ -27,7 +26,7 @@ The SI module defines all base units and derived units of the International
System of Units (SI) plus an Angle dimension, as well as the full set of metric
prefixes. Dimensional values are generated primarily by parsing a string value.

>>> v = SI.parse('7μN*5h/6g')
>>> v = parse('7μN*5h/6g')

The parser recognizes the multiplication (\*) and division (/) operators to
separate factors. Every factor can be prefixed with a scale and suffixed with a
Expand All @@ -39,28 +38,28 @@ which is a subtype of Quantity that stores the powers L=1 and T=-1. Many
subtypes are readily defined by their physical names; others can be created
through manipulation.

>>> type(v) == SI.Velocity == SI.Length / SI.Time
>>> type(v) == Velocity == Length / Time
True

While `parse` can instantiate any subtype of Quantity, we could have created
the same object by instantiating Velocity directly, which has the advantage of
verifying that the specified quantity is indeed of the desired dimension.

>>> w = SI.Velocity('8km')
>>> w = Velocity('8km')
Traceback (most recent call last):
...
TypeError: expected [L/T], got [L]

Explicit subtypes can also be used in function annotations:

>>> def f(size: SI.Length, load: SI.Force): pass
>>> def f(size: Length, load: Force): pass

The Quantity types act as an opaque container. As long as a quantity has a
physical dimension, its value is inaccessible. The value can only be retrieved
by dividing out a reference quantity, so that the result becomes dimensionless
and the Quantity wrapper falls away.

>>> v / SI.parse('m/s')
>>> v / parse('m/s')
21.0

To simplify the fairly common situation that the reference quantity is a unit
Expand All @@ -83,7 +82,7 @@ idiomatic way is to rely on multiplication so as not to depend on the specifics
of the internal reference system.

>>> import numpy
>>> F = numpy.array([1,2,3]) * SI.parse('N')
>>> F = numpy.array([1,2,3]) * parse('N')

For convenience, Quantity objects define the shape, ndim and size attributes.
Beyond this, however, no Numpy specific methods or attributes are defined.
Expand All @@ -103,30 +102,31 @@ might be desirable to add an angular dimension. This is done by creating a new
Dimension instance, using a symbol that avoids the existing symbols T, L, M, I,
Θ, N, J and A:

>>> Xonon = SI.Dimension.create('X')
>>> from nutils_SI import Dimension, units
>>> Xonon = Dimension.create('X')

At this point, the dimension is not very useful yet as it lacks units. To
rectify this we define the unit yam as being internally represented by a unit
value:

>>> SI.units.yam = Xonon.reference_quantity
>>> units.yam = Xonon.reference_quantity

Additional units can be defined by relating them to pre-existing ones. Here we
define the unit zop as the equal of 15 kilo-yam:

>>> import math
>>> SI.units.zop = 15 * SI.units.kyam
>>> units.zop = 15 * units.kyam

Alternatively, units can be defined using the same string syntax that is used
by the Quantity constructor. Nevertheless, the following statement fails as we
cannot define the same unit twice.

>>> SI.units.zop = '15kyam'
>>> units.zop = '15kyam'
Traceback (most recent call last):
...
ValueError: cannot define 'zop': unit is already defined

Having defined the new units we can directly use them:

>>> SI.parse('24mzop/h')
>>> parse('24mzop/h')
0.1[X/T]
File renamed without changes.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ dynamic = ["version", "description"]
Home = "https://github.com/evalf/nutils-SI/"

[tool.flit.module]
name = "nutils.SI"
name = "nutils_SI"

0 comments on commit a2d6d7e

Please sign in to comment.