Exemplo n.º 1
0
def otherGaussSiedel(m,A,a,epsilon,p):
    VECTOR = 1 # See above. Here, it is 1 because we use math notation
    nOptim = 0
    xOptim = Matrix(m,m)
    mathP = p+1 #We use mathP (p+1) because the algorithm is written in mathematical notation (k = 1,p). By employing the matrix API, we can directly use the mathematical notation
    mathM = m+1 #We use mathM for the same reason we use mathP. It is used only for iterations, not defining sizes

    B = Matrix(m,m)
    B = copy.deepcopy(otherGaussSiedelB(A,m))

    q0 = B.normOne()
    q1 = B.infiniteNorm()
    q = -1
    norm = -1
    if q0 < 1 or q1 < 1:
        if q0 < 1:
            q = q0
            norm = 1
        elif q1 < 1:
            q = q1
            norm = 999
        #Initialize x
        x = Matrix(m,VECTOR)
        #print("X:")
        #x.display()
        x.mathInsert(random.randrange(1,mathM),VECTOR,random.randrange(1,5))
        #print("X after first insertion:")
        #x.display()
        x.mathInsert(random.randrange(1,mathM)-1,VECTOR,random.randrange(1,5)+2)
        #print("X after second insertion:")
        #x.display()
        x.mathInsert(random.randrange(1,mathM)-1,VECTOR,random.randrange(1,5)+7)
        #print("X after third insertion:")
        #x.display()

        newX = copy.deepcopy(Matrix(m,VECTOR))
        condition = True
        iteration = 0
        while condition:
            iteration += 1
            newX = copy.deepcopy(Matrix(m,VECTOR))
            for i in range(1,mathM):
                newElementOfX = otherGaussSiedelSum(B,x,a,i,mathM)
                #print(">!< Haha")
                newX.mathInsert(i,VECTOR,newElementOfX)
            conditionValue = ( (q/(1-q))*computeNorm(x,newX,norm) )
            condition = conditionValue > epsilon

            '''
            print("Old x:")
            x.display()
            print("New x:")
            newX.display()
            '''

            for xIndex in range(1,mathM):
                x.mathInsert(xIndex,VECTOR,newX.mathAt(xIndex,VECTOR))

            '''
            print("X after reasignment:")
            x.display()
            '''

            #print("Iteration:",iteration," - ",conditionValue,"out of",epsilon,".")
        print("===== Other Gauss Siedel =====")
        print("Optim solution (x):")
        x.display()
        print("Test:")
        result = A.multiplyMatrix(newX)
        result.display()
        print("====================")
    else:
        print("Else Other Gauss Siedel.")