| 1 | |||||||||||||||||||||||||||||
| 2 | =encoding utf8 | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | Math::Symbolic::AuxFunctions - Auxiliary functions for Math::Symbolic hierarchy | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | use Math::Symbolic::AuxFunctions; | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Math::Symbolic::AuxFunctions::acos($x); | ||||||||||||||||||||||||||||
| 13 | # etc | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | This module contains implementations of some auxiliary functions that are | ||||||||||||||||||||||||||||
| 18 | used within the Math::Symbolic hierarchy of modules. In particular, this | ||||||||||||||||||||||||||||
| 19 | module holds all trigonometric functions used for numeric evaluation of | ||||||||||||||||||||||||||||
| 20 | trees by Math::Symbolic::Operator. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head2 EXPORT | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | None. On purpose. If I wished this module would pollute others' namespaces, | ||||||||||||||||||||||||||||
| 25 | I'd have put the functions right where they're used. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | package Math::Symbolic::AuxFunctions; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 23 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 23 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 23 | ▶ | use Math::Symbolic::ExportConstants qw/:all/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 23 | ▶ | use Memoize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 23 | our $VERSION = '0.613'; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 TRIGONOMETRIC FUNCTIONS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 tan | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Computes the tangent sin(x) / cos(x). | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 20 | ▶ | sub tan { sin( $_[0] ) / cos( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
tan: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head2 cot | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Computes the cotangent cos(x) / sin(x). | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 20 | ▶ | sub cot { cos( $_[0] ) / sin( $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
cot: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head2 asin | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Computes the arc sine asin(z) = -i log(iz + sqrt(1-z*z)). | ||||||||||||||||||||||||||||
| 63 | Above formula is for complex numbers. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 20 | ▶ | sub asin { atan2( $_[0], sqrt( 1 - $_[0] * $_[0] ) ) } | ||||||||||||||||||||||||||
|
Subroutine
asin: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head2 acos | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Computes the arc cosine acos(z) = -i log(z + sqrt(z*z-1)). | ||||||||||||||||||||||||||||
| 72 | Above formula is for complex numbers. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 20 | ▶ | sub acos { atan2( sqrt( 1 - $_[0] * $_[0] ), $_[0] ) } | ||||||||||||||||||||||||||
|
Subroutine
acos: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head2 atan | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Computes the arc tangent atan(z) = i/2 log((i+z) / (i-z)). | ||||||||||||||||||||||||||||
| 81 | Above formula is for complex numbers. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 20 | ▶ | sub atan { atan2( $_[0], 1 ) } | ||||||||||||||||||||||||||
|
Subroutine
atan: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head2 acot | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Computes the arc cotangent ( atan( 1 / x ) ). | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 20 | ▶ | sub acot { atan2( 1 / $_[0], 1 ) } | ||||||||||||||||||||||||||
|
Subroutine
acot: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 asinh | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Computes the arc hyperbolic sine asinh(z) = log(z + sqrt(z*z+1)) | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 20 | ▶ | sub asinh { log( $_[0] + sqrt( $_[0] * $_[0] + 1 ) ) } | ||||||||||||||||||||||||||
|
Subroutine
asinh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 acosh | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Computes the arc hyperbolic cosine acosh(z) = log(z + sqrt(z*z-1)). | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 20 | ▶ | sub acosh { log( $_[0] + sqrt( $_[0] * $_[0] - 1 ) ) } | ||||||||||||||||||||||||||
|
Subroutine
acosh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head1 OTHER FUNCTIONS | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 binomial_coeff | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Calculates the binomial coefficient n over k of its first two | ||||||||||||||||||||||||||||
| 118 | arguments (n, k). | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Code taken from Orwant et al, "Mastering Algorithms with Perl" | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 23 | memoize('binomial_coeff'); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 57 | ▶ | sub binomial_coeff { | ||||||||||||||||||||||||||
|
Subroutine
binomial_coeff: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 127 | 57 | my ( $n, $k ) = @_; | |||||||||||||||||||||||||||
| 128 | 57 | my ( $res, $j ) = ( 1, 1 ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 57 | ▶ | return 0 if $k > $n || $k < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$k > $n or $k < 0
MC/DC100%2/2
$k > $n or $k < 0
$k > $n
$k < 0
Truth table100%3/3
A or B
A= $k > $nB= $k < 0
| |||||||||||||||||||||||||||||
| 131 | 55 | ▶ | $k = ( $n - $k ) if ( $n - $k ) < $k; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 55 | ▶ | while ( $j <= $k ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 70 | $res *= $n--; | |||||||||||||||||||||||||||
| 135 | 70 | $res /= $j++; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 55 | return $res; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 bell_number | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | The Bell numbers are defined as follows: | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | B_0 = 1 | ||||||||||||||||||||||||||||
| 145 | B_n+1 = sum_k=0_to_n( B_k * binomial_coeff(n, k) ) | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | This function uses memoization. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 23 | memoize('bell_number'); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 12 | ▶ | sub bell_number { | ||||||||||||||||||||||||||
|
Subroutine
bell_number: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 154 | 12 | my $n = shift; | |||||||||||||||||||||||||||
| 155 | 12 | ▶ | return undef if $n < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 11 | ▶ | return 1 if $n == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 10 | my $bell = 0; | |||||||||||||||||||||||||||
| 158 | 10 | $bell += bell_number($_) * binomial_coeff( $n - 1, $_ ) for 0 .. $n - 1; | |||||||||||||||||||||||||||
| 159 | 10 | return $bell; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 23 | 1; | |||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||