2

閉形式解を使用して多項式の根 (理想的には 4 次デブリーまでですが、何でも構いません) を見つけることができる堅牢なアルゴリズム (またはアルゴリズムを説明する論文) を探しています。私は本当のルーツだけに興味があります。

二次方程式を解く最初の試みには、次のことが含まれていました (3 次方程式や 4 次方程式にも同様のスタイルのコードがありますが、ここでは 2 次方程式に焦点を当てましょう):

/**
 *  @brief a simple quadratic equation solver
 *
 *  With double-precision floating-point, this reaches 1e-12 worst-case and 1e-15 average
 *  precision of the roots (the value of the function in the roots). The roots can be however
 *  quite far from the true roots, up to 1e-10 worst-case and 1e-18 average absolute difference
 *  for cases when two roots exist. If only a single root exists, the worst-case precision is
 *  1e-13 and average-case precision is 1e-18.
 *
 *  With single-precision floating-point, this reaches 1e-3 worst-case and 1e-7 average
 *  precision of the roots (the value of the function in the roots). The roots can be however
 *  quite far from the true roots, up to 1e-1 worst-case and 1e-10 average absolute difference
 *  for cases when two roots exist. If only a single root exists, the worst-case precision is
 *  1e+2 (!) and average-case precision is 1e-2. Do not use single-precision floating point,
 *  except if pressed by time.
 *
 *  All the precision measurements are scaled by the maximum absolute coefficient value.
 *
 *  @tparam T is data type of the arguments (default double)
 *  @tparam b_sort_roots is root sorting flag (if set, the roots are
 *      given in ascending (not absolute) value; default true)
 *  @tparam n_2nd_order_coeff_log10_thresh is base 10 logarithm of threshold
 *      on the first coefficient (if below threshold, the equation is a linear one; default -6)
 *  @tparam n_zero_discriminant_log10_thresh is base 10 logarithm of threshold
 *      on the discriminant (if below negative threshold, the equation does not
 *      have a real root, if below threshold, the equation has just a single solution; default -6)
 */
template <class T = double, const bool b_sort_roots = true,
    const int n_2nd_order_coeff_log10_thresh = -6,
    const int n_zero_discriminant_log10_thresh = -6>
class CQuadraticEq {
protected:
    T a; /**< @brief the 2nd order coefficient */
    T b; /**< @brief the 1st order coefficient */
    T c; /**< @brief 0th order coefficient */
    T p_real_root[2]; /**< @brief list of the roots (real parts) */
    //T p_im_root[2]; // imaginary part of the roots
    size_t n_real_root_num; /**< @brief number of real roots */

public:
    /**
     *  @brief default constructor; solves for roots of \f$ax^2 + bx + c = 0\f$
     *
     *  This finds roots of the given equation. It tends to find two identical roots instead of one, rather
     *  than missing one of two different roots - the number of roots found is therefore orientational,
     *  as the roots might have the same value.
     *
     *  @param[in] _a is the 2nd order coefficient
     *  @param[in] _b is the 1st order coefficient
     *  @param[in] _c is 0th order coefficient
     */
    CQuadraticEq(T _a, T _b, T _c) // ax2 + bx + c = 0
        :a(_a), b(_b), c(_c)
    {
        T _aa = fabs(_a);
        if(_aa < f_Power_Static(10, n_2nd_order_coeff_log10_thresh)) { // otherwise division by a yields large numbers, this is then more precise
            p_real_root[0] = -_c / _b;
            //p_im_root[0] = 0;
            n_real_root_num = 1;
            return;
        }
        // a simple linear equation

        if(_aa < 1) { // do not divide always, that makes it worse
            _b /= _a;
            _c /= _a;
            _a = 1;

            // could copy the code here and optimize away division by _a (optimizing compiler might do it for us)
        }
        // improve numerical stability if the coeffs are very small

        const double f_thresh = f_Power_Static(10, n_zero_discriminant_log10_thresh);
        double f_disc = _b * _b - 4 * _a * _c;
        if(f_disc < -f_thresh) // only really negative
            n_real_root_num = 0; // only two complex roots
        else if(/*fabs(f_disc) < f_thresh*/f_disc <= f_thresh) { // otherwise gives problems for double root situations
            p_real_root[0] = T(-_b / (2 * _a));
            n_real_root_num = 1;
        } else {
            f_disc = sqrt(f_disc);
            int i = (b_sort_roots)? ((_a > 0)? 0 : 1) : 0; // produce sorted roots, if required
            p_real_root[i] = T((-_b - f_disc) / (2 * _a));
            p_real_root[1 - i] = T((-_b + f_disc) / (2 * _a));
            //p_im_root[0] = 0;
            //p_im_root[1] = 0;
            n_real_root_num = 2;
        }
    }

