Debian Codegraph @

dwsys/NUM2.cpp

raw symbols SCIP
1 /* NUM2.cpp
2 *
3 * Copyright (C) 1993-2025 David Weenink, 2017-2026 Paul Boersma
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 3 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 20020819 GPL header
21 djmw 20020819 Split nonGLP part off.
22 djmw 20001109 Changed stop criteria in NUMsvdcmp and NUMtqli.
23 djmw 20020819 Split into GPL and nonGPL part.
24 djmw 20021008 Removed SVD_sort.
25 djmw 20030619 Removed calls to NRC svd-routines.
26 djmw 20030623 Removed tqli en tred calls.
27 djmw 20030703 Replaced NUMincompleteBeta with gsl_sf_beta_inc.
28 djmw 20030710 NUMminimize_brent now also returns the minimum function value.
29 djmw 20030731 NUMridders: better approximation for small d.
30 NUMinvFisherQ better approximation for p < 0.5
31 djmw 20030813 Added NUMmad and NUMstatistics_huber.
32 djmw 20030825 Replaced gsl_sf_beta_inc with NUMincompleteBeta
33 pb 20030828 Improvements for invFisherQ, ridders, studentP, studentQ,
34 invStudentQ, invChiSquareQ: modifications for 'undefined' return values.
35 djmw 20030830 Corrected a bug in NUMtriangularfilter_amplitude
36 djmw 20031111 Added NUMdmatrix_transpose, NUMdmatrix_printMatlabForm
37 djmw 20040105 Added NUMmahalanobisDistanceSquared_chi
38 djmw 20040303 Added NUMstring_containsPrintableCharacter.
39 djmw 20050406 NUMprocrutus->NUMprocrustes
40 djmw 20060319 NUMinverse_cholesky: calculation of determinant is made optional
41 djmw 20060517 Added NUMregexp_compile
42 djmw 20060518 Treat NULL string as empty string in strs_replace_regexp/literal. Don't accept empty search in replace_regexStr
43 djmw 20060626 Extra NULL argument for ExecRE.
44 djmw 20070302 NUMclipLineWithinRectangle
45 djmw 20070614 updated to version 1.30 of regular expressions.
46 djmw 20071022 Removed function NUMfvector_moment2.
47 djmw 20071201 Melder_warning<n>
48 djmw 20080110 Corrected some bugs in replace_regexStr
49 djmw 20080122 Bug in replace_regexStr
50 djmw 20080317 +NUMsinc
51 pb 20080410 FisherQ from gsl
52 djmw 20090630 NUMlogNormalP/Q from gsl
53 djmw 20090707 Rename NUMinverse_cholesky to NUMlowerCholeskyInverse,
54 +NUMcovarianceFromColumnCentredMatrix, +NUMmultivariateKurtosis
55 djmw 20100311 +NUMsolveQuadraticEquation
56 djmw 20100426 replace wcstok by Melder_wcstok
57 djmw 20101209 removed NUMwcscmp is Melder_wcscmp now
58 djmw 20110304 Thing_new
59 djmw 20111110 use autostringvector
60 */
61
62 #include "SVD.h"cxx . . $ `<file>/dwsys/SVD.h`/
63 #include "Eigen.h"cxx . . $ `<file>/dwsys/Eigen.h`/
64 #include "NUMlapack.h"cxx . . $ `<file>/dwsys/NUMlapack.h`/
65 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
66 #include "NUMmachar.h"cxx . . $ `<file>/dwsys/NUMmachar.h`/
67 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
68
69 #include "gsl_randist.h"cxx . . $ `<file>/external/gsl/gsl_randist.h`/
70
71 #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/
72 #include "gsl_sf_bessel.h"cxx . . $ `<file>/external/gsl/gsl_sf_bessel.h`/
73 #include "gsl_sf_gamma.h"cxx . . $ `<file>/external/gsl/gsl_sf_gamma.h`/
74 #include "gsl_sf_erf.h"cxx . . $ `<file>/external/gsl/gsl_sf_erf.h`/
75 #include "gsl_sf_trig.h"cxx . . $ `<file>/external/gsl/gsl_sf_trig.h`/
76 #include "gsl_poly.h"cxx . . $ `<file>/external/gsl/gsl_poly.h`/
77 #include "gsl_cdf.h"cxx . . $ `<file>/external/gsl/gsl_cdf.h`/
78
79 int NUMcompareTwoIntegerscxx . . $ NUMcompareTwoIntegers(b8efda0003921f3b). (integercxx . . $ integer#& a, integercxx . . $ integer#& b) {
80 return a < b ? -1 : a > b ? 1 : 0;
81 }
82
83 int NUMcompareTwoDoublescxx . . $ NUMcompareTwoDoubles(dbbea7f52f02e557). (double& a, double& b) {
84 return a < b ? -1 : a > b ? 1 : 0;
85 }
86
87 #define SIGNcxx . . $ `dwsys/NUM2.cpp:87:9`!(a,b) ((b < 0) ? -fabs(a) : fabs(a))
88
89 struct pdf1_structcxx . . $ pdf1_struct# {
90 double pcxx . . $ pdf1_struct#p.;
91 double dfcxx . . $ pdf1_struct#df.;
92 };
93 struct pdf2_structcxx . . $ pdf2_struct# {
94 double pcxx . . $ pdf2_struct#p.;
95 double df1cxx . . $ pdf2_struct#df1.;
96 double df2cxx . . $ pdf2_struct#df2.;
97 };
98
99 void MATprintMatlabFormcxx . . $ MATprintMatlabForm(3eca1ab4cb8e50b4). (constMATVUcxx . . $ constMATVU# const& m, conststring32cxx . . $ conststring32# name) {
100 constexpr integercxx . . $ integer# npc = 5;
101 const ldiv_tcxx . . $ ldiv_t# n = ldivcxx . . $ ldiv(c73433836d37e8a1). (m.ncolcxx . . $ constmatrixview#ncol., npc);
102
103 MelderInfo_opencxx . . $ MelderInfo_open(49f6e7a06ebc5aa8). ();
104 MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (name, U"= [");
105 for (integercxx . . $ integer# i = 1; i <= m.nrowcxx . . $ constmatrixview#nrow.; i ++) {
106 for (integercxx . . $ integer# j = 1; j <= n.quot; j ++) {
107 for (integercxx . . $ integer# k = 1; k <= npc; k ++)
108 MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (m [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).i] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).(j - 1) * npc + k], (k < npc ? U", " : U""));
109 MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (j < n.quot ? U",\n" : U"");
110 }
111
112 for (integercxx . . $ integer# k = 1; k <= n.rem; k ++)
113 MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (m [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).i] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).n.quot * npc + k], (k < n.rem ? U", " : U""));
114 MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (i < m.nrowcxx . . $ constmatrixview#nrow. ? U";\n" : U"];\n");
115 }
116 MelderInfo_closecxx . . $ MelderInfo_close(49f6e7a06ebc5aa8). ();
117 }
118
119 void VECsmoothByMovingAverage_preallocatedcxx . . $ VECsmoothByMovingAverage_preallocated(5e8ce1ad74af04af). (VECVUcxx . . $ VECVU# const& out, constVECVUcxx . . $ constVECVU# const& in, integercxx . . $ integer# window) {
120 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (out.size == in.size);
121 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (window > 0,
122 U"The averaging window should be larger than 0.");
123 for (integercxx . . $ integer# i = 1; i <= out.sizecxx . . $ vectorview#size.; i ++) {
124 integercxx . . $ integer# jfrom = i - window / 2, jto = i + window / 2;
125 if (window % 2 == 0)
126 jto --;
127 Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & jfrom);
128 Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& jto, out.sizecxx . . $ vectorview#size.);
129 out [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i] = NUMmeancxx . . $ NUMmean(4bb9c3e3a8dd5442). (in.partcxx . . $ constvectorview#part(27d925039583bea4). (jfrom, jto));
130 }
131 }
132
133 void VECsmooth_gaussiancxx . . $ VECsmooth_gaussian(f66dda4be53d10a1). (VECVUcxx . . $ VECVU# const& out, constVECVUcxx . . $ constVECVU# const& in, double sigma, NUMFourierTablecxx . . $ NUMFourierTable# fftTable) {
134 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (out.size == in.size,
135 U"The sizes of the input and output vectors should be equal.");
136 out <<=cxx . . $ `operator<<=`(adea042513329390). in;
137 VECsmooth_gaussian_inplacecxx . . $ VECsmooth_gaussian_inplace(2a239a383ab729f). (out, sigma, fftTable);
138 }
139
140 void VECsmooth_gaussian_inplacecxx . . $ VECsmooth_gaussian_inplace(2a239a383ab729f). (VECVUcxx . . $ VECVU# const& in_out, double sigma, NUMFourierTablecxx . . $ NUMFourierTable# fftTable) {
141 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (in_out.size <= fftTable -> n,
142 U"The dimension of the table should at least equal the length of the input vector.");
143 autoVECcxx . . $ autoVEC# smooth = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (fftTable -> ncxx . . $ structNUMFourierTable#n.);
144 smoothcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, in_out.sizecxx . . $ vectorview#size.) <<=cxx . . $ `operator<<=`(adea042513329390). in_outcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401).;
145 NUMfft_forwardcxx . . $ NUMfft_forward(aea41fd94d07eee). (fftTable, smooth.getcxx . . $ autovector#get(e8d1da7fe68a6585).());
146 /*
147 Low pass filter with a Gaussian.
148 The Fourier Transform of h(x)= (exp(-a.x^2) is
149 H(f) = sqrt (pi/a) exp (-pi^2.f^2/a).
150 A Gaussian g(x)=sqrt(a/pi)h(x), where a = 1/(2s^2)
151 The "frequency" region is divided in fftTable -> n steps,
152 where one "frequency" unit f equals 1 / fftTable -> n.
153 */
154 const double b = 2.0 * NUMpicxx . . $ NUMpi. * sigma * NUMpicxx . . $ NUMpi. * sigma ;
155 for (integercxx . . $ integer# k = 2; k <= (fftTable -> ncxx . . $ structNUMFourierTable#n. + 1) / 2; k ++) {
156 const double f = (k - 1) / (double) fftTable -> ncxx . . $ structNUMFourierTable#n.;
157 const double weight = expcxx . . $ exp(fee4f1d22bc8e617). (- b * f * f);
158 smooth [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k * 2 - 2] *= weight; // re
159 smooth [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k * 2 - 1] *= weight; // im
160 }
161 if (fftTable -> ncxx . . $ structNUMFourierTable#n. % 2 == 0)
162 smooth [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).fftTable -> ncxx . . $ structNUMFourierTable#n.] *= expcxx . . $ exp(fee4f1d22bc8e617). (- b * 1 / 2 * 1 / 2);
163 NUMfft_backwardcxx . . $ NUMfft_backward(aea41fd94d07eee). (fftTable, smooth.getcxx . . $ autovector#get(e8d1da7fe68a6585).());
164 /*
165 backwardFT (forwardFT (data)) = n * data;
166 */
167 const double scaleFacor = 1.0 / fftTable -> ncxx . . $ structNUMFourierTable#n.;
168 smoothcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, in_out.sizecxx . . $ vectorview#size.) *=cxx . . $ `operator*=`(c555d170d72ff256). scaleFacor;
169 in_out <<=cxx . . $ `operator<<=`(adea042513329390). smoothcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, in_out.sizecxx . . $ vectorview#size.);
170 }
171
172 void VECsmooth_gaussian_inplacecxx . . $ VECsmooth_gaussian_inplace(cb3d2a9455fe8b3e). (VECVUcxx . . $ VECVU# const& in_out, double sigma) {
173 const integercxx . . $ integer# nfft = Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (in_out.sizecxx . . $ vectorview#size.);
174 autoNUMFourierTablecxx . . $ autoNUMFourierTable# fftTable = NUMFourierTable_createcxx . . $ NUMFourierTable_create(10012c71e147318e). (nfft);
175 VECsmooth_gaussian_inplacecxx . . $ VECsmooth_gaussian_inplace(2a239a383ab729f). (in_out, sigma, fftTable.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
176 }
177
178 autoMATcxx . . $ autoMAT# MATcovarianceFromColumnCentredMatrixcxx . . $ MATcovarianceFromColumnCentredMatrix(34f350791c8ce91c). (constMATVUcxx . . $ constMATVU# const& x, integercxx . . $ integer# ndf) {
179 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (ndf >= 0 && x.nrow - ndf > 0,
180 U"Invalid arguments.");
181 autoMATcxx . . $ autoMAT# covar = mtm_MATcxx . . $ mtm_MAT(a392b09ce3fc3972). (x);
182 covar.allcxx . . $ automatrix#all(2114117ba8ed4224).() *=cxx . . $ `operator*=`(50b469ae42a42f64). 1.0 / (x.nrowcxx . . $ constmatrixview#nrow. - ndf);
183 return covarcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
184 }
185
186 static void MATweighRowscxx . . $ MATweighRows(88779db6ac139909). (MATVUcxx . . $ MATVU# const& x, constVECVUcxx . . $ constVECVU# const& y) {
187 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.nrow == y.size);
188 for (integercxx . . $ integer# irow = 1; irow <= x.nrowcxx . . $ matrixview#nrow.; irow ++)
189 x.rowcxx . . $ matrixview#row(a8b9553e85c2b1fc). (irow) *=cxx . . $ `operator*=`(c555d170d72ff256). y [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).irow];
190 }
191
192 void MATmtm_weighRowscxx . . $ MATmtm_weighRows(ea80f54d76faf70). (MATVUcxx . . $ MATVU# const& result, constMATVUcxx . . $ constMATVU# const& data, constVECVUcxx . . $ constVECVU# const& rowWeights) {
193 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (data.nrow == rowWeights.size);
194 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (data.ncol == result.ncol);
195 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (result.nrow == result.ncol);
196 result <<=cxx . . $ `operator<<=`(50b469ae42a42f64). 0.0;
197 if (true) {
198 autoMATcxx . . $ autoMAT# outer = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (result.ncolcxx . . $ matrixview#ncol., result.ncolcxx . . $ matrixview#ncol.);
199 for (integercxx . . $ integer# irow = 1; irow <= data.nrowcxx . . $ constmatrixview#nrow.; irow ++) {
200 outer_MAT_outcxx . . $ outer_MAT_out(2869a0b58d3aa0f4). (outer.allcxx . . $ automatrix#all(2114117ba8ed4224).(), data.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (irow), data.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (irow));
201 result +=cxx . . $ `operator+=`(b78e8a4998bd3572). outercxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..allcxx . . $ automatrix#all(2114117ba8ed4224).() *cxx . . $ `operator*`(3dd25102fdc24343). rowWeights [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).irow];
202 }
203 } else {
204 autoVECcxx . . $ autoVEC# w = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (rowWeights.sizecxx . . $ constvectorview#size.);
205 autoMATcxx . . $ autoMAT# d = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (data);
206 for (integercxx . . $ integer# irow = 1; irow <= w.sizecxx . . $ autovector#size.; irow ++)
207 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).irow] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (rowWeights [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).irow]);
208 MATweighRowscxx . . $ MATweighRows(88779db6ac139909). (dcxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
209 mtm_MAT_outcxx . . $ mtm_MAT_out(bcb62980f535259). (result, dcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
210 }
211 }
212
213 inline void MATmultiplyRows_inplacecxx . . $ MATmultiplyRows_inplace(88779db6ac139909). (MATVUcxx . . $ MATVU# const& x, constVECVUcxx . . $ constVECVU# const& v) {
214 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.nrow == v.size);
215 for (integercxx . . $ integer# irow = 1; irow <= x.nrowcxx . . $ matrixview#nrow.; irow ++)
216 x.rowcxx . . $ matrixview#row(a8b9553e85c2b1fc). (irow) *=cxx . . $ `operator*=`(c555d170d72ff256). v [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).irow]; // x [i,j] * v [i]
217 }
218 inline void MATmultiplyColumns_inplacecxx . . $ MATmultiplyColumns_inplace(88779db6ac139909). (MATVUcxx . . $ MATVU# const& x, constVECVUcxx . . $ constVECVU# const& v) {
219 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.ncol == v.size);
220 for (integercxx . . $ integer# icol = 1; icol <= x.nrowcxx . . $ matrixview#nrow.; icol ++)
221 x.columncxx . . $ matrixview#column(a8b9553e85c2b1fc). (icol) *=cxx . . $ `operator*=`(c555d170d72ff256). v [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol]; // x [i,j] * v [j]
222 }
223
224 double NUMmultivariateKurtosiscxx . . $ NUMmultivariateKurtosis(c140e5803c561bff). (constMATVUcxx . . $ constMATVU# const& m, integercxx . . $ integer# method) {
225 double kurt = undefinedcxx . . $ undefined.;
226 if (m.nrowcxx . . $ constmatrixview#nrow. < 5)
227 return kurt;
228 autoMATcxx . . $ autoMAT# x = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (m);
229 autoVECcxx . . $ autoVEC# mean = columnMeans_VECcxx . . $ columnMeans_VEC(81359d2d5440d947). (xcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
230 x.allcxx . . $ automatrix#all(2114117ba8ed4224).() -=cxx . . $ `operator-=`(88779db6ac139909). meancxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).();
231 autoMATcxx . . $ autoMAT# covar = MATcovarianceFromColumnCentredMatrixcxx . . $ MATcovarianceFromColumnCentredMatrix(34f350791c8ce91c). (xcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), 1);
232
233 if (method == 1) { // Schott (2001, page 33)
234 kurt = 0.0;
235 for (integercxx . . $ integer# l = 1; l <= x.ncolcxx . . $ automatrix#ncol.; l ++) {
236 const double sll2 = covar [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).l] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).l] * covar [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).l] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).l];
237 double zl = 0.0, wl;
238 for (integercxx . . $ integer# j = 1; j <= x.nrowcxx . . $ automatrix#nrow.; j ++) {
239 const double d = x [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).l] - mean [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).l], d2 = d * d;
240 zl += d2 * d2;
241 }
242 zl = (zl - 6.0 * sll2) / (x.nrowcxx . . $ automatrix#nrow. - 4);
243 wl = (sll2 - zl / x.nrowcxx . . $ automatrix#nrow.) * x.nrowcxx . . $ automatrix#nrow. / (x.nrowcxx . . $ automatrix#nrow. - 1);
244 kurt += zl / wl;
245 }
246 kurt = kurt / (3.0 * x.ncolcxx . . $ automatrix#ncol.) - 1.0;
247 }
248 return kurt;
249 }
250
251 /*
252 The following algorithm for monotone regession is on the average
253 3.5 times faster than
254 Kruskal's algorithm for monotone regression (and much simpler).
255 Regression is ascending
256 */
257 autoVECcxx . . $ autoVEC# newVECmonotoneRegressioncxx . . $ newVECmonotoneRegression(9f611ce50d479c14). (constVECcxx . . $ constVEC# x) {
258 autoVECcxx . . $ autoVEC# fit = copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (xcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).);
259 double xt = undefinedcxx . . $ undefined.; // only to stop gcc from complaining "may be used uninitialized"
260 for (integercxx . . $ integer# i = 2; i <= x.sizecxx . . $ constvector#size.; i ++) {
261 if (fit [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] >= fit [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1])
262 continue;
263 longdoublecxx . . $ longdouble# sum = fit [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i];
264 integercxx . . $ integer# nt = 1;
265 for (integercxx . . $ integer# j = 1; j <= i - 1; j ++) {
266 sum += fit [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - j];
267 nt ++;
268 xt = (double) sum / nt; // i >= 2 -> xt always gets a value
269 if (j < i - 1 && xt >= fit [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - j - 1])
270 break;
271 }
272 for (integercxx . . $ integer# j = i - nt + 1; j <= i; j ++)
273 fit [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = xt;
274 }
275 return fitcxx . . $ autovector#autovector(d33c649a3aceae2f).;
276 }
277
278 #undef TINY
279
280 double NUMdeterminant_fromSymmetricMatrixcxx . . $ NUMdeterminant_fromSymmetricMatrix(3ee797411d4a69d9). (constMATcxx . . $ constMAT# m) {
281 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (m.nrow == m.ncol);
282 autoMATcxx . . $ autoMAT# a = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
283 /*
284 Cholesky decomposition in lower, leave upper intact
285 */
286 integercxx . . $ integer# info;
287 NUMlapack_dpotf2_cxx . . $ NUMlapack_dpotf2_(d846c6f385f2d874). ("U", a.nrowcxx . . $ automatrix#nrow., & a [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], m.nrowcxx . . $ constmatrix#nrow., & info);
288 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (info == 0,
289 U"dpotf2_ failed with error = ", info);
290 longdoublecxx . . $ longdouble# lnd = 0.0;
291 for (integercxx . . $ integer# i = 1; i <= a.nrowcxx . . $ automatrix#nrow.; i ++)
292 lnd += logcxx . . $ log(fee4f1d22bc8e617). (a [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
293 lnd *= 2.0; // because A = L . L'
294 return (double) lnd;
295 }
296
297 autoMATcxx . . $ autoMAT# newMATlowerCholeskycxx . . $ newMATlowerCholesky(a974ce8d83fe0cb9). (constMATVUcxx . . $ constMATVU# const& a, double *out_lnd) {
298 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == a.ncol);
299 autoMATcxx . . $ autoMAT# result = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (a);
300 MATlowerCholesky_inplacecxx . . $ MATlowerCholesky_inplace(c08215d9f94fcc4f). (result.getcxx . . $ automatrix#get(b2b81450b5bdda66).(), out_lnd);
301 for (integercxx . . $ integer# irow = 1; irow <= a.nrowcxx . . $ constmatrixview#nrow. - 1; irow ++)
302 for (integercxx . . $ integer# icol = irow + 1; icol <= a.nrowcxx . . $ constmatrixview#nrow.; icol ++)
303 result [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = 0.0;
304 return resultcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
305 }
306
307
308 autoMATcxx . . $ autoMAT# newMATlowerCholeslyInverse_fromLowerCholeskycxx . . $ newMATlowerCholeslyInverse_fromLowerCholesky(609cc9fb56c8e948). (constMATcxx . . $ constMAT# const& m) {
309 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (m.nrow == m.ncol);
310 autoMATcxx . . $ autoMAT# result = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
311 MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (result.getcxx . . $ automatrix#get(b2b81450b5bdda66).(), nullptr);
312 return resultcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
313 }
314
315 void MATlowerCholesky_inplacecxx . . $ MATlowerCholesky_inplace(c08215d9f94fcc4f). (MATcxx . . $ MAT# a, double *out_lnDeterminant) {
316 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == a.ncol);
317 /*
318 Cholesky decomposition in lower, leave upper intact
319 Fortran storage -> use uplo='U' to get 'L'.
320 */
321 integercxx . . $ integer# info;
322 NUMlapack_dpotf2_cxx . . $ NUMlapack_dpotf2_(d846c6f385f2d874). ("U", a.nrowcxx . . $ matrix#nrow., & a [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], a.nrowcxx . . $ matrix#nrow., & info);
323 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (info == 0,
324 U"dpotf2 fails with code ", info, U".");
325 /*
326 Determinant from diagonal, diagonal is now sqrt (a [i] [i]) !
327 */
328 if (out_lnDeterminant) {
329 longdoublecxx . . $ longdouble# lnDeterminant = 0.0;
330 for (integercxx . . $ integer# i = 1; i <= a.nrowcxx . . $ matrix#nrow.; i ++)
331 lnDeterminant += logcxx . . $ log(fee4f1d22bc8e617). (a [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
332 lnDeterminant *= 2.0; /* because A = L . L' */
333 *out_lnDeterminant = double (lnDeterminant);
334 }
335 }
336
337 void MATlowerCholeskyInverse_inplacecxx . . $ MATlowerCholeskyInverse_inplace(c08215d9f94fcc4f). (MATcxx . . $ MAT# a, double *out_lnd) {
338 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == a.ncol);
339 MATlowerCholesky_inplacecxx . . $ MATlowerCholesky_inplace(c08215d9f94fcc4f). (a, out_lnd);
340 /*
341 Get the inverse
342 */
343 integercxx . . $ integer# info;
344 (void) NUMlapack_dtrtri_cxx . . $ NUMlapack_dtrtri_(a45231551f8ed625). ("U", "N", a.nrowcxx . . $ matrix#nrow., & a [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], a.nrowcxx . . $ matrix#nrow., & info);
345 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (info == 0,
346 U"dtrtri_ fails with code ", info, U".");
347 }
348
349 autoMATcxx . . $ autoMAT# newMATinverse_fromLowerCholeskyInversecxx . . $ newMATinverse_fromLowerCholeskyInverse(88e1bde181068261). (constMATcxx . . $ constMAT# m) {
350 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (m.nrow == m.ncol);
351 autoMATcxx . . $ autoMAT# result = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (m.nrowcxx . . $ constmatrix#nrow., m.nrowcxx . . $ constmatrix#nrow.);
352 for (integercxx . . $ integer# irow = 1; irow <= m.nrowcxx . . $ constmatrix#nrow.; irow ++) {
353 for (integercxx . . $ integer# icol = 1; icol <= irow; icol ++) {
354 longdoublecxx . . $ longdouble# sum = 0.0;
355 for (integercxx . . $ integer# k = irow; k <= m.nrowcxx . . $ constmatrix#nrow.; k ++)
356 sum += m [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).k] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).irow] * m [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).k] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol];
357 result [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = result [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icol] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = (double) sum;
358 }
359 }
360 return resultcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
361 }
362
363 autoMATcxx . . $ autoMAT# newMATsymmetricPositiveDefinte_inversecxx . . $ newMATsymmetricPositiveDefinte_inverse(88e1bde181068261). (constMATcxx . . $ constMAT# m) {
364 autoMATcxx . . $ autoMAT# inversecxx . . $ automatrix#automatrix(49f6e7a06ebc5aa8).;
365 try {
366 if (m.nrowcxx . . $ constmatrix#nrow. == 1) {
367 inverse =cxx . . $ automatrix#`operator=`(a0f46581f260c488). copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
368 inverse [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = ( m [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).1] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] != 0.0 ? 1.0 / m [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).1] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] : undefinedcxx . . $ undefined. );
369 } else {
370 autoMATcxx . . $ autoMAT# mc = newMATlowerCholeskycxx . . $ newMATlowerCholesky(a974ce8d83fe0cb9). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b)., nullptr);
371 inverse =cxx . . $ automatrix#`operator=`(a0f46581f260c488). newMATinverse_fromLowerCholeskyInversecxx . . $ newMATinverse_fromLowerCholeskyInverse(88e1bde181068261). (mccxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
372 }
373 } catch (MelderErrorcxx . . $ MelderError#) {
374 inverse =cxx . . $ automatrix#`operator=`(a0f46581f260c488). copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
375 inversecxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).() <<=cxx . . $ `operator<<=`(50b469ae42a42f64). undefinedcxx . . $ undefined.;
376 }
377 return inversecxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
378 }
379
380 autoMATcxx . . $ autoMAT# newMATsymmetric_inverse2cxx . . $ newMATsymmetric_inverse2(88e1bde181068261). (constMATcxx . . $ constMAT# m) {
381 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (m.nrow == m.ncol);
382 autoINTVECcxx . . $ autoINTVEC# ipiv = zero_INTVECcxx . . $ zero_INTVEC(dab5d3ef28d6a25). (m.nrowcxx . . $ constmatrix#nrow.);
383 autoMATcxx . . $ autoMAT# result = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
384 const integercxx . . $ integer# lda = m.nrowcxx . . $ constmatrix#nrow., lwork = m.nrowcxx . . $ constmatrix#nrow. * m.ncolcxx . . $ constmatrix#ncol.;
385 /*
386 We don't query for the size of the work array, just make it big enough
387 */
388 autoVECcxx . . $ autoVEC# work = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (lwork);
389 integercxx . . $ integer# info;
390 (void) NUMlapack_dsytrf_cxx . . $ NUMlapack_dsytrf_(c9f1502ed13b16d6).("L", m.nrowcxx . . $ constmatrix#nrow., & result [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], lda, ipiv.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(),
391 work.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(), lwork, & info);
392 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (info == 0,
393 U"dsytrf_ fails with code ", info, U".");
394
395 (void) NUMlapack_dsytri_cxx . . $ NUMlapack_dsytri_(f4e10e5bb55f0f9c). ("L", m.nrowcxx . . $ constmatrix#nrow., & result [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], lda, ipiv.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(),
396 work.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).(), & info);
397 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (info == 0,
398 U"dsytri_ fails with code ", info, U".");
399
400 return resultcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
401 }
402
403 autoMATcxx . . $ autoMAT# newMATinversecxx . . $ newMATinverse(88e1bde181068261). (constMATcxx . . $ constMAT# m) {
404 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (m.nrow == m.ncol);
405 try {
406 autoSVDcxx . . $ autoSVD# svd = SVD_createFromGeneralMatrixcxx . . $ SVD_createFromGeneralMatrix(35acab982e3981c8). (mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
407 autoMATcxx . . $ autoMAT# vp = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (m.nrowcxx . . $ constmatrix#nrow., m.ncolcxx . . $ constmatrix#ncol.);
408 for (integercxx . . $ integer# irow = 1; irow <= m.nrowcxx . . $ constmatrix#nrow.; irow ++)
409 for (integercxx . . $ integer# icol = 1; icol <= m.ncolcxx . . $ constmatrix#ncol.; icol ++)
410 vp [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = svd ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). vcxx . . $ structSVD#v. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow][cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] / svd ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dcxx . . $ structSVD#d. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icol];
411 autoMATcxx . . $ autoMAT# inverse = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (vpcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), svdcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). ucxx . . $ structSVD#u..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
412 return inversecxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
413 } catch (MelderErrorcxx . . $ MelderError#) {
414 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"newMATinverse: cannot get inverse.");
415 }
416 }
417 double NUMmahalanobisDistanceSquaredcxx . . $ NUMmahalanobisDistanceSquared(b128fcdd00c3195b). (constMATcxx . . $ constMAT# lowerInverse, constVECcxx . . $ constVEC# v, constVECcxx . . $ constVEC# m) {
418 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (lowerInverse.ncol == v.size && v.size == m.size);
419 longdoublecxx . . $ longdouble# chisq = 0.0;
420 if (lowerInverse.nrowcxx . . $ constmatrix#nrow. == 1) { // diagonal matrix is one row matrix
421 for (integercxx . . $ integer# icol = 1; icol <= v.sizecxx . . $ constvector#size.; icol ++) {
422 const double t = lowerInverse [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).1] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol] * (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol] - m [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol]);
423 chisq += t * t;
424 }
425 } else { // square matrix
426 for (integercxx . . $ integer# irow = v.sizecxx . . $ constvector#size.; irow > 0; irow --) {
427 longdoublecxx . . $ longdouble# t = 0.0;
428 for (integercxx . . $ integer# icol = 1; icol <= irow; icol ++)
429 t += lowerInverse [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).irow] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol] * (v [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol] - m [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).icol]);
430 chisq += t * t;
431 }
432 }
433 return (double) chisq;
434 }
435
436 /*
437 G. Golub & C. van Loan (1996), Matrix computations, third edition,
438 The Johns Hopkins University Press Ltd.,
439 London, (Par. 7.3.1 The Power Method)
440 */
441 double VECdominantEigenvector_inplacecxx . . $ VECdominantEigenvector_inplace(35a6868fae9f156b). (VECcxx . . $ VEC# inout_q, constMATcxx . . $ constMAT# m, double tolerance) {
442 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (m.nrow == m.ncol && inout_q.size == m.nrow);
443
444 double lambda0, lambda = NUMmulcxx . . $ NUMmul(1cc921b496420d09). (inout_qcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b)., inout_qcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).); // q'. M . q
445 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (lambda > 0.0,
446 U"Zero matrices ??");
447 autoVECcxx . . $ autoVEC# z = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (m.nrowcxx . . $ constmatrix#nrow.);
448 integercxx . . $ integer# maximunNumberOfIterations = 30;
449 for (integercxx . . $ integer# iter = 1; iter <= maximunNumberOfIterations; iter ++) {
450 lambda0 = lambda;
451 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (zcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b)., inout_qcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).);
452 VECnormalize_inplacecxx . . $ VECnormalize_inplace(963991e5268b1933). (zcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 2.0, 1.0);
453 lambda = NUMmulcxx . . $ NUMmul(1cc921b496420d09). (zcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), mcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b)., zcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); // z'. M . z
454 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (lambda - lambda0) < tolerance)
455 break;
456 }
457 inout_qcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(adea042513329390). zcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).();
458 return lambda;
459 }
460
461 void MATprojectColumnsOnEigenspace_preallocatedcxx . . $ MATprojectColumnsOnEigenspace_preallocated(c1f15f52bb77a4f). (MATcxx . . $ MAT# projection, constMATVUcxx . . $ constMATVU# const& data, constMATVUcxx . . $ constMATVU# const& eigenvectors) {
462 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (data.nrow == eigenvectors.ncol && projection.nrow == eigenvectors.nrow);
463 for (integercxx . . $ integer# icol = 1; icol <= data.ncolcxx . . $ constmatrixview#ncol.; icol ++)
464 for (integercxx . . $ integer# irow = 1; irow <= eigenvectors.nrowcxx . . $ constmatrixview#nrow.; irow ++)
465 projection [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (eigenvectors.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (irow), data.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (icol));
466 // MATmul_tt (data.get(), eigenvectors.get()) ??
467 }
468
469 integercxx . . $ integer# NUMsolveQuadraticEquationcxx . . $ NUMsolveQuadraticEquation(727311d4260346f0). (double a, double b, double c, double *out_x1, double *out_x2) {
470 double x1, x2;
471 integercxx . . $ integer# result = gsl_poly_solve_quadratic (a, b, c, & x1, & x2);
472 if (out_x1)
473 *out_x1 = x1;
474 if (out_x2)
475 *out_x2 = x2;
476 return result;
477 }
478
479 autoVECcxx . . $ autoVEC# solve_VECcxx . . $ solve_VEC(27d4aa16c6844bf6). (constMATVUcxx . . $ constMATVU# const& a, constVECVUcxx . . $ constVECVU# const& b, double tolerance) {
480 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == b.size);
481 autoSVDcxx . . $ autoSVD# me = SVD_createFromGeneralMatrixcxx . . $ SVD_createFromGeneralMatrix(35acab982e3981c8). (a);
482 SVD_zeroSmallSingularValuescxx . . $ SVD_zeroSmallSingularValues(3c6b93a29a648e9a). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tolerance);
483 autoVECcxx . . $ autoVEC# x = SVD_solvecxx . . $ SVD_solve(8ead2e281a66fa69). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), b);
484 return xcxx . . $ autovector#autovector(d33c649a3aceae2f).;
485 }
486
487 autoMATcxx . . $ autoMAT# solve_MATcxx . . $ solve_MAT(5d4edd2029231381). (constMATVUcxx . . $ constMATVU# const& a, constMATVUcxx . . $ constMATVU# const& b, double tolerance) {
488 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == b.nrow);
489 const double tol = ( tolerance > 0.0 ? tolerance : NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps. * a.nrowcxx . . $ constmatrixview#nrow. );
490
491 autoSVDcxx . . $ autoSVD# me = SVD_createFromGeneralMatrixcxx . . $ SVD_createFromGeneralMatrix(35acab982e3981c8). (a);
492 autoMATcxx . . $ autoMAT# x = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (b.nrowcxx . . $ constmatrixview#nrow., b.ncolcxx . . $ constmatrixview#ncol.);
493
494 SVD_zeroSmallSingularValuescxx . . $ SVD_zeroSmallSingularValues(3c6b93a29a648e9a). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tol);
495
496 for (integercxx . . $ integer# k = 1; k <= b.ncolcxx . . $ constmatrixview#ncol.; k ++) {
497 autoVECcxx . . $ autoVEC# xt = SVD_solvecxx . . $ SVD_solve(8ead2e281a66fa69). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), b.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (k));
498 x.columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (k) <<=cxx . . $ `operator<<=`(adea042513329390). xtcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).();
499 }
500 return xcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
501 }
502
503 /*
504 Non-negative least squares: Solve Ax = y, i.e
505 minimize || Ax - y ||^2 for vector x, where all x [i] >= 0.0
506 */
507 void VECsolveNonnegativeLeastSquaresRegressioncxx . . $ VECsolveNonnegativeLeastSquaresRegression(2e2c493d9e4ea95e). (VECVUcxx . . $ VECVU# const& x, constMATVUcxx . . $ constMATVU# const& a, constVECVUcxx . . $ constVECVU# const& y, integercxx . . $ integer# maximumNumberOfIterations, double tol, integercxx . . $ integer# infoLevel) {
508 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == y.size);
509 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.ncol == x.size);
510 for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ vectorview#size.; i ++)
511 Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (0.0, & x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i]);
512 autoVECcxx . . $ autoVEC# r = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (y.sizecxx . . $ constvectorview#size.);
513 const double normSquared_y = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (y);
514 integercxx . . $ integer# iter = 1;
515 bool farFromConvergence = true;
516 double difsq = undefinedcxx . . $ undefined., difsq_previous = 1e100; // large enough
517 while (iter <= maximumNumberOfIterations && farFromConvergence) {
518 /*
519 Alternating Least Squares: Fixate all except x [icol]
520 */
521 for (integercxx . . $ integer# icol = 1; icol <= a.ncolcxx . . $ constmatrixview#ncol.; icol ++) {
522 r.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). y;
523 for (integercxx . . $ integer# jcol = 1; jcol <= a.ncolcxx . . $ constmatrixview#ncol.; jcol ++)
524 if (jcol != icol)
525 rcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).() -=cxx . . $ `operator-=`(d166bd5b5c96d95c). x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).jcol] *cxx . . $ `operator*`(7f4bd74ab5a11911). a.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (jcol);
526 const double ajr = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (a.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (icol), rcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).());
527 const double ajaj = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (a.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (icol));
528 x [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).icol] = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (0.0, ajr / ajaj);
529 }
530 /*
531 Calculate t(x) and compare with previous result.
532 */
533 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (rcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), a, xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401).);
534 rcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).() -=cxx . . $ `operator-=`(adea042513329390). y;
535 difsq = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (rcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).());
536 farFromConvergence = ( fabscxx . . $ fabs(fee4f1d22bc8e617). (difsq - difsq_previous) > stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (tol * normSquared_y, NUMepscxx . . $ `dwsys/NUM2.h:36:9`!) );
537 if (infoLevel > 1)
538 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"\t\tIteration: ", iter, U", error: ", difsq);
539 difsq_previous = difsq;
540 iter ++;
541 }
542 iter --;
543 if (infoLevel >= 1)
544 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"\t\tNumber of iterations: ", iter, U"; Minimum: ", difsq);
545 if (infoLevel > 0)
546 MelderInfo_draincxx . . $ MelderInfo_drain(49f6e7a06ebc5aa8).();
547 }
548
549 struct nr_structcxx . . $ nr_struct# {
550 double *ycxx . . $ nr_struct#y., *deltacxx . . $ nr_struct#delta.;
551 };
552
553 /*
554 f (lambda) = sum (y [i]^2 delta [i] / (delta [i]-lambda)^2, i=1..3)
555 f'(lambda) = 2 * sum (y [i]^2 delta [i] / (delta [i]-lambda)^3, i=1..3)
556 */
557
558 static double nr_funccxx . . $ nr_func(b9cf8b1375751fce). (double x, double *df, void *data) {
559 const struct nr_structcxx . . $ nr_struct# *me = (struct nr_structcxx . . $ nr_struct# *) data;
560 longdoublecxx . . $ longdouble# f = 0.0, derivative = 0.0;
561 for (integercxx . . $ integer# i = 1; i <= 3; i ++) {
562 const longdoublecxx . . $ longdouble# t1 = (mycxx . . $ `melder/melder.h:44:9`! deltacxx . . $ nr_struct#delta. [i] - x);
563 const longdoublecxx . . $ longdouble# t2 = mycxx . . $ `melder/melder.h:44:9`! ycxx . . $ nr_struct#y. [i] / t1;
564 const longdoublecxx . . $ longdouble# t3 = t2 * t2 * mycxx . . $ `melder/melder.h:44:9`! deltacxx . . $ nr_struct#delta. [i];
565 f += t3;
566 derivative += t3 * 2.0 / t1;
567 }
568 *df = (double) derivative;
569 return (double) f;
570 }
571
572 void NUMsolveConstrainedLSQuadraticRegressioncxx . . $ NUMsolveConstrainedLSQuadraticRegression(e56c852ad24f641e). (constMATcxx . . $ constMAT# const& x, constVECcxx . . $ constVEC# const& d, double *out_alpha, double *out_gamma) {
573 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.ncol == x.nrow && d.size == x.ncol && d.size == 3);
574 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.nrow >= x.ncol);
575 integercxx . . $ integer# n3 = 3;
576 const double eps = 1e-6;
577 /*
578 Construct X'.X which will be 3x3
579 */
580 autoMATcxx . . $ autoMAT# xtx = mtm_MATcxx . . $ mtm_MAT(a392b09ce3fc3972). (xcxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
581 /*
582 Eq (2): get lower Cholesky decomposition from X'.X (3x3)
583 X'X -> lowerCholesky * lowerCholesky.transpose()
584 F'^-1 == lowerCholesky
585 F^-1 == lowerCholesky.transpose()
586 F = lowerCholeskyInverse.transpose()
587 */
588 autoMATcxx . . $ autoMAT# lowerCholesky = newMATlowerCholeskycxx . . $ newMATlowerCholesky(a974ce8d83fe0cb9). (xtxcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), nullptr);
589 autoMATcxx . . $ autoMAT# lowerCholeskyInverse = newMATlowerCholeslyInverse_fromLowerCholeskycxx . . $ newMATlowerCholeslyInverse_fromLowerCholesky(609cc9fb56c8e948). (lowerCholeskycxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
590 /*
591 Construct G and its eigen-decomposition (eq. (4,5))
592 Sort eigenvalues (& eigenvectors) ascending.
593 */
594 autoMATcxx . . $ autoMAT# b = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (n3, n3);
595 b [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).3] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = b [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).3] = -0.5;
596 b [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = 1.0;
597 /*
598 G = F^-1 B (F')^-1 (eq. 4)
599 */
600 autoMATcxx . . $ autoMAT# g = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (n3, n3);
601 MATmul3_XYXtcxx . . $ MATmul3_XYXt(9e368e0da0f1874a). (gcxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), lowerCholeskycxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).(), bcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
602 /*
603 G's eigen-decomposition with eigenvalues (assumed ascending). (eq. 5)
604 */
605 autoMATcxx . . $ autoMAT# pcxx . . $ automatrix#automatrix(49f6e7a06ebc5aa8).;
606 autoVECcxx . . $ autoVEC# deltacxx . . $ autovector#autovector(49f6e7a06ebc5aa8).;
607 MAT_getEigenSystemFromSymmetricMatrixcxx . . $ MAT_getEigenSystemFromSymmetricMatrix(5c0054344d6d0394). (gcxx . . $ constmatrix#constmatrix(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), & p, & delta, true);
608 /*
609 Construct y = P'*F'*O'*d = (F*P)'*(O'*d) (page 632)
610 We need F*P for later
611 */
612 autoVECcxx . . $ autoVEC# otd = mul_VECcxx . . $ mul_VEC(9a5f078c2b4f07dc). (x.transposecxx . . $ constmatrix#transpose(3a2dc78a4d19c6bb).(), dcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).);
613 autoMATcxx . . $ autoMAT# fp = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (pcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).(), lowerCholeskyInversecxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).()); // F*P !!
614 autoVECcxx . . $ autoVEC# y = mul_VECcxx . . $ mul_VEC(9a5f078c2b4f07dc). (fpcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).(), otdcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); // P'F' * O'd
615 /*
616 The solution (3 cases)
617 */
618 autoVECcxx . . $ autoVEC# w = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (n3);
619 autoVECcxx . . $ autoVEC# chi = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (n3);
620 autoVECcxx . . $ autoVEC# diag = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (n3);
621
622 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]) < eps) {
623 /*
624 Case 1: page 633
625 */
626 const double t21 = y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] / (delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] - delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]);
627 const double t31 = y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] / (delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] - delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]);
628 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (- delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] * (t21 * t21 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] + t31 * t31 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3]));
629 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = t21 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2];
630 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] = t31 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3];
631
632 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (chicxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fpcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); // chi = F*P*w
633
634 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (chi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] / chi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]) < eps) {
635 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = - w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
636 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (chicxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fpcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
637 }
638 } else if (fabscxx . . $ fabs(fee4f1d22bc8e617). (y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2]) < eps) {
639 /*
640 Case 2: page 633
641 */
642 const double t12 = y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] / (delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] - delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2]);
643 const double t32 = y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] / (delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] - delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2]);
644 double t2;
645 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = t12 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
646 if ( (delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] < delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] && (t2 = (t12 * t12 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] + t32 * t32 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3])) <= 0.0)) {
647 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (- delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] * t2); /* +- */
648 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] = t32 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3];
649 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (chicxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), pcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
650 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (chi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] / chi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]) < eps) {
651 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = -w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2];
652 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (chicxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fpcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
653 }
654 } else if (fabscxx . . $ fabs(fee4f1d22bc8e617). (delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] - delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3]) < eps && fabscxx . . $ fabs(fee4f1d22bc8e617). (y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3]) < eps) {
655 /*
656 Choose one value for w [2] from an infinite number
657 */
658 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
659 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (- t12 * t12 * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] * delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] - w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] * w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2]);
660 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (chicxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fpcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
661 } else {
662 // should not be here
663 }
664 } else {
665 /*
666 Case 3: page 634 use Newton-Raphson root finder
667 */
668 struct nr_structcxx . . $ nr_struct# me;
669 me.ycxx . . $ nr_struct#y. = y.asArgumentToFunctionThatExpectsOneBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsOneBasedArray(5a2a78a048fb49a8).();
670 me.deltacxx . . $ nr_struct#delta. = delta.asArgumentToFunctionThatExpectsOneBasedArraycxx . . $ autovector#asArgumentToFunctionThatExpectsOneBasedArray(5a2a78a048fb49a8).();
671
672 double lambda = NUMnrbiscxx . . $ NUMnrbis(928da06fce274b3e). (nr_funccxx . . $ nr_func(b9cf8b1375751fce)., delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] + eps, delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] - eps, & me);
673
674 for (integercxx . . $ integer# i = 1; i <= 3; i++)
675 w [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] / (1.0 - lambda / delta [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]);
676 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (chicxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), fpcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), wcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
677 }
678 if (out_alpha)
679 *out_alpha = chi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
680 if (out_gamma)
681 *out_gamma = chi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).3];
682 }
683
684 /*
685 To find the value of b for which the derivative of psi(b) equals zero (Ten Berge, formula 4)
686 psi (b) = delta*b - b^2 /(4*alpha) - sum (i=1..n, x [i]^2 / (c [i] - b)) (formula 4)
687 psi'(b) = delta - b / (2 alpha) - sum (x [i]^2 / (c [i] - b)^2, i=1..n). (formula 5)
688 We use NewTon-Raphson with bisection because we can also use psi'(b)'s derivative:
689 f (b) = psi'(b) = delta - b / (2 alpha) - sum (x [i]^2 / (c [i] - b)^2, i=1..n)
690 f'(b) = psi''(b) = - 1 / (2 alpha) + 2 * sum (x [i]^2 / (c [i] - b)^3, i=1..n)
691 */
692 struct nr2_structcxx . . $ nr2_struct# {
693 integercxx . . $ integer# numberOfTermscxx . . $ nr2_struct#numberOfTerms.;
694 double deltacxx . . $ nr2_struct#delta., alphacxx . . $ nr2_struct#alpha.;
695 VECcxx . . $ VEC# xcxx . . $ nr2_struct#x., ccxx . . $ nr2_struct#c.;
696 };
697
698 static double bolzanoFunctioncxx . . $ bolzanoFunction(81b005370e6958c4). (double b, void *data) {
699 const struct nr2_structcxx . . $ nr2_struct# *me = (struct nr2_structcxx . . $ nr2_struct# *) data;
700 longdoublecxx . . $ longdouble# f = mycxx . . $ `melder/melder.h:44:9`! deltacxx . . $ nr2_struct#delta. - 0.5 * b / mycxx . . $ `melder/melder.h:44:9`! alphacxx . . $ nr2_struct#alpha.;
701 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfTermscxx . . $ nr2_struct#numberOfTerms.; i ++) {
702 const longdoublecxx . . $ longdouble# c = mycxx . . $ `melder/melder.h:44:9`! xcxx . . $ nr2_struct#x. [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] / (mycxx . . $ `melder/melder.h:44:9`! ccxx . . $ nr2_struct#c. [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - b);
703 f -= c * c;
704 }
705 return (double) f;
706 }
707
708 static double NUMbolzanoSearchcxx . . $ NUMbolzanoSearch(25f8e7b9d159e245). (double (*func) (double x, void *closure), double xmin, double xmax, void *closure) {
709 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (xmin < xmax);
710 double fleft = (*func)(xmin, closure);
711 double fright = (*func)(xmax, closure);
712 if (fleft == 0.0)
713 return fleft;
714 if (fright == 0.0)
715 return fright;
716 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (fleft * fright < 0.0,
717 U"Invalid interval: the function values at the borders should have different signs.");
718 double xdifference = fabscxx . . $ fabs(fee4f1d22bc8e617). (xmax - xmin), xdifference_old;
719 do {
720 const double xmid = 0.5 * (xmax + xmin);
721 const double fmid = (*func)(xmid, closure);
722 if (fmid == 0.0)
723 return fmid;
724 if (fleft * fmid < 0.0)
725 xmax = xmid;
726 else
727 xmin = xmid;
728 xdifference_old = xdifference;
729 xdifference = fabscxx . . $ fabs(fee4f1d22bc8e617). (xmax - xmin);
730 } while (xdifference < xdifference_old);
731 return 0.5 * (xmax + xmin);
732 }
733
734 autoVECcxx . . $ autoVEC# solveWeaklyConstrainedLinearRegression_VECcxx . . $ solveWeaklyConstrainedLinearRegression_VEC(7c02f05d3410d3c). (constMATcxx . . $ constMAT# const& a, constVECcxx . . $ constVEC# const& y, double alpha, double delta) {
735 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == y.size);
736 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (a.nrow >= a.ncol,
737 U"The number of rows should not be less than the number of columns.");
738 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (alpha >= 0.0,
739 U"The coefficient of the penalty function should not be negative.");
740 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (delta > 0,
741 U"The solution's vector length should be positive.");
742
743 autoVECcxx . . $ autoVEC# c = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (a.ncolcxx . . $ constmatrix#ncol.);
744 autoSVDcxx . . $ autoSVD# svd = SVD_createFromGeneralMatrixcxx . . $ SVD_createFromGeneralMatrix(35acab982e3981c8). (acxx . . $ constmatrixview#constmatrixview(5fba671ea590c05b).);
745
746 if (alpha == 0.0) {
747 /*
748 Solve by standard least squares
749 */
750 autoVECcxx . . $ autoVEC# result = SVD_solvecxx . . $ SVD_solve(8ead2e281a66fa69). (svd.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), ycxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).);
751 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
752 }
753 /*
754 Step 1: (page 608)
755 Compute U and C from the eigendecomposition F'F = UCU'
756 U is from the SVD (F) = VCU' as in the paper (our notation would be: SVD(F)=UDV')
757 Therefore, the paper's U is our V but since our V is stored as the transpose: U == svd -> v.transpose()!
758 */
759 constMATVUcxx . . $ constMATVU# u = svdcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). vcxx . . $ structSVD#v..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).();
760 for (integercxx . . $ integer# i = 1; i <= a.ncolcxx . . $ constmatrix#ncol.; i++) {
761 const double ci = svd ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dcxx . . $ structSVD#d. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i];
762 c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = ci * ci;
763 }
764 /*
765 Evaluate q, the multiplicity of the smallest eigenvalue in C.
766 The c [i] are ordered, i.e. c [i] >= c [i+1] for all i.
767 */
768 integercxx . . $ integer# q = 1;
769 const double tol = 1e-6;
770 while (q < a.ncolcxx . . $ constmatrix#ncol. && (c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).a.ncolcxx . . $ constmatrix#ncol. - q] - c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).a.ncolcxx . . $ constmatrix#ncol.]) < tol)
771 q ++;
772 /*
773 Step 2: x = U'F'y
774 */
775 autoVECcxx . . $ autoVEC# ftphi = mul_VECcxx . . $ mul_VEC(9a5f078c2b4f07dc). (a.transposecxx . . $ constmatrix#transpose(3a2dc78a4d19c6bb).(), ycxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).);
776 autoVECcxx . . $ autoVEC# x = mul_VECcxx . . $ mul_VEC(9a5f078c2b4f07dc). (u.transposecxx . . $ constmatrixview#transpose(f17ed7654a0ebced).(), ftphicxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
777 /*
778 Step 3:
779 */
780 struct nr2_structcxx . . $ nr2_struct# me;
781 me.numberOfTermscxx . . $ nr2_struct#numberOfTerms. = a.ncolcxx . . $ constmatrix#ncol.;
782 me.deltacxx . . $ nr2_struct#delta. = delta;
783 me.alphacxx . . $ nr2_struct#alpha. = alpha;
784 me.xcxx . . $ nr2_struct#x. =cxx . . $ vector#`operator=`(b5ebf627ac2fae1e). x.getcxx . . $ autovector#get(e8d1da7fe68a6585).();
785 me.ccxx . . $ nr2_struct#c. =cxx . . $ vector#`operator=`(b5ebf627ac2fae1e). c.getcxx . . $ autovector#get(e8d1da7fe68a6585).();
786
787 const double xqsq = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (xcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (a.ncolcxx . . $ constmatrix#ncol. - q + 1, a.ncolcxx . . $ constmatrix#ncol.)); // = NUMinner (xq,xq)
788 integercxx . . $ integer# r = a.ncolcxx . . $ constmatrix#ncol.;
789 if (xqsq < tol) { // Case3.b page 608
790 r = a.ncolcxx . . $ constmatrix#ncol. - q;
791 me.numberOfTermscxx . . $ nr2_struct#numberOfTerms. = r;
792 const double fm = bolzanoFunctioncxx . . $ bolzanoFunction(81b005370e6958c4). (c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).a.ncolcxx . . $ constmatrix#ncol.], & me);
793 if (fm >= 0.0) { // step 3.b1
794 /*
795 Get w0 by overwriting vector x.
796 */
797 x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).r + 1] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (fm);
798 for (integercxx . . $ integer# j = 1; j <= r; j ++)
799 x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] /= c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).a.ncolcxx . . $ constmatrix#ncol.]; // eq. 10
800 if (q > 1)
801 xcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (r + 2, a.ncolcxx . . $ constmatrix#ncol.) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0;
802 autoVECcxx . . $ autoVEC# result = mul_VECcxx . . $ mul_VEC(9a5f078c2b4f07dc). (u, xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).());
803 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
804 }
805 // else continue with r = m - q
806 }
807 /*
808 Step 3a & 3b2, determine interval lower bound for root finder
809 */
810 longdoublecxx . . $ longdouble# xCx = 0.0;
811 for (integercxx . . $ integer# j = 1; j <= r; j ++)
812 xCx += x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] * x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] / c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j];
813
814 const double bmin = ( delta > 0.0 ? - xCx / delta : -2.0 * sqrtcxx . . $ sqrt(cd6880bb7604d12e). (alpha * xCx) );
815 //const double eps = (c [a.ncol] - bmin) * tol;
816 /*
817 Find the root of d(psi(b)/db in interval (bmin, c [m])
818 */
819 const double b0 = NUMbolzanoSearchcxx . . $ NUMbolzanoSearch(25f8e7b9d159e245). (bolzanoFunctioncxx . . $ bolzanoFunction(81b005370e6958c4)., bmin, c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).a.ncolcxx . . $ constmatrix#ncol.], & me);
820 /*
821 Get w0 by overwriting vector x.
822 */
823 for (integercxx . . $ integer# j = 1; j <= r; j ++)
824 x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] /= c [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - b0; // eq. 7
825 if (q > 1)
826 xcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (r + 1, a.ncolcxx . . $ constmatrix#ncol.) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0;
827 autoVECcxx . . $ autoVEC# result = mul_VECcxx . . $ mul_VEC(9a5f078c2b4f07dc). (u, xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).());
828 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
829 }
830
831
832 void NUMprocrustescxx . . $ NUMprocrustes(f3a1b3dd0f6bd4d5). (constMATVUcxx . . $ constMATVU# const& x, constMATVUcxx . . $ constMATVU# const& y, autoMATcxx . . $ autoMAT# *out_rotation, autoVECcxx . . $ autoVEC# *out_translation, double *out_scale) {
833 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.nrow == y.nrow && x.ncol == y.ncol);
834 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.nrow >= x.ncol);
835 const bool orthogonal = ! out_translation || ! out_scale; // else similarity transform
836 /*
837 Reference: Borg & Groenen (1997), Modern multidimensional scaling,
838 Springer
839 1. Calculate C = X'JY (page 346) for similarity transform
840 else X'Y for othogonal (page 341)
841 JY amounts to centering the columns of Y.
842 */
843 autoMATcxx . . $ autoMAT# yc = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (y);
844 if (! orthogonal)
845 centreEachColumn_MAT_inoutcxx . . $ centreEachColumn_MAT_inout(5dd9f6ea610bc98c). (yccxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
846 autoMATcxx . . $ autoMAT# c = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (x.transposecxx . . $ constmatrixview#transpose(f17ed7654a0ebced).(), yccxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); // X'(JY)
847 /*
848 2. Decompose C by SVD: C = UDV' (our SVD has eigenvectors stored row-wise V!)
849 */
850 autoSVDcxx . . $ autoSVD# svd = SVD_createFromGeneralMatrixcxx . . $ SVD_createFromGeneralMatrix(35acab982e3981c8). (ccxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
851 const double trace = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (svdcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dcxx . . $ structSVD#d..allcxx . . $ autovector#all(a26726dad87555bb).());
852 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (trace > 0.0,
853 U"NUMprocrustes: degenerate configuration(s).");
854 /*
855 3. T = VU'
856 */
857 autoMATcxx . . $ autoMAT# rotation = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (svdcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58).->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).vcxx . . $ structSVD#v..allcxx . . $ automatrix#all(2114117ba8ed4224).(), svdcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58).->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab).ucxx . . $ structSVD#u..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).());
858
859 if (! orthogonal) {
860 /*
861 4. Dilation factor s = (tr X'JYT) / (tr Y'JY)
862 First we need YT.
863 */
864 autoMATcxx . . $ autoMAT# yt = mul_MATcxx . . $ mul_MAT(341c1d932d99e4e3). (y, rotationcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
865 /*
866 X'J = (JX)' centering the columns of X
867 */
868 autoMATcxx . . $ autoMAT# xc = copy_MATcxx . . $ copy_MAT(a392b09ce3fc3972). (x);
869 centreEachColumn_MAT_inoutcxx . . $ centreEachColumn_MAT_inout(5dd9f6ea610bc98c). (xccxx . . $ matrixview#matrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
870 /*
871 tr X'J YT == tr xc' yt
872 */
873 const double traceXtJYT = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (xccxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58)..transposecxx . . $ automatrix#transpose(2114117ba8ed4224).(), ytcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); // trace (Xc'.(YT))
874 const double traceYtJY = NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (y.transposecxx . . $ constmatrixview#transpose(f17ed7654a0ebced).(), yccxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).()); // trace (Y'.Yc)
875 const longdoublecxx . . $ longdouble# scale = traceXtJYT / traceYtJY;
876 /*
877 5. Translation vector tr = (X - sYT)'1 / x.nrow
878 */
879 if (out_translation) {
880 autoVECcxx . . $ autoVEC# translation = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (x.ncolcxx . . $ constmatrixview#ncol.);
881 for (integercxx . . $ integer# i = 1; i <= x.ncolcxx . . $ constmatrixview#ncol.; i ++) {
882 longdoublecxx . . $ longdouble# productsum = 0.0;
883 for (integercxx . . $ integer# j = 1; j <= x.nrowcxx . . $ constmatrixview#nrow.; j ++)
884 productsum += x [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).j] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i] - scale * yt [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
885 translation [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = double (productsum / x.nrowcxx . . $ constmatrixview#nrow.);
886 }
887 *out_translation =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). translation.movecxx . . $ autovector#move(57d90d7d77dfc011).();
888 }
889 if (out_scale)
890 *out_scale = (double) scale;
891 }
892 if (out_rotation)
893 *out_rotation =cxx . . $ automatrix#`operator=`(a0f46581f260c488). rotation.movecxx . . $ automatrix#move(8771494f681de611).();
894 }
895
896 double NUMmsplinecxx . . $ NUMmspline(55addd8cdfddff3d). (constVECcxx . . $ constVEC# const & knot, integercxx . . $ integer# order, integercxx . . $ integer# i, double x) {
897 const integercxx . . $ integer# nSplines = knot.sizecxx . . $ constvector#size. - order;
898
899 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nSplines > 0,
900 U"No splines.");
901 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (order > 0 && i <= nSplines,
902 U"Combination of order and index not correct.");
903 /*
904 Find the interval where x is located.
905 M-splines of order k have degree k-1.
906 M-splines are zero outside interval [ knot [i], knot [i+order] ).
907 First and last 'order' knots are equal, i.e.,
908 knot [1] = ... = knot [order] && knot [knot.size-order+1] = ... knot [knot.size].
909 */
910 integercxx . . $ integer# jj;
911 for (jj = order; jj <= knot.sizecxx . . $ constvector#size. - order + 1; jj ++)
912 if (x < knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).jj])
913 break;
914
915 if (jj < i || (jj > i + order) || jj == order || jj > (knot.sizecxx . . $ constvector#size. - order + 1))
916 return 0.0;
917 /*
918 Calculate M [i](x|1,t) according to eq.2.
919 */
920 const integercxx . . $ integer# ito = i + order - 1;
921 autoVECcxx . . $ autoVEC# m = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (order);
922 for (integercxx . . $ integer# j = i; j <= ito; j ++)
923 if (x >= knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] && x < knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j + 1])
924 m [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - i + 1] = 1 / (knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j + 1] - knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j]);
925 /*
926 Iterate to get M [i](x|k,t)
927 */
928 for (integercxx . . $ integer# k = 2; k <= order; k ++) {
929 for (integercxx . . $ integer# j = i; j <= i + order - k; j ++) {
930 const double kj = knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j], kjpk = knot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j + k];
931 if (kjpk > kj)
932 m [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - i + 1] = k * ((x - kj) * m [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - i + 1] + (kjpk - x) * m [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - i + 1 + 1]) / ((k - 1) * (kjpk - kj));
933 }
934 }
935 return m [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
936 }
937
938 double NUMisplinecxx . . $ NUMispline(55addd8cdfddff3d). (constVECcxx . . $ constVEC# const & aknot, integercxx . . $ integer# order, integercxx . . $ integer# i, double x) {
939 const integercxx . . $ integer# orderp1 = order + 1;
940 integercxx . . $ integer# j;
941 for (j = orderp1; j <= aknot.sizecxx . . $ constvector#size. - order; j ++)
942 if (x < aknot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j])
943 break;
944
945 if (-- j < i)
946 return 0.0;
947
948 if (j > i + order || (j == aknot.sizecxx . . $ constvector#size. - order && x == aknot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j]))
949 return 1.0;
950
951 /*
952 Equation 5 in Ramsay's article contains some errors!!!
953 1. the interval selection should be 'j-k <= i <= j' instead of
954 j-k+1 <= i <= j'
955 2. the summation index m starts at 'i+1' instead of 'i'
956 */
957 double y = 0.0;
958 for (integercxx . . $ integer# m = i + 1; m <= j; m ++) {
959 const double r = NUMmsplinecxx . . $ NUMmspline(55addd8cdfddff3d). (aknot, orderp1, m, x);
960 y += (aknot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).m + orderp1] - aknot [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).m]) * r;
961 }
962 y /= orderp1;
963 return y;
964 }
965
966 double NUMwilksLambdacxx . . $ NUMwilksLambda(c8387802e749a904). (constVECcxx . . $ constVEC# const& lambda, integercxx . . $ integer# from, integercxx . . $ integer# to) {
967 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (from > 0 && to <= lambda.size && from <= to);
968 longdoublecxx . . $ longdouble# result = 1.0;
969 for (integercxx . . $ integer# i = from; i <= to; i ++)
970 result *= 1.0 / (1.0 + lambda [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]);
971 return (double) result;
972 }
973
974 double NUMfactlncxx . . $ NUMfactln(66f4a6ec74012662). (integercxx . . $ integer# n) {
975 static double table [101];
976 if (n < 0)
977 return undefinedcxx . . $ undefined.;
978 if (n <= 1)
979 return 0.0;
980 return n > 100 ? NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (n + 1.0) : table [n] != 0.0 ? table [n] : (table [n] = NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (n + 1.0));
981 }
982
983 double NUMnrbiscxx . . $ NUMnrbis(928da06fce274b3e). (double (*f) (double x, double *dfx, void *closure), double xmin, double xmax, void *closure) {
984 double df, tmp, xh, xl, tol;
985 const integercxx . . $ integer# itermax = 80;
986 double fl = (*f) (xmin, & df, closure);
987 if (fl == 0.0)
988 return xmin;;
989
990 double fh = (*f) (xmax, & df, closure);
991 if (fh == 0.0)
992 return xmax;
993
994 if ((fl > 0.0 && fh > 0.0) || (fl < 0.0 && fh < 0.0))
995 return undefinedcxx . . $ undefined.;
996
997 if (fl < 0.0) {
998 xl = xmin;
999 xh = xmax;
1000 } else {
1001 xh = xmin;
1002 xl = xmax;
1003 }
1004
1005 double dxold = fabscxx . . $ fabs(fee4f1d22bc8e617). (xmax - xmin);
1006 double dx = dxold;
1007 double root = 0.5 * (xmin + xmax);
1008 double fx = (*f) (root, & df, closure);
1009
1010 for (integercxx . . $ integer# iter = 1; iter <= itermax; iter ++) {
1011 if ((((root - xh) * df - fx) * ((root - xl) * df - fx) >= 0.0) || (fabscxx . . $ fabs(fee4f1d22bc8e617). (2.0 * fx) > fabscxx . . $ fabs(fee4f1d22bc8e617). (dxold * df))) {
1012 dxold = dx;
1013 dx = 0.5 * (xh - xl);
1014 root = xl + dx;
1015 if (xl == root)
1016 return root;
1017 } else {
1018 dxold = dx;
1019 dx = fx / df;
1020 tmp = root;
1021 root -= dx;
1022 if (tmp == root)
1023 return root;
1024 }
1025 tol = NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps. * (root == 0.0 ? 1.0 : fabscxx . . $ fabs(fee4f1d22bc8e617). (root));
1026 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (dx) < tol)
1027 return root;
1028
1029 fx = (*f) (root, & df, closure);
1030
1031 if (fx < 0.0)
1032 xl = root;
1033 else
1034 xh = root;
1035 }
1036 Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"NUMnrbis: maximum number of iterations (", itermax, U") exceeded.");
1037 return root;
1038 }
1039
1040 double NUMridderscxx . . $ NUMridders(25f8e7b9d159e245). (double (*f) (double x, void *closure), double x1, double x2, void *closure) {
1041 /* There is still a problem with this implementation:
1042 tol may be zero;
1043 */
1044 double d, root = undefinedcxx . . $ undefined., tol;
1045 const integercxx . . $ integer# itermax = 100;
1046
1047 double f1 = f (x1, closure);
1048 if (f1 == 0.0)
1049 return x1;
1050 if (isundefcxx . . $ isundef(f8197d536af80c96). (f1))
1051 return undefinedcxx . . $ undefined.;
1052
1053 double f2 = f (x2, closure);
1054 if (f2 == 0.0)
1055 return x2;
1056 if (isundefcxx . . $ isundef(f8197d536af80c96). (f2))
1057 return undefinedcxx . . $ undefined.;
1058 if ((f1 < 0.0 && f2 < 0.0) || (f1 > 0.0 && f2 > 0.0))
1059 return undefinedcxx . . $ undefined.;
1060
1061 for (integercxx . . $ integer# iter = 1; iter <= itermax; iter ++) {
1062 const double x3 = 0.5 * (x1 + x2);
1063 const double f3 = f (x3, closure);
1064 if (f3 == 0.0)
1065 return x3;
1066 if (isundefcxx . . $ isundef(f8197d536af80c96). (f3))
1067 return undefinedcxx . . $ undefined.;
1068
1069 // New guess: x4 = x3 + (x3 - x1) * sign(f1 - f2) * f3 / sqrt(f3^2 - f1*f2)
1070
1071 d = f3 * f3 - f1 * f2;
1072 if (d < 0.0) {
1073 Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"d < 0 in ridders (iter = ", iter, U").");
1074 return undefinedcxx . . $ undefined.;
1075 }
1076
1077 if (d == 0.0) {
1078 // pb test added because f1 f2 f3 may be 1e-170 or so
1079 tol = NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps. * (x3 == 0.0 ? 1.0 : fabscxx . . $ fabs(fee4f1d22bc8e617). (x3));
1080 if (iter > 1 && fabscxx . . $ fabs(fee4f1d22bc8e617). (x3 - root) < tol)
1081 return root;
1082 root = x3;
1083
1084 // Perform bisection.
1085
1086 if (f1 > 0.0) {
1087 // falling curve: f1 > 0, f2 < 0
1088 if (f3 > 0.0) {
1089 x1 = x3;
1090 f1 = f3; // retain invariant: f1 > 0, f2 < 0
1091 } else {
1092 // f3 <= 0.0
1093 x2 = x3;
1094 f2 = f3; // retain invariant: f1 > 0, f2 < 0
1095 }
1096 } else {
1097 // rising curve: f1 < 0, f2 > 0
1098 if (f3 > 0.0) {
1099 x2 = x3;
1100 f2 = f3; // retain invariant: f1 < 0, f2 > 0
1101 } else {
1102 // f3 < 0.0
1103 x1 = x3;
1104 f1 = f3; // retain invariant: f1 < 0, f2 > 0
1105 }
1106 }
1107 } else {
1108 d = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (d);
1109 if (isnancxx . . $ isnan(f8197d536af80c96). (d)) {
1110 // pb: square root of denormalized small number fails on some computers
1111 tol = NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps. * (x3 == 0.0 ? 1.0 : fabscxx . . $ fabs(fee4f1d22bc8e617). (x3));
1112 if (iter > 1 && fabscxx . . $ fabs(fee4f1d22bc8e617). (x3 - root) < tol)
1113 return root;
1114 root = x3;
1115
1116 // Perform bisection.
1117
1118 if (f1 > 0.0) {
1119 // falling curve: f1 > 0, f2 < 0
1120 if (f3 > 0.0) {
1121 x1 = x3;
1122 f1 = f3; // retain invariant: f1 > 0, f2 < 0
1123 } else {
1124 // f3 <= 0.0
1125 x2 = x3;
1126 f2 = f3; // retain invariant: f1 > 0, f2 < 0
1127 }
1128 } else {
1129 // rising curve: f1 < 0, f2 > 0
1130 if (f3 > 0.0) {
1131 x2 = x3;
1132 f2 = f3; // retain invariant: f1 < 0, f2 > 0
1133 } else {
1134 // f3 < 0.0
1135 x1 = x3;
1136 f1 = f3; // retain invariant: f1 < 0, f2 > 0 */
1137 }
1138 }
1139 } else {
1140 d = (x3 - x1) * f3 / d;
1141 const double x4 = f1 - f2 < 0 ? x3 - d : x3 + d;
1142 tol = NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps. * (x4 == 0.0 ? 1.0 : fabscxx . . $ fabs(fee4f1d22bc8e617). (x4));
1143 if (iter > 1 && fabscxx . . $ fabs(fee4f1d22bc8e617). (x4 - root) < tol)
1144 return root;
1145 root = x4;
1146 const double f4 = f (x4, closure);
1147 if (f4 == 0.0)
1148 return root;
1149 if (isundefcxx . . $ isundef(f8197d536af80c96). (f4))
1150 return undefinedcxx . . $ undefined.;
1151 if ((f1 > f2) == (d > 0.0)) { // pb: instead of x3 < x4
1152 if (SIGNcxx . . $ `dwsys/NUM2.cpp:87:9`! (f3, f4) != f3) {
1153 x1 = x3;
1154 f1 = f3;
1155 x2 = x4;
1156 f2 = f4;
1157 } else {
1158 x1 = x4;
1159 f1 = f4;
1160 }
1161 } else {
1162 if (SIGNcxx . . $ `dwsys/NUM2.cpp:87:9`! (f3, f4) != f3) {
1163 x1 = x4;
1164 f1 = f4;
1165 x2 = x3;
1166 f2 = f3;
1167 } else {
1168 x2 = x4;
1169 f2 = f4;
1170 }
1171 }
1172 }
1173 }
1174 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (x1 - x2) < tol)
1175 return root;
1176 }
1177
1178 {
1179 static integercxx . . $ integer# nwarnings = 0;
1180 nwarnings ++;
1181 Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"NUMridders: maximum number of iterations (", itermax, U") exceeded.");
1182 }
1183 return root;
1184 }
1185
1186 double NUMlogNormalPcxx . . $ NUMlogNormalP(7678bba071383ab). (double x, double zeta, double sigma) {
1187 return gsl_cdf_lognormal_P (x, zeta, sigma);
1188 }
1189
1190 double NUMlogNormalQcxx . . $ NUMlogNormalQ(7678bba071383ab). (double x, double zeta, double sigma) {
1191 return gsl_cdf_lognormal_Q (x, zeta, sigma);
1192 }
1193
1194 double NUMstudentPcxx . . $ NUMstudentP(d8e7e0733fa41c87). (double t, double df) {
1195 if (df < 1.0) return undefinedcxx . . $ undefined.;
1196 const double ib = NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (0.5 * df, 0.5, df / (df + t * t));
1197 if (isundefcxx . . $ isundef(f8197d536af80c96). (ib))
1198 return undefinedcxx . . $ undefined.;
1199 return t < 0.0 ? 0.5 * ib : 1.0 - 0.5 * ib;
1200 }
1201
1202 double NUMstudentQcxx . . $ NUMstudentQ(d8e7e0733fa41c87). (double t, double df) {
1203 if (df < 1)
1204 return undefinedcxx . . $ undefined.;
1205 const double ib = NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (0.5 * df, 0.5, df / (df + t * t));
1206 if (isundefcxx . . $ isundef(f8197d536af80c96). (ib))
1207 return undefinedcxx . . $ undefined.;
1208 return t > 0.0 ? 0.5 * ib : 1.0 - 0.5 * ib;
1209 }
1210
1211 double NUMfisherPcxx . . $ NUMfisherP(7678bba071383ab). (double f, double df1, double df2) {
1212 if (f < 0.0 || df1 < 1.0 || df2 < 1.0)
1213 return undefinedcxx . . $ undefined.;
1214 const double ib = NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (0.5 * df2, 0.5 * df1, df2 / (df2 + f * df1));
1215 if (isundefcxx . . $ isundef(f8197d536af80c96). (ib))
1216 return undefinedcxx . . $ undefined.;
1217 return 1.0 - ib;
1218 }
1219
1220 double NUMfisherQcxx . . $ NUMfisherQ(7678bba071383ab). (double f, double df1, double df2) {
1221 if (f < 0.0 || df1 < 1.0 || df2 < 1.0)
1222 return undefinedcxx . . $ undefined.;
1223 if (Melder_debugcxx . . $ Melder_debug. == 28) {
1224 return NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (0.5 * df2, 0.5 * df1, df2 / (df2 + f * df1));
1225 } else {
1226 const double result = gsl_cdf_fdist_Q (f, df1, df2);
1227 if (isnancxx . . $ isnan(f8197d536af80c96). (result))
1228 return undefinedcxx . . $ undefined.;
1229 return result;
1230 }
1231 }
1232
1233 double NUMinvGaussQcxx . . $ NUMinvGaussQ(447c67c6676f75c3). (double p) {
1234 return gsl_cdf_ugaussian_Qinv (p);
1235 }
1236
1237 static double studentQ_funccxx . . $ studentQ_func(81b005370e6958c4). (double x, void *voidParams) {
1238 const struct pdf1_structcxx . . $ pdf1_struct# *params = (struct pdf1_structcxx . . $ pdf1_struct# *) voidParams;
1239 const double q = NUMstudentQcxx . . $ NUMstudentQ(d8e7e0733fa41c87). (x, params -> dfcxx . . $ pdf1_struct#df.);
1240 return isundefcxx . . $ isundef(f8197d536af80c96). (q) ? undefinedcxx . . $ undefined. : q - params -> pcxx . . $ pdf1_struct#p.;
1241 }
1242
1243 double NUMinvStudentQcxx . . $ NUMinvStudentQ(d8e7e0733fa41c87). (double p, double df) {
1244 struct pdf1_structcxx . . $ pdf1_struct# params;
1245 const double pc = ( p > 0.5 ? 1.0 - p : p );
1246
1247 if (p < 0.0 || p >= 1.0)
1248 return undefinedcxx . . $ undefined.;
1249 /*
1250 Bracket the function f(x) = NUMstudentQ (x, df) - p.
1251 */
1252 double xmax = 1.0;
1253 for (;;) {
1254 const double q = NUMstudentQcxx . . $ NUMstudentQ(d8e7e0733fa41c87). (xmax, df);
1255 if (isundefcxx . . $ isundef(f8197d536af80c96). (q))
1256 return undefinedcxx . . $ undefined.;
1257 if (q < pc)
1258 break;
1259 xmax *= 2.0;
1260 }
1261
1262 const double xmin = ( xmax > 1.0 ? xmax / 2.0 : 0.0 );
1263 /*
1264 Find zero of f(x) with Ridders' method.
1265 */
1266 params. dfcxx . . $ pdf1_struct#df. = df;
1267 params. pcxx . . $ pdf1_struct#p. = pc;
1268 const double x = NUMridderscxx . . $ NUMridders(25f8e7b9d159e245). (studentQ_funccxx . . $ studentQ_func(81b005370e6958c4)., xmin, xmax, & params);
1269 if (isundefcxx . . $ isundef(f8197d536af80c96). (x))
1270 return undefinedcxx . . $ undefined.;
1271 return p > 0.5 ? -x : x;
1272 }
1273
1274 static double chiSquareQ_funccxx . . $ chiSquareQ_func(81b005370e6958c4). (double x, void *voidParams) {
1275 const struct pdf1_structcxx . . $ pdf1_struct# *params = (struct pdf1_structcxx . . $ pdf1_struct# *) voidParams;
1276 const double q = NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (x, params -> dfcxx . . $ pdf1_struct#df.);
1277 return isundefcxx . . $ isundef(f8197d536af80c96). (q) ? undefinedcxx . . $ undefined. : q - params -> pcxx . . $ pdf1_struct#p.;
1278 }
1279
1280 double NUMinvChiSquareQcxx . . $ NUMinvChiSquareQ(d8e7e0733fa41c87). (double p, double df) {
1281 struct pdf1_structcxx . . $ pdf1_struct# params;
1282
1283 if (p < 0.0 || p >= 1.0)
1284 return undefinedcxx . . $ undefined.;
1285 /*
1286 Bracket the function f(x) = NUMchiSquareQ (x, df) - p.
1287 */
1288 double xmax = 1.0;
1289 for (;;) {
1290 const double q = NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (xmax, df);
1291 if (isundefcxx . . $ isundef(f8197d536af80c96). (q))
1292 return undefinedcxx . . $ undefined.;
1293 if (q < p)
1294 break;
1295 xmax *= 2.0;
1296 }
1297 const double xmin = ( xmax > 1.0 ? xmax / 2.0 : 0.0 );
1298 /*
1299 Find zero of f(x) with Ridders' method.
1300 */
1301 params. dfcxx . . $ pdf1_struct#df. = df;
1302 params. pcxx . . $ pdf1_struct#p. = p;
1303 return NUMridderscxx . . $ NUMridders(25f8e7b9d159e245). (chiSquareQ_funccxx . . $ chiSquareQ_func(81b005370e6958c4)., xmin, xmax, & params);
1304 }
1305
1306 static double fisherQ_funccxx . . $ fisherQ_func(81b005370e6958c4). (double x, void *voidParams) {
1307 struct pdf2_structcxx . . $ pdf2_struct# *params = (struct pdf2_structcxx . . $ pdf2_struct# *) voidParams;
1308 const double q = NUMfisherQcxx . . $ NUMfisherQ(7678bba071383ab). (x, params -> df1cxx . . $ pdf2_struct#df1., params -> df2cxx . . $ pdf2_struct#df2.);
1309 return ( isundefcxx . . $ isundef(f8197d536af80c96). (q) ? undefinedcxx . . $ undefined. : q - params -> pcxx . . $ pdf2_struct#p. );
1310 }
1311
1312 double NUMinvFisherQcxx . . $ NUMinvFisherQ(7678bba071383ab). (double p, double df1, double df2) {
1313 if (p <= 0.0 || p > 1.0 || df1 < 1.0 || df2 < 1.0)
1314 return undefinedcxx . . $ undefined.;
1315 if (Melder_debugcxx . . $ Melder_debug. == 29) {
1316 //if (p == 1.0) return 0.0;
1317 return gsl_cdf_fdist_Qinv (p, df1, df2);
1318 } else {
1319 struct pdf2_structcxx . . $ pdf2_struct# params;
1320 if (p == 1.0)
1321 return 0.0;
1322 params. pcxx . . $ pdf2_struct#p. = p;
1323 params. df1cxx . . $ pdf2_struct#df1. = df1;
1324 params. df2cxx . . $ pdf2_struct#df2. = df2;
1325 double top = 1000.0;
1326 for (;;) {
1327 const double q = NUMfisherQcxx . . $ NUMfisherQ(7678bba071383ab). (top, df1, df2);
1328 if (isundefcxx . . $ isundef(f8197d536af80c96). (q))
1329 return undefinedcxx . . $ undefined.;
1330 if (q < p)
1331 break;
1332 if (top > 0.9e300)
1333 return undefinedcxx . . $ undefined.;
1334 top *= 1e9;
1335 }
1336 return NUMridderscxx . . $ NUMridders(25f8e7b9d159e245). (fisherQ_funccxx . . $ fisherQ_func(81b005370e6958c4)., 0.0, p > 0.5 ? 2.2 : top, & params);
1337 }
1338 }
1339
1340 double NUMbeta2cxx . . $ NUMbeta2(d8e7e0733fa41c87). (double z, double w) {
1341 gsl_sf_result result;
1342 const integercxx . . $ integer# status = gsl_sf_beta_e (z, w, &result);
1343 return status == GSL_SUCCESS ? result.val : undefinedcxx . . $ undefined.;
1344 }
1345
1346 double NUMlnBetacxx . . $ NUMlnBeta(d8e7e0733fa41c87). (double a, double b) {
1347 gsl_sf_result result;
1348 const integercxx . . $ integer# status = gsl_sf_lnbeta_e (a, b, & result);
1349 return status == GSL_SUCCESS ? result.val : undefinedcxx . . $ undefined.;
1350 }
1351
1352 /*************** Hz <--> other freq reps *********************/
1353
1354 double NUMmelToHertz3cxx . . $ NUMmelToHertz3(447c67c6676f75c3). (double mel) {
1355 if (mel < 0.0)
1356 return undefinedcxx . . $ undefined.;
1357 return mel < 1000.0 ? mel : 1000.0 * (expcxx . . $ exp(fee4f1d22bc8e617). (mel * log10cxx . . $ log10(fee4f1d22bc8e617). (2.0) / 1000.0) - 1.0);
1358 }
1359
1360 double NUMhertzToMel3cxx . . $ NUMhertzToMel3(447c67c6676f75c3). (double hz) {
1361 if (hz < 0.0)
1362 return undefinedcxx . . $ undefined.;
1363 return hz < 1000.0 ? hz : 1000.0 * log10cxx . . $ log10(fee4f1d22bc8e617). (1.0 + hz / 1000.0) / log10cxx . . $ log10(fee4f1d22bc8e617). (2.0);
1364 }
1365
1366 double NUMmelToHertz2cxx . . $ NUMmelToHertz2(447c67c6676f75c3). (double mel) {
1367 if (mel < 0.0)
1368 return undefinedcxx . . $ undefined.;
1369 return 700.0 * (powcxx . . $ pow(b71928d39fde4295). (10.0, mel / 2595.0) - 1.0);
1370 }
1371
1372 double NUMhertzToMel2cxx . . $ NUMhertzToMel2(447c67c6676f75c3). (double hz) {
1373 if (hz < 0.0)
1374 return undefinedcxx . . $ undefined.;
1375 return 2595.0 * log10cxx . . $ log10(fee4f1d22bc8e617). (1.0 + hz / 700.0);
1376 }
1377
1378 double NUMhertzToBark_traunmuellercxx . . $ NUMhertzToBark_traunmueller(447c67c6676f75c3). (double hz) {
1379 if (hz < 0.0)
1380 return undefinedcxx . . $ undefined.;
1381 return 26.81 * hz / (1960.0 + hz) - 0.53;
1382 }
1383
1384 double NUMbarkToHertz_traunmuellercxx . . $ NUMbarkToHertz_traunmueller(447c67c6676f75c3). (double bark) {
1385 if (bark < 0.0 || bark > 26.28)
1386 return undefinedcxx . . $ undefined.;
1387 return 1960.0 * (bark + 0.53) / (26.28 - bark);
1388 }
1389
1390
1391 double NUMbarkToHertz_zwickerterhardtcxx . . $ NUMbarkToHertz_zwickerterhardt(447c67c6676f75c3). (double hz) {
1392 if (hz < 0.0)
1393 return undefinedcxx . . $ undefined.;
1394 return 13.0 * atancxx . . $ atan(fee4f1d22bc8e617). (0.00076 * hz) + 3.5 * atancxx . . $ atan(fee4f1d22bc8e617). (hz / 7500.0);
1395 }
1396
1397 double NUMbladonlindblomfilter_amplitudecxx . . $ NUMbladonlindblomfilter_amplitude(d8e7e0733fa41c87). (double zc, double z) {
1398 const double dz = zc - z + 0.474;
1399 return powcxx . . $ pow(b71928d39fde4295). (10.0, 1.581 + 0.75 * dz - 1.75 * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (1.0 + dz * dz));
1400 }
1401
1402 double NUMsekeyhansonfilter_amplitudecxx . . $ NUMsekeyhansonfilter_amplitude(d8e7e0733fa41c87). (double zc, double z) {
1403 const double dz = zc - z - 0.215;
1404 return powcxx . . $ pow(b71928d39fde4295). (10.0, 0.7 - 0.75 * dz - 1.75 * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (0.196 + dz * dz));
1405 }
1406
1407 double NUMtriangularfilter_amplitudecxx . . $ NUMtriangularfilter_amplitude(6ba6c9f826937c6e). (double fl, double fc, double fh, double f) {
1408 double a = 0.0;
1409 if (f > fl && f < fh) {
1410 a = f < fc ? (f - fl) / (fc - fl) : (fh - f) / (fh - fc);
1411
1412 /* Normalize such that area under the filter is always 1. ???
1413
1414 a /= 2 * (fh - fl);*/
1415 }
1416 return a;
1417 }
1418
1419 double NUMformantfilter_amplitudecxx . . $ NUMformantfilter_amplitude(7678bba071383ab). (double fc, double bw, double f) {
1420 const double dq = (fc * fc - f * f) / (bw * f);
1421 return 1.0 / (dq * dq + 1.0);
1422 }
1423
1424 /* Childers (1978), Modern Spectrum analysis, IEEE Press, 252-255) */
1425 /* work [1..n+n+n];
1426 b1 = & work [1];
1427 b2 = & work [n+1];
1428 aa = & work [n+n+1];
1429 for (i=1; i<=n+n+n; i ++) work [i]=0;
1430 */
1431 double VECburgcxx . . $ VECburg(5935962a19f9e6ab). (VECcxx . . $ VEC# const& a, constVECcxx . . $ constVEC# const& x) {
1432 const integercxx . . $ integer# n = x.sizecxx . . $ constvector#size., m = a.sizecxx . . $ vector#size.;
1433 for (integercxx . . $ integer# j = 1; j <= m; j ++)
1434 a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = 0.0;
1435 if (n <= 2) {
1436 a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = -1.0;
1437 return ( n == 2 ? 0.5 * (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] + x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2] * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2]) : x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] );
1438 }
1439
1440 autoVECcxx . . $ autoVEC# b1 = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (n), b2 = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (n), aa = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (m);
1441
1442 // (3)
1443
1444 longdoublecxx . . $ longdouble# p = 0.0;
1445 for (integercxx . . $ integer# j = 1; j <= n; j ++)
1446 p += x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j];
1447
1448 longdoublecxx . . $ longdouble# xms = p / n;
1449 if (xms <= 0.0)
1450 return xms; // warning empty
1451
1452 // (9)
1453
1454 b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1];
1455 if (n < 2)
1456 return (double) xms;
1457 b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).n - 1] = x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).n];
1458 for (integercxx . . $ integer# j = 2; j <= n - 1; j ++)
1459 b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - 1] = x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j];
1460
1461 for (integercxx . . $ integer# i = 1; i <= m; i ++) {
1462 // (7)
1463
1464 longdoublecxx . . $ longdouble# num = 0.0, denum = 0.0;
1465 for (integercxx . . $ integer# j = 1; j <= n - i; j ++) {
1466 num += b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] * b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j];
1467 denum += b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] * b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] + b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] * b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j];
1468 }
1469
1470 if (denum <= 0.0)
1471 return 0.0; // warning ill-conditioned
1472
1473 a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 2.0 * num / denum;
1474
1475 // (10)
1476
1477 xms *= 1.0 - a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
1478
1479 // (5)
1480
1481 for (integercxx . . $ integer# j = 1; j <= i - 1; j ++)
1482 a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = aa [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] - a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * aa [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - j];
1483
1484 if (i < m) {
1485
1486 // (8) Watch out: i -> i+1
1487
1488 for (integercxx . . $ integer# j = 1; j <= i; j ++)
1489 aa [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j];
1490 for (integercxx . . $ integer# j = 1; j <= n - i - 1; j ++) {
1491 b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] -= aa [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] * b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j];
1492 b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = b2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j + 1] - aa [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] * b1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j + 1];
1493 }
1494 }
1495 }
1496 return xms;
1497 }
1498
1499 autoVECcxx . . $ autoVEC# newVECburgcxx . . $ newVECburg(88f3d1b66fd48bae). (constVECcxx . . $ constVEC# const& x, integercxx . . $ integer# numberOfPredictionCoefficients, double *out_xms) {
1500 autoVECcxx . . $ autoVEC# a = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfPredictionCoefficients);
1501 const double xms = VECburgcxx . . $ VECburg(5935962a19f9e6ab). (a.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), x);
1502 if (out_xms)
1503 *out_xms = xms;
1504 return acxx . . $ autovector#autovector(d33c649a3aceae2f).;
1505 }
1506
1507 void VECfilterInverse_inplacecxx . . $ VECfilterInverse_inplace(90fde51006936352). (VECcxx . . $ VEC# const& s, constVECcxx . . $ constVEC# const& filter, VECcxx . . $ VEC# const& filterMemory) {
1508 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (filterMemory.size >= filter.size);
1509 filterMemorycxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (1, filter.sizecxx . . $ constvector#size.) <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0;
1510 for (integercxx . . $ integer# i = 1; i <= s.sizecxx . . $ vector#size.; i ++) {
1511 const double y0 = s [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
1512 for (integercxx . . $ integer# j = 1; j <= filter.sizecxx . . $ constvector#size.; j ++)
1513 s [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] += filter [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] * filterMemory [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j];
1514 for (integercxx . . $ integer# j = filter.sizecxx . . $ constvector#size.; j > 1; j --)
1515 filterMemory [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = filterMemory [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j - 1];
1516 filterMemory [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = y0;
1517 }
1518 }
1519
1520 void NUMdmatrix_to_dBscxx . . $ NUMdmatrix_to_dBs(42c31a7526bc0965). (MATcxx . . $ MAT# const& m, double ref, double factor, double floor) {
1521 const double factor10 = factor * 10.0;
1522 MelderExtremaWithInitcxx . . $ MelderExtremaWithInit# extrema;
1523
1524 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (ref > 0 && factor > 0);
1525
1526 for (integercxx . . $ integer# irow = 1; irow <= m.nrowcxx . . $ matrix#nrow.; irow ++)
1527 for (integercxx . . $ integer# icol = 1; icol <= m.ncolcxx . . $ matrix#ncol.; icol ++)
1528 extrema.updatecxx . . $ MelderExtremaWithInit#update(ba8836b59fb42830). (m [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol]);
1529
1530
1531 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (extrema.min >= 0.0 && extrema.max >= 0.0,
1532 U"All matrix elements should be positive.");
1533
1534 const double ref_db = factor10 * log10cxx . . $ log10(fee4f1d22bc8e617). (ref);
1535
1536 for (integercxx . . $ integer# irow = 1; irow <= m.nrowcxx . . $ matrix#nrow.; irow ++) {
1537 for (integercxx . . $ integer# icol = 1; icol <= m.ncolcxx . . $ matrix#ncol.; icol ++) {
1538 double mij = floor;
1539 if (m [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] > 0.0) {
1540 mij = factor10 * log10cxx . . $ log10(fee4f1d22bc8e617). (m [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol]) - ref_db;
1541 if (mij < floor)
1542 mij = floor;
1543 }
1544 m [cxx . . $ matrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = mij;
1545 }
1546 }
1547 }
1548
1549 autoMATcxx . . $ autoMAT# MATcosinesTablecxx . . $ MATcosinesTable(bb735016fb5774e0). (integercxx . . $ integer# n) {
1550 autoMATcxx . . $ autoMAT# result = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (n, n);
1551 for (integercxx . . $ integer# irow = 1; irow <= n; irow ++)
1552 for (integercxx . . $ integer# icol = 1; icol <= n; icol ++)
1553 result [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = coscxx . . $ cos(fee4f1d22bc8e617). (NUMpicxx . . $ NUMpi. * (irow - 1) * (icol - 0.5) / n);
1554 return resultcxx . . $ automatrix#automatrix(a69681c49ef9c9eb).;
1555 }
1556
1557 void VECcosineTransform_preallocatedcxx . . $ VECcosineTransform_preallocated(2b6621d50092d283). (VECcxx . . $ VEC# const& target, constVECcxx . . $ constVEC# const& x, constMATcxx . . $ constMAT# const& cosinesTable) {
1558 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (cosinesTable.nrow == cosinesTable.ncol);
1559 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == target.size && x.size == cosinesTable.nrow);
1560 for (integercxx . . $ integer# k = 1; k <= target.sizecxx . . $ vector#size.; k ++)
1561 target [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (xcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf)., cosinesTablecxx . . $ constvectorview#constvectorview(fc39adb4395e9baf)..rowcxx . . $ constmatrix#row(aae4ad074ae650ba). (k));
1562 }
1563
1564 void VECinverseCosineTransform_preallocatedcxx . . $ VECinverseCosineTransform_preallocated(2b6621d50092d283). (VECcxx . . $ VEC# const& target, constVECcxx . . $ constVEC# const& x, constMATcxx . . $ constMAT# const& cosinesTable) {
1565 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (cosinesTable.nrow == cosinesTable.ncol);
1566 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == target.size && x.size == cosinesTable.nrow);
1567 for (integercxx . . $ integer# j = 1; j <= target.sizecxx . . $ vector#size.; j ++) {
1568 target [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = 0.5 * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] * cosinesTable [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).1] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j];
1569 for (integercxx . . $ integer# k = 2; k <= target.sizecxx . . $ vector#size.; k ++)
1570 target [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).k] * cosinesTable [cxx . . $ constmatrix#`operator[]`(aae4ad074ae650ba).k] [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j];
1571 target [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] *= 2.0 / target.sizecxx . . $ vector#size.;
1572 }
1573 }
1574
1575 void NUMcubicSplineInterpolation_getSecondDerivativescxx . . $ NUMcubicSplineInterpolation_getSecondDerivatives(1ccc247dbce018f8). (VECcxx . . $ VEC# const& out_y, constVECcxx . . $ constVEC# const& x, constVECcxx . . $ constVEC# const& y, double yp1, double ypn) {
1576 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == y.size && out_y.size == y.size);
1577
1578 autoVECcxx . . $ autoVEC# u = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (x.sizecxx . . $ constvector#size. - 1);
1579
1580 if (yp1 > 0.99e30)
1581 out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 0.0;
1582 else {
1583 out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = -0.5;
1584 u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = (3.0 / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1])) * ( (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]) - yp1);
1585 }
1586
1587 for (integercxx . . $ integer# i = 2; i <= x.sizecxx . . $ constvector#size. - 1; i ++) {
1588 const double sig = (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1]);
1589 const double p = sig * out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] + 2.0;
1590 out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = (sig - 1.0) / p;
1591 u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]) - (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1]);
1592 u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (6.0 * u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1]) - sig * u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1]) / p;
1593 }
1594
1595 double qn, un;
1596 if (ypn > 0.99e30)
1597 qn = un = 0.0;
1598 else {
1599 qn = 0.5;
1600 un = (3.0 / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).x.sizecxx . . $ constvector#size.] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).x.sizecxx . . $ constvector#size. - 1])) * (ypn - (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).x.sizecxx . . $ constvector#size.] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).x.sizecxx . . $ constvector#size. - 1]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).x.sizecxx . . $ constvector#size.] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).x.sizecxx . . $ constvector#size. - 1]));
1601 }
1602
1603 out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).x.sizecxx . . $ constvector#size.] = (un - qn * u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).x.sizecxx . . $ constvector#size. - 1]) / (qn * out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).x.sizecxx . . $ constvector#size. - 1] + 1.0);
1604 for (integercxx . . $ integer# k = x.sizecxx . . $ constvector#size. - 1; k >= 1; k--)
1605 out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] = out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] * out_y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k + 1] + u [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k];
1606 }
1607
1608 double NUMcubicSplineInterpolationcxx . . $ NUMcubicSplineInterpolation(b0781aae48543720). (constVECcxx . . $ constVEC# const& x, constVECcxx . . $ constVEC# const& y, constVECcxx . . $ constVEC# const& y2, double xin) {
1609 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == y.size && x.size == y2.size);
1610 integercxx . . $ integer# klo = 1, khi = x.sizecxx . . $ constvector#size.;
1611 while (khi - klo > 1) {
1612 integercxx . . $ integer# k = (khi + klo) >> 1;
1613 if (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).k] > xin)
1614 khi = k;
1615 else
1616 klo = k;
1617 }
1618 const double h = x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).khi] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).klo];
1619 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (h != 0.0,
1620 U"NUMcubicSplineInterpolation: bad input value.");
1621
1622 const double a = (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).khi] - xin) / h;
1623 const double b = (xin - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).klo]) / h;
1624 const double yint = a * y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).klo] + b * y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).khi] + ((a * a * a - a) * y2 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).klo] + (b * b * b - b) * y2 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).khi]) * (h * h) / 6.0;
1625 return yint;
1626 }
1627
1628 double NUMsinccxx . . $ NUMsinc(447c67c6676f75c3). (const double x) {
1629 struct gsl_sf_result_struct result;
1630 const integercxx . . $ integer# status = gsl_sf_sinc_e (x / NUMpicxx . . $ NUMpi., &result);
1631 return status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined.;
1632 }
1633
1634 double NUMsincpicxx . . $ NUMsincpi(447c67c6676f75c3). (const double x) {
1635 struct gsl_sf_result_struct result;
1636 const integercxx . . $ integer# status = gsl_sf_sinc_e (x, &result);
1637 return status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined.;
1638 }
1639
1640 /* Does the line segment from (x1,y1) to (x2,y2) intersect with the line segment from (x3,y3) to (x4,y4)? */
1641 bool NUMdoLineSegmentsIntersectcxx . . $ NUMdoLineSegmentsIntersect(e8998c524a6963da). (double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
1642 const integercxx . . $ integer# o11 = NUMgetOrientationOfPointscxx . . $ NUMgetOrientationOfPoints(58038ef257127341). (x1, y1, x2, y2, x3, y3);
1643 const integercxx . . $ integer# o12 = NUMgetOrientationOfPointscxx . . $ NUMgetOrientationOfPoints(58038ef257127341). (x1, y1, x2, y2, x4, y4);
1644 const integercxx . . $ integer# o21 = NUMgetOrientationOfPointscxx . . $ NUMgetOrientationOfPoints(58038ef257127341). (x3, y3, x4, y4, x1, y1);
1645 const integercxx . . $ integer# o22 = NUMgetOrientationOfPointscxx . . $ NUMgetOrientationOfPoints(58038ef257127341). (x3, y3, x4, y4, x2, y2);
1646 return ((o11 * o12 < 0) && (o21 * o22 < 0)) || (o11 *o12 *o21 *o22 == 0);
1647 }
1648
1649 integercxx . . $ integer# NUMgetOrientationOfPointscxx . . $ NUMgetOrientationOfPoints(58038ef257127341). (double x1, double y1, double x2, double y2, double x3, double y3) {
1650 integercxx . . $ integer# orientation;
1651 const longdoublecxx . . $ longdouble# dx2 = x2 - x1, dy2 = y2 - y1;
1652 const longdoublecxx . . $ longdouble# dx3 = x3 - x1, dy3 = y3 - y1;
1653 if (dx2 * dy3 > dy2 * dx3)
1654 orientation = -1;
1655 else if (dx2 * dy3 < dy2 * dx3)
1656 orientation = 1;
1657 else {
1658 if ((dx2 * dx3 < 0) || (dy2 * dy3 < 0))
1659 orientation = -1;
1660 else if ((dx2 * dx2 + dy2 * dy2) >= (dx3 * dx3 + dy3 * dy3))
1661 orientation = 0;
1662 else
1663 orientation = 1;
1664 }
1665 return orientation;
1666 }
1667
1668 integercxx . . $ integer# NUMgetIntersectionsWithRectanglecxx . . $ NUMgetIntersectionsWithRectangle(427fee5e47058269). (double x1, double y1, double x2, double y2, double xmin, double ymin, double xmax, double ymax, double *xi, double *yi) {
1669 double x [6], y [6];
1670 integercxx . . $ integer# ni = 0;
1671
1672 x [1] = x [4] = x [5] = xmin;
1673 x [2] = x [3] = xmax;
1674 y [1] = y [2] = y [5] = ymin;
1675 y [3] = y [4] = ymax;
1676 /*
1677 Calculate intersection of line segment through p1=(x1,y1) to p2(x2,y2) with line segment
1678 through p3=(x3,y3) to p4=(x4,y4).
1679 Parametrisation of the lines:
1680 l1 = p1 + s (p2 - p1), s in (-inf,+inf)
1681 l2 = p3 + t (p4 - p3), t in (-inf,+inf).
1682 When s and t are in [0, 1] we have line segments between the points.
1683 At the intersection l1 == l2. We get for the x and y coordinates:
1684 x1 + s (x2 - x1) = x3 + t (x4 - x3).............(1)
1685 y1 + s (y2 - y1) = y3 + t (y4 - y3).............(2)
1686 Multiply (1)*(y2 - y1) and (2)*(x2 - x1):
1687 x1 (y2 - y1) + s (x2 - x1)(y2 - y1) = x3 (y2 - y1) + t (x4 - x3)(y2 - y1).......(3)
1688 y1 (x2 - x1) + s (y2 - y1)(x2 - x1) = y3 (x2 - x1) + t (y4 - y3)(x2 - x1).......(4)
1689 (3)-(4) with y21 = y2 -y1, x21 = x2 - x1, x43 = x4 - x3, ...
1690 x1 y21 - y1 x21 = x3 y21 - y3 x21 +t (x43 y21 - y43 x21)
1691 Combining:
1692 y31 x21 - x31 y21 = t (x43 y21 - y43 x21)
1693 Therefore at the intersection we have:
1694
1695 t = (y31 x21 - x31 y21) / (x43 y21 - y43 x21)
1696
1697 If (x43 y21 - y43 x21) == 0
1698 There is no intersection.
1699 If (t < 0 || t >= 1)
1700 No intersection in the segment l2
1701 To count intersections in a corner only once we have t < 0 instead of t <= 0!
1702 */
1703
1704 for (integercxx . . $ integer# i = 1; i <= 4; i ++) {
1705 const double denom = (x [i + 1] - x [i]) * (y2 - y1) - (y [i + 1] - y [i]) * (x2 - x1);
1706 if (denom == 0.0)
1707 continue;
1708
1709 // We have an intersection.
1710
1711 const double t = ((y [i] - y1) * (x2 - x1) - (x [i] - x1) * (y2 - y1)) / denom;
1712 if (t < 0.0 || t >= 1.0)
1713 continue;
1714
1715 // Intersection is within rectangle side.
1716
1717 const double x3 = x [i] + t * (x [i + 1] - x [i]);
1718 const double y3 = y [i] + t * (y [i + 1] - y [i]);
1719
1720 // s must also be valid
1721
1722 const double s = ( x1 != x2 ? (x3 - x1) / (x2 - x1) : (y3 - y1) / (y2 - y1) );
1723 if (s < 0.0 || s >= 1.0)
1724 continue;
1725
1726 ni ++;
1727 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (ni <= 3,
1728 U"Too many intersections.");
1729
1730 xi [ni] = x3;
1731 yi [ni] = y3;
1732 }
1733 return ni;
1734 }
1735
1736 bool NUMclipLineWithinRectanglecxx . . $ NUMclipLineWithinRectangle(dbd26f6c64c2fbf9). (double line_x1, double line_y1, double line_x2, double line_y2, double rect_x1, double rect_y1, double rect_x2, double rect_y2, double *out_line_x1, double *out_line_y1, double *out_line_x2, double *out_line_y2) {
1737 integercxx . . $ integer# numberOfRectangleCrossings = 0;
1738 double x, y, a, b;
1739 double crossing_x [5], crossing_y [5], xmin, xmax, ymin, ymax;
1740 double segment_x1 = line_x1, segment_y1 = line_y1, segment_x2 = line_x2, segment_y2 = line_y2;
1741 /*
1742 This test first because we expect the majority of the tested segments to be within the rectangle
1743 */
1744 if (line_x1 >= rect_x1 && line_x1 <= rect_x2 && line_y1 >= rect_y1 && line_y1 <= rect_y2 &&
1745 line_x2 >= rect_x1 && line_x2 <= rect_x2 && line_y2 >= rect_y1 && line_y2 <= rect_y2)
1746 goto end;
1747 /*
1748 All lines that are completely outside the rectangle
1749 */
1750 if ( (line_x1 <= rect_x1 && line_x2 <= rect_x1) || (line_x1 >= rect_x2 && line_x2 >= rect_x2) ||
1751 (line_y1 <= rect_y1 && line_y2 <= rect_y1) || (line_y1 >= rect_y2 && line_y2 >= rect_y2))
1752 return false;
1753 /*
1754 At least line spans (part of) the rectangle.
1755 Get extremes in x and y of the line for easy testing further on.
1756 */
1757 bool xswap, yswap;
1758 if (line_x1 < line_x2) {
1759 xmin = line_x1;
1760 xmax = line_x2;
1761 xswap = false;
1762 } else {
1763 xmin = line_x2;
1764 xmax = line_x1;
1765 xswap = true;
1766 }
1767 if (line_y1 < line_y2) {
1768 ymin = line_y1;
1769 ymax = line_y2;
1770 yswap = false;
1771 } else {
1772 ymin = line_y2;
1773 ymax = line_y1;
1774 yswap = true;
1775 }
1776
1777 if (line_y1 == line_y2) {
1778 if (xmin < rect_x1)
1779 segment_x1 = rect_x1;
1780 if (xmax > rect_x2)
1781 segment_x2 = rect_x2;
1782 if (xswap)
1783 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (segment_x1, segment_x2);
1784 goto end;
1785 }
1786 if (line_x1 == line_x2) {
1787 if (ymin < rect_y1)
1788 segment_y1 = rect_y1;
1789 if (ymax > rect_y2)
1790 segment_y2 = rect_y2;
1791 if (yswap)
1792 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (segment_y1, segment_y2);
1793 goto end;
1794 }
1795 /*
1796 Now we know that the line from (line_x1,line_y1) to (line_x2,line_y2) is neither horizontal nor vertical.
1797 We can parametrize it as y = ax + b
1798 */
1799 a = (line_y1 - line_y2) / (line_x1 - line_x2);
1800 b = line_y1 - a * line_x1;
1801
1802 /*
1803 To determine the crossings we have to avoid counting the crossings in a corner twice.
1804 Therefore we test the corners inclusive (..<=..<=..) on the vertical borders of the rectangle
1805 and exclusive (..<..<) at the horizontal borders.
1806 */
1807
1808 y = a * rect_x1 + b; // Crossing at y with left border: x = rect_x1
1809
1810 if (y >= rect_y1 && y <= rect_y2 && xmin < rect_x1) { // Within vertical range?
1811 crossing_x [++ numberOfRectangleCrossings] = rect_x1;
1812 crossing_y [numberOfRectangleCrossings] = y;
1813 crossing_x [2] = xmax;
1814 crossing_y [2] = line_x1 > line_x2 ? line_y1 : line_y2;
1815 }
1816
1817 x = (rect_y2 - b) / a; // Crossing at x with top border: y = rect_y2
1818
1819 if (x > rect_x1 && x < rect_x2 && ymax > rect_y2) { // Within horizontal range?
1820 crossing_x [++ numberOfRectangleCrossings] = x;
1821 crossing_y [numberOfRectangleCrossings] = rect_y2;
1822 if (numberOfRectangleCrossings == 1) {
1823 crossing_y [2] = ymin;
1824 crossing_x [2] = line_y1 < line_y2 ? line_x1 : line_x2;
1825 }
1826 }
1827
1828 y = a * rect_x2 + b; // Crossing at y with right border: x = rect_x2
1829
1830 if (y >= rect_y1 && y <= rect_y2 && xmax > rect_x2) { // Within vertical range?
1831 crossing_x [++ numberOfRectangleCrossings] = rect_x2;
1832 crossing_y [numberOfRectangleCrossings] = y;
1833 if (numberOfRectangleCrossings == 1) {
1834 crossing_x [2] = xmin;
1835 crossing_y [2] = line_x1 < line_x2 ? line_y1 : line_y2;
1836 }
1837 }
1838
1839 x = (rect_y1 - b) / a; // Crossing at x with bottom border: y = rect_y1
1840
1841 if (x > rect_x1 && x < rect_x2 && ymin < rect_y1) {
1842 crossing_x [++ numberOfRectangleCrossings] = x;
1843 crossing_y [numberOfRectangleCrossings] = rect_y1;
1844 if (numberOfRectangleCrossings == 1) {
1845 crossing_y [2] = ymax;
1846 crossing_x [2] = line_y1 > line_y2 ? line_x1 : line_x2;
1847 }
1848 }
1849 if (numberOfRectangleCrossings == 0)
1850 return false;
1851 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfRectangleCrossings <= 2,
1852 U"Too many crossings found.");
1853
1854 /*
1855 If start and endpoint of line are outside rectangle and numberOfRectangleCrossings == 1, than the line only touches.
1856 */
1857
1858 if (numberOfRectangleCrossings == 1 && (line_x1 < rect_x1 || line_x1 > rect_x2 || line_y1 < rect_y1 || line_y1 > rect_y2) &&
1859 (line_x2 < rect_x1 || line_x2 > rect_x2 || line_y2 < rect_y1 || line_y2 > rect_y2))
1860 goto end;
1861
1862 if ((crossing_x [1] > crossing_x [2] && ! xswap) || (crossing_x [1] < crossing_x [2] && xswap)) {
1863 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (crossing_x [1], crossing_x [2]);
1864 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (crossing_y [1], crossing_y [2]);
1865 }
1866 segment_x1 = crossing_x [1];
1867 segment_y1 = crossing_y [1];
1868 segment_x2 = crossing_x [2];
1869 segment_y2 = crossing_y [2];
1870
1871 end:
1872
1873 if (out_line_x1)
1874 *out_line_x1 = segment_x1;
1875 if (out_line_y1)
1876 *out_line_y1 = segment_y1;
1877 if (out_line_x2)
1878 *out_line_x2 = segment_x2;
1879 if (out_line_y2)
1880 *out_line_y2 = segment_y2;
1881 return true;
1882 }
1883
1884 void NUMgetEllipseBoundingBoxcxx . . $ NUMgetEllipseBoundingBox(14709244db55ea9c). (double a, double b, double cospsi, double *out_width, double *out_height) {
1885 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (cospsi >= -1.0 && cospsi <= 1.0,
1886 U"NUMgetEllipseBoundingBox: abs (cospi) should not exceed 1.", cospsi);
1887 double width, height;
1888 if (cospsi == 1.0) { // a-axis along x-axis
1889 width = a;
1890 height = b;
1891 } else if (cospsi == 0.0) { // a-axis along y-axis
1892 width = b;
1893 height = a;
1894 } else {
1895 const double psi = acoscxx . . $ acos(fee4f1d22bc8e617). (cospsi), sinpsi = sincxx . . $ sin(fee4f1d22bc8e617). (psi);
1896 double phi = atan2cxx . . $ atan2(b71928d39fde4295). (-b * sinpsi, a * cospsi);
1897 width = fabscxx . . $ fabs(fee4f1d22bc8e617). (a * cospsi * coscxx . . $ cos(fee4f1d22bc8e617). (phi) - b * sinpsi * sincxx . . $ sin(fee4f1d22bc8e617). (phi));
1898 phi = atan2cxx . . $ atan2(b71928d39fde4295). (b * cospsi, a * sinpsi);
1899 height = fabscxx . . $ fabs(fee4f1d22bc8e617). (a * sinpsi * coscxx . . $ cos(fee4f1d22bc8e617). (phi) + b * cospsi * sincxx . . $ sin(fee4f1d22bc8e617). (phi));
1900 }
1901 if (out_width)
1902 *out_width = width;
1903 if (out_height)
1904 *out_height = height;
1905 }
1906
1907 /*
1908 Closely modeled after the netlib code by Oleg Keselyov.
1909 */
1910 double NUMminimize_brentcxx . . $ NUMminimize_brent(132a2f8a908660df). (double (*f) (double x, void *closure), double a, double b, void *closure, double tol, double *fx) {
1911 double x, v, fv, w, fw;
1912 const double golden = 1 - NUM_goldenSectioncxx . . $ NUM_goldenSection.;
1913 const double sqrt_epsilon = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps.);
1914 const integercxx . . $ integer# itermax = 60;
1915
1916 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (tol > 0 && a < b);
1917
1918 /*
1919 First step - golden section
1920 */
1921 v = a + golden * (b - a);
1922 fv = (*f) (v, closure);
1923 x = v;
1924 w = v;
1925 *fx = fv;
1926 fw = fv;
1927
1928 for (integercxx . . $ integer# iter = 1; iter <= itermax; iter ++) {
1929 const double middle_range = (a + b) / 2.0;
1930 const double tol_act = sqrt_epsilon * fabscxx . . $ fabs(fee4f1d22bc8e617). (x) + tol / 3.0;
1931 double range = b - a;
1932 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (x - middle_range) + range / 2.0 <= 2.0 * tol_act)
1933 return x;
1934
1935 // Obtain the golden section step
1936
1937 double new_step = golden * (x < middle_range ? b - x : a - x);
1938
1939 // Decide if the parabolic interpolation can be tried
1940
1941 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (x - w) >= tol_act) {
1942 /*
1943 Interpolation step is calculated as p/q;
1944 division operation is delayed until last moment.
1945 */
1946
1947 const double t = (x - w) * (*fx - fv);
1948 double q = (x - v) * (*fx - fw);
1949 double p = (x - v) * q - (x - w) * t;
1950 q = 2.0 * (q - t);
1951
1952 if (q > 0.0)
1953 p = -p;
1954 else
1955 q = -q;
1956
1957 /*
1958 If x+p/q falls in [a,b], not too close to a and b,
1959 and isn't too large, it is accepted.
1960 If p/q is too large then the golden section procedure can
1961 reduce [a,b] range.
1962 */
1963
1964 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (p) < fabscxx . . $ fabs(fee4f1d22bc8e617). (new_step * q) &&
1965 p > q * (a - x + 2.0 * tol_act) &&
1966 p < q * (b - x - 2.0 * tol_act)) {
1967 new_step = p / q;
1968 }
1969 }
1970
1971 // Adjust the step to be not less than tolerance.
1972
1973 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (new_step) < tol_act)
1974 new_step = new_step > 0.0 ? tol_act : - tol_act;
1975
1976 // Obtain the next approximation to min and reduce the enveloping range
1977
1978 {
1979 const double t = x + new_step; // Tentative point for the min
1980 const double ft = (*f) (t, closure);
1981
1982 /*
1983 If t is a better approximation, reduce the range so that
1984 t would fall within it. If x remains the best, reduce the range
1985 so that x falls within it.
1986 */
1987
1988 if (ft <= *fx) {
1989 if (t < x)
1990 b = x;
1991 else
1992 a = x;
1993
1994 v = w;
1995 w = x;
1996 x = t;
1997 fv = fw;
1998 fw = *fx;
1999 *fx = ft;
2000 } else {
2001 if (t < x)
2002 a = t;
2003 else
2004 b = t;
2005
2006 if (ft <= fw || w == x) {
2007 v = w;
2008 w = t;
2009 fv = fw;
2010 fw = ft;
2011 } else if (ft <= fv || v == x || v == w) {
2012 v = t;
2013 fv = ft;
2014 }
2015 }
2016 }
2017 }
2018 Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"NUMminimize_brent: maximum number of iterations (", itermax, U") exceeded.");
2019 return x;
2020 }
2021
2022 /*
2023 probs is probability vector, i.e. all 0 <= probs [i] <= 1 and sum(i=1...probs.size, probs [i])= 1
2024 */
2025 integercxx . . $ integer# NUMgetIndexFromProbabilitycxx . . $ NUMgetIndexFromProbability(38981f9d60616ce2). (constVECcxx . . $ constVEC# probs, double p) {
2026 integercxx . . $ integer# index = 1;
2027 double psum = probs [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).index];
2028 while (p > psum && index < probs.sizecxx . . $ constvector#size.)
2029 psum += probs [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).++ index];
2030 return index;
2031 }
2032
2033 // straight line fitting
2034
2035 void NUMfitLine_theilcxx . . $ NUMfitLine_theil(c6f3b2557f11da6e). (constVECcxx . . $ constVEC# const& x, constVECcxx . . $ constVEC# const& y, double *out_m, double *out_intercept, bool completeMethod) {
2036 try {
2037 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (x.size == y.size,
2038 U"NUMfitLine_theil: the sizes of the two vectors should be equal.");
2039 /*
2040 Theil's incomplete method:
2041 Split (x [i],y [i]) as
2042 (x [i],y [i]), (x [N+i],y [N+i], i=1..numberOfPoints/2
2043 m [i] = (y [N+i]-y [i])/(x [N+i]-x [i])
2044 m = median (m [i])
2045 b = median(y [i]-m*x [i])
2046 */
2047 autoVECcxx . . $ autoVEC# lineParameters = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (6);
2048 NUMfitLine_theil_preallocatedcxx . . $ NUMfitLine_theil_preallocated(a1eca6a1572b75dc). (lineParameters.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), x, y, out_intercept, 0.025, completeMethod);
2049
2050 if (out_m)
2051 *out_m = lineParameters [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
2052 if (out_intercept)
2053 *out_intercept = lineParameters [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).4];
2054 } catch (MelderErrorcxx . . $ MelderError#) {
2055 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No line fit (Theil's method).");
2056 }
2057 }
2058
2059 void NUMfitLine_theil_preallocatedcxx . . $ NUMfitLine_theil_preallocated(a1eca6a1572b75dc). (VECcxx . . $ VEC# const& out_lineParameters, constVECcxx . . $ constVEC# const& x, constVECcxx . . $ constVEC# const& y, bool wantIntercept, double oneTailedUnconfidence, bool completeMethod) {
2060 try {
2061 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (x.size == y.size,
2062 U"NUMfitLine_theil: the sizes of the two vectors should be equal.");
2063 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (out_lineParameters.size == 6,
2064 U"The line parameters vector should have size 6.");
2065 /*
2066 Theil's incomplete method:
2067 Split (x [i],y [i]) as
2068 (x [i],y [i]), (x [N+i],y [N+i], i=1..numberOfPoints/2
2069 m [i] = (y [N+i]-y [i])/(x [N+i]-x [i])
2070 m = median (m [i])
2071 b = median(y [i]-m*x [i])
2072 */
2073 out_lineParameterscxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(c555d170d72ff256). undefinedcxx . . $ undefined.;
2074 if (x.sizecxx . . $ constvector#size. == 1) {
2075 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = 0.0; // m
2076 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).4] = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]; // intercept
2077 } else if (x.sizecxx . . $ constvector#size. == 2) {
2078 const double m = out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).2] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1]);
2079 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).4] = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] - m * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1];
2080 } else {
2081 integercxx . . $ integer# numberOfCombinations;
2082 autoVECcxx . . $ autoVEC# mbscxx . . $ autovector#autovector(49f6e7a06ebc5aa8).;
2083 if (! completeMethod) {
2084 numberOfCombinations = x.sizecxx . . $ constvector#size. / 2;
2085 mbs =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (x.sizecxx . . $ constvector#size.); // allocate for the intercept calculation too
2086 integercxx . . $ integer# n2 = x.sizecxx . . $ constvector#size. % 2 == 1 ? numberOfCombinations + 1 : numberOfCombinations;
2087 for (integercxx . . $ integer# i = 1; i <= numberOfCombinations; i ++)
2088 mbs [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).n2 + i] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).n2 + i] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]);
2089 } else { // use all combinations
2090 numberOfCombinations = (x.sizecxx . . $ constvector#size. - 1) * x.sizecxx . . $ constvector#size. / 2;
2091 mbs =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfCombinations);
2092 integercxx . . $ integer# index = 0;
2093 for (integercxx . . $ integer# i = 1; i < x.sizecxx . . $ constvector#size.; i ++)
2094 for (integercxx . . $ integer# j = i + 1; j <= x.sizecxx . . $ constvector#size.; j ++)
2095 mbs [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ index] = (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] - y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]) / (x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] - x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]);
2096 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (index == numberOfCombinations);
2097 }
2098 /*
2099 Get slope as the median of all slopes
2100 */
2101 VECcxx . . $ VEC# mbsPart = mbs.partcxx . . $ autovector#part(2d8d48654ca64e70). (1, numberOfCombinations);
2102 sort_e_VEC_inoutcxx . . $ sort_e_VEC_inout(e75f7985a5e05502). (mbsPart);
2103 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (mbsPartcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., 0.5);
2104 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (mbsPartcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., oneTailedUnconfidence);
2105 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).3] = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (mbsPartcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., 1.0 - oneTailedUnconfidence);
2106 /*
2107 Reuse array for intercept
2108 */
2109 if (wantIntercept) {
2110 for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ constvector#size.; i ++)
2111 mbs [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] - out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] * x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i];
2112 mbsPart =cxx . . $ vector#`operator=`(b5ebf627ac2fae1e). mbs.partcxx . . $ autovector#part(2d8d48654ca64e70). (1, x.sizecxx . . $ constvector#size.);
2113 sort_e_VEC_inoutcxx . . $ sort_e_VEC_inout(e75f7985a5e05502). (mbsPart);
2114 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).4] = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (mbsPartcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., 0.5);
2115 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).5] = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (mbsPartcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., oneTailedUnconfidence);
2116 out_lineParameters [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).6] = NUMquantilecxx . . $ NUMquantile(885a97927582bd48). (mbsPartcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)., 1.0 - oneTailedUnconfidence);
2117 }
2118 }
2119 } catch (MelderErrorcxx . . $ MelderError#) {
2120 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No line fit (Theil's method).");
2121 }
2122 }
2123
2124 void NUMfitLine_LScxx . . $ NUMfitLine_LS(85689700950555b9). (constVECcxx . . $ constVEC# const& x, constVECcxx . . $ constVEC# const& y, double *out_m, double *out_intercept) {
2125 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (x.size == y.size,
2126 U"NUMfitLine_LS: the sizes of the two vectors should be equal.");
2127 const double xsum = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (xcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).);
2128 const double xmean = xsum / x.sizecxx . . $ constvector#size.;
2129 longdoublecxx . . $ longdouble# variance = 0.0, m = 0.0;
2130 for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ constvector#size.; i ++) {
2131 const double t = x [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] - xmean;
2132 variance += t * t;
2133 m += t * y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i];
2134 }
2135 // y = m*x + b
2136 m /= variance;
2137 if (out_intercept) {
2138 const double ysum = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (ycxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).);
2139 *out_intercept = (ysum - m * xsum) / x.sizecxx . . $ constvector#size.;
2140 }
2141 if (out_m)
2142 *out_m = m;
2143 }
2144
2145 void NUMlineFitcxx . . $ NUMlineFit(8c21accacb989bfe). (constVECcxx . . $ constVEC# x, constVECcxx . . $ constVEC# y, double *out_m, double *out_intercept, integercxx . . $ integer# method) {
2146 if (method == 1)
2147 NUMfitLine_LScxx . . $ NUMfitLine_LS(85689700950555b9). (x, y, out_m, out_intercept);
2148 else if (method == 3)
2149 NUMfitLine_theilcxx . . $ NUMfitLine_theil(c6f3b2557f11da6e). (x, y, out_m, out_intercept, true);
2150 else
2151 NUMfitLine_theilcxx . . $ NUMfitLine_theil(c6f3b2557f11da6e). (x, y, out_m, out_intercept, false);
2152 }
2153
2154 // IEEE: Programs for digital signal processing section 4.3 LPTRN
2155 // lpc [1..n] to rc [1..n]
2156
2157 void VECrc_from_lpccxx . . $ VECrc_from_lpc(9ec4162bb4ce9309). (VECcxx . . $ VEC# rc, constVECcxx . . $ constVEC# lpc) {
2158 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (rc.size == lpc.size);
2159 autoVECcxx . . $ autoVEC# b = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (lpc.sizecxx . . $ constvector#size.);
2160 autoVECcxx . . $ autoVEC# a = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (lpc.sizecxx . . $ constvector#size.);
2161 a.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). lpccxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).;
2162 for (integercxx . . $ integer# m = lpc.sizecxx . . $ constvector#size.; m > 0; m--) {
2163 rc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m] = a [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).m];
2164 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (fabs (rc [m]) <= 1.0,
2165 U"Relection coefficient [", m, U"] larger than 1.");
2166 bcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, m) <<=cxx . . $ `operator<<=`(adea042513329390). acxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, m);
2167 for (integercxx . . $ integer# i = 1; i < m; i ++)
2168 a [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (b [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] - rc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m] * b [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).m - i]) / (1.0 - rc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m] * rc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m]);
2169 }
2170 }
2171
2172 static void VEClpc_from_rccxx . . $ VEClpc_from_rc(9ec4162bb4ce9309). (VECcxx . . $ VEC# lpc, constVECcxx . . $ constVEC# rc) {
2173 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (lpc.size == rc.size);
2174 lpccxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(adea042513329390). rccxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).;
2175 for (integercxx . . $ integer# j = 2; j <= lpc.sizecxx . . $ vector#size.; j ++) {
2176 for (integercxx . . $ integer# k = 1; k <= j / 2; k ++) {
2177 double at = lpc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] + rc [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] * lpc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j - k];
2178 lpc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j - k] += rc [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] * lpc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k];
2179 lpc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k] = at;
2180 }
2181 }
2182 }
2183
2184 void VECarea_from_rccxx . . $ VECarea_from_rc(9ec4162bb4ce9309). (VECcxx . . $ VEC# area, constVECcxx . . $ constVEC# rc) {
2185 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (area.size == rc.size);
2186 longdoublecxx . . $ longdouble# s = 0.0001; // 1.0 cm^2 at glottis
2187 for (integercxx . . $ integer# i = area.sizecxx . . $ vector#size.; i > 0; i --) {
2188 s *= (1.0 + rc [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]) / (1.0 - rc [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]);
2189 area [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = s;
2190 }
2191 }
2192
2193 void VECrc_from_areacxx . . $ VECrc_from_area(9ec4162bb4ce9309). (VECcxx . . $ VEC# rc, constVECcxx . . $ constVEC# area) {
2194 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (rc.size == area.size);
2195 double ar;
2196 for (integercxx . . $ integer# j = 1; j <= rc.sizecxx . . $ vector#size. - 1; j ++) {
2197 ar = area [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j + 1] / area [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j];
2198 rc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = (1.0 - ar) / (1.0 + ar);
2199 }
2200 ar = 0.0001 / area [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).rc.sizecxx . . $ vector#size.]; // 1.0 cm^2 at glottis
2201 rc [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rc.sizecxx . . $ vector#size.] = (1.0 - ar) / (1.0 + ar);
2202 }
2203
2204 void VEClpc_from_areacxx . . $ VEClpc_from_area(9ec4162bb4ce9309). (VECcxx . . $ VEC# lpc, constVECcxx . . $ constVEC# area) {
2205 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (lpc.size == area.size);
2206 autoVECcxx . . $ autoVEC# rc = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (lpc.sizecxx . . $ vector#size.);
2207 VECrc_from_areacxx . . $ VECrc_from_area(9ec4162bb4ce9309). (rc.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), area);
2208 VEClpc_from_rccxx . . $ VEClpc_from_rc(9ec4162bb4ce9309). (lpc, rccxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
2209 }
2210
2211 void VECarea_from_lpccxx . . $ VECarea_from_lpc(9ec4162bb4ce9309). (VECcxx . . $ VEC# area, constVECcxx . . $ constVEC# lpc) {
2212 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (area.size == lpc.size);
2213 autoVECcxx . . $ autoVEC# rc = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (lpc.sizecxx . . $ constvector#size.);
2214 VECrc_from_lpccxx . . $ VECrc_from_lpc(9ec4162bb4ce9309). (rc.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), lpc);
2215 VECarea_from_rccxx . . $ VECarea_from_rc(9ec4162bb4ce9309). (area, rccxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
2216 }
2217
2218 #if 0
2219 /*********** Begin deprecated LPC routines ***********************************/
2220 void NUMlpc_lpc_to_rc (double *lpc, integer p, double *rc) {
2221 autoVEC b = zero_VEC (p);
2222 autoVEC a <<= VEC (lpc, p);
2223 for (integer m = p; m > 0; m--) {
2224 rc [m] = a [m];
2225 Melder_require (fabs (rc [m]) <= 1.0,
2226 U"Relection coefficient [", m, U"] larger than 1.");
2227 for (integer i = 1; i < m; i ++) {
2228 b [i] = a [i];
2229 }
2230 for (integer i = 1; i < m; i ++) {
2231 a [i] = (b [i] - rc [m] * b [m - i]) / (1.0 - rc [m] * rc [m]);
2232 }
2233 }
2234 }
2235
2236 void NUMlpc_rc_to_area2 (double *rc, integer n, double *area);
2237 void NUMlpc_rc_to_area2 (double *rc, integer n, double *area) {
2238 double s = 0.0001; /* 1.0 cm^2 at glottis */
2239 for (integer i = n; i > 0; i--) {
2240 s *= (1.0 + rc [i]) / (1.0 - rc [i]);
2241 area [i] = s;
2242 }
2243 }
2244
2245 void NUMlpc_area_to_lpc2 (double *area, integer n, double *lpc);
2246 void NUMlpc_area_to_lpc2 (double *area, integer n, double *lpc) {
2247 // from area to reflection coefficients
2248 autoVEC rc =raw_VEC (n);
2249 // normalisation: area [n+1] = 0.0001
2250 for (integer j = n; j > 0; j--) {
2251 double ar = area [j+1] / area [j];
2252 rc [j] = (1 - ar) / (1 + ar);
2253 }
2254 // LPTRAN works from mouth to lips:
2255 for (integer j = 1; j <= n; j ++) {
2256 lpc [j] = rc [n - j + 1];
2257 }
2258 for (integer j = 2; j <= n; j ++) {
2259 integer nh = j / 2;
2260 double q = rc [j];
2261 for (integer k = 1; k <= nh; k ++) {
2262 double at = lpc [k] + q * lpc [j - k];
2263 lpc [j - k] += q * lpc [k];
2264 lpc [k] = at;
2265 }
2266 }
2267 }
2268
2269 void NUMlpc_lpc_to_rc2 (double *lpc, integer m, double *rc);
2270 void NUMlpc_lpc_to_rc2 (double *lpc, integer m, double *rc) { // klopt nog niet
2271 rc.part (1, m) <<= lpc.part (1, m)
2272 for (integer j = 2; j <= m; j ++) {
2273 integer jb = m + 1 - j;
2274 integer mh = (jb + 1) / 2;
2275 double rct = rc [jb+1];
2276 double d = 1.0 - rct * rct;
2277 for (integer k = 1; k <= mh; k ++) {
2278 rc [k] *= (1 - rct) / d;
2279 }
2280 }
2281 }
2282 // area [1] at lips generates n+1 areas from n rc's
2283 void NUMlpc_rc_to_area (double *rc, integer m, double *area) {
2284 area [m+1] = 0.0001; /* 1.0 cm^2 */
2285 for (integer j = 1; j <= m; j ++) {
2286 double ar = (1.0 - rc [m+1-j]) / (1.0 + rc [m+1-j]);
2287 area [m+1-j] = area [m+2-j] / ar;
2288 }
2289 }
2290
2291 // returns m-1 reflection coefficients from m areas
2292 void NUMlpc_area_to_rc (double *area, integer m, double *rc) {
2293 for (integer j = 1; j <= m - 1; j ++) {
2294 double ar = area [j+1] / area [j];
2295 rc [j] = (1.0 - ar) / (1.0 + ar);
2296 }
2297 }
2298
2299 void NUMlpc_rc_to_lpc (double *rc, integer m, double *lpc);
2300 void NUMlpc_rc_to_lpc (double *rc, integer m, double *lpc) {
2301 lpc.part (1, m) <<= rc.part (1, m)
2302 for (integer j = 2; j <= m; j ++) {
2303 for (integer k = 1; k <= j / 2; k ++) {
2304 double at = lpc [k] + rc [j] * lpc [j - k];
2305 lpc [j - k] += rc [j] * lpc [k];
2306 lpc [k] = at;
2307 }
2308 }
2309 }
2310
2311 void NUMlpc_area_to_lpc (double *area, integer m, double *lpc) {
2312 // from area to reflection coefficients
2313 autoVEC rc = zero_VEC (m);
2314 // normalisation: area [n+1] = 0.0001
2315 NUMlpc_area_to_rc (area, m, rc.peek());
2316 NUMlpc_rc_to_lpc (rc.peek(), m - 1, lpc); // m-1 ???
2317 }
2318
2319 void NUMlpc_lpc_to_area (double *lpc, integer m, double *area) {
2320 autoVEC rc = zero_VEC (m);
2321 NUMlpc_lpc_to_rc (lpc, m, rc.peek());
2322 NUMlpc_rc_to_area (rc.peek(), m, area);
2323
2324 }
2325 /*********** End deprecated LPC routines ***********************************/
2326 #endif
2327
2328 #undef SIGNcxx . . $ `dwsys/NUM2.cpp:87:9`!
2329
2330 /* Start of GSL based routines:
2331 The following random number generating routines are based on the GSL library
2332 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
2333
2334 */
2335
2336 #define SMALL_MEANcxx . . $ `dwsys/NUM2.cpp:2336:9`! 14
2337 /* If n*p < SMALL_MEAN then use BINV algorithm. The ranlib implementation used cutoff=30;
2338 * but on my (Brian Gough) computer 14 works better
2339 */
2340
2341 #define BINV_CUTOFFcxx . . $ `dwsys/NUM2.cpp:2341:9`! 110
2342 /* In BINV, do not permit ix too large */
2343
2344 #define FAR_FROM_MEANcxx . . $ `dwsys/NUM2.cpp:2344:9`! 20
2345 /* If ix-n*p is larger than this, then use the "squeeze" algorithm.
2346 * Ranlib used 20, and this seems to be the best choice on my (Brian Gough) machine as well.
2347 */
2348
2349 #define LNFACTcxx . . $ `dwsys/NUM2.cpp:2349:9`!(x) gsl_sf_lnfact(x)
2350
2351 inline static double Stirlingcxx . . $ Stirling(447c67c6676f75c3). (double y1)
2352 {
2353 const double y2 = y1 * y1;
2354 const double s = (13860.0 - (462.0 - (132.0 - (99.0 - 140.0 / y2) / y2) / y2) / y2) / y1 / 166320.0;
2355 return s;
2356 }
2357
2358 /*
2359 This implementation of NUMrandomBinomial is by James Theiler, April 2003,
2360 djmw 20121211 replaced calls to gsl_rng_uniform with NUMrandomUniform (0,1)
2361 */
2362 integercxx . . $ integer# NUMrandomBinomialcxx . . $ NUMrandomBinomial(a2381fb6979456a7). (double p, integercxx . . $ integer# n) {
2363 if (p < 0.0 || p > 1.0 || n < 0) {
2364 return -100000000;
2365 }
2366 integercxx . . $ integer# ix; // return value
2367 bool flipped = false;
2368
2369 if (n == 0)
2370 return 0;
2371 if (p > 0.5) {
2372 p = 1.0 - p; // work with small p
2373 flipped = true;
2374 }
2375
2376 const double q = 1.0 - p;
2377 const double s = p / q;
2378 const double np = n * p;
2379
2380 /*
2381 Inverse cdf logic for small mean (BINV in K+S)
2382 */
2383
2384 if (np < SMALL_MEANcxx . . $ `dwsys/NUM2.cpp:2336:9`!) {
2385 const double f0 = powcxx . . $ pow(9454dda30a4b8335). (q, n); // djmw gsl_pow_int (q, n); f(x), starting with x=0
2386
2387 while (1) {
2388 /*
2389 This while(1) loop will almost certainly only loop once; but
2390 if u=1 to within a few epsilons of machine precision, then it
2391 is possible for roundoff to prevent the main loop over ix to
2392 achieve its proper value. Following the ranlib implementation,
2393 we introduce a check for that situation, and when it occurs,
2394 we just try again.
2395 */
2396
2397 double f = f0;
2398 double u = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, 1.0); // djmw gsl_rng_uniform (rng);
2399
2400 for (ix = 0; ix <= BINV_CUTOFFcxx . . $ `dwsys/NUM2.cpp:2341:9`!; ++ ix) {
2401 if (u < f)
2402 goto Finish;
2403 u -= f;
2404 // Use recursion f(x+1) = f(x)* [(n-x)/(x+1)]* [p/(1-p)]
2405 f *= s * (n - ix) / (ix + 1.0);
2406 }
2407
2408 /*
2409 It should be the case that the 'goto Finish' was encountered
2410 before this point was ever reached. But if we have reached
2411 this point, then roundoff has prevented u from decreasing
2412 all the way to zero. This can happen only if the initial u
2413 was very nearly equal to 1, which is a rare situation. In
2414 that rare situation, we just try again.
2415
2416 Note, following the ranlib implementation, we loop ix only to
2417 a hardcoded value of SMALL_MEAN_LARGE_N=110; we could have
2418 looped to n, and 99.99...% of the time it won't matter. This
2419 choice, I think is a little more robust against the rare
2420 roundoff error. If n>LARGE_N, then it is technically
2421 possible for ix>LARGE_N, but it is astronomically rare, and
2422 if ix is that large, it is more likely due to roundoff than
2423 probability, so better to nip it at LARGE_N than to take a
2424 chance that roundoff will somehow conspire to produce an even
2425 larger (and more improbable) ix. If n<LARGE_N, then once
2426 ix=n, f=0, and the loop will continue until ix=LARGE_N.
2427 */
2428 }
2429 } else {
2430
2431 /*
2432 For n >= SMALL_MEAN, we invoke the BTPE algorithm
2433 */
2434
2435 const double ffm = np + p; // ffm = n*p+p
2436 const integercxx . . $ integer# m = (integercxx . . $ integer#) ffm; // m = int floor [n*p+p]
2437 const double fm = m; // fm = double m
2438 const double xm = fm + 0.5; // xm = half integer mean (tip of triangle)
2439 const double npq = np * q; // npq = n*p*q
2440
2441 /*
2442 Compute cumulative area of tri, para, exp tails
2443
2444 p1: radius of triangle region; since height=1, also: area of region
2445 p2: p1 + area of parallelogram region
2446 p3: p2 + area of left tail
2447 p4: p3 + area of right tail
2448 pi/p4: probability of i'th area (i=1,2,3,4)
2449
2450 Note: magic numbers 2.195, 4.6, 0.134, 20.5, 15.3
2451 These magic numbers are not adjustable...at least not easily!
2452 */
2453
2454 const double p1 = Melder_roundDowncxx . . $ Melder_roundDown(447c67c6676f75c3). (2.195 * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (npq) - 4.6 * q) + 0.5;
2455
2456 // xl, xr: left and right edges of triangle
2457 const double xl = xm - p1;
2458 const double xr = xm + p1;
2459
2460 /*
2461 Parameter of exponential tails
2462 Left tail: t(x) = c*exp(-lambda_l* [xl - (x+0.5)])
2463 Right tail: t(x) = c*exp(-lambda_r* [(x+0.5) - xr])
2464 */
2465
2466 const double c = 0.134 + 20.5 / (15.3 + fm);
2467 const double p2 = p1 * (1.0 + c + c);
2468 const double al = (ffm - xl) / (ffm - xl * p);
2469 const double lambda_l = al * (1.0 + 0.5 * al);
2470 const double ar = (xr - ffm) / (xr * q);
2471 const double lambda_r = ar * (1.0 + 0.5 * ar);
2472 const double p3 = p2 + c / lambda_l;
2473 const double p4 = p3 + c / lambda_r;
2474 double var, accept;
2475
2476 TryAgain:
2477
2478 /*
2479 Generate random variates, u specifies which region: Tri, Par, Tail
2480 */
2481
2482 const double u = p4 * NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, 1.0); // djmw gsl_rng_uniform (rng) * p4;
2483 double v = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, 1.0); // djmw gsl_rng_uniform (rng);
2484
2485 if (u <= p1) {
2486 // Triangular region
2487 ix = (integercxx . . $ integer#) (xm - p1 * v + u);
2488 goto Finish;
2489 } else if (u <= p2) {
2490 // Parallelogram region
2491 const double x = xl + (u - p1) / c;
2492 v = v * c + 1.0 - fabscxx . . $ fabs(fee4f1d22bc8e617). (x - xm) / p1;
2493 if (v > 1.0 || v <= 0.0)
2494 goto TryAgain;
2495 ix = (integercxx . . $ integer#) x;
2496 } else if (u <= p3) {
2497 // Left tail
2498 ix = (integercxx . . $ integer#) (xl + logcxx . . $ log(fee4f1d22bc8e617). (v) / lambda_l);
2499 if (ix < 0)
2500 goto TryAgain;
2501 v *= ((u - p2) * lambda_l);
2502 } else {
2503 // Right tail
2504 ix = (integercxx . . $ integer#) (xr - logcxx . . $ log(fee4f1d22bc8e617). (v) / lambda_r);
2505 if (ix > (double) n)
2506 goto TryAgain;
2507 v *= ((u - p3) * lambda_r);
2508 }
2509
2510 /*
2511 At this point, the goal is to test whether v <= f(x)/f(m)
2512 v <= f(x)/f(m) = (m!(n-m)! / (x!(n-x)!)) * (p/q)^{x-m}
2513
2514 Here is a direct test using logarithms. It is a little
2515 slower than the various "squeezing" computations below, but
2516 if things are working, it should give exactly the same answer
2517 (given the same random number seed).
2518 */
2519
2520 #ifdef DIRECT
2521 var = log (v);
2522
2523 accept = LNFACT (m) + LNFACT (n - m) - LNFACT (ix) - LNFACT (n - ix) + (ix - m) * log (p / q);
2524
2525 #else // SQUEEZE METHOD
2526
2527 /*
2528 More efficient determination of whether v < f(x)/f(M)
2529 */
2530
2531 const integercxx . . $ integer# k = integer_abscxx . . $ integer_abs(8ce0d1b217db7fd1). (ix - m);
2532
2533 if (k <= FAR_FROM_MEANcxx . . $ `dwsys/NUM2.cpp:2344:9`!) {
2534 /*
2535 If ix near m (ie, |ix-m|<FAR_FROM_MEAN), then do
2536 explicit evaluation using recursion relation for f(x)
2537 */
2538 const double g = (n + 1) * s;
2539 double f = 1.0;
2540
2541 var = v;
2542
2543 if (m < ix) {
2544 for (integercxx . . $ integer# i = m + 1; i <= ix; i ++)
2545 f *= (g / i - s);
2546 } else if (m > ix) {
2547 for (integercxx . . $ integer# i = ix + 1; i <= m; i ++)
2548 f /= (g / i - s);
2549 }
2550
2551 accept = f;
2552 } else {
2553 // If ix is far from the mean m: k=ABS(ix-m) large
2554
2555 var = logcxx . . $ log(fee4f1d22bc8e617). (v);
2556
2557 if (k < npq / 2 - 1) {
2558 /*
2559 "Squeeze" using upper and lower bounds on
2560 log(f(x)) The squeeze condition was derived
2561 under the condition k < npq/2-1
2562 */
2563 const double amaxp = k / npq * ((k * (k / 3.0 + 0.625) + (1.0 / 6.0)) / npq + 0.5);
2564 const double ynorm = -(k * k / (2.0 * npq));
2565 if (var < ynorm - amaxp)
2566 goto Finish;
2567 if (var > ynorm + amaxp)
2568 goto TryAgain;
2569 }
2570
2571 /*
2572 Now, again: do the test log(v) vs. log f(x)/f(M)
2573 */
2574
2575 #if USE_EXACT
2576 /*
2577 This is equivalent to the above, but is a little (~20%) slower
2578 There are five log's vs three above, maybe that's it?
2579 */
2580
2581 accept = LNFACT (m) + LNFACT (n - m) - LNFACT (ix) - LNFACT (n - ix) + (ix - m) * log (p / q);
2582
2583 #else
2584 /* USE STIRLING:
2585 The "#define Stirling" above corresponds to the first five
2586 terms in asymptotic formula for
2587 log Gamma (y) - (y-0.5)log(y) + y - 0.5 log(2*pi);
2588 See Abramowitz and Stegun, eq 6.1.40
2589
2590
2591 Note below: two Stirling's are added, and two are
2592 subtracted. In both K+S, and in the ranlib
2593 implementation, all four are added. I (jt) believe that
2594 is a mistake -- this has been confirmed by personal
2595 correspondence w/ Dr. Kachitvichyanukul. Note, however,
2596 the corrections are so small, that I couldn't find an
2597 example where it made a difference that could be
2598 observed, let alone tested. In fact, define'ing Stirling
2599 to be zero gave identical results!! In practice, alv is
2600 O(1), ranging 0 to -10 or so, while the Stirling
2601 correction is typically O(10^{-5}) ...setting the
2602 correction to zero gives about a 2% performance boost;
2603 might as well keep it just to be pedantic.
2604 */
2605
2606 {
2607 const double x1 = ix + 1.0;
2608 const double w1 = n - ix + 1.0;
2609 const double f1 = fm + 1.0;
2610 const double z1 = n + 1.0 - fm;
2611
2612 accept = xm * logcxx . . $ log(fee4f1d22bc8e617). (f1 / x1) + (n - m + 0.5) * logcxx . . $ log(fee4f1d22bc8e617). (z1 / w1) + (ix - m) * logcxx . . $ log(fee4f1d22bc8e617). (w1 * p / (x1 * q))
2613 + Stirlingcxx . . $ Stirling(447c67c6676f75c3). (f1) + Stirlingcxx . . $ Stirling(447c67c6676f75c3). (z1) - Stirlingcxx . . $ Stirling(447c67c6676f75c3). (x1) - Stirlingcxx . . $ Stirling(447c67c6676f75c3). (w1);
2614 }
2615 #endif
2616 #endif
2617 }
2618
2619
2620 if (var <= accept)
2621 goto Finish;
2622 else
2623 goto TryAgain;
2624 }
2625
2626 Finish:
2627
2628 return flipped ? (n - ix) : ix;
2629 }
2630
2631 double NUMrandomBinomial_realcxx . . $ NUMrandomBinomial_real(3b6c2eefe8d0b5cf). (double p, integercxx . . $ integer# n) {
2632 if (p < 0.0 || p > 1.0 || n < 0)
2633 return undefinedcxx . . $ undefined.;
2634 else
2635 return (double) NUMrandomBinomialcxx . . $ NUMrandomBinomial(a2381fb6979456a7). (p, n);
2636 }
2637
2638 /*
2639 From gsl_ran_weibull.c
2640 */
2641 double NUMrandomWeibullcxx . . $ NUMrandomWeibull(d8e7e0733fa41c87). (double scale_lambda, double shape_k) {
2642 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (scale_lambda > 0.0 && shape_k > 0.0,
2643 U"NUMrandomWeibull: both arguments should be positive.");
2644 const double u = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0, 1);
2645 return scale_lambda * powcxx . . $ pow(b71928d39fde4295). (- logcxx . . $ log(fee4f1d22bc8e617). (u), 1.0 / shape_k);
2646 }
2647
2648 /*
2649 Notice in GSL gsl_randist_gamma:
2650 Version based on Marsaglia and Tsang, "A Simple Method for generating gamma variables",
2651 ACM Transactions on Mathematical Software, Vol 26, No 3 (2000), p363-372.
2652
2653 Implemented by [email protected], minor modifications for GSL by Brian Gough
2654 */
2655 double NUMrandomGammacxx . . $ NUMrandomGamma(d8e7e0733fa41c87). (const double alpha, const double beta) {
2656 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (alpha > 0 && beta > 0,
2657 U"NUMrandomGamma: both arguments should be positive.");
2658 double result;
2659 if (alpha >= 1.0) {
2660 double x, v, d = alpha - 1.0 / 3.0;
2661 double c = (1.0 / 3.0) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (d);
2662 while (1) {
2663 do {
2664 x = NUMrandomGausscxx . . $ NUMrandomGauss(d8e7e0733fa41c87). (0.0, 1.0);
2665 v = 1.0 + c * x;
2666 } while (v <= 0.0);
2667 v = v * v * v;
2668 double u = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, 1.0);
2669 if (u < 1.0 - 0.0331 * (x * x) * (x * x))
2670 break;
2671 if (logcxx . . $ log(fee4f1d22bc8e617).(u) < 0.5 * x * x + d * (1.0 - v + logcxx . . $ log(fee4f1d22bc8e617).(v)))
2672 break;
2673 }
2674 result = d * v / beta;
2675 } else {
2676 double u = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0.0, 1.0);
2677 result = NUMrandomGammacxx . . $ NUMrandomGamma(d8e7e0733fa41c87). (alpha + 1.0, beta) * powcxx . . $ pow(b71928d39fde4295). (u, 1.0 / alpha);
2678 }
2679 return result;
2680 }
2681
2682 void NUMlngamma_complexcxx . . $ NUMlngamma_complex(1c2b5cb2f7ad0402). (double zr, double zi, double *out_lnr, double *out_arg) {
2683 double ln_re = undefinedcxx . . $ undefined., ln_arg = undefinedcxx . . $ undefined.;
2684 gsl_sf_result gsl_lnr, gsl_arg;
2685 if (gsl_sf_lngamma_complex_e (zr, zi, & gsl_lnr, & gsl_arg)) {
2686 ln_re = gsl_lnr.val;
2687 ln_arg = gsl_arg.val;
2688 }
2689 if (out_lnr)
2690 *out_lnr = ln_re;
2691 if (out_arg)
2692 *out_arg = ln_arg;
2693 }
2694
2695 /* End of GSL based routines */
2696
2697 autoVECcxx . . $ autoVEC# newVECbiharmonic2DSplineInterpolation_getWeightscxx . . $ newVECbiharmonic2DSplineInterpolation_getWeights(2cab18b620a7291c). (constVECVUcxx . . $ constVECVU# const& x, constVECVUcxx . . $ constVECVU# const& y, constVECVUcxx . . $ constVECVU# const& z) {
2698 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == y.size && x.size == z.size);
2699 autoMATcxx . . $ autoMAT# g = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (x.sizecxx . . $ constvectorview#size., x.sizecxx . . $ constvectorview#size.);
2700 /*
2701 1. Calculate the Green matrix G = |point [i]-point [j]|^2 (ln (|point [i]-point [j]|) - 1.0)
2702 2. Solve z = G.w for w
2703 */
2704 for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ constvectorview#size.; i ++) {
2705 for (integercxx . . $ integer# j = i + 1; j <= x.sizecxx . . $ constvectorview#size.; j ++) {
2706 double dx = x [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i] - x [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).j], dy = y [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i] - y [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).j];
2707 double distanceSquared = dx * dx + dy * dy;
2708 g [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = g [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = distanceSquared * (0.5 * logcxx . . $ log(fee4f1d22bc8e617). (distanceSquared) - 1.0); // Green's function
2709 }
2710 g [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.0;
2711 }
2712 autoVECcxx . . $ autoVEC# w = solve_VECcxx . . $ solve_VEC(27d4aa16c6844bf6). (gcxx . . $ constmatrixview#constmatrixview(eed9513d24570b3d)..getcxx . . $ automatrix#get(b2b81450b5bdda66).(), z, 0.0);
2713 return wcxx . . $ autovector#autovector(d33c649a3aceae2f).;
2714 }
2715
2716 double NUMbiharmonic2DSplineInterpolationcxx . . $ NUMbiharmonic2DSplineInterpolation(62b4eb8773fcd3c). (constVECVUcxx . . $ constVECVU# const& x, constVECVUcxx . . $ constVECVU# const& y, constVECVUcxx . . $ constVECVU# const& w, double xp, double yp) {
2717 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == y.size && x.size == w.size);
2718 longdoublecxx . . $ longdouble# result = 0.0;
2719 for (integercxx . . $ integer# i = 1; i <= x.sizecxx . . $ constvectorview#size.; i ++) {
2720 const double dx = xp - x [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i], dy = yp - y [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i];
2721 const double d = dx * dx + dy * dy;
2722 result += w [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i] * d * (0.5 * logcxx . . $ log(fee4f1d22bc8e617). (d) - 1.0);
2723 }
2724 return (double) result;
2725 }
2726
2727 void NUMfixIndicesInRangecxx . . $ NUMfixIndicesInRange(ea00d9166321b1fb). (integercxx . . $ integer# lowerLimit, integercxx . . $ integer# upperLimit, integercxx . . $ integer# *lowIndex, integercxx . . $ integer# *highIndex) {
2728 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (lowerLimit <= upperLimit, U"The lower limit should not exceed the upper limit.");
2729 if (*highIndex < *lowIndex) {
2730 *lowIndex = lowerLimit; *highIndex = upperLimit;
2731 } else if (*highIndex == *lowIndex) {
2732 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (*lowIndex >= lowerLimit && *highIndex <= upperLimit,
2733 U"Both lower and upper indices are out of range.");
2734 } else { // low < high
2735 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (*lowIndex < upperLimit && *highIndex > lowerLimit,
2736 U"Both lower and upper indices are out of range.");
2737 if (*lowIndex < lowerLimit)
2738 *lowIndex = lowerLimit;
2739 if (*highIndex > upperLimit)
2740 *highIndex = upperLimit;
2741 }
2742 }
2743
2744 void NUMgetEntropiescxx . . $ NUMgetEntropies(176578b5e4041486). (constMATVUcxx . . $ constMATVU# const& m, double *out_h, double *out_hx, double *out_hy, double *out_hygx, double *out_hxgy, double *out_uygx, double *out_uxgy, double *out_uxy) {
2745
2746 double h = undefinedcxx . . $ undefined., hx = undefinedcxx . . $ undefined., hy = undefinedcxx . . $ undefined.;
2747 double hxgy = undefinedcxx . . $ undefined., hygx = undefinedcxx . . $ undefined., uygx = undefinedcxx . . $ undefined., uxgy = undefinedcxx . . $ undefined., uxy = undefinedcxx . . $ undefined.;
2748
2749 // Get total sum and check if all elements are not negative.
2750
2751 longdoublecxx . . $ longdouble# totalSum = 0.0;
2752 for (integercxx . . $ integer# i = 1; i <= m.nrowcxx . . $ constmatrixview#nrow.; i ++) {
2753 for (integercxx . . $ integer# j = 1; j <= m.ncolcxx . . $ constmatrixview#ncol.; j++) {
2754 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (m [i] [j] >= 0.0,
2755 U"Matrix elements should not be negative.");
2756 totalSum += m [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).i] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).j];
2757 }
2758 }
2759
2760 if (totalSum > 0.0) {
2761 longdoublecxx . . $ longdouble# hy_t = 0.0;
2762 for (integercxx . . $ integer# i = 1; i <= m.nrowcxx . . $ constmatrixview#nrow.; i ++) {
2763 const double rowsum = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (m.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (i));
2764 if (rowsum > 0.0) {
2765 const double p = rowsum / double (totalSum);
2766 hy_t -= p * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (p);
2767 }
2768 }
2769 hy = (double) hy_t;
2770
2771 longdoublecxx . . $ longdouble# hx_t = 0.0;
2772 for (integercxx . . $ integer# j = 1; j <= m.ncolcxx . . $ constmatrixview#ncol.; j ++) {
2773 const double colsum = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (m.columncxx . . $ constmatrixview#column(7a6174ed67b49158). (j));
2774 if (colsum > 0.0) {
2775 const double p = colsum / double (totalSum);
2776 hx_t -= p * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (p);
2777 }
2778 }
2779 hx = (double) hx_t;
2780
2781 // Total entropy
2782 longdoublecxx . . $ longdouble# h_t = 0.0;
2783 for (integercxx . . $ integer# i = 1; i <= m.nrowcxx . . $ constmatrixview#nrow.; i ++) {
2784 for (integercxx . . $ integer# j = 1; j <= m.ncolcxx . . $ constmatrixview#ncol.; j ++) {
2785 if (m [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).i] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).j] > 0.0) {
2786 const double p = m [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).i] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).j] / double (totalSum);
2787 h_t -= p * NUMlog2cxx . . $ NUMlog2(fee4f1d22bc8e617). (p);
2788 }
2789 }
2790 }
2791 h = (double) h_t;
2792 hygx = h - hx;
2793 hxgy = h - hy;
2794 uygx = (hy - hygx) / hy;
2795 uxgy = (hx - hxgy) / hx;
2796 uxy = 2.0 * (hx + hy - h) / (hx + hy);
2797 }
2798 if (out_h)
2799 *out_h = h;
2800 if (out_hx)
2801 *out_hx = hx;
2802 if (out_hy)
2803 *out_hy = hy;
2804 // Conditional entropies
2805 if (out_hygx)
2806 *out_hygx = hygx;
2807 if (out_hxgy)
2808 *out_hxgy = hxgy;
2809 if (out_uygx)
2810 *out_uygx = uygx;
2811 if (out_uxgy)
2812 *out_uxgy = uxgy;
2813 if (out_uxy)
2814 *out_uxy = uxy;
2815 }
2816 #undef TINY
2817
2818 double NUMtracecxx . . $ NUMtrace(7a3744b654164b28). (constMATVUcxx . . $ constMATVU# const& a) {
2819 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (a.nrow == a.ncol);
2820 longdoublecxx . . $ longdouble# trace = 0.0;
2821 for (integercxx . . $ integer# i = 1; i <= a.nrowcxx . . $ constmatrixview#nrow.; i ++)
2822 trace += a [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).i] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).i];
2823 return (double) trace;
2824 }
2825
2826 double NUMtrace2cxx . . $ NUMtrace2(8213cc2c710f3dcb). (constMATVUcxx . . $ constMATVU# const& x, constMATVUcxx . . $ constMATVU# const& y) {
2827 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.ncol == y.nrow && x.nrow == y.ncol);
2828 longdoublecxx . . $ longdouble# trace = 0.0;
2829 for (integercxx . . $ integer# irow = 1; irow <= x.nrowcxx . . $ constmatrixview#nrow.; irow ++)
2830 for (integercxx . . $ integer# k = 1; k <= x.ncolcxx . . $ constmatrixview#ncol.; k ++)
2831 trace += x [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).k] * y [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).k] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).irow];
2832 return (double) trace;
2833 }
2834
2835 void NUMeigencmp22cxx . . $ NUMeigencmp22(628ba330b6233ab7). (double a, double b, double c, double *out_rt1, double *out_rt2, double *out_cs1, double *out_sn1) {
2836 const longdoublecxx . . $ longdouble# sm = a + c, df = a - c, adf = fabscxx . . $ fabs(cd6880bb7604d12e). (df);
2837 const longdoublecxx . . $ longdouble# tb = b + b, ab = fabscxx . . $ fabs(cd6880bb7604d12e). (tb);
2838 longdoublecxx . . $ longdouble# acmx = c, acmn = a;
2839 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (a) > fabscxx . . $ fabs(fee4f1d22bc8e617). (c)) {
2840 acmx = a;
2841 acmn = c;
2842 }
2843 longdoublecxx . . $ longdouble# rt, tn;
2844 if (adf > ab) {
2845 tn = ab / adf;
2846 rt = adf * sqrtcxx . . $ sqrt(cd6880bb7604d12e). (1.0 + tn * tn);
2847 } else if (adf < ab) {
2848 tn = adf / ab;
2849 rt = ab * sqrtcxx . . $ sqrt(cd6880bb7604d12e). (1.0 + tn * tn);
2850 } else
2851 rt = ab * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (2.0);
2852
2853 longdoublecxx . . $ longdouble# rt1, rt2;
2854 integercxx . . $ integer# sgn1, sgn2;
2855 if (sm < 0.0) {
2856 rt1 = 0.5 * (sm - rt);
2857 sgn1 = -1;
2858 /*
2859 Order of execution important.
2860 To get fully accurate smaller eigenvalue,
2861 next line needs to be executed in higher precision.
2862 */
2863 rt2 = (acmx / rt1) * acmn - (b / rt1) * b;
2864 } else if (sm > 0.0) {
2865 rt1 = 0.5 * (sm + rt);
2866 sgn1 = 1;
2867 /*
2868 Order of execution important.
2869 To get fully accurate smaller eigenvalue,
2870 next line needs to be executed in higher precision.
2871 */
2872 rt2 = (acmx / rt1) * acmn - (b / rt1) * b;
2873 } else {
2874 rt1 = 0.5 * rt;
2875 rt2 = -0.5 * rt;
2876 sgn1 = 1;
2877 }
2878
2879 // Compute the eigenvector
2880
2881 longdoublecxx . . $ longdouble# cs;
2882 if (df >= 0.0) {
2883 cs = df + rt;
2884 sgn2 = 1;
2885 } else {
2886 cs = df - rt;
2887 sgn2 = -1;
2888 }
2889 const longdoublecxx . . $ longdouble# acs = fabscxx . . $ fabs(cd6880bb7604d12e). (cs);
2890 longdoublecxx . . $ longdouble# cs1, sn1;
2891 if (acs > ab) {
2892 const longdoublecxx . . $ longdouble# ct = -tb / cs;
2893 sn1 = 1.0 / sqrtcxx . . $ sqrt(cd6880bb7604d12e). (1.0 + ct * ct);
2894 cs1 = ct * sn1;
2895 } else {
2896 if (ab == 0.0) {
2897 cs1 = 1.0;
2898 sn1 = 0.0;
2899 } else {
2900 tn = -cs / tb;
2901 cs1 = 1.0 / sqrtcxx . . $ sqrt(cd6880bb7604d12e). (1.0 + tn * tn);
2902 sn1 = tn * cs1;
2903 }
2904 }
2905 if (sgn1 == sgn2) {
2906 tn = cs1;
2907 cs1 = -sn1;
2908 sn1 = tn;
2909 }
2910 if (fabscxx . . $ fabs(cd6880bb7604d12e). (cs1) > 1.0)
2911 cs1 = copysigncxx . . $ copysign(9454dda30a4b8335). (1.0, cs1);
2912 if (fabscxx . . $ fabs(cd6880bb7604d12e). (sn1) > 1.0)
2913 sn1 = copysigncxx . . $ copysign(9454dda30a4b8335). (1.0, sn1);
2914 if (out_rt1)
2915 *out_rt1 = (double) rt1;
2916 if (out_rt2)
2917 *out_rt2 = (double) rt2;
2918 if (out_cs1)
2919 *out_cs1 = (double) cs1;
2920 if (out_sn1)
2921 *out_sn1 = (double) sn1;
2922 }
2923
2924 void MATmul3_XYXtcxx . . $ MATmul3_XYXt(9e368e0da0f1874a). (MATVUcxx . . $ MATVU# const& target, constMATVUcxx . . $ constMATVU# const& x, constMATVUcxx . . $ constMATVU# const& y) { // X.Y.X'
2925 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.ncol == y.nrow && y.ncol == x.ncol);
2926 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (target.nrow == target.ncol && target.nrow == x.nrow);
2927 for (integercxx . . $ integer# irow = 1; irow <= target.nrowcxx . . $ matrixview#nrow.; irow ++)
2928 for (integercxx . . $ integer# icol = 1; icol <= target.ncolcxx . . $ matrixview#ncol.; icol ++) {
2929 longdoublecxx . . $ longdouble# sum = 0.0;
2930 for (integercxx . . $ integer# k = 1; k <= x.ncolcxx . . $ constmatrixview#ncol.; k ++)
2931 sum += x [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).k] * NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (y.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (k), x.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (icol));
2932 target [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).icol] = double (sum);
2933 }
2934 }
2935
2936 void MATmul3_XYsXtcxx . . $ MATmul3_XYsXt(9e368e0da0f1874a). (MATVUcxx . . $ MATVU# const& target, constMATVUcxx . . $ constMATVU# const& x, constMATVUcxx . . $ constMATVU# const& y) { // X.Y.X'
2937 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.ncol == y.nrow && y.ncol == x.ncol);
2938 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (target.nrow == target.ncol && target.nrow == x.nrow);
2939 for (integercxx . . $ integer# irow = 1; irow <= target.nrowcxx . . $ matrixview#nrow.; irow ++)
2940 for (integercxx . . $ integer# icol = irow; icol <= target.ncolcxx . . $ matrixview#ncol.; icol ++) {
2941 longdoublecxx . . $ longdouble# sum = 0.0;
2942 for (integercxx . . $ integer# k = 1; k <= x.ncolcxx . . $ constmatrixview#ncol.; k ++)
2943 sum += x [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).k] * NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (y.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (k), x.rowcxx . . $ constmatrixview#row(7a6174ed67b49158). (icol));
2944 target [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).icol] = double (sum);
2945 }
2946 for (integercxx . . $ integer# irow = 1; irow <= target.nrowcxx . . $ matrixview#nrow.; irow ++)
2947 for (integercxx . . $ integer# icol = irow + 1; icol <= target.ncolcxx . . $ matrixview#ncol.; icol ++)
2948 target [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).icol] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).irow] = target [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).icol];
2949 }
2950
2951 /*
2952 1. Take absolute value of v.
2953 2. Sort abs(v) and its index together.
2954 3. Make all elements of v zero, except the numberOfNonZeros largest elements.
2955 4. Set the support of these largest elements to 1 and the rest to zero.
2956 */
2957 static void VECupdateDataAndSupport_inplacecxx . . $ VECupdateDataAndSupport_inplace(ace92788853dacf2). (VECVUcxx . . $ VECVU# const& v, BOOLVECVUcxx . . $ BOOLVECVU# const& support, integercxx . . $ integer# numberOfNonZeros) {
2958 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (v.size == support.size);
2959 autoVECcxx . . $ autoVEC# abs = abs_VECcxx . . $ abs_VEC(d4415e68b25283a8). (vcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401).);
2960 autoINTVECcxx . . $ autoINTVEC# index = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (v.sizecxx . . $ vectorview#size.);
2961 NUMsortTogethercxx . . $ NUMsortTogether(13c11f7bd0e7e3d2). (abs.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), index.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); // sort is always increasing
2962 for (integercxx . . $ integer# i = 1; i <= v.sizecxx . . $ vectorview#size. - numberOfNonZeros; i ++) {
2963 v [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]] = 0.0;
2964 support [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]] = false;
2965 }
2966 for (integercxx . . $ integer# i = v.sizecxx . . $ vectorview#size. - numberOfNonZeros + 1; i <= v.sizecxx . . $ vectorview#size.; i ++)
2967 support [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).index [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]] = true;
2968 }
2969
2970 static double updatecxx . . $ update(6844d3adbb82ffb6). (VECcxx . . $ VEC# const& x_new, VECcxx . . $ VEC# const& y_new, BOOLVECVUcxx . . $ BOOLVECVU# const& support_new, constVECVUcxx . . $ constVECVU# const& xn, double stepSize, constVECcxx . . $ constVEC# const& gradient, constMATVUcxx . . $ constMATVU# const& dictionary, constVECcxx . . $ constVEC# const& yn, integercxx . . $ integer# numberOfNonZeros, VECcxx . . $ VEC# buffer) {
2971 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x_new.size == xn.size && buffer.size == x_new.size);
2972 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (gradient.size == support_new.size && gradient.size == x_new.size);
2973 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (y_new.size == yn.size);
2974 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dictionary.nrow == yn.size && dictionary.ncol == xn.size);
2975
2976 buffercxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(d166bd5b5c96d95c). stepSize *cxx . . $ `operator*`(7f4bd74ab5a11911). gradientcxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).;
2977 x_newcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(6ae1e3b199c5b085). xn +cxx . . $ operator+(5e695a3bee7ea816). buffercxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).; // x(n) + stepSize * gradient
2978 VECupdateDataAndSupport_inplacecxx . . $ VECupdateDataAndSupport_inplace(ace92788853dacf2). (x_newcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)., support_new, numberOfNonZeros);
2979 buffercxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(ce74bae6c180f663). x_newcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). -cxx . . $ operator-(fcdbb1279f7b512e). xn; // x(n+1) - x (n)
2980 const double xdifsq = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (buffercxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).); // ||x(n+1) - x (n)||^2
2981
2982 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (y_newcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)., dictionary, x_newcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd).); // y(n+1) = D. x(n+1)
2983 buffercxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (1, yn.sizecxx . . $ constvector#size.) <<=cxx . . $ `operator<<=`(ce74bae6c180f663). y_newcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). -cxx . . $ operator-(fcdbb1279f7b512e). yncxx . . $ constvectorview#constvectorview(fc39adb4395e9baf).; // y(n+1) - y(n) = D.(x(n+1) - x(n))
2984 const double ydifsq = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (buffercxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (1, yn.sizecxx . . $ constvector#size.)); // ||y(n+1) - y(n)||^2
2985 return xdifsq / ydifsq;
2986 }
2987
2988 autoVECcxx . . $ autoVEC# solveSparse_IHT_VECcxx . . $ solveSparse_IHT_VEC(f538a6905be9afbb). (constMATVUcxx . . $ constMATVU# const& dictionary, constVECVUcxx . . $ constVECVU# const& y, integercxx . . $ integer# numberOfNonZeros, integercxx . . $ integer# maximumNumberOfIterations, double tolerance, integercxx . . $ integer# infoLevel) {
2989 try {
2990 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dictionary.ncol > dictionary.nrow); // must be underdetermined system
2991 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dictionary.nrow == y.size); // y = D.x + e
2992 autoVECcxx . . $ autoVEC# result = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (dictionary.ncolcxx . . $ constmatrixview#ncol.);
2993 VECsolveSparse_IHTcxx . . $ VECsolveSparse_IHT(da092c38e9e93446). (resultcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), dictionary, y, numberOfNonZeros, maximumNumberOfIterations, tolerance, infoLevel);
2994 return resultcxx . . $ autovector#autovector(d33c649a3aceae2f).;
2995 } catch (MelderErrorcxx . . $ MelderError#) {
2996 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Solution of sparse problem not found.");
2997 }
2998 }
2999
3000 void VECsolveSparse_IHTcxx . . $ VECsolveSparse_IHT(da092c38e9e93446). (VECVUcxx . . $ VECVU# const& x, constMATVUcxx . . $ constMATVU# const& dictionary, constVECVUcxx . . $ constVECVU# const& y, integercxx . . $ integer# numberOfNonZeros, integercxx . . $ integer# maximumNumberOfIterations, double tolerance, integercxx . . $ integer# infoLevel) {
3001 try {
3002 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dictionary.ncol > dictionary.nrow); // must be underdetermined system
3003 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dictionary.ncol == x.size); // we calculate D.x
3004 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (dictionary.nrow == y.size); // y = D.x + e
3005
3006 autoVECcxx . . $ autoVEC# gradient = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (x.sizecxx . . $ vectorview#size.);
3007 autoVECcxx . . $ autoVEC# x_new = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (x.sizecxx . . $ vectorview#size.); // x(n+1), x == x(n)
3008 autoVECcxx . . $ autoVEC# yfromx = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (y.sizecxx . . $ constvectorview#size.); // D.x(n)
3009 autoVECcxx . . $ autoVEC# yfromx_new = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (y.sizecxx . . $ constvectorview#size.); // D.x(n+1)
3010 autoVECcxx . . $ autoVEC# ydif = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (y.sizecxx . . $ constvectorview#size.); // y - D.x(n)
3011 autoVECcxx . . $ autoVEC# buffer = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (x.sizecxx . . $ vectorview#size.);
3012 autoBOOLVECcxx . . $ autoBOOLVEC# support = raw_BOOLVECcxx . . $ raw_BOOLVEC(ba622805578e0bb3). (x.sizecxx . . $ vectorview#size.);
3013 autoBOOLVECcxx . . $ autoBOOLVEC# support_new = raw_BOOLVECcxx . . $ raw_BOOLVEC(ba622805578e0bb3). (x.sizecxx . . $ vectorview#size.);
3014
3015 const double xnormSq = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401).);
3016 const double rms_y = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (y) / y.sizecxx . . $ constvectorview#size.;
3017 double rms = rms_y;
3018
3019 if (xnormSq == 0.0) {
3020 /*
3021 Start with x == 0.
3022 Get initial support supp (Hard_K (D'y))
3023 Hard_K (v) is a hard thresholder which only keeps the largest K elements from the vector v
3024 */
3025 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (buffercxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), dictionary.transposecxx . . $ constmatrixview#transpose(f17ed7654a0ebced).(), y);
3026 VECupdateDataAndSupport_inplacecxx . . $ VECupdateDataAndSupport_inplace(ace92788853dacf2). (buffercxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), supportcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), numberOfNonZeros);
3027 yfromx.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0;
3028 ydif.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). y; // ydif = y - D.x(1) = y - D.0 = y
3029 } else {
3030 /*
3031 We improve a current solution x
3032 */
3033 VECupdateDataAndSupport_inplacecxx . . $ VECupdateDataAndSupport_inplace(ace92788853dacf2). (x, supportcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), numberOfNonZeros);
3034 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (yfromxcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), dictionary, xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401).); // D.x(n)
3035 ydif.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(ce74bae6c180f663). y -cxx . . $ operator-(fcdbb1279f7b512e). yfromxcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).(); // y - D.x(n)
3036 rms = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (ydifcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()) / y.sizecxx . . $ constvectorview#size.; // ||y - D.x(n)||^2
3037 }
3038
3039 bool convergence = false;
3040 integercxx . . $ integer# iter = 1;
3041 while (iter <= maximumNumberOfIterations && not convergence) {
3042
3043 mul_VEC_outcxx . . $ mul_VEC_out(82b3fcbc7daf2365). (gradientcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), dictionary.transposecxx . . $ constmatrixview#transpose(f17ed7654a0ebced).(), ydifcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); // D'.(y - D.x(n))
3044 /*
3045 Calculate stepSize mu according to Eq. (13)
3046 mu = || g_sparse ||^2 / || D_sparse * g_sparse ||^2
3047 where g_sparse only contains the supported elements from the gradient and D_sparse only the supported columns from the dictionary.
3048 */
3049 longdoublecxx . . $ longdouble# normsq_gs = 0.0; // squared norm of the sparse gradient
3050 for (integercxx . . $ integer# ig = 1; ig <= gradient.sizecxx . . $ autovector#size.; ig ++) {
3051 if (support [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ig])
3052 normsq_gs += gradient [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ig] * gradient [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ig];
3053 }
3054 longdoublecxx . . $ longdouble# normsq_dgs = 0.0; // squared norm of the transformed sparse gradient
3055 for (integercxx . . $ integer# irow = 1; irow <= dictionary.nrowcxx . . $ constmatrixview#nrow.; irow ++) {
3056 longdoublecxx . . $ longdouble# sum = 0.0;
3057 for (integercxx . . $ integer# icol = 1; icol <= dictionary.ncolcxx . . $ constmatrixview#ncol.; icol ++)
3058 if (support [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icol])
3059 sum += dictionary [cxx . . $ constmatrixview#`operator[]`(7a6174ed67b49158).irow] [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).icol] * gradient [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icol];
3060 normsq_dgs += sum * sum;
3061 }
3062
3063 double stepSize = normsq_gs / normsq_dgs;
3064
3065 double normsq_ratio = updatecxx . . $ update(6844d3adbb82ffb6). (x_new.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), yfromx_new.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), support_newcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)., stepSize, gradientcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), dictionary, yfromxcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), numberOfNonZeros, buffer.getcxx . . $ autovector#get(e8d1da7fe68a6585).());
3066
3067 if (! NUMequalcxx . . $ NUMequal(6596de982874463a). (support.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), support_new.getcxx . . $ autovector#get(e8d1da7fe68a6585).())) {
3068 double omega;
3069 const double kappa = 2.0, c = 0.0;
3070 while (stepSize > (omega = (1.0 - c) * normsq_ratio)) { // stepSize > omega, from Eq. 14
3071 stepSize *= 1.0 / (kappa * (1.0 - c));
3072 normsq_ratio = updatecxx . . $ update(6844d3adbb82ffb6). (x_new.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), yfromx_new.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), support_newcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), xcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)., stepSize, gradientcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), dictionary, yfromxcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), numberOfNonZeros, buffer.getcxx . . $ autovector#get(e8d1da7fe68a6585).());
3073 }
3074 }
3075
3076 ydif.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(ce74bae6c180f663). y -cxx . . $ operator-(fcdbb1279f7b512e). yfromx_newcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).(); // y - D.x(n+1)
3077
3078 const double rms_new = NUMsum2cxx . . $ NUMsum2(fa513f1eb826a064). (ydifcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()) / y.sizecxx . . $ constvectorview#size.;
3079 const double relativeError = fabscxx . . $ fabs(fee4f1d22bc8e617). (rms - rms_new) / rms_y;
3080 convergence = relativeError < tolerance;
3081 if (infoLevel > 1)
3082 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"\t\tIteration: ", iter, U", error: ", rms_new, U" relative: ", relativeError, U" stepSize: ", stepSize);
3083
3084 x <<=cxx . . $ `operator<<=`(adea042513329390). x_newcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).();
3085 support.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(564e2b2a15b4f2d8). support_newcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).();
3086 yfromx.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). yfromx_newcxx . . $ constvectorview#constvectorview(5c6f246bf1d99401)..allcxx . . $ autovector#all(a26726dad87555bb).();
3087 rms = rms_new;
3088 iter ++;
3089 }
3090 iter = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (iter, maximumNumberOfIterations);
3091 if (infoLevel >= 1)
3092 MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"\t\tNumber of iterations: ", iter, U" Difference squared: ", rms);
3093 if (infoLevel > 0)
3094 MelderInfo_draincxx . . $ MelderInfo_drain(49f6e7a06ebc5aa8).();
3095 } catch (MelderErrorcxx . . $ MelderError#) {
3096 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Solution of sparse problem not found.");
3097 }
3098 }
3099
3100 void NUMpolynomial_recurrencecxx . . $ NUMpolynomial_recurrence(627eaf5e3a164f6a). (VECcxx . . $ VEC# const& pn, double a, double b, double c, constVECcxx . . $ constVEC# const& pnm1, constVECcxx . . $ constVEC# const& pnm2) {
3101 const integercxx . . $ integer# degree = pn.sizecxx . . $ vector#size. - 1;
3102 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (degree > 1 && pnm1.size >= pn.size && pnm2.size >= pn.size);
3103
3104 pn [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = b * pnm1 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1] + c * pnm2 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).1];
3105 for (integercxx . . $ integer# i = 2; i <= degree - 1; i ++)
3106 pn [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = a * pnm1 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1] + b * pnm1 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] + c * pnm2 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i];
3107 pn [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).degree] = a * pnm1 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).degree - 1] + b * pnm1 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).degree];
3108 pn [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).degree + 1] = a * pnm1 [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).degree];
3109 }
3110
3111 /* End of file NUM2.cpp */