def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q,R = factor(A)
    return triangular_solve(mat2rowdict(R),list(A.D[1]), Q.transpose()*b)

# test
#domain = ({'a','b','c'},{'A','B'})
#A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
#Q, R = factor(A)
#b = Vec(domain[0], {'a': 1, 'b': -1})
#x = QR_solve(A, b)
#result = A.transpose()*(b-A*x)
#print(result * result < 1E-10)
示例#2
0
文件: hw7.py 项目: fperezlo/matrix
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    from triangular import triangular_solve
    from QR import factor
    from matutil import mat2rowdict
    
    Q,R = factor(A)
    rowlist = [mat2rowdict(R)[v] for v in mat2rowdict(R)]
    label_list = sorted(A.D[1], key = repr)
    
    return triangular_solve(rowlist, label_list, b*Q)
示例#3
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    from triangular import triangular_solve
    from QR import factor
    from dictutil import dict2list
    from vecutil import vec2list

    Q,R = factor(A)
    b = Q.transpose()*b
    rowdict = mat2rowdict(R)
    rowlist = dict2list(rowdict, list(rowdict.keys()))
    x = triangular_solve(rowlist, list(rowlist[0].D), vec2list(b))
    return x
示例#4
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    from QR import factor
    from triangular import triangular_solve
    from mat import transpose
    from matutil import mat2rowdict
    Q, R = factor(A)
    qT = Q.transpose()
    c = qT * b
    rows = [rowVec for index, rowVec in mat2rowdict(R).items()]
    colLabels = sorted(A.D[1], key=repr)
    x_hat = triangular_solve(rows, colLabels, c)
    return x_hat
示例#5
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    from QR import factor
    from triangular import triangular_solve
    from mat import transpose
    from matutil import mat2rowdict
    Q, R = factor(A)
    qT = Q.transpose()
    c = qT*b
    rows = [rowVec for index,rowVec in mat2rowdict(R).items()]
    colLabels = sorted(A.D[1], key=repr)
    x_hat = triangular_solve(rows,colLabels,c)
    return x_hat
示例#6
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    c = Q.transpose() * b
    return triangular_solve(mat2rowdict(R), sorted(A.D[1], key=repr), c)


# A=Mat(({'a','b','c'},{'A','B'}), {('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1, ('c','B'):-2})
# b = Vec({'a','b','c'}, {'a':1,'b':-1})
# x = QR_solve(A,b)
# print(x)
# print(A.transpose()*(b-A*x))

# print(QR_solve(least_squares_A1, least_squares_b1))
# print(QR_solve(least_squares_A2, least_squares_b2))
示例#7
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    from triangular import triangular_solve
    from QR import factor
    from matutil import mat2rowdict

    Q, R = factor(A)
    rowlist = [mat2rowdict(R)[v] for v in mat2rowdict(R)]
    label_list = sorted(A.D[1], key=repr)

    return triangular_solve(rowlist, label_list, b * Q)
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q,R = factor(A)
    c = Q.transpose()*b
    return triangular_solve(mat2rowdict(R), sorted(A.D[1],key=repr), c)

# A=Mat(({'a','b','c'},{'A','B'}), {('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1, ('c','B'):-2})
# b = Vec({'a','b','c'}, {'a':1,'b':-1})
# x = QR_solve(A,b)
# print(x)
# print(A.transpose()*(b-A*x))

# print(QR_solve(least_squares_A1, least_squares_b1))
# print(QR_solve(least_squares_A2, least_squares_b2))
示例#9
0
def find_null_basis(A):
    Q, R = factor(A)
    R_inverse = find_matrix_inverse(R)
    R_inverse_list = mat2coldict(R_inverse)
    Q_list = mat2coldict(Q)
    zero_vector = zero_vec(Q_list[0].D)
    return [
        R_inverse_list[i] for i in range(len(Q_list))
        if Q_list[i] is zero_vector
    ]
