Provides support for physical quantities, automatic error calculation (including numeric errors), and dimension-checking done in the form of class-type checking.
This package contains more than 40 predefined quantities with
specific methods (e.g. Angle.sin(), Scalar.log()
)
and constants (e.g. Velocity.SPEED_OF_LIGHT, Mass.PROTON, Constants.µ0
).
The appropriate quantity sub-class is automatically instantiated when needed.
Quantity classes support derivation, making the framework easily extendable. For example:
// Derives from top Quantity class. public class MagneticDipoleMoment extends Quantity { private final static Factory FACTORY = new Factory(SI.AMPERE.multiply(SI.METER.pow(2))) { protected Quantity create() { return new MagneticDipoleMoment(); } }; } // Derives from quantities other than the Quantity class. public class Altitude extends Length { private final static Factory FACTORY = new Factory(SI.METER.alternate("meter_height")) { protected Quantity create() { return new Altitude(); } }; };Note: Mapping of the predefined quantities occurs automatically when the library is initialized If the framework is extended (e.g. new quantity sub-classes) the application has to make sure that the new classes are initialized to ensure proper unit registration.
Quantities take into account measurement errors as well as numeric errors. The default {@link org.jscience.physics.quantities.QuantityFormat QuantityFormat} outputs only the decimal digits being exact. For example:
Length x = (Length) Quantity.valueOf(1, SI.METER); Velocity v = (Velocity) Quantity.valueOf(0.01, SI.METER.divide(SI.SECOND)); Duration t = (Duration) Quantity.valueOf(1, SI.MICRO(SI.SECOND)); for (int i=0; i < 10000000; i++) { x = (Length) x.add(v.multiply(t)); } System.out.println(x); > 1.10000000 mThe exact value is guaranteed to be in the range:
]1.09999999 m, 1.10000001 m[
.
The same calculation using double
would have printed> 1.099999999392253with no idea on the accuracy of this result.
The system unit of a quantity determinates its class.
For example, Quantity.valueOf("1 µm")
and Quantity.valueOf("1.2 ft")
return a Length
instance (both "µm"
and "ft"
units
are derived from SI.METER
).
Multiple physical models are supported (e.g. Standard
,
Relativistic
, High-Energy
, Quantum
and Natural
). The current model defines the conversions being allowed
as well as the default units to output quantities. For example:
final Quantity x = Length.valueOf(1, NonSI.INCH); System.out.println(x); // Default standard model, length are stated in meters. LocalContext.enter(); try { RelativisticModel.select(); // Selects the relativistic model. System.out.println(x); // Lengths are stated in second. Quantity y = x.add(Quantity.valueOf("2.3 µs")); // Length and Duration can be added. Mass m = Mass.massOf(Quantity.valueOf("12 GeV")); // Energy is compatible with mass (E=mc2) } finally { LocalContext.exit(); } > 0.02540000000000000 m > 8.47252801803306E-11 s
Custom units for quantity output is allowed. For example:
Length x = (Length) Quantity.valueOf(123, SI.CENTI(SI.METER)); Length.showAs(NonSI.INCH); // Context-local. System.out.println(x); > 48.42519685039370 in