Exemplo n.º 1
0
def rotationMethod(m,A,a,epsilon,p):
    #print("Start Rotation.")
    VECTOR = 1
    X = Matrix(m,VECTOR)
    oldA = Matrix(m,m)
    r = Matrix(m,VECTOR)
    iterationNumber = 0
    condition = True
    #print("Entering while.")
    while condition:
        #print("OldA copy.")
        oldA = copy.deepcopy(A)
        #print("0")
        r = copy.deepcopy(a.substractMatrix(A.matrixMultiplication(X,a)))
        #print("1")
        (xpq,p,q) = getMax(A.lowerTriangularMatrix())
        #print("2")
        theta = 0
        if A.lowerTriangularMatrix().at(p,p) == A.lowerTriangularMatrix().at(q,q):
            #print("3")
            theta = math.pi/4
        else:
            #print("4")
            theta = ( 2 * xpq )/(A.upperTriangularMatrix().at(p,p) - A.upperTriangularMatrix().at(q,q) + (10**(-5)))
        #print("Determined theta.")
        c = math.cos(theta)
        s = math.sin(theta)
        #print("5")
        T = Matrix(m,m)
        for i in range(0,T.numberOfColumns):
            T.insert(i,i,1)
        T.insert(p,p,c)
        T.insert(p,q,s)
        T.insert(q,p,-s)
        T.insert(q,q,c)
        #print("Determined T.")
        A = copy.deepcopy((T.transpose()).multiplyMatrix(oldA.multiplyMatrix(T)))
        #condition = not r.isAlmostZero()
        if iterationNumber%100 == 0:
            print("-----")
            print("Situation at iteration:",iterationNumber)
            print("Condition:",condition)
            print("A matrix:")
            A.display()
            print("X matrix:")
            X.display()
            print("-----")
        X = copy.deepcopy(oldA)
        iterationNumber += 1
        condition = iterationNumber > 10**(15)
        #condition = checkX(X)

    print("===== Rotation Method =====")
    print("X:")
    X.display()
    print("A:")
    A.display()
    #print("Test:")
    #r.display()
    print("====================")
Exemplo n.º 2
0
def rotationMethod(m,A,a,epsilon,p):
    VECTOR = 1
    X = Matrix(m,VECTOR)
    oldA = Matrix(m,m)
    r = Matrix(m,VECTOR)
    iterationNumber = 0
    condition = True
    while condition:
        oldA = copy.deepcopy(A)
        r = copy.deepcopy(a.substractMatrix(A.matrixMultiplication(X,a)))
        (xpq,p,q) = getMax(A.lowerTriangularMatrix())
        theta = 0
        if A.lowerTriangularMatrix().at(p,p) == A.lowerTriangularMatrix().at(q,q):
            theta = math.pi/4
        else:
            theta = (1/2)*math.arct(( 2 * xpq )/(A.upperTriangularMatrix().at(p,p) - A.upperTriangularMatrix().at(q,q)))
        c = math.cos(theta)
        s = math.sin(theta)
        T = Matrix(m,m)
        for i in range(0,T.numberOfColumns):
            T.insert(i,i,1)
        T.insert(p,p,c)
        T.insert(p,q,s)
        T.insert(q,p,-s)
        T.insert(q,q,c)
        A = copy.deepcopy((T.transpose()).multiplyMatrix(oldA.multiplyMatrix(T)))
        '''
        if iterationNumber%100 == 0:
            print("-----")
            print("Situation at iteration:",iterationNumber)
            print("Condition:",condition)
            print("A matrix:")
            A.display()
            print("X matrix:")
            X.display()
            print("-----")
        '''
        X = copy.deepcopy(oldA)
        iterationNumber += 1
        condition = (iterationNumber < ITERATIONS)

    print("===== Rotation Method =====")
    print("X:")
    X.display()
    print("A:")
    A.display()
    print("Test:")
    r.display()
    print("====================")