blib/lib/Math/LinearApprox.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 22 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 100% 94 / 94 CC 12 SCAR 10.1
CC
12
Cov
100%
new
5.0
equation
3.0
equation_str
3.0
CRAP 2.7
↑ Summary
1 #!/usr/bin/perl
2 #made by: KorG
3 # vim: sw=4 ts=4 et cc=79 :
4  
5 package Math::LinearApprox;
6  
7 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = '0.02';
14 4 $VERSION =~ tr/_//d;
15  
16 4 our @EXPORT_OK = qw( linear_approx linear_approx_str );
17  
18 ##
19 # @brief Model constructor
20 # @param __PACKAGE__
21 # @param (optional) ARRAYref with points to add ( x1, y1, x2, y2, ... )
22 # @return blessed reference to empty model
23 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
24 11 my $self = bless {
25 x_sum => 0,
26 y_sum => 0,
27 N => 0,
28 delta => 0,
29 }, __PACKAGE__;
30  
31 # Handle array, if any
32 11 ▶ if (ref $_[1] eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'ARRAY') { } 8 3 11
33 8 my $half = @{$_[1]} / 2;
34 8 ▶ croak "Array has odd number of elements!" if int $half != $half;
Branch: 1 branch
BranchTrueFalseTotal
if int $half != $half 1 7 8
35 7 ▶ for (my $i = 0; $i < @{$_[1]}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[1];}) 18 7 25
36 18 $self->add_point($_[1]->[$i], $_[1]->[$i + 1]);
37 }
38 } else {
39 3 ▶ croak "Unknown argument specified!" if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 1 2 3
40 }
41  
42 9 return $self;
43 }
44  
45 ##
46 # @brief Translate two points into line equation (coefficients)
47 # @param $_[0] X_1 coordinate
48 # @param $_[1] Y_1 coordinate
49 # @param $_[2] X_2 coordinate
50 # @param $_[3] Y_2 coordinate
51 # @return ($A, $B) for equation [y = Ax + B]
52 11 ▶sub _eq_by_points {
Subroutine _eq_by_points: called CC=2 SCAR=6.9
53 11 ▶ die "X_1 == X_2" if $_[0] == $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] == $_[2] 1 10 11
54  
55 10 my $A = ($_[3] - $_[1]) / ($_[2] - $_[0]);
56 10 my $B = $_[3] - ($_[2] * ($_[3] - $_[1])) / ($_[2] - $_[0]);
57  
58 10 return ($A, $B);
59 }
60  
61 ##
62 # @brief Get numeric equation of model
63 # @param $_[0] self reference
64 # @return undef or ($A, $B) for equation [y = Ax + B]
65 13 ▶sub equation {
Subroutine equation: called CC=3 SCAR=11.0
Pod documented
66 # Check conditions
67 # - check points number
68 13 ▶ return unless $_[0]->{N} > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"N"} > 1 2 11 13
69 # - handle vertical lines
70 11 ▶ return if $_[0]->{x_last} == $_[0]->{x_0};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"x_last"} == $_[0]{"x_0"} 1 10 11
71  
72 # Calculate means
73 10 my $M_delta = $_[0]->{delta} / ( $_[0]->{x_last} - $_[0]->{x_0} );
74 10 my $M_x = $_[0]->{x_sum} / $_[0]->{N};
75 10 my $M_y = $_[0]->{y_sum} / $_[0]->{N};
76  
77 # Translate them into a line
78 10 my ($A, $B) = _eq_by_points($M_x, $M_y, $M_x + 1, $M_y + $M_delta);
79  
80 # Return coefficients
81 10 return ($A, $B);
82 }
83  
84 ##
85 # @brief Get stringified equation of model
86 # @param $_[0] self reference
87 # @return die or String in forms: "y = A * x + B", "x = X"
88 8 ▶sub equation_str {
Subroutine equation_str: called CC=3 SCAR=11.0
Pod documented
89 8 my ($A, $B) = $_[0]->equation();
90  
91 8 ▶ unless (defined $A) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $A) 3 5 8
92 3 ▶ die "Too few points in model!" if $_[0]->{N} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"N"} == 0 1 2 3
93  
94 # Calculate avg
95 2 my $avg = $_[0]->{x_sum} / $_[0]->{N};
96 2 return "x = $avg";
97 }
98  
99 5 return "y = $A * x + $B";
100 }
101  
102 ##
103 # @brief Add new point to model
104 # @param $_[0] self reference
105 # @param $_[1] X coordinate
106 # @param $_[2] Y coordinate
107 # @return Nothing
108 21 ▶sub add_point {
Subroutine add_point: called CC=3 SCAR=11.0
Pod documented
109 # Save first point
110 21 ▶ $_[0]->{x_0} = $_[1] unless defined $_[0]->{x_0};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0]{"x_0"} 8 13 21
111  
112 # Sum up Y deltas
113 21 ▶ $_[0]->{delta} += $_[2] - $_[0]->{y_last} if $_[0]->{N} != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"N"} != 0 13 8 21
114  
115 # Append the point to sums
116 21 $_[0]->{x_sum} += $_[1];
117 21 $_[0]->{y_sum} += $_[2];
118  
119 # Save right-most coordinates
120 21 $_[0]->{x_last} = $_[1];
121 21 $_[0]->{y_last} = $_[2];
122  
123 # Increase x, y counters
124 21 $_[0]->{N}++;
125 }
126  
127 ##
128 # @brief Decorators for procedural style
129 1 ▶sub linear_approx { return __PACKAGE__->new($_[0])->equation(); }
Subroutine linear_approx: called CC=1 SCAR=0.0
Pod documented
130 1 ▶sub linear_approx_str { return __PACKAGE__->new($_[0])->equation_str(); }
Subroutine linear_approx_str: called CC=1 SCAR=0.0
Pod documented
131  
132 4 1;
133  
134 __END__
↑ Summary