lib/Math/MatrixDecomposition/Util.pm

filter: Statementstmt 94.2% 49 / 52 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 94.1% 80 / 85 CC 8 SCAR 6.0
CC
8
Cov
92%
hypot
3.0
cdiv
2.1
INIT
2.0
CRAP 1.8
« lib/Math/MatrixDecomposition/LU.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 3 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 our $VERSION = '1.06';
20 3 our @EXPORT_OK = qw(eps isnan 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 56 ▶*eps = sub () { $epsilon; };
Subroutine __ANON__: called CC=1 SCAR=0.0
28  
29 INIT
30 3 ▶{
Subroutine INIT: called CC=2 SCAR=6.9
31 3 my $tem;
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 # Not-a-number.
42 sub isnan ($)
43 1 ▶{
Subroutine isnan: called CC=1 SCAR=0.0
Pod documented
44 1 my $x = shift;
45  
46 1 $x != $x;
47 }
48  
49 # Remainder of floating-point division.
50 3 *mod = \&fmod;
51  
52 # Minimum value.
53 sub min ($$)
54 46 ▶{
Subroutine min: called CC=2 SCAR=6.9
Pod documented
55 46 my ($a, $b) = @_;
56  
57 46 ▶ $a < $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 34 12 46
58 }
59  
60 # Maximum value.
61 sub max ($$)
62 7 ▶{
Subroutine max: called CC=2 SCAR=6.9
Pod documented
63 7 my ($a, $b) = @_;
64  
65 7 ▶ $a > $b ? $a : $b;
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 4 3 7
66 }
67  
68 # Transfer sign.
69 sub sign ($$)
70 48 ▶{
Subroutine sign: called CC=2 SCAR=6.9
Pod documented
71 48 my ($a, $b) = @_;
72  
73 48 ▶ ($a < 0) == ($b < 0) ? $a : -$a;
Branch: 1 branch
BranchTrueFalseTotal
$a < 0 == $b < 0 ? : 28 20 48
74 }
75  
76 # Length of the hypotenuse of a right triangle.
77 sub hypot ($$)
78 15 ▶{
Subroutine hypot: called CC=3 SCAR=11.0
Pod documented
79 15 my $a = abs (shift);
80 15 my $b = abs (shift);
81  
82 # Work variables.
83 15 my ($s, $t);
84  
85 15 ▶ if ($a >= $b)
Branch: 1 branch
BranchTrueFalseTotal
if ($a >= $b) { } 12 3 15
86 {
87 12 $s = $a;
88 # Avoid division by zero.
89 12 ▶ $t = ($a == $b ? 1.0 : $b / $a);
Branch: 1 branch
BranchTrueFalseTotal
$a == $b ? : 0 12 12
90 }
91 else
92 {
93 3 $s = $b;
94 3 $t = $a / $b;
95 }
96  
97 15 $s * sqrt (1.0 + $t * $t);
98 }
99  
100 # Complex division.
101 sub cdiv ($$$$)
102 1 ▶{
Subroutine cdiv: called CC=2 SCAR=7.6
Pod documented
103 1 my ($a_re, $a_im, $b_re, $b_im) = @_;
104  
105 # Work variables.
106 1 my ($r, $d, @z);
107  
108 1 ▶ if (abs ($b_re) > abs ($b_im))
Branch: 1 branch
BranchTrueFalseTotal
if (abs $b_re > abs $b_im) { } 0 1 1
109 {
110 0 $r = $b_im / $b_re;
111 0 $d = $b_re + $r * $b_im;
112 0 @z = (($a_re + $r * $a_im) / $d,
113 ($a_im - $r * $a_re) / $d);
114 }
115 else
116 {
117 1 $r = $b_re / $b_im;
118 1 $d = $b_im + $r * $b_re;
119 1 @z = (($r * $a_re + $a_im) / $d,
120 ($r * $a_im - $a_re) / $d);
121 }
122  
123 1 @z;
124 }
125  
126 3 1;
127  
128 __END__
« lib/Math/MatrixDecomposition/LU.pm ↑ Summary