| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Math::Business::WMA; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 3543 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 37 |  | 
| 4 | 2 |  |  | 2 |  | 7 | use warnings; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 30 |  | 
| 5 | 2 |  |  | 2 |  | 6 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 644 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | 1; | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 0 |  |  | 0 | 0 | 0 | sub tag { (shift)->[-1] } | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 0 |  |  | 0 | 0 | 0 | sub recommended { croak "no recommendation" } | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub new { | 
| 14 | 5 |  |  | 5 | 0 | 169 | my $class = shift; | 
| 15 | 5 |  |  |  |  | 8 | my $this  = bless [], $class; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 5 |  |  |  |  | 8 | my $days = shift; | 
| 18 | 5 | 50 |  |  |  | 9 | if( defined $days ) { | 
| 19 | 5 |  |  |  |  | 16 | $this->set_days( $days ); | 
| 20 |  |  |  |  |  |  | } | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 5 |  |  |  |  | 21 | return $this; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub set_days { | 
| 26 | 5 |  |  | 5 | 0 | 7 | my $this = shift; | 
| 27 | 5 |  |  |  |  | 6 | my $arg  = int(shift); | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 5 | 50 |  |  |  | 9 | croak "days must be a positive non-zero even integer" if $arg <= 0; | 
| 30 | 5 |  |  |  |  | 21 | @$this = ( | 
| 31 |  |  |  |  |  |  | $arg, | 
| 32 |  |  |  |  |  |  | ($arg*($arg+1))/2, | 
| 33 |  |  |  |  |  |  | [],     # the data (we actually need to store it, although we can avoid calculating much of it) | 
| 34 |  |  |  |  |  |  | undef,  # the sum of the data | 
| 35 |  |  |  |  |  |  | undef,  # the the last numerator | 
| 36 |  |  |  |  |  |  | undef,  # the WMA | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | undef,  # tag must be last | 
| 39 |  |  |  |  |  |  | ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 5 |  |  |  |  | 11 | $this->[-1] = "WMA($arg)"; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub insert { | 
| 45 | 4044 |  |  | 4044 | 0 | 302718 | my $this = shift; | 
| 46 | 4044 |  |  |  |  | 5177 | my ($N, $D, $dat, $total_m, $numerator_m, $EMA) = @$this; | 
| 47 |  |  |  |  |  |  |  | 
| 48 | 4044 | 50 |  |  |  | 4888 | croak "You must set the number of days before you try to insert" if not defined $N; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 4044 |  |  |  |  | 3534 | my $old; | 
| 51 | 4044 |  |  |  |  | 5039 | while( defined( my $P = shift ) ) { | 
| 52 | 4044 |  |  |  |  | 4099 | push @$dat, $P; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 4044 | 100 |  |  |  | 4691 | if( @$dat > $N ) { | 
|  |  | 100 |  |  |  |  |  | 
| 55 | 4002 |  |  |  |  | 3570 | $old = shift @$dat; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 4002 |  |  |  |  | 4392 | $numerator_m = $numerator_m + $P*$N - $total_m; | 
| 58 | 4002 |  |  |  |  | 5346 | $total_m =     $total_m + $P - $old; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | } elsif( @$dat == $N ) { | 
| 61 | 5 |  |  |  |  | 8 | $old = 1; | 
| 62 | 5 |  |  |  |  | 6 | my $x = 1; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 5 |  |  |  |  | 9 | $total_m = $numerator_m = 0; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 5 |  |  |  |  | 8 | $numerator_m += $_ for map {$_*$x++} @$dat; | 
|  | 42 |  |  |  |  | 52 |  | 
| 67 | 5 |  |  |  |  | 31 | $total_m += $_ for @$dat; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 4044 | 100 |  |  |  | 9149 | @$this = ($N, $D, $dat, $total_m, $numerator_m, (defined($old) ? $numerator_m/$D:undef)); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub query { | 
| 75 | 4044 |  |  | 4044 | 0 | 6556 | my $this = shift; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 4044 |  |  |  |  | 5317 | return $this->[5]; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | __END__ |