You are a guest. Restricted access. Read more.
Command disabled: backlink
SCaVis manual

Measurements with units

Before we considered how to perform conversion between different units (see Sect.unit_convert). Now we will consider calculations with units. This part is based on the JScience project.

First, let us check the available units:

First, let us import International units and their derivatives:

from javax.measure.unit.SI import *
dir()

You will see the available units:

['AMPERE', 'ATTO', 'BECQUEREL', 'BIT', 'CANDELA', 'CELSIUS', 'CENTI',
'CENTIMETER', 'CENTIMETRE', 'COULOMB', 'CUBIC_METRE', 'DECI', 'DEKA',
 'EXA', 'Editor', 'FARAD', 'FEMTO', 'GIGA', 'GRAM', 'GRAY', 'HECTO',
 'HENRY', 'HERTZ', 'JOULE', 'KATAL', 'KELVIN', 'KILO', 'KILOGRAM',
 'KILOMETER', 'KILOMETRE', 'LUMEN', 'LUX', 'MEGA', 'METER', 'METERS_PER_SECOND',
 'METERS_PER_SQUARE_SECOND', 'METRE', 'METRES_PER_SECOND', 'METRES_PER_SQUARE_SECOND',
 'METRE_PER_SECOND', 'METRE_PER_SQUARE_SECOND', 'MICRO', 'MILLI', 'MILLIMETER',
 'MILLIMETRE', 'MOLE', 'NANO', 'NEWTON', 'OHM', 'PASCAL', 'PETA', 'PICO',
 'RADIAN', 'SECOND', 'SIEMENS', 'SIEVERT', 'SQUARE_METRE', 'STERADIAN', 'TERA',
 'TESLA', 'VOLT', 'WATT', 'WEBER', 'YOCTO', 'YOTTA',  'ZEPTO', 'ZETTA',

Similarly, one can import non-international units:

from javax.measure.unit.NonSI import *
dir()

which shows

['ANGSTROM', 'ARE', 'ASTRONOMICAL_UNIT', 'ATMOSPHERE', 'ATOM',
 'ATOMIC_MASS', 'BAR', 'BYTE', 'C', 'CENTIRADIAN', 'COMPUTER_POINT',
 'CUBIC_INCH', 'CURIE', 'ClassPath', 'DAY', 'DAY_SIDEREAL', 'DECIBEL',
 'DEGREE_ANGLE', 'DYNE', 'E', 'ELECTRON_MASS', 'ELECTRON_VOLT', 'ERG',
 'FAHRENHEIT', 'FARADAY', 'FOOT', 'FOOT_SURVEY_US', 'FRANKLIN', 'G',
 'GALLON_DRY_US', 'GALLON_LIQUID_US', 'GALLON_UK', 'GAUSS', 'GILBERT',
 'GRADE', 'HECTARE', 'HORSEPOWER', 'HOUR', 'INCH', 'INCH_OF_MERCURY',
 'KILOGRAM_FORCE', 'KILOMETERS_PER_HOUR', 'KILOMETRES_PER_HOUR', 'KNOT',
 'LAMBERT', 'LIGHT_YEAR', 'LITER', 'LITRE', 'MACH', 'MAXWELL', 'METRIC_TON',
 'MILE', 'MILES_PER_HOUR', 'MILLIMETER_OF_MERCURY', 'MINUTE', 'MINUTE_ANGLE',
 'MONTH', 'NAUTICAL_MILE', 'OCTET', 'OUNCE', 'OUNCE_LIQUID_UK', 'OUNCE_LIQUID_US',
 'PARSEC', 'PERCENT', 'PIXEL', 'POINT', 'POISE', 'POUND', 'POUND_FORCE', 'RAD',
 'RANKINE', 'REM', 'REVOLUTION', 'ROENTGEN', 'RUTHERFORD', 'SECOND_ANGLE', 'SPHERE',
 'STOKE', 'TON_UK', 'TON_US', 'UserMacrosDir', 'WEEK', 'YARD', 'YEAR',
 'YEAR_CALENDAR', 'YEAR_SIDEREAL',

Let is define the mass in pounds:

>> from  org.jscience.physics.amount import *
>> from javax.measure.unit.NonSI import *
>>>from javax.measure.unit.SI import *
>> m0=Amount.valueOf(100, POUND)    # define mass in pounds
>> m1=Amount.valueOf(100, KILOGRAM) # in kg
>> print m0, m1
Output: 100 lb  100 kg

Arithmetics with measures using units

You can do the standard math with measures using different units.

In this small example we defined two masses in different units and then divided by 3, multiplied by 5 and added another mass in kilograms. When we print it, we convert the answer to kilograms.

from  org.jscience.physics.amount import *
from javax.measure.unit.NonSI import *
from javax.measure.unit.SI import *
m0=Amount.valueOf(100, POUND)    # define mass in pounds
m1=Amount.valueOf(100, KILOGRAM) # in kg
m= m0.divide(2).times(5).plus(m1)
 
from java.lang.System import * # we use Java system output in UTF-8
out.println( m.to(KILOGRAM))

The answer in kg is:

(213.39809249999996 ± 2.8E-14) kg

The last “± 2.8E-14” does nor mean much since we defined the values without errors. We can do now manipulation assuming that each value has measurement uncertainty (“error”).

rom  org.jscience.physics.amount import *
from javax.measure.unit.NonSI import *
from javax.measure.unit.SI import *
m0=Amount.valueOf(100, 5, POUND)    # define mass with error in pounds 100 +- 5
m1=Amount.valueOf(100, 2, KILOGRAM) # in kg with error, 100 +-2 kg
m= m0.divide(2).times(5).plus(m1)
from java.lang.System import *
out.println( m.toString());

Now the output has a meaningful error:

(4.7E2 ± 17.) lb

Look at the class API for the “Amount” objects here: Amount or in the summary Amount package.

Let's build a small example. Let's calculate the speed of a free falling object:

from org.jscience.physics.amount import *;
from javax.measure.unit import import *
from javax.measure.unit.Dimension import *
 
TIME=Amount.valueOf(100,SI.SECOND)
v=TIME.times(Constants.g) # time times gravitation constant

Which will show the output:

980.6649999999997 ± 1.7E-13) m/s

Note that “Constants.g” is the gravitational constant which is

(9.8066499999999976 ± 8.9E-16) m/s²

You are not full member and have a limited access to this section. One can unlock this part after becoming a full member.

man/meas/unit_measurements.txt · Last modified: 2013/12/14 19:55 by admin
CC Attribution-Share Alike 3.0 Unported
Powered by PHP Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 Valid HTML5