    /**
     *  @brief gets number of real roots
     *  @return Returns number of real roots (0 to 2).
     */
    size_t n_RealRoot_Num() const
    {
        _ASSERTE(n_real_root_num >= 0);
        return n_real_root_num;
    }

    /**
     *  @brief gets value of a real root
     *  @param[in] n_index is zero-based index of the root
     *  @return Returns value of the specified root.
     */
    T f_RealRoot(size_t n_index) const
    {
        _ASSERTE(n_index < 2 && n_index < n_real_root_num);
        return p_real_root[n_index];
    }

    /**
     *  @brief evaluates the equation for a given argument
     *  @param[in] f_x is value of the argument \f$x\f$
     *  @return Returns value of \f$ax^2 + bx + c\f$.
     */
    T operator ()(T f_x) const
    {
        T f_x2 = f_x * f_x;
        return f_x2 * a + f_x * b + c;
    }
};

コードはひどいもので、すべてのしきい値が嫌いです。しかし、根が[-100, 100]区間内にあるランダム方程式の場合、これはそれほど悪くありません。

root response precision 1e-100: 6315 cases
root response precision 1e-19: 2 cases
root response precision 1e-17: 2 cases
root response precision 1e-16: 6 cases
root response precision 1e-15: 6333 cases
root response precision 1e-14: 3765 cases
root response precision 1e-13: 241 cases
root response precision 1e-12: 3 cases
2-root solution precision 1e-100: 5353 cases
2-root solution precision 1e-19: 656 cases
2-root solution precision 1e-18: 4481 cases
2-root solution precision 1e-17: 2312 cases
2-root solution precision 1e-16: 455 cases
2-root solution precision 1e-15: 68 cases
2-root solution precision 1e-14: 7 cases
2-root solution precision 1e-13: 2 cases
1-root solution precision 1e-100: 3022 cases
1-root solution precision 1e-19: 38 cases
1-root solution precision 1e-18: 197 cases
1-root solution precision 1e-17: 68 cases
1-root solution precision 1e-16: 7 cases
1-root solution precision 1e-15: 1 cases

この精度は、通常は 10^6 の範囲にある係数の大きさに相対的であることに注意してください (したがって、最終的に精度は完全にはほど遠いですが、おそらくほとんど使用可能です)。ただし、しきい値がないと、ほとんど役に立たなくなります。

多精度演算を使用してみましたが、これは一般的にうまく機能しますが、多項式の係数が多精度ではなく、一部の多項式を正確に表すことができないという理由だけで、根の多くを拒否する傾向があります (2 次に二重根がある場合)。多項式、それはほとんどそれを2つのルートに分割するか(私は気にしません)、ルートがまったくないと言います)。少しでも不正確なルートを復元したい場合、コードは複雑になり、しきい値でいっぱいになります。

今までCCmathを使ってみたのですが、うまく使えないか、精度がすごく悪いです。また、 では反復 (閉形式ではない) ソルバーを使用しplrt()ます。

GNU科学ライブラリを使用してみgsl_poly_solve_quadratic()ましたが、それは単純なアプローチのようで、数値的に安定していません.

単純に数値を使用std::complexすることも、精度と速度の両方が悪い可能性があるため、非常に悪い考えであることが判明しました (特に、コードが超越関数で重い 3 次/4 次方程式の場合)。

根を複素数として復元することが唯一の方法ですか? この場合、根が失われることはなく、ユーザーは根がどの程度正確である必要があるかを選択できます (したがって、精度の低い根の小さな虚数成分は無視されます)。

4

1 に答える 1

2

これは実際にはあなたの質問に答えているわけではありませんが、b^2 >> ac. このような場合、(-b + (b + eps))/(2 * a)b をキャンセルすると から多くの有効数字が失われる可能性があるという行に沿った式になりepsます。

これを処理する正しい方法は、一方の根には二次方程式の根の「正規」方程式を使用し、もう一方の根にはあまり知られていない「代替」または「逆さま」の方程式を使用することです。どの方向にそれらを取るかは、 の記号によって異なります_b

次の行に沿ってコードを変更すると、これに起因するエラーが減少するはずです。

if( _b > 0 ) {
    p_real_root[i] = T((-_b - f_disc) / (2 * _a));
    p_real_root[1 - i] = T((2 * _c) / (-_b - f_disc));
}
else{
    p_real_root[i] = T((2 * _c) / (-_b + f_disc));
    p_real_root[1 - i] = T((-_b + f_disc) / (2 * _a));
}
于 2015-01-15T22:43:25.270 に答える