[英]How to extract coefficients from my equation of: x^3 - 60x^2 + 1100x - 6000 / -6000?
Given an array of integers, I am trying to extract the coefficient in front of each "x^i"
, ...., "x^3"
, "x^2"
, "x"
.给定一个整数数组,我试图提取每个"x^i"
、 ....、 "x^3"
、 "x^2"
、 "x"
前面的系数。
Given the equation v(x) = (x-x0)(x-x1)...(xx(i-1)) / (xi-x0)(xi-x1)...(xi-x(i-1))
给定方程v(x) = (x-x0)(x-x1)...(xx(i-1)) / (xi-x0)(xi-x1)...(xi-x(i-1))
For example if given array is [30, 20, 10, 0] then the expected result will be:例如,如果给定数组是 [30, 20, 10, 0],那么预期结果将是:
x^3 - 60x^2 + 1100x - 6000 / -6000
I am nearly there I just need to find a way to obtain 1, -60, 1100, -6000
from the numerator.我快到了,我只需要找到一种方法从分子中获得1, -60, 1100, -6000
。
I've been trying various split methods but it did not seem to work.我一直在尝试各种拆分方法,但似乎没有奏效。
My current code:我当前的代码:
from sympy import *
def extract_coeff(array):
coeff = []
x=Symbol('x')
count = 0
for item in array:
if item != 0:
count = count + 1
total = len(array)
list = []
numerator_list = []
numerator = 1
denom_list = []
denom = 1
for i in range(total):
list.append(array[i])
for item in list:
denom_list.append((list[-1] - item))
denom_list.pop()
for item in denom_list:
denom = denom * item
# numerator
for item in array:
if item != 0:
numerator = numerator * (x-item)
numerator = expand(numerator)
print(numerator)
numerator = str(numerator)
# store coefficient
if numerator[0] == 'x':
coeff.append(1)
# trying to split (PLEASE HELP)
numerator = numerator.replace('+ ', ', ')
print(numerator.split(', '))
extract_coeff([30,20,10,0])
Say you have an equation假设你有一个方程
>>> from sympy.abc import x
>>> eq = (x**3 - 60*x**2 + 1100*x - 6000) / -6000
And you want to look at the numerator.你想看看分子。 If you are sure there are no compound fractions then numer(eq)
will work.如果您确定没有复合分数,那么numer(eq)
将起作用。 If there might be more than one term as in x/3 + x
then use as_numer_denom()
如果x/3 + x
中可能有多个术语,则使用as_numer_denom()
>>> n, d = eq.as_numer_denom()
Now convert the numerator n
to a Poly and ask for all coefficients:现在将分子n
转换为 Poly 并询问所有系数:
>>> Poly(n).all_coeffs()
[-1, 60, -1100, 6000]
Since the denominator was negative, that negative sign is now in the numerator.由于分母为负数,因此该负号现在位于分子中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.