[system] / trunk / pg / lib / Units.pm Repository:
ViewVC logotype

Diff of /trunk/pg/lib/Units.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1050 Revision 1079
1#!/usr/math/bin/perl -w
2 1
3 2
4# This is the "exported" subroutine. Use this to evaluate the units given in an answer. 3# This is the "exported" subroutine. Use this to evaluate the units given in an answer.
5 4
6sub evaluate_units { 5sub evaluate_units {
239 'factor' => (10**6)*2*$PI, #10^6 * 2pi, 238 'factor' => (10**6)*2*$PI, #10^6 * 2pi,
240 's' => -1, 239 's' => -1,
241 'rad' => 1 240 'rad' => 1
242 }, 241 },
243 'rev' => { 242 'rev' => {
244 'factor' => 2*$PI, 243 'factor' => 2*$PI,
245 'rad' => 1 244 'rad' => 1
246 }, 245 },
247 'cycles' => { 246 'cycles' => {
248 'factor' => 2*$PI, 247 'factor' => 2*$PI,
249 'rad' => 1 248 'rad' => 1
250 }, 249 },
251 250
252# COMPOUND UNITS 251# COMPOUND UNITS
253# 252#
254# FORCE 253# FORCE
255# N -- Newton 254# N -- Newton
402); 401);
403 402
404 403
405 404
406sub process_unit { 405sub process_unit {
407 406
408 my $string = shift; 407 my $string = shift;
409 die ("UNIT ERROR: No units were defined.") unless defined($string); # 408 die ("UNIT ERROR: No units were defined.") unless defined($string); #
410 #split the string into numerator and denominator --- the separator is / 409 #split the string into numerator and denominator --- the separator is /
411 my ($numerator,$denominator) = split( m{/}, $string ); 410 my ($numerator,$denominator) = split( m{/}, $string );
412 411
413 412
414 413
415 $denominator = "" unless defined($denominator); 414 $denominator = "" unless defined($denominator);
416 my %numerator_hash = process_term($numerator); 415 my %numerator_hash = process_term($numerator);
417 my %denominator_hash = process_term($denominator); 416 my %denominator_hash = process_term($denominator);
418 417
419 418
421 my $u; 420 my $u;
422 foreach $u (keys %unit_hash) { 421 foreach $u (keys %unit_hash) {
423 if ( $u eq 'factor' ) { 422 if ( $u eq 'factor' ) {
424 $unit_hash{$u} = $numerator_hash{$u}/$denominator_hash{$u}; # calculate the correction factor for the unit 423 $unit_hash{$u} = $numerator_hash{$u}/$denominator_hash{$u}; # calculate the correction factor for the unit
425 } else { 424 } else {
426 425
427 $unit_hash{$u} = $numerator_hash{$u} - $denominator_hash{$u}; # calculate the power of the fundamental unit in the unit 426 $unit_hash{$u} = $numerator_hash{$u} - $denominator_hash{$u}; # calculate the power of the fundamental unit in the unit
428 } 427 }
429 } 428 }
430 # return a unit hash. 429 # return a unit hash.
431 return(%unit_hash); 430 return(%unit_hash);
432} 431}
433 432
434sub process_term { 433sub process_term {
435 my $string = shift; 434 my $string = shift;
436 my %unit_hash = %fundamental_units; 435 my %unit_hash = %fundamental_units;
437 if ($string) { 436 if ($string) {
438 437
439 #split the numerator or denominator into factors -- the separators are * 438 #split the numerator or denominator into factors -- the separators are *
440 439
441 my @factors = split(/\*/, $string); 440 my @factors = split(/\*/, $string);
442 441
443 my $f; 442 my $f;
444 foreach $f (@factors) { 443 foreach $f (@factors) {
445 my %factor_hash = process_factor($f); 444 my %factor_hash = process_factor($f);
446 445
447 my $u; 446 my $u;
448 foreach $u (keys %unit_hash) { 447 foreach $u (keys %unit_hash) {
449 if ( $u eq 'factor' ) { 448 if ( $u eq 'factor' ) {
450 $unit_hash{$u} = $unit_hash{$u} * $factor_hash{$u}; # calculate the correction factor for the unit 449 $unit_hash{$u} = $unit_hash{$u} * $factor_hash{$u}; # calculate the correction factor for the unit
451 } else { 450 } else {
452 451
453 $unit_hash{$u} = $unit_hash{$u} + $factor_hash{$u}; # calculate the power of the fundamental unit in the unit 452 $unit_hash{$u} = $unit_hash{$u} + $factor_hash{$u}; # calculate the power of the fundamental unit in the unit
454 } 453 }
455 } 454 }
456 } 455 }
457 } 456 }
458 #returns a unit hash. 457 #returns a unit hash.
459 #print "process_term returns", %unit_hash, "\n"; 458 #print "process_term returns", %unit_hash, "\n";
460 return(%unit_hash); 459 return(%unit_hash);
461} 460}
462 461
463 462
464sub process_factor { 463sub process_factor {
465 my $string = shift; 464 my $string = shift;
466 #split the factor into unit and powers 465 #split the factor into unit and powers
467 466
468 my ($unit_name,$power) = split(/\^/, $string); 467 my ($unit_name,$power) = split(/\^/, $string);
469 $power = 1 unless defined($power); 468 $power = 1 unless defined($power);
470 my %unit_hash = %fundamental_units; 469 my %unit_hash = %fundamental_units;
471 470
472 if ( defined( $known_units{$unit_name} ) ) { 471 if ( defined( $known_units{$unit_name} ) ) {
473 my %unit_name_hash = %{$known_units{$unit_name}}; # $reference_units contains all of the known units. 472 my %unit_name_hash = %{$known_units{$unit_name}}; # $reference_units contains all of the known units.
474 my $u; 473 my $u;
475 foreach $u (keys %unit_hash) { 474 foreach $u (keys %unit_hash) {
476 if ( $u eq 'factor' ) { 475 if ( $u eq 'factor' ) {

Legend:
Removed from v.1050  
changed lines
  Added in v.1079

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9