// given f and its derivative f_, and a init x0, return its newton_iteration result to find a root static bool newton_root(long double x0, const Polynomial & f, const Polynomial & f_, long double & root) { root = x0; long double nf, nf_; int iter = 0; while (fabs(nf = f.func(root)) > smalleps) { nf_ = f_.func(root); if (fabs(nf_) < smalleps) return false; root = root - nf / nf_; iter++; if (iter >= 128) return false; } return true; }
// after we find a root of a polynomial, use this method to determine its multiple static int findRootMultiple(long double x, const Polynomial & f) { int ret = 1; Polynomial now = f; double zeroeps = 1e-6; while (true) { now = now.qiudiao(); if (now.coff.size() == 0) break; long double nowf = now.func(x); if (fabs(nowf) < zeroeps) ret++; else break; zeroeps *= 4; } return ret; }