示例#10
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    c = transpose(Q)*b
    return solve(R,c)
示例#11
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    c = transpose(Q) * b
    return solve(R, c)
示例#12
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q,R = factor(A)
    Qt = Q.transpose()
    Rx = Qt * b
    return triangular_solve(list(mat2rowdict(R).values()), sorted(R.D[1], key=repr), Rx)
示例#13
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    Qt = Q.transpose()
    Rx = Qt * b
    return triangular_solve(list(mat2rowdict(R).values()),
                            sorted(R.D[1], key=repr), Rx)
示例#14
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    bb = b * Q
    label_list = sorted(A.D[1], key=repr)
    sol = triangular_solve(mat2rowdict(R), label_list, bb)
    return sol
示例#15
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    bb = b * Q
    label_list = sorted(A.D[1], key=repr)
    sol = triangular_solve(mat2rowdict(R), label_list, bb)
    return sol
示例#16
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    c = Q.transpose()*b
    Rdict = mat2rowdict(R)
    Rrows = [Rdict[key] for key in Rdict]
    labels = sorted(A.D[1], key=repr)
    return triangular_solve(Rrows, labels, c)
示例#17
0
def QR_solve(A, b):
    '''
Input:
- A: a Mat
- b: a Vec
Output:
- vector x that minimizes norm(b - A*x)
Example:
>>> domain = ({'a','b','c'},{'A','B'})
>>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
>>> Q, R = QR.factor(A)
>>> b = Vec(domain[0], {'a': 1, 'b': -1})
>>> x = QR_solve(A, b)
>>> result = A.transpose()*(b-A*x)
>>> result * result < 1E-10
True
'''
    from triangular import triangular_solve
    from QR import factor
    from matutil import mat2rowdict
    Q, R = factor(A)
    c = Q.transpose() * b
    x = triangular_solve(list(mat2rowdict(R).values()), list(A.D[1]), c)
    return x
示例#18
0
文件: hw7.py 项目: niujie/CMLACSA
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    from triangular import triangular_solve
    from QR import factor
    from matutil import mat2rowdict
    Q, R = factor(A)
    c = Q.transpose() * b
    x = triangular_solve(list(mat2rowdict(R).values()), list(A.D[1]), c)
    return x
示例#19
0
def QR_solve(A, b):
    Q, R = factor(A)
    R_rowlist = mat2rowdict(R)
    label_list = sorted(A.D[1], key=repr)
    return triangular_solve(R_rowlist, label_list, Q.transpose() * b)
示例#20
0
from hw7 import QR_solve
from mat import coldict2mat
from mat import Mat
from orthonormalization import aug_orthonormalize
from QR import factor
from vec import Vec
from vecutil import list2vec

print('Augmented Orthonormalize')
L = [list2vec(v) for v in [[4,3,1,2],[8,9,-5,-5],[10,1,-1,5]]]
print(coldict2mat(L))
Qlist, Rlist = aug_orthonormalize(L)
print(coldict2mat(Qlist))
print(coldict2mat(Rlist))
print((coldict2mat(Qlist)*coldict2mat(Rlist)))

print('QR Solve')

A=Mat(({'a','b','c'},{'A','B'}), {('a','A'):-1, ('a','B'):2, ('b','A'):5, ('b','B'):3,('c','A'):1, ('c','B'):-2})
print(A)
Q, R = factor(A)
print(Q)
print(R)
b = Vec({'a','b','c'}, {'a':1,'b':-1})
x = QR_solve(A,b)
print(x)
residual = A.transpose()*(b-A*x)
print(residual)
示例#21
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    c = transpose(Q) * b
    x_hat = solve(R, transpose(Q) * b)
    return x_hat

