1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Shared\JAMA; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* For an m-by-n matrix A with m >= n, the singular value decomposition is |
7
|
|
|
* an m-by-n orthogonal matrix U, an n-by-n diagonal matrix S, and |
8
|
|
|
* an n-by-n orthogonal matrix V so that A = U*S*V'. |
9
|
|
|
* |
10
|
|
|
* The singular values, sigma[$k] = S[$k][$k], are ordered so that |
11
|
|
|
* sigma[0] >= sigma[1] >= ... >= sigma[n-1]. |
12
|
|
|
* |
13
|
|
|
* The singular value decompostion always exists, so the constructor will |
14
|
|
|
* never fail. The matrix condition number and the effective numerical |
15
|
|
|
* rank can be computed from this decomposition. |
16
|
|
|
* |
17
|
|
|
* @author Paul Meagher |
18
|
|
|
* |
19
|
|
|
* @version 1.1 |
20
|
|
|
*/ |
21
|
|
|
class SingularValueDecomposition |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Internal storage of U. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $U = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Internal storage of V. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $V = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Internal storage of singular values. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $s = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Row dimension. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $m; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Column dimension. |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
private $n; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Construct the singular value decomposition. |
60
|
|
|
* |
61
|
|
|
* Derived from LINPACK code. |
62
|
|
|
* |
63
|
|
|
* @param $A Rectangular matrix |
|
|
|
|
64
|
|
|
* @param mixed $Arg |
65
|
|
|
* |
66
|
|
|
* @return Structure to access U, S and V |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function __construct($Arg) |
69
|
|
|
{ |
70
|
|
|
// Initialize. |
71
|
|
|
$A = $Arg->getArrayCopy(); |
72
|
|
|
$this->m = $Arg->getRowDimension(); |
73
|
|
|
$this->n = $Arg->getColumnDimension(); |
74
|
|
|
$nu = min($this->m, $this->n); |
75
|
|
|
$e = []; |
76
|
|
|
$work = []; |
77
|
|
|
$wantu = true; |
78
|
|
|
$wantv = true; |
79
|
|
|
$nct = min($this->m - 1, $this->n); |
80
|
|
|
$nrt = max(0, min($this->n - 2, $this->m)); |
81
|
|
|
|
82
|
|
|
// Reduce A to bidiagonal form, storing the diagonal elements |
83
|
|
|
// in s and the super-diagonal elements in e. |
84
|
|
|
for ($k = 0; $k < max($nct, $nrt); ++$k) { |
85
|
|
|
if ($k < $nct) { |
86
|
|
|
// Compute the transformation for the k-th column and |
87
|
|
|
// place the k-th diagonal in s[$k]. |
88
|
|
|
// Compute 2-norm of k-th column without under/overflow. |
89
|
|
|
$this->s[$k] = 0; |
90
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
91
|
|
|
$this->s[$k] = hypo($this->s[$k], $A[$i][$k]); |
92
|
|
|
} |
93
|
|
|
if ($this->s[$k] != 0.0) { |
94
|
|
|
if ($A[$k][$k] < 0.0) { |
95
|
|
|
$this->s[$k] = -$this->s[$k]; |
96
|
|
|
} |
97
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
98
|
|
|
$A[$i][$k] /= $this->s[$k]; |
99
|
|
|
} |
100
|
|
|
$A[$k][$k] += 1.0; |
101
|
|
|
} |
102
|
|
|
$this->s[$k] = -$this->s[$k]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
for ($j = $k + 1; $j < $this->n; ++$j) { |
106
|
|
|
if (($k < $nct) & ($this->s[$k] != 0.0)) { |
|
|
|
|
107
|
|
|
// Apply the transformation. |
108
|
|
|
$t = 0; |
109
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
110
|
|
|
$t += $A[$i][$k] * $A[$i][$j]; |
111
|
|
|
} |
112
|
|
|
$t = -$t / $A[$k][$k]; |
113
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
114
|
|
|
$A[$i][$j] += $t * $A[$i][$k]; |
115
|
|
|
} |
116
|
|
|
// Place the k-th row of A into e for the |
117
|
|
|
// subsequent calculation of the row transformation. |
118
|
|
|
$e[$j] = $A[$k][$j]; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($wantu and ($k < $nct)) { |
123
|
|
|
// Place the transformation in U for subsequent back |
124
|
|
|
// multiplication. |
125
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
126
|
|
|
$this->U[$i][$k] = $A[$i][$k]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($k < $nrt) { |
131
|
|
|
// Compute the k-th row transformation and place the |
132
|
|
|
// k-th super-diagonal in e[$k]. |
133
|
|
|
// Compute 2-norm without under/overflow. |
134
|
|
|
$e[$k] = 0; |
135
|
|
|
for ($i = $k + 1; $i < $this->n; ++$i) { |
136
|
|
|
$e[$k] = hypo($e[$k], $e[$i]); |
137
|
|
|
} |
138
|
|
|
if ($e[$k] != 0.0) { |
139
|
|
|
if ($e[$k + 1] < 0.0) { |
140
|
|
|
$e[$k] = -$e[$k]; |
141
|
|
|
} |
142
|
|
|
for ($i = $k + 1; $i < $this->n; ++$i) { |
143
|
|
|
$e[$i] /= $e[$k]; |
144
|
|
|
} |
145
|
|
|
$e[$k + 1] += 1.0; |
146
|
|
|
} |
147
|
|
|
$e[$k] = -$e[$k]; |
148
|
|
|
if (($k + 1 < $this->m) and ($e[$k] != 0.0)) { |
149
|
|
|
// Apply the transformation. |
150
|
|
View Code Duplication |
for ($i = $k + 1; $i < $this->m; ++$i) { |
|
|
|
|
151
|
|
|
$work[$i] = 0.0; |
152
|
|
|
} |
153
|
|
|
for ($j = $k + 1; $j < $this->n; ++$j) { |
154
|
|
|
for ($i = $k + 1; $i < $this->m; ++$i) { |
155
|
|
|
$work[$i] += $e[$j] * $A[$i][$j]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
for ($j = $k + 1; $j < $this->n; ++$j) { |
159
|
|
|
$t = -$e[$j] / $e[$k + 1]; |
160
|
|
|
for ($i = $k + 1; $i < $this->m; ++$i) { |
161
|
|
|
$A[$i][$j] += $t * $work[$i]; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
View Code Duplication |
if ($wantv) { |
|
|
|
|
166
|
|
|
// Place the transformation in V for subsequent |
167
|
|
|
// back multiplication. |
168
|
|
|
for ($i = $k + 1; $i < $this->n; ++$i) { |
169
|
|
|
$this->V[$i][$k] = $e[$i]; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Set up the final bidiagonal matrix or order p. |
176
|
|
|
$p = min($this->n, $this->m + 1); |
177
|
|
|
if ($nct < $this->n) { |
178
|
|
|
$this->s[$nct] = $A[$nct][$nct]; |
179
|
|
|
} |
180
|
|
|
if ($this->m < $p) { |
181
|
|
|
$this->s[$p - 1] = 0.0; |
182
|
|
|
} |
183
|
|
|
if ($nrt + 1 < $p) { |
184
|
|
|
$e[$nrt] = $A[$nrt][$p - 1]; |
185
|
|
|
} |
186
|
|
|
$e[$p - 1] = 0.0; |
187
|
|
|
// If required, generate U. |
188
|
|
|
if ($wantu) { |
189
|
|
|
for ($j = $nct; $j < $nu; ++$j) { |
190
|
|
|
for ($i = 0; $i < $this->m; ++$i) { |
191
|
|
|
$this->U[$i][$j] = 0.0; |
192
|
|
|
} |
193
|
|
|
$this->U[$j][$j] = 1.0; |
194
|
|
|
} |
195
|
|
|
for ($k = $nct - 1; $k >= 0; --$k) { |
196
|
|
|
if ($this->s[$k] != 0.0) { |
197
|
|
View Code Duplication |
for ($j = $k + 1; $j < $nu; ++$j) { |
|
|
|
|
198
|
|
|
$t = 0; |
199
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
200
|
|
|
$t += $this->U[$i][$k] * $this->U[$i][$j]; |
201
|
|
|
} |
202
|
|
|
$t = -$t / $this->U[$k][$k]; |
203
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
204
|
|
|
$this->U[$i][$j] += $t * $this->U[$i][$k]; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
for ($i = $k; $i < $this->m; ++$i) { |
208
|
|
|
$this->U[$i][$k] = -$this->U[$i][$k]; |
209
|
|
|
} |
210
|
|
|
$this->U[$k][$k] = 1.0 + $this->U[$k][$k]; |
211
|
|
View Code Duplication |
for ($i = 0; $i < $k - 1; ++$i) { |
|
|
|
|
212
|
|
|
$this->U[$i][$k] = 0.0; |
213
|
|
|
} |
214
|
|
|
} else { |
215
|
|
View Code Duplication |
for ($i = 0; $i < $this->m; ++$i) { |
|
|
|
|
216
|
|
|
$this->U[$i][$k] = 0.0; |
217
|
|
|
} |
218
|
|
|
$this->U[$k][$k] = 1.0; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
// If required, generate V. |
224
|
|
|
if ($wantv) { |
225
|
|
|
for ($k = $this->n - 1; $k >= 0; --$k) { |
226
|
|
|
if (($k < $nrt) and ($e[$k] != 0.0)) { |
227
|
|
|
for ($j = $k + 1; $j < $nu; ++$j) { |
228
|
|
|
$t = 0; |
229
|
|
|
for ($i = $k + 1; $i < $this->n; ++$i) { |
230
|
|
|
$t += $this->V[$i][$k] * $this->V[$i][$j]; |
231
|
|
|
} |
232
|
|
|
$t = -$t / $this->V[$k + 1][$k]; |
233
|
|
|
for ($i = $k + 1; $i < $this->n; ++$i) { |
234
|
|
|
$this->V[$i][$j] += $t * $this->V[$i][$k]; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
View Code Duplication |
for ($i = 0; $i < $this->n; ++$i) { |
|
|
|
|
239
|
|
|
$this->V[$i][$k] = 0.0; |
240
|
|
|
} |
241
|
|
|
$this->V[$k][$k] = 1.0; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// Main iteration loop for the singular values. |
246
|
|
|
$pp = $p - 1; |
247
|
|
|
$iter = 0; |
248
|
|
|
$eps = pow(2.0, -52.0); |
249
|
|
|
|
250
|
|
|
while ($p > 0) { |
251
|
|
|
// Here is where a test for too many iterations would go. |
252
|
|
|
// This section of the program inspects for negligible |
253
|
|
|
// elements in the s and e arrays. On completion the |
254
|
|
|
// variables kase and k are set as follows: |
255
|
|
|
// kase = 1 if s(p) and e[k-1] are negligible and k<p |
256
|
|
|
// kase = 2 if s(k) is negligible and k<p |
257
|
|
|
// kase = 3 if e[k-1] is negligible, k<p, and |
258
|
|
|
// s(k), ..., s(p) are not negligible (qr step). |
259
|
|
|
// kase = 4 if e(p-1) is negligible (convergence). |
260
|
|
|
for ($k = $p - 2; $k >= -1; --$k) { |
261
|
|
|
if ($k == -1) { |
262
|
|
|
break; |
263
|
|
|
} |
264
|
|
|
if (abs($e[$k]) <= $eps * (abs($this->s[$k]) + abs($this->s[$k + 1]))) { |
265
|
|
|
$e[$k] = 0.0; |
266
|
|
|
|
267
|
|
|
break; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
if ($k == $p - 2) { |
271
|
|
|
$kase = 4; |
272
|
|
|
} else { |
273
|
|
|
for ($ks = $p - 1; $ks >= $k; --$ks) { |
274
|
|
|
if ($ks == $k) { |
275
|
|
|
break; |
276
|
|
|
} |
277
|
|
|
$t = ($ks != $p ? abs($e[$ks]) : 0.) + ($ks != $k + 1 ? abs($e[$ks - 1]) : 0.); |
278
|
|
|
if (abs($this->s[$ks]) <= $eps * $t) { |
279
|
|
|
$this->s[$ks] = 0.0; |
280
|
|
|
|
281
|
|
|
break; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
if ($ks == $k) { |
285
|
|
|
$kase = 3; |
286
|
|
|
} elseif ($ks == $p - 1) { |
287
|
|
|
$kase = 1; |
288
|
|
|
} else { |
289
|
|
|
$kase = 2; |
290
|
|
|
$k = $ks; |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
++$k; |
294
|
|
|
|
295
|
|
|
// Perform the task indicated by kase. |
296
|
|
|
switch ($kase) { |
297
|
|
|
// Deflate negligible s(p). |
298
|
|
|
case 1: |
299
|
|
|
$f = $e[$p - 2]; |
300
|
|
|
$e[$p - 2] = 0.0; |
301
|
|
|
for ($j = $p - 2; $j >= $k; --$j) { |
302
|
|
|
$t = hypo($this->s[$j], $f); |
303
|
|
|
$cs = $this->s[$j] / $t; |
304
|
|
|
$sn = $f / $t; |
305
|
|
|
$this->s[$j] = $t; |
306
|
|
|
if ($j != $k) { |
307
|
|
|
$f = -$sn * $e[$j - 1]; |
308
|
|
|
$e[$j - 1] = $cs * $e[$j - 1]; |
309
|
|
|
} |
310
|
|
View Code Duplication |
if ($wantv) { |
|
|
|
|
311
|
|
|
for ($i = 0; $i < $this->n; ++$i) { |
312
|
|
|
$t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$p - 1]; |
313
|
|
|
$this->V[$i][$p - 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$p - 1]; |
314
|
|
|
$this->V[$i][$j] = $t; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
break; |
320
|
|
|
// Split at negligible s(k). |
321
|
|
|
case 2: |
322
|
|
|
$f = $e[$k - 1]; |
323
|
|
|
$e[$k - 1] = 0.0; |
324
|
|
|
for ($j = $k; $j < $p; ++$j) { |
325
|
|
|
$t = hypo($this->s[$j], $f); |
326
|
|
|
$cs = $this->s[$j] / $t; |
327
|
|
|
$sn = $f / $t; |
328
|
|
|
$this->s[$j] = $t; |
329
|
|
|
$f = -$sn * $e[$j]; |
330
|
|
|
$e[$j] = $cs * $e[$j]; |
331
|
|
View Code Duplication |
if ($wantu) { |
|
|
|
|
332
|
|
|
for ($i = 0; $i < $this->m; ++$i) { |
333
|
|
|
$t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$k - 1]; |
334
|
|
|
$this->U[$i][$k - 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$k - 1]; |
335
|
|
|
$this->U[$i][$j] = $t; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
break; |
341
|
|
|
// Perform one qr step. |
342
|
|
|
case 3: |
343
|
|
|
// Calculate the shift. |
344
|
|
|
$scale = max(max(max(max(abs($this->s[$p - 1]), abs($this->s[$p - 2])), abs($e[$p - 2])), abs($this->s[$k])), abs($e[$k])); |
345
|
|
|
$sp = $this->s[$p - 1] / $scale; |
346
|
|
|
$spm1 = $this->s[$p - 2] / $scale; |
347
|
|
|
$epm1 = $e[$p - 2] / $scale; |
348
|
|
|
$sk = $this->s[$k] / $scale; |
349
|
|
|
$ek = $e[$k] / $scale; |
350
|
|
|
$b = (($spm1 + $sp) * ($spm1 - $sp) + $epm1 * $epm1) / 2.0; |
351
|
|
|
$c = ($sp * $epm1) * ($sp * $epm1); |
352
|
|
|
$shift = 0.0; |
353
|
|
|
if (($b != 0.0) || ($c != 0.0)) { |
354
|
|
|
$shift = sqrt($b * $b + $c); |
355
|
|
|
if ($b < 0.0) { |
356
|
|
|
$shift = -$shift; |
357
|
|
|
} |
358
|
|
|
$shift = $c / ($b + $shift); |
359
|
|
|
} |
360
|
|
|
$f = ($sk + $sp) * ($sk - $sp) + $shift; |
361
|
|
|
$g = $sk * $ek; |
362
|
|
|
// Chase zeros. |
363
|
|
|
for ($j = $k; $j < $p - 1; ++$j) { |
364
|
|
|
$t = hypo($f, $g); |
365
|
|
|
$cs = $f / $t; |
366
|
|
|
$sn = $g / $t; |
367
|
|
|
if ($j != $k) { |
368
|
|
|
$e[$j - 1] = $t; |
369
|
|
|
} |
370
|
|
|
$f = $cs * $this->s[$j] + $sn * $e[$j]; |
371
|
|
|
$e[$j] = $cs * $e[$j] - $sn * $this->s[$j]; |
372
|
|
|
$g = $sn * $this->s[$j + 1]; |
373
|
|
|
$this->s[$j + 1] = $cs * $this->s[$j + 1]; |
374
|
|
View Code Duplication |
if ($wantv) { |
|
|
|
|
375
|
|
|
for ($i = 0; $i < $this->n; ++$i) { |
376
|
|
|
$t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$j + 1]; |
377
|
|
|
$this->V[$i][$j + 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$j + 1]; |
378
|
|
|
$this->V[$i][$j] = $t; |
379
|
|
|
} |
380
|
|
|
} |
381
|
|
|
$t = hypo($f, $g); |
382
|
|
|
$cs = $f / $t; |
383
|
|
|
$sn = $g / $t; |
384
|
|
|
$this->s[$j] = $t; |
385
|
|
|
$f = $cs * $e[$j] + $sn * $this->s[$j + 1]; |
386
|
|
|
$this->s[$j + 1] = -$sn * $e[$j] + $cs * $this->s[$j + 1]; |
387
|
|
|
$g = $sn * $e[$j + 1]; |
388
|
|
|
$e[$j + 1] = $cs * $e[$j + 1]; |
389
|
|
View Code Duplication |
if ($wantu && ($j < $this->m - 1)) { |
|
|
|
|
390
|
|
|
for ($i = 0; $i < $this->m; ++$i) { |
391
|
|
|
$t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$j + 1]; |
392
|
|
|
$this->U[$i][$j + 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$j + 1]; |
393
|
|
|
$this->U[$i][$j] = $t; |
394
|
|
|
} |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
$e[$p - 2] = $f; |
398
|
|
|
$iter = $iter + 1; |
399
|
|
|
|
400
|
|
|
break; |
401
|
|
|
// Convergence. |
402
|
|
|
case 4: |
403
|
|
|
// Make the singular values positive. |
404
|
|
|
if ($this->s[$k] <= 0.0) { |
405
|
|
|
$this->s[$k] = ($this->s[$k] < 0.0 ? -$this->s[$k] : 0.0); |
406
|
|
View Code Duplication |
if ($wantv) { |
|
|
|
|
407
|
|
|
for ($i = 0; $i <= $pp; ++$i) { |
408
|
|
|
$this->V[$i][$k] = -$this->V[$i][$k]; |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
// Order the singular values. |
413
|
|
|
while ($k < $pp) { |
414
|
|
|
if ($this->s[$k] >= $this->s[$k + 1]) { |
415
|
|
|
break; |
416
|
|
|
} |
417
|
|
|
$t = $this->s[$k]; |
418
|
|
|
$this->s[$k] = $this->s[$k + 1]; |
419
|
|
|
$this->s[$k + 1] = $t; |
420
|
|
View Code Duplication |
if ($wantv and ($k < $this->n - 1)) { |
|
|
|
|
421
|
|
|
for ($i = 0; $i < $this->n; ++$i) { |
422
|
|
|
$t = $this->V[$i][$k + 1]; |
423
|
|
|
$this->V[$i][$k + 1] = $this->V[$i][$k]; |
424
|
|
|
$this->V[$i][$k] = $t; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
View Code Duplication |
if ($wantu and ($k < $this->m - 1)) { |
|
|
|
|
428
|
|
|
for ($i = 0; $i < $this->m; ++$i) { |
429
|
|
|
$t = $this->U[$i][$k + 1]; |
430
|
|
|
$this->U[$i][$k + 1] = $this->U[$i][$k]; |
431
|
|
|
$this->U[$i][$k] = $t; |
432
|
|
|
} |
433
|
|
|
} |
434
|
|
|
++$k; |
435
|
|
|
} |
436
|
|
|
$iter = 0; |
437
|
|
|
--$p; |
438
|
|
|
|
439
|
|
|
break; |
440
|
|
|
} // end switch |
441
|
|
|
} // end while |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Return the left singular vectors. |
446
|
|
|
* |
447
|
|
|
* @return U |
|
|
|
|
448
|
|
|
*/ |
449
|
|
|
public function getU() |
450
|
|
|
{ |
451
|
|
|
return new Matrix($this->U, $this->m, min($this->m + 1, $this->n)); |
|
|
|
|
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Return the right singular vectors. |
456
|
|
|
* |
457
|
|
|
* @return V |
|
|
|
|
458
|
|
|
*/ |
459
|
|
|
public function getV() |
460
|
|
|
{ |
461
|
|
|
return new Matrix($this->V); |
|
|
|
|
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Return the one-dimensional array of singular values. |
466
|
|
|
* |
467
|
|
|
* @return diagonal of S |
|
|
|
|
468
|
|
|
*/ |
469
|
|
|
public function getSingularValues() |
470
|
|
|
{ |
471
|
|
|
return $this->s; |
|
|
|
|
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* Return the diagonal matrix of singular values. |
476
|
|
|
* |
477
|
|
|
* @return S |
478
|
|
|
*/ |
479
|
|
|
public function getS() |
480
|
|
|
{ |
481
|
|
|
for ($i = 0; $i < $this->n; ++$i) { |
482
|
|
|
for ($j = 0; $j < $this->n; ++$j) { |
483
|
|
|
$S[$i][$j] = 0.0; |
484
|
|
|
} |
485
|
|
|
$S[$i][$i] = $this->s[$i]; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
return new Matrix($S); |
|
|
|
|
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
/** |
492
|
|
|
* Two norm. |
493
|
|
|
* |
494
|
|
|
* @return max(S) |
|
|
|
|
495
|
|
|
*/ |
496
|
|
|
public function norm2() |
497
|
|
|
{ |
498
|
|
|
return $this->s[0]; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Two norm condition number. |
503
|
|
|
* |
504
|
|
|
* @return max(S)/min(S) |
505
|
|
|
*/ |
506
|
|
|
public function cond() |
507
|
|
|
{ |
508
|
|
|
return $this->s[0] / $this->s[min($this->m, $this->n) - 1]; |
|
|
|
|
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Effective numerical matrix rank. |
513
|
|
|
* |
514
|
|
|
* @return Number of nonnegligible singular values |
515
|
|
|
*/ |
516
|
|
|
public function rank() |
517
|
|
|
{ |
518
|
|
|
$eps = pow(2.0, -52.0); |
519
|
|
|
$tol = max($this->m, $this->n) * $this->s[0] * $eps; |
520
|
|
|
$r = 0; |
521
|
|
|
for ($i = 0; $i < count($this->s); ++$i) { |
|
|
|
|
522
|
|
|
if ($this->s[$i] > $tol) { |
523
|
|
|
++$r; |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
return $r; |
528
|
|
|
} |
529
|
|
|
} |
530
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths