lib/Math/MatrixDecomposition/Util.pm

filter: Statementstmt 94.0% 47 / 50 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 93.8% 76 / 81 CC 8 SCAR 6.2
CC
8
Cov
92%
hypot
3.0
cdiv
2.1
INIT
2.0
CRAP 1.9
« blib/lib/Math/MatrixDecomposition/Util.pm ↑ Summary
1 ## Math/MatrixDecomposition/Util.pm --- utility functions.
2  
3 # Copyright (C) 2010 Ralph Schleicher. All rights reserved.
4  
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7  
8 ## Code:
9  
10 package Math::MatrixDecomposition::Util;
11  
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use POSIX qw(fmod);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 BEGIN
18 {
19 3 ▶ our $VERSION = '1.04';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 our @EXPORT_OK = qw(eps mod min max sign hypot cdiv);
21 3 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
22 }
23  
24 # Machine precision.
25 3 my $epsilon = 1.0;
26  
27 38 ▶*eps = sub () { $epsilon; };
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 INIT
30 {
31 3 ▶ my $tem;
Subroutine INIT: called CC=2 SCAR=6.9
32  
33 3 while (1)
34 {
35 159 $tem = 1.0 + $epsilon / 2.0;
36 159 ▶ last if $tem == 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $tem == 1 3 156 159
37 156 $epsilon /= 2.0;
38 }
39 }
40  
41 # Remainder of floating-point division.
42 3 *mod = \&fmod;
43  
44 # Minimum value.
45 sub min ($$)
46 {
47 22 ▶ my ($a, $b) = @_;
Subroutine min: called CC=2 SCAR=6.9
Pod documented
48  
49 22 ▶ $a < $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 17 5 22
50 }
51  
52 # Maximum value.
53 sub max ($$)
54 {
55 3 ▶ my ($a, $b) = @_;
Subroutine max: called CC=2 SCAR=6.9
Pod documented
56  
57 3 ▶ $a > $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 1 2 3
58 }
59  
60 # Transfer sign.
61 sub sign ($$)
62 {
63 34 ▶ my ($a, $b) = @_;
Subroutine sign: called CC=2 SCAR=6.9
Pod documented
64  
65 34 ▶ ($a < 0) == ($b < 0) ? $a : -$a;
Branch: 1 branch
BranchTrueFalseTotal
$a < 0 == $b < 0 ? : 21 13 34
66 }
67  
68 # Length of the hypotenuse of a right triangle.
69 sub hypot ($$)
70 {
71 15 ▶ my $a = abs (shift);
Subroutine hypot: called CC=3 SCAR=11.0
Pod documented
72 15 my $b = abs (shift);
73  
74 # Work variables.
75 15 my ($s, $t);
76  
77 15 ▶ if ($a >= $b)
Branch: 1 branch
BranchTrueFalseTotal
if ($a >= $b) { } 12 3 15
78 {
79 12 $s = $a;
80 # Avoid division by zero.
81 12 ▶ $t = ($a == $b ? 1.0 : $b / $a);
Branch: 1 branch
BranchTrueFalseTotal
$a == $b ? : 0 12 12
82 }
83 else
84 {
85 3 $s = $b;
86 3 $t = $a / $b;
87 }
88  
89 15 $s * sqrt (1.0 + $t * $t);
90 }
91  
92 # Complex division.
93 sub cdiv ($$$$)
94 {
95 1 ▶ my ($a_re, $a_im, $b_re, $b_im) = @_;
Subroutine cdiv: called CC=2 SCAR=7.6
Pod documented
96  
97 # Work variables.
98 1 my ($r, $d, @z);
99  
100 1 ▶ if (abs ($b_re) > abs ($b_im))
Branch: 1 branch
BranchTrueFalseTotal
if (abs $b_re > abs $b_im) { } 0 1 1
101 {
102 0 $r = $b_im / $b_re;
103 0 $d = $b_re + $r * $b_im;
104 0 @z = (($a_re + $r * $a_im) / $d,
105 ($a_im - $r * $a_re) / $d);
106 }
107 else
108 {
109 1 $r = $b_re / $b_im;
110 1 $d = $b_im + $r * $b_re;
111 1 @z = (($r * $a_re + $a_im) / $d,
112 ($r * $a_im - $a_re) / $d);
113 }
114  
115 1 @z;
116 }
117  
118 3 1;
119  
120 __END__
« blib/lib/Math/MatrixDecomposition/Util.pm ↑ Summary