]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/Calc.pm
disabling buggy bzip2-ruby
[biopieces.git] / code_perl / Maasha / Calc.pm
index 4271182b573768a7252af6e5ce9c55219ff5631b..6f83fd09d06dceb3faa2fece3bdc905b425b73a0 100644 (file)
@@ -28,6 +28,7 @@ package Maasha::Calc;
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
+use warnings;
 use strict;
 use Data::Dumper;
 use Storable qw( dclone );
@@ -58,6 +59,27 @@ sub is_a_number
 }
 
 
+sub commify
+{
+    # Martin A. Hansen, October 2009.
+
+    # Insert comma in long numbers.
+
+    my ( $num,   # number to commify
+       ) = @_;
+
+    # Returns a string.
+
+    my ( $copy );
+
+    $copy = $num;
+
+    $copy =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g;
+
+    return $copy;
+}
+
+
 sub dist_point2line
 {
     # Martin A. Hansen, June 2004.
@@ -184,6 +206,39 @@ sub median
 }
 
 
+sub standard_deviation
+{
+    # Martin A. Hansen, September 2008
+
+    # Given a list of numbers calculate and return the standard deviation:
+    # http://en.wikipedia.org/wiki/Standard_deviation
+
+    my ( $numbers,   # list of numbers
+       ) = @_;
+
+    # Returns a float.
+
+    my ( $mean_num, $num, $dev, $dev_sum, $mean_dev, $std_dev );
+
+    $mean_num = mean( $numbers );
+
+    $dev_sum  = 0;
+
+    foreach $num ( @{ $numbers } )
+    {
+        $dev = ( $num - $mean_num ) ** 2;
+    
+        $dev_sum += $dev;
+    }
+
+    $mean_dev = $dev_sum / scalar @{ $numbers };
+
+    $std_dev  = sqrt( $mean_dev );
+
+    return $std_dev;
+}
+
+
 sub min
 {
     # Martin A. Hansen, August 2006.
@@ -331,6 +386,33 @@ sub log10
 }
 
 
+sub interpolate_linear
+{
+    # Martin A. Hansen, February 2010.
+
+    # Given two data points and an x value returns the
+    # interpolant (y).
+    #
+    # Formula for linear interpolation:
+    # http://en.wikipedia.org/wiki/Interpolation#Example
+
+    my ( $x1,
+         $y1,
+         $x2,
+         $y2,
+         $x
+       ) = @_;
+
+    # Returns a float
+
+    my ( $y );
+
+    $y = $y1 + ( $x - $x1 ) * ( ( $y2 - $y1 ) / ( $x2 - $x1 ) );
+
+    return $y;
+}
+
+
 sub overlap
 {
     # Martin A. Hansen, November 2003.