在Python中,如何重构循环?

发布于 2025-02-06 20:49:25 字数 539 浏览 0 评论 0原文

我为函数构建了牛顿多项式。它具有相当简单的结构:

“牛顿的polyenmial公式”

在这里,y y的deltas是取自String_one listx_0 - 对于每个块 - 符号都有不同的值(来自X_COL_LIST列表) - 实际上,这是指示的通过方括号中的非步骤:[1] [2] [n]

我正在尝试构建一个公式,其中:

  1. x保留x而无需获得值(这是由sympy模块提供的);

  2. 每个分数的分母增加一个;

  3. 元素(x -x_0)在每个summand -block中,我们添加一个额外的括号,保持已经累积的:

I construct a Newton polynomial for a function. It has a fairly simple structure:

Newton 's polynomial formula

Here, the deltas for y are taken from the string_one list, and x_0 - for each block-summands have different values (from the x_col_list list) - actually, this is indicated by non-steps in square brackets: [1] [2] [n].

I'm trying to build a formula where:

  1. x remains x without getting values (this is served by the sympy module);

  2. the denominator of each fraction increases by one;

  3. element (x - x_0) in each summand-block we add one additional bracket, keeping the already accumulated:

    (????−0)
    (????−0)(????−0.3142)
    (????−0)
    (????−0.3142)(????−0.6283)

    with the last term:

    (????−0)*(????−0.3142)(????−0.6283)(????−0.9425)(????−1.2566)(????−1.5708)(????−1.885)(????−2.1991)

I'm in trouble right now:

  1. I get too much in the opposite direction: ????(????-2.1991)(????-1.885)...(????−0.3142)

  2. x with zero is immediately truncated to x, and I need (x - 0)

  3. and somehow now it has to be folded.

Code:

from sympy import symbols, prod
from scipy.special import factorial

x = symbols('x')

string_one = [0.3091,-0.0304,-0.0271,0.0054,0.0025,-0.0016,0.0019,-0.0046,0.0099,-0.019]
x_col_list = [0.0, 0.3142, 0.6283, 0.9425, 1.2566, 1.5708, 1.885, 2.1991, 2.5133]
h=1.25665

x = symbols('x')

 # we go along the deltas:
num=1
for delta in string_one:
    # we go along the column of x's: 
    polynom = round( ( delta/(factorial(num)*h**num) ), 4 ) * prod([x - args for args in x_col_list[::-1]])
    num+=1
    print(polynom)

Now the way out is this:
0.246*x*(x - 2.5133)*(x - 2.1991)*(x - 1.885)*(x - 1.5708)*(x - 1.2566)*(x - 0.9425)*(x - 0.6283)*(x - 0.3142) -0.0096*x*(x - 2.5133)*(x - 2.1991)...

As you can see, brackets are attached to each term. And all the other flaws. What can be done? Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文