Exemplo n.º 1
0
def physical_constants(name):
    """return a physical constant from sp.constants.physical_constants as uval
    """
    unittable = {'m':ETQ.UVal(1, {'meter':Fraction(1,1)}),
                 'K':ETQ.UVal(1, {'kelvin':Fraction(1,1)}),
                 'W':ETQ.Power(1, 'W').uval,
                 'Hz':ETQ.Frequency(1,'Hz').uval,
                 'ohm':ETQ.Resistance(1,'Ohm').uval}
    try:
        import scipy as _sp
        phConst = _sp.constants.physical_constants[name]
    except KeyError as e:
        raise e

    value = phConst[0]
    unitStr = phConst[1]
    ul = unitStr.split(' ')
    uval = ETQ.UVal(value, {})
    for ui in ul:
        ux = ui.split('^')
        if len(ux) < 2:
            ux.append(1)
        try:
            uval *= power(unittable[ux[0]], int(ux[1]))
        except KeyError:
            raise KeyError('{} : {}'.format(ux[0], ui))
    return uval
Exemplo n.º 2
0
def arcsin(scalar):
    """ arcsin

    >>> scalar = ETQ.Scalar(0.5, '1.0')
    >>> print(scalar)
       0.500  (Scalar)
    >>> print(asin(scalar.get_value()))  # rad
    0.52359877559...
    >>> asin(scalar.get_uval())
    UVal(0.5235987755982989, {})
    >>> print(asin(scalar.get_uval()))
    0.5236 {}
    >>> a=asin(scalar); a.set_displayUnit('deg'); print(a)
      30.000 deg (Angle)
    >>> print(asin(ETQ.Distance(0.5, 'm')))
    Traceback (most recent call last):
        ...
    EngineeringTools.tools.functions.EngineeringTools_tools_Error_units: wrong type : <class 'EngineeringTools.quantities.mechanics.Distance'>

    """
    if isinstance(scalar, float):
        return _np.arcsin(scalar)
    elif isinstance(scalar, ETQ.UVal):
        scalar.check_units({})
        return ETQ.UVal(_np.arcsin(scalar.get_value()), {})
    elif isinstance(scalar, ETQ.Scalar):
        return ETQ.Angle(_np.arcsin(scalar.get_value()), 'rad')
    else:
        raise EngineeringTools_tools_Error_units('wrong type : %s' % type(scalar))
Exemplo n.º 3
0
def sin(angle):
    """ sin

    >>> angle = ETQ.Angle(30.0, 'deg')
    >>> print(angle)
      30.000 deg (Angle)
    >>> print(round(sin(angle.get_value('rad')), 3))
    0.5
    >>> sin(angle.get_uval())
    UVal(0.49999999999999994, {})
    >>> print(sin(angle.get_uval()))
    0.5000 {}
    >>> print(sin(angle))
       0.500  (Scalar)
    >>> print(sin(ETQ.Distance(0.5, 'm')))
    Traceback (most recent call last):
        ...
    EngineeringTools.tools.functions.EngineeringTools_tools_Error_units: wrong type : <class 'EngineeringTools.quantities.mechanics.Distance'>

    """
    if isinstance(angle, float):
        return _np.sin(angle)
    elif isinstance(angle, ETQ.UVal):
        angle.check_units({})
        return ETQ.UVal(_np.sin(angle.get_value()), {})
    elif isinstance(angle, ETQ.Angle):
        return ETQ.Scalar(_np.sin(angle.get_value()))
    else:
        raise EngineeringTools_tools_Error_units('wrong type : %s' % type(angle))
Exemplo n.º 4
0
def tan(angle):
    """ tan

    >>> angle = ETQ.Angle(30.0, 'deg')
    >>> print(angle)
      30.000 deg (Angle)
    >>> print(tan(angle.get_value('rad')))
    0.5773502691...
    >>> tan(angle.get_uval())
    UVal(0.5773502691896257, {})
    >>> print(tan(angle.get_uval()))
    0.5774 {}
    >>> print(tan(angle))
       0.577  (Scalar)
    >>> print(tan(ETQ.Distance(0.5, 'm')))
    Traceback (most recent call last):
        ...
    EngineeringTools.tools.functions.EngineeringTools_tools_Error_units: wrong type : <class 'EngineeringTools.quantities.mechanics.Distance'>

    """
    if isinstance(angle, float):
        return _np.tan(angle)
    elif isinstance(angle, ETQ.UVal):
        angle.check_units({})
        return ETQ.UVal(_np.tan(angle.get_value()), {})
    elif isinstance(angle, ETQ.Angle):
        return ETQ.Scalar(_np.tan(angle.get_value()), '1.0')
    else:
        raise EngineeringTools_tools_Error_units('wrong type : %s' % type(angle))
Exemplo n.º 5
0
def exp(scalar):
    """exp
    """
    if isinstance(scalar, int):
        scalar = float(scalar)
    if isinstance(scalar, float):
        return _np.exp(scalar)
    elif isinstance(scalar, ETQ.UVal):
        scalar.check_units({})
        return ETQ.UVal(_np.exp(scalar.get_value()), {})
    else:
        raise EngineeringTools_tools_Error_units('type not recognized: {}'.format(type(scalar)))
Exemplo n.º 6
0
def power(value, exp):
    """  value**exp
    """
    if isinstance(value, ETQ.QuantityNumeric):
        value = value.uval
    if isinstance(exp, (ETQ.Number, ETQ.Scalar)):
        exp = exp.get_value()

    if isinstance(exp, int):
        exp = float(exp)

    if isinstance(value, (float, int)) and isinstance(exp, float):
        return _np.power(value, exp)
    elif isinstance(value, ETQ.UVal) and isinstance(exp, float):
        units = {k:v*exp for k,v in value._units.items()}
        return ETQ.UVal(_np.power(value.get_value(), exp), units)
    else:
        raise EngineeringTools_tools_Error_units('type not recognized: {}, {}'.format(type(value), type(exp)))
Exemplo n.º 7
0
def atan2(y, x):
    """ atan2

    >>> y = ETQ.Distance(1/3.0**(0.5), 'm')
    >>> x = ETQ.Distance(1.0, 'm')
    >>> print(y, x)
     577.350 mm (Distance) 1000.000 mm (Distance)
    >>> print(atan2(y, x))
      30.000 deg (Angle)
    >>> atan2(y.uval, x.uval)
    UVal(0.5235987755982989, {})
    >>> print(atan2(y.value, x.value) / _np.pi * 180.0)
    30.0...

    """
    if isinstance(y, float) and isinstance(x, float):
        return _np.arctan2(y, x)
    elif isinstance(y, ETQ.UVal) and isinstance(x, ETQ.UVal):
        y.check_units(x)
        return ETQ.UVal(_np.arctan2(y.get_value(), x.get_value()), {})
    elif y.match_quantities(x):
        return ETQ.Angle(_np.arctan2(y.get_value(), x.get_value()), 'rad')
    else:
        raise EngineeringTools_tools_Error_units('wrong type or combination : %s, %s' % (type(y), type(x)))
Exemplo n.º 8
0
 def test__pow(self):
     d = ETQ.Distance(2, 'm')
     self.assertEqual(d**2, ETQ.UVal(4,{'meter': Fraction(2, 1)}))
     self.assertEqual(ETQ.Area(4, 'm2')**(1/2), ETQ.UVal(2,{'meter': Fraction(1, 1)}))