| 1 | /* Spline.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 "Spline.h" |
| 20 | #include "NUM2.h" |
| 21 | |
| 22 | #include "oo_DESTROY.h" |
| 23 | #include "Spline_def.h" |
| 24 | #include "oo_COPY.h" |
| 25 | #include "Spline_def.h" |
| 26 | #include "oo_EQUAL.h" |
| 27 | #include "Spline_def.h" |
| 28 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 | #include "Spline_def.h" |
| 30 | #include "oo_WRITE_TEXT.h" |
| 31 | #include "Spline_def.h" |
| 32 | #include "oo_WRITE_BINARY.h" |
| 33 | #include "Spline_def.h" |
| 34 | #include "oo_READ_TEXT.h" |
| 35 | #include "Spline_def.h" |
| 36 | #include "oo_READ_BINARY.h" |
| 37 | #include "Spline_def.h" |
| 38 | #include "oo_DESCRIPTION.h" |
| 39 | #include "Spline_def.h" |
| 40 | |
| 41 | /* |
| 42 | Functions for calculating an mspline and an ispline. These functions should replace |
| 43 | the functions in NUM2.c. Before we can do that we first have to adapt the spline- |
| 44 | dependencies in MDS.c. |
| 45 | |
| 46 | Formally nKnots == order + numberOfInteriorKnots + order. |
| 47 | We forget about the multiple knots at start and end. |
| 48 | Our point-sequece xmin < interiorKont [1] < ... < interiorKnot [n] < xmax. |
| 49 | nKnots is now numberOfinteriorKnots + 2. |
| 50 | */ |
| 51 | static double NUMmspline2 (constVEC points, integer order, integer index, double x) { |
| 52 | const integer numberOfSplines = points.size + order - 2; |
| 53 | double m [Spline_MAXIMUM_DEGREE + 2]; |
| 54 | |
| 55 | Melder_assert (points.size > 2 && order > 0 && index > 0); |
| 56 | |
| 57 | if (index > numberOfSplines) |
| 58 | return undefined; |
| 59 | |
| 60 | /* |
| 61 | Find the range/interval where x is located. |
| 62 | M-splines of order k have degree k-1. |
| 63 | M-splines are zero outside interval [knot [i], knot [i+order]). |
| 64 | First and last 'order' knots are equal, i.e., |
| 65 | knot [1] = ... = knot [order] && knot [nKnots-order+1] = ... knot [nKnots]. |
| 66 | */ |
| 67 | |
| 68 | const integer index_b = Melder_clippedLeft (1_integer, index - order + 1); |
| 69 | if (x < points [index_b]) |
| 70 | return 0.0; |
| 71 | |
| 72 | const integer index_e = Melder_clippedRight (index_b + std::min (index, order), points.size); |
| 73 | if (x > points [index_e]) |
| 74 | return 0.0; |
| 75 | /* |
| 76 | Calculate M [i](x|1,t) according to eq.2. |
| 77 | */ |
| 78 | for (integer k = 1; k <= order; k ++) { |
| 79 | const integer k1 = index - order + k, k2 = k1 + 1; |
| 80 | m [k] = 0.0; |
| 81 | if (k1 > 0 && k2 <= points.size && x >= points [k1] && x < points [k2]) |
| 82 | m [k] = 1.0 / (points [k2] - points [k1]); |
| 83 | } |
| 84 | /* |
| 85 | Iterate to get M [i](x|k,t) |
| 86 | */ |
| 87 | for (integer k = 2; k <= order; k ++) { |
| 88 | for (integer j = 1; j <= order - k + 1; j ++) { |
| 89 | integer k1 = index - order + j, k2 = k1 + k; |
| 90 | if (k2 > 1 && k1 < 1) |
| 91 | k1 = 1; |
| 92 | else if (k2 > points.size && k1 < points.size) |
| 93 | k2 = points.size; |
| 94 | if (k1 > 0 && k2 <= points.size) { |
| 95 | const double p1 = points [k1], p2 = points [k2]; |
| 96 | m [j] = k * ((x - p1) * m [j] + (p2 - x) * m [j + 1]) / |
| 97 | ((k - 1) * (p2 - p1)); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | return m [1]; |
| 102 | } |
| 103 | |
| 104 | static double NUMispline2 (constVEC points, integer order, integer index, double x) { |
| 105 | Melder_assert (points.size > 2 && order > 0 && index > 0); |
| 106 | |
| 107 | const integer index_b = Melder_clippedLeft (1_integer, index - order + 1); |
| 108 | if (x < points [index_b]) |
| 109 | return 0.0; |
| 110 | |
| 111 | const integer index_e = Melder_clippedRight (index_b + std::min (index, order), points.size); |
| 112 | if (x > points [index_e]) |
| 113 | return 1.0; |
| 114 | |
| 115 | integer j; |
| 116 | for (j = index_e - 1; j >= index_b; j--) |
| 117 | if (x > points [j]) |
| 118 | break; |
| 119 | /* |
| 120 | Equation 5 in Ramsay's article contains some errors!!! |
| 121 | 1. the interval selection must be 'j-k <= i <= j' instead of |
| 122 | 'j-k+1 <= i <= j' |
| 123 | 2. the summation index m starts at 'i+1' instead of 'i' |
| 124 | */ |
| 125 | longdouble y = 0.0; |
| 126 | for (integer m = index + 1; m <= j + order; m ++) { |
| 127 | integer km = m - order, kmp = km + order + 1; |
| 128 | Melder_clipLeft (1_integer, & km); |
| 129 | Melder_clipRight (& kmp, points.size); |
| 130 | y += (points [kmp] - points [km]) * NUMmspline2 (points, order + 1, m, x); |
| 131 | } |
| 132 | return double (y / (order + 1)); |
| 133 | } |
| 134 | |
| 135 | Thing_implement (Spline, FunctionSeries, 0); |
| 136 | |
| 137 | double structSpline :: v_evaluate (double /* x */) { |
| 138 | return 0.0; |
| 139 | } |
| 140 | |
| 141 | integer structSpline :: v_getDegree () { |
| 142 | return degree; |
| 143 | } |
| 144 | |
| 145 | integer structSpline :: v_getOrder () { |
| 146 | return degree + 1; |
| 147 | } |
| 148 | |
| 149 | /* Precondition: FunctionSeries part inited + degree */ |
| 150 | static void Spline_initKnots (Spline me, integer degree, constVECVU const& interiorKnots_org) { |
| 151 | Melder_require (degree <= Spline_MAXIMUM_DEGREE, |
| 152 | U"Degree should be <= ", Spline_MAXIMUM_DEGREE, U"."); |
| 153 | |
| 154 | autoVEC interiorKnots = sort_e_VEC (interiorKnots_org); |
| 155 | Melder_require (interiorKnots [1] > my xmin && interiorKnots [interiorKnots.size] <= my xmax, |
| 156 | U"Knots should be inside domain."); |
| 157 | |
| 158 | my degree = degree; |
| 159 | const integer order = Spline_getOrder (me); /* depends on spline type !! */ |
| 160 | const integer n = interiorKnots.size + order; |
| 161 | Melder_require (my numberOfCoefficients == n, |
| 162 | U"Number of coefficients should equal ", n, U"."); |
| 163 | |
| 164 | my numberOfKnots = interiorKnots.size + 2; |
| 165 | my knots = zero_VEC (my numberOfKnots); |
| 166 | my knots.part (2, interiorKnots.size + 1) <<= interiorKnots.all(); |
| 167 | my knots [1] = my xmin; |
| 168 | my knots [my numberOfKnots] = my xmax; |
| 169 | } |
| 170 | |
| 171 | void Spline_init (Spline me, double xmin, double xmax, integer degree, integer numberOfCoefficients, integer numberOfKnots) { |
| 172 | Melder_require (degree <= Spline_MAXIMUM_DEGREE, |
| 173 | U"Degree should be <= ", Spline_MAXIMUM_DEGREE, U"."); |
| 174 | FunctionSeries_init (me, xmin, xmax, numberOfCoefficients); |
| 175 | my knots = zero_VEC (numberOfKnots); |
| 176 | my degree = degree; |
| 177 | my numberOfKnots = numberOfKnots; |
| 178 | my knots [1] = xmin; |
| 179 | my knots [numberOfKnots] = xmax; |
| 180 | } |
| 181 | |
| 182 | void Spline_drawKnots (Spline me, Graphics g, double xmin, double xmax, double ymin, double ymax, bool garnish) { |
| 183 | const integer order = Spline_getOrder (me); |
| 184 | Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 185 | if (xmax < my xmin || xmin > my xmax) |
| 186 | return; |
| 187 | |
| 188 | if (ymax <= ymin) |
| 189 | FunctionSeries_getExtrema (me, xmin, xmax, nullptr, & ymin, nullptr, & ymax); |
| 190 | |
| 191 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 192 | |
| 193 | if (my knots [1] >= xmin && my knots [1] <= xmax) { |
| 194 | Graphics_markTop (g, my knots [1], false, true, true, |
| 195 | ! garnish ? U"" : |
| 196 | order == 1 ? U"t__1_" : |
| 197 | order == 2 ? U"{t__1_, t__2_}" : |
| 198 | Melder_cat (U"{t__1_..t__", order, U"_}") |
| 199 | ); |
| 200 | } |
| 201 | for (integer i = 2; i <= my numberOfKnots - 1; i ++) { |
| 202 | if (my knots [i] >= xmin && my knots [i] <= xmax) { |
| 203 | Graphics_markTop (g, my knots [i], false, true, true, |
| 204 | ! garnish ? U"" : |
| 205 | Melder_cat (U"t__", i + order - 1, U"_") |
| 206 | ); |
| 207 | } |
| 208 | } |
| 209 | if (my knots [my numberOfKnots] >= xmin && my knots [my numberOfKnots] <= xmax) { |
| 210 | const integer numberOfKnots = ! garnish ? 0 : my numberOfKnots + 2 * (order - 1); |
| 211 | Graphics_markTop (g, my knots [my numberOfKnots], false, true, true, |
| 212 | ! garnish ? U"" : |
| 213 | order == 1 ? Melder_cat (U"t__", numberOfKnots, U"_") : |
| 214 | order == 2 ? Melder_cat (U"{t__", numberOfKnots - 1, U"_, t__", numberOfKnots, U"_}") : |
| 215 | Melder_cat (U"{t__", numberOfKnots - order + 1, U"_..t__", numberOfKnots, U"_}") |
| 216 | ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | integer Spline_getOrder (Spline me) { |
| 221 | return my v_getOrder (); |
| 222 | } |
| 223 | |
| 224 | autoSpline Spline_scaleX (Spline me, double xmin, double xmax) { |
| 225 | try { |
| 226 | Melder_assert (xmin < xmax); |
| 227 | |
| 228 | autoSpline thee = Data_copy (me); |
| 229 | |
| 230 | thy xmin = xmin; |
| 231 | thy xmax = xmax; |
| 232 | /* |
| 233 | x = a x + b |
| 234 | Constraints: |
| 235 | my xmin = a xmin + b; a = (my xmin - my xmax) / (xmin - xmax); |
| 236 | my xmax = a xmax + b; b = my xmin - a * xmin |
| 237 | */ |
| 238 | const double a = (xmin - xmax) / (my xmin - my xmax); |
| 239 | const double b = xmin - a * my xmin; |
| 240 | for (integer i = 1; i <= my numberOfKnots; i ++) |
| 241 | thy knots [i] = a * my knots [i] + b; |
| 242 | return thee; |
| 243 | } catch (MelderError) { |
| 244 | Melder_throw (U"Scaled Spline not created."); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /********* MSplines ************************************************/ |
| 249 | |
| 250 | double structMSpline :: v_evaluate (double x) { |
| 251 | if (x < our xmin || x > our xmax) |
| 252 | return 0.0; |
| 253 | double result = 0.0; |
| 254 | for (integer i = 1; i <= numberOfCoefficients; i ++) |
| 255 | if (coefficients [i] != 0.0) |
| 256 | result += coefficients [i] * NUMmspline2 (knots.get(), degree + 1, i, x); |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | void structMSpline :: v_evaluateTerms (double x, VEC terms) { |
| 261 | Melder_assert (terms.size == numberOfCoefficients); |
| 262 | if (x < our xmin || x > our xmax) |
| 263 | return; |
| 264 | for (integer i = 1; i <= numberOfCoefficients; i ++) |
| 265 | terms [i] = NUMmspline2 (knots.get(), degree + 1, i, x); |
| 266 | } |
| 267 | |
| 268 | Thing_implement (MSpline, Spline, 0); |
| 269 | |
| 270 | autoMSpline MSpline_create (double xmin, double xmax, integer degree, integer numberOfInteriorKnots) { |
| 271 | try { |
| 272 | autoMSpline me = Thing_new (MSpline); |
| 273 | const integer numberOfCoefficients = numberOfInteriorKnots + degree + 1; |
| 274 | const integer numberOfKnots = numberOfCoefficients + degree + 1; |
| 275 | Spline_init (me.get(), xmin, xmax, degree, numberOfCoefficients, numberOfKnots); |
| 276 | return me; |
| 277 | } catch (MelderError) { |
| 278 | Melder_throw (U"MSpline not created."); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | autoMSpline MSpline_createWithCoefficients (double xmin, double xmax, integer degree, constVECVU const& coefficients, constVECVU const& interiorKnots) { |
| 283 | try { |
| 284 | Melder_require (degree <= Spline_MAXIMUM_DEGREE, |
| 285 | U"Degree should be <= ", Spline_MAXIMUM_DEGREE, U"."); |
| 286 | autoMSpline me = Thing_new (MSpline); |
| 287 | FunctionSeries_initWithCoefficients (me.get(), xmin, xmax, coefficients, true); |
| 288 | Spline_initKnots (me.get(), degree, interiorKnots); |
| 289 | return me; |
| 290 | } catch (MelderError) { |
| 291 | Melder_throw (U"MSpline not created from strings."); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /******** ISplines ************************************************/ |
| 296 | |
| 297 | double structISpline :: v_evaluate (double x) { |
| 298 | if (x < our xmin || x > our xmax) |
| 299 | return 0.0; |
| 300 | double result = 0.0; |
| 301 | for (integer i = 1; i <= numberOfCoefficients; i ++) |
| 302 | if (coefficients [i] != 0.0) |
| 303 | result += coefficients [i] * NUMispline2 (knots.get(), degree, i, x); |
| 304 | return result; |
| 305 | } |
| 306 | |
| 307 | void structISpline :: v_evaluateTerms (double x, VEC terms) { |
| 308 | Melder_assert (terms.size == numberOfCoefficients); |
| 309 | for (integer i = 1; i <= numberOfCoefficients; i ++) |
| 310 | terms [i] = NUMispline2 (knots.get(), degree, i, x); |
| 311 | } |
| 312 | |
| 313 | integer structISpline :: v_getOrder () { |
| 314 | return degree; |
| 315 | } |
| 316 | |
| 317 | Thing_implement (ISpline, Spline, 0); |
| 318 | |
| 319 | autoISpline ISpline_create (double xmin, double xmax, integer degree, integer numberOfInteriorKnots) { |
| 320 | try { |
| 321 | autoISpline me = Thing_new (ISpline); |
| 322 | const integer numberOfCoefficients = numberOfInteriorKnots + degree; |
| 323 | const integer numberOfKnots = numberOfCoefficients + degree; |
| 324 | Spline_init (me.get(), xmin, xmax, degree, numberOfCoefficients, numberOfKnots); |
| 325 | return me; |
| 326 | } catch (MelderError) { |
| 327 | Melder_throw (U"ISpline not created."); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | autoISpline ISpline_createWithCoefficients (double xmin, double xmax, integer degree, constVECVU const& coefficients, constVECVU const& interiorKnots) { |
| 332 | try { |
| 333 | Melder_require (degree <= Spline_MAXIMUM_DEGREE, |
| 334 | U"Degree should should not exceed ", Spline_MAXIMUM_DEGREE); |
| 335 | autoISpline me = Thing_new (ISpline); |
| 336 | FunctionSeries_initWithCoefficients (me.get(), xmin, xmax, coefficients, true); |
| 337 | Spline_initKnots (me.get(), degree, interiorKnots); |
| 338 | return me; |
| 339 | } catch (MelderError) { |
| 340 | Melder_throw (U"ISpline not created from strings."); |
| 341 | }; |
| 342 | } |
| 343 | |
| 344 | /* end of file Spline.cpp */ |