| 1 | package MDK::Common::Math; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | MDK::Common::Math - miscellaneous math functions | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | use MDK::Common::Math qw(:all); | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 EXPORTS | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =over | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =item $PI | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | the well-known constant | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =item even(INT) | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =item odd(INT) | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | is the number even or odd? | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =item sqr(FLOAT) | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | C<sqr(3)> gives C<9> | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =item sign(FLOAT) | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | returns a value in { -1, 0, 1 } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =item round(FLOAT) | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | C<round(1.2)> gives C<1>, C<round(1.6)> gives C<2> | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =item round_up(FLOAT, INT) | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | returns the number rounded up to the modulo: | ||||||||||||||||||||
| 40 | C<round_up(11,10)> gives C<20> | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =item round_down(FLOAT, INT) | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | returns the number rounded down to the modulo: | ||||||||||||||||||||
| 45 | C<round_down(11,10)> gives C<10> | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =item divide(INT, INT) | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | integer division (which is lacking in perl). In array context, also returns the remainder: | ||||||||||||||||||||
| 50 | C<($a, $b) = divide(10,3)> gives C<$a is 3> and C<$b is 1> | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =item min(LIST) | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =item max(LIST) | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | returns the minimum/maximum number in the list | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =item or_(LIST) | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | is there a true value in the list? | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =item and_(LIST) | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | are all values true in the list? | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | =item sum(LIST) | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | =item product(LIST) | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | returns the sum/product of all the element in the list | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =item factorial(INT) | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | C<factorial(4)> gives C<24> (4*3*2) | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =back | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =head1 OTHER | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | the following functions are provided, but not exported: | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =over | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =item factorize(INT) | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | C<factorize(40)> gives C<([2,3], [5,1])> as S<40 = 2^3 + 5^1> | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =item decimal2fraction(FLOAT) | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | C<decimal2fraction(1.3333333333)> gives C<(4, 3)> | ||||||||||||||||||||
| 91 | ($PRECISION is used to decide which precision to use) | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =item poly2(a,b,c) | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | Solves the a*x2+b*x+c=0 polynomial: | ||||||||||||||||||||
| 96 | C<poly2(1,0,-1)> gives C<(1, -1)> | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =item permutations(n,p) | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | A(n,p) | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =item combinaisons(n,p) | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | C(n,p) | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =back | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | =head1 SEE ALSO | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | L<MDK::Common> | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =cut | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 1 | ▶ | use Exporter; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 116 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||
| 117 | 1 | our @EXPORT_OK = qw($PI even odd sqr sign round round_up round_down divide min max or_ and_ sum product factorial); | |||||||||||||||||||
| 118 | 1 | our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 1 | our $PRECISION = 10; | |||||||||||||||||||
| 122 | 1 | our $PI = 3.1415926535897932384626433832795028841972; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | ▶ | sub even { $_[0] % 2 == 0 } | ||||||||||||||||||
|
Subroutine
even: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 125 | 0 | ▶ | sub odd { $_[0] % 2 == 1 } | ||||||||||||||||||
|
Subroutine
odd: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 126 | 0 | ▶ | sub sqr { $_[0] * $_[0] } | ||||||||||||||||||
|
Subroutine
sqr: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 127 | 0 | ▶ | sub sign { $_[0] <=> 0 } | ||||||||||||||||||
|
Subroutine
sign: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 128 | 0 | ▶ | sub round { int($_[0] + 0.5) } | ||||||||||||||||||
|
Subroutine
round: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 129 | 0 | ▶ | sub round_up { my ($i, $r) = @_; $r ||= 1; $i = int $i; $i += $r - ($i + $r - 1) % $r - 1 } | ||||||||||||||||||
|
Subroutine
round_up: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$r ||= 1
MC/DC0%0/1
$r ||= 1
$r
Truth table0%0/2
A
A= $r
| |||||||||||||||||||||
| 130 | 0 | ▶ | sub round_down { my ($i, $r) = @_; $r ||= 1; $i = int $i; $i -= $i % $r } | ||||||||||||||||||
|
Subroutine
round_down: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/2
$r ||= 1
MC/DC0%0/1
$r ||= 1
$r
Truth table0%0/2
A
A= $r
| |||||||||||||||||||||
| 131 | 0 | ▶ | sub divide { my $d = int $_[0] / $_[1]; wantarray() ? ($d, $_[0] % $_[1]) : $d } | ||||||||||||||||||
|
Subroutine
divide: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 0 | ▶ | sub min { my $n = shift; $_ < $n and $n = $_ foreach @_; $n } | ||||||||||||||||||
|
Subroutine
min: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 133 | 0 | ▶ | sub max { my $n = shift; $_ > $n and $n = $_ foreach @_; $n } | ||||||||||||||||||
|
Subroutine
max: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||
| 134 | 0 | ▶ | sub or_ { my $n = 0; $n ||= $_ foreach @_; $n } | ||||||||||||||||||
|
Subroutine
or_: not called CC=3 SCAR=24.8
Pod
documented
Condition0%0/3
$n ||= $_
MC/DC0%0/2
$n ||= $_
$n
$_
Truth table0%0/3
A ||= B
A= $nB= $_
| |||||||||||||||||||||
| 135 | 0 | ▶ | sub and_ { my $n = 1; $n &&= $_ foreach @_; $n } | ||||||||||||||||||
|
Subroutine
and_: not called CC=3 SCAR=24.8
Pod
documented
Condition0%0/3
$n &&= $_
MC/DC0%0/2
$n &&= $_
$n
$_
Truth table0%0/3
A &&= B
A= $nB= $_
| |||||||||||||||||||||
| 136 | 0 | ▶ | sub sum { my $n = 0; $n += $_ foreach @_; $n } | ||||||||||||||||||
|
Subroutine
sum: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 137 | 0 | ▶ | sub product { my $n = 1; $n *= $_ foreach @_; $n } | ||||||||||||||||||
|
Subroutine
product: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 0 | ▶ | sub factorize { | ||||||||||||||||||
|
Subroutine
factorize: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||
| 141 | 0 | my ($n) = @_; | |||||||||||||||||||
| 142 | 0 | my @r; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | $n == 1 and return [ 1, 1 ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 145 | 0 | ▶ | for (my $k = 2; sqr($k) <= $n; $k++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 146 | 0 | my $i = 0; | |||||||||||||||||||
| 147 | 0 | ▶ | for ($i = 0; $n % $k == 0; $i++) { $n /= $k } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | 0 | ▶ | $i and push @r, [ $k, $i ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 0 | ▶ | $n > 1 and push @r, [ $n, 1 ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 0 | @r; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | sub decimal2fraction { # ex: 1.33333333 -> (4, 3) | ||||||||||||||||||
|
Subroutine
decimal2fraction: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 155 | 0 | my $n0 = shift; | |||||||||||||||||||
| 156 | 0 | ▶ | my $precision = 10 ** -(shift || $PRECISION); | ||||||||||||||||||
|
Condition0%0/3
shift() || $PRECISION
MC/DC0%0/2
shift() || $PRECISION
shift()
$PRECISION
Truth table0%0/3
A || B
A= shift()B= $PRECISION
| |||||||||||||||||||||
| 157 | 0 | my ($a, $b) = (int $n0, 1); | |||||||||||||||||||
| 158 | 0 | my ($c, $d) = (1, 0); | |||||||||||||||||||
| 159 | 0 | my $n = $n0 - int $n0; | |||||||||||||||||||
| 160 | 0 | my $k; | |||||||||||||||||||
| 161 | 0 | ▶ | until (abs($n0 - $a / $c) < $precision) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | 0 | $n = 1 / $n; | |||||||||||||||||||
| 163 | 0 | $k = int $n; | |||||||||||||||||||
| 164 | 0 | ($a, $b) = ($a * $k + $b, $a); | |||||||||||||||||||
| 165 | 0 | ($c, $d) = ($c * $k + $d, $c); | |||||||||||||||||||
| 166 | 0 | $n -= $k; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | 0 | ($a, $c); | |||||||||||||||||||
| 169 | } | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 0 | ▶ | sub poly2 { | ||||||||||||||||||
|
Subroutine
poly2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 172 | 0 | my ($a, $b, $c) = @_; | |||||||||||||||||||
| 173 | 0 | my $d = ($b**2 - 4 * $a * $c) ** 0.5; | |||||||||||||||||||
| 174 | 0 | (-$b + $d) / 2 / $a, (-$b - $d) / 2 / $a; | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | # A(n,p) | ||||||||||||||||||||
| 178 | 0 | ▶ | sub permutations { | ||||||||||||||||||
|
Subroutine
permutations: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 179 | 0 | my ($n, $p) = @_; | |||||||||||||||||||
| 180 | 0 | my ($r, $i); | |||||||||||||||||||
| 181 | 0 | ▶ | for ($r = 1, $i = 0; $i < $p; $i++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | 0 | $r *= $n - $i; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||
| 184 | 0 | $r; | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | # C(n,p) | ||||||||||||||||||||
| 188 | 0 | ▶ | sub combinaisons { | ||||||||||||||||||
|
Subroutine
combinaisons: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 189 | 0 | my ($n, $p) = @_; | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 0 | permutations($n, $p) / factorial($p); | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 0 | ▶ | sub factorial { permutations($_[0], $_[0]) } | ||||||||||||||||||
|
Subroutine
factorial: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 1 | 1; | |||||||||||||||||||