在Python中,如何添加多项式的递增元素?
In Python, how do I add an incrementing element of a polynomial?
我根据给定的简单正弦函数构造牛顿多项式。执行了中间计算,但在最后阶段停止了——获得多项式的公式。递归在这里可能有所帮助,但它是不准确的。这是多项式的公式
公式迭代下面 table 中的值:我们遍历 x 的列和计算的增量的第一行(我们向上移动到增量,我们得到多项式的阶数).例如,如果度数为 2,那么我们将在第一行取 2 个增量,在 x 列中取最大 2.512 的值(具有 x 差的 9 个括号将在多项式的最后一个块中)
在公式中,有一组常量块,其中迭代值,但我在元素 (x —x_0)**[n] 中遇到了障碍。这是用户设置的多项式 n 的次数。这里[n]表示括号内的表达式被展开:
我使用 sympy 库进行符号计算:未来多项式公式中的 x 应保留为 x(作为符号,而不是其值)。如何实现在多项式中重复的块的一部分随着多项式次数的新括号增长?
代码:
import numpy as np
from sympy import *
import pandas as pd
from scipy.special import factorial
def func(x):
return np.sin(x)
def poly(order):
# building columns X and Y:
x_i_list = [round( (0.1*np.pi*i), 4 ) for i in range(0, 11)]
y_i_list = []
for x in x_i_list:
y_i = round( (func(x)), 4 )
y_i_list.append(y_i)
# we get deltas:
n=order
if n < len(y_i_list):
result = [ np.diff(y_i_list, n=d) for d in np.arange(1, len(y_i_list)) ]
print(result)
else:
print(f'Determine the order of the polynomial less than {len(y_i_list)}')
# We determine the index in the x column based on the degree of the polynomial:
delta_index=len(result[order-1])-1
x_index = delta_index
h = (x_i_list[x_index] - x_i_list[0]) / n # calculate h
b=x_i_list[x_index]
a=x_i_list[0]
y_0=x_i_list[0]
string_one = [] # list with deltas of the first row (including the degree column of the polynomial)
for elen in result:
string_one.append(round(elen[0], 4))
# creating a list for the subsequent passage through the x's
x_col_list = []
for col in x_i_list:
if col <= x_i_list[x_index]:
x_col_list.append(col)
x = Symbol('x') # for symbolic representation of x's
# we go along the deltas of the first line:
for delta in string_one:
# we go along the column of x's
for arg in x_col_list:
for n in range(1, order+1):
polynom = ( delta/(factorial(n)*h**n) )*(x - arg) # Here I stopped
我猜你正在寻找这样的东西:
In [52]: from sympy import symbols, prod
In [53]: x = symbols('x')
In [54]: nums = [1, 2, 3, 4]
In [55]: prod((x-n) for n in nums)
Out[55]: (x - 4)⋅(x - 3)⋅(x - 2)⋅(x - 1)
编辑:实际上使用 Mul
而不是 prod
更有效:
In [134]: Mul(*((x-n) for n in nums))
Out[134]: (x - 4)⋅(x - 3)⋅(x - 2)⋅(x - 1)
我根据给定的简单正弦函数构造牛顿多项式。执行了中间计算,但在最后阶段停止了——获得多项式的公式。递归在这里可能有所帮助,但它是不准确的。这是多项式的公式
公式迭代下面 table 中的值:我们遍历 x 的列和计算的增量的第一行(我们向上移动到增量,我们得到多项式的阶数).例如,如果度数为 2,那么我们将在第一行取 2 个增量,在 x 列中取最大 2.512 的值(具有 x 差的 9 个括号将在多项式的最后一个块中)
在公式中,有一组常量块,其中迭代值,但我在元素 (x —x_0)**[n] 中遇到了障碍。这是用户设置的多项式 n 的次数。这里[n]表示括号内的表达式被展开:
代码:
import numpy as np
from sympy import *
import pandas as pd
from scipy.special import factorial
def func(x):
return np.sin(x)
def poly(order):
# building columns X and Y:
x_i_list = [round( (0.1*np.pi*i), 4 ) for i in range(0, 11)]
y_i_list = []
for x in x_i_list:
y_i = round( (func(x)), 4 )
y_i_list.append(y_i)
# we get deltas:
n=order
if n < len(y_i_list):
result = [ np.diff(y_i_list, n=d) for d in np.arange(1, len(y_i_list)) ]
print(result)
else:
print(f'Determine the order of the polynomial less than {len(y_i_list)}')
# We determine the index in the x column based on the degree of the polynomial:
delta_index=len(result[order-1])-1
x_index = delta_index
h = (x_i_list[x_index] - x_i_list[0]) / n # calculate h
b=x_i_list[x_index]
a=x_i_list[0]
y_0=x_i_list[0]
string_one = [] # list with deltas of the first row (including the degree column of the polynomial)
for elen in result:
string_one.append(round(elen[0], 4))
# creating a list for the subsequent passage through the x's
x_col_list = []
for col in x_i_list:
if col <= x_i_list[x_index]:
x_col_list.append(col)
x = Symbol('x') # for symbolic representation of x's
# we go along the deltas of the first line:
for delta in string_one:
# we go along the column of x's
for arg in x_col_list:
for n in range(1, order+1):
polynom = ( delta/(factorial(n)*h**n) )*(x - arg) # Here I stopped
我猜你正在寻找这样的东西:
In [52]: from sympy import symbols, prod
In [53]: x = symbols('x')
In [54]: nums = [1, 2, 3, 4]
In [55]: prod((x-n) for n in nums)
Out[55]: (x - 4)⋅(x - 3)⋅(x - 2)⋅(x - 1)
编辑:实际上使用 Mul
而不是 prod
更有效:
In [134]: Mul(*((x-n) for n in nums))
Out[134]: (x - 4)⋅(x - 3)⋅(x - 2)⋅(x - 1)