解决多项式乘法和除法“溢出”问题问题

发布于 2025-01-09 19:02:47 字数 1330 浏览 0 评论 0原文

我有一个 1 次多项式的系数列表,其中 a[i][0]*x^1 + a[i][1]

a = np.array([[ 1.        , 77.48514702],
          [ 1.        ,  0.        ],
             [ 1.        ,  2.4239275 ],
           [ 1.        ,  1.21848739],
            [ 1.        ,  0.        ],
            [ 1.        ,  1.18181818],
           [ 1.        ,  1.375     ],
           [ 1.        ,  2.        ],
          [ 1.        ,  2.        ],
          [ 1.        ,  2.        ]])

并遇到以下操作的问题,

np.polydiv(reduce(np.polymul, a), a[0])[0] != reduce(np.polymul, a[1:])

其中

In [185]: reduce(np.polymul, a[1:])
Out[185]:
array([  1.        ,  12.19923307,  63.08691612, 179.21045388,
       301.91486027, 301.5756213 , 165.35814595,  38.39582615,
         0.        ,   0.        ])

In [186]: np.polydiv(reduce(np.polymul, a), a[0])[0]
Out[186]:
array([ 1.00000000e+00,  1.21992331e+01,  6.30869161e+01,  1.79210454e+02,
        3.01914860e+02,  3.01575621e+02,  1.65358169e+02,  3.83940472e+01,
        1.37845155e-01, -1.06809521e+01])

首先,np.polydiv(reduce(np.polymul, a), a[0]) 的余数远大于 0,准确地说是 827.61514239,其次,商的最后两项应该为 0,但比 0 更大。1.37845155e-01, -1.06809521e+01

我想知道我有哪些选择可以提高准确性?

I have a list of the coefficient to degree 1 polynomials, with a[i][0]*x^1 + a[i][1]

a = np.array([[ 1.        , 77.48514702],
          [ 1.        ,  0.        ],
             [ 1.        ,  2.4239275 ],
           [ 1.        ,  1.21848739],
            [ 1.        ,  0.        ],
            [ 1.        ,  1.18181818],
           [ 1.        ,  1.375     ],
           [ 1.        ,  2.        ],
          [ 1.        ,  2.        ],
          [ 1.        ,  2.        ]])

And running into issues with the following operation,

np.polydiv(reduce(np.polymul, a), a[0])[0] != reduce(np.polymul, a[1:])

where

In [185]: reduce(np.polymul, a[1:])
Out[185]:
array([  1.        ,  12.19923307,  63.08691612, 179.21045388,
       301.91486027, 301.5756213 , 165.35814595,  38.39582615,
         0.        ,   0.        ])

and

In [186]: np.polydiv(reduce(np.polymul, a), a[0])[0]
Out[186]:
array([ 1.00000000e+00,  1.21992331e+01,  6.30869161e+01,  1.79210454e+02,
        3.01914860e+02,  3.01575621e+02,  1.65358169e+02,  3.83940472e+01,
        1.37845155e-01, -1.06809521e+01])

First of all the remainder of np.polydiv(reduce(np.polymul, a), a[0]) is way bigger than 0, 827.61514239 to be exact, and secondly, the last two terms to quotient should be 0, but way larger from 0. 1.37845155e-01, -1.06809521e+01.

I'm wondering what are my options to improve the accuracy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2025-01-16 19:02:47

有一个稍微复杂的方法,先保留产品,然后划分结构。

首先使用n个点并对a进行评估。

xs = np.linspace(0, 1., 10) 
ys = np.array([np.prod(list(map(lambda r: np.polyval(r, x), a))) for x in xs])

然后对 ys 而不是系数进行除法。

ys = ys/np.array([np.polyval(a[0], x) for x in xs])

最后使用 xsys 使用多项式插值恢复系数

from scipy.interpolate import lagrange
lagrange(xs, ys)

找到了第二个解决方案

b = a[:,::-1]
np.polydiv(reduce(np.polymul, b), b[0])[0][::-1]

There is a slightly complicated way to keep the product first and then divide structure.

By first employ n points and evaluate on a.

xs = np.linspace(0, 1., 10) 
ys = np.array([np.prod(list(map(lambda r: np.polyval(r, x), a))) for x in xs])

then do the division on ys instead of coefficients.

ys = ys/np.array([np.polyval(a[0], x) for x in xs])

finally recover the coefficient using polynomial interpolation with xs and ys

from scipy.interpolate import lagrange
lagrange(xs, ys)

Found a second solution

b = a[:,::-1]
np.polydiv(reduce(np.polymul, b), b[0])[0][::-1]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文