| 1 | /* SVD.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2024 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 20010719 |
| 21 | */ |
| 22 | |
| 23 | #include "SVD.h" |
| 24 | #include <algorithm> |
| 25 | #include "NUMmachar.h" |
| 26 | #include "Collection.h" |
| 27 | #include "../melder/melder.h" |
| 28 | #include "NUMlapack.h" |
| 29 | #include "NUM2.h" |
| 30 | |
| 31 | #include "oo_DESTROY.h" |
| 32 | #include "SVD_def.h" |
| 33 | #include "oo_COPY.h" |
| 34 | #include "SVD_def.h" |
| 35 | #include "oo_EQUAL.h" |
| 36 | #include "SVD_def.h" |
| 37 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 38 | #include "SVD_def.h" |
| 39 | #include "oo_WRITE_TEXT.h" |
| 40 | #include "SVD_def.h" |
| 41 | #include "oo_WRITE_BINARY.h" |
| 42 | #include "SVD_def.h" |
| 43 | #include "oo_READ_TEXT.h" |
| 44 | #include "SVD_def.h" |
| 45 | #include "oo_READ_BINARY.h" |
| 46 | #include "SVD_def.h" |
| 47 | #include "oo_DESCRIPTION.h" |
| 48 | #include "SVD_def.h" |
| 49 | |
| 50 | void structSVD :: v1_info () { |
| 51 | // skipping parent classes |
| 52 | MelderInfo_writeLine (U"Number of rows: ", numberOfRows); |
| 53 | MelderInfo_writeLine (U"Number of columns: ", numberOfColumns); |
| 54 | MelderInfo_writeLine (U"This matrix is", (isTransposed ? U"" : U" not "), U" transposed."); |
| 55 | } |
| 56 | |
| 57 | Thing_implement (SVD, Daata, 1); |
| 58 | |
| 59 | /* |
| 60 | m >=n, mxn matrix A has svd UDV', where u is mxn, D is n and V is nxn. |
| 61 | m < n, then transpose A. Consider A' with svd (UDV')'= VDU', where v is mxm, D is m and U' is mxn |
| 62 | */ |
| 63 | void SVD_init (SVD me, integer numberOfRows, integer numberOfColumns) { |
| 64 | if (numberOfRows < numberOfColumns) { |
| 65 | my isTransposed = true; |
| 66 | std::swap (numberOfRows, numberOfColumns); |
| 67 | } |
| 68 | my numberOfRows = numberOfRows; |
| 69 | my numberOfColumns = numberOfColumns; |
| 70 | if (! NUMfpp) |
| 71 | NUMmachar (); |
| 72 | my tolerance = NUMfpp -> eps * numberOfRows; |
| 73 | my u = zero_MAT (numberOfRows, numberOfColumns); |
| 74 | my v = zero_MAT (numberOfColumns, numberOfColumns); |
| 75 | my d = zero_VEC (numberOfColumns); |
| 76 | } |
| 77 | |
| 78 | autoSVD SVD_create (integer numberOfRows, integer numberOfColumns) { |
| 79 | try { |
| 80 | autoSVD me = Thing_new (SVD); |
| 81 | SVD_init (me.get(), numberOfRows, numberOfColumns); |
| 82 | return me; |
| 83 | } catch (MelderError) { |
| 84 | Melder_throw (U"SVD not created."); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void SVD_resizeWithinOldBounds (SVD me, integer nrowmax, integer ncolmax, integer nrownew, integer ncolnew) { |
| 89 | /* |
| 90 | An automatrix does not have a _capacity variable, only nrow and ncol values. Therefoe we cannot check whether |
| 91 | nrowmax and ncolmax are valid. |
| 92 | We can only check if ncolmax confirms with the size of the singular values array |
| 93 | */ |
| 94 | Melder_require (nrowmax >= ncolmax, |
| 95 | U"The number of rows should not be less than the number of columns."); // for the time being |
| 96 | Melder_assert (ncolmax <= my d._capacity); |
| 97 | Melder_require (nrownew <= nrowmax && ncolnew <= ncolmax, |
| 98 | U"The new size must fit within the old size."); |
| 99 | my numberOfRows = nrownew; |
| 100 | my numberOfColumns = ncolnew; |
| 101 | my u.resize (nrownew, ncolnew); |
| 102 | my v.resize (ncolnew, ncolnew); |
| 103 | my d.resize (ncolnew); |
| 104 | } |
| 105 | |
| 106 | autoSVD SVD_createFromGeneralMatrix (constMATVU const& m) { |
| 107 | try { |
| 108 | autoSVD me = SVD_create (m.nrow, m.ncol); |
| 109 | my u.all() <<= ( my isTransposed ? m.transpose() : m ); |
| 110 | SVD_compute (me.get()); |
| 111 | return me; |
| 112 | } catch (MelderError) { |
| 113 | Melder_throw (U"SVD not created from general matrix."); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void SVD_update (SVD me, constMATVU const& m) { |
| 118 | Melder_assert ((! my isTransposed && my numberOfRows == m.nrow && my numberOfColumns == m.ncol) || |
| 119 | (my isTransposed && my numberOfRows == m.ncol && my numberOfColumns == m.nrow)); |
| 120 | my u.all() <<= ( my isTransposed ? m.transpose() : m ); |
| 121 | SVD_compute (me); |
| 122 | } |
| 123 | |
| 124 | void SVD_setTolerance (SVD me, double tolerance) { |
| 125 | my tolerance = tolerance; |
| 126 | } |
| 127 | |
| 128 | double SVD_getTolerance (SVD me) { |
| 129 | return my tolerance; |
| 130 | } |
| 131 | |
| 132 | integer SVD_getWorkspaceSize (SVD me) { |
| 133 | /* |
| 134 | To compute the SVD |
| 135 | */ |
| 136 | double wtmp; |
| 137 | integer lwork = -1, info; |
| 138 | NUMlapack_dgesvd_ ("S", "O", my numberOfColumns, my numberOfRows, & my u [1] [1], my numberOfColumns, & my d [1], |
| 139 | & my v [1] [1], my numberOfColumns, nullptr, my numberOfColumns, & wtmp, lwork, & info); |
| 140 | Melder_require (info == 0, |
| 141 | U"NUMlapack_dgesvd_ query returns error ", info, U"."); |
| 142 | return Melder_roundUp (wtmp); |
| 143 | } |
| 144 | |
| 145 | void SVD_compute (SVD me) { |
| 146 | try { |
| 147 | integer info; |
| 148 | const integer lwork = SVD_getWorkspaceSize (me); |
| 149 | autoVEC work = raw_VEC (lwork); |
| 150 | NUMlapack_dgesvd_ ("S", "O", my numberOfColumns, my numberOfRows, & my u [1] [1], my numberOfColumns, |
| 151 | & my d [1], & my v [1] [1], my numberOfColumns, nullptr, my numberOfColumns, & work [1], lwork, & info); |
| 152 | Melder_require (info == 0, |
| 153 | U"NUMlapack_dgesvd_ returns error ", info, U"."); |
| 154 | /* |
| 155 | Because we store the eigenvectors row-wise, they must be transposed |
| 156 | */ |
| 157 | transpose_mustBeSquare_MAT_inout (my v.get()); |
| 158 | } catch (MelderError) { |
| 159 | Melder_throw (me, U": SVD could not be computed."); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void SVD_compute (SVD me, VEC const& workspace) { |
| 164 | try { |
| 165 | integer info; |
| 166 | const integer lwork = SVD_getWorkspaceSize (me); |
| 167 | Melder_require (lwork <= workspace.size, |
| 168 | U"Your workspace size, ", workspace.size, U", is too small (required ", lwork, U")."); |
| 169 | NUMlapack_dgesvd_ ("S", "O", my numberOfColumns, my numberOfColumns, & my u [1] [1], my numberOfColumns, |
| 170 | & my d [1], & my v [1] [1], my numberOfColumns, nullptr, my numberOfColumns, & workspace [1], lwork, & info); |
| 171 | Melder_require (info == 0, |
| 172 | U"NUMlapack_dgesvd_ returns error ", info, U"."); |
| 173 | /* |
| 174 | Because we store the eigenvectors row-wise, they must be transposed |
| 175 | */ |
| 176 | transpose_mustBeSquare_MAT_inout (my v.get()); |
| 177 | } catch (MelderError) { |
| 178 | Melder_throw (me, U": SVD could not be computed."); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | // V D^2 V'or V D^-2 V |
| 184 | void SVD_getSquared_preallocated (SVD me, bool inverse, MAT const& m) { |
| 185 | Melder_assert (m.nrow == m.ncol && m.ncol == my numberOfColumns); |
| 186 | for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 187 | for (integer j = i; j <= my numberOfColumns; j ++) { |
| 188 | longdouble val = 0.0; |
| 189 | for (integer k = 1; k <= my numberOfColumns; k ++) { |
| 190 | if (my d [k] > 0.0) { |
| 191 | const longdouble dsq = my d [k] * my d [k]; |
| 192 | const longdouble factor = ( inverse ? 1.0 / dsq : dsq ); |
| 193 | val += my v [i] [k] * my v [j] [k] * factor; |
| 194 | } |
| 195 | } |
| 196 | m [i] [j] = m [j] [i] = double (val); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | autoMAT SVD_getSquared (SVD me, bool inverse) { |
| 202 | autoMAT result = raw_MAT (my numberOfColumns, my numberOfColumns); |
| 203 | SVD_getSquared_preallocated (me, inverse, result.get()); |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | void SVD_solve_preallocated (SVD me, constVECVU const& b, VECVU const& result, VEC const& workspace) { |
| 208 | try { |
| 209 | /* |
| 210 | Solve UDV' x = b. |
| 211 | Solution: x = V D^-1 U' b |
| 212 | */ |
| 213 | Melder_assert (workspace.size >= my numberOfColumns); |
| 214 | VEC work = workspace.part (1, my numberOfColumns); |
| 215 | work <<= 0.0; |
| 216 | if (! my isTransposed) { |
| 217 | Melder_assert (my numberOfRows == b.size); |
| 218 | Melder_assert (result.size == my numberOfColumns); |
| 219 | for (integer j = 1; j <= my numberOfColumns; j ++) |
| 220 | if (my d [j] > 0.0) |
| 221 | work [j] = NUMinner (my u.column (j), b) / my d [j]; |
| 222 | mul_VEC_out (result, my v.get(), work); |
| 223 | } else { |
| 224 | /* |
| 225 | Solve (UDV')' x = b or VDU' x = b. |
| 226 | Solution: x = UD^(-1)V'b |
| 227 | */ |
| 228 | Melder_assert (my numberOfColumns == b.size); |
| 229 | Melder_assert (result.size == my numberOfRows); |
| 230 | for (integer i = 1; i <= my numberOfColumns; i ++) |
| 231 | if (my d [i] > 0.0) |
| 232 | work [i] = NUMinner (my v.column (i), b) / my d [i]; |
| 233 | for (integer i = 1; i <= my numberOfColumns; i ++) |
| 234 | result [i] = NUMinner (my u.row (i), work); |
| 235 | } |
| 236 | } catch (MelderError) { |
| 237 | Melder_throw (me, U": not solved."); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | void SVD_solve_preallocated (SVD me, constVECVU const& b, VECVU const& result) { |
| 242 | autoVEC workspace = raw_VEC (my numberOfColumns); |
| 243 | SVD_solve_preallocated (me, b, result, workspace.get()); |
| 244 | } |
| 245 | |
| 246 | autoVEC SVD_solve (SVD me, constVECVU const& b) { |
| 247 | integer numberOfRows = my numberOfRows, numberOfColumns = my numberOfColumns; |
| 248 | if (my isTransposed) |
| 249 | std::swap (numberOfRows, numberOfColumns); |
| 250 | Melder_assert (numberOfRows == b.size); |
| 251 | autoVEC result = zero_VEC (numberOfColumns); |
| 252 | SVD_solve_preallocated (me, b, result.get()); |
| 253 | return result; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | Solve UDV' X = B. |
| 258 | */ |
| 259 | void SVD_solve_preallocated (SVD me, constMATVU const& b, MATVU const& result) { |
| 260 | Melder_assert (b.nrow == my numberOfRows && b.ncol == result.ncol); |
| 261 | Melder_assert (result.nrow == my numberOfColumns); |
| 262 | autoVEC bcol = raw_VEC (b.nrow); |
| 263 | autoVEC resultcol = raw_VEC (result.nrow); |
| 264 | for (integer icol = 1; icol <= b.ncol; icol ++) { |
| 265 | bcol.all() <<= b.column (icol); |
| 266 | SVD_solve_preallocated (me, bcol.get(), resultcol.get()); |
| 267 | result.column (icol) <<= resultcol.get(); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | integer SVD_getMinimumNumberOfSingularValues (SVD me, double fractionOfSumOfSingularValues) { |
| 272 | const double sumOfSingularValues = SVD_getSumOfSingularValues (me, 1, my numberOfColumns); |
| 273 | const double criterion = sumOfSingularValues * fractionOfSumOfSingularValues; |
| 274 | integer j = 1; |
| 275 | longdouble sum = my d [1]; |
| 276 | while (sum < criterion && j < my numberOfColumns) { |
| 277 | sum += my d [++ j]; |
| 278 | } |
| 279 | return j; |
| 280 | } |
| 281 | |
| 282 | void SVD_sort (SVD me) { // Superfluous??, SVD is always sorted |
| 283 | try { |
| 284 | autoSVD thee = Data_copy (me); |
| 285 | autoINTVEC index = newINTVECindex (my d.get()); |
| 286 | |
| 287 | for (integer j = 1; j <= my numberOfColumns; j ++) { |
| 288 | const integer from = index [my numberOfColumns - j + 1]; |
| 289 | my d [j] = thy d [from]; |
| 290 | for (integer i = 1; i <= my numberOfRows; i ++) |
| 291 | my u [i] [j] = thy u [i] [from]; |
| 292 | for (integer i = 1; i <= my numberOfColumns; i ++) |
| 293 | my v [i] [j] = thy v [i] [from]; |
| 294 | } |
| 295 | } catch (MelderError) { |
| 296 | Melder_throw (me, U": not sorted."); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | double SVD_getConditionNumber (SVD me) { |
| 301 | return my d [my numberOfColumns] > 0.0 ? my d [1] / my d [my numberOfColumns] : undefined; |
| 302 | } |
| 303 | |
| 304 | double SVD_getSumOfSingularValuesAsFractionOfTotal (SVD me, integer from, integer to) { |
| 305 | const double part = SVD_getSumOfSingularValues (me, from, to); |
| 306 | const double total = SVD_getSumOfSingularValues (me, 1, my numberOfColumns); |
| 307 | return part / total; |
| 308 | } |
| 309 | |
| 310 | double SVD_getSumOfSingularValues (SVD me, integer from, integer to) { |
| 311 | from = ( from == 0 ? 1 : from ); |
| 312 | to = ( to == 0 ? my numberOfColumns : to ); |
| 313 | Melder_require (from > 0 && from <= to && to <= my numberOfColumns, |
| 314 | U"The range should be within [1,", my numberOfColumns, U"]."); |
| 315 | return NUMsum (my d.part (from, to)); |
| 316 | } |
| 317 | |
| 318 | integer SVD_zeroSmallSingularValues (SVD me, double tolerance) { |
| 319 | if (tolerance == 0.0) |
| 320 | tolerance = my tolerance; |
| 321 | |
| 322 | const double dmax = my d [1]; // invariant |
| 323 | |
| 324 | integer numberOfZeroed = 0; |
| 325 | for (integer i = 2; i <= my numberOfColumns; i ++) |
| 326 | if (my d [i] < dmax * tolerance) { |
| 327 | my d [i] = 0.0; |
| 328 | numberOfZeroed ++; |
| 329 | } |
| 330 | return numberOfZeroed; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | integer SVD_getRank (SVD me) { |
| 335 | integer rank = 1; |
| 336 | const double dmax = my d [1]; |
| 337 | for (integer i = 2; i <= my numberOfColumns; i ++) |
| 338 | if (my d [i] >= dmax * my tolerance) |
| 339 | rank ++; |
| 340 | return rank; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | SVD of A = U D V'. |
| 345 | This can be written as A = sum_{r=1}^n d [i] u [i] v [i]', where u [i] and v [i] are columnvectors |
| 346 | (Golub & van Loan, 3rd ed, p 71). |
| 347 | If (internally) the matrix was transposed we can rewrite this as A=sum_{r=1}^n d [i] u [i]' v [i]. |
| 348 | */ |
| 349 | autoMAT SVD_synthesize (SVD me, integer sv_from, integer sv_to) { |
| 350 | if (sv_to == 0) |
| 351 | sv_to = my numberOfColumns; |
| 352 | try { |
| 353 | Melder_require (sv_from > 0 && sv_from <= sv_to && sv_to <= my numberOfColumns, |
| 354 | U"Indices must be in range [1, ", my numberOfColumns, U"]."); |
| 355 | long nrow = my numberOfRows; |
| 356 | long ncol = my numberOfColumns; |
| 357 | if (my isTransposed) |
| 358 | std::swap (nrow, ncol); |
| 359 | autoMAT result = zero_MAT (nrow, ncol); |
| 360 | autoMAT outer = zero_MAT (nrow, ncol); |
| 361 | |
| 362 | for (integer k = sv_from; k <= sv_to; k ++) { |
| 363 | if (my isTransposed) |
| 364 | outer_MAT_out (outer.get(), my v.column (k), my u.column (k)); |
| 365 | else |
| 366 | outer_MAT_out (outer.get(), my u.column (k), my v.row (k)); // because the transposed of v is in the svd! |
| 367 | result.get() += outer.get() * my d [k]; |
| 368 | } |
| 369 | return result; |
| 370 | } catch (MelderError) { |
| 371 | Melder_throw (me, U": no synthesis."); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | The effective degrees of freedom df(lambda) is defined as: |
| 377 | df(lambda) = sum (i=1, p, d[i]^2 / (d[i]^2 + lambda)), |
| 378 | where p is the number of singular values. |
| 379 | df(0) == p and df(infinity) == 0. |
| 380 | The equation to solve for lambda is |
| 381 | f(lambda) = sum(i=1, p, d[i]^2 / (d[i]^2 + lambda)) - df = 0, |
| 382 | with derivative df/dlambda = - sum(i=1, p, d[i]^2 / (d[i]^2 + lambda)^2). |
| 383 | Taylor series around lambda[0] gives |
| 384 | f(lambda)=f[lambda [0])+(lambda -lambda [0]) df/dlambda (lambda [0]) |
| 385 | Solving for lambda gives: |
| 386 | lambda = lambda [0] - 1 / {df/dlambda (lambda [0])} * f(lambda [0]) |
| 387 | This gives iteration |
| 388 | lambda [j+1] = lambda [j] + 1 / {df/dlambda (lambda [j])} * f(lambda [j]). |
| 389 | As starting value for lambda [0] we choose (p - df)/df. |
| 390 | */ |
| 391 | double SVD_getShrinkageParameter (SVD me, double effectiveDegreesOfFreedom) { |
| 392 | const integer rank = SVD_getRank (me); |
| 393 | if (effectiveDegreesOfFreedom >= rank) |
| 394 | return 0.0; |
| 395 | if (effectiveDegreesOfFreedom <= 0.0) |
| 396 | return 1.0e38; // infinity or a very big number |
| 397 | if (my isTransposed) |
| 398 | return undefined; |
| 399 | const double eps = 1e-6; // a little bit ad hoc number. |
| 400 | double lambda = (my numberOfColumns - effectiveDegreesOfFreedom) / effectiveDegreesOfFreedom; |
| 401 | double diff = 1.0e38; |
| 402 | do { |
| 403 | longdouble f = -effectiveDegreesOfFreedom, df = 0.0; |
| 404 | for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 405 | const longdouble dsq = my d [i] * my d [i]; |
| 406 | const longdouble dsqpl = dsq + lambda; |
| 407 | f += dsq / dsqpl; |
| 408 | df += dsq / (dsqpl * dsqpl); // always > 0 |
| 409 | } |
| 410 | const double lambda_new = lambda + double (f / df); |
| 411 | diff = abs (lambda - lambda_new); |
| 412 | lambda = lambda_new; |
| 413 | } while (diff > eps); |
| 414 | return lambda; |
| 415 | } |
| 416 | |
| 417 | double SVD_getEffectiveDegreesOfFreedom (SVD me, double shrinkageParameter) { |
| 418 | longdouble edf = 0.0; |
| 419 | for (integer i = 1; i <= my numberOfColumns; i ++) { |
| 420 | longdouble dsq = my d [i] * my d [i]; |
| 421 | edf += dsq / (dsq + shrinkageParameter); |
| 422 | } |
| 423 | return (double) edf; |
| 424 | } |
| 425 | |
| 426 | Thing_implement (GSVD, Daata, 0); |
| 427 | |
| 428 | void structGSVD :: v1_info () { |
| 429 | MelderInfo_writeLine (U"Number of columns: ", numberOfColumns); |
| 430 | } |
| 431 | |
| 432 | autoGSVD GSVD_create (integer numberOfColumns) { |
| 433 | try { |
| 434 | autoGSVD me = Thing_new (GSVD); |
| 435 | my numberOfColumns = numberOfColumns; |
| 436 | |
| 437 | my q = zero_MAT (numberOfColumns, numberOfColumns); |
| 438 | my r = zero_MAT (numberOfColumns, numberOfColumns); |
| 439 | my d1 = zero_VEC (numberOfColumns); |
| 440 | my d2 = zero_VEC (numberOfColumns); |
| 441 | return me; |
| 442 | } catch (MelderError) { |
| 443 | Melder_throw (U"GSVD not created."); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | autoGSVD GSVD_create (constMATVU const& m1, constMATVU const& m2) { |
| 448 | try { |
| 449 | const integer m = m1.nrow, n = m1.ncol, p = m2.nrow; |
| 450 | |
| 451 | // Store the matrices a and b as column major! |
| 452 | autoMAT a = transpose_MAT (m1); |
| 453 | autoMAT b = transpose_MAT (m2); |
| 454 | autoMAT q = raw_MAT (n, n); |
| 455 | autoVEC alpha = raw_VEC (n); |
| 456 | autoVEC beta = raw_VEC (n); |
| 457 | integer lwork = std::max (std::max (3 * n, m), p) + n; |
| 458 | autoVEC work = raw_VEC (lwork); |
| 459 | autoINTVEC iwork = raw_INTVEC (n); |
| 460 | |
| 461 | integer k, l, info; |
| 462 | NUMlapack_dggsvd_ ("N", "N", "Q", m, n, p, & k, & l, |
| 463 | & a [1] [1], m, & b [1] [1], p, & alpha [1], & beta [1], nullptr, m, |
| 464 | nullptr, p, & q [1] [1], n, & work [1], & iwork [1], & info); |
| 465 | Melder_require (info == 0, |
| 466 | U"dggsvd failed with error = ", info); |
| 467 | |
| 468 | const integer kl = k + l; |
| 469 | autoGSVD me = GSVD_create (kl); |
| 470 | |
| 471 | for (integer i = 1; i <= kl; i ++) { |
| 472 | my d1 [i] = alpha [i]; |
| 473 | my d2 [i] = beta [i]; |
| 474 | } |
| 475 | |
| 476 | transpose_MAT_out (my q.get(), q.get()); |
| 477 | /* |
| 478 | Get R from a(1:k+l,n-k-l+1:n) |
| 479 | */ |
| 480 | const double *pr = & a [1] [1]; |
| 481 | for (integer i = 1; i <= kl; i ++) { |
| 482 | for (integer j = i; j <= kl; j ++) { |
| 483 | my r [i] [j] = pr [i - 1 + (n - kl + j - 1) * m]; /* from col-major */ |
| 484 | } |
| 485 | } |
| 486 | return me; |
| 487 | } catch (MelderError) { |
| 488 | Melder_throw (U"GSVD not created."); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | void GSVD_setTolerance (GSVD me, double tolerance) { |
| 493 | my tolerance = tolerance; |
| 494 | } |
| 495 | |
| 496 | double GSVD_getTolerance (GSVD me) { |
| 497 | return my tolerance; |
| 498 | } |
| 499 | |
| 500 | /* End of file SVD.c */ |