blib/lib/Math/MatrixDecomposition/Util.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 85 CC 8 SCAR 17.7
CC
8
Cov
0%
hypot
12.0
INIT
6.0
min
6.0
CRAP 5.9
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 use strict;
13 use warnings;
14 use Exporter qw(import);
15 use POSIX qw(fmod);
16  
17 BEGIN
18 {
19 our $VERSION = '1.05';
20 our @EXPORT_OK = qw(eps mod min max sign hypot cdiv);
21 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
22 }
23  
24 # Machine precision.
25 my $epsilon = 1.0;
26  
27 *eps = sub () { $epsilon; };
28  
29 INIT
30 {
31 my $tem;
32  
33 while (1)
34 {
35 $tem = 1.0 + $epsilon / 2.0;
36 last if $tem == 1.0;
37 $epsilon /= 2.0;
38 }
39 }
40  
41 # Remainder of floating-point division.
42 *mod = \&fmod;
43  
44 # Minimum value.
45 sub min ($$)
46 {
47 my ($a, $b) = @_;
48  
49 $a < $b ? $a : $b;
50 }
51  
52 # Maximum value.
53 sub max ($$)
54 {
55 my ($a, $b) = @_;
56  
57 $a > $b ? $a : $b;
58 }
59  
60 # Transfer sign.
61 sub sign ($$)
62 {
63 my ($a, $b) = @_;
64  
65 ($a < 0) == ($b < 0) ? $a : -$a;
66 }
67  
68 # Length of the hypotenuse of a right triangle.
69 sub hypot ($$)
70 {
71 my $a = abs (shift);
72 my $b = abs (shift);
73  
74 # Work variables.
75 my ($s, $t);
76  
77 if ($a >= $b)
78 {
79 $s = $a;
80 # Avoid division by zero.
81 $t = ($a == $b ? 1.0 : $b / $a);
82 }
83 else
84 {
85 $s = $b;
86 $t = $a / $b;
87 }
88  
89 $s * sqrt (1.0 + $t * $t);
90 }
91  
92 # Complex division.
93 sub cdiv ($$$$)
94 {
95 my ($a_re, $a_im, $b_re, $b_im) = @_;
96  
97 # Work variables.
98 my ($r, $d, @z);
99  
100 if (abs ($b_re) > abs ($b_im))
101 {
102 $r = $b_im / $b_re;
103 $d = $b_re + $r * $b_im;
104 @z = (($a_re + $r * $a_im) / $d,
105 ($a_im - $r * $a_re) / $d);
106 }
107 else
108 {
109 $r = $b_re / $b_im;
110 $d = $b_im + $r * $b_re;
111 @z = (($r * $a_re + $a_im) / $d,
112 ($r * $a_im - $a_re) / $d);
113 }
114  
115 @z;
116 }
117  
118 1;
119  
120 __END__