Exemplo n.º 1
0
def factor(A):
    col_labels = sorted(A.D[1], key=repr)
    Acols = dict2list(mat2coldict(A),col_labels)
    Qlist, Rlist = aug_orthonormalize(Acols)
    #Now make Mats
    Q = coldict2mat(Qlist)
    R = coldict2mat(list2dict(Rlist, col_labels))
    return Q,R
Exemplo n.º 2
0
def factor(A):
    """ Implementation of the Gram-Schmidt procedure.
    """
    col_labels = sorted(A.D[1], key=hash)
    Acols = dict2list(matrix2coldict(A), col_labels)
    Qlist, Rlist = aug_orthonormalize(Acols)
    # Now make Matrices
    Q = coldict2mat(Qlist)
    R = coldict2mat(list2dict(Rlist, col_labels))
    return Q, R
Exemplo n.º 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
    '''
    Qlist,Rlist=orthonormalization.aug_orthonormalize([v for v in mat2coldict(A).values()])
    Q = coldict2mat(Qlist)
    R = coldict2mat(Rlist)
    return solve(R, transpose(Q)*b)
Exemplo n.º 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
    '''
    Qlist, Rlist = orthonormalization.aug_orthonormalize(
        [v for v in mat2coldict(A).values()])
    Q = coldict2mat(Qlist)
    R = coldict2mat(Rlist)
    return solve(R, transpose(Q) * b)
Exemplo n.º 5
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)