在Python中,如何重构循环?
我为函数构建了牛顿多项式。它具有相当简单的结构:
在这里,y y
的deltas是取自String_one list
,x_0
- 对于每个块 - 符号都有不同的值(来自X_COL_LIST列表
) - 实际上,这是指示的通过方括号中的非步骤:[1] [2] [n]
。
我正在尝试构建一个公式,其中:
x
保留x
而无需获得值(这是由sympy
模块提供的);每个分数的分母增加一个;
元素
(x -x_0)
在每个summand -block中,我们添加一个额外的括号,保持已经累积的:(
I construct a Newton polynomial for a function. It has a fairly simple structure:
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:
x
remainsx
without getting values (this is served by thesympy
module);the denominator of each fraction increases by one;
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:
I get too much in the opposite direction:
????(????-2.1991)(????-1.885)...(????−0.3142)
x
withzero
is immediately truncated tox
, and I need(x - 0)
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论