0

現在、割り当てに従ってPythonで多項式を使用して算術演算を行うクラスに取り組んでいます。算術演算とコードがどのように機能するかは理解していますが、以前にクラスを使用したことがなく、変数がクラスに出入りする方法がわかりません...特に、たとえば... 2つの多項式を渡して戻りたい場合機能。

関数を挿入しました (メソッドを再構築したい) 過去に多項式を乗算するために使用しました (基数が同じで、アプリケーションのニーズに合わせて修正する必要があります)。

2つのポリゴンを入力してから乗算したいという構文を誰かが教えてくれますか? オンラインのビデオはあまり役に立たないので、何が起こっているのかについて段階的な説明を使用できます. これは主に構文の問題であり、コードは非常に初期の (そして壊れた) 段階にあります。

乾杯、D

編集:この形式にしたい多項式の形式。intPoly([2,4,1,2], z) は実際には 2z^3+4z^2+z+2 です

class IntPoly:
    def __init__(build,length,var):
        build.length = length
        build.var = var

    def addPoly:

    def multiply(a, b):
        a.reverse()
        b.reverse()
        c=[0 for x in range(len(a)+len(b)-1)]

        for i in range (len(a)):
            for j in range (len(b)):
                k = a[i]*b[j]
                ii=i+j
                c[ii]+=k

        c.reverse()

        return (c)

    def equalTo:

    def deg:

    def itterate:

    def printReal:
4

2 に答える 2

0

それらをコンストラクターに渡すことができます。ここにあなたが探していると思うものがあります:

class IntPoly(object):
    def __init__(self, poly1, poly2):
        self.poly1 = poly1
        self.poly2 = poly2

    def multiply():
        """Here you would do your proper conversion and parsing
        given the representation of the polynomials.
        But pretending that they are just numbers, you would do the following:"""
        return self.poly1 * self.poly2

そして、そのようなオブジェクトを作成し、乗算メソッドを使用して積を取得できます。

myPolyObject = IntPoly([4, 3, 2], [1, 2, 3])
print myPolyObject.multiply()          

任意の量の多項式を説明する必要がある場合、それを多項式と呼びましょうn。それらすべてを単純にリストに保持できます。

class IntPoly(object):
    def __init__(self, poly_list):
        self.poly_list = poly_list

    def multiply():
        """Here you would do your proper conversion and parsing
        given the representation of the polynomials.
        But pretending that they are just numbers, you would do the following:"""
        return reduce(lambda x,y: x*y, self.poly_list)

myPolyObject = IntPoly([[4, 3, 2], [1, 2, 3], [4, 7, 8]])
print myPolyObject.multiply()

このreduce関数は基本的に関数を受け取り、それをリストの各要素に適用して結果を収集します。したがって、reduce(lambda: x,y: x*y, [1, 2, 3])計算は次のようになります((1*2)*3))

于 2014-10-26T15:26:43.313 に答える
0
class Poly():
    """DocString"""
    def __init__(self, loc):
        self.loc = loc
        self.degree = len(loc)
        ...
    ## __add__ is a standard method name, google for python __add__
    def __add__(self, poly2):
        loc1 = self.loc
        loc2 = poly2.loc
        # do stuff and compute loc3, list of coefficients for p1+p2
        return Poly(loc3)
    def __mul__ ...

p1 = Poly([4,3,2,1])
p2 = Poly([3,4,0,0])
p3 = p1+p2

print多項式が必要な場合は、標準的な方法__repr__を読んで、 __str__Python がカスタム オブジェクトを印刷または表現するために使用します。使用する機能を気にしない場合は、printいつでもインスタンスのメンバーにアクセスできます

print "Degree of sum", p3.degree
print "List of coefficients of sum", p3.loc
于 2014-10-26T16:30:34.577 に答える