我的多项式课有一个错误

时间:2017-08-09 17:50:08

标签: python class debugging

我通过构建表示多项式的类来学习类。我知道这是一个很好的方式在numpy btw中做多项式。 我定义了add,multiply和call dunner方法。多项式是a*x**b形式的项的总和,因此该类被称为'sot'。 任何sot实例都包含self.term,它是一组数字[a,b]的梨。每个代表a*x**b。 add方法返回一个新的sot实例,其中self.terms与other.terms的串联作为参数

在其初始化

中将梨的列表作为其参数

问题在于,当我添加两个sot a + b a的实例时,a会发生变化,所以如果我重复添加a + b,我会得到不同的结果。 我的乘法方法不会发生同样的情况。

谁能看到这里发生了什么?

#The class 'sot' represents a Sum Of Terms of the form a*x^b, aka a polinomeal
#On creating a new instanse, sot must be fead a list containg its terms.
#Each terms is represented by a list of two vareavles. [a,b] represesnting a*x^b
#Eg: a = sot(( [1,2], [2,1], [3,0] )) represents x^2 + 2x + 3
#Caling an instanse will return the evaluation of the polinomeal
# If the terms of sot are given as a tuple (eg sot(( (1,2) )) ) the object will not function as intended.
# Creating a sot of no terms, and empty polinomeal, must be done with squer brackets. eg sot([[0,3]])
# This vesion of the file prints out what its doing in the simplifying step, this is for debuging perposes



class sot:

    def __init__(self,tupe):
        #The first step un-packs the argument into a list of terms
        self.term=[]
        for t in tupe:
            print("add " + str(t) + " to " + str(self.term))
            self.term = self.term+[t]
        #Simpler is a method that simplifys the polinomeal
        self.simpler() 

    def simpler(self):
        #The first step is to sort the polinnomeal by the assending powers eg c + x + x^2 ...

        print("simplifying " +  str(self))
        self.term = sorted(self.term,key = lambda element: element[1] ) 
        print("sorted "+str(self))

        # now this while loop will inspect the list of terms to see if anny 
            # (a) have a zero coefichent
                # in which case there removed from the list
            # (b) have the same power 
                # in which case the terms second of ther terms will be poped form the list 
                # sotored in 'carry'  and its constant 'carry[0]' will be added to the first term
        # If the loop has not run then ther must be onlly one element and it must not have been
        #  checked for beeing zero 

        i=0     
        while i < len(self.term)-1:
            print("checking " + str(i) + " out of " + str( len(self.term)-2))
            if self.term[i][0] ==0:
                self.term.remove(i)
                print("removeing zero")
            if self.term[i][1] ==self.term[i+1][1]:
                print("adding similers")
                carry = self.term.pop(i+1)
                print("carry term "+str(carry))
                self.term[i][0] = self.term[i][0] +carry[0]
                print("new sum "+ str(self))
            i=i+1

        if i==0 & self.term[0][0] == 0:
            self.term = []
            print("the list has one term and that term is zero")

        print("simplifyed compleat " + str(self))


    def __call__(self,x):
        # the result of a call the sum of each term, remember that each term [a,b] represents a*x**b
        result = 0
        for t in self:
            result = result + t[0]*x**t[1]
        return result

    def __repr__(self):
        string = []
        for t in self:
            string = string+ [str(t[0]) + "x**" + str(t[1])]
        return " + ".join(string)

    def __len__(self):
        #The length of a sot is just the number of terms
        return len(self.term)

    def __iter__(self):
        # The iteration of a sot steps throgh the list of terms starting from the zerothe
        # and returning each tem as it dose so
        self.i=0
        return self

    def __next__(self):
        if self.i < len(self):
            self.i=self.i+1
            return self.term[self.i-1]
        else:
            raise StopIteration


    def __add__(self,other):
        # adding tow sots together is the same as haveing a new sot, so for a + b there list of terms 
        # are concatanated and fed into a new instanse of sot
        argument = self.term+other.term
        return  sot(argument)

    def __mul__(self,other):
        # multiplication of sots is done with the distributive law:
            # sum_i(x_i)*sum_j(x_j) = sum_i(x_i *sum_j(x_j)) = sum_i(sum_j(x_i*x_j))
        # this is realised with two imbeded for loops
        # when two terms are multiplyed, the constant parts are multiplyed and the powers are added:
            #(ax**b)*(cx**d) = (a*c)x**(b+d)
        # The relustant list of terms is fed into a new instanse of sot
        argument = []
        for s in self:
            for o in other:
                argument = argument + [[ s[0]*o[0] , s[1]+o[1] ]]
        return sot(argument)

0 个答案:

没有答案