| 1 | package Math::Calc::Units::Compute; | ||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use vars qw(@EXPORT_OK); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 1 | @EXPORT_OK = qw(compute | |||||||||||||||||||||||||||||||||||
| 5 | plus minus mult divide power | ||||||||||||||||||||||||||||||||||||
| 6 | unit_mult unit_divide unit_power | ||||||||||||||||||||||||||||||||||||
| 7 | construct); | ||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Math::Calc::Units::Convert qw(reduce); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Math::Calc::Units::Rank qw(render_unit); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Math::Calc::Units::Convert::Base; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 13 | 1 | require Math::Calc::Units::Grammar; | |||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | 26 | ▶ | sub equivalent { | ||||||||||||||||||||||||||||||||||
|
Subroutine
equivalent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 16 | 26 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 17 | 26 | return Math::Calc::Units::Convert::Base->same($u, $v); | |||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | 16 | ▶ | sub is_unit { | ||||||||||||||||||||||||||||||||||
|
Subroutine
is_unit: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 21 | 16 | my ($x, $unit) = @_; | |||||||||||||||||||||||||||||||||||
| 22 | 16 | return equivalent($x, { $unit => 1 }); | |||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | # All these assume the values are in canonical units. | ||||||||||||||||||||||||||||||||||||
| 26 | 5 | ▶ | sub plus { | ||||||||||||||||||||||||||||||||||
|
Subroutine
plus: called CC=6 SCAR=18.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 27 | 5 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 28 | 5 | $u = reduce($u); | |||||||||||||||||||||||||||||||||||
| 29 | 5 | $v = reduce($v); | |||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||
| 31 | 5 | ▶ | if (equivalent($u->[1], $v->[1])) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 32 | 3 | return [ $u->[0] + $v->[0], $u->[1] ]; | |||||||||||||||||||||||||||||||||||
| 33 | ▶ | } elsif (is_unit($u->[1], 'timestamp') && is_unit($v->[1], 'sec')) { | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
is_unit($u->[1], "timestamp") and is_unit($v->[1], "sec")
MC/DC50%1/2
is_unit($u->[1], "timestamp") and is_unit($v->[1], "sec")
is_unit($u->[1], "timestamp")
is_unit($v->[1], "sec")
Truth table66%2/3
A and B
A= is_unit($u->[1], "timestamp")B= is_unit($v->[1], "sec")
| |||||||||||||||||||||||||||||||||||||
| 34 | 1 | return [ $u->[0] + $v->[0], $u->[1] ]; | |||||||||||||||||||||||||||||||||||
| 35 | ▶ | } elsif (is_unit($u->[1], 'sec') && is_unit($v->[1], 'timestamp')) { | |||||||||||||||||||||||||||||||||||
|
Condition33%1/3
is_unit($u->[1], "sec") and is_unit($v->[1], "timestamp")
MC/DC0%0/2
is_unit($u->[1], "sec") and is_unit($v->[1], "timestamp")
is_unit($u->[1], "sec")
is_unit($v->[1], "timestamp")
Truth table33%1/3
A and B
A= is_unit($u->[1], "sec")B= is_unit($v->[1], "timestamp")
| |||||||||||||||||||||||||||||||||||||
| 36 | 0 | return [ $u->[0] + $v->[0], $v->[1] ]; | |||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||
| 39 | 1 | die "Unable to add incompatible units `".render_unit($u->[1])."' and `".render_unit($v->[1])."'"; | |||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | sub minus { | ||||||||||||||||||||||||||||||||||
|
Subroutine
minus: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 43 | 6 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 44 | 6 | $u = reduce($u); | |||||||||||||||||||||||||||||||||||
| 45 | 6 | $v = reduce($v); | |||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||
| 47 | 6 | ▶ | if (is_unit($u->[1], 'timestamp') && is_unit($v->[1], 'timestamp')) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
is_unit($u->[1], "timestamp") and is_unit($v->[1], "timestamp")
MC/DC100%2/2
is_unit($u->[1], "timestamp") and is_unit($v->[1], "timestamp")
is_unit($u->[1], "timestamp")
is_unit($v->[1], "timestamp")
Truth table100%3/3
A and B
A= is_unit($u->[1], "timestamp")B= is_unit($v->[1], "timestamp")
| |||||||||||||||||||||||||||||||||||||
| 48 | 1 | return [ $u->[0] - $v->[0], { sec => 1 } ]; | |||||||||||||||||||||||||||||||||||
| 49 | } elsif (equivalent($u->[1], $v->[1])) { | ||||||||||||||||||||||||||||||||||||
| 50 | 3 | return [ $u->[0] - $v->[0], $u->[1] ]; | |||||||||||||||||||||||||||||||||||
| 51 | ▶ | } elsif (is_unit($u->[1], 'timestamp') && is_unit($v->[1], 'sec')) { | |||||||||||||||||||||||||||||||||||
|
Condition66%2/3
is_unit($u->[1], "timestamp") and is_unit($v->[1], "sec")
MC/DC50%1/2
is_unit($u->[1], "timestamp") and is_unit($v->[1], "sec")
is_unit($u->[1], "timestamp")
is_unit($v->[1], "sec")
Truth table66%2/3
A and B
A= is_unit($u->[1], "timestamp")B= is_unit($v->[1], "sec")
| |||||||||||||||||||||||||||||||||||||
| 52 | 1 | return [ $u->[0] - $v->[0], $u->[1] ]; | |||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | 1 | die "Unable to subtract incompatible units `".render_unit($u->[1])."' and `".render_unit($v->[1])."'"; | |||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | sub mult { | ||||||||||||||||||||||||||||||||||
|
Subroutine
mult: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 59 | 11 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 60 | 11 | return [ $u->[0] * $v->[0], unit_mult($u->[1], $v->[1]) ]; | |||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||
| 63 | 24 | ▶ | sub divide { | ||||||||||||||||||||||||||||||||||
|
Subroutine
divide: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 64 | 24 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 65 | 24 | return [ $u->[0] / $v->[0], unit_divide($u->[1], $v->[1]) ]; | |||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||
| 68 | 7 | ▶ | sub power { | ||||||||||||||||||||||||||||||||||
|
Subroutine
power: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 69 | 7 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | die "Can only raise to unit-less powers" if keys %{ $v->[1] }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 71 | 6 | $u = reduce($u); | |||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | if (keys %{ $u->[1] } != 0) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 5 | my $power = $v->[0]; | |||||||||||||||||||||||||||||||||||
| 74 | 5 | ▶ | die "Can only raise a value with units to an integral power" | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | if abs($power - int($power)) > 1e-20; | ||||||||||||||||||||||||||||||||||||
| 76 | 4 | return [ $u->[0] ** $power, unit_power($u->[1], $power) ]; | |||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||
| 78 | 1 | return [ $u->[0] ** $v->[0], {} ]; | |||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | 35 | ▶ | sub unit_mult { | ||||||||||||||||||||||||||||||||||
|
Subroutine
unit_mult: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 82 | 35 | my ($u, $v, $mult) = @_; | |||||||||||||||||||||||||||||||||||
| 83 | 35 | ▶ | $mult ||= 1; | ||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$mult ||= 1
MC/DC100%1/1
$mult ||= 1
$mult
Truth table100%2/2
A
A= $mult
| |||||||||||||||||||||||||||||||||||||
| 84 | 35 | ▶ | while (my ($unit, $vp) = each %$v) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 34 | $u->{$unit} += $vp * $mult; | |||||||||||||||||||||||||||||||||||
| 86 | 34 | ▶ | delete $u->{$unit} if $u->{$unit} == 0; # Keep zeroes out! | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | 35 | return $u; | |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | 24 | ▶ | sub unit_divide { | ||||||||||||||||||||||||||||||||||
|
Subroutine
unit_divide: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 92 | 24 | my ($u, $v) = @_; | |||||||||||||||||||||||||||||||||||
| 93 | 24 | return unit_mult($u, $v, -1); | |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | sub unit_power { | ||||||||||||||||||||||||||||||||||
|
Subroutine
unit_power: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 97 | 4 | my ($u, $power) = @_; | |||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | return {} if $power == 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 99 | 4 | $u->{$_} *= $power foreach (keys %$u); | |||||||||||||||||||||||||||||||||||
| 100 | 4 | return $u; | |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||
| 103 | 7 | ▶ | sub construct { | ||||||||||||||||||||||||||||||||||
|
Subroutine
construct: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 104 | 7 | my $s = shift; | |||||||||||||||||||||||||||||||||||
| 105 | 7 | my ($constructor, $args) = $s =~ /^(\w+)\((.*)\)/; | |||||||||||||||||||||||||||||||||||
| 106 | 7 | return Math::Calc::Units::Convert::construct($constructor, $args); | |||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||
| 109 | package Math::Calc::Units::Compute; | ||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | # Poor-man's tokenizer | ||||||||||||||||||||||||||||||||||||
| 112 | 109 | ▶ | sub tokenize { | ||||||||||||||||||||||||||||||||||
|
Subroutine
tokenize: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 113 | 109 | my $data = shift; | |||||||||||||||||||||||||||||||||||
| 114 | 109 | my @tokens = $data =~ m{\s* | |||||||||||||||||||||||||||||||||||
| 115 | ( | ||||||||||||||||||||||||||||||||||||
| 116 | \w+\([^\(\)]*\) # constructed (eg date(2001...)) | ||||||||||||||||||||||||||||||||||||
| 117 | |[\d.]+ # Numbers | ||||||||||||||||||||||||||||||||||||
| 118 | |\w+ # Words | ||||||||||||||||||||||||||||||||||||
| 119 | |\*\* # Exponentiation (**) | ||||||||||||||||||||||||||||||||||||
| 120 | |[-+*/()@] # Operators | ||||||||||||||||||||||||||||||||||||
| 121 | )}xg; | ||||||||||||||||||||||||||||||||||||
| 122 | 109 | ▶ | my @types = map { /\w\(/ ? 'CONSTRUCT' | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||
| 123 | :( /\d/ ? 'NUMBER' | ||||||||||||||||||||||||||||||||||||
| 124 | :( /\w/ ? 'WORD' | ||||||||||||||||||||||||||||||||||||
| 125 | :( $_))) } @tokens; | ||||||||||||||||||||||||||||||||||||
| 126 | 109 | return \@tokens, \@types; | |||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||
| 129 | # compute : string -> <value,unit> | ||||||||||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||||||||||
| 131 | # If the first character of the string is '#', this will attempt to avoid | ||||||||||||||||||||||||||||||||||||
| 132 | # canonicalization as much as possible. | ||||||||||||||||||||||||||||||||||||
| 133 | # | ||||||||||||||||||||||||||||||||||||
| 134 | 109 | ▶ | sub compute { | ||||||||||||||||||||||||||||||||||
|
Subroutine
compute: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 135 | 109 | my $expr = shift; | |||||||||||||||||||||||||||||||||||
| 136 | 109 | my $canonicalize = $expr !~ /^\#/; | |||||||||||||||||||||||||||||||||||
| 137 | 109 | my ($vals, $types) = tokenize($expr); | |||||||||||||||||||||||||||||||||||
| 138 | 442 | ▶ | my $lexer = sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 139 | # print "TOK($vals->[0]) TYPE($types->[0])\n" if @$vals; | ||||||||||||||||||||||||||||||||||||
| 140 | 442 | ▶ | return shift(@$types), shift(@$vals) if (@$types); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 141 | 108 | return ('', undef); | |||||||||||||||||||||||||||||||||||
| 142 | 109 | }; | |||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||
| 144 | 109 | my $parser = new Math::Calc::Units::Grammar; | |||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||
| 146 | my $v = | ||||||||||||||||||||||||||||||||||||
| 147 | $parser->YYParse(yylex => $lexer, | ||||||||||||||||||||||||||||||||||||
| 148 | 0 | ▶ | yyerror => sub { | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||
| 149 | 0 | my $parser = shift; | |||||||||||||||||||||||||||||||||||
| 150 | 0 | die "Error: expected ".join(" ", $parser->YYExpect)." got `".$parser->YYCurtok."', rest=".join(" ", @$types)."\nfrom ".join(" ", @$vals)."\n"; | |||||||||||||||||||||||||||||||||||
| 151 | }, | ||||||||||||||||||||||||||||||||||||
| 152 | 109 | yydebug => 0); # 0x1f); | |||||||||||||||||||||||||||||||||||
| 153 | 105 | ▶ | return $canonicalize ? reduce($v) : $v; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 154 | }; | ||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||
| 156 | 1 | 1; | |||||||||||||||||||||||||||||||||||