コード例 #1
0
def bits2mat(bits,nrows=4,trans=False):
    """
Convert a list of bits into a matrix with nrows rows.
The matrix is populated by bits column by column
Keyword arguments:
nrows -- number of rows in the matrix (default 4)
trans -- whether to reverse rows and columns of the matrix (default False)
"""
    ncols = len(bits)//nrows
    f = {(i,j):one for j in range(ncols) for i in range(nrows) if bits[nrows*j+i]}
    A = mat.Mat((set(range(nrows)), set(range(ncols))), f)
    if trans: A = mat.transpose(A)
    return A
コード例 #2
0
def orthonormal_projection_orthogonal(W, b):
    '''
    Input:
        - W: Mat whose rows are orthonormal
        - b: Vec whose labels are equal to W's column labels
    Output:
        - The projection of b orthogonal to W's row space.
    Example: 
        >>> 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})
        True
    '''
    return b - transpose(W) * W * b
コード例 #3
0
ファイル: orthonormalization.py プロジェクト: varren/matrix
def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)
    '''
    V, S = aug_orthogonalize(L)
    Q = orthonormalize(L)
    R = mat2coldict(transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S))
    
    return (Q,[x for x in R.values()])
コード例 #4
0
ファイル: hw7.py プロジェクト: gregorycook/PythonProjects
def orthonormal_projection_orthogonal(W, b):
    '''
    Input:
        - W: Mat whose rows are orthonormal
        - b: Vec whose labels are equal to W's column labels
    Output:
        - The projection of b orthogonal to W's row space.
    Example: 
        >>> 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})
        True
    '''
    return b-transpose(W)*W*b
コード例 #5
0
ファイル: hw7.py プロジェクト: tri2sing/CodingTheMatrix
def orthogonal_vec2rep(Q, b):
    '''
    Input:
        - Q: an orthogonal Mat
        - b: Vec whose domain equals the column-label set of Q.
    Output:
        - The coordinate representation of b in terms of the rows of Q.
    Example:
        >>> 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})
        True
    '''
    # for orthogonal matrix the transpose is equal to its inverse 
    return b*transpose(Q)
コード例 #6
0
ファイル: orthonormalization.py プロジェクト: varren/matrix
def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)
    '''
    V, S = aug_orthogonalize(L)
    Q = orthonormalize(L)
    R = mat2coldict(
        transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S))

    return (Q, [x for x in R.values()])
コード例 #7
0
ファイル: hw7.py プロジェクト: iryek219/LinearAlgebra_python
def orthogonal_change_of_basis(A, B, a):
    '''
    Input:
        - A: an orthogonal Mat
        - B: an orthogonal Mat whose column labels are the row labels of A
        - a: the coordinate representation in terms of rows of A of some vector v 
    Output:
        - the Vec b such that b is the coordinate representation of v in terms of columns of B
    Example:
        >>> 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})
        True
    '''
    return transpose(A)*a*B
コード例 #8
0
ファイル: hw5.py プロジェクト: iryek219/LinearAlgebra_python
def find_triangular_matrix_inverse(A): 
    '''
    input: An upper triangular Mat, A, with nonzero diagonal elements
    output: Inverse of A
    >>> A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
    >>> find_triangular_matrix_inverse(A) == Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): -0.5, (1, 2): -0.3, (3, 2): 0.0, (0, 0): 1.0, (3, 3): 1.0, (3, 0): 0.0, (3, 1): 0.0, (2, 1):
    0.0, (0, 2): -0.05000000000000002, (2, 0): 0.0, (1, 3): -0.87, (2, 3): -0.1, (2, 2): 1.0, (1, 0): 0.0, (0, 3): -3.545, (1, 1): 1.0})
    True
    '''
    identity_matrix = identity(A.D[0], one)
    dict1_rowmatrix = mat2rowdict(identity_matrix)
    newdict1 = {}
    for key in dict1_rowmatrix:
        newdict1[key] = solve(A,dict1_rowmatrix[key])
    result = rowdict2mat(newdict1)
    final_result = transpose(result)
    return final_result
