Debian Codegraph @

dwsys/SVD.h

raw
1 #ifndef _SVD_h_
2 #define _SVD_h_
3 /* SVD.h
4 *
5 * Copyright (C) 1994-2024 David Weenink
6 *
7 * This code is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This code is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this work. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /*
22 djmw 20020423 GPL header
23 djmw 20120808 Latest modification.
24 */
25
26 #include "NUM2.h"
27 #include "Data.h"
28
29 #include "SVD_def.h"
30
31 /*
32 The SVD represents the singular value decomposition of a real matrix A of size nrow x ncol.
33 If nrow >= ncol then
34 A = U D V',
35 where U is an orthogonal nrow x ncol matrix, V is an orthogonal ncol x ncol matrix,
36 and D is a diagonal matrix with diagonal elements d[1]...d[ncol] and V' means the transpose of V.
37 Invariants:
38 U'U = V'V = I (identity matrix of size ncol x ncol
39 d[1] >= d[2] >=... >= d[ncol] >= 0.
40 For ncol > nrow we use the transposed system.
41 */
42 void SVD_init (SVD me, integer numberOfRows, integer numberOfColumns);
43
44
45 autoSVD SVD_create (integer numberOfRows, integer numberOfColumns);
46 /*
47 my tolerance = eps * MAX (numberOfRows, numberOfColumns)
48 where eps is the floating point precision, approximately 2.2e-16
49 */
50
51 void SVD_resizeWithinOldBounds (SVD me, integer nrowmax, integer ncolmax, integer nrownew, integer ncolnew);
52
53 autoSVD SVD_createFromGeneralMatrix (constMATVU const& m);
54
55 void SVD_update (SVD me, constMATVU const& m);
56 /*
57 Perform SVD analysis on matrix M, i.e., decompose M as M = UDV'.
58 Watch out: dataType contains V, not V' !!
59 */
60
61 void SVD_compute (SVD me);
62
63 /* Solve Ax = b */
64 void SVD_solve_preallocated (SVD me, constVECVU const& b, VECVU const& result);
65 autoVEC SVD_solve (SVD me, constVECVU const& b);
66
67 /* Solve A*X = B */
68 void SVD_solve_preallocated (SVD me, constMATVU const& b, MATVU const& result);
69 autoMAT SVD_solve (SVD me, constMATVU const& b);
70
71 void SVD_sort (SVD me);
72 /*
73 Sort singular values (and corresponding column vectors of U and V) in decreasing order.
74 */
75
76 void SVD_setTolerance (SVD me, double tolerance);
77
78 double SVD_getTolerance (SVD me);
79
80 double SVD_getConditionNumber (SVD me);
81
82 double SVD_getSumOfSingularValuesAsFractionOfTotal (SVD me, integer from, integer to);
83
84 integer SVD_getMinimumNumberOfSingularValues (SVD me, double fractionOfSumOfSingularValues);
85
86 double SVD_getSumOfSingularValues (SVD me, integer from, integer to);
87
88 integer SVD_zeroSmallSingularValues (SVD me, double tolerance);
89 /*
90 Zero singular values smaller than maximum_singular_value * tolerance
91 If tolerance == 0 then then my tolerance will be used.
92 Return the number of s.v.'s zeroed.
93 */
94
95 autoMAT SVD_synthesize (SVD me, integer sv_from, integer sv_to);
96 /*
97 Synthesize matrix as U D(sv_from:sv_to) V'.
98 (The synthesized matrix is an approximation of the svd'ed matrix if
99 only a selected number of sv's is used).
100 */
101
102 autoMAT SVD_getSquared (SVD me, bool inverse);
103 void SVD_getSquared_preallocated (SVD me, bool inverse, MAT const& m);
104 // compute V D^2 V' or V D^-2 V'
105
106 integer SVD_getRank (SVD me);
107
108 double SVD_getEffectiveDegreesOfFreedom (SVD me, double shrinkageParameter);
109 double SVD_getShrinkageParameter (SVD me, double effectiveDegreesOfFreedom);
110 /*
111 For ridge regression:
112 The effective degrees of freedom df(lambda) is defined as:
113 df(lambda) = sum (i=1, p, d[i]^2 / (d[i]^2 + lambda)),
114 where d[i] are the singular values and p is the number of singular values.
115 df(0) == p and df(infinity) == 0.
116 The equation to solve for lambda is
117 f(lambda) = sum(i=1, p, d[i]^2 / (d[i]^2 + lambda)) - df = 0,
118 */
119
120 integer SVD_getWorkspaceSize (SVD me);
121
122 autoGSVD GSVD_create (integer numberOfColumns);
123
124 autoGSVD GSVD_create (constMATVU const& m1, constMATVU const& m2);
125
126 void GSVD_setTolerance (GSVD me, double tolerance);
127
128 double GSVD_getTolerance (GSVD me);
129
130 /**** with workspace for multithreading *********************/
131
132 integer SVD_getWorkspaceSize (SVD me);
133
134 void SVD_compute (SVD me, VEC const& workspace);
135 // workspace size should be computed by SVD_getWorkspaceSize
136
137 void SVD_solve_preallocated (SVD me, constVECVU const& b, VECVU const& result, VEC const& workspace);
138 // workspace size >= svd->numberOfColumns
139
140
141 #endif /* _SVD_h_ */