def Jacobi(self, b, mat=True):
    if isinstance(b, matrix):
        b = b.c(range(b.size[0]), 0)
    D = self.D
    A = self.A.data
    B=[[-A_ij/D_i if i!=j \
       else 1-A_ij/D_i for j,A_ij in enumerate(A[i])]for i,D_i in enumerate(D)]
    if mat == True:
        B = matrix(B)
    f = [b_i / D[i] for i, b_i in enumerate(b)]
    f = matrix(f).T()
    return B, f
def GaussEq(A, B):
    if isinstance(B, matrix):
        B = B.c(range(B.size[0]), 0)
    B = copy.deepcopy(B)
    size = A.size
    data = copy.deepcopy(A.data)
    row_arg = range(size[0])
    if size[0] != size[1]:
        print('error\n')
        return ValueError
    size = size[0]
    for i in row_arg[:-1]:
        vec = matrix(data).c(range(i, size), i)
        max_index = i + argabsmax(vec)
        data[max_index], data[i] = data[i], data[max_index]
        B[max_index], B[i] = B[i], B[max_index]
        for index in range(i + 1, size):
            try:
                param = data[index][i] / data[i][i]
                data[index] = add_vec(data[index], data[i], -param)
                B[index] += B[i] * (-param)
            except:
                print('error: a singular matrix')
                return ValueError
    row_arg.reverse()
    x = [0 for i in range(size)]
    for i in row_arg:
        x[i] = (B[i] - sum(dot_vec(x[i:], data[i][i:]))) / data[i][i]
    return x
 def __init__(self, A):
     data = A.data
     self.size = A.size
     size = A.size[0]
     self.A = copy.deepcopy(A)
     self.L = [[-data[i][j] for j in range(i)] for i in range(1, size)]
     self.D = [data[i][i] for i in range(size)]
     self.X = matrix([1 for i in self.D]).T()
def Gauss_Seidel(self, b, mat=True):
    if isinstance(b, matrix):
        b = b.c(range(b.size[0]), 0)
    D = self.D
    L = self.L
    A = self.A
    size = A.size
    Dinv = [[-L[i - 1][j] if j < i else 0 for j in xrange(size[1])]
            for i in xrange(size[0])]
    for i in range(size[0]):
        Dinv[i][i] = D[i]
    Dinv = matrix(Dinv).inv(mat=True)
    B = matrix(size, eye=True) - Dinv * A
    if mat == False:
        B = B.data
    f = Dinv * matrix(b).T()
    return B, f
def LU(A):
    size = A.size
    if size[0] != size[1]:
        return ValueError
    L = matrix(size, eye=True)
    U = matrix(size, eye=False)
    size = size[0]
    aij = A.data
    for i in range(0, size):
        rang = range(0, i)
        for r in range(i, size):
            U.data[i][r] = aij[i][r] - sum(
                dot_vec(L.c([i], rang), U.c(rang, [r])))
        for r in range(i + 1, size):
            L.data[r][i] = (aij[r][i] - sum(
                dot_vec(L.c([r], rang), U.c(rang, [i])))) / U.data[i][i]

    return L.data, U.data
def _SOR(self, b, w, mat=True):
    if isinstance(b, matrix):
        b = b.c(range(b.size[0]), 0)
    D = self.D
    L = self.L
    A = self.A
    size = A.size
    data = A.data
    Dinv = [[-w * L[i - 1][j] if j < i else 0 for j in xrange(size[1])]
            for i in xrange(size[0])]
    LA = [[-w * data[i][j] if j >= i else 0 for j in xrange(size[1])]
          for i in xrange(size[0])]

    for i in range(size[0]):
        Dinv[i][i] = D[i]
        LA[i][i] += D[i]
    Dinv = matrix(Dinv).inv(mat=True)
    LA = matrix(LA)
    SORmat = Dinv * LA
    b = matrix(b).T()
    f = w * Dinv * b
    return SORmat, f
def QR_split(B, NumLimit=10e-9, P_is_a_matrix=False):
    if isinstance(B, matrix):
        size = B.size[0]
        A = copy.deepcopy(B.data)
    else:
        A = copy.deepcopy(B)
        size = len(A)
    P = functionStack()
    for i in xrange(size):
        for j in xrange(i + 1, size):

            c, s = cg_qr(A, i, j)
            kw = {'a': i, 'b': j, 'c': c, 's': s}
            P.getin(Givens(A, **kw))
            if abs(A[j][i]) < NumLimit:
                A[j][i] = 0
    if P_is_a_matrix:
        e = matrix(size).data
        e = P.pullout(e)
        del P
        P = matrix(e).T()
    return P, matrix(A)
def Norm(A, p='inf', epoch_if_norm2=200):
    if isinstance(A, matrix):
        data = A.data
        tdata = A.T().data
    else:
        data = A
        A = matrix(data)
        tdata = A.T().data
    switch = {
        1: lambda A: max([sum(abs(j) for j in i) for i in tdata]),
        2: lambda A: (PowIter(A.T() * A).fit(epoch_if_norm2)**(1 / 2)),
        'inf': lambda A: max([sum(abs(j) for j in i) for i in data]),
        'F':
        lambda A: math.sqrt(sum([data[i][i]**2 for i in range(len(data))]))
    }
    return switch[p](A)
def Chol(A):
    size = A.size
    if size[0] != size[1]:
        return ValueError
    if not check_symmetrical(A):
        print 'Not symmetrical!\n'
        return ValueError
    aij = A.data
    L = matrix(size, eye=False)
    size = size[0]
    for j in range(size):
        try:
            L[j][j] = math.sqrt(aij[j][j] - sum(dot_vec(L[j][:j], L[j][:j])))
        except:
            print 'error:Not positive!\n'

        for i in range(j + 1, size):
            try:
                L[i][j] = (aij[i][j] -
                           sum(dot_vec(L[i][:j], L[j][:j]))) / L[j][j]
            except:
                print 'error:Not positive!\n'
    return L
 def __init__(self, A):
     L, U = LU(A)
     self.L = matrix(L)
     self.U = matrix(U)
def Cond(A, p=2):
    if isinstance(A, matrix):
        inv = A.inv()
    else:
        inv = matrix(A).inv()
    return Norm(A, p) * Norm(inv, p)