コード例 #9
0
def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)
            
    >>> from vec import Vec
    >>> D={'a','b','c','d'}
    >>> L = [Vec(D, {'a':4,'b':3,'c':1,'d':2}), Vec(D, {'a':8,'b':9,'c':-5,'d':-5}), Vec(D, {'a':10,'b':1,'c':-1,'d':5})]
    >>> Qlist, Rlist = aug_orthonormalize(L)
    >>> from matutil import coldict2mat
    >>> print(coldict2mat(Qlist))
    <BLANKLINE>
               0      1      2
         ---------------------
     a  |   0.73  0.187  0.528
     b  |  0.548  0.403 -0.653
     c  |  0.183 -0.566 -0.512
     d  |  0.365 -0.695  0.181
    <BLANKLINE>
    >>> print(coldict2mat(Rlist))
    <BLANKLINE>
              0    1      2
         ------------------
     0  |  5.48 8.03   9.49
     1  |     0 11.4 -0.636
     2  |     0    0   6.04
    <BLANKLINE>
    >>> print(coldict2mat(Qlist)*coldict2mat(Rlist))
    <BLANKLINE>
           0  1  2
         ---------
     a  |  4  8 10
     b  |  3  9  1
     c  |  1 -5 -1
     d  |  2 -5  5
    <BLANKLINE>
    '''
    V, S = aug_orthogonalize(L)
    Q = orthonormalize(L)
    R = mat2coldict(
        transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S))
    return (Q, [x for x in R.values()])
コード例 #10
0
ファイル: hw7.py プロジェクト: tri2sing/CodingTheMatrix
def orthonormal_projection_orthogonal(W, b):
    '''
    Input:
        - W: Mat whose rows are orthonormal
        - b: Vec whose labels are equal to W's column labels
    Output:
        - The projection of b orthogonal to W's row space.
    Example: 
        >>> 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})
        True
    '''
    # rows are orthonormal meaning that the row vectors have unit length
    # so the sigmas for every row can be written as <b*rowW>/<rowW*rowW> = <b*rowW> 
    
    return b-(b*transpose(W))*W 
コード例 #11
0
def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)
            
    >>> from vec import Vec
    >>> D={'a','b','c','d'}
    >>> L = [Vec(D, {'a':4,'b':3,'c':1,'d':2}), Vec(D, {'a':8,'b':9,'c':-5,'d':-5}), Vec(D, {'a':10,'b':1,'c':-1,'d':5})]
    >>> Qlist, Rlist = aug_orthonormalize(L)
    >>> from matutil import coldict2mat
    >>> print(coldict2mat(Qlist))
    <BLANKLINE>
               0      1      2
         ---------------------
     a  |   0.73  0.187  0.528
     b  |  0.548  0.403 -0.653
     c  |  0.183 -0.566 -0.512
     d  |  0.365 -0.695  0.181
    <BLANKLINE>
    >>> print(coldict2mat(Rlist))
    <BLANKLINE>
              0    1      2
         ------------------
     0  |  5.48 8.03   9.49
     1  |     0 11.4 -0.636
     2  |     0    0   6.04
    <BLANKLINE>
    >>> print(coldict2mat(Qlist)*coldict2mat(Rlist))
    <BLANKLINE>
           0  1  2
         ---------
     a  |  4  8 10
     b  |  3  9  1
     c  |  1 -5 -1
     d  |  2 -5  5
    <BLANKLINE>
    '''
    V, S = aug_orthogonalize(L)
    Q = orthonormalize(L)
    R = mat2coldict(transpose(coldict2mat(V)) * coldict2mat(Q) * coldict2mat(S))
    return (Q,[x for x in R.values()])
