简体   繁体   English

如何从我的方程中提取系数:x^3 - 60x^2 + 1100x - 6000 / -6000?

[英]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.

相关问题 Locust 似乎触发 request_success 事件钩子比我的实际请求高 60 倍 - Locust seeming to fire request_success event hook 60x than my actual requests 与7个请求线程相比,CherryPy 60x的基准测试速度较慢 - CherryPy 60x as slow in benchmark with 8 requesting threads compared to 7 为什么numpy.power比内衬慢60倍? - Why is numpy.power 60x slower than in-lining? 如何用x [0]和x [1]替换方程式中的x和y? - How to replace x and y in an equation with x[0] and x[1]? sklearn 线性回归中的 Function 方程/使用系数计算得出的结果与 model.predict(x) 不同 - Function equation from sklearn linear regression / Calculating with coefficients gives different results than model.predict(x) 如何从方程 x+yx*y 中找到 x 和 y - how can find x and y from equation x+y x*y 如何从csv文件将6000条记录上传到Google数据存储 - How to upload 6000 record to Google Datastore from csv file 在字符串中查找 x 的系数 - Finding coefficients of x in a string 如何在不使用numpy的情况下从python中的线方程中提取系数? - How to extract coefficients from a line equation in python without using numpy? 方程中 x 值的迭代 - Iteration for x value in equation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM