line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
3
|
|
83860
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
86
|
|
13
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
90
|
|
14
|
3
|
|
|
3
|
|
14
|
use Exporter qw(import); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
71
|
|
15
|
3
|
|
|
3
|
|
546
|
use POSIX qw(fmod); |
|
3
|
|
|
|
|
6528
|
|
|
3
|
|
|
|
|
21
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
BEGIN |
18
|
|
|
|
|
|
|
{ |
19
|
3
|
|
|
3
|
|
1702
|
our $VERSION = '1.06'; |
20
|
3
|
|
|
|
|
8
|
our @EXPORT_OK = qw(eps isnan mod min max sign hypot cdiv); |
21
|
3
|
|
|
|
|
1319
|
our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Machine precision. |
25
|
|
|
|
|
|
|
my $epsilon = 1.0; |
26
|
|
|
|
|
|
|
|
27
|
56
|
|
|
56
|
|
206
|
*eps = sub () { $epsilon; }; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
INIT |
30
|
|
|
|
|
|
|
{ |
31
|
3
|
|
|
3
|
|
222
|
my $tem; |
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
|
|
7
|
while (1) |
34
|
|
|
|
|
|
|
{ |
35
|
159
|
|
|
|
|
142
|
$tem = 1.0 + $epsilon / 2.0; |
36
|
159
|
100
|
|
|
|
194
|
last if $tem == 1.0; |
37
|
156
|
|
|
|
|
130
|
$epsilon /= 2.0; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Not-a-number. |
42
|
|
|
|
|
|
|
sub isnan ($) |
43
|
|
|
|
|
|
|
{ |
44
|
1
|
|
|
1
|
1
|
3
|
my $x = shift; |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
5
|
$x != $x; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Remainder of floating-point division. |
50
|
|
|
|
|
|
|
*mod = \&fmod; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Minimum value. |
53
|
|
|
|
|
|
|
sub min ($$) |
54
|
|
|
|
|
|
|
{ |
55
|
46
|
|
|
46
|
1
|
59
|
my ($a, $b) = @_; |
56
|
|
|
|
|
|
|
|
57
|
46
|
100
|
|
|
|
95
|
$a < $b ? $a : $b; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Maximum value. |
61
|
|
|
|
|
|
|
sub max ($$) |
62
|
|
|
|
|
|
|
{ |
63
|
7
|
|
|
7
|
1
|
14
|
my ($a, $b) = @_; |
64
|
|
|
|
|
|
|
|
65
|
7
|
100
|
|
|
|
36
|
$a > $b ? $a : $b; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Transfer sign. |
69
|
|
|
|
|
|
|
sub sign ($$) |
70
|
|
|
|
|
|
|
{ |
71
|
48
|
|
|
48
|
1
|
72
|
my ($a, $b) = @_; |
72
|
|
|
|
|
|
|
|
73
|
48
|
100
|
|
|
|
148
|
($a < 0) == ($b < 0) ? $a : -$a; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Length of the hypotenuse of a right triangle. |
77
|
|
|
|
|
|
|
sub hypot ($$) |
78
|
|
|
|
|
|
|
{ |
79
|
15
|
|
|
15
|
1
|
20
|
my $a = abs (shift); |
80
|
15
|
|
|
|
|
17
|
my $b = abs (shift); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Work variables. |
83
|
15
|
|
|
|
|
15
|
my ($s, $t); |
84
|
|
|
|
|
|
|
|
85
|
15
|
100
|
|
|
|
23
|
if ($a >= $b) |
86
|
|
|
|
|
|
|
{ |
87
|
12
|
|
|
|
|
26
|
$s = $a; |
88
|
|
|
|
|
|
|
# Avoid division by zero. |
89
|
12
|
50
|
|
|
|
19
|
$t = ($a == $b ? 1.0 : $b / $a); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
else |
92
|
|
|
|
|
|
|
{ |
93
|
3
|
|
|
|
|
4
|
$s = $b; |
94
|
3
|
|
|
|
|
6
|
$t = $a / $b; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
15
|
|
|
|
|
30
|
$s * sqrt (1.0 + $t * $t); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
# Complex division. |
101
|
|
|
|
|
|
|
sub cdiv ($$$$) |
102
|
|
|
|
|
|
|
{ |
103
|
1
|
|
|
1
|
1
|
3
|
my ($a_re, $a_im, $b_re, $b_im) = @_; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Work variables. |
106
|
1
|
|
|
|
|
2
|
my ($r, $d, @z); |
107
|
|
|
|
|
|
|
|
108
|
1
|
50
|
|
|
|
13
|
if (abs ($b_re) > abs ($b_im)) |
109
|
|
|
|
|
|
|
{ |
110
|
0
|
|
|
|
|
0
|
$r = $b_im / $b_re; |
111
|
0
|
|
|
|
|
0
|
$d = $b_re + $r * $b_im; |
112
|
0
|
|
|
|
|
0
|
@z = (($a_re + $r * $a_im) / $d, |
113
|
|
|
|
|
|
|
($a_im - $r * $a_re) / $d); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
else |
116
|
|
|
|
|
|
|
{ |
117
|
1
|
|
|
|
|
3
|
$r = $b_re / $b_im; |
118
|
1
|
|
|
|
|
4
|
$d = $b_im + $r * $b_re; |
119
|
1
|
|
|
|
|
5
|
@z = (($r * $a_re + $a_im) / $d, |
120
|
|
|
|
|
|
|
($r * $a_im - $a_re) / $d); |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
4
|
@z; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__END__ |