コード例 #12
0
def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)
    '''
    Q=orthonormalize(L)
    ##print("1--------------")
    ##print(Q)
    Rdict = mat2coldict(transpose(coldict2mat(Q))*coldict2mat(L))
    R = [Rdict[k] for k in Rdict]
    ##print("2--------------")
    ##print(R)
    ##print("--------------2")
    return Q,R
コード例 #13
0
def rep2vec(u, veclist):
    '''
    Input:
        - u: a Vec whose domain is set(range(len(veclist)))
        - veclist: a list of Vecs
    Output:
        the Vec whose coordinate representation is u
        (i.e u[0] is the coefficient of veclist[0], u[1] is the coefficient of veclist[1], etc.)
    Example:
        >>> v0 = Vec({'a','b','c','d'}, {'a':1})
        >>> v1 = Vec({'a','b','c','d'}, {'a':1, 'b':2})
        >>> v2 = Vec({'a','b','c','d'}, {'c':4, 'd':8})
        >>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [v0,v1,v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 24, 'b': 8, 'd': 48})
        True
        >>> rep2vec(Vec({0,1,2}, {0:2, 1:4}), [v0, v1, v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 0, 'b': 8, 'd': 0})
        True
    '''
    assert u.D == set(range(len(veclist)))
    return u * transpose(coldict2mat(veclist))
コード例 #14
0
def rep2vec(u, veclist):
    '''
    Input:
        - u: a Vec whose domain is set(range(len(veclist)))
        - veclist: a list of Vecs
    Output:
        the Vec whose coordinate representation is u
        (i.e u[0] is the coefficient of veclist[0], u[1] is the coefficient of veclist[1], etc.)
    Example:
        >>> v0 = Vec({'a','b','c','d'}, {'a':1})
        >>> v1 = Vec({'a','b','c','d'}, {'a':1, 'b':2})
        >>> v2 = Vec({'a','b','c','d'}, {'c':4, 'd':8})
        >>> rep2vec(Vec({0,1,2}, {0:2, 1:4, 2:6}), [v0,v1,v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 24, 'b': 8, 'd': 48})
        True
        >>> rep2vec(Vec({0,1,2}, {0:2, 1:4}), [v0, v1, v2]) == Vec({'d', 'a', 'c', 'b'},{'a': 6, 'c': 0, 'b': 8, 'd': 0})
        True
    '''
    assert u.D == set(range(len(veclist)))
    return u*transpose(coldict2mat(veclist))
コード例 #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 = QR.factor(A)
    return triangular_solve(mat2rowdict(R), sorted(A.D[1], key=repr), transpose(Q) * b)
コード例 #16
0
ファイル: hw7.py プロジェクト: vasuharish/CodingTheMatrix
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)
コード例 #17
0
ファイル: hw7.py プロジェクト: df1111/CodingTheMatrix
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)
コード例 #18
0
ファイル: hw7.py プロジェクト: gregorycook/PythonProjects
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)
コード例 #19
0
def aug_orthonormalize(L):
    '''
    Input:
        - L: a list of Vecs
    Output:
        - A pair Qlist, Rlist such that:
            * coldict2mat(L) == coldict2mat(Qlist) * coldict2mat(Rlist)
            * Qlist = orthonormalize(L)

    >>> L = [list2vec(v) for v in [[4,3,1,2], [8,9,-5,-5],[10,1,-1,5]] ]
>>> L
[Vec({0, 1, 2, 3},{0: 4, 1: 3, 2: 1, 3: 2}), Vec({0, 1, 2, 3},{0: 8, 1: 9, 2: -5, 3: -5}), Vec({0, 1, 2, 3},{0: 10, 1: 1, 2: -1, 3: 5})]
    '''
    Qlist = orthonormalize(L)
 
    Rsub = transpose(coldict2mat(Qlist)) * coldict2mat(L)
    x = mat2coldict(Rsub)
    Rlist  = []
    for i in x:
        Rlist.append(x[i])
    return  Qlist,  Rlist
コード例 #20
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)
コード例 #21
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat with linearly independent columns
        - b: a Vec whose domain equals the set of row-labels of A
    Output:
        - vector x that minimizes norm(b - A*x)
    Note: This procedure uses the procedure QR_factor, which in turn uses dict2list and list2dict.
           You wrote these procedures long back in python_lab.  Make sure the completed python_lab.py
           is in your matrix directory.
    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.is_almost_zero()
        True
    '''
    Q, R = QR_factor(A)
    c = transpose(Q) * b
    x = triangular_solve_n(mat2rowdict(R), c)
    return x
コード例 #22
0
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat with linearly independent columns
        - b: a Vec whose domain equals the set of row-labels of A
    Output:
        - vector x that minimizes norm(b - A*x)
    Note: This procedure uses the procedure QR_factor, which in turn uses dict2list and list2dict.
           You wrote these procedures long back in python_lab.  Make sure the completed python_lab.py
           is in your matrix directory.
    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.is_almost_zero()
        True
    '''
    Q, R = QR_factor(A)
    c = transpose(Q) * b
    x = triangular_solve_n(mat2rowdict(R), c)
    return x
コード例 #23
0
ファイル: hw7.py プロジェクト: varren/matrix
def solve8(A,Q,R,b):
    c =  transpose(Q) * b
    x = triangular_solve_n(mat2rowdict(R), c)
    return x
コード例 #24
0
def solve8(A, Q, R, b):
    c = transpose(Q) * b
    x = triangular_solve_n(mat2rowdict(R), c)
    return x
コード例 #25
0
ファイル: ecc_lab.py プロジェクト: roman-spiridonov/sandbox
import sys
sys.path.append('..\\matlib')  # for compatibility with running from console
from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, bits2str, mat2bits, noise
from GF2 import one
from matutil import listlist2mat
from mat import transpose

## Task 1
""" Create an instance of Mat representing the generator matrix G. You can use
the procedure listlist2mat in the matutil module (be sure to import first).
Since we are working over GF (2), you should use the value one from the
GF2 module to represent 1"""
G = transpose(listlist2mat([[one, one, 0, one, 0, 0, one],
                            [0, one, 0, one, 0, one, 0],
                            [one, 0, 0, one, one, 0, 0],
                            [one, one, one, 0, 0, 0, 0]]))

## Task 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
encoding_1001 = [0, 0, one, one, 0, 0, one]


## Task 3
# Express your answer as an instance of the Mat class.
R = listlist2mat([[0, 0, 0, 0, 0, 0, one],
                  [0, 0, 0, 0, 0, one, 0],
                  [0, 0, 0, 0, one, 0, 0],
                  [0, 0, one, 0, 0, 0, 0]])  # Rc = p: G(p) = c => R - inverse for G

print(R*G)  # identity matrix
コード例 #26
0
ファイル: hw7.py プロジェクト: zezei/linearAlgebra-coursera
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
コード例 #27
0
ファイル: hw7.py プロジェクト: buptdjd/linearAlgebra-coursera
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
コード例 #28
0
ファイル: hw7.py プロジェクト: enigne/CodingMatrix

## Problem 7
# Write your solution for this problem in orthonormalization.py.



## Problem 8
# Please give each solution as a Vec

least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]])
least_squares_Q1 = listlist2mat([[.8,-0.099],[.6, 0.132],[0,0.986]])
least_squares_R1 = listlist2mat([[10,2],[0,6.08]]) 
least_squares_b1 = list2vec([10, 8, 6])

x_hat_1 = triangular_solve_n(mat2rowdict(least_squares_R1),transpose(least_squares_Q1)*least_squares_b1)


least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]])
least_squares_Q2 = listlist2mat([[.424, .808],[.566, .115],[.707, -.577]])
least_squares_R2 = listlist2mat([[7.07, 1.7],[0,.346]])
least_squares_b2 = list2vec([10,13,15])

x_hat_2 = triangular_solve_n(mat2rowdict(least_squares_R2),transpose(least_squares_Q2)*least_squares_b2)




## Problem 9
def QR_solve(A, b):
    '''
コード例 #29
0
ファイル: hw7.py プロジェクト: df1111/CodingTheMatrix
def helper1():
    c = transpose(least_squares_Q1)*least_squares_b1
    return solve(least_squares_R1,c) 
コード例 #30
0
ファイル: hw7.py プロジェクト: vasuharish/CodingTheMatrix
def helper1():
    c = transpose(least_squares_Q1) * least_squares_b1
    return solve(least_squares_R1, c)
コード例 #31
0
ファイル: hw7.py プロジェクト: vasuharish/CodingTheMatrix
def helper2():
    c = transpose(least_squares_Q2) * least_squares_b2
    return solve(least_squares_R2, c)
コード例 #32
0
    r: Vec(
        D, {(x, y): image_dict[r][y][x]
            for y in range(len(image_dict[r]))
            for x in range(len(image_dict[r][y]))})
    for r in image_dict
}  # dict of Vecs

## Task 2

centroid = find_centroid([face_images[r] for r in face_images])
centered_face_images = {r: face_images[r] - centroid for r in face_images}

## Task 3

A = rowdict2mat(centered_face_images)  # centered image vectors
V, Sigma, U = svd.factor(transpose(A))
VT = mat2coldict(V)
orthonormal_basis = rowdict2mat([VT[r] for r in range(10)])  # 10 rows

## Task 4


#This is the "transpose" of what was specified in the text.
#Follow the spec given here.
def projected_representation(M, x):
    '''
    Input:
        - M: a matrix with orthonormal rows with M.D[1] == x.D
        - x: a vector
    Output:
        - the projection of x onto the row-space of M
コード例 #33
0
ファイル: hw7.py プロジェクト: gregorycook/PythonProjects

## Problem 7
# Write your solution for this problem in orthonormalization.py.



## Problem 8
# Please give each solution as a Vec

least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]])
least_squares_Q1 = listlist2mat([[.8,-0.099],[.6, 0.132],[0,0.986]])
least_squares_R1 = listlist2mat([[10,2],[0,6.08]]) 
least_squares_b1 = list2vec([10, 8, 6])

x_hat_1 = solve(least_squares_R1, transpose(least_squares_Q1)*least_squares_b1)


least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]])
least_squares_Q2 = listlist2mat([[.424, .808],[.566, .115],[.707, -.577]])
least_squares_R2 = listlist2mat([[7.07, 1.7],[0,.346]])
least_squares_b2 = list2vec([10,13,15])

x_hat_2 = solve(least_squares_R2, transpose(least_squares_Q2)*least_squares_b2)



## Problem 9
def QR_solve(A, b):
    '''
    Input:
コード例 #34
0
ファイル: hw7.py プロジェクト: df1111/CodingTheMatrix
def helper2():
    c = transpose(least_squares_Q2)*least_squares_b2
    return solve(least_squares_R2,c) 
コード例 #35
0
## Problem 6
# Write your solution for this problem in orthonormalization.py.

## Problem 7
# Write your solution for this problem in orthonormalization.py.

## Problem 8
# Please give each solution as a Vec

least_squares_A1 = listlist2mat([[8, 1], [6, 2], [0, 6]])
least_squares_Q1 = listlist2mat([[.8, -0.099], [.6, 0.132], [0, 0.986]])
least_squares_R1 = listlist2mat([[10, 2], [0, 6.08]])
least_squares_b1 = list2vec([10, 8, 6])

x_hat_1 = solve(least_squares_R1,
                transpose(least_squares_Q1) * least_squares_b1)

least_squares_A2 = listlist2mat([[3, 1], [4, 1], [5, 1]])
least_squares_Q2 = listlist2mat([[.424, .808], [.566, .115], [.707, -.577]])
least_squares_R2 = listlist2mat([[7.07, 1.7], [0, .346]])
least_squares_b2 = list2vec([10, 13, 15])

x_hat_2 = solve(least_squares_R2,
                transpose(least_squares_Q2) * least_squares_b2)


## Problem 9
def QR_solve(A, b):
    '''
    Input:
        - A: a Mat
コード例 #36
0
import sys
sys.path.append('..\\matlib')  # for compatibility with running from console
from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, bits2str, mat2bits, noise
from GF2 import one
from matutil import listlist2mat
from mat import transpose

## Task 1
""" Create an instance of Mat representing the generator matrix G. You can use
the procedure listlist2mat in the matutil module (be sure to import first).
Since we are working over GF (2), you should use the value one from the
GF2 module to represent 1"""
G = transpose(
    listlist2mat([[one, one, 0, one, 0, 0, one], [0, one, 0, one, 0, one, 0],
                  [one, 0, 0, one, one, 0, 0], [one, one, one, 0, 0, 0, 0]]))

## Task 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
encoding_1001 = [0, 0, one, one, 0, 0, one]

## Task 3
# Express your answer as an instance of the Mat class.
R = listlist2mat([[0, 0, 0, 0, 0, 0, one], [0, 0, 0, 0, 0, one, 0],
                  [0, 0, 0, 0, one, 0, 0],
                  [0, 0, one, 0, 0, 0,
                   0]])  # Rc = p: G(p) = c => R - inverse for G

print(R * G)  # identity matrix