Example #1
0
def ConjugatedGradient(m,A,a,epsilon,p):
    '''
    The following 5 lines are not part of the algorithm per se. They are helpers.
    '''
    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

    X = Matrix(m,VECTOR)
    Y = Matrix(m,VECTOR)
    r = Matrix(m,VECTOR)
    aux = Matrix(m,VECTOR)
    v = Matrix(m,VECTOR)

    for i in range(1,mathM):
        X.mathInsert(i,VECTOR,1)
    aux = copy.deepcopy(A.multiplyMatrix(X))
    r = copy.deepcopy(a.substractMatrix(aux))
    v = copy.deepcopy(r)
    for i in range(1,mathM):
        sum1 = 0
        for j in range(1,mathM):
            sum1 = sum1 + r.mathAt(j,1)**2
        av = Matrix(m,VECTOR)
        av = copy.deepcopy(A.multiplyMatrix(v))
        sum2 = 0
        for j in range(1,mathM):
            sum2 = sum2 + av.mathAt(j,1) * v.mathAt(j,1)
        ai = 0
        ai = sum1 / sum2+(10**(-10))
        aux = copy.deepcopy(v.scalarMultiplication(ai))
        aux = copy.deepcopy(aux.addMatrix(X))
        Y = copy.deepcopy(aux)
        aux = copy.deepcopy(A.multiplyMatrix(Y))
        r = copy.deepcopy(a.substractMatrix(aux))
        sum3 = 0
        ci = 0
        for j in range(1,mathM):
            sum3 = sum3 + r.mathAt(j,1)**2
        ci = sum3 / sum1
        aux = copy.deepcopy(v.scalarMultiplication(ci))
        aux = copy.deepcopy(r.addMatrix(aux))
        v = copy.deepcopy(aux)
        X = copy.deepcopy(Y)
    print("===== Conjugated Gradient =====")
    print("Optim solution (x):")
    X.display()
    print("Test:")
    result = A.multiplyMatrix(X)
    result.display()
    print("====================")
Example #2
0
def otherJacobi(m,A,a,epsilon,p):
    VECTOR = 1
    X = Matrix(m,VECTOR)
    oldX = Matrix(m,VECTOR)
    r = Matrix(m,VECTOR)
    D = copy.deepcopy(A.diagonalMatrix())
    E = copy.deepcopy((A.negativeElements()).lowerTriangularMatrix())
    F = copy.deepcopy((A.negativeElements()).upperTriangularMatrix())

    P = copy.deepcopy(D)
    N = copy.deepcopy(D.substractMatrix(A))

    inverseD = copy.deepcopy(D.inverse())
    Bj = copy.deepcopy(inverseD.multiplyMatrix(copy.deepcopy(E.addMatrix(F))))

    for iteration in range(0,100):
        Bjp = copy.deepcopy((Bj.scalarMultiplication(p)).addMatrix((matrix.newIdentiryMatrix(m,m)).scalarMultiplication(1-p)))

    condition = True
    iterationNumber = 0
    while condition:
        r = copy.deepcopy(a.substractMatrix(A.multiplyMatrix(X)))
        oldX = copy.deepcopy(X)
        X = copy.deepcopy(oldX.addMatrix( copy.deepcopy( inverseD.scalarMultiplication(p) ).multiplyMatrix(r) ))
        condition = not r.isAlmostZero()

        if iterationNumber%100 == 0:
            print("-----")
            print("Situation at iteration:",iterationNumber)
            print("Condition:",condition)
            print("oldX:")
            oldX.display()
            print("X:")
            X.display()
            print("The r vector:")
            r.display()
            print("-----")
        iterationNumber += 1

    print("===== Other Jacobi =====")
    print("X:")
    X.display()
    print("Test:")
    r.display()
    print("====================")