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))
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
def determinant(A):
    tr = triangalize(A)
    print("Triangalized matrix:")
    print_matrix(tr)
    det = 1.
    for i in xrange(len(tr)):
        det *= tr[i][i]
    return det
def backward_right(A, row):
    m = len(A[row])
    res = A[row][m - 1] * 1. / A[row][row]
    for i in xrange(row - 1, -1, -1):
        ai = A[i][row] * 1. / A[row][row]
        for j in xrange(row, m):
            A[i][j] -= ai * A[row][j]
    print("Backward for row #{}".format(row))
    print_matrix(A)
    print()
    return res
def backward_left(A, row):
    n = len(A)
    m = len(A[row])
    res = A[row][m - 1] * 1. / A[row][row]
    for i in xrange(row + 1, n):
        ai = A[i][row] * 1. / A[row][row]
        for j in xrange(row + 1):
            A[i][j] -= ai * A[row][j]
        A[i][m - 1] -= ai * A[row][m - 1]
    print("Backward for row #{}".format(row))
    print_matrix(A)
    print()
    return res
def forward(A, row, verbose=True):
    a = A[row][row]
    n = len(A)
    m = len(A[row])
    for j in xrange(row, m):
        A[row][j] /= 1. * a
    for i in xrange(row + 1, n):
        ai = A[i][row]
        for j in xrange(row, m):
            A[i][j] -= ai * A[row][j]
    if verbose:
        print("Forward for row #{}".format(row))
        print_matrix(A)
        print()
Пример #7
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
Пример #8
0
def solve(A, b):
  n = len(A)
  U = getU(A)
  print_matrix(U)
  Ut = transpose(U)

  y = []
  Utb = append_column(Ut, b)
  for row in xrange(n):
    y.append(gk.backward_left(Utb, row))
  print(y)
  Uy = append_column(U, [[e] for e in y])
  x = []
  for row in xrange(n - 1, -1, -1):
    x.append(gk.backward_right(Uy, row))
  x.reverse()
  return x, U
def inverse(A):
    n = len(A)
    m = len(A[0])
    I = eye(n)
    AE = append_column(A, I)
    for row in xrange(n):
        forward(AE, row, verbose=False)
    print("Forward pass:"******"Backward pass:")
    print_matrix(AE)
    print()
    E = []
    for i in xrange(n):
        E.append(AE[i][n:])
    return E
Пример #10
0
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)

C = []
for i in xrange(len(L)):
    C.append(L[i] + [B[i][0]])

print_matrix(C, round_elem=True)
y = transpose([[backward_left(C, i) for i in xrange(len(C))]])
x = multiply(transpose(Q), y)
print_matrix(x)

check_roots(A, [xi[0] for xi in x], B)

print "\ndetA = detQ * detL = " + str(
    get_determinant(len(householders_reflections), L))
print "\ninv(A) = inv(L) * inv(Q) = inv(L) * Q' (транспонированая) = "
inversed = get_invariant(L, Q)
print_matrix(inversed)
print("Check inverse matrix:")
print_matrix(multiply(inversed, A))
Пример #11
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)))