Ejemplo n.º 1
0
def ShultsZeidelSolve(A):
    gamma = max((A * A.getTrans()).getEigenvalues())
    alpha = 1 / gamma

    U = A.getTrans() * alpha
    U_prev = U.clone()
    PSI_prev = Sole.idMatrix(A.size) - (A * U_prev)

    R = PSI_prev.getR()
    L = PSI_prev - R
    U = U_prev + (U_prev * L)
    for i in range(A.size):
        for j in range(A.size):
            for k in range(A.size):
                U[i][j] += U[i][k] * R[k][j]

    while ((U_prev * PSI_prev) / (1 - PSI_prev.norm())).norm() > 0.00001:
        U_prev = U.clone()
        PSI_prev = Sole.idMatrix(A.size) - (A * U_prev)

        R = PSI_prev.getR()
        L = PSI_prev - R
        U = U_prev + (U_prev * L)
        for i in range(A.size):
            for j in range(A.size):
                for k in range(A.size):
                    U[i][j] += U[i][k] * R[k][j]
    A.obr = U
    return A.obr * A.getB()
Ejemplo n.º 2
0
def GaussInv(A):
    result = []
    for i in range(A.size):
        idRow = [0] * A.size
        idRow[i] = 1
        result.append(GaussSolve(Sole(A.getA(), idRow)))
    result = Sole(result)
    result.trans()
    return result
Ejemplo n.º 3
0
def CholeskyDecomposition(A):
    U = Sole.idMatrix(A.size)
    U.setB(A.getB())
    det = 1

    for i in range(A.size):
        s = 0
        for k in range(i):
            s += U[k][i] ** 2
        U[i][i] = (A[i][i] - s) ** 0.5
        det *= U[i][i]
        for j in range(i + 1, A.size):
            s = 0
            for k in range(i):
                s += U[k][i] * U[k][j]
            U[i][j] = (A[i][j] - s) / U[i][i]
    A.det = det
    return U
Ejemplo n.º 4
0

def ThomasSolve(A):
    n = A.size
    b = A.getB()

    deltas = [0] * (n - 1)
    lambdas = [0] * (n - 1)
    taus = [0] * n

    deltas[0] = -A[0][1] / A[0][0]
    lambdas[0] = b[0] / A[0][0]
    taus[0] = A[0][0]
    for i in range(1, A.size - 1):
        taus[i] = A[i][i] + A[i][i - 1] * deltas[i - 1]
        deltas[i] = -A[i][i + 1] / taus[i]
        lambdas[i] = (b[i] - A[i][i - 1] * lambdas[i - 1]) / taus[i]

        taus[n - 1] = A[n - 1][n - 1] + A[n - 1][n - 2] * deltas[n - 2]
        x = [0] * A.size
        x[n - 1] = (b[n - 1] - A[n - 1][n - 2] * lambdas[n - 2]) / taus[n - 1]
        for i in range(A.size - 2, -1, -1):
            x[i] = deltas[i] * x[i + 1] + lambdas[i]

    A.det = reduce(operator.mul, taus)
    return x


if __name__ == "__main__":
    Sole.printVector(ThomasSolve(Sole(filename="filename=A3")))
Ejemplo n.º 5
0
            s += U[k][i] ** 2
        U[i][i] = (A[i][i] - s) ** 0.5
        det *= U[i][i]
        for j in range(i + 1, A.size):
            s = 0
            for k in range(i):
                s += U[k][i] * U[k][j]
            U[i][j] = (A[i][j] - s) / U[i][i]
    A.det = det
    return U


def CholeskySolve(A):
    U = CholeskyDecomposition(A)
    A.det **= 2

    y = GaussSolve(Sole(U.getTrans()))
    U.setB(y)
    x = GaussSolve(U)

    inv = GaussInv(U)
    A.inv = inv * Sole(inv.getTrans())

    return x


if __name__ == '__main__':
    sole = Sole(filename='../Data/A2')
    Sole.printVector(CholeskySolve(sole))
    sole.inv.Print()
Ejemplo n.º 6
0
            A[i][k] /= tmp

        for j in range(i + 1, A.size):
            tmp = A[j][i]
            for k in range(A.size + 1):
                A[j][k] -= A[i][k] * tmp

    for i in range(A.size - 1, -1, -1):
        for j in range(i - 1, -1, -1):
            tmp = A[j][i]
            for k in range(A.size + 1):
                A[j][k] -= A[i][k] * tmp

    _A.det = det
    return [float(elem) for elem in A.getB()]


def GaussInv(A):
    result = []
    for i in range(A.size):
        idRow = [0] * A.size
        idRow[i] = 1
        result.append(GaussSolve(Sole(A.getA(), idRow)))
    result = Sole(result)
    result.trans()
    return result


if __name__ == '__main__':
    Sole.printVector(GaussSolve(Sole(filename='../Data/A1')))
Ejemplo n.º 7
0

def PowerSolve(A):
	y = [0] * A.size
	eps = 0.01
	p = 3

	z_prev = y
	z_next = [1] * A.size
	k = 1

	while Vector.Norm(Vector.Sub(z_next, z_prev)) > eps:
		z_prev = z_next
		y = A * z_next
		lambd = Vector.Div(y, z_next)

		if k % p == 0:
			z_next = Vector.Div(y, Vector.Norm(y))
		else:
			z_next = y
		k += 1

		print k,

	print sum(map(lambda e: e ** k, lambd)) / A.size
	return z_next


if __name__ == '__main__':
    Sole.printVector(PowerSolve(Sole(filename='../Data/B1')))
Ejemplo n.º 8
0
            div = (a1 ** 2 + a2 ** 2) ** 0.5

            c = a1 / div
            s = a2 / div

            for k in range(i, A.size + 1):
                tmp = A[i][k]
                A[i][k] = c * A[i][k] + s * A[j][k]
                A[j][k] = -s * tmp + c * A[j][k]

    x = [0] * A.size
    for i in range(A.size - 1, -1, -1):
        s = 0
        for j in range(i + 1, A.size):
            s += A[i][j] * x[j]
        s = A[i][A.size] - s
        x[i] = s / A[i][i]
    x = [0] * A.size
    for i in range(A.size - 1, -1, -1):
        s = 0
        for j in range(i + 1, A.size):
            s += A[i][j] * x[j]
        s = A[i][A.size] - s
        x[i] = s / A[i][i]
    return x


if __name__ == '__main__':
    Sole.printVector(RotateSolve(Sole(filename='../Data/A2')))
Ejemplo n.º 9
0
    U = A.getTrans() * alpha
    U_prev = U.clone()
    PSI_prev = Sole.idMatrix(A.size) - (A * U_prev)

    R = PSI_prev.getR()
    L = PSI_prev - R
    U = U_prev + (U_prev * L)
    for i in range(A.size):
        for j in range(A.size):
            for k in range(A.size):
                U[i][j] += U[i][k] * R[k][j]

    while ((U_prev * PSI_prev) / (1 - PSI_prev.norm())).norm() > 0.00001:
        U_prev = U.clone()
        PSI_prev = Sole.idMatrix(A.size) - (A * U_prev)

        R = PSI_prev.getR()
        L = PSI_prev - R
        U = U_prev + (U_prev * L)
        for i in range(A.size):
            for j in range(A.size):
                for k in range(A.size):
                    U[i][j] += U[i][k] * R[k][j]
    A.obr = U
    return A.obr * A.getB()


if __name__ == '__main__':
    Sole.printVector(ShultsZeidelSolve(Sole(filename='../Data/A1')))
Ejemplo n.º 10
0
# -*- coding: utf-8 -*-
from Sole import Sole
from time import time
from Relax import RelaxSolve


if __name__ == '__main__':
    start = time()
    Sole.printVector(RelaxSolve(Sole(filename='../Data/A1')))
    print time() - start
Ejemplo n.º 11
0
    Mj = (Dinv * (L + R)) * -1

    xk = [0] * A.size
    x = [0] * A.size

    c = Dinv * A.getB()
    q = Mj.norm()

    k = 1
    for i in range(A.size):
        xk[i] = c[i]
        for j in range(i):
            xk[i] += Mj[i][j] * xk[j]
        for j in range(i, _A.size):
            xk[i] += Mj[i][j] * x[j]
    while norm(xk + (x * -1)) > abs((1 - q) / q * 0.00001) \
    and k < 30000:
        x = xk[:]
        for i in range(_A.size):
            xk[i] = c[i]
            for j in range(i):
                xk[i] += Mj[i][j] * xk[j]
            for j in range(i, _A.size):
                xk[i] += Mj[i][j] * x[j]
        k += 1
    return xk


if __name__ == '__main__':
    Sole.printVector(ZeidelSolve(Sole(filename='../Data/A2')))