Esempio n. 1
0
    static vector<long double> findAllRoots(const Polynomial & f)
    {
        vector<long double> ret;
        Polynomial f_ = f.qiudiao();

        long double minRoot = -200.0;
        long double maxRoot = 200.0;

        for (int iter = 0; iter <= 1000; ++iter)
        {
            //cout << iter << endl;
            long double x0 = (long double) rand() / (long double) RAND_MAX;
            x0 = minRoot + (maxRoot - minRoot) * x0;
            long double root;
            bool findRoot = newton_root(x0, f, f_, root);

            if (findRoot)
            {
                bool newroot = true;
                for (int i = 0; i < ret.size(); ++i)
                    if (fabs(ret[i] - root) < bigeps)
                        newroot = false;

                if (newroot)
                {
                    int multi = findRootMultiple(root, f);
                    vector<long double> newroots(multi, root);
                    ret.insert(ret.end(), newroots.begin(), newroots.end());
                }
            }
        }

        sort(ret.begin(), ret.end());
        return ret;
    }
Esempio n. 2
0
 // 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;
 }