DMelt:Units/4 Calculations with Units
Calculations with units
Calculations with units are performed using the Java class jhplot.UCalc. Here is the example of such calculations:
from jhplot import * u=UCalc() s="2 hours + 23 min - 32 sec" print s,"=",u.eval(s,"sec") # "sec" is expected unit
You will the the output with the correct number of seconds hat follows this expression.
One can make quite complicated calculations using the following constants:
pi ratio of circumference to diameter c speed of light e charge on an electron h Planck's constant
and the following functions:
ln natural logarithm log base-10 logarithm log2 base-2 logarithm exp exponential sqrt square root, sqrt(x) = x^(1/2) cuberoot cube root, cuberoot(x) = x^(1/3)
Exponents are specified using the operator ^ or .
Let us give a more complicated example:
from jhplot import * u=UCalc() print u.eval("sqrt(10*50ft/90 (cm/s^2))","s")
which will return "13.012814
Another example: let us find suppose you want to find the wavelength, in meters, of a 200 MHz radio wave. You can do it as:
from jhplot import * u=UCalc() print u.eval("c / 200 MHz","m")
which will print the correct wavelength.
Try to use the method "help()" to get a help about the unit conversion. Another useful help system is done using "search(unit)" method that shows the available definitions. For example:
from jhplot import * u=UCalc() u.search("sec")
will show some help associated with "seconds".
This program is based on the units-in-java program, please read first that tutorial. The difference between the JavaUnits program and the current implementation in Scavis is in the possibility to evaluate all expressions in Jython and Java.
The results are returned using "eval()" function (return string), or using getValue() function (return double).
Values with units using VisAd
The Java package VisAd contains powerful classes visad.Real and visad.Data to perform calculations with units and errors.
If your quantities are physical and have associated Units, then you might prefer to create a VisAD MathType that explicitly defines the metadata characteristics of your quantities. A MathType is used to define the kind of mathematical object that the Data object approximates. Every Data object in VisAD must have a MathType. In the previous examples, a default MathType with the name "Generic" was implicitly used for our Real objects.
In the simplest form for dealing with Units, the constructor for a MathType which defines Real values is:
RealType(String name, Unit u, Set s)
which allows you to assign a unique name to this MathType, a Unit for this, and define a default Set. In practice, the Set is seldom used and should just be passed as null in most cases. Here is an example using Java codding:
import visad.*; Real t1,t2,sum; RealType k = new RealType("kelvin",SI.kelvin,null); t2 = new Real(k,273.); t1 = new Real(k,255.); sum = (Real) t1.add(t2); System.out.println("sum = "+sum+" "+sum.getUnit());
When you run this example, you get: sum = 528.0 K In this example, we use an SI Unit (ampere, candela, kelvin, kilogram, meter, second, mole, radian, steradian). Note that we constructed two variables with the same MathType, that is the same name, Unit, and Set.
This example shows how to define values with errors
and how to perform mathematical transformation while propagating errors (independently) using Jython/Python:
from visad import SI,Data,Real,RealType def printReal(x): print x.toString(), "+-", (x.getError()).getErrorValue() a=Real(10,2) # 10 +- 2 (error) b=Real(30,5) # 30 +- 5 (error) c=a.add(b,Data.NEAREST_NEIGHBOR, Data.INDEPENDENT) print "a+b=", printReal(c) c=a.divide(b,Data.NEAREST_NEIGHBOR, Data.INDEPENDENT) print "a+b=", printReal(c) a=Real(0.9,0.1) # 10 +- 2 (error) c=a.cos(Data.NEAREST_NEIGHBOR, Data.INDEPENDENT) print "acos(a)=", printReal(c) a=Real(0.9,0.1) # 10 +- 2 (error) c=a.pow(c,Data.NEAREST_NEIGHBOR, Data.INDEPENDENT) print "a^4=", printReal(c) print "use units:" k=RealType("kelvin",SI.kelvin) t2=Real(k,273.); t1= Real(k,255.); xsum =t1.add(t2); print "sum = ",xsum," ",xsum.getUnit()