| 1 | #cxx . . $ `<file>/dwsys/SVD.h`/ifndef _SVD_h_ |
| 2 | #define _SVD_h_cxx . . $ `dwsys/SVD.h:2:9`! |
| 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"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 27 | #include "Data.h"cxx . . $ `<file>/sys/Data.h`/ |
| 28 | |
| 29 | #include "SVD_def.h"cxx . . $ `<file>/dwsys/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_initcxx . . $ SVD_init(7c022d513d3b71df). (SVDcxx . . $ SVD# me, integercxx . . $ integer# numberOfRows, integercxx . . $ integer# numberOfColumns); |
| 43 | |
| 44 | |
| 45 | autoSVDcxx . . $ autoSVD# SVD_createcxx . . $ SVD_create(1ec6f5698ce1ada6). (integercxx . . $ integer# numberOfRows, integercxx . . $ 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_resizeWithinOldBoundscxx . . $ SVD_resizeWithinOldBounds(fd3486631380876d). (SVDcxx . . $ SVD# me, integercxx . . $ integer# nrowmax, integercxx . . $ integer# ncolmax, integercxx . . $ integer# nrownew, integercxx . . $ integer# ncolnew); |
| 52 | |
| 53 | autoSVDcxx . . $ autoSVD# SVD_createFromGeneralMatrixcxx . . $ SVD_createFromGeneralMatrix(35acab982e3981c8). (constMATVUcxx . . $ constMATVU# const& m); |
| 54 | |
| 55 | void SVD_updatecxx . . $ SVD_update(595abe6963532d23). (SVDcxx . . $ SVD# me, constMATVUcxx . . $ 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_computecxx . . $ SVD_compute(bfdf92b97e18fef0). (SVDcxx . . $ SVD# me); |
| 62 | |
| 63 | /* Solve Ax = b */ |
| 64 | void SVD_solve_preallocatedcxx . . $ SVD_solve_preallocated(419542b4bf1262f0). (SVDcxx . . $ SVD# me, constVECVUcxx . . $ constVECVU# const& b, VECVUcxx . . $ VECVU# const& result); |
| 65 | autoVECcxx . . $ autoVEC# SVD_solvecxx . . $ SVD_solve(8ead2e281a66fa69). (SVDcxx . . $ SVD# me, constVECVUcxx . . $ constVECVU# const& b); |
| 66 | |
| 67 | /* Solve A*X = B */ |
| 68 | void SVD_solve_preallocatedcxx . . $ SVD_solve_preallocated(de21905c2ea937a3). (SVDcxx . . $ SVD# me, constMATVUcxx . . $ constMATVU# const& b, MATVUcxx . . $ MATVU# const& result); |
| 69 | autoMATcxx . . $ autoMAT# SVD_solvecxx . . $ SVD_solve(289b6d8a58551a59). (SVDcxx . . $ SVD# me, constMATVUcxx . . $ constMATVU# const& b); |
| 70 | |
| 71 | void SVD_sortcxx . . $ SVD_sort(bfdf92b97e18fef0). (SVDcxx . . $ SVD# me); |
| 72 | /* |
| 73 | Sort singular values (and corresponding column vectors of U and V) in decreasing order. |
| 74 | */ |
| 75 | |
| 76 | void SVD_setTolerancecxx . . $ SVD_setTolerance(e291c81952ef8ef2). (SVDcxx . . $ SVD# me, double tolerance); |
| 77 | |
| 78 | double SVD_getTolerancecxx . . $ SVD_getTolerance(490b0b34a90f4cec). (SVDcxx . . $ SVD# me); |
| 79 | |
| 80 | double SVD_getConditionNumbercxx . . $ SVD_getConditionNumber(490b0b34a90f4cec). (SVDcxx . . $ SVD# me); |
| 81 | |
| 82 | double SVD_getSumOfSingularValuesAsFractionOfTotalcxx . . $ SVD_getSumOfSingularValuesAsFractionOfTotal(7497dfd703b4e111). (SVDcxx . . $ SVD# me, integercxx . . $ integer# from, integercxx . . $ integer# to); |
| 83 | |
| 84 | integercxx . . $ integer# SVD_getMinimumNumberOfSingularValuescxx . . $ SVD_getMinimumNumberOfSingularValues(3c6b93a29a648e9a). (SVDcxx . . $ SVD# me, double fractionOfSumOfSingularValues); |
| 85 | |
| 86 | double SVD_getSumOfSingularValuescxx . . $ SVD_getSumOfSingularValues(7497dfd703b4e111). (SVDcxx . . $ SVD# me, integercxx . . $ integer# from, integercxx . . $ integer# to); |
| 87 | |
| 88 | integercxx . . $ integer# SVD_zeroSmallSingularValuescxx . . $ SVD_zeroSmallSingularValues(3c6b93a29a648e9a). (SVDcxx . . $ 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 | autoMATcxx . . $ autoMAT# SVD_synthesizecxx . . $ SVD_synthesize(b3cfff2ecdde7129). (SVDcxx . . $ SVD# me, integercxx . . $ integer# sv_from, integercxx . . $ 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 | autoMATcxx . . $ autoMAT# SVD_getSquaredcxx . . $ SVD_getSquared(594dd67443f775d5). (SVDcxx . . $ SVD# me, bool inverse); |
| 103 | void SVD_getSquared_preallocatedcxx . . $ SVD_getSquared_preallocated(cbf79aaec0a83e3). (SVDcxx . . $ SVD# me, bool inverse, MATcxx . . $ MAT# const& m); |
| 104 | // compute V D^2 V' or V D^-2 V' |
| 105 | |
| 106 | integercxx . . $ integer# SVD_getRankcxx . . $ SVD_getRank(c9b8894ea6dfb8fa). (SVDcxx . . $ SVD# me); |
| 107 | |
| 108 | double SVD_getEffectiveDegreesOfFreedomcxx . . $ SVD_getEffectiveDegreesOfFreedom(68c214eb547f9476). (SVDcxx . . $ SVD# me, double shrinkageParameter); |
| 109 | double SVD_getShrinkageParametercxx . . $ SVD_getShrinkageParameter(68c214eb547f9476). (SVDcxx . . $ 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 | integercxx . . $ integer# SVD_getWorkspaceSizecxx . . $ SVD_getWorkspaceSize(c9b8894ea6dfb8fa). (SVDcxx . . $ SVD# me); |
| 121 | |
| 122 | autoGSVDcxx . . $ autoGSVD# GSVD_createcxx . . $ GSVD_create(60480306e302261a). (integercxx . . $ integer# numberOfColumns); |
| 123 | |
| 124 | autoGSVDcxx . . $ autoGSVD# GSVD_createcxx . . $ GSVD_create(4bbc5f65e0ebf193). (constMATVUcxx . . $ constMATVU# const& m1, constMATVUcxx . . $ constMATVU# const& m2); |
| 125 | |
| 126 | void GSVD_setTolerancecxx . . $ GSVD_setTolerance(2e750110fcb6cdc4). (GSVDcxx . . $ GSVD# me, double tolerance); |
| 127 | |
| 128 | double GSVD_getTolerancecxx . . $ GSVD_getTolerance(3d8f580dbd6ff0b8). (GSVDcxx . . $ GSVD# me); |
| 129 | |
| 130 | /**** with workspace for multithreading *********************/ |
| 131 | |
| 132 | integercxx . . $ integer# SVD_getWorkspaceSizecxx . . $ SVD_getWorkspaceSize(c9b8894ea6dfb8fa). (SVDcxx . . $ SVD# me); |
| 133 | |
| 134 | void SVD_computecxx . . $ SVD_compute(ddb1bcafe9937c37). (SVDcxx . . $ SVD# me, VECcxx . . $ VEC# const& workspace); |
| 135 | // workspace size should be computed by SVD_getWorkspaceSize |
| 136 | |
| 137 | void SVD_solve_preallocatedcxx . . $ SVD_solve_preallocated(a7076b13da87f7b9). (SVDcxx . . $ SVD# me, constVECVUcxx . . $ constVECVU# const& b, VECVUcxx . . $ VECVU# const& result, VECcxx . . $ VEC# const& workspace); |
| 138 | // workspace size >= svd->numberOfColumns |
| 139 | |
| 140 | |
| 141 | #endif /* _SVD_h_ */ |