| 1 | package Astro::Montenbruck::MathUtils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use 5.22.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use feature qw/signatures/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | no warnings qw/experimental::signatures/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | # The line below disables wrong perlcritic warnings | ||||||||||||||||||||||||||||
| 7 | ## no critic qw/Subroutines::ProhibitSubroutinePrototypes/ | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Exporter qw/import/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use POSIX qw (floor ceil acos modf fmod); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use List::Util qw/any reduce/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Math::Trig qw/:pi :radial deg2rad rad2deg/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use constant { ARCS => 3600.0 * 180.0 / pi }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 13 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||
| 17 | all => [ | ||||||||||||||||||||||||||||
| 18 | qw/frac frac360 dms hms zdms ddd polynome sine | ||||||||||||||||||||||||||||
| 19 | reduce_deg reduce_rad to_range opposite_deg opposite_rad | ||||||||||||||||||||||||||||
| 20 | angle_s angle_c angle_c_rad diff_angle polar cart quad | ||||||||||||||||||||||||||||
| 21 | ARCS/ | ||||||||||||||||||||||||||||
| 22 | ], | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | 13 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 25 | 13 | our $VERSION = 0.02; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 18340 | ▶ | sub frac($x) { ( modf($x) )[0] } | ||||||||||||||||||||||||||
|
Subroutine
frac: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub frac360($x) { frac($x) * 360 } | ||||||||||||||||||||||||||
|
Subroutine
frac360: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 45 | ▶ | sub dms ( $x, $places = 3 ) { | ||||||||||||||||||||||||||
|
Subroutine
dms: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 45 | ▶ | return $x if $places == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 29 | my ( $f, $i ) = modf($x); | |||||||||||||||||||||||||||
| 35 | 29 | ▶ | $f = -$f if $i != 0 && $f < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$i != 0 and $f < 0
MC/DC0%0/2
$i != 0 and $f < 0
$i != 0
$f < 0
Truth table66%2/3
A and B
A= $i != 0B= $f < 0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 29 | ( $i, dms( $f * 60, $places - 1 ) ); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub hms { dms @_ } | ||||||||||||||||||||||||||
|
Subroutine
hms: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | ▶ | sub zdms($x) { | ||||||||||||||||||||||||||
|
Subroutine
zdms: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 12 | my ( $d, $m, $s ) = dms($x); | |||||||||||||||||||||||||||
| 44 | 12 | my $z = int( $d / 30 ); | |||||||||||||||||||||||||||
| 45 | 12 | $d %= 30; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 12 | $z, $d, $m, $s; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 110 | ▶ | sub ddd(@args) { | ||||||||||||||||||||||||||
|
Subroutine
ddd: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 110 | ▶ | my $b = any { $_ < 0 } @args; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 110 | ▶ | my $sgn = $b ? -1 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 110 | ▶ | my ( $d, $m, $s ) = map { abs( $args[$_] || 0 ) } ( 0 .. 2 ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$args[$_] || 0
MC/DC100%1/1
$args[$_] || 0
$args[$_]
Truth table100%2/2
A
A= $args[$_]
| |||||||||||||||||||||||||||||
| 54 | 110 | return $sgn * ( $d + ( $m + $s / 60.0 ) / 60.0 ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 83 | ▶ | sub polynome ( $t, @terms ) { | ||||||||||||||||||||||||||
|
Subroutine
polynome: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 83 | ▶ | reduce { $a * $t + $b } reverse @terms; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 74 | ▶ | sub to_range ( $x, $limit ) { | ||||||||||||||||||||||||||
|
Subroutine
to_range: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 74 | $x = fmod( $x, $limit ); | |||||||||||||||||||||||||||
| 63 | 74 | ▶ | $x < 0 ? $x + $limit : $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | #sub reduce_deg($x) { to_range( $x, 360 ) } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 920 | ▶ | sub reduce_deg($x) { | ||||||||||||||||||||||||||
|
Subroutine
reduce_deg: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 920 | my $res = Math::Trig::deg2deg($x); | |||||||||||||||||||||||||||
| 70 | 920 | ▶ | $res < 0 ? $res + 360 : $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #sub reduce_rad($x) { to_range( $x, pi2 ) } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1385 | ▶ | sub reduce_rad($x) { | ||||||||||||||||||||||||||
|
Subroutine
reduce_rad: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 1385 | my $res = Math::Trig::rad2rad($x); | |||||||||||||||||||||||||||
| 77 | 1385 | ▶ | $res < 0 ? $res + pi2 : $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4600 | ▶ | sub sine($x) { sin( pi2 * frac($x) ) } | ||||||||||||||||||||||||||
|
Subroutine
sine: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 4 | ▶ | sub opposite_deg($x) { reduce_deg( $x + 180 ) } | ||||||||||||||||||||||||||
|
Subroutine
opposite_deg: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | sub opposite_rad($x) { reduce_rad( $x + pi ) } | ||||||||||||||||||||||||||
|
Subroutine
opposite_rad: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 27 | ▶ | sub angle_c ( $a, $b ) { | ||||||||||||||||||||||||||
|
Subroutine
angle_c: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 27 | my $x = abs( $a - $b ); | |||||||||||||||||||||||||||
| 88 | 27 | ▶ | $x > 180 ? 360 - $x : $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | ▶ | sub angle_c_rad ( $a, $b ) { | ||||||||||||||||||||||||||
|
Subroutine
angle_c_rad: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 4 | my $x = abs( $a - $b ); | |||||||||||||||||||||||||||
| 93 | 4 | ▶ | $x > pi ? pi2 - $x : $x; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub angle_s { | ||||||||||||||||||||||||||
|
Subroutine
angle_s: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 1 | my ( $x1, $y1, $x2, $y2 ) = map { deg2rad $_ } @_; | |||||||||||||||||||||||||||
| 98 | 1 | rad2deg( | |||||||||||||||||||||||||||
| 99 | acos( sin($y1) * sin($y2) + cos($y1) * cos($y2) * cos( $x1 - $x2 ) ) ); | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 18 | ▶ | sub diff_angle($a, $b, $mode = 'degrees') { | ||||||||||||||||||||||||||
|
Subroutine
diff_angle: called CC=8 SCAR=21.8
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 18 | my $m = lc $mode; | |||||||||||||||||||||||||||
| 104 | 18 | ▶ | my $whole = $m eq 'degrees' ? 360 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 105 | : $m eq 'radians' ? pi2 | ||||||||||||||||||||||||||||
| 106 | : undef; | ||||||||||||||||||||||||||||
| 107 | 18 | ▶ | die "Expected 'degrees' or 'radians' mode" unless $whole; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 18 | ▶ | my $half = $m eq 'degrees' ? 180 : pi; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 18 | ▶ | my $x = $b < $a ? $b + $whole : $b; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 18 | $x -= $a; | |||||||||||||||||||||||||||
| 111 | 18 | ▶ | return $x - $whole if $x > $half; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 9 | return $x; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 220 | ▶ | sub cart( $r, $theta, $phi ) { | ||||||||||||||||||||||||||
|
Subroutine
cart: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 220 | my $rcst = $r * cos($theta); | |||||||||||||||||||||||||||
| 118 | 220 | $rcst * cos($phi), $rcst * sin($phi), $r * sin($theta); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # in previous versions was named 'polar' | ||||||||||||||||||||||||||||
| 122 | 721 | ▶ | sub polar ( $x, $y, $z ) { | ||||||||||||||||||||||||||
|
Subroutine
polar: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 721 | my $rho = $x * $x + $y * $y; | |||||||||||||||||||||||||||
| 124 | 721 | my $r = sqrt( $rho + $z * $z ); | |||||||||||||||||||||||||||
| 125 | 721 | my $phi = atan2( $y, $x ); | |||||||||||||||||||||||||||
| 126 | 721 | ▶ | $phi += pi2 if $phi < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 721 | $rho = sqrt($rho); | |||||||||||||||||||||||||||
| 128 | 721 | my $theta = atan2( $z, $rho ); | |||||||||||||||||||||||||||
| 129 | 721 | $r, $theta, $phi; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 312 | ▶ | sub quad { | ||||||||||||||||||||||||||
|
Subroutine
quad: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 312 | my ( $y_minus, $y_0, $y_plus ) = @_; | |||||||||||||||||||||||||||
| 134 | 312 | my $nz = 0; | |||||||||||||||||||||||||||
| 135 | 312 | my $a = 0.5 * ( $y_minus + $y_plus ) - $y_0; | |||||||||||||||||||||||||||
| 136 | 312 | my $b = 0.5 * ( $y_plus - $y_minus ); | |||||||||||||||||||||||||||
| 137 | 312 | my $c = $y_0; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 312 | my $xe = -$b / ( 2 * $a ); | |||||||||||||||||||||||||||
| 140 | 312 | my $ye = ( $a * $xe + $b ) * $xe + $c; | |||||||||||||||||||||||||||
| 141 | 312 | my $dis = $b * $b - 4 * $a * $c; # discriminant of y = axx+bx+c | |||||||||||||||||||||||||||
| 142 | 312 | my @zeroes; | |||||||||||||||||||||||||||
| 143 | 312 | ▶ | if ( $dis >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # parabola intersects x-axis | ||||||||||||||||||||||||||||
| 146 | 305 | my $dx = 0.5 * sqrt($dis) / abs($a); | |||||||||||||||||||||||||||
| 147 | 305 | @zeroes[ 0, 1 ] = ( $xe - $dx, $xe + $dx ); | |||||||||||||||||||||||||||
| 148 | 305 | ▶ | $nz++ if abs( $zeroes[0] ) <= 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 305 | ▶ | $nz++ if abs( $zeroes[1] ) <= 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 305 | ▶ | $zeroes[0] = $zeroes[1] if $zeroes[0] < -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 312 | $nz, $xe, $ye, @zeroes; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 13 | 1; # End of Astro::Montenbruck::Core::MathUtils | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||