# from vecutil import *
# from orthonormalization import *
# import QR
# from mat import Mat,transpose
# from solver import solve
# from vec import Vec
# from math import sqrt
# import matutil
# from hw7 import *
# vlist =[list2vec(x) for x in [[2, 4, 3, 5, 0], [4, -2, -5, 4, 0], [-8, 14, 21, -2, 0], [-1, -4,-4, 0, 0], [-2, -18, -19, -6, 0], [5, -3, 1, -5, 2]]]
# print(basis(vlist))
# v = [2, 4, 3, 5, 0]
# v * v
# Q = Mat(({0, 1}, {0, 1}), {(0, 1): 0, (1, 0): 0, (0, 0): 2, (1, 1): 2})
# b = Vec({0, 1},{0: 4, 1: 2})
# orthogonal_vec2rep(Q, b) == Vec({0, 1},{0: 8, 1: 4})
# A = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 1, (2, 0): 0, (1, 0): 0, (2, 2): 1, (0, 2): 0, (2, 1): 0, (1, 1): 1})
# B = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 2, (2, 0): 0, (1, 0): 0, (2, 2): 2, (0, 2): 0, (2, 1): 0, (1, 1): 2})
# a = Vec({0, 1, 2},{0: 4, 1: 1, 2: 3})
# orthogonal_change_of_basis(A, B, a) == Vec({0, 1, 2},{0: 8, 1: 2, 2: 6})
# A2 = matutil.listlist2mat([[1/sqrt(2), 1/sqrt(2), 0], [1/sqrt(3), -1/sqrt(3), 1/sqrt(3)], [-1/sqrt(6), 1/sqrt(6), 2/sqrt(6)]])
# B2 = matutil.listlist2mat([[1/sqrt(2), 1/sqrt(2), 0], [1/sqrt(3), -1/sqrt(3), 1/sqrt(3)], [-1/sqrt(6), 1/sqrt(6), 2/sqrt(6)]])
# a2 = Vec({0, 1, 2}, {0: sqrt(2), 1: 1/sqrt(3), 2: 2/sqrt(6)})
# orthogonal_change_of_basis(A2, B2, a2)
# W = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 1, (1, 0): 0, (0, 2): 0, (1, 1): 1})
# b = Vec({0, 1, 2},{0: 3, 1: 1, 2: 4})
# orthonormal_projection_orthogonal(W, b) == Vec({0, 1, 2},{0: 0, 1: 0, 2: 4})
# L = [list2vec(v) for v in [[4,3,1,2],[8,9,-5,-5],[10,1,-1,5]]]
# print(matutil.coldict2mat(L))
# Qlist, Rlist = aug_orthonormalize(L)
# print(matutil.coldict2mat(Qlist))
# print(matutil.coldict2mat(Rlist))
# # to solve prob 8, read lecture 8-8 early half
# B = list2vec([10,8,6])
# Q = matutil.listlist2mat([[0.8, -0.099],[0.6, 0.132],[0,0.986]])
# R = matutil.listlist2mat([[10,2],[0,6.08]])
# A = Q * R
# c = transpose(Q) * B
# x = solve(R, c)
# x
# B = list2vec([10,13,15])
# Q = matutil.listlist2mat([[.424, .808],[.566, .115],[.707, -.577]])
# R = matutil.listlist2mat([[7.07, 1.7],[0,.346]])
# A = Q * R
# c = transpose(Q) * B
# x = solve(R, c)
# x

# domain = ({'a','b','c'},{'A','B'})
# A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
# Q, R = QR.factor(A)
# b = Vec(domain[0], {'a': 1, 'b': -1})
# x = QR_solve(A, b)
# result = A.transpose()*(b-A*x)
# result * result < 1E-10
示例#22
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
        - b: a Vec
    Output:
        - vector x that minimizes norm(b - A*x)
    Example:
        >>> domain = ({'a','b','c'},{'A','B'})
        >>> A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
        >>> Q, R = QR.factor(A)
        >>> b = Vec(domain[0], {'a': 1, 'b': -1})
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result * result < 1E-10
        True
    '''
    Q, R = factor(A)
    c = transpose(Q) * b
    x_hat = solve(R, transpose(Q) * b)
    return x_hat


# from vecutil import *
# from orthonormalization import *
# import QR
# from mat import Mat,transpose
# from solver import solve
# from vec import Vec
# from math import sqrt
# import matutil
# from hw7 import *
# vlist =[list2vec(x) for x in [[2, 4, 3, 5, 0], [4, -2, -5, 4, 0], [-8, 14, 21, -2, 0], [-1, -4,-4, 0, 0], [-2, -18, -19, -6, 0], [5, -3, 1, -5, 2]]]
# print(basis(vlist))
# v = [2, 4, 3, 5, 0]
# v * v
# Q = Mat(({0, 1}, {0, 1}), {(0, 1): 0, (1, 0): 0, (0, 0): 2, (1, 1): 2})
# b = Vec({0, 1},{0: 4, 1: 2})
# orthogonal_vec2rep(Q, b) == Vec({0, 1},{0: 8, 1: 4})
# A = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 1, (2, 0): 0, (1, 0): 0, (2, 2): 1, (0, 2): 0, (2, 1): 0, (1, 1): 1})
# B = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 2, (2, 0): 0, (1, 0): 0, (2, 2): 2, (0, 2): 0, (2, 1): 0, (1, 1): 2})
# a = Vec({0, 1, 2},{0: 4, 1: 1, 2: 3})
# orthogonal_change_of_basis(A, B, a) == Vec({0, 1, 2},{0: 8, 1: 2, 2: 6})
# A2 = matutil.listlist2mat([[1/sqrt(2), 1/sqrt(2), 0], [1/sqrt(3), -1/sqrt(3), 1/sqrt(3)], [-1/sqrt(6), 1/sqrt(6), 2/sqrt(6)]])
# B2 = matutil.listlist2mat([[1/sqrt(2), 1/sqrt(2), 0], [1/sqrt(3), -1/sqrt(3), 1/sqrt(3)], [-1/sqrt(6), 1/sqrt(6), 2/sqrt(6)]])
# a2 = Vec({0, 1, 2}, {0: sqrt(2), 1: 1/sqrt(3), 2: 2/sqrt(6)})
# orthogonal_change_of_basis(A2, B2, a2)
# W = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 0, (1, 2): 0, (0, 0): 1, (1, 0): 0, (0, 2): 0, (1, 1): 1})
# b = Vec({0, 1, 2},{0: 3, 1: 1, 2: 4})
# orthonormal_projection_orthogonal(W, b) == Vec({0, 1, 2},{0: 0, 1: 0, 2: 4})
# L = [list2vec(v) for v in [[4,3,1,2],[8,9,-5,-5],[10,1,-1,5]]]
# print(matutil.coldict2mat(L))
# Qlist, Rlist = aug_orthonormalize(L)
# print(matutil.coldict2mat(Qlist))
# print(matutil.coldict2mat(Rlist))
# # to solve prob 8, read lecture 8-8 early half
# B = list2vec([10,8,6])
# Q = matutil.listlist2mat([[0.8, -0.099],[0.6, 0.132],[0,0.986]])
# R = matutil.listlist2mat([[10,2],[0,6.08]])
# A = Q * R
# c = transpose(Q) * B
# x = solve(R, c)
# x
# B = list2vec([10,13,15])
# Q = matutil.listlist2mat([[.424, .808],[.566, .115],[.707, -.577]])
# R = matutil.listlist2mat([[7.07, 1.7],[0,.346]])
# A = Q * R
# c = transpose(Q) * B
# x = solve(R, c)
# x

# domain = ({'a','b','c'},{'A','B'})
# A = Mat(domain,{('a','A'):-1, ('a','B'):2,('b','A'):5, ('b','B'):3,('c','A'):1,('c','B'):-2})
# Q, R = QR.factor(A)
# b = Vec(domain[0], {'a': 1, 'b': -1})
# x = QR_solve(A, b)
# result = A.transpose()*(b-A*x)
# result * result < 1E-10