-
Notifications
You must be signed in to change notification settings - Fork 5
/
SVDCMP.CPP
614 lines (512 loc) · 18.9 KB
/
SVDCMP.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/******************************************************************************/
/* */
/* SING_VAL - SingularValueDecomp object routines for performing singular */
/* value decomposition on a matrix, and using backsubstitution */
/* to find least squares solutions to simultaneous equations. */
/* */
/* The decomposition algorithm is yet another implementation of */
/* the classic method of Golub and Reinsch (Wilkinson, J.H. and */
/* Reinsch, C., 1971, 'Handbook for Automatic Computation' vol. 2)*/
/* Some tricks have been taken from later sources. See (Press */
/* et al 'Numerical Recipes in C') for a complete list of */
/* references. */
/* */
/* Copyright (c) 1993 by Academic Press, Inc. */
/* */
/* All rights reserved. Permission is hereby granted, until further notice, */
/* to make copies of this diskette, which are not for resale, provided these */
/* copies are made from this master diskette only, and provided that the */
/* following copyright notice appears on the diskette label: */
/* (c) 1993 by Academic Press, Inc. */
/* */
/* Except as previously stated, no part of the computer program embodied in */
/* this diskette may be reproduced or transmitted in any form or by any means,*/
/* electronic or mechanical, including input into storage in any information */
/* system for resale, without permission in writing from the publisher. */
/* */
/* Produced in the United States of America. */
/* */
/* ISBN 0-12-479041-0 */
/* */
/******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
/*
Prototypes for local subroutines
*/
static double bidiag ( double *a , int rows , int cols , double *w ,
double *work ) ;
static void cancel ( int rows , int cols , int lower , int index ,
double matnorm , double *a , double *w , double *work ) ;
static void qr ( int rows , int cols , int lower , int index ,
double *a , double *v , double *w , double *work ) ;
static void transforms ( double *a , int rows , int cols , double *w ,
double *v , double *work ) ;
static void verify_nonneg ( int cols , int index , double *w , double *v ) ;
/*
Local macros. RSS computes the root of the sum of squares of its arguments.
This clever implementation avoids over/underflow. SIGN is the old FORTRAN
routine which returns the value of its first argument with the sign of its
second. The variables va, vb and vc are local work areas for these macros.
*/
static double va, vb, vc ;
#define RSS(a,b) ((va=fabs(a)) > (vb=fabs(b)) ? \
(vc=vb/va , va*sqrt(vc*vc+1.0)) : \
((vb != 0.0) ? (vc=va/vb , vb*sqrt(vc*vc+1.0)) : 0.0))
#define SIGN(a,b) (va=fabs(a) , (b) >= 0.0 ? va : -va)
/*
--------------------------------------------------------------------------------
SingularValueDecomp - Singular value decomposition of matrices
Normally, the user would:
1) Use 'new' to create a SingularValueDecomp object with all necessary
memory (a, u?, w, v, work, b) allocated by the constructor.
2) Fill in the public 'a' with the matrix to be decomposed.
3) Call svdcmp to decompose a, replacing it with the U matrix if preserve
is zero, else computing u.
This will also compute w and v, which are normally not used but are
public just in case the user wants to access them.
4) Fill in the 'b' vector with the right hand side of the equations.
5) Call backsub with a pointer to the cols vector which is where the
solution will be placed. This vector is NOT allocated by the
constructor. The outputs of svdcmp (a, u?, w, v) will not be disturbed.
6) Repeat the above step as desired.
7) Delete the SingularValueDecomp object, which frees all memory which
was allocated by the constructor.
--------------------------------------------------------------------------------
*/
class SingularValueDecomp {
public:
SingularValueDecomp ( int rows , int cols , int preserve ) ;
~SingularValueDecomp () ;
void svdcmp () ;
void backsub ( double thresh , double *x ) ;
int ok ; // Was memory allocation successful?
/*
The following four input/output areas are allocated by the constructor
*/
double *a ; // Rows by cols input of 'A' matrix, output of U
double *u ; // unless preserve != 0, in which case U output here
double *w ; // Cols vector output of singular values, not sorted
double *v ; // Cols by cols output of 'V' matrix
double *b ; // Rows vector of RHS input to backsub
private:
int rows ; // Number of rows in 'A' matrix
int cols ; // and number of columns
double *work ; // Cols work vector (allocated by constructor)
} ;
/*
--------------------------------------------------------------------------------
Constructor - This allocates memory for the input/output matrix 'a' and
any work areas which it will need (including the public
outputs of w and v). It also allocates 'b' which will be
input to the backsub routine. It does not allocate 'x' which
is the output of backsub.
If there is a problem (rows < cols, or insufficient memory),
it leaves public ok=0. The user should check for this after
allocating with new.
--------------------------------------------------------------------------------
*/
SingularValueDecomp::SingularValueDecomp ( int nrows , int ncols , int preserve)
{
if (nrows < ncols) {
rows = cols = ok = 0 ;
return ;
}
a = u = w = v = work = b = NULL ;
if (((a = (double *) malloc ( nrows * ncols * sizeof(double) )) == NULL)
|| (preserve &&
(u = (double *) malloc ( nrows * ncols * sizeof(double)))== NULL)
|| ((w = (double *) malloc ( ncols * sizeof(double) )) == NULL)
|| ((v = (double *) malloc ( ncols * ncols * sizeof(double) )) == NULL)
|| ((work = (double *) malloc ( ncols * sizeof(double) )) == NULL)
|| ((b = (double *) malloc ( nrows * sizeof(double) )) == NULL)) {
if (a != NULL)
free ( a ) ;
if (u != NULL)
free ( u ) ;
if (w != NULL)
free ( w ) ;
if (v != NULL)
free ( v ) ;
if (work != NULL)
free ( work ) ;
if (b != NULL)
free ( b ) ;
rows = cols = ok = 0 ;
return ;
}
rows = nrows ;
cols = ncols ;
ok = 1 ;
}
/*
--------------------------------------------------------------------------------
Destructor - This frees all memory allocated by the constructor.
--------------------------------------------------------------------------------
*/
SingularValueDecomp::~SingularValueDecomp ()
{
if (! ok) // If constructor's mallocs failed
return ; // there is nothing to free
free ( a ) ;
if (u != NULL) // This was allocated only if preserve was nonzero
free ( u ) ;
free ( w ) ;
free ( v ) ;
free ( work ) ;
free ( b ) ;
}
/*
--------------------------------------------------------------------------------
svdcmp - Perform singular value decomposition on the matrix already stored.
--------------------------------------------------------------------------------
*/
void SingularValueDecomp::svdcmp ()
{
int cflag, iter, index, lower ;
double matnorm, *mat ;
if (u == NULL) // Do we replace a with u
mat = a ;
else { // or preserve it?
memcpy ( u , a , rows * cols * sizeof(double) ) ;
mat = u ;
}
matnorm = bidiag ( mat , rows , cols , w , work ) ; // Reduce to bidiagonal
transforms ( mat , rows , cols , w , v , work ) ; // Accumulate R&L trans
for (index=cols-1 ; index>=0 ; index--) { // All singular values
for (iter=0 ; iter<100 ; iter++) { // Conservative limit on QR tries
cflag = 1 ;
for (lower=index ; lower ; lower--) { // Split?
if (fabs (work[lower]) + matnorm == matnorm) {
cflag = 0 ;
break ;
}
if (fabs (w[lower-1]) + matnorm == matnorm)
break ;
}
if (lower && cflag)
cancel ( rows , cols , lower , index , matnorm , mat , w , work ) ;
if (lower == index) { // Converged?
verify_nonneg ( cols , index , w , v ) ; // Want nonegative singvals
break ;
}
qr ( rows , cols , lower , index , mat , v , w , work ) ; // Another QR
}
}
}
/*
--------------------------------------------------------------------------------
bidiag - Local routine for Householder reduction to bidiagonal form
--------------------------------------------------------------------------------
*/
static double bidiag (
double *a ,
int rows ,
int cols ,
double *w ,
double *work
)
{
int col, j, k, nextcol ;
double pp, qq, denom, sum ;
double matnorm, scale ;
matnorm = qq = sum = scale = 0.0 ;
for (col=0 ; col<cols ; col++) {
nextcol = col + 1 ;
work[col] = scale * qq ;
qq = sum = scale = 0.0 ;
for (k=col ; k<rows ; k++)
scale += fabs ( a[k*cols+col] ) ;
if (scale > 0.0) {
for (k=col ; k<rows ; k++) {
a[k*cols+col] /= scale ;
sum += a[k*cols+col] * a[k*cols+col] ;
}
pp = a[col*cols+col] ;
qq = -SIGN ( sqrt(sum) , pp ) ;
denom = pp * qq - sum ;
a[col*cols+col] = pp - qq ;
for (j=nextcol ; j<cols ; j++) {
sum = 0.0 ;
for (k=col ; k<rows ; k++)
sum += a[k*cols+col] * a[k*cols+j] ;
pp = sum / denom ;
for (k=col ; k<rows ; k++)
a[k*cols+j] += pp * a[k*cols+col] ;
}
for (k=col ; k<rows ; k++)
a[k*cols+col] *= scale ;
} // if scale > 0
w[col] = scale * qq ;
qq = sum = scale = 0.0 ;
for (k=nextcol ; k<cols ; k++)
scale += fabs ( a[col*cols+k] ) ;
if (scale > 0.0) {
for (k=nextcol ; k<cols ; k++) {
a[col*cols+k] /= scale ;
sum += a[col*cols+k] * a[col*cols+k] ;
}
pp = a[col*cols+nextcol] ;
qq = -SIGN ( sqrt ( sum ) , pp ) ;
denom = pp * qq - sum ;
a[col*cols+nextcol] = pp - qq ;
for (k=nextcol ; k<cols ; k++)
work[k] = a[col*cols+k] / denom ;
if (col != rows-1) {
for (j=nextcol ; j<rows ; j++) {
sum = 0.0 ;
for (k=nextcol ; k<cols ; k++)
sum += a[j*cols+k] * a[col*cols+k] ;
for (k=nextcol ; k<cols ; k++)
a[j*cols+k] += sum * work[k] ;
}
}
for (k=nextcol ; k<cols ; k++)
a[col*cols+k] *= scale ;
}
sum = fabs (w[col]) + fabs (work[col]) ;
if (sum > matnorm)
matnorm = sum ;
}
return matnorm ;
}
/*
--------------------------------------------------------------------------------
cancel
--------------------------------------------------------------------------------
*/
static void cancel (
int rows ,
int cols ,
int lower ,
int index ,
double matnorm ,
double *a ,
double *w ,
double *work
)
{
int col, row ;
double c, rr, ww, hypot, s, pp, qq ;
s = 1.0 ;
for (col=lower ; col<=index ; col++) {
rr = s * work[col] ;
if (fabs (rr) + matnorm != matnorm) {
ww = w[col] ;
hypot = RSS ( rr , ww ) ;
w[col] = hypot ;
c = ww / hypot ;
s = -rr / hypot ;
for (row=0 ; row<rows ; row++) {
pp = a[row*cols+lower-1] ;
qq = a[row*cols+col] ;
a[row*cols+lower-1] = qq * s + pp * c ;
a[row*cols+col] = qq * c - pp * s ;
}
}
}
}
/*
--------------------------------------------------------------------------------
Cumulate right and left transforms
--------------------------------------------------------------------------------
*/
static void transforms (
double *a ,
int rows ,
int cols ,
double *w ,
double *v ,
double *work
)
{
int col, j, k ;
double temp, ww, sum ;
/*
Right
*/
ww = 0.0 ; // Insures failure of upcoming if first time
for (col=cols-1 ; col>=0 ; col--) {
if (ww != 0.0) {
for (j=col+1 ; j<cols ; j++) // Double division avoids underflow
v[j*cols+col] = (a[col*cols+j] / a[col*cols+col+1]) / ww ;
for (j=col+1 ; j<cols ; j++) {
sum = 0.0 ;
for (k=col+1 ; k<cols ; k++)
sum += a[col*cols+k] * v[k*cols+j] ;
for (k=col+1 ; k<cols ; k++)
v[k*cols+j] += v[k*cols+col] * sum ;
}
}
for (j=col+1 ; j<cols ; j++)
v[col*cols+j] = v[j*cols+col] = 0.0 ;
v[col*cols+col] = 1.0 ;
ww = work[col] ;
}
/*
Left
*/
for (col=cols-1 ; col>=0 ; col--) {
for (j=col+1 ; j<cols ; j++)
a[col*cols+j] = 0.0 ;
if (w[col] == 0.0) {
for (j=col ; j<rows ; j++)
a[j*cols+col] = 0.0 ;
}
else {
ww = 1.0 / w[col] ;
for (j=col+1 ; j<cols ; j++) {
sum = 0.0 ;
for (k=col+1 ; k<rows ; k++)
sum += a[k*cols+col] * a[k*cols+j] ;
temp = sum / a[col*cols+col] * ww ;
for (k=col ; k<rows ; k++)
a[k*cols+j] += a[k*cols+col] * temp ;
}
for (j=col ; j<rows ; j++)
a[j*cols+col] *= ww ;
}
a[col*cols+col] += 1.0 ;
}
}
/*
--------------------------------------------------------------------------------
qr
--------------------------------------------------------------------------------
*/
static void qr (
int rows ,
int cols ,
int lower ,
int index ,
double *a ,
double *v ,
double *w ,
double *work )
{
int col, colp1, row ;
double c, cn, s, sn, thisw, rot1, rot2, hypot, temp, ww ;
ww = w[index] ;
sn = work[index] ;
rot1 = work[index-1] ;
rot2 = w[index-1] ;
temp = ((rot2-ww) * (rot2+ww) + (rot1-sn) * (rot1+sn)) / (2.0 * sn * rot2) ;
hypot = RSS ( temp , 1.0 ) ;
thisw = w[lower] ;
cn = ((thisw-ww) * (thisw+ww) + sn *
((rot2 / (temp + SIGN(hypot,temp))) - sn )) / thisw ;
c = s = 1.0 ;
for (col=lower ; col<index ; col++) {
colp1 = col+1 ;
rot1 = work[colp1] ;
sn = s * rot1 ;
rot1 = c * rot1 ;
hypot = RSS ( cn , sn ) ;
work[col] = hypot ;
c = cn / hypot ;
s = sn / hypot ;
cn = thisw * c + rot1 * s ;
rot1 = rot1 * c - thisw * s ;
rot2 = w[colp1] ;
sn = rot2 * s ;
rot2 *= c ;
for (row=0 ; row<cols ; row++) {
thisw = v[row*cols+col] ;
temp = v[row*cols+colp1] ;
v[row*cols+col] = thisw * c + temp * s ;
v[row*cols+colp1] = temp * c - thisw * s ;
}
hypot = RSS ( cn , sn ) ;
w[col] = hypot ;
if (hypot != 0.0) {
c = cn / hypot ;
s = sn / hypot ;
}
cn = c * rot1 + s * rot2 ;
thisw = c * rot2 - s * rot1 ;
for (row=0 ; row<rows ; row++) {
rot1 = a[row*cols+col] ;
rot2 = a[row*cols+colp1] ;
a[row*cols+col] = rot1 * c + rot2 * s ;
a[row*cols+colp1] = rot2 * c - rot1 * s ;
}
}
w[index] = thisw ;
work[lower] = 0.0 ;
work[index] = cn ;
}
/*
--------------------------------------------------------------------------------
verify_nonneg - Flip sign of this singular value and its vector if negative
--------------------------------------------------------------------------------
*/
static void verify_nonneg (
int cols ,
int index ,
double *w ,
double *v
)
{
int i ;
if (w[index] < 0.0) {
w[index] = -w[index] ;
for (i=0 ; i<cols ; i++)
v[i*cols+index] = -v[i*cols+index] ;
}
}
/*
--------------------------------------------------------------------------------
Backsubstitution algorithm for solving Ax=b where A generated u, w, v
Inputs are not destroyed, so it may be called with several b's.
The user must have filled in the public RHS 'b' before calling this.
--------------------------------------------------------------------------------
*/
void SingularValueDecomp::backsub (
double thresh , // Threshold for zeroing singular values. Typically 1.e-8.
double *x // Output of solution
)
{
int row, col, cc ;
double sum, *mat ;
if (u == NULL) // Did we replace a with u
mat = a ;
else // or preserve it?
mat = u ;
/*
Set the threshold according to the maximum singular value
*/
sum = 0.0 ; // Will hold max w
for (col=0 ; col<cols ; col++) {
if (w[col] > sum)
sum = w[col] ;
}
thresh *= sum ;
if (thresh <= 0.0) // Avoid dividing by zero in next step
thresh = 1.e-30 ;
/*
Find U'b
*/
for (col=0 ; col<cols ; col++) {
sum = 0.0 ;
if (w[col] > thresh) {
for (row=0 ; row<rows ; row++)
sum += mat[row*cols+col] * b[row] ;
sum /= w[col] ;
}
work[col] = sum ;
}
/*
Multiply by V
*/
for (col=0 ; col<cols ; col++) {
sum = 0.0 ;
for (cc=0 ; cc<cols ; cc++)
sum += v[col*cols+cc] * work[cc] ;
x[col] = sum ;
}
}