3 # Copyright (C) 2007 Martin A. Hansen.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # http://www.gnu.org/copyleft/gpl.html
22 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # This modules contains subroutines for simple algebra.
28 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
34 use Storable qw( dclone );
35 use vars qw ( @ISA @EXPORT );
38 @ISA = qw( Exporter );
41 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
46 # Identify if a string is a number or not.
47 # Taken from perldoc -q 'is a number'.
49 my ( $str, # string to test
54 if ( $str =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) {
64 # Martin A. Hansen, October 2009.
66 # Insert comma in long numbers.
68 my ( $num, # number reference to commify
71 ${ $num } =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
77 # Martin A. Hansen, June 2004.
79 # calculates the distance from at point to a line.
80 # the line is represented by a beg/end set of coordinates.
82 my ( $px, # point x coordinate
83 $py, # point y coordinate
84 $x1, # line 1 x coordinate
85 $y1, # line 1 y coordinate
86 $x2, # line 2 x coordinate
87 $y2, # line 2 y coordinate
94 $a = ( $y2 - $y1 ) / ( $x2 - $x1 );
98 $dist = abs( $a * $px + $b - $py ) / sqrt( $a ** 2 + 1 );
106 # Martin A. Hansen, April 2004.
108 # calculates the distance between two set of coordinates
120 $dist = sqrt( ( $x2 - $x1 ) ** 2 + ( $y2 - $y1 ) ** 2 );
128 # Martin A. Hansen, February 2008.
130 # Returns the distance between two given intervals.
131 # 0 indicates that the intervals are overlapping.
141 if ( $beg2 > $end1 ) {
142 return $beg2 - $end1;
143 } elsif ( $beg1 > $end2 ) {
144 return $beg1 - $end2;
153 # Martin A. Hansen, April 2007
155 # Given a list of numbers, calculates and returns the mean.
157 my ( $numbers, # list of numbers
160 # returns decimal number
166 map { $sum += $_ } @{ $numbers };
168 $mean = $sum / @{ $numbers };
176 # Martin A. Hansen, January 2008
178 # Given a list of numbers, calculates and returns the median.
180 my ( $numbers, # list of numbers
183 # returns decimal number
185 my ( $num, $median );
187 @{ $numbers } = sort { $a <=> $b } @{ $numbers };
189 $num = scalar @{ $numbers };
191 if ( $num % 2 == 0 ) {
192 $median = mean( [ $numbers->[ $num / 2 ], $numbers->[ $num / 2 + 1 ] ] );
194 $median = $numbers->[ int( $num / 2 ) ];
201 sub standard_deviation
203 # Martin A. Hansen, September 2008
205 # Given a list of numbers calculate and return the standard deviation:
206 # http://en.wikipedia.org/wiki/Standard_deviation
208 my ( $numbers, # list of numbers
213 my ( $mean_num, $num, $div, $div_sum, $mean_div, $std_div );
215 $mean_num = mean( $numbers );
219 foreach $num ( @{ $numbers } )
221 $div = ( $num - $mean_num ) ** 2;
226 $mean_div = $div_sum / scalar @{ $numbers };
228 $std_div = sqrt( abs( $mean_div ) );
236 # Martin A. Hansen, August 2006.
238 # Return the smallest of two given numbers.
240 my ( $x, # first number
255 # Martin A. Hansen, November 2006.
257 # Return the largest of two given numbers.
259 my ( $x, # first number
275 # Martin A. Hansen, April 2007.
277 # given a list of numbers returns a tuple with min and max
279 my ( $list, # list of numbers
284 my ( $num, $min, $max );
286 $min = $max = $list->[ 0 ];
288 foreach $num ( @{ $list } )
290 $min = $num if $num < $min;
291 $max = $num if $num > $max;
294 return wantarray ? ( $min, $max ) : [ $min, $max ];
300 # Martin A. Hansen, August 2007.
302 # Returns the maximum number in a given list.
304 my ( $list, # list of numbers
313 foreach $num ( @{ $list } ) {
314 $max = $num if $num > $max;
323 # Martin A. Hansen, August 2007.
325 # Returns the minimum number in a given list.
327 my ( $list, # list of numbers
336 foreach $num ( @{ $list } ) {
337 $min = $num if $num < $min;
346 # Martin A. Hansen, April 2007.
348 # Sums a list of given numbers and
351 my ( $list, # list of numbers
360 map { $sum += $_ } @{ $list };
368 # Martin A. Hansen, August 2008.
370 # Calculate the log10 of a given number.
377 return log( $num ) / log( 10 );
383 # Martin A. Hansen, November 2003.
385 # Tests if two invervals overlap
386 # returns 1 if overlapping else 0.
396 if ( $beg1 > $end1 ) { ( $beg1, $end1 ) = ( $end1, $beg1 ) };
397 if ( $beg2 > $end2 ) { ( $beg2, $end2 ) = ( $end2, $beg2 ) };
399 if ( $end1 < $beg2 or $beg1 > $end2 ) {
407 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<