lib/Math/MatrixDecomposition/Eigen.pm

filter: Statementstmt 70.8% 443 / 625 Branchbran 42.3% 94 / 222 Conditioncond 32.9% 27 / 82 MC/DCmcdc 8.5% 4 / 47 Subroutinesub 88.2% 15 / 17 Podpod 100% 7 / 7 totaltotal 59.0% 590 / 1000 CC 240 SCAR 73.0
CC
240
Cov
61%
decompose
1808.8
sort
613.6
_cmp_vec
20.0
CRAP 1476.3
« blib/lib/Math/MatrixDecomposition/Eigen.pm ↑ Summary blib/lib/Math/MatrixDecomposition/LU.pm »
1 ## Math/MatrixDecomposition/Eigen.pm --- eigenvalues and eigenvectors.
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 # Commentary:
9 #
10 # Code derived from EiSPACK procedures and
11 # Jama's 'EigenvalueDecomposition' class.
12  
13 ## Code:
14  
15 package Math::MatrixDecomposition::Eigen;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Math::Complex qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Math::BLAS;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Math::MatrixDecomposition::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 BEGIN
28 {
29 1 ▶ our $VERSION = '1.04';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 our @EXPORT_OK = qw(eig);
31 }
32  
33 # Calculate eigenvalues and eigenvectors (convenience function).
34 sub eig
35 {
36 0 ▶ __PACKAGE__->new (@_);
Subroutine eig: not called CC=1 SCAR=6.9
Pod documented
37 }
38  
39 # Standard constructor.
40 sub new
41 {
42 1 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.8
Pod documented
43 1 my $self =
44 {
45 # Eigenvalues (a vector).
46 value => undef,
47  
48 # Eigenvectors (an array of vectors).
49 vector => undef,
50 };
51  
52 1 ▶ bless ($self, ref ($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
53  
54 # Process arguments.
55 1 ▶ $self->decompose (@_)
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 0 0 1 1
56 if @_ > 0;
57  
58 # Return object.
59 1 $self;
60 }
61  
62 # Calculate eigenvalues and eigenvectors.
63 sub decompose
64 {
65 2 ▶ my $self = shift;
Subroutine decompose: called CC=197 SCAR=75.0
Pod documented
66  
67 # Check arguments.
68 2 my $a = shift;
69  
70 2 ▶ my $m = @_ > 0 && looks_like_number ($_[0]) ? shift : sqrt (@$a);
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 && &looks_like_number($_[0]) ? : 0 2 2
Condition33%1/3
@_ > 0 and &looks_like_number($_[0])
!ll&&!rl&&r
200
MC/DC0%0/2
@_ > 0 and &looks_like_number($_[0])
@_ > 0 &looks_like_number($_[0])
Truth table33%1/3
A and B
A= @_ > 0B= &looks_like_number($_[0])
ABresult
0X0
100
111
71 2 ▶ my $n = @_ > 0 && looks_like_number ($_[0]) ? shift : $m ? @$a / $m : 0;
Branch: 2 branches
BranchTrueFalseTotal
@_ > 0 && &looks_like_number($_[0]) ? : 0 2 2
$m ? : 2 0 2
Condition33%1/3
@_ > 0 and &looks_like_number($_[0])
!ll&&!rl&&r
200
MC/DC0%0/2
@_ > 0 and &looks_like_number($_[0])
@_ > 0 &looks_like_number($_[0])
Truth table33%1/3
A and B
A= @_ > 0B= &looks_like_number($_[0])
ABresult
0X0
100
111
72  
73 2 ▶ croak ('Invalid argument')
Branch: 1 branch
BranchTrueFalseTotal
if @$a != $m * $n or mod($m, 1) != 0 or $m < 1 or mod($n, 1) != 0 or $n < 1 0 2 2
Condition33%4/12
@$a != $m * $n or mod($m, 1) != 0 or $m < 1 or mod($n, 1) != 0 or $n < 1
l!l&&r!l&&!r
002
@$a != $m * $n or mod($m, 1) != 0 or $m < 1 or mod($n, 1) != 0
l!l&&r!l&&!r
002
@$a != $m * $n or mod($m, 1) != 0 or $m < 1
l!l&&r!l&&!r
002
@$a != $m * $n or mod($m, 1) != 0
l!l&&r!l&&!r
002
MC/DC0%0/5
@$a != $m * $n or mod($m, 1) != 0 or $m < 1 or mod($n, 1) != 0 or $n < 1
@$a != $m * $n mod($m, 1) != 0 $m < 1 mod($n, 1) != 0 $n < 1
Truth table16%1/6
A or B or C or D or E
A= @$a != $m * $nB= mod($m, 1) != 0C= $m < 1D= mod($n, 1) != 0E= $n < 1
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
74 if (@$a != ($m * $n)
75 || mod ($m, 1) != 0 || $m < 1
76 || mod ($n, 1) != 0 || $n < 1);
77  
78 2 ▶ croak ('Matrix has to be square')
Branch: 1 branch
BranchTrueFalseTotal
if $m != $n 0 2 2
79 if $m != $n;
80  
81 # Get properties.
82 2 my %prop = @_;
83  
84 2 ▶ $prop{balance} //= 1;
Condition50%1/2
$prop{"balance"} //= 1
l!l
02
MC/DC0%0/1
$prop{"balance"} //= 1
$prop{"balance"}
Truth table50%1/2
A
A= $prop{"balance"}
Aresult
11
00
85 2 ▶ $prop{normalize} //= 1;
Condition50%1/2
$prop{"normalize"} //= 1
l!l
02
MC/DC0%0/1
$prop{"normalize"} //= 1
$prop{"normalize"}
Truth table50%1/2
A
A= $prop{"normalize"}
Aresult
11
00
86 2 ▶ $prop{positive} //= 1;
Condition50%1/2
$prop{"positive"} //= 1
l!l
02
MC/DC0%0/1
$prop{"positive"} //= 1
$prop{"positive"}
Truth table50%1/2
A
A= $prop{"positive"}
Aresult
11
00
87  
88 # Index of last row/column.
89 2 my $end = $n - 1;
90  
91 # Eigenvalues (a vector).
92 2 ▶ $$self{value} //= [];
Condition100%2/2
$self->{"value"} //= []
l!l
11
MC/DC100%1/1
$self->{"value"} //= []
$self->{"value"}
Truth table100%2/2
A
A= $self->{"value"}
Aresult
11
00
93  
94 # Vector $d contains the real part of the eigenvalues and vector $e
95 # contains the imaginary part of the eigenvalues.
96 2 my $d = $$self{value};
97 2 my $e = [];
98  
99 2 ▶ splice (@$d, $n)
Branch: 1 branch
BranchTrueFalseTotal
if @$d > $n 0 2 2
100 if @$d > $n;
101  
102 # Eigenvectors (an array of vectors).
103 2 ▶ $$self{vector} //= [];
Condition100%2/2
$self->{"vector"} //= []
l!l
11
MC/DC100%1/1
$self->{"vector"} //= []
$self->{"vector"}
Truth table100%2/2
A
A= $self->{"vector"}
Aresult
11
00
104  
105 # Matrix $Z contains the eigenvectors.
106 2 my $Z = $$self{vector};
107  
108 2 ▶ splice (@$Z, $n)
Branch: 1 branch
BranchTrueFalseTotal
if @$Z > $n 0 2 2
109 if @$Z > $n;
110  
111 2 for my $v (@$Z)
112 {
113 3 ▶ splice (@$v, $n)
Branch: 1 branch
BranchTrueFalseTotal
if @$v > $n 0 3 3
114 if @$v > $n;
115 }
116  
117 # True if matrix is symmetric.
118 2 my $sym = 1;
119  
120 SYM:
121  
122 2 for my $i (0 .. $end)
123 {
124 4 for my $j ($i + 1 .. $end)
125 {
126 4 ▶ if ($$a[$i * $n + $j] != $$a[$j * $n + $i])
Branch: 1 branch
BranchTrueFalseTotal
if ($$a[$i * $n + $j] != $$a[$j * $n + $i]) 1 3 4
127 {
128 1 $sym = 0;
129 1 last SYM;
130 }
131 }
132 }
133  
134 2 ▶ if ($sym)
Branch: 1 branch
BranchTrueFalseTotal
if ($sym) { } 1 1 2
135 {
136 # Copy matrix elements.
137 1 for my $j (0 .. $end)
138 {
139 3 ▶ $$Z[$j] //= [];
Condition50%1/2
$Z->[$j] //= []
l!l
03
MC/DC0%0/1
$Z->[$j] //= []
$Z->[$j]
Truth table50%1/2
A
A= $Z->[$j]
Aresult
11
00
140  
141 3 for my $i (0 .. $end)
142 {
143 9 $$Z[$j][$i] = $$a[$i * $n + $j];
144 }
145 }
146  
147 # Reduce a real symmetric matrix to a symmetric tridiagonal matrix
148 # using and accumulating orthogonal similarity transformations.
149 #
150 # See EiSPACK procedure 'tred2'.
151 1 if (1)
152 {
153 1 my ($i, $j, $k, $l,
154 $f, $g, $h, $hh, $scale);
155  
156 1 for $i (0 .. $end)
157 {
158 3 $$d[$i] = $$Z[$i][$end];
159 }
160  
161 1 for $i (reverse (1 .. $end))
162 {
163 2 $l = $i - 1;
164 2 $h = 0;
165  
166 # Scale row.
167 2 $scale = 0;
168  
169 2 for $k (0 .. $l)
170 {
171 3 $scale += abs ($$d[$k]);
172 }
173  
174 2 ▶ if ($scale == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 0) { } 0 2 2
175 {
176 0 $$e[$i] = $$d[$l];
177  
178 0 for $j (0 .. $l)
179 {
180 0 $$d[$j] = $$Z[$j][$l];
181  
182 0 $$Z[$j][$i] = 0;
183 0 $$Z[$i][$j] = 0;
184 }
185 }
186 else
187 {
188 2 for $k (0 .. $l)
189 {
190 3 $$d[$k] /= $scale;
191 3 $h += $$d[$k] ** 2;
192 }
193  
194 2 $f = $$d[$l];
195 2 $g = - sign (sqrt ($h), $f);
196 2 $h -= $f * $g;
197  
198 2 $$d[$l] = $f - $g;
199 2 $$e[$i] = $scale * $g;
200  
201 # Form a*u.
202 2 for $j (0 .. $l)
203 {
204 3 $$e[$j] = 0;
205 }
206  
207 2 for $j (0 .. $l)
208 {
209 3 $f = $$d[$j];
210 3 $$Z[$i][$j] = $f;
211 3 $g = $$e[$j] + $$Z[$j][$j] * $f;
212  
213 3 for $k ($j + 1 .. $l)
214 {
215 1 $g += $$Z[$j][$k] * $$d[$k];
216 1 $$e[$k] += $$Z[$j][$k] * $f;
217 }
218  
219 3 $$e[$j] = $g;
220 }
221  
222 # Form p.
223 2 $f = 0;
224  
225 2 for $j (0 .. $l)
226 {
227 3 $$e[$j] /= $h;
228 3 $f += $$e[$j] * $$d[$j];
229 }
230  
231 # Form q.
232 2 $hh = $f / ($h + $h);
233  
234 2 for $j (0 .. $l)
235 {
236 3 $$e[$j] -= $hh * $$d[$j];
237 }
238  
239 # Form reduced a.
240 2 for $j (0 .. $l)
241 {
242 3 $f = $$d[$j];
243 3 $g = $$e[$j];
244  
245 3 for $k ($j .. $l)
246 {
247 4 $$Z[$j][$k] -= ($f * $$e[$k] + $g * $$d[$k]);
248 }
249  
250 3 $$d[$j] = $$Z[$j][$l];
251 3 $$Z[$j][$i] = 0;
252 }
253 }
254  
255 2 $$d[$i] = $h;
256 }
257  
258 # Accumulation of transformation matrices.
259 1 for $i (1 .. $end)
260 {
261 2 $l = $i - 1;
262  
263 2 $$Z[$l][$end] = $$Z[$l][$l];
264 2 $$Z[$l][$l] = 1;
265  
266 2 $h = $$d[$i];
267 2 ▶ if ($h != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($h != 0) 2 0 2
268 {
269 2 for $k (0 .. $l)
270 {
271 3 $$d[$k] = $$Z[$i][$k] / $h;
272 }
273  
274 2 for $j (0 .. $l)
275 {
276 3 $g = 0;
277  
278 3 for $k (0 .. $l)
279 {
280 5 $g += $$Z[$i][$k] * $$Z[$j][$k];
281 }
282  
283 3 for $k (0 .. $l)
284 {
285 5 $$Z[$j][$k] -= $g * $$d[$k];
286 }
287 }
288 }
289  
290 2 for $k (0 .. $l)
291 {
292 3 $$Z[$i][$k] = 0;
293 }
294 }
295  
296 1 for $j (0 .. $end)
297 {
298 3 $$d[$j] = $$Z[$j][$end];
299 3 $$Z[$j][$end] = 0;
300 }
301  
302 1 $$Z[$end][$end] = 1;
303 1 $$e[0] = 0;
304 }
305  
306 # Find the eigenvalues and eigenvectors of a symmetric tridiagonal
307 # matrix by the QL method.
308 #
309 # See EiSPACK procedure 'tql2'.
310 1 if (1)
311 {
312 1 my ($i, $j, $k, $l, $m,
313 $c, $c2, $c3, $dl1, $el1, $f, $g, $h, $p, $r, $s, $s2, $t, $t2);
314  
315 1 for $i (1 .. $end)
316 {
317 2 $$e[$i - 1] = $$e[$i];
318 }
319  
320 1 $$e[$end] = 0;
321  
322 1 $f = 0;
323 1 $t = 0;
324  
325 1 for $l (0 .. $end)
326 {
327 3 $t2 = abs ($$d[$l]) + abs ($$e[$l]);
328 3 ▶ $t = $t2 if $t2 > $t;
Branch: 1 branch
BranchTrueFalseTotal
if $t2 > $t 1 2 3
329  
330 3 ▶ for ($m = $l; $m < $n; ++$m)
Branch: 1 branch
BranchTrueFalseTotal
if ($m < $n) 6 0 6
331 {
332 6 ▶ last if abs ($$e[$m]) <= eps * $t;
Branch: 1 branch
BranchTrueFalseTotal
if abs $e->[$m] <= &eps() * $t 3 3 6
333 }
334  
335 3 ▶ if ($m > $l)
Branch: 1 branch
BranchTrueFalseTotal
if ($m > $l) 2 1 3
336 {
337 2 while (1)
338 {
339 5 $g = $$d[$l];
340 5 $p = ($$d[$l + 1] - $g) / (2 * $$e[$l]);
341 5 $r = sign (hypot ($p, 1), $p);
342  
343 5 $$d[$l] = $$e[$l] / ($p + $r);
344 5 $$d[$l + 1] = $$e[$l] * ($p + $r);
345 5 $dl1 = $$d[$l + 1];
346 5 $h = $g - $$d[$l];
347  
348 5 for $i ($l + 2 .. $end)
349 {
350 4 $$d[$i] -= $h;
351 }
352  
353 5 $f += $h;
354  
355 5 $p = $$d[$m];
356 5 $c = 1;
357 5 $c2 = $c;
358 5 $el1 = $$e[$l + 1];
359 5 $s = 0;
360  
361 5 for $i (reverse ($l .. $m - 1))
362 {
363 9 $c3 = $c2;
364 9 $c2 = $c;
365 9 $s2 = $s;
366 9 $g = $c * $$e[$i];
367 9 $h = $c * $p;
368 9 $r = hypot ($p, $$e[$i]);
369 9 $$e[$i + 1] = $s * $r;
370 9 $s = $$e[$i] / $r;
371 9 $c = $p / $r;
372 9 $p = $c * $$d[$i] - $s * $g;
373 9 $$d[$i + 1] = $h + $s * ($c * $g + $s * $$d[$i]);
374  
375 9 for $k (0 .. $end)
376 {
377 27 $h = $$Z[$i + 1][$k];
378 27 $$Z[$i + 1][$k] = $s * $$Z[$i][$k] + $c * $h;
379 27 $$Z[$i][$k] = $c * $$Z[$i][$k] - $s * $h;
380 }
381 }
382  
383 5 $p = 0 - $s * $s2 * $c3 * $el1 * $$e[$l] / $dl1;
384  
385 5 $$e[$l] = $s * $p;
386 5 $$d[$l] = $c * $p;
387  
388 # Check convergence.
389 5 ▶ last if abs ($$e[$l]) <= eps * $t;
Branch: 1 branch
BranchTrueFalseTotal
if abs $e->[$l] <= &eps() * $t 2 3 5
390 }
391 }
392  
393 3 $$d[$l] = $$d[$l] + $f;
394 3 $$e[$l] = 0;
395 }
396 }
397 }
398 else
399 {
400 # Hessenberg matrix (an array of row vectors).
401 1 my @H = map ([], 0 .. $end);
402  
403 1 for my $i (0 .. $end)
404 {
405 3 for my $j (0 .. $end)
406 {
407 9 $H[$i][$j] = $$a[$i * $n + $j];
408 }
409 }
410  
411 # Row and column indices of the beginning and end
412 # of the principal sub-matrix.
413 1 my $lo = 0;
414 1 my $hi = $end;
415  
416 # Permutation vector.
417 1 my @perm = ();
418  
419 # Scaling vector.
420 1 my @scale = ();
421  
422 # Balance a real matrix and isolate eigenvalues whenever possible.
423 #
424 # See EiSPACK procedure 'balanc' and LAPACK procedure 'dgebal'.
425 1 ▶ if ($prop{balance})
Branch: 1 branch
BranchTrueFalseTotal
if ($prop{"balance"}) 1 0 1
426 {
427 1 @perm = (0 .. $end);
428 1 @scale = map (1, 0 .. $end);
429  
430 # Work variables.
431 1 my ($i, $j, $k, $l,
432 $b, $b2, $c, $f, $g, $r, $s, $no_conv);
433  
434 # Scale factors are powers of two.
435 1 $b = 2;
436 1 $b2 = $b ** 2;
437  
438 1 $k = 0;
439 1 $l = $end;
440  
441 1 ▶ if ($l > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($l > 0) 1 0 1
442 {
443 # Search for rows isolating an eigenvalue
444 # and push them down.
445 L:
446 {
447 1 for $j (reverse (0 .. $l))
448 {
449 3 $r = 0;
450  
451 3 for $i (0 .. $l)
452 {
453 9 ▶ $r = 1 if $i != $j && $H[$j][$i] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i != $j and $H[$j][$i] != 0 6 3 9
Condition66%2/3
$i != $j and $H[$j][$i] != 0
!ll&&!rl&&r
306
MC/DC50%1/2
$i != $j and $H[$j][$i] != 0
$i != $j $H[$j][$i] != 0
Truth table66%2/3
A and B
A= $i != $jB= $H[$j][$i] != 0
ABresult
0X0
100
111
454 }
455  
456 3 ▶ next if $r != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r != 0 3 0 3
457  
458 0 ▶ if ($j != $l)
Branch: 1 branch
BranchTrueFalseTotal
if ($j != $l) 0 0 0
459 {
460 # Exchange row and column.
461 0 @perm[$j, $l] = @perm[$l, $j];
462  
463 0 for $i (0 .. $l)
464 {
465 0 ($H[$i][$j], $H[$i][$l])
466 = ($H[$i][$l], $H[$i][$j]);
467 }
468  
469 0 for $i ($k .. $end)
470 {
471 0 ($H[$j][$i], $H[$l][$i])
472 = ($H[$l][$i], $H[$j][$i]);
473 }
474 }
475  
476 0 $l -= 1;
477 0 next L;
478 }
479 }
480  
481 # Search for columns isolating an eigenvalue
482 # and push them left.
483 K:
484 {
485 1 for $j ($k .. $l)
486 {
487 3 $c = 0;
488  
489 3 for $i ($k .. $l)
490 {
491 9 ▶ $c = 1 if $i != $j && $H[$i][$j] != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i != $j and $H[$i][$j] != 0 6 3 9
Condition66%2/3
$i != $j and $H[$i][$j] != 0
!ll&&!rl&&r
306
MC/DC50%1/2
$i != $j and $H[$i][$j] != 0
$i != $j $H[$i][$j] != 0
Truth table66%2/3
A and B
A= $i != $jB= $H[$i][$j] != 0
ABresult
0X0
100
111
492 }
493  
494 3 ▶ next if $c != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c != 0 3 0 3
495  
496 0 ▶ if ($j != $k)
Branch: 1 branch
BranchTrueFalseTotal
if ($j != $k) 0 0 0
497 {
498 # Exchange row and column.
499 0 @perm[$j, $k] = @perm[$k, $j];
500  
501 0 for $i (0 .. $l)
502 {
503 0 ($H[$i][$j], $H[$i][$k])
504 = ($H[$i][$k], $H[$i][$j]);
505 }
506  
507 0 for $i ($k .. $end)
508 {
509 0 ($H[$j][$i], $H[$k][$i])
510 = ($H[$k][$i], $H[$j][$i]);
511 }
512 }
513  
514 0 $k += 1;
515 0 next K;
516 }
517 }
518  
519 ## Now balance the sub-matrix in rows k to l.
520  
521 # Iterative loop for norm reduction.
522 1 while (1)
523 {
524 1 $no_conv = 0;
525  
526 1 for $i ($k .. $l)
527 {
528 3 $c = 0;
529 3 $r = 0;
530  
531 3 for $j ($k .. $l)
532 {
533 9 ▶ next if $j == $i;
Branch: 1 branch
BranchTrueFalseTotal
if $j == $i 3 6 9
534  
535 6 $c += abs ($H[$j][$i]);
536 6 $r += abs ($H[$i][$j]);
537 }
538  
539 # Guard against zero c or r due to underflow.
540 3 ▶ next if $c == 0 || $r == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c == 0 or $r == 0 0 3 3
Condition33%1/3
$c == 0 or $r == 0
l!l&&r!l&&!r
003
MC/DC0%0/2
$c == 0 or $r == 0
$c == 0 $r == 0
Truth table33%1/3
A or B
A= $c == 0B= $r == 0
ABresult
1X1
011
000
541  
542 3 $s = $c + $r;
543 3 $f = 1;
544  
545 3 $g = $r / $b;
546 3 ▶ while ($c < $g)
Branch: 1 branch
BranchTrueFalseTotal
if ($c < $g) 0 3 3
547 {
548 0 $f *= $b;
549 0 $c *= $b2;
550 }
551  
552 3 $g = $r * $b;
553 3 ▶ while ($c >= $g)
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= $g) 1 3 4
554 {
555 1 $f /= $b;
556 1 $c /= $b2;
557 }
558  
559 # Now balance.
560 3 ▶ if (($c + $r) / $f < 0.95 * $s)
Branch: 1 branch
BranchTrueFalseTotal
if (($c + $r) / $f < "0.95" * $s) 0 3 3
561 {
562 0 $g = 1 / $f;
563  
564 0 for $j ($k .. $end)
565 {
566 0 $H[$i][$j] *= $g;
567 }
568  
569 0 for $j (0 .. $l)
570 {
571 0 $H[$j][$i] *= $f;
572 }
573  
574 0 $scale[$i] *= $f;
575 0 $no_conv = 1;
576 }
577 }
578  
579 1 ▶ last unless $no_conv;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_conv 1 0 1
580 }
581 }
582  
583 1 $lo = $k;
584 1 $hi = $l;
585 }
586  
587 # Reduce matrix to upper Hessenberg form by orthogonal similarity
588 # transformations.
589 #
590 # See EiSPACK procedure 'orthes'.
591 1 my @ort = ();
592  
593 1 if (1)
594 {
595 1 my ($i, $j, $k, $m,
596 $f, $g, $h, $scale);
597  
598 1 for $m ($lo + 1 .. $hi - 1)
599 {
600 1 $scale = 0;
601  
602 1 for $i ($m .. $hi)
603 {
604 2 $scale += abs ($H[$i][$m - 1]);
605 }
606  
607 1 ▶ next if $scale == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale == 0 0 1 1
608  
609 1 $h = 0;
610  
611 1 for $i (reverse ($m .. $hi))
612 {
613 2 $ort[$i] = $H[$i][$m - 1] / $scale;
614 2 $h += $ort[$i] ** 2;
615 }
616  
617 1 $g = - sign (sqrt ($h), $ort[$m]);
618 1 $h -= $ort[$m] * $g;
619 1 $ort[$m] -= $g;
620  
621 1 for $j ($m .. $end)
622 {
623 2 $f = 0;
624  
625 2 for $i (reverse ($m .. $hi))
626 {
627 4 $f += $ort[$i] * $H[$i][$j];
628 }
629  
630 2 $f /= $h;
631  
632 2 for $i ($m .. $hi)
633 {
634 4 $H[$i][$j] -= $f * $ort[$i];
635 }
636 }
637  
638 1 for $i (0 .. $hi)
639 {
640 3 $f = 0;
641  
642 3 for $j (reverse ($m .. $hi))
643 {
644 6 $f += $ort[$j] * $H[$i][$j];
645 }
646  
647 3 $f /= $h;
648  
649 3 for $j ($m .. $hi)
650 {
651 6 $H[$i][$j] -= $f * $ort[$j];
652 }
653 }
654  
655 1 $ort[$m] *= $scale;
656 1 $H[$m][$m - 1] = $scale * $g;
657 }
658 }
659  
660 # Accumulate the orthogonal similarity transformations.
661 #
662 # See EiSPACK procedure 'ortran'.
663 1 if (1)
664 {
665 1 my ($i, $j, $k, $m,
666 $g);
667  
668 1 for $j (0 .. $end)
669 {
670 3 ▶ $$Z[$j] //= [];
Condition50%1/2
$Z->[$j] //= []
l!l
30
MC/DC0%0/1
$Z->[$j] //= []
$Z->[$j]
Truth table50%1/2
A
A= $Z->[$j]
Aresult
11
00
671  
672 3 for $i (0 .. $end)
673 {
674 9 ▶ $$Z[$j][$i] = ($i == $j ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$i == $j ? : 3 6 9
675 }
676 }
677  
678 1 for $m (reverse ($lo + 1 .. $hi - 1))
679 {
680 1 ▶ next if $H[$m][$m - 1] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $H[$m][$m - 1] == 0 0 1 1
681  
682 1 for $i ($m + 1 .. $hi)
683 {
684 1 $ort[$i] = $H[$i][$m - 1];
685 }
686  
687 1 for $j ($m .. $hi)
688 {
689 2 $g = 0;
690  
691 2 for $i ($m .. $hi)
692 {
693 4 $g += $ort[$i] * $$Z[$j][$i];
694 }
695  
696 2 $g = ($g / $ort[$m]) / $H[$m][$m - 1];
697  
698 2 for $i ($m .. $hi)
699 {
700 4 $$Z[$j][$i] += $g * $ort[$i];
701 }
702 }
703 }
704 }
705  
706 # Find the eigenvalues and eigenvectors of a real upper Hessenberg
707 # matrix by the QR method.
708 #
709 # See EiSPACK procedure 'hqr2'.
710 1 if (1)
711 {
712 1 my ($i, $j, $k, $l, $m,
713 $h, $g, $f, $p, $q, $r, $s, $t, $w, $x, $y, $z,
714 $norm, $iter, $not_last, $vr, $vi, $ra, $sa);
715  
716 1 $t = 0;
717  
718 # Store isolated roots.
719 1 for $i (0 .. $end)
720 {
721 3 ▶ if ($i < $lo || $i > $hi)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $lo or $i > $hi) 0 3 3
Condition33%1/3
$i < $lo or $i > $hi
l!l&&r!l&&!r
003
MC/DC0%0/2
$i < $lo or $i > $hi
$i < $lo $i > $hi
Truth table33%1/3
A or B
A= $i < $loB= $i > $hi
ABresult
1X1
011
000
722 {
723 0 $$d[$i] = $H[$i][$i];
724 0 $$e[$i] = 0;
725 }
726 }
727  
728 # Compute matrix norm.
729 1 $norm = 0;
730  
731 1 for $i (0 .. $end)
732 {
733 3 for $j ($i .. $end)
734 {
735 6 $norm += abs ($H[$i][$j]);
736 }
737 }
738  
739 # Search for next eigenvalue.
740 1 $iter = 0;
741  
742 1 ▶ for ($n = $end; $n >= $lo; )
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= $lo) 10 1 11
743 {
744 # Look for single small sub-diagonal element.
745 10 ▶ for ($l = $n; $l > $lo; --$l)
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $lo) 16 8 24
746 {
747 16 $s = abs ($H[$l - 1][$l - 1]) + abs ($H[$l][$l]);
748 16 ▶ $s = $norm
Branch: 1 branch
BranchTrueFalseTotal
if $s == 0 0 16 16
749 if $s == 0;
750  
751 16 ▶ last if abs ($H[$l][$l - 1]) < eps * $s;
Branch: 1 branch
BranchTrueFalseTotal
if abs $H[$l][$l - 1] < &eps() * $s 2 14 16
752 }
753  
754 10 $x = $H[$n][$n];
755  
756 10 ▶ if ($l == $n)
Branch: 2 branches
BranchTrueFalseTotal
if ($l == $n) { } 3 7 10
elsif ($l == $n - 1) { } 0 7 7
757 {
758 # One root found,
759 3 $H[$n][$n] = $x + $t;
760  
761 3 $$d[$n] = $H[$n][$n];
762 3 $$e[$n] = 0;
763  
764 3 $n -= 1;
765 3 $iter = 0;
766 }
767 elsif ($l == $n - 1)
768 {
769 # Two roots found.
770 0 $y = $H[$n - 1][$n - 1];
771 0 $w = $H[$n][$n - 1] * $H[$n - 1][$n];
772  
773 0 $p = ($y - $x) / 2;
774 0 $q = $p * $p + $w;
775 0 $z = sqrt (abs ($q));
776  
777 0 $H[$n][$n] = $x + $t;
778 0 $H[$n - 1][$n - 1] = $y + $t;
779 0 $x = $H[$n][$n];
780  
781 0 ▶ if ($q >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($q >= 0) { } 0 0 0
782 {
783 # Real pair.
784 0 $z = $p + sign ($z, $p);
785  
786 0 $$d[$n - 1] = $x + $z;
787 0 $$d[$n] = $$d[$n - 1];
788 0 ▶ $$d[$n] = $x - $w / $z
Branch: 1 branch
BranchTrueFalseTotal
if $z != 0 0 0 0
789 if $z != 0;
790  
791 0 $$e[$n - 1] = 0;
792 0 $$e[$n] = 0;
793  
794 0 $x = $H[$n][$n - 1];
795 0 $s = abs ($x) + abs ($z);
796 0 $p = $x / $s;
797 0 $q = $z / $s;
798 0 $r = sqrt ($p * $p + $q * $q);
799 0 $p = $p / $r;
800 0 $q = $q / $r;
801  
802 # Row modification.
803 0 for $j ($n - 1 .. $end)
804 {
805 0 $z = $H[$n - 1][$j];
806 0 $H[$n - 1][$j] = $q * $z + $p * $H[$n][$j];
807 0 $H[$n][$j] = $q * $H[$n][$j] - $p * $z;
808 }
809  
810 # Column modification.
811 0 for $i (0 .. $n)
812 {
813 0 $z = $H[$i][$n - 1];
814 0 $H[$i][$n - 1] = $q * $z + $p * $H[$i][$n];
815 0 $H[$i][$n] = $q * $H[$i][$n] - $p * $z;
816 }
817  
818 # Accumulate transformations.
819 0 for $i ($lo .. $hi)
820 {
821 0 $z = $$Z[$n - 1][$i];
822 0 $$Z[$n - 1][$i] = $q * $z + $p * $$Z[$n][$i];
823 0 $$Z[$n][$i] = $q * $$Z[$n][$i] - $p * $z;
824 }
825 }
826 else
827 {
828 # Complex pair.
829 0 $$d[$n - 1] = $x + $p;
830 0 $$d[$n] = $x + $p;
831 0 $$e[$n - 1] = $z;
832 0 $$e[$n] = 0 - $z;
833 }
834  
835 0 $n -= 2;
836 0 $iter = 0;
837 }
838 else
839 {
840 # Form shift.
841 7 $y = $H[$n - 1][$n - 1];
842 7 $w = $H[$n][$n - 1] * $H[$n - 1][$n];
843  
844 # Wilkinson's original ad hoc shift.
845 7 ▶ if ($iter == 10 || $iter == 20)
Branch: 1 branch
BranchTrueFalseTotal
if ($iter == 10 or $iter == 20) 0 7 7
Condition33%1/3
$iter == 10 or $iter == 20
l!l&&r!l&&!r
007
MC/DC0%0/2
$iter == 10 or $iter == 20
$iter == 10 $iter == 20
Truth table33%1/3
A or B
A= $iter == 10B= $iter == 20
ABresult
1X1
011
000
846 {
847 0 $t += $x;
848  
849 0 for $i ($lo .. $n)
850 {
851 0 $H[$i][$i] -= $x;
852 }
853  
854 0 $s = abs ($H[$n][$n - 1]) + abs ($H[$n - 1][$n - 2]);
855 0 $x = 0.75 * $s;
856 0 $y = $x;
857 0 $w = -0.4375 * $s * $s;
858 }
859  
860 # Matlab's new ad hoc shift.
861 7 ▶ if ($iter == 30)
Branch: 1 branch
BranchTrueFalseTotal
if ($iter == 30) 0 7 7
862 {
863 0 $s = ($y - $x) / 2;
864 0 $s = $s * $s + $w;
865 0 ▶ if ($s > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 0) 0 0 0
866 {
867 0 $s = sqrt ($s);
868 0 ▶ $s = - $s if $y < $x;
Branch: 1 branch
BranchTrueFalseTotal
if $y < $x 0 0 0
869 0 $s = $x - $w / (($y - $x) / 2 + $s);
870  
871 0 for $i ($lo .. $n)
872 {
873 0 $H[$i][$i] -= $s;
874 }
875  
876 0 $t += $s;
877 0 $x = 0.964;
878 0 $w = $y = $x;
879 }
880 }
881  
882 7 ++$iter;
883  
884 # Look for two consecutive small sub-diagonal elements.
885 7 ▶ for ($m = $n - 2; $m >= $l; --$m)
Branch: 1 branch
BranchTrueFalseTotal
if ($m >= $l) 7 0 7
886 {
887 7 $z = $H[$m][$m];
888 7 $r = $x - $z;
889 7 $s = $y - $z;
890 7 $p = ($r * $s - $w) / $H[$m + 1][$m] + $H[$m][$m + 1];
891 7 $q = $H[$m + 1][$m + 1] - $z - $r - $s;
892 7 $r = $H[$m + 2][$m + 1];
893 7 $s = abs ($p) + abs ($q) + abs ($r);
894 7 $p = $p / $s;
895 7 $q = $q / $s;
896 7 $r = $r / $s;
897  
898 7 ▶ last if $m == $l;
Branch: 1 branch
BranchTrueFalseTotal
if $m == $l 7 0 7
899 0 ▶ last if abs ($H[$m][$m - 1]) * (abs ($q) + abs ($r)) < eps * (abs ($p) * (abs ($H[$m - 1][$m - 1]) + abs ($z) + abs ($H[$m + 1][$m + 1])));
Branch: 1 branch
BranchTrueFalseTotal
if abs($H[$m][$m - 1]) * (abs($q) + abs($r)) < &eps() * (abs($p) * (abs($H[$m - 1][$m - 1]) + abs($z) + abs($H[$m + 1][$m + 1]))) 0 0 0
900 }
901  
902 7 for $i ($m + 2 .. $n)
903 {
904 7 $H[$i][$i - 2] = 0;
905 7 ▶ $H[$i][$i - 3] = 0
Branch: 1 branch
BranchTrueFalseTotal
if $i > $m + 2 0 7 7
906 if $i > $m + 2;
907 }
908  
909 # Double QR step.
910 7 for $k ($m .. $n - 1)
911 {
912 14 $not_last = ($k != $n - 1);
913  
914 14 ▶ if ($k != $m)
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $m) 7 7 14
915 {
916 7 $p = $H[$k][$k - 1];
917 7 $q = $H[$k + 1][$k - 1];
918 7 ▶ $r = $not_last ? $H[$k + 2][$k - 1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$not_last ? : 0 7 7
919 7 $x = abs ($p) + abs ($q) + abs ($r);
920  
921 7 ▶ next if $x == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x == 0 0 7 7
922  
923 7 $p = $p / $x;
924 7 $q = $q / $x;
925 7 $r = $r / $x;
926 }
927  
928 14 $s = sign (sqrt ($p * $p + $q * $q + $r * $r), $p);
929 14 ▶ if ($s != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($s != 0) 14 0 14
930 {
931 14 ▶ if ($k != $m)
Branch: 2 branches
BranchTrueFalseTotal
if ($k != $m) { } 7 7 14
elsif ($l != $m) { } 0 7 7
932 {
933 7 $H[$k][$k - 1] = 0 - $s * $x;
934 }
935 elsif ($l != $m)
936 {
937 0 $H[$k][$k - 1] = - $H[$k][$k - 1];
938 }
939  
940 14 $p = $p + $s;
941 14 $x = $p / $s;
942 14 $y = $q / $s;
943 14 $z = $r / $s;
944 14 $q = $q / $p;
945 14 $r = $r / $p;
946  
947 14 ▶ if ($not_last)
Branch: 1 branch
BranchTrueFalseTotal
if ($not_last) { } 7 7 14
948 {
949 # Row modification.
950 7 for $j ($k .. $end)
951 {
952 21 $p = $H[$k][$j] + $q * $H[$k + 1][$j] + $r * $H[$k + 2][$j];
953  
954 21 $H[$k][$j] -= $p * $x;
955 21 $H[$k + 1][$j] -= $p * $y;
956 21 $H[$k + 2][$j] -= $p * $z;
957 }
958  
959 # Column modification.
960 7 for $i (0 .. min ($n, $k + 3))
961 {
962 21 $p = $x * $H[$i][$k] + $y * $H[$i][$k + 1] + $z * $H[$i][$k + 2];
963  
964 21 $H[$i][$k] -= $p;
965 21 $H[$i][$k + 1] -= $p * $q;
966 21 $H[$i][$k + 2] -= $p * $r;
967 }
968  
969 # Accumulate transformations.
970 7 for $i ($lo .. $hi)
971 {
972 21 $p = $x * $$Z[$k][$i] + $y * $$Z[$k + 1][$i] + $z * $$Z[$k + 2][$i];
973  
974 21 $$Z[$k][$i] -= $p;
975 21 $$Z[$k + 1][$i] -= $p * $q;
976 21 $$Z[$k + 2][$i] -= $p * $r;
977 }
978 }
979 else
980 {
981 # Row modification.
982 7 for $j ($k .. $end)
983 {
984 14 $p = $H[$k][$j] + $q * $H[$k + 1][$j];
985  
986 14 $H[$k][$j] -= $p * $x;
987 14 $H[$k + 1][$j] -= $p * $y;
988 }
989  
990 # Column modification.
991 7 for $i (0 .. min ($n, $k + 3))
992 {
993 21 $p = $x * $H[$i][$k] + $y * $H[$i][$k + 1];
994  
995 21 $H[$i][$k] -= $p;
996 21 $H[$i][$k + 1] -= $p * $q;
997 }
998  
999 # Accumulate transformations.
1000 7 for $i (0 .. $end)
1001 {
1002 21 $p = $x * $$Z[$k][$i] + $y * $$Z[$k + 1][$i];
1003  
1004 21 $$Z[$k][$i] -= $p;
1005 21 $$Z[$k + 1][$i] -= $p * $q;
1006 }
1007 }
1008 }
1009 }
1010 }
1011 }
1012  
1013 # Backsubstitute to find vectors of upper triangular form.
1014 1 ▶ return if $norm == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $norm == 0 0 1 1
1015  
1016 1 for $n (reverse (0 .. $end))
1017 {
1018 3 $p = $$d[$n];
1019 3 $q = $$e[$n];
1020  
1021 3 ▶ if ($q == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($q == 0) { } 3 0 3
elsif ($q < 0) { } 0 0 0
1022 {
1023 # Real vector.
1024 3 $m = $n;
1025 3 $H[$n][$n] = 1;
1026  
1027 3 for $i (reverse (0 .. $n - 1))
1028 {
1029 3 $w = $H[$i][$i] - $p;
1030 3 $r = 0;
1031  
1032 3 for $j ($m .. $n)
1033 {
1034 4 $r += $H[$i][$j] * $H[$j][$n];
1035 }
1036  
1037 3 ▶ if ($$e[$i] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[$i] < 0) { } 0 3 3
1038 {
1039 0 $z = $w;
1040 0 $s = $r;
1041 }
1042 else
1043 {
1044 3 $m = $i;
1045  
1046 3 ▶ if ($$e[$i] == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[$i] == 0) { } 3 0 3
1047 {
1048 3 ▶ $H[$i][$n] = ($w != 0 ?
Branch: 1 branch
BranchTrueFalseTotal
$w != 0 ? : 3 0 3
1049 0 - $r / $w :
1050 0 - $r / (eps * $norm));
1051 }
1052 else
1053 {
1054 # Solve real equations.
1055 0 $x = $H[$i][$i + 1];
1056 0 $y = $H[$i + 1][$i];
1057 0 $q = ($$d[$i] - $p) ** 2 + $$e[$i] ** 2;
1058 0 $t = ($x * $s - $z * $r) / $q;
1059  
1060 0 $H[$i][$n] = $t;
1061 0 ▶ $H[$i + 1][$n] = (abs ($x) > abs ($z) ?
Branch: 1 branch
BranchTrueFalseTotal
abs $x > abs $z ? : 0 0 0
1062 (0 - $r - $w * $t) / $x :
1063 (0 - $s - $y * $t) / $z);
1064 }
1065  
1066 # Overflow control.
1067 3 $t = abs ($H[$i][$n]);
1068 3 ▶ if ((eps * $t) * $t > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (&eps() * $t * $t > 1) 0 3 3
1069 {
1070 0 for $j ($i .. $n)
1071 {
1072 0 $H[$j][$n] /= $t;
1073 }
1074 }
1075 }
1076 }
1077 }
1078 elsif ($q < 0)
1079 {
1080 # Complex vector.
1081 0 $m = $n - 1;
1082  
1083 # Last vector component chosen imaginary so that
1084 # eigenvector matrix is triangular.
1085 0 ▶ if (abs ($H[$n][$n - 1]) > abs ($H[$n - 1][$n]))
Branch: 1 branch
BranchTrueFalseTotal
if (abs $H[$n][$n - 1] > abs $H[$n - 1][$n]) { } 0 0 0
1086 {
1087 0 $H[$n - 1][$n - 1] = $q / $H[$n][$n - 1];
1088 0 $H[$n - 1][$n] = ($p - $H[$n][$n]) / $H[$n][$n - 1];
1089 }
1090 else
1091 {
1092 0 ($H[$n - 1][$n - 1], $H[$n - 1][$n])
1093 = cdiv (0, - $H[$n - 1][$n],
1094 $H[$n - 1][$n - 1] - $p, $q);
1095 }
1096  
1097 0 $H[$n][$n - 1] = 0;
1098 0 $H[$n][$n] = 1;
1099  
1100 0 for $i (reverse (0 .. $n - 2))
1101 {
1102 0 $w = $H[$i][$i] - $p;
1103  
1104 0 $ra = 0;
1105 0 $sa = 0;
1106  
1107 0 for $j ($m .. $n)
1108 {
1109 0 $ra += $H[$i][$j] * $H[$j][$n - 1];
1110 0 $sa += $H[$i][$j] * $H[$j][$n];
1111 }
1112  
1113 0 ▶ if ($$e[$i] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[$i] < 0) { } 0 0 0
1114 {
1115 0 $z = $w;
1116 0 $r = $ra;
1117 0 $s = $sa;
1118 }
1119 else
1120 {
1121 0 $m = $i;
1122  
1123 0 ▶ if ($$e[$i] == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($e->[$i] == 0) { } 0 0 0
1124 {
1125 0 ($H[$i][$n - 1], $H[$i][$n])
1126 = cdiv (- $ra, - $sa, $w, $q);
1127 }
1128 else
1129 {
1130 # Solve complex equations.
1131 0 $x = $H[$i][$i + 1];
1132 0 $y = $H[$i + 1][$i];
1133  
1134 0 $vr = ($$d[$i] - $p) ** 2 + $$e[$i] ** 2 - $q ** 2;
1135 0 $vi = ($$d[$i] - $p) * 2 * $q;
1136  
1137 0 ▶ if ($vr == 0 && $vi == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($vr == 0 and $vi == 0) 0 0 0
Condition0%0/3
$vr == 0 and $vi == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$vr == 0 and $vi == 0
$vr == 0 $vi == 0
Truth table0%0/3
A and B
A= $vr == 0B= $vi == 0
ABresult
0X0
100
111
1138 {
1139 0 $vr = eps * $norm * (abs ($w) + abs ($q) + abs ($x) + abs ($y) + abs ($z));
1140 }
1141  
1142 0 ($H[$i][$n - 1], $H[$i][$n])
1143 = cdiv ($x * $r - $z * $ra + $q * $sa,
1144 $x * $s - $z * $sa - $q * $ra,
1145 $vr,
1146 $vi);
1147  
1148 0 ▶ if (abs ($x) > (abs ($z) + abs ($q)))
Branch: 1 branch
BranchTrueFalseTotal
if (abs $x > abs($z) + abs($q)) { } 0 0 0
1149 {
1150 0 $H[$i + 1][$n - 1] = (0 - $ra - $w * $H[$i][$n - 1] + $q * $H[$i][$n]) / $x;
1151 0 $H[$i + 1][$n] = (0 - $sa - $w * $H[$i][$n] - $q * $H[$i][$n - 1]) / $x;
1152 }
1153 else
1154 {
1155 0 ($H[$i + 1][$n - 1], $H[$i + 1][$n])
1156 = cdiv (0 - $r - $y * $H[$i][$n - 1],
1157 0 - $s - $y * $H[$i][$n],
1158 $z,
1159 $q);
1160 }
1161 }
1162  
1163 # Overflow control.
1164 0 $t = max (abs ($H[$i][$n - 1]), abs ($H[$i][$n]));
1165 0 ▶ if ((eps * $t) * $t > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (&eps() * $t * $t > 1) 0 0 0
1166 {
1167 0 for $j ($i .. $n)
1168 {
1169 0 $H[$j][$n - 1] /= $t;
1170 0 $H[$j][$n] /= $t;
1171 }
1172 }
1173 }
1174 }
1175 }
1176 }
1177  
1178 # Vectors of isolated roots.
1179 1 for $i (0 .. $end)
1180 {
1181 3 ▶ if ($i < $lo || $i > $hi)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $lo or $i > $hi) 0 3 3
Condition33%1/3
$i < $lo or $i > $hi
l!l&&r!l&&!r
003
MC/DC0%0/2
$i < $lo or $i > $hi
$i < $lo $i > $hi
Truth table33%1/3
A or B
A= $i < $loB= $i > $hi
ABresult
1X1
011
000
1182 {
1183 0 for $j ($i .. $end)
1184 {
1185 0 $$Z[$j][$i] = $H[$i][$j];
1186 }
1187 }
1188 }
1189  
1190 # Multiply by transformation matrix to give
1191 # vectors of original full matrix.
1192 1 for $j (reverse ($lo .. $end))
1193 {
1194 3 $m = min ($j, $hi);
1195  
1196 3 for $i ($lo .. $hi)
1197 {
1198 9 $z = 0;
1199  
1200 9 for $k ($lo .. $m)
1201 {
1202 18 $z += $$Z[$k][$i] * $H[$k][$j];
1203 }
1204  
1205 9 $$Z[$j][$i] = $z;
1206 }
1207 }
1208 }
1209  
1210 # Form the eigenvectors of a real general matrix by back
1211 # transforming those of the corresponding balanced matrix
1212 # determined by 'balance'.
1213 #
1214 # See EiSPACK procedure 'balbak'.
1215 1 ▶ if ($prop{balance})
Branch: 1 branch
BranchTrueFalseTotal
if ($prop{"balance"}) 1 0 1
1216 {
1217 1 my ($i, $j, $k);
1218  
1219 # Undo permutations.
1220 1 for $i (reverse (0 .. $lo - 1))
1221 {
1222 0 $k = $perm[$i];
1223 0 ▶ if ($k != $i)
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $i) 0 0 0
1224 {
1225 0 for $j (0 .. $end)
1226 {
1227 0 ($$Z[$j][$i], $$Z[$j][$k])
1228 = ($$Z[$j][$k], $$Z[$j][$i]);
1229 }
1230 }
1231 }
1232  
1233 1 for $i ($hi + 1 .. $end)
1234 {
1235 0 $k = $perm[$i];
1236 0 ▶ if ($k != $i)
Branch: 1 branch
BranchTrueFalseTotal
if ($k != $i) 0 0 0
1237 {
1238 0 for $j (0 .. $end)
1239 {
1240 0 ($$Z[$j][$i], $$Z[$j][$k])
1241 = ($$Z[$j][$k], $$Z[$j][$i]);
1242 }
1243 }
1244 }
1245 }
1246 }
1247  
1248 # Create complex eigenvalues.
1249 2 for my $i (0 .. $end)
1250 {
1251 6 ▶ $$d[$i] = Math::Complex->make ($$d[$i], $$e[$i])
Branch: 1 branch
BranchTrueFalseTotal
if $e->[$i] != 0 0 6 6
1252 if $$e[$i] != 0;
1253 }
1254  
1255 # Normalize eigenvectors.
1256 $self->normalize
1257 2 ▶ if $prop{normalize};
Branch: 1 branch
BranchTrueFalseTotal
if $prop{"normalize"} 2 0 2
1258  
1259 # Make first non-zero vector element a positive number.
1260 2 ▶ if ($prop{positive})
Branch: 1 branch
BranchTrueFalseTotal
if ($prop{"positive"}) 2 0 2
1261 {
1262 2 my ($i, $j, $k);
1263  
1264 2 for $j (0 .. $end)
1265 {
1266 6 for $i (0 .. $end)
1267 {
1268 6 ▶ next if $$Z[$j][$i] == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $Z->[$j][$i] == 0 0 6 6
1269  
1270 6 ▶ if ($$Z[$j][$i] < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($Z->[$j][$i] < 0) 4 2 6
1271 {
1272 4 for $k ($i .. $end)
1273 {
1274 12 $$Z[$j][$k] = - $$Z[$j][$k];
1275 }
1276 }
1277  
1278 6 last;
1279 }
1280 }
1281 }
1282  
1283 # Return object.
1284 2 $self;
1285 }
1286  
1287 # Normalize eigenvectors.
1288 sub normalize
1289 {
1290 2 ▶ my $self = shift;
Subroutine normalize: called CC=3 SCAR=11.0
Pod documented
1291  
1292 # Work variables.
1293 2 my $len;
1294  
1295 2 for my $v (@{ $$self{vector} })
1296 {
1297 6 $len = blas_norm (@$v, $v, norm => BLAS_TWO_NORM);
1298 6 ▶ blas_rscale (@$v, $v, alpha => $len) if $len != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len != 0 6 0 6
1299 }
1300  
1301 # Return object.
1302 2 $self;
1303 }
1304  
1305 # Sort eigenvalues and corresponding eigenvectors.
1306 sub sort
1307 {
1308 2 ▶ my $self = shift;
Subroutine sort: called CC=35 SCAR=64.2
Pod documented
1309 2 ▶ my $order = shift // 'abs_desc';
Condition50%1/2
shift() // "abs_desc"
l!l
20
MC/DC0%0/1
shift() // "abs_desc"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1310  
1311 # Permutation vector.
1312 2 my @p = ();
1313  
1314 2 ▶ if ($order =~ m/\Avec_/)
Branch: 2 branches
BranchTrueFalseTotal
if ($order =~ /\Avec_/) { } 0 2 2
elsif (grep ref($_), @{$$self{'value'};}) { } 0 2 2
1315 {
1316 0 my $Z = $$self{vector};
1317  
1318 @p = ($order eq 'vec_desc' ?
1319 0 ▶ sort { _cmp_vec ($$Z[$b], $$Z[$a]) } 0 .. $#$Z :
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'vec_asc' ? : 0 0 0
1320 ($order eq 'vec_asc' ?
1321 0 ▶ sort { _cmp_vec ($$Z[$a], $$Z[$b]) } 0 .. $#$Z :
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'vec_desc' ? : 0 0 0
1322 croak ("Invalid argument")));
1323 }
1324 2 elsif (grep (ref ($_), @{ $$self{value} }))
1325 {
1326 # Consider complex eigenvalues.
1327 0 my (@d, @e, @m) = ();
1328  
1329 0 for (@{ $$self{value} })
1330 {
1331 0 ▶ if (ref ($_))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
1332 {
1333 0 push (@d, $_->Re);
1334 0 push (@e, $_->Im);
1335 0 push (@m, abs ($_));
1336 }
1337 else
1338 {
1339 0 push (@d, $_);
1340 0 push (@e, 0);
1341 0 push (@m, abs ($_));
1342 }
1343 }
1344  
1345 @p = ($order eq 'abs_desc' ?
1346 0 ▶ sort { abs ($d[$b]) <=> abs ($d[$a]) || $m[$b] <=> $m[$a] || $d[$b] <=> $d[$a] || $e[$b] <=> $e[$a] } 0 .. $#d :
Branch: 2 branches
BranchTrueFalseTotal
abs $d[$b] <=> abs $d[$a] or $m[$b] <=> $m[$a] or $d[$b] <=> $d[$a] or $e[$b] <=> $e[$a] 0 0 0
$order eq 'abs_asc' ? : 0 0 0
Condition0%0/6
abs $d[$b] <=> abs $d[$a] or $m[$b] <=> $m[$a] or $d[$b] <=> $d[$a]
l!l&&r!l&&!r
000
abs $d[$b] <=> abs $d[$a] or $m[$b] <=> $m[$a]
l!l&&r!l&&!r
000
MC/DC0%0/3
abs $d[$b] <=> abs $d[$a] or $m[$b] <=> $m[$a] or $d[$b] <=> $d[$a]
abs $d[$b] <=> abs $d[$a] $m[$b] <=> $m[$a] $d[$b] <=> $d[$a]
Truth table0%0/4
A or B or C
A= abs $d[$b] <=> abs $d[$a]B= $m[$b] <=> $m[$a]C= $d[$b] <=> $d[$a]
ABCresult
1XX1
01X1
0011
0000
1347 ($order eq 'abs_asc' ?
1348 0 ▶ sort { abs ($d[$a]) <=> abs ($d[$b]) || $m[$a] <=> $m[$b] || $d[$a] <=> $d[$b] || $e[$a] <=> $e[$b] } 0 .. $#d :
Branch: 2 branches
BranchTrueFalseTotal
abs $d[$a] <=> abs $d[$b] or $m[$a] <=> $m[$b] or $d[$a] <=> $d[$b] or $e[$a] <=> $e[$b] 0 0 0
$order eq 'norm_desc' ? : 0 0 0
Condition0%0/6
abs $d[$a] <=> abs $d[$b] or $m[$a] <=> $m[$b] or $d[$a] <=> $d[$b]
l!l&&r!l&&!r
000
abs $d[$a] <=> abs $d[$b] or $m[$a] <=> $m[$b]
l!l&&r!l&&!r
000
MC/DC0%0/3
abs $d[$a] <=> abs $d[$b] or $m[$a] <=> $m[$b] or $d[$a] <=> $d[$b]
abs $d[$a] <=> abs $d[$b] $m[$a] <=> $m[$b] $d[$a] <=> $d[$b]
Truth table0%0/4
A or B or C
A= abs $d[$a] <=> abs $d[$b]B= $m[$a] <=> $m[$b]C= $d[$a] <=> $d[$b]
ABCresult
1XX1
01X1
0011
0000
1349 ($order eq 'norm_desc' ?
1350 0 ▶ sort { $m[$b] <=> $m[$a] || $d[$b] <=> $d[$a] || $e[$b] <=> $e[$a] } 0 .. $#d :
Branch: 2 branches
BranchTrueFalseTotal
$m[$b] <=> $m[$a] or $d[$b] <=> $d[$a] or $e[$b] <=> $e[$a] 0 0 0
$order eq 'norm_asc' ? : 0 0 0
Condition0%0/3
$m[$b] <=> $m[$a] or $d[$b] <=> $d[$a]
l!l&&r!l&&!r
000
MC/DC0%0/2
$m[$b] <=> $m[$a] or $d[$b] <=> $d[$a]
$m[$b] <=> $m[$a] $d[$b] <=> $d[$a]
Truth table0%0/3
A or B
A= $m[$b] <=> $m[$a]B= $d[$b] <=> $d[$a]
ABresult
1X1
011
000
1351 ($order eq 'norm_asc' ?
1352 0 ▶ sort { $m[$a] <=> $m[$b] || $d[$a] <=> $d[$b] || $e[$a] <=> $e[$b] } 0 .. $#d :
Branch: 2 branches
BranchTrueFalseTotal
$m[$a] <=> $m[$b] or $d[$a] <=> $d[$b] or $e[$a] <=> $e[$b] 0 0 0
$order eq 'desc' ? : 0 0 0
Condition0%0/3
$m[$a] <=> $m[$b] or $d[$a] <=> $d[$b]
l!l&&r!l&&!r
000
MC/DC0%0/2
$m[$a] <=> $m[$b] or $d[$a] <=> $d[$b]
$m[$a] <=> $m[$b] $d[$a] <=> $d[$b]
Truth table0%0/3
A or B
A= $m[$a] <=> $m[$b]B= $d[$a] <=> $d[$b]
ABresult
1X1
011
000
1353 ($order eq 'desc' ?
1354 0 ▶ sort { $d[$b] <=> $d[$a] || $e[$b] <=> $e[$a] } 0 .. $#d :
Branch: 2 branches
BranchTrueFalseTotal
$d[$b] <=> $d[$a] or $e[$b] <=> $e[$a] 0 0 0
$order eq 'asc' ? : 0 0 0
1355 ($order eq 'asc' ?
1356 0 ▶ sort { $d[$a] <=> $d[$b] || $e[$a] <=> $e[$b] } 0 .. $#d :
Branch: 2 branches
BranchTrueFalseTotal
$order eq 'abs_desc' ? : 0 0 0
$d[$a] <=> $d[$b] or $e[$a] <=> $e[$b] 0 0 0
1357 croak ("Invalid argument")))))));
1358 }
1359 else
1360 {
1361 # Only real eigenvalues.
1362 2 my $d = $$self{value};
1363  
1364 @p = ($order eq 'abs_desc' || $order eq 'norm_desc' ?
1365 0 ▶ sort { abs ($$d[$b]) <=> abs ($$d[$a]) || $$d[$b] <=> $$d[$a] } 0 .. $#$d :
Branch: 2 branches
BranchTrueFalseTotal
abs $d->[$b] <=> abs $d->[$a] or $d->[$b] <=> $d->[$a] 0 0 0
$order eq 'abs_asc' || $order eq 'norm_asc' ? : 0 2 2
Condition33%1/3
$order eq "abs_asc" or $order eq "norm_asc"
l!l&&r!l&&!r
002
MC/DC0%0/2
$order eq "abs_asc" or $order eq "norm_asc"
$order eq "abs_asc" $order eq "norm_asc"
Truth table33%1/3
A or B
A= $order eq "abs_asc"B= $order eq "norm_asc"
ABresult
1X1
011
000
1366 ($order eq 'abs_asc' || $order eq 'norm_asc' ?
1367 0 ▶ sort { abs ($$d[$a]) <=> abs ($$d[$b]) || $$d[$a] <=> $$d[$b] } 0 .. $#$d :
Branch: 2 branches
BranchTrueFalseTotal
abs $d->[$a] <=> abs $d->[$b] or $d->[$a] <=> $d->[$b] 0 0 0
$order eq 'desc' ? : 0 2 2
1368 ($order eq 'desc' ?
1369 0 ▶ sort { $$d[$b] <=> $$d[$a] } 0 .. $#$d :
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'asc' ? : 2 0 2
1370 ($order eq 'asc' ?
1371 2 ▶ sort { $$d[$a] <=> $$d[$b] } 0 .. $#$d :
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'abs_desc' || $order eq 'norm_desc' ? : 0 2 2
Condition33%1/3
$order eq "abs_desc" or $order eq "norm_desc"
l!l&&r!l&&!r
002
MC/DC0%0/2
$order eq "abs_desc" or $order eq "norm_desc"
$order eq "abs_desc" $order eq "norm_desc"
Truth table33%1/3
A or B
A= $order eq "abs_desc"B= $order eq "norm_desc"
ABresult
1X1
011
000
1372 croak ("Invalid argument")))));
1373 }
1374  
1375 # Reorder eigenvalues and corresponding eigenvectors.
1376 2 ▶ if (@p > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@p > 0) 2 0 2
1377 {
1378 2 my $ref;
1379  
1380 2 $ref = $$self{value};
1381 2 @$ref = @$ref[@p];
1382  
1383 2 $ref = $$self{vector};
1384 2 @$ref = @$ref[@p];
1385 }
1386  
1387 # Return object.
1388 2 $self;
1389 }
1390  
1391 # Compare two eigenvectors.
1392 sub _cmp_vec
1393 {
1394 0 ▶ my ($u, $v) = @_;
Subroutine _cmp_vec: not called CC=4 SCAR=30.0
1395  
1396 0 ▶ croak ("Invalid argument")
Branch: 1 branch
BranchTrueFalseTotal
if $u->$#* != $v->$#* 0 0 0
1397 if $#$u != $#$v;
1398  
1399 0 my $d = 0;
1400  
1401 0 for my $i (0 .. $#$u)
1402 {
1403 0 ▶ last if $d = ($$u[$i] <=> $$v[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if $d = $u->[$i] <=> $v->[$i] 0 0 0
1404 }
1405  
1406 0 $d;
1407 }
1408  
1409 # Return one or more eigenvalues.
1410 sub value
1411 {
1412 2 ▶ my $self = shift;
Subroutine value: called CC=2 SCAR=7.6
Pod documented
1413  
1414 2 ▶ @_ > 0 ? @{ $$self{value} }[@_] : @{ $$self{value} };
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 ? : 0 2 2
1415 }
1416  
1417 1 *values = \&value;
1418  
1419 # Return one or more eigenvectors.
1420 sub vector
1421 {
1422 6 ▶ my $self = shift;
Subroutine vector: called CC=2 SCAR=7.6
Pod documented
1423  
1424 6 ▶ @_ > 0 ? @{ $$self{vector} }[@_] : @{ $$self{vector} };
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 ? : 6 0 6
1425 }
1426  
1427 1 *vectors = \&vector;
1428  
1429 1 1;
1430  
1431 __END__
« blib/lib/Math/MatrixDecomposition/Eigen.pm ↑ Summary blib/lib/Math/MatrixDecomposition/LU.pm »