Example #1
0
def seidel(a, b, coeff=1.0, eps=1e-10, limit=1000):
    if not checkSize(a, b):
        raise Exception("Invalid operands")
    a = deepcopy(a)
    b = deepcopy(b)
    n = shape(a)[0]
    for i in range(n):
        if a[i, i] == 0:
            raise Exception("Powerless method")
        b[i] /= a[i, i]
        a[i] /= a[i, i]
    x = zeroTerm(n)
    iters = 0
    maxNorm = 1e100 * n
    while norm(a * x - b) > eps:
        nextX = deepcopy(x)
        for i in range(n):
            nextX[i] -= a[i] * nextX - b[i]
        x += coeff * (nextX - x)
        iters += 1
        curNorm = norm(a * x - b)
        if iters >= limit or curNorm > maxNorm:
            return None
        it.append(iters)
        norms.append(curNorm)
    return x
Example #2
0
def seidel(a, b, coeff=1., eps=1e-10, limit=1000):
    if not checkSize(a, b):
        raise Exception("Invalid operands")
    a = deepcopy(a)
    b = deepcopy(b)
    n = shape(a)[0]
    for i in range(n):
        if a[i, i] == 0:
            raise Exception("Powerless method")
        b[i] /= a[i, i]
        a[i] /= a[i, i]
    x = zeroTerm(n)
    iters = 0
    maxNorm = 1e100 * n
    while norm(a * x - b) > eps:
        nextX = deepcopy(x)
        for i in range(n):
            nextX[i] -= a[i] * nextX - b[i]
        x += coeff * (nextX - x)
        iters += 1
        curNorm = norm(a * x - b)
        if iters >= limit or curNorm > maxNorm:
            return None
        it.append(iters)
        norms.append(curNorm)
    return x
Example #3
0
def gradient(a, b, eps = 1e-10, limit = 1000):
    a = deepcopy(a)
    b = deepcopy(b)
    if not checkSize(a, b):
        raise Exception("Invalid operands")
    n = shape(a)[0]
    x = zeroTerm(n)
    r1 = b - a * x
    r2 = b - a.transpose() * x
    p1 = r1
    p2 = r2
    x0 = matrix([[inf]] * n)
    iters = 0
    maxNorm = 1e100 * n
    while norm(r1) > eps:
        koef = (r2.transpose() * r1).item(0) / (p2.transpose() * (a * p1)).item(0)
        x0 = x
        x = x0 + koef * p1
        r1_new = r1 - koef * a * p1
        r2_new = r2 - koef * a.transpose() * p2
        beta = (r2_new.transpose() * r1_new).item(0) / (r2.transpose() * r1).item(0)
        r1 = r1_new
        r2 = r2_new
        p1 = r1 + beta * p1
        p2 = r2 + beta * p2
        iters += 1
        curNorm = norm(r1)
        if iters >= limit or curNorm > maxNorm:
            return None
        it.append(iters)
        norms.append(curNorm)
    return x