| 1 | # POD documentation - main docs before the code | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | GenOO::Helper::MyMath - A collection of useful mathematical methods | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | A collection of useful mathematical methods | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | This class contains useful mathematical methods such as mean, round_digits, sigmoid, min, max, glog | ||
| 14 | |||
| 15 | =head1 EXAMPLES | ||
| 16 | |||
| 17 | #mean | ||
| 18 | my @array = [0,20,100]; | ||
| 19 | my $mean = GenOO::Helper::MyMath->mean(\@array); #returns mean (40) | ||
| 20 | |||
| 21 | #round_digits | ||
| 22 | my $number = 1.012345; | ||
| 23 | my $rounded = GenOO::Helper::MyMath->round_digits($number,2); #returns (1.01) | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | # Let the code begin... | ||
| 28 | |||
| 29 | package GenOO::Helper::MyMath; | ||
| 30 | $GenOO::Helper::MyMath::VERSION = '1.5.0'; | ||
| 31 | |||
| 32 | ####################################################################### | ||
| 33 | ####################### Load External modules ##################### | ||
| 34 | ####################################################################### | ||
| 35 | use Modern::Perl; | ||
| 36 | |||
| 37 | |||
| 38 | ####################################################################### | ||
| 39 | ####################### Interface Functions ####################### | ||
| 40 | ####################################################################### | ||
| 41 | sub mean { | ||
| 42 | # Calculates the mean of the values in an array | ||
| 43 | my ($array_ref) = @_; | ||
| 44 | my $n=0; | ||
| 45 | my $result=0; | ||
| 46 | my $sum=0; | ||
| 47 | |||
| 48 | unless (defined $array_ref) { | ||
| 49 | warn "undefined array reference in sub mean\n"; | ||
| 50 | return undef; | ||
| 51 | } | ||
| 52 | |||
| 53 | foreach my $item (@$array_ref) { | ||
| 54 | unless (defined $item) {next;} | ||
| 55 | $sum += $item; | ||
| 56 | $n++; | ||
| 57 | } | ||
| 58 | if ($n==0) {return undef;} | ||
| 59 | |||
| 60 | $result = $sum/$n; | ||
| 61 | return $result; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub round_digits { | ||
| 65 | my ($num,$digits) = @_; | ||
| 66 | |||
| 67 | my $decimal = 10**$digits; | ||
| 68 | my $rounded = (int($num*$decimal))/$decimal; | ||
| 69 | return $rounded; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub sigmoid { | ||
| 73 | return 1/(1+exp(-$_[1])); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub max { | ||
| 77 | # Calculates the max of the values in an array | ||
| 78 | my ($array_ref) = @_; | ||
| 79 | my $max_value; | ||
| 80 | my $max_index; | ||
| 81 | |||
| 82 | unless (defined $array_ref) { | ||
| 83 | warn "undefined array reference in sub max\n"; | ||
| 84 | return (undef,undef); | ||
| 85 | } | ||
| 86 | |||
| 87 | for (my $i=0;$i<@{$array_ref};$i++) { | ||
| 88 | if (defined $$array_ref[$i]) { | ||
| 89 | if ((!defined $max_value) or ($$array_ref[$i] > $max_value)) { | ||
| 90 | $max_value = $$array_ref[$i]; | ||
| 91 | $max_index = $i; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | return ($max_index,$max_value); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub min { | ||
| 100 | # Calculates the min of the values in an array | ||
| 101 | my ($array_ref) = @_; | ||
| 102 | my $min_value; | ||
| 103 | my $min_index; | ||
| 104 | |||
| 105 | unless (defined $array_ref) { | ||
| 106 | warn "undefined array reference in sub min\n"; | ||
| 107 | return (undef,undef); | ||
| 108 | } | ||
| 109 | |||
| 110 | for (my $i=0;$i<@{$array_ref};$i++) { | ||
| 111 | if (defined $$array_ref[$i]) { | ||
| 112 | if ((!defined $min_value) or ($$array_ref[$i] < $min_value)) { | ||
| 113 | $min_value = $$array_ref[$i]; | ||
| 114 | $min_index = $i; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return ($min_index,$min_value); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub glog { | ||
| 123 | my ($value) = @_; | ||
| 124 | my $glog = log($value + sqrt(1 + $value**2))/log(2); | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; |