コード例 #1
0
def main():
    from constants import A2_21 as A, b2_21 as b
    start_time = time.time()
    x = solve(A, b)
    solve_time = time.time()
    print("Roots:")
    print_vector(x)
    check_roots(A, x, b)
    det = gk.determinant(A)
    det_time = time.time()
    print
    print("Determinant: {}".format(det))
    print
    inversed = gk.inverse(A)
    inverse_time = time.time()
    print("Inverse matrix:")
    print_matrix(inversed, precision=8)
    print("Check inverse matrix:")
    print_matrix(multiply(inversed, A))

    print("Time elapsed: ")
    print(
        "\tSolution:\t{} ms\n\tDeterminant:\t{} ms\n\tInversion:\t{} ms\n\tOverall:\t{} ms"
        .format((solve_time - start_time) * 1000,
                (det_time - solve_time) * 1000,
                (inverse_time - det_time) * 1000,
                (inverse_time - start_time) * 1000))
コード例 #2
0
def get_inv(L, U):
    detL, detU = 1, 1
    for i in xrange(len(U)):
        detL *= L[i][i]
        detU *= U[i][i]
    invL = m.divide_by_scalar(m.transpose(L), detL)
    invu = m.divide_by_scalar(m.transpose(U), detU)
    return m.multiply(invu, invL)
コード例 #3
0
def solve(Q, R, b):
    y = multiply(transpose(Q), b)
    print_matrix(R, round_elem=True)
    C = append_column(R, y)

    x = [backward_right(C, i) for i in xrange(len(C) - 1, -1, -1)]
    x.reverse()
    return x
コード例 #4
0
def decompose(A):
    n = len(A)
    Q = eye(n)
    R = A
    householders_reflections = []
    params_matrix_list = [R]

    for i in xrange(n - 1):
        col = transpose([get_column(R, i)[i:]])
        H = get_householder_reflection_matrix(col)
        if (i != 0):
            for j in xrange(i):
                H = extend_householder_matrix(H)
        R = multiply(H, R)
        Q = multiply(Q, H)
        householders_reflections.append(H)
        params_matrix_list.append(R)
    return Q, R, householders_reflections
コード例 #5
0
def inverse(A, U):
  n = len(U)
  T = inverse_triangle_right(U)
  print
  print("U^-1:")
  print_matrix(T)
  print("Check U^-1:")
  print_matrix(multiply(T, U))
  B = zeros(n, n)
  for i in xrange(n):
    for j in xrange(n):
      for k in xrange(min(i, j), n):
        B[i][j] += T[i][k] * T[j][k]
  return B
コード例 #6
0
def lu(A):
    """Decomposes a nxn matrix A by PA=LU and returns L, U and P."""
    n = len(A)
    L = [[0.0] * n for i in xrange(n)]
    U = [[0.0] * n for i in xrange(n)]
    P = pivotize(A)
    A2 = m.multiply(P, A)
    for j in xrange(n):
        L[j][j] = 1.0
        for i in xrange(j + 1):
            s1 = sum(U[k][j] * L[i][k] for k in xrange(i))
            U[i][j] = A2[i][j] - s1
        for i in xrange(j, n):
            s2 = sum(U[k][j] * L[i][k] for k in xrange(j))
            L[i][j] = (A2[i][j] - s2) / U[j][j]
    return L, U, P
コード例 #7
0
def inverse(Q, R):
    invR = inverse_triangle_right(R)
    invQ = transpose(Q)
    return multiply(invR, invQ)
コード例 #8
0
def get_invariant(L, Q):
    invL = inverse_triangle_left(L)
    invQ = transpose(Q)
    return multiply(invQ, invL)
コード例 #9
0
def inverse_triangle_left(L):
    n = len(L)
    T = zeros(n, n)
    for i in xrange(n - 1, -1, -1):
        T[i][i] = 1. / L[i][i]
        for j in xrange(i - 1, -1, -1):
            for k in xrange(n - 1, j, -1):
                T[i][j] -= T[i][k] * L[k][j]
            T[i][j] /= L[j][j]
    return T


from constants import A1_30 as A, B1_30 as B
n = len(A)
Q = eye(n)
L = multiply(Q, A)

householders_reflections = []
params_matrix_list = [L]

for i in xrange(n - 1):
    col = transpose([get_row(L, i)[i:]])
    H = get_householder_reflection_matrix(col)
    if (i != 0):
        for j in xrange(i):
            H = extend_householder_matrix(H)
    L = multiply(L, H)
    Q = multiply(H, Q)
    householders_reflections.append(H)
    params_matrix_list.append(L)
コード例 #10
0
    A2 = m.multiply(P, A)
    for j in xrange(n):
        L[j][j] = 1.0
        for i in xrange(j + 1):
            s1 = sum(U[k][j] * L[i][k] for k in xrange(i))
            U[i][j] = A2[i][j] - s1
        for i in xrange(j, n):
            s2 = sum(U[k][j] * L[i][k] for k in xrange(j))
            L[i][j] = (A2[i][j] - s2) / U[j][j]
    return L, U, P


from var30_constants import A1 as A, B1 as B
L, U, P = lu(A)

D = m.multiply(P, B)
C = []
for i in xrange(len(L)):
    C.append(L[i] + [D[i][0]])
m.print_matrix(C)
y = m.transpose([[backward_left(C, i) for i in xrange(len(C))]])

C = []
for i in xrange(len(U)):
    C.append(U[i] + [y[i][0]])
x = [backward_right(C, i) for i in xrange(len(C) - 1, -1, -1)]
x.reverse()

m.print_matrix(m.multiply(A, m.transpose(x)))
m.print_matrix((get_inv(L, U)))