|
NAMEPhysics::Unit::Scalar SYNOPSIS # Distances
$d = new Physics::Unit::Distance('98 mi');
print $d->ToString, "\n"; # prints 157715.712 meter
$d->add('10 km');
print $d->ToString, "\n"; # prints 167715.712 meter
print $d->value, ' ', $d->default_unit->name, "\n"; # same thing
# Convert
print $d->ToString('mile'), "\n"; # prints 104.213... mile
print $d->convert('mile'), " miles\n"; # same thing (except 'miles')
$d2 = new Physics::Unit::Distance('2000'); # no unit given, use the default
print $d2->ToString, "\n"; # prints 2000 meter
# Times
$t = Physics::Unit::Time->new('36 hours');
print $t->ToString, "\n"; # prints 129600 second
# Speed = Distance / Time
$s = $d->divide($t); # $s is a Physics::Unit::Speed object
print $s->ToString, "\n"; # prints 1.2941... mps
# Automatic typing
$s = new Physics::Unit::Scalar('kg m s'); # Unrecognized type
print ref $s, "\n"; # $s is a Physics::Unit::Scalar
$f = $s->divide('3000 s^3');
print ref $f, "\n"; # $f is a Physics::Unit::Force
DESCRIPTIONThis package encapsulates information about physical quantities. Each instance of a class that derives from Physics::Unit::Scalar holds the value of some type of measurable quantity. When you use this module, several new classes are immediately available. See the UnitsByType page for a list of types included with the unit library. You will probably only need to use the classes that derive from Physics::Unit::Scalar, such as Physics::Unit::Distance, Physics::Unit::Speed, etc. You can also define your own derived classes, based on types of physical quantities. This module relies heavily on Physics::Unit. Each Scalar object references a Unit object that defines the dimensionality of the Scalar. The dimensionality also identifies (usually) the type of physical quantity that is stored, and the derived class of the object. For example, the class Physics::Unit::Distance uses the Physics::Unit object named 'meter' to define the scale of its object instances. The type of the 'meter' object is 'Distance'. Defining classes that correspond to physical quantity types allows us to overload the arithmetic methods to produce derived classes of the correct type automatically. For example: $d = new Physics::Unit::Distance('98 mi');
$t = new Physics::Unit::Time('36 years');
# $s will be of type Physics::Unit::Speed.
$s = $d->divide($t);
When a new object is created, this package attempts to determine its subclass based on its dimensionality. Thus, when you multiply two Distances together, the result is an Area object. This behavior can be selectively overridden when necessary. For example, both energy and torque have the same dimensions, Force * Distance. Therefore, it remains the programmer's responsibility, in this case, to assign the correct subclass to Scalars that have this dimensionality. See also the Physics::Unit::Scalar::Implementation page for more details. EXPORT OPTIONSBy default, this module exports nothing. You can request all of the functions to be exported as follows: use Physics::Unit::Scalar ':ALL'; Or, you can just get specific ones. For example: use Physics::Unit::Scalar qw( ScalarFactory GetScalar ); FUNCTIONS
METHODS
OVERLOADED OPERATORSThese operators/functions are overloaded: +, -, *, /, **, int, abs, exp, log, sin, cos, atan2, <=>, eq, ne, "", 0+ and bool. As mentioned above, it is possible to units to be indeterminate from their dimensions alone, for example, energy and torque have the same dimensions. For cases where this ambiguity might arise during use of the overloaded interface, a package variable @type_context has been provided so the user can specify. It's not a requirement, but it's possible an inappropriate unit might appear if this variable is not set up. To facilitate output when producing a string representation, the $format_string package variable can be given a sprintf-compatible format string to e.g. restrict the number of decimal places. The following example illustrates usage of the overloaded interface and the variables described above. # simple projectile motion simulation on different planets
use Physics::Unit::Scalar ':ALL';
@Physics::Unit::Scalar::type_context = ('Energy'); # we are working with energy i.e. Joules
$Physics::Unit::Scalar::format_string = "%.3f"; # provide a format string for output
my $m = GetScalar("1 Kg"); # mass
my $u = GetScalar("1 meter per second"); # initial upward velocity
foreach my $body ( qw(mercury earth mars jupiter pluto) ) {
my $a = GetScalar("-1 $body-gravity"); # e.g. "-1 earth-gravity" (-1 for direction vector)
my $t = GetScalar("0 seconds"); # start at t = 0s
print "On " . ucfirst($body) . ":\n"; # so we know which planet we're on
while ( $t < 3.5 ) { # simulate for a few seconds
my $s = $u * $t + (1/2) * $a * $t**2; # 'suvat' equations
my $v = $u + $a * $t;
my $KE = (1/2) * $m * $v**2; # kinetic energy
my $PE = $m * -1 * $a * $s; # potential energy, again -1 for direction
my $TE = $KE + $PE; # total energy (should be constant)
# display with units
print "At $t: dist = $s;\tvel = $v;\tKE = $KE;\tPE = $PE;\tTotal energy = $TE\n";
$t += 0.1; # increment timestep
}
}
AUTHORChris Maloney <voldrani@gmail.com>
|