| 1 | /* Roots.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2020 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "NUMlapack.h"cxx . . $ `<file>/dwsys/NUMlapack.h`/ |
| 20 | #include "NUMmachar.h"cxx . . $ `<file>/dwsys/NUMmachar.h`/ |
| 21 | #include "Polynomial.h"cxx . . $ `<file>/dwsys/Polynomial.h`/ |
| 22 | #include "Roots.h"cxx . . $ `<file>/dwsys/Roots.h`/ |
| 23 | |
| 24 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 25 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 26 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 27 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 28 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 29 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 30 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 31 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 32 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 33 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 34 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 35 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 36 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 37 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 38 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 39 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 40 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 41 | #include "Roots_def.h"cxx . . $ `<file>/dwsys/Roots_def.h`/ |
| 42 | |
| 43 | |
| 44 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Roots, Daata, 1); |
| 45 | |
| 46 | void structRoots :: v1_info () { |
| 47 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 48 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Number of roots: ", numberOfRoots); |
| 49 | } |
| 50 | |
| 51 | autoRoots Roots_createcxx . . $ Roots_create(27f7be40982d89fc). (integercxx . . $ integer# numberOfRoots) { |
| 52 | try { |
| 53 | autoRoots me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Roots); |
| 54 | mycxx . . $ `melder/melder.h:44:9`! numberOfRoots = numberOfRoots; |
| 55 | mycxx . . $ `melder/melder.h:44:9`! roots =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_COMPVECcxx . . $ zero_COMPVEC(77d5c134260f0bd0). (numberOfRoots); |
| 56 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 57 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 58 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Roots not created."); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | integercxx . . $ integer# Roots_getNumberOfRootscxx . . $ Roots_getNumberOfRoots(ff6c6b0c7b042759). (constRoots me) { |
| 63 | return mycxx . . $ `melder/melder.h:44:9`! numberOfRoots; |
| 64 | } |
| 65 | |
| 66 | dcomplexcxx . . $ dcomplex# Roots_getRootcxx . . $ Roots_getRoot(5013e1763267f53b). (constRoots me, integercxx . . $ integer# index) { |
| 67 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (index > 0 && index <= mycxx . . $ `melder/melder.h:44:9`! numberOfRoots, |
| 68 | U"Root index out of range."); |
| 69 | return mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index]; |
| 70 | } |
| 71 | |
| 72 | void Roots_fixIntoUnitCirclecxx . . $ Roots_fixIntoUnitCircle(ed161181b88d194d). (mutableRoots me) { |
| 73 | dcomplexcxx . . $ dcomplex# z10cxx . . $ std/complex#complex(bc12c9ec15586e6a). { 1.0, 0.0 }; |
| 74 | for (integercxx . . $ integer# iroot = 1; iroot <= mycxx . . $ `melder/melder.h:44:9`! numberOfRoots; iroot ++) |
| 75 | if (abscxx . . $ std/abs(bb57395c51337e15). (mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot]) > 1.0) |
| 76 | mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot] =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). z10 /cxx . . $ std/`operator/`(804c2413f71972e2). conjcxx . . $ std/conj(a358fd9634d01384). (mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot]); |
| 77 | } |
| 78 | |
| 79 | static void NUMdcvector_extrema_recxx . . $ NUMdcvector_extrema_re(3a5fc6c17b31a679). (COMPVECcxx . . $ COMPVEC# const& v, integercxx . . $ integer# lo, integercxx . . $ integer# hi, double *out_min, double *out_max) { |
| 80 | double min = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).lo].realcxx . . $ std/complex#real(21b6c25e127938e2).(), max = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).lo].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 81 | for (integercxx . . $ integer# i = lo + 1; i <= hi; i ++) |
| 82 | if (v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).() < min) |
| 83 | min = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 84 | else if (v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).() > max) |
| 85 | max = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 86 | if (out_min) |
| 87 | *out_min = min; |
| 88 | if (out_max) |
| 89 | *out_max = max; |
| 90 | } |
| 91 | |
| 92 | static void NUMdcvector_extrema_imcxx . . $ NUMdcvector_extrema_im(3a5fc6c17b31a679). (COMPVECcxx . . $ COMPVEC# const& v, integercxx . . $ integer# lo, integercxx . . $ integer# hi, double *out_min, double *out_max) { |
| 93 | double min = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).lo].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(), max = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).lo].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 94 | for (integercxx . . $ integer# i = lo + 1; i <= hi; i ++) |
| 95 | if (v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() < min) |
| 96 | min = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 97 | else if (v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() > max) |
| 98 | max = v [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 99 | if (out_min) |
| 100 | *out_min = min; |
| 101 | if (out_max) |
| 102 | *out_max = max; |
| 103 | } |
| 104 | |
| 105 | void Roots_drawcxx . . $ Roots_draw(f7f3f81724c0846f). (constRoots me, Graphicscxx . . $ Graphics# g, double rmin, double rmax, double imin, double imax, |
| 106 | conststring32cxx . . $ conststring32# symbol, double fontSize, bool garnish) { |
| 107 | const double oldFontSize = Graphics_inqFontSizecxx . . $ Graphics_inqFontSize(f906975acce41a59). (g); |
| 108 | const double eps = 1e-6; |
| 109 | |
| 110 | if (rmax <= rmin) |
| 111 | NUMdcvector_extrema_recxx . . $ NUMdcvector_extrema_re(3a5fc6c17b31a679). (mycxx . . $ `melder/melder.h:44:9`! roots.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1, mycxx . . $ `melder/melder.h:44:9`! numberOfRoots, & rmin, & rmax); |
| 112 | |
| 113 | double denominator = fabscxx . . $ fabs(fee4f1d22bc8e617). (rmax) > fabscxx . . $ fabs(fee4f1d22bc8e617). (rmin) ? fabscxx . . $ fabs(fee4f1d22bc8e617). (rmax) : fabscxx . . $ fabs(fee4f1d22bc8e617). (rmin); |
| 114 | if (denominator == 0.0) |
| 115 | denominator = 1.0; |
| 116 | if (fabscxx . . $ fabs(fee4f1d22bc8e617). ((rmax - rmin) / denominator) < eps) { |
| 117 | rmin -= 1.0; |
| 118 | rmax += 1.0; |
| 119 | } |
| 120 | if (imax <= imin) |
| 121 | NUMdcvector_extrema_imcxx . . $ NUMdcvector_extrema_im(3a5fc6c17b31a679). (mycxx . . $ `melder/melder.h:44:9`! roots.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), 1, mycxx . . $ `melder/melder.h:44:9`! numberOfRoots, & imin, & imax); |
| 122 | denominator = fabscxx . . $ fabs(fee4f1d22bc8e617). (imax) > fabscxx . . $ fabs(fee4f1d22bc8e617). (imin) ? fabscxx . . $ fabs(fee4f1d22bc8e617). (imax) : fabscxx . . $ fabs(fee4f1d22bc8e617). (imin); |
| 123 | if (denominator == 0.0) |
| 124 | denominator = 1.0; |
| 125 | if (fabscxx . . $ fabs(fee4f1d22bc8e617). ((imax - imin) / denominator) < eps) { |
| 126 | imin -= 1; |
| 127 | imax += 1; |
| 128 | } |
| 129 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 130 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, rmin, rmax, imin, imax); |
| 131 | Graphics_setFontSizecxx . . $ Graphics_setFontSize(2514a1c07b6be5e2). (g, fontSize); |
| 132 | Graphics_setTextAlignmentcxx . . $ Graphics_setTextAlignment(a697afee548943ae). (g, kGraphics_horizontalAlignmentcxx . . $ kGraphics_horizontalAlignment#::CENTREcxx . . $ kGraphics_horizontalAlignment#CENTRE., Graphics_HALFcxx . . $ `sys/Graphics.h:253:9`!); |
| 133 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! numberOfRoots; i ++) { |
| 134 | const double re = mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).(), im = mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 135 | if (re >= rmin && re <= rmax && im >= imin && im <= imax) |
| 136 | Graphics_textcxx . . $ Graphics_text(8e1820bb471fc8c8). (g, re, im, symbol); |
| 137 | } |
| 138 | Graphics_setFontSizecxx . . $ Graphics_setFontSize(2514a1c07b6be5e2). (g, oldFontSize); |
| 139 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 140 | if (garnish) { |
| 141 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 142 | if (rmin * rmax < 0.0) |
| 143 | Graphics_markLeftcxx . . $ Graphics_markLeft(e82cc8dc402e8bd7). (g, 0.0, true, true, true, U"0"); |
| 144 | if (imin * imax < 0.0) |
| 145 | Graphics_markBottomcxx . . $ Graphics_markBottom(e82cc8dc402e8bd7). (g, 0.0, true, true, true, U"0"); |
| 146 | Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 147 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, U"Imaginary part"); |
| 148 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 149 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Real part"); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | autoRoots Polynomial_to_Rootscxx . . $ Polynomial_to_Roots(877cc1a78227ca1). (constPolynomialcxx . . $ constPolynomial# me) { |
| 154 | try { |
| 155 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients == mycxx . . $ `melder/melder.h:44:9`! coefficients.size); // check invariant |
| 156 | integercxx . . $ integer# np1 = mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients., n = np1 - 1; |
| 157 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (n > 0, |
| 158 | U"Cannot find roots of a constant function."); |
| 159 | /* |
| 160 | Allocate storage for a special upper Hessenberg matrix (n * n) |
| 161 | The roots of a polynomial are the eigenvalues of an |
| 162 | upper Hessenberg matrix with the coefficients of the polynomial. |
| 163 | See for example the introduction in: |
| 164 | G.S. Ammar, D. Calvetti, W.B. Gragg, L. Reichel (2001): |
| 165 | "Polynomial zero finders based on Szegö polynomials.", |
| 166 | Journal of Computational and Applied Mathematics 127: 1-–16. |
| 167 | */ |
| 168 | autoVECcxx . . $ autoVEC# wr = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (n); |
| 169 | autoVECcxx . . $ autoVEC# wi = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (n); |
| 170 | autoMATcxx . . $ autoMAT# upperHessenberg = zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (n, n); |
| 171 | MATVUcxx . . $ MATVU# uh_CMcxx . . $ matrixview#matrixview(eed9513d24570b3d). (upperHessenberg.getcxx . . $ automatrix#get(b2b81450b5bdda66).()); |
| 172 | uh_CM.rowStridecxx . . $ matrixview#rowStride. = 1; uh_CM.colStridecxx . . $ matrixview#colStride. = n; |
| 173 | |
| 174 | uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).1] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).n] = - (mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] / mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).np1]); |
| 175 | for (integercxx . . $ integer# irow = 2; irow <= n; irow ++) { |
| 176 | uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).n] = - (mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).irow] / mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).np1]); |
| 177 | uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).irow - 1] = 1.0; |
| 178 | } |
| 179 | /* |
| 180 | Find out the working storage needed |
| 181 | */ |
| 182 | double wtmp; |
| 183 | integercxx . . $ integer# lwork = -1, info; |
| 184 | NUMlapack_dhseqr_cxx . . $ NUMlapack_dhseqr_(fd6d6f6c630bffc7). ("E", "N", n, 1, n, & upperHessenberg [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], n, & wr [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1], & wi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1], nullptr, n, & wtmp, lwork, & info); |
| 185 | lwork = Melder_roundUpcxx . . $ Melder_roundUp(447c67c6676f75c3). (wtmp); |
| 186 | autoVECcxx . . $ autoVEC# work = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (lwork); |
| 187 | /* |
| 188 | Find eigenvalues/roots. |
| 189 | */ |
| 190 | NUMlapack_dhseqr_cxx . . $ NUMlapack_dhseqr_(fd6d6f6c630bffc7). ("E", "N", n, 1, n, & upperHessenberg [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], n, & wr [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1], & wi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1], nullptr, n, & work [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1], lwork, & info); |
| 191 | |
| 192 | integercxx . . $ integer# numberOfEigenvaluesFound = n, ioffset = 0; |
| 193 | if (info > 0) { |
| 194 | /* |
| 195 | if INFO = i, NUMlapack_dhseqr_ failed to compute all of the eigenvalues. Elements i+1:n of |
| 196 | WR and WI contain those eigenvalues which have been successfully computed |
| 197 | */ |
| 198 | numberOfEigenvaluesFound -= info; |
| 199 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfEigenvaluesFound > 0, |
| 200 | U"No eigenvalues found."); |
| 201 | ioffset = info; |
| 202 | } else if (info < 0) { |
| 203 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"NUMlapack_dhseqr_ returns error ", info, U"."); |
| 204 | } |
| 205 | |
| 206 | autoRoots thee = Roots_createcxx . . $ Roots_create(27f7be40982d89fc). (numberOfEigenvaluesFound); |
| 207 | for (integercxx . . $ integer# i = 1; i <= numberOfEigenvaluesFound; i ++) { |
| 208 | thycxx . . $ `melder/melder.h:45:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. realcxx . . $ std/complex#real(ba8836b59fb42830). (wr [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ioffset + i]); |
| 209 | thycxx . . $ `melder/melder.h:45:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. imagcxx . . $ std/complex#imag(ba8836b59fb42830). (wi [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ioffset + i]); |
| 210 | } |
| 211 | Roots_Polynomial_polishcxx . . $ Roots_Polynomial_polish(2f1d9b63ffdab6ed). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 212 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 213 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 214 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no roots can be calculated."); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | workspace.size >= n * n + 2 * n + 11 * n = |
| 220 | n * n ; for hessenberg matrix |
| 221 | + 2 * n ; for real and imaginary parts |
| 222 | + 11 * n ; the maximum for dhseqr_ |
| 223 | */ |
| 224 | void Polynomial_into_Rootscxx . . $ Polynomial_into_Roots(ecf59616bf38ddc0). (constPolynomialcxx . . $ constPolynomial# me, mutableRoots roots, VECcxx . . $ VEC# workspace) { |
| 225 | integercxx . . $ integer# np1 = mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients., n = np1 - 1; |
| 226 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (workspace.size >= n * n + 2 * n + 11 * n); |
| 227 | |
| 228 | if (n == 0) |
| 229 | return; |
| 230 | /* |
| 231 | Use the workspace reserve storage for Hessenberg matrix (n * n) |
| 232 | */ |
| 233 | integercxx . . $ integer# size = n * n; |
| 234 | VECcxx . . $ VEC# uh = workspace.partcxx . . $ vector#part(ae267db9e2c04be7). (1, size); |
| 235 | MATcxx . . $ MAT# upperHessenberg = uh.asmatrixcxx . . $ vector#asmatrix(2dd9a590cd4c0fe2). (n, n); |
| 236 | upperHessenbergcxx . . $ matrixview#matrixview(eed9513d24570b3d). <<=cxx . . $ `operator<<=`(50b469ae42a42f64). 0.0; |
| 237 | MATVUcxx . . $ MATVU# uh_CMcxx . . $ matrixview#matrixview(eed9513d24570b3d). (upperHessenberg); |
| 238 | uh_CM.rowStridecxx . . $ matrixview#rowStride. = 1; uh_CM.colStridecxx . . $ matrixview#colStride. = n; |
| 239 | uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).1] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).n] = - (mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] / mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).np1]); |
| 240 | for (integercxx . . $ integer# irow = 2; irow <= n; irow ++) { |
| 241 | uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).n] = - (mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).irow] / mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).np1]); |
| 242 | uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).irow] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).irow - 1] = 1.0; |
| 243 | } |
| 244 | /* |
| 245 | We don't need to find out size of the working storage needed because for the current version |
| 246 | of NUMlapack_dhseqr (20240608) its size equals maximally 11*n. |
| 247 | */ |
| 248 | VECcxx . . $ VEC# wr = workspace.partcxx . . $ vector#part(ae267db9e2c04be7). (size + 1, size + n); |
| 249 | size += n; |
| 250 | VECcxx . . $ VEC# wi = workspace.partcxx . . $ vector#part(ae267db9e2c04be7). (size + 1, size + n); |
| 251 | size += n; |
| 252 | VECcxx . . $ VEC# work = workspace.partcxx . . $ vector#part(ae267db9e2c04be7). (size + 1, size + 11 * n); |
| 253 | |
| 254 | integercxx . . $ integer# lwork = work.sizecxx . . $ vector#size., info; |
| 255 | NUMlapack_dhseqr_cxx . . $ NUMlapack_dhseqr_(fd6d6f6c630bffc7). ("E", "N", n, 1, n, & uh_CM [cxx . . $ matrixview#`operator[]`(a8b9553e85c2b1fc).1] [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).1], n, & wr [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], & wi [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], nullptr, n, & work [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], lwork, & info); |
| 256 | integercxx . . $ integer# numberOfEigenvaluesFound = n, ioffset = 0; |
| 257 | if (info > 0) { |
| 258 | /* |
| 259 | if INFO = i, NUMlapack_dhseqr failed to compute all of the eigenvalues. |
| 260 | Elements i+1:n of WR and WI contain those eigenvalues which have been successfully computed |
| 261 | */ |
| 262 | numberOfEigenvaluesFound -= info; |
| 263 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfEigenvaluesFound > 0, |
| 264 | U"No eigenvalues found."); |
| 265 | ioffset = info; |
| 266 | } else if (info < 0) { |
| 267 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"NUMlapack_dhseqr_ returns error ", info, U"."); |
| 268 | } |
| 269 | |
| 270 | roots -> resize (numberOfEigenvaluesFound); |
| 271 | for (integercxx . . $ integer# i = 1; i <= numberOfEigenvaluesFound; i ++) { |
| 272 | roots -> roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(ba8836b59fb42830). (wr [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ioffset + i]) ; |
| 273 | roots -> roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(ba8836b59fb42830). (wi [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ioffset + i]); |
| 274 | } |
| 275 | Roots_Polynomial_polishcxx . . $ Roots_Polynomial_polish(2f1d9b63ffdab6ed). (roots, me); |
| 276 | } |
| 277 | |
| 278 | void Roots_sortcxx . . $ Roots_sort(ed161181b88d194d). (Roots me) { |
| 279 | (void) me; |
| 280 | } |
| 281 | |
| 282 | /* Get value and derivative */ |
| 283 | static void Polynomial_evaluateWithDerivative_zcxx . . $ Polynomial_evaluateWithDerivative_z(df1e701162c09024). (constPolynomialcxx . . $ constPolynomial# me, dcomplexcxx . . $ dcomplex# *in_z, dcomplexcxx . . $ dcomplex# *out_p, dcomplexcxx . . $ dcomplex# *out_dp) { |
| 284 | longdoublecxx . . $ longdouble# pr = mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.], pi = 0.0; |
| 285 | longdoublecxx . . $ longdouble# dpr = 0.0, dpi = 0.0, x = in_z->realcxx . . $ std/complex#real(21b6c25e127938e2).(), y = in_z->imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); |
| 286 | |
| 287 | for (integercxx . . $ integer# i = mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1; i > 0; i --) { |
| 288 | longdoublecxx . . $ longdouble# tr = dpr; |
| 289 | dpr = dpr * x - dpi * y + pr; |
| 290 | dpi = tr * y + dpi * x + pi; |
| 291 | tr = pr; |
| 292 | pr = pr * x - pi * y + mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 293 | pi = tr * y + pi * x; |
| 294 | } |
| 295 | if (out_p) |
| 296 | *out_p =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). {cxx . . $ std/complex#complex(bc12c9ec15586e6a). (double) pr, (double) pi }; |
| 297 | if (out_dp) |
| 298 | *out_dp =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). {cxx . . $ std/complex#complex(bc12c9ec15586e6a). (double) dpr, (double) dpi }; |
| 299 | } |
| 300 | |
| 301 | static void Polynomial_polish_complexroot_nrcxx . . $ Polynomial_polish_complexroot_nr(601d3d342d01666e). (constPolynomialcxx . . $ constPolynomial# me, dcomplexcxx . . $ dcomplex# *root, integercxx . . $ integer# maxit) { |
| 302 | if (! NUMfppcxx . . $ NUMfpp.) |
| 303 | NUMmacharcxx . . $ NUMmachar(49f6e7a06ebc5aa8). (); |
| 304 | dcomplexcxx . . $ dcomplex# zbest = *cxx . . $ std/complex#complex(20ef1611af677bca).root; |
| 305 | double ymin = 1e308; |
| 306 | for (integercxx . . $ integer# iter = 1; iter <= maxit; iter ++) { |
| 307 | dcomplexcxx . . $ dcomplex# ycxx . . $ std/complex#complex(bc12c9ec15586e6a)., dycxx . . $ std/complex#complex(bc12c9ec15586e6a).; |
| 308 | Polynomial_evaluateWithDerivative_zcxx . . $ Polynomial_evaluateWithDerivative_z(df1e701162c09024). (me, root, & y, & dy); |
| 309 | const double fabsy = abscxx . . $ std/abs(bb57395c51337e15). (y); |
| 310 | if (fabsy > ymin || fabscxx . . $ fabs(fee4f1d22bc8e617). (fabsy - ymin) < NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps.) { |
| 311 | /* |
| 312 | We stop, because the approximation is getting worse. |
| 313 | Return the previous (hitherto best) value for z. |
| 314 | */ |
| 315 | *root =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). zbest; |
| 316 | return; |
| 317 | } |
| 318 | ymin = fabsy; |
| 319 | zbest =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). *root; |
| 320 | if (abscxx . . $ std/abs(bb57395c51337e15). (dy) == 0.0) |
| 321 | return; |
| 322 | const dcomplexcxx . . $ dcomplex# dz = y /cxx . . $ std/`operator/`(804c2413f71972e2). dy; // Newton-Raphson |
| 323 | *root -=cxx . . $ std/complex#`operator-=`(f1f782a256ae8b93). dz; |
| 324 | } |
| 325 | // Melder_throw (U"Maximum number of iterations exceeded."); |
| 326 | } |
| 327 | |
| 328 | static double Polynomial_polish_realrootcxx . . $ Polynomial_polish_realroot(dceb7b5c779db4c0). (constPolynomialcxx . . $ constPolynomial# me, double x, integercxx . . $ integer# maxit) { |
| 329 | if (! NUMfppcxx . . $ NUMfpp.) |
| 330 | NUMmacharcxx . . $ NUMmachar(49f6e7a06ebc5aa8). (); |
| 331 | double xbest = x, ymin = 1e308; |
| 332 | for (integercxx . . $ integer# iter = 1; iter <= maxit; iter ++) { |
| 333 | double y, dy; |
| 334 | Polynomial_evaluateWithDerivativecxx . . $ Polynomial_evaluateWithDerivative(df1c6b65354818cb). (me, x, & y, & dy); |
| 335 | const double fabsy = fabscxx . . $ fabs(fee4f1d22bc8e617). (y); |
| 336 | if (fabsy > ymin || fabscxx . . $ fabs(fee4f1d22bc8e617). (fabsy - ymin) < NUMfppcxx . . $ NUMfpp. -> epscxx . . $ structmachar_Table#eps.) { |
| 337 | /* |
| 338 | We stop, because the approximation is getting worse or we cannot get any closer. |
| 339 | Return the previous (hitherto best) value for x. |
| 340 | */ |
| 341 | x = xbest; |
| 342 | return x; |
| 343 | } |
| 344 | ymin = fabsy; |
| 345 | xbest = x; |
| 346 | if (fabscxx . . $ fabs(fee4f1d22bc8e617). (dy) == 0.0) |
| 347 | return x; |
| 348 | const double dx = y / dy; // Newton-Raphson |
| 349 | x -= dx; |
| 350 | } |
| 351 | return x; |
| 352 | // Melder_throw (U"Maximum number of iterations exceeded."); |
| 353 | } |
| 354 | |
| 355 | // Precondition: complex roots occur in pairs (a,bi), (a,-bi) with b>0 |
| 356 | void Roots_Polynomial_polishcxx . . $ Roots_Polynomial_polish(2f1d9b63ffdab6ed). (mutableRoots me, constPolynomialcxx . . $ constPolynomial# thee) { |
| 357 | const integercxx . . $ integer# maxit = 80; |
| 358 | integercxx . . $ integer# i = 1; |
| 359 | while (i <= mycxx . . $ `melder/melder.h:44:9`! numberOfRoots) { |
| 360 | const double im = mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(), re = mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 361 | if (im != 0.0) { |
| 362 | Polynomial_polish_complexroot_nrcxx . . $ Polynomial_polish_complexroot_nr(601d3d342d01666e). (thee, & mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i], maxit); |
| 363 | if (i < mycxx . . $ `melder/melder.h:44:9`! numberOfRoots && im == - mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() && re == mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1].realcxx . . $ std/complex#real(21b6c25e127938e2).()) { |
| 364 | mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1]. realcxx . . $ std/complex#real(ba8836b59fb42830). (mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).()); |
| 365 | mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1]. imagcxx . . $ std/complex#imag(ba8836b59fb42830). (- mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].imagcxx . . $ std/complex#imag(21b6c25e127938e2).()); |
| 366 | i ++; |
| 367 | } |
| 368 | } else { |
| 369 | mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. realcxx . . $ std/complex#real(ba8836b59fb42830). (Polynomial_polish_realrootcxx . . $ Polynomial_polish_realroot(dceb7b5c779db4c0). (thee, mycxx . . $ `melder/melder.h:44:9`! roots [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i].realcxx . . $ std/complex#real(21b6c25e127938e2).(), maxit)); |
| 370 | } |
| 371 | i ++; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | autoPolynomialcxx . . $ autoPolynomial# Roots_to_Polynomialcxx . . $ Roots_to_Polynomial(a1d77b5dd50e0fb0). (Roots me, bool rootsAreReal) { |
| 376 | try { |
| 377 | (void) me; |
| 378 | autoPolynomialcxx . . $ autoPolynomial# theecxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 379 | if (! rootsAreReal) |
| 380 | throw MelderErrorcxx . . $ MelderError#(); |
| 381 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 382 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 383 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Not implemented yet"); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* End of file Roots.cpp */ |