Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用字典在Python中乘法多项式_Python_Dictionary_Polynomials - Fatal编程技术网

使用字典在Python中乘法多项式

使用字典在Python中乘法多项式,python,dictionary,polynomials,Python,Dictionary,Polynomials,我写了一个小类,它的初始值设定项以字典作为参数。下面的字典{2:3,4:5,6:7}转换为多项式3x^2+5x^4+7x^6,因此我字典的键是指数,它的值是系数 我已经成功地使用eq方法在我的课堂上实现了两个多项式的比较,我可以添加它们。这是我的密码: class Polynomial(object): def __init__(self, polynom = {}): self.polynom = polynom self.poly_string = self.nicePoly

我写了一个小类,它的初始值设定项以字典作为参数。下面的字典
{2:3,4:5,6:7}
转换为多项式
3x^2+5x^4+7x^6
,因此我字典的键是指数,它的值是系数

我已经成功地使用eq方法在我的课堂上实现了两个多项式的比较,我可以添加它们。这是我的密码:

class Polynomial(object):
def __init__(self, polynom = {}):
    self.polynom = polynom
    self.poly_string = self.nicePolynom(polynom) #just a method that will make my output look nice

def __str__(self):
    return self.poly_string # for debugging purposes

def coefficient(self, exponent):
    """
    A small function that returns the coefficient of the corresponding exponent

    i.e. if our Polynomial is P = 3x^9 then p.coefficient(9) return 3
    """
    try:
        return self.polynom[exponent]
    except KeyError:
        pass

 def __add__(self,other):
    """
    Overloading the + operator

    Not the most elegant solution but easily understandable. 
    We check first if our exponent is present in both polynomials
    then if its only present in one and the symmetric case, adding the result
    to the dictionary add
    """
    add = {}

    for exponent in self.polynom:
        if exponent in other.polynom:
            add[exponent] = self.polynom[exponent] + other.polynom[exponent]

    for exponent in self.polynom:
        if exponent not in other.polynom:
            add[exponent] = self.polynom[exponent]

    for exponent in other.polynom:
        if exponent not in self.polynom:
            add[exponent] = other.polynom[exponent]
    return add

def __mul__(self, other):

    mult = {}

    for exponent1 in self.polynom:
        for exponent2 in other.polynom:
            mult[exponent1 + exponent2] = self.coefficient(exponent1) * other.coefficient(exponent2)

    return mult
关键的一步和我的主要问题是,在乘法过程中,我想利用加法。但是我对OOP是个新手,我现在不知道如何初始化一个可以执行加法运算的多项式对象


如果我乘以一个多项式,此时我得到了正确的指数,但是除了初始项和结束项,所有的系数都相差很远

以下是一种可能有效的方法:

for exponent1 in self.polynom:
    for exponent2 in other.polynom:
        this_exponent = exponent1 + exponent2
        this_coeff = self.coefficient(exponent1) *  other.coefficient(exponent2)
        try:
            mult[this_exponent] += this_coeff
        except KeyError:
            mult[this_exponent] = this_coeff

也就是说,更新新幂的系数,并捕获第一次遇到幂时引发的异常,以在相应的指数键处初始化字典。(如果你关心这样的事情,这比一个
if..else
子句更“Pythonic”。

系数是关闭的,因为你的展开式中有多个项对答案中的相同幂起作用,在这种情况下,你在同一个指数键上写
mult
条目。。。e、 g.当
exponent1,exponent2=1,3
之后,
exponent1,exponent2=2,2
时会发生什么?我同意@xnx,但我不知道如何修复它,有没有一种简单的方法可以通过if/else参数来防止这种情况发生?如果这是一个学习练习,那么一定要去做。但是如果你想用它来做任何严肃的事情,我的建议是不要重新发明轮子,而只是看看,它能很好地处理多项式(除其他外)。