math4610

Routine Name: BackSub

Author: David Merkley

Language: Python

Description/Purpose: Computes solution to an upper triangular matrix.

Input:

Required:

A - matrix upper triangular

Optional:

b - vector

Output: Solution to Ax = b

Implementation/Code:

def backSub(A, b=[]):
    if (len(b) != 0):
        n = len(b)
        ans = [x for x in b]
    else:
        n = len(A)
        m = len(A[0])
        ans = [A[i][m-1] for i in range(n)]
    for i in range(n - 1, -1, -1):
        for j in range(n - 1, i, -1):
            ans[i] -= A[i][j] * ans[j]
        ans[i] /= A[i][i]
    return ans