282 {
283
284 const double zero = 0.0;
285 double root = zero;
286 if (degree == 1) {
287 int32_t numRoots;
288 if (c[1] != zero) {
289 root = -c[0] / c[1];
290 numRoots = 1;
291 }
292 else if (c[0] == zero) {
293 root = zero;
294 numRoots = 1;
295 }
296 else
297 numRoots = 0;
298
299 if (numRoots > 0 && tmin <= root && root <= tmax) {
300 roots[0] = root;
301 return 1;
302 }
303 return 0;
304 }
305
306
307
308
309
310
311
312 const int32_t derivDegree = degree - 1;
313 std::vector<double> derivCoeff(static_cast<size_t>(derivDegree) + 1);
314 std::vector<double> derivRoots(derivDegree);
315 for (int32_t i = 0, ip1 = 1; i <= derivDegree; ++i, ++ip1) {
316 derivCoeff[i] = c[ip1] * (double)(ip1) / (double)degree;
317 }
318 const int32_t numDerivRoots =
FindRecursive(degree - 1, &derivCoeff[0], tmin, tmax, maxIterations, &derivRoots[0]);
319
320 int32_t numRoots = 0;
321 if (numDerivRoots > 0) {
322
323 if (
Find(degree, c, tmin, derivRoots[0], maxIterations, root))
324 roots[numRoots++] = root;
325
326
327 for (int32_t i = 0, ip1 = 1; i <= numDerivRoots - 2; ++i, ++ip1) {
328 if (
Find(degree, c, derivRoots[i], derivRoots[ip1], maxIterations, root))
329 roots[numRoots++] = root;
330 }
331
332
333 if (
Find(degree, c, derivRoots[
static_cast<size_t>(numDerivRoots) - 1], tmax, maxIterations, root))
334 roots[numRoots++] = root;
335 }
336 else {
337
338 if (
Find(degree, c, tmin, tmax, maxIterations, root))
339 roots[numRoots++] = root;
340 }
341 return numRoots;
342 }
static int32_t Find(int32_t degree, const double *c, uint32_t maxIterations, double *roots)
Definition MeasureUtils.hpp:195