def test_offset_unit_computations():
    """ Executing some basic computations with a basic custom unit with offset.
    """
    my_u = unit(12, m.derivation, 14)
    s1 = UnitScalar(3, units=my_u)
    s2 = UnitScalar(5, units=my_u)
    s3 = s1 + s2
    assert_equal(s3, UnitScalar(8, units=my_u))
Beispiel #2
0
def test_offset_unit_computations():
    """ Executing some basic computations with a basic custom unit with offset.
    """
    my_u = unit(12, m.derivation, 14)
    s1 = UnitScalar(3, units=my_u)
    s2 = UnitScalar(5, units=my_u)
    s3 = s1+s2
    assert_equal(s3,UnitScalar(8, units=my_u))
 def test_offset_unit_computations(self):
     """ Basic computations with a basic custom unit with offset.
     """
     my_u = unit(12, m.derivation, 14)
     s1 = UnitScalar(3, units=my_u)
     s2 = UnitScalar(5, units=my_u)
     s3 = s1 + s2
     self.assertEqual(s3, UnitScalar(8, units=my_u))
Beispiel #4
0
 def test_offset_unit_computations(self):
     """ Basic computations with a basic custom unit with offset.
     """
     my_u = unit(12, m.derivation, 14)
     s1 = UnitScalar(3, units=my_u)
     s2 = UnitScalar(5, units=my_u)
     s3 = s1 + s2
     self.assertEqual(s3, UnitScalar(8, units=my_u))
Beispiel #5
0
    def __convert_other(self, other):
        su = getattr(self, 'units', dimensionless)
        ou = getattr(other, 'units', dimensionless)

        if su == None and ou == None:
            u = None
        else:
            if isinstance(other, unit):
                # Handles 5 * liters
                ou = unit(1, other.derivation)
                other = convert(other.value, ou, su)
            elif isinstance(other, UnitArray):
                # Handles UnitArray or UnitScalar
                other = convert(numpy.array(other), ou, su)
            elif isinstance(other, numpy.ndarray):
                if len(other.shape) > 0 and hasattr(other.item(0), 'derivation'):
                    # Handles array([1,2,3] * liters)
                    ou = unit(1, other.item(0).derivation)
                    other = convert(other/ou, ou, su)
            u = su
        return other, u
    def __convert_other(self, other):
        su = getattr(self, 'units', dimensionless)
        ou = getattr(other, 'units', dimensionless)

        if su == None and ou == None:
            u = None
        else:
            if isinstance(other, unit):
                # Handles 5 * liters
                ou = unit(1, other.derivation)
                other = convert(other.value, ou, su)
            elif isinstance(other, UnitArray):
                # Handles UnitArray or UnitScalar
                other = convert(numpy.array(other), ou, su)
            elif isinstance(other, numpy.ndarray):
                if len(other.shape) > 0 and hasattr(
                        other.item(0), 'derivation'):
                    # Handles array([1,2,3] * liters)
                    ou = unit(1, other.item(0).derivation)
                    other = convert(other / ou, ou, su)
            u = su
        return other, u
Beispiel #7
0
 def test_unit_parser_derivation_valid(self):
     # Make sure every derivation of the SI core units can be parsed.
     for i, label in enumerate(unit._labels):
         derivation = [0] * len(unit._labels)
         derivation[i] = 1
         base_unit = unit(1.0, tuple(derivation))
         base_unit.label = label
         self.assertEqual(
             unit_parser.parse_unit(label, suppress_unknown=False),
             base_unit,
         )
     self.assertEqual(
         unit_parser.parse_unit('S', suppress_unknown=False),
         SI.siemens,
     )
 def test_unit_parser_derivation_valid(self):
     # Make sure every derivation of the SI core units can be parsed.
     for i, label in enumerate(unit._labels):
         derivation = [0] * len(unit._labels)
         derivation[i] = 1
         base_unit = unit(1.0, tuple(derivation))
         base_unit.label = label
         self.assertEqual(
             unit_parser.parse_unit(label, suppress_unknown=False),
             base_unit,
         )
     self.assertEqual(
         unit_parser.parse_unit('S', suppress_unknown=False),
         SI.siemens,
     )
#                        (C) 1998-2003  All Rights Reserved
#
# <LicenseText>
#
# Modified: 2005-05-23, Travis N. Vaught added label attribs and more aliases
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from scimath.units.unit import unit

# Tk = Tk
# Tk = 1 * (Tc+273.15)
# Tk = 5/9 * (Tr)
# Tk = 5/9 * (Tf + 459.67)

kelvin = unit(1.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
kelvin.label = 'kelvin'
celsius = unit(1.0, (0, 0, 0, 0, 1, 0, 0), 273.15)
celsius.label = 'celsius'
rankine = unit(5.0 / 9.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
fahrenheit = unit(5.0 / 9.0, (0, 0, 0, 0, 1, 0, 0), 459.67)
fahrenheit.label = 'fahrenheit'

# aliases
K = kelvin
degC = celsius
degc = celsius
degF = fahrenheit
degf = fahrenheit
degK = kelvin
degk = kelvin
Beispiel #10
0
#                        (C) 1998-2003  All Rights Reserved
#
# <LicenseText>
#
# Modified: 2005-5-23, inserted into enthought package...re-rooted import
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#


from copy import copy
from scimath.units.unit import unit, dimensionless, none

# basic SI units

meter = unit(1.0, (1, 0, 0, 0, 0, 0, 0))
kilogram = unit(1.0, (0, 1, 0, 0, 0, 0, 0))
second = unit(1.0, (0, 0, 1, 0, 0, 0, 0))
ampere = unit(1.0, (0, 0, 0, 1, 0, 0, 0))
mole = unit(1.0, (0, 0, 0, 0, 0, 1, 0))
candela = unit(1.0, (0, 0, 0, 0, 0, 0, 1))
# moved kelvin to temperature for now

# the 22 derived SI units with special names

radian = copy(dimensionless)                #  plane angle
radian.label = 'radian'

steradian = copy(dimensionless)              #  solid angle
steradian.label = 'steradian'
hertz = 1/second                       #  frequency
Beispiel #11
0
#                        California Institute of Technology
#                        (C) 1998-2003  All Rights Reserved
#
# <LicenseText>
#
# Modified: 2005-5-23, inserted into enthought package...re-rooted import
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from copy import copy
from scimath.units.unit import unit, dimensionless, none

# basic SI units

meter = unit(1.0, (1, 0, 0, 0, 0, 0, 0))
kilogram = unit(1.0, (0, 1, 0, 0, 0, 0, 0))
second = unit(1.0, (0, 0, 1, 0, 0, 0, 0))
ampere = unit(1.0, (0, 0, 0, 1, 0, 0, 0))
mole = unit(1.0, (0, 0, 0, 0, 0, 1, 0))
candela = unit(1.0, (0, 0, 0, 0, 0, 0, 1))
# moved kelvin to temperature for now

# the 22 derived SI units with special names

radian = copy(dimensionless)  #  plane angle
radian.label = 'radian'

steradian = copy(dimensionless)  #  solid angle
steradian.label = 'steradian'
hertz = 1 / second  #  frequency
Beispiel #12
0
kilobar = kilo * bar
kilobar.label = 'kbar'
kbar = kilobar
kbars = kbar

millibar = 100 * pascal
millibar.label = 'mbar'
mbar = millibar

torr = 133.3 * pascal
torr.label = 'torr'

atmosphere = 101325 * pascal
atmosphere.label = 'atm'
atm = atmosphere

pounds_per_square_inch = lbf / inch ** 2
pounds_per_square_inch.label = 'psi'
psi = pounds_per_square_inch
apsi = psi
psig = unit(psi.value, psi.derivation, 14.6959494)

inHg = 3386.389 * pascal
inHg.label = 'inHg'

# version
__id__ = "$Id: pressure.py,v 1.1.1.1 2003/07/02 21:39:14 aivazis Exp $"

#
# End of file
Beispiel #13
0
# others

bar = 1e5 * pascal
bar.label = 'bar'
bars = bar

kilobar = kilo * bar
kilobar.label = 'kbar'
kbar = kilobar
kbars = kbar

millibar = 100 * pascal

torr = 133.3 * pascal
atmosphere = 101325 * pascal

atm = atmosphere

pounds_per_square_inch = lbf / inch ** 2
pounds_per_square_inch.label = 'psi'
psi = pounds_per_square_inch
apsi = psi
psig = unit(psi.value, psi.derivation, 14.6959494)


# version
__id__ = "$Id: pressure.py,v 1.1.1.1 2003/07/02 21:39:14 aivazis Exp $"

#
# End of file
Beispiel #14
0
 def test_units_should_hash_equal_if_they_compare_equal(self):
     meter = unit(1.0, (1, 0, 0, 0, 0, 0, 0))
     metre = unit(1.0, (1, 0, 0, 0, 0, 0, 0))
     self.assertEqual(meter, metre)
     self.assertEqual(hash(meter), hash(metre))
Beispiel #15
0
#                        (C) 1998-2003  All Rights Reserved
#
# <LicenseText>
#
# Modified: 2005-05-23, Travis N. Vaught added label attribs and more aliases
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#

from scimath.units.unit import unit

# Tk = Tk
# Tk = 1 * (Tc+273.15)
# Tk = 5/9 * (Tr)
# Tk = 5/9 * (Tf + 459.67)

kelvin = unit(1.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
kelvin.label = 'kelvin'
celsius = unit(1.0, (0, 0, 0, 0, 1, 0, 0), 273.15)
celsius.label = 'celsius'
rankine = unit(5.0/9.0, (0, 0, 0, 0, 1, 0, 0), 0.0)
fahrenheit = unit(5.0/9.0,(0, 0, 0, 0, 1, 0, 0),  459.67)
fahrenheit.label = 'fahrenheit'

# aliases
K = kelvin
degC = celsius
degc = celsius
degF = fahrenheit
degf = fahrenheit
degK = kelvin
degk = kelvin