Class PhysicalQuantity
Physical quantity with units
PhysicalQuantity instances allow addition, subtraction,
multiplication, and division with each other as well as multiplication,
division, and exponentiation with numbers. Addition and subtraction check
that the units of the two operands are compatible and return the result
in the units of the first operand. A limited set of mathematical
functions (from module Numeric) is applicable as well:
-
sqrt: equivalent to exponentiation with 0.5.
-
sin, cos, tan: applicable only to objects whose unit is compatible
with 'rad'.
See the documentation of the PhysicalQuantities module for a list of
the available units.
Here is an example on usage:
>>> from PhysicalQuantities import PhysicalQuantity as p
>>> distance1 = p('10 m')
>>> distance2 = p('10 km')
>>> total = distance1 + distance2
>>> total
PhysicalQuantity(10010.0,'m')
>>> total.convertToUnit('km')
>>> total.getValue()
10.01
>>> total.getUnitName()
'km'
>>> total = total.inBaseUnits()
>>> total
PhysicalQuantity(10010.0,'m')
>>>
>>> t = p(314159., 's')
>>>
>>> t2 = t.inUnitsOf('d','h','min','s')
>>> t2_print = ' '.join([str(i) for i in t2])
>>> t2_print
'3.0 d 15.0 h 15.0 min 59.0 s'
>>>
>>> e = p('2.7 Hartree*Nav')
>>> e.convertToUnit('kcal/mol')
>>> e
PhysicalQuantity(1694.2757596034764,'kcal/mol')
>>> e = e.inBaseUnits()
>>> str(e)
'7088849.77818 kg*m**2/s**2/mol'
>>>
>>> freeze = p('0 degC')
>>> freeze = freeze.inUnitsOf ('degF')
>>> str(freeze)
'32.0 degF'
>>>
|
|
|
|
|
|
|
|
|
__init__(self,
*args)
There are two constructor calling patterns: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
convertToUnit(self,
unit)
Change the unit and adjust the value such that the combination is
equivalent to the original one. |
|
|
|
|
|
getUnitName(self)
Return unit (string) of physical quantity. |
|
|
|
getValue(self)
Return value (float) of physical quantity (no unit). |
|
|
PhysicalQuantity
|
inBaseUnits(self)
Returns:
the same quantity converted to base units, i.e. |
|
|
PhysicalQuantity or tuple of PhysicalQuantity
|
inUnitsOf(self,
*units)
Express the quantity in different units. |
|
|
bool
|
isCompatible(self,
unit)
Returns:
True if the specified unit is compatible with the one of
the quantity |
|
|
|
|
|
|
|
|
__init__(self,
*args)
(Constructor)
|
|
There are two constructor calling patterns:
-
PhysicalQuantity(value, unit), where value is any number and unit is
a string defining the unit
-
PhysicalQuantity(value_with_unit), where value_with_unit is a string
that contains both the value and the unit, i.e. '1.5 m/s'. This form
is provided for more convenient interactive use.
- Parameters:
args ((number, str ) or (str ,)) - either (value, unit) or (value_with_unit,)
|
convertToUnit(self,
unit)
|
|
Change the unit and adjust the value such that the combination is
equivalent to the original one. The new unit must be compatible with the
previous unit of the object.
- Parameters:
- Raises:
TypeError - if the unit string is not a know unit or a unit incompatible with
the current one
|
- Returns: PhysicalQuantity
- the same quantity converted to base units, i.e. SI units in most
cases
|
Express the quantity in different units. If one unit is specified, a
new PhysicalQuantity object is returned that expresses the quantity in
that unit. If several units are specified, the return value is a tuple of
PhysicalObject instances with with one element per unit such that the sum
of all quantities in the tuple equals the the original quantity and all
the values except for the last one are integers. This is used to convert
to irregular unit systems like hour/minute/second.
- Parameters:
units (str or sequence of str ) - one or several units
- Returns: PhysicalQuantity or
tuple of PhysicalQuantity
- one or more physical quantities
- Raises:
TypeError - if any of the specified units are not compatible with the original
unit
|
- Parameters:
- Returns:
bool
True if the specified unit is compatible with the
one of the quantity
|