Package ivs :: Package units :: Module conversions :: Class Unit
[hide private]
[frames] | no frames]

Class Unit

source code


Class to calculate with numbers (and uncertainties) containing units.

You can put Units in an array and calculate with them. It is then allowed to mix Units with and without uncertainties. It is also allowed to mix Units with different units in one array, though some operations (e.g. sum) will not be possible.

Initalisation is done via (see __init__ for more options):

>>> a = Unit(2.,'m')
>>> b = Unit(4.,'km')
>>> c = Unit(3.,'cm2')

And you can calculate via:

>>> print a*b
8000.0 m2
>>> print a/c
6666.66666667 m-1
>>> print a+b
4002.0 m

Example 1: You want to calculate the equatorial velocity of the Sun:

>>> distance = Unit(2*np.pi,'Rsol')
>>> time = Unit(22.,'d')
>>> print (distance/time)
2299.03495719 m1 s-1

or directly to km/s:

>>> print (distance/time).convert('km/s')
2.29903495719 km/s

and with uncertainties:

>>> distance = Unit((2*np.pi,0.1),'Rsol')
>>> print (distance/time).convert('km/s')
2.29903495719+/-0.0365902777778 km/s

Example 2: The surface gravity of the Sun:

>>> G = Unit('GG')
>>> M = Unit('Msol')
>>> R = Unit('Rsol')
>>> logg = np.log10((G*M/R**2).convert('cgs'))
>>> print logg
4.43830739117

or

>>> G = Unit('GG')
>>> M = Unit(1.,'Msol')
>>> R = Unit(1.,'Rsol')
>>> logg = np.log10((G*M/R**2).convert('cgs'))
>>> print logg
4.43830739117

or

>>> old = set_convention('cgs')
>>> G = Unit('GG')
>>> M = Unit((1.,0.01),'Msol')
>>> R = Unit((1.,0.01),'Rsol')
>>> logg = np.log10(G*M/R**2)
>>> print logg
4.43830739117+/-0.00971111983789
>>> old = set_convention('SI')

Example 3: The speed of light in vacuum:

>>> eps0 = Unit('eps0')
>>> mu0 = Unit('mu0')
>>> cc = Unit('cc')
>>> cc_ = 1./np.sqrt(eps0*mu0)
>>> print eps0
8.85418781762e-12 F m-1
>>> print mu0
1.2566370614e-06 T m A-1
>>> print cc
299792458.0 m s-1
>>> print cc_
299792458.004 m1 s-1
>>> print cc_/cc
1.00000000001
Instance Methods [hide private]
a new object with type S, a subtype of T
__new__(cls, *args, **kwargs)
Create a new Unit instance.
source code
 
__init__(self, value, unit=None, **kwargs)
Different ways to initialize a Unit.
source code
 
convert(self, unit, compress=True)
Convert this Unit to different units.
source code
 
compress(self)
Compress current unit to a more readable version.
source code
 
as_tuple(self) source code
 
get_value(self)
Returns (value,error) in case of uncertainties.
source code
 
__getitem__(self, key)
Implements indexing, [0] returns value, [1] return unit.
source code
 
__lt__(self, other)
Compare SI-values of Units with eacht other.
source code
 
__radd__(self, other) source code
 
__add__(self, other)
Add a Unit to a Unit.
source code
 
__sub__(self, other)
Subtract a Unit from a Unit.
source code
 
__rsub__(self, other)
Subtract a Unit from a Unit.
source code
 
__mul__(self, other)
Multiply a Unit with something.
source code
 
__rmul__(self, other) source code
 
__div__(self, other)
Divide two units.
source code
 
__rdiv__(self, other) source code
 
__pow__(self, power)
Raise a unit to a power:
source code
 
sin(self) source code
 
cos(self) source code
 
tan(self) source code
 
arcsin(self) source code
 
arccos(self) source code
 
arctan(self) source code
 
log10(self) source code
 
log(self) source code
 
exp(self) source code
 
sqrt(self) source code
 
__str__(self)
str(x)
source code
 
__repr__(self)
repr(x)
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __subclasshook__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__new__(cls, *args, **kwargs)

source code 

Create a new Unit instance.

Overriding the default __new__ method makes sure that it is possible to initialize a Unit with an existing Unit instance. In that case, the original instance will simply be returned, and the __init__ functions is smart enough that it will not re-initialize the class instance.

Returns: a new object with type S, a subtype of T
Overrides: object.__new__

__init__(self, value, unit=None, **kwargs)
(Constructor)

source code 

Different ways to initialize a Unit.

Without uncertainties:

>>> x1 = Unit(5.,'m')
>>> x4 = Unit((5.,'m'))
>>> x5 = Unit(5.,'length')

The latter only works with the basic names ('length', 'time', 'temperature', etc..., and assures that the value is interpreted correctly in the current convention (SI,cgs...)

With uncertainties:

>>> x2 = Unit((5.,1.),'m')
>>> x3 = Unit((5.,1.,'m'))

Initiating with an existing instance will not do anything (not even making a copy! Only a new reference to the original object is made):

>>> x6 = Unit(x3)

This is the output from printing the examples above:

>>> print x1
5.0 m
>>> print x2
5.0+/-1.0 m
>>> print x3
5.0+/-1.0 m
>>> print x4
5.0 m
>>> print x5
5.0 m
>>> print x6
5.0+/-1.0 m
Overrides: object.__init__

convert(self, unit, compress=True)

source code 

Convert this Unit to different units.

By default, the converted unit will be compressed (i.e. it will probably not consist of only basic units, but be more readable).

__add__(self, other)
(Addition operator)

source code 

Add a Unit to a Unit.

You can add a non-Unit to a Unit only if the Unit has an empty unit string.

__mul__(self, other)

source code 

Multiply a Unit with something.

If `something' is a Unit, simply join the two Unit strings and call breakdown to collect.

__div__(self, other)

source code 

Divide two units.

>>> x = Unit(5.,'m15')
>>> y = Unit(2.5,'m6.5')
>>> print(x/y)
2.0 m8.5

__pow__(self, power)

source code 

Raise a unit to a power:

>>> x = Unit(5.,'m')
>>> print(x**0.5)
2.2360679775 m0.5
>>> print(x**-1)
0.2 m-1
>>> print(x**3)
125.0 m3
>>> x = Unit(9.,'m11')
>>> print(x**0.5)
3.0 m5.5

__str__(self)
(Informal representation operator)

source code 

str(x)

Overrides: object.__str__
(inherited documentation)

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)