コード例 #1
0
ファイル: ecc_lab.py プロジェクト: fandres70/matrix
def find_error_matrix(S):
    """
    Input: a matrix S whose columns are error syndromes
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    return coldict2mat([find_error(mat2coldict(S)[k]) for k in mat2coldict(S)])
コード例 #2
0
ファイル: hw3.py プロジェクト: kenvifire/coursera
def dot_prod_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    return Mat(
        (A.D[0], B.D[1]),
        {
            (row, col): matutil.mat2rowdict(A)[row] * matutil.mat2coldict(B)[col]
            for row in matutil.mat2rowdict(A)
            for col in matutil.mat2coldict(B)
        },
    )
コード例 #3
0
ファイル: HW.py プロジェクト: vasuharish/Coding_the_matrix
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
    ]
コード例 #4
0
def find_error_matrix(S):
    """
    Input: a matrix S whose columns are error syndromes
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S) == Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (6, 2): 0, (5, 1): one, (0, 3): 0, (4, 0): 0, (1, 2): 0, (3, 3): 0, (6, 3): 0, (5, 0): 0, (2, 2): 0, (4, 1): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (6, 0): 0, (2, 3): 0, (4, 2): 0, (1, 0): 0, (5, 3): 0, (0, 1): 0, (6, 1): 0, (3, 1): 0, (2, 0): 0, (4, 3): one, (5, 2): 0, (0, 2): 0})
        True
    """

    return coldict2mat({k: find_error(mat2coldict(S)[k]) for k in mat2coldict(S).keys()})
コード例 #5
0
ファイル: hw5.py プロジェクト: trelsco/codingthematrix
def is_invertible(M):
    '''
input: A matrix, M
outpit: A boolean indicating if M is invertible.
>>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
>>> is_invertible(M)
True
'''
    if M.D[0] != M.D[1]: return False
    if (len(list(mat2coldict(M).values())) == rank(list(mat2coldict(M).values()))):
        return True
    else: return False
コード例 #6
0
def find_error_matrix(S):
    """
    Input: a matrix S whose columns are error syndromes
    Output: a matrix whose cth column is the error corresponding to the cth column of S.
    Example:
        >>> S = listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
        >>> find_error_matrix(S)
        Mat(({0, 1, 2, 3, 4, 5, 6}, {0, 1, 2, 3}), {(1, 2): 0, (3, 2): one, (0, 0): 0, (4, 3): one, (3, 0): 0, (6, 0): 0, (2, 1): 0, (6, 2): 0, (2, 3): 0, (5, 1): one, (4, 2): 0, (1, 0): 0, (0, 3): 0, (4, 0): 0, (0, 1): 0, (3, 3): 0, (4, 1): 0, (6, 1): 0, (3, 1): 0, (1, 1): 0, (6, 3): 0, (2, 0): 0, (5, 0): 0, (2, 2): 0, (1, 3): 0, (5, 3): 0, (5, 2): 0, (0, 2): 0})
    """
    from matutil import mat2coldict
    from matutil import coldict2mat
    return coldict2mat([find_error(mat2coldict(S)[i]) for i in mat2coldict(S)])
コード例 #7
0
ファイル: hw3.py プロジェクト: srikanth235/Matrix
def dot_product_vec_mat_mult(v, M):
    assert v.D == M.D[0]
    result = Vec(M.D[1], {r: 0 for r in M.D[1]})
    cols = mat2coldict(M)
    for k in M.D[1]:
        result.f[k] = v * cols[k]
    return result
コード例 #8
0
ファイル: hw3.py プロジェクト: srikanth235/Matrix
def lin_comb_mat_vec_mult(M, v):
    assert M.D[1] == v.D
    result = Vec(M.D[0], {c: 0 for c in M.D[0]})
    cols = mat2coldict(M)
    for k, value in v.f.items():
        result = result + value * (cols[k])
    return result
コード例 #9
0
def dot_product_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    res = Vec(M.D[1], {})
    dct = matutil.mat2coldict(M)
    for k,n in dct.items():
        res[k] = n * v
    return res
コード例 #10
0
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    res = Vec(M.D[0], {})
    dct = matutil.mat2coldict(M)
    for k in v.D:
        res = res + v[k]*dct[k]
    return res
コード例 #11
0
def lin_comb_mat_vec_mult(M, v):
    assert M.D[1] == v.D
    from matutil import mat2coldict

    m = mat2coldict(M)
    y = sum([v[c] * m[c] for c in m.D[1]])
    return y
コード例 #12
0
def QR_solve(A):
    col_labels = sorted(A.D[1], key=repr)
    Acolms = dict2list(mat2coldict(A), col_labels)
    (Qlist, Rlist) = aug_orthogonalize(Acolms)
    Q = coldict2mat(list2dict(Qlist, col_labels))
    R = coldict2mat(list2dict(Rlist, col_labels))
    return Q, R
コード例 #13
0
ファイル: hw3.py プロジェクト: srikanth235/Matrix
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    cols = mat2coldict(B)
    rcols = {}
    for index, col in cols.items():
        rcols[index] = A * col
    return coldict2mat(rcols)
コード例 #14
0
ファイル: hw3.py プロジェクト: skreynolds/coding_the_matrix
def dot_prod_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    from matutil import mat2rowdict
    from matutil import mat2coldict
    return Mat(
        (A.D[0], B.D[1]), {(i, j): mat2rowdict(A)[i] * mat2coldict(B)[j]
                           for i in A.D[0] for j in B.D[1]})
コード例 #15
0
def is_invertible(M):
    '''
    input: A matrix, M
    outpit: A boolean indicating if M is invertible.
    
    >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1,
    (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2,
    (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3,
    (1, 0): 0, (0, 3): 0})
    >>> is_invertible(M)
    True
    '''
    R = []
    C = []
    R_dict = mat2rowdict(M)
    C_dict = mat2coldict(M)
    for k, v in R_dict.items():
        R.append(v)
    for k, v in C_dict.items():
        C.append(v)

    if rank(R) != rank(C):
        return False
    elif my_is_independent(R) == False:
        return False
    elif my_is_independent(C) == False:
        return False
    return True
コード例 #16
0
ファイル: hw3.py プロジェクト: tri2sing/CodingTheMatrix
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
#    print (str(A.D) + str(A.f))
#    print (str(B.D) + str(B.f))
#    print('\n')
    colsB = utils.mat2coldict(B)
    return utils.coldict2mat({k:A*colsB[k] for k in colsB})
コード例 #17
0
def lin_comb_mat_vec_mult(M, v):
    '''
    Input:
      -M: a matrix
      -v: a vector
    Output: M*v
    The following doctests are not comprehensive; they don't test the
    main question, which is whether the procedure uses the appropriate
    linear-combination definition of matrix-vector multiplication.
    Examples:
    >>> M=Mat(({'a','b'},{'A','B'}), {('a','A'):7, ('a','B'):1, ('b','A'):-5, ('b','B'):2})
    >>> v=Vec({'A','B'},{'A':4, 'B':2})
    >>> lin_comb_mat_vec_mult(M,v) == Vec({'a', 'b'},{'a': 30, 'b': -16})
    True
    >>> M1=Mat(({'a','b'},{'A','B'}), {('a','A'):8, ('a','B'):2, ('b','A'):-2, ('b','B'):1})
    >>> v1=Vec({'A','B'},{'A':4,'B':3})
    >>> lin_comb_mat_vec_mult(M1,v1) == Vec({'a', 'b'},{'a': 38, 'b': -5})
    True
    '''
    assert (M.D[1] == v.D)

    # represents as a linear combination
    cols = mat2coldict(M)
    comb = [(v[i], cols[i]) for i in v.D]

    # sums up into a vector
    return sum(coef * col for coef, col in comb)
コード例 #18
0
def mat_move2board(Y):
    '''
    Input:
        - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec
          giving the whiteboard coordinates of a point q.
    Output:
        - a Mat each column of which is the corresponding point in the
          whiteboard plane (the point of intersection with the whiteboard plane 
          of the line through the origin and q).

    Example:
    >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}),
    ...     {('y1',0):2, ('y2',0):4, ('y3',0):8,
    ...      ('y1',1):10, ('y2',1):5, ('y3',1):5,
    ...      ('y1',2):4, ('y2',2):25, ('y3',2):2,
    ...      ('y1',3):5, ('y2',3):10, ('y3',3):4})
    >>> print(Y_in)
    <BLANKLINE>
            0  1  2  3
          ------------
     y1  |  2 10  4  5
     y2  |  4  5 25 10
     y3  |  8  5  2  4
    <BLANKLINE>
    >>> print(mat_move2board(Y_in))
    <BLANKLINE>
               0 1    2    3
          ------------------
     y1  |  0.25 2    2 1.25
     y2  |   0.5 1 12.5  2.5
     y3  |     1 1    1    1
    <BLANKLINE>
    '''
    coldict = mat2coldict(Y)
    return coldict2mat({key:move2board(val) for key, val in coldict.items()})
コード例 #19
0
ファイル: hw3.py プロジェクト: peachyo/codingthematrix
def lin_comb_mat_vec_mult(M, v):
    assert M.D[1] == v.D
    import matutil
    from matutil import mat2coldict

    mat2col = mat2coldict(M)
    return sum([getitem(v, key) * mat2col[key] for key in v.D])
コード例 #20
0
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    col_dict = mat2coldict(B)
    res = dict()
    for c in col_dict.keys():
        res[c] = A * col_dict[c]
    return coldict2mat(res)
コード例 #21
0
ファイル: hw5.py プロジェクト: buptdjd/linearAlgebra-coursera
def is_invertible(M): 
    '''
    input: A matrix, M
    outpit: A boolean indicating if M is invertible.
    
    >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
    >>> is_invertible(M)
    True
    '''
    nullspace = solve(M, list2vec([0]))
    ker_zero = nullspace == Vec(nullspace.D, {})

    im_f_eq_codomain = independence.rank(subset_basis(matutil.mat2coldict(M).values())) == len(M.D[1])

    # invertible if its one-to-one, onto, and square matrix
    return ker_zero and im_f_eq_codomain and len(matutil.mat2coldict(M)) == len(matutil.mat2rowdict(M))
コード例 #22
0
ファイル: hw3.py プロジェクト: kiori/coding_the_matrix
def dot_prod_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    import vec, matutil
    A_dict = matutil.mat2rowdict(A)
    B_dict = matutil.mat2coldict(B)
    dp = {(i,j): A_dict[i]*B_dict[j] for j in B_dict.keys() for i in A_dict.keys()}
    return Mat((set(A_dict.keys()), set(B_dict.keys())), dp)
コード例 #23
0
def mat_move2board(Y):
    '''
    Input:
        - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec
          giving the whiteboard coordinates of a point q.
    Output:
        - a Mat each column of which is the corresponding point in the
          whiteboard plane (the point of intersection with the whiteboard plane 
          of the line through the origin and q).

    Example:
    >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}),
    ...     {('y1',0):2, ('y2',0):4, ('y3',0):8,
    ...      ('y1',1):10, ('y2',1):5, ('y3',1):5,
    ...      ('y1',2):4, ('y2',2):25, ('y3',2):2,
    ...      ('y1',3):5, ('y2',3):10, ('y3',3):4})
    >>> print(Y_in)
    <BLANKLINE>
            0  1  2  3
          ------------
     y1  |  2 10  4  5
     y2  |  4  5 25 10
     y3  |  8  5  2  4
    <BLANKLINE>
    >>> print(mat_move2board(Y_in))
    <BLANKLINE>
               0 1    2    3
          ------------------
     y1  |  0.25 2    2 1.25
     y2  |   0.5 1 12.5  2.5
     y3  |     1 1    1    1
    <BLANKLINE>
    '''
    return coldict2mat({c: move2board(r) for c, r in mat2coldict(Y).items()})
コード例 #24
0
ファイル: mat.py プロジェクト: vellvisher/matrix
def matrix_matrix_mul(A, B):
    "Returns the product of A and B"
    assert A.D[1] == B.D[0]
    from matutil import mat2coldict, mat2rowdict
    return Mat(
        (A.D[0], B.D[1]), {(r, c): mat2rowdict(A)[r] * mat2coldict(B)[c]
                           for r in A.D[0] for c in B.D[1]})
コード例 #25
0
ファイル: hw3.py プロジェクト: zezei/linearAlgebra-coursera
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)


    # vec_list = []
    #
    #
    # col_dict = matutil.mat2coldict(M)
    # for key, val in col_dict.items():
    #     vec_list.append(mult_vec_by_int(val, v[key]))
    #
    # new_v = Vec(M.D[0], {})
    #
    # for vec in vec_list:
    #     new_v += vec
    #
    # return new_v

    new_v = Vec(M.D[0], {})

    col_dict = matutil.mat2coldict(M)

    for key, val in col_dict.items():
        new_v += v[key] * val

    return new_v
コード例 #26
0
ファイル: perspective_lab.py プロジェクト: omrigildor/cs53
def mat_move2board(Y):
    '''
    Input:
        - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec
          giving the whiteboard coordinates of a point q.
    Output:
        - a Mat each column of which is the corresponding point in the
          whiteboard plane (the point of intersection with the whiteboard plane 
          of the line through the origin and q).

    Example:
    >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}),
         {('y1',0):2, ('y2',0):4, ('y3',0):8,
          ('y1',1):10, ('y2',1):5, ('y3',1):5,
          ('y1',2):4, ('y2',2):25, ('y3',2):2,
          ('y1',3):5, ('y2',3):10, ('y3',3):4})
    >>> print(Y_in)
    
            0  1  2  3
          ------------
     y1  |  2 10  4  5
     y2  |  4  5 25 10
     y3  |  8  5  2  4
    
    >>> print(mat_move2board(Y_in))
    
               0 1    2    3
          ------------------
     y1  |  0.25 2    2 1.25
     y2  |   0.5 1 12.5  2.5
     y3  |     1 1    1    1
    '''
    y_in = mat2coldict(Y)
    y_out = {x:move2board(y_in[x]) for x in y_in.keys()}
    return coldict2mat(y_out)
コード例 #27
0
ファイル: perspective_lab.py プロジェクト: omrigildor/cs53
def mat_move2board(Y):
    '''
    Input:
        - Y: a Mat each column of which is a {'y1', 'y2', 'y3'}-Vec
          giving the whiteboard coordinates of a point q.
    Output:
        - a Mat each column of which is the corresponding point in the
          whiteboard plane (the point of intersection with the whiteboard plane 
          of the line through the origin and q).

    Example:
    >>> Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}),
         {('y1',0):2, ('y2',0):4, ('y3',0):8,
          ('y1',1):10, ('y2',1):5, ('y3',1):5,
          ('y1',2):4, ('y2',2):25, ('y3',2):2,
          ('y1',3):5, ('y2',3):10, ('y3',3):4})
    >>> print(Y_in)
    
            0  1  2  3
          ------------
     y1  |  2 10  4  5
     y2  |  4  5 25 10
     y3  |  8  5  2  4
    
    >>> print(mat_move2board(Y_in))
    
               0 1    2    3
          ------------------
     y1  |  0.25 2    2 1.25
     y2  |   0.5 1 12.5  2.5
     y3  |     1 1    1    1
    '''
    y_in = mat2coldict(Y)
    y_out = {x: move2board(y_in[x]) for x in y_in.keys()}
    return coldict2mat(y_out)
コード例 #28
0
ファイル: hw5.py プロジェクト: df1111/CodingTheMatrix
def is_invertible(M): 
    '''
    input: A matrix, M
    outpit: A boolean indicating if M is invertible.
    
    >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1,
    (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2,
    (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3,
    (1, 0): 0, (0, 3): 0})
    >>> is_invertible(M)
    True
    '''
    R=[]
    C=[]
    R_dict = mat2rowdict(M)
    C_dict = mat2coldict(M)
    for k,v in R_dict.items():
        R.append(v)
    for k,v in C_dict.items():
        C.append(v)
        
    if rank(R) != rank(C):
        return False
    elif my_is_independent(R) == False:
        return False
    elif my_is_independent(C) == False:
        return False
    return True
コード例 #29
0
def lin_comb_mat_vec_mult(M, v):
    '''
    Input:
      -M: a matrix
      -v: a vector
    Output: M*v
    The following doctests are not comprehensive; they don't test the
    main question, which is whether the procedure uses the appropriate
    linear-combination definition of matrix-vector multiplication. 
    Examples:
    >>> M=Mat(({'a','b'},{0,1}), {('a',0):7, ('a',1):1, ('b',0):-5, ('b',1):2})
    >>> v=Vec({0,1},{0:4, 1:2})
    >>> lin_comb_mat_vec_mult(M,v) == Vec({'a', 'b'},{'a': 30, 'b': -16})
    True
    >>> M1=Mat(({'a','b'},{0,1}), {('a',0):8, ('a',1):2, ('b',0):-2, ('b',1):1})
    >>> v1=Vec({0,1},{0:4,1:3})
    >>> lin_comb_mat_vec_mult(M1,v1) == Vec({'a', 'b'},{'a': 38, 'b': -5})
    True
    '''
    assert (M.D[1] == v.D)
    cols = matutil.mat2coldict(M)
    result = Vec(M.D[0], {})
    for j in M.D[1]:
        result += v[j] * cols[j]

    return result
コード例 #30
0
ファイル: hw3.py プロジェクト: fandres70/matrix
def dot_prod_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    from matutil import mat2rowdict, mat2coldict
    rowsA = mat2rowdict(A)
    colsB = mat2coldict(B)
    rset = A.D[0]
    cset = B.D[1]
    return Mat((rset, cset), {(i,j):rowsA[i]*colsB[j] for i in rset for j in cset})
コード例 #31
0
ファイル: hw3.py プロジェクト: vineetyadav/coding_the_matrix
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    cols = mat2coldict(M)
    new_dict= Vec(M.D[0],{})
    for k,col_vec in cols.items():
        tmp = v[k] * col_vec
        new_dict = new_dict + tmp
    return new_dict
コード例 #32
0
def matrix_matrix_mul(A, B):
    "Returns the product of A and B"
    assert A.D[1] == B.D[0]
    from matutil import mat2coldict
    from matutil import mat2rowdict
    rows = mat2rowdict(A)
    columns = mat2coldict(B)
    return Mat((A.D[0],B.D[1]), { (i,j):rows[i]*columns[j] for i in rows for j in columns })
コード例 #33
0
ファイル: hw3.py プロジェクト: Shubashree/matrix_coding
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    from matutil import mat2coldict
    from matutil import coldict2mat
    columns = mat2coldict(B)
    for column,colVec in columns.items():
    	columns[column] = A*colVec
    return coldict2mat(columns)
コード例 #34
0
def QR_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
コード例 #35
0
ファイル: hw3.py プロジェクト: niujie/CMLACSA
def dot_product_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    output = Vec(M.D[1], {})
    from matutil import mat2coldict
    M_col = mat2coldict(M)
    for i in M.D[1]:
        output[i] = output[i] + v*M_col[i]
    return output
コード例 #36
0
def QR_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
コード例 #37
0
ファイル: hw3.py プロジェクト: murphytalk/math
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    cols = mat2coldict(M)
    s = Vec(M.D[0],{})
    for k,vec in cols.items():
        s=s+v[k]*vec
        
    return s
コード例 #38
0
ファイル: hw3.py プロジェクト: peachyo/codingthematrix
def dot_prod_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    import matutil
    from matutil import mat2coldict
    from matutil import coldict2mat

    mat2col_B = mat2coldict(B)
    return coldict2mat({key: dot_product_mat_vec_mult(A, mat2col_B[key]) for key in B.D[1]})
コード例 #39
0
ファイル: hw3.py プロジェクト: peachyo/codingthematrix
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    import matutil
    from matutil import mat2coldict
    from matutil import coldict2mat

    mat2col_B = mat2coldict(B)
    return coldict2mat({key: A * v for (key, v) in zip(mat2col_B.keys(), mat2col_B.values())})
コード例 #40
0
ファイル: hw3.py プロジェクト: peachyo/codingthematrix
def dot_product_vec_mat_mult(v, M):

    assert v.D == M.D[0]
    import matutil
    from matutil import mat2coldict

    mat2col = mat2coldict(M)
    return Vec(M.D[1], {key: v * u for (key, u) in zip(mat2col.keys(), mat2col.values())})
コード例 #41
0
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    dct = matutil.mat2coldict(B)
    #return (matutil.coldict2mat(dict([ (c , lin_comb_mat_vec_mult(A, dct[c]))  for c in dct])))
    v = Vec(dct.keys(), dict())
    for c in dct:
        print(A * dct[c])
        v[c] = A * dct[c]
コード例 #42
0
def mat_move2board(Y):
    '''
    Input:
        - Y: Mat instance, each column of which is a 'y1', 'y2', 'y3' vector 
          giving the whiteboard coordinates of a point q.
    Output:
        - Mat instance, each column of which is the corresponding point in the
          whiteboard plane (the point of intersection with the whiteboard plane 
          of the line through the origin and q).
    '''
    col_dict = matutil.mat2coldict(Y)
    new_col_dic = {}

    for key, val in col_dict.items():
        new_col_dic[key] = Vec(val.D,
                               {k: v / val.f['y3']
                                for k, v in val.f.items()})

    return matutil.coldict2mat(new_col_dic)


# import perspective_lab
# from mat import Mat
# import vecutil
# import matutil
# import image_mat_util
# from vec import Vec
# from GF2 import one
# from solver import solve
# row_dict = {}
# row_dict[0] = perspective_lab.make_equations(358, 36, 0, 0)[0]
# row_dict[1] = perspective_lab.make_equations(358, 36, 0, 0)[1]
# row_dict[2] = perspective_lab.make_equations(329, 597, 0, 1)[0]
# row_dict[3] = perspective_lab.make_equations(329, 597, 0, 1)[1]
# row_dict[4] = perspective_lab.make_equations(592, 157, 1, 0)[0]
# row_dict[5] = perspective_lab.make_equations(592, 157, 1, 0)[1]
# row_dict[6] = perspective_lab.make_equations(580, 483, 1, 1)[0]
# row_dict[7] = perspective_lab.make_equations(580, 483, 1, 1)[1]
# foo = perspective_lab.make_equations(0, 0, 0, 0)[0]
# foo[('y1', 'x1')] = 1
# foo[('y1', 'x3')] = 0
# row_dict[8] = foo
# M = matutil.rowdict2mat(row_dict)
# print(M)
# solve(M, vecutil.list2vec([0, 0, 0, 0, 0, 0, 0, 0, 1]))
# Y_in = Mat(({'y1', 'y2', 'y3'}, {0,1,2,3}),
#            {('y1',0):2, ('y2',0):4, ('y3',0):8,
#             ('y1',1):10, ('y2',1):5, ('y3',1):5,
#             ('y1',2):4, ('y2',2):25, ('y3',2):2,
#             ('y1',3):5, ('y2',3):10, ('y3',3):4})
# print(Y_in)
# print(perspective_lab.mat_move2board(Y_in))
# (X_pts, colors) = image_mat_util.file2mat('board.png', ('x1','x2','x3'))
# H = Mat(({'y1', 'y3', 'y2'}, {'x2', 'x3', 'x1'}), {('y3', 'x1'): -0.7219356810710031, ('y2', 'x1'): -0.3815213180054361, ('y2', 'x2'): 0.7378180860600992, ('y1', 'x1'): 1.0, ('y2', 'x3'): 110.0231807477826, ('y3', 'x3'): 669.4762699006177, ('y1', 'x3'): -359.86096256684493, ('y3', 'x2'): -0.011690730864965311, ('y1', 'x2'): 0.05169340463458105})
# Y_pts = H * X_pts
# Y_board = perspective_lab.mat_move2board(Y_pts)
# image_mat_util.mat2display(Y_board, colors, ('y1', 'y2', 'y3'),
#                            scale=100, xmin=None, ymin=None)
コード例 #43
0
ファイル: hw3.py プロジェクト: fandres70/matrix
def dot_prod_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    from matutil import mat2rowdict, mat2coldict
    rowsA = mat2rowdict(A)
    colsB = mat2coldict(B)
    rset = A.D[0]
    cset = B.D[1]
    return Mat((rset, cset), {(i, j): rowsA[i] * colsB[j]
                              for i in rset for j in cset})
コード例 #44
0
ファイル: hw5.py プロジェクト: zezei/linearAlgebra-coursera
def is_invertible(M):
    '''
    input: A matrix, M
    outpit: A boolean indicating if M is invertible.
    
    >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
    >>> is_invertible(M)
    True
    '''
    nullspace = solve(M, list2vec([0]))
    ker_zero = nullspace == Vec(nullspace.D, {})

    im_f_eq_codomain = independence.rank(
        subset_basis(matutil.mat2coldict(M).values())) == len(M.D[1])

    # invertible if its one-to-one, onto, and square matrix
    return ker_zero and im_f_eq_codomain and len(
        matutil.mat2coldict(M)) == len(matutil.mat2rowdict(M))
コード例 #45
0
ファイル: hw3.py プロジェクト: niujie/CMLACSA
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    output = {}
    from matutil import mat2coldict
    from matutil import coldict2mat
    B_col = mat2coldict(B)
    for i in B.D[1]:
        output[i] = A * B_col[i]
    return coldict2mat(output)
コード例 #46
0
ファイル: mat.py プロジェクト: peachyo/codingthematrix
def matrix_matrix_mul(A, B):
    "Returns the product of A and B"
    assert A.D[1] == B.D[0]
    import matutil
    from matutil import mat2coldict
    from matutil import coldict2mat

    mat2col_B = mat2coldict(B)
    return coldict2mat({key: matrix_vector_mul(A, mat2col_B[key]) for key in B.D[1]})
コード例 #47
0
ファイル: hw3.py プロジェクト: Shubashree/matrix_coding
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    from matutil import mat2coldict
    columns = mat2coldict(M)
    vector = Vec(M.D[0], {c:0 for c in M.D[0]})
    for c, column in columns.items():
    	altered = v[c] * column
    	vector = vector + altered
    return vector
コード例 #48
0
ファイル: mat.py プロジェクト: peachyo/codingthematrix
def vector_matrix_mul(v, M):
    "Returns the product of vector v and matrix M"
    assert M.D[0] == v.D
    import matutil
    from matutil import mat2coldict
    from vec import dot

    mat2col = mat2coldict(M)
    return Vec(M.D[1], {key: dot(v, mat2col[key]) for key in M.D[1]})
コード例 #49
0
def is_invertible(M):
	L = mat2coldict(M)
	L = list(L.values())
	cols = len(L)
	rows = len(L[0].D)
	if cols == rows and my_is_independent(L):
		return True
	else:
		return False
コード例 #50
0
def lin_comb_mat_vec_mult(M, v):
    assert (M.D[1] == v.D)

    cols = mat2coldict(M)
    new_dict = Vec(M.D[0], {k: 0 for k in M.D[0]})
    for k, col_vec in cols.items():
        tmp = v[k] * col_vec
        new_dict = new_dict + tmp
    return new_dict
コード例 #51
0
def is_invertible(M):
    '''
input: A matrix, M
outpit: A boolean indicating if M is invertible.
>>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
>>> is_invertible(M)
True
'''
    L = mat2coldict(M).values()
    return True if ( my_is_independent(L) and ( len(M.D[0]) == len(M.D[1]) ) ) else False
コード例 #52
0
def is_invertible(M): 
    '''
    input: A matrix, M
    outpit: A boolean indicating if M is invertible.
    
    >>> M = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 0, (1, 2): 1, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 1, (2, 0): 0, (1, 3): 0, (2, 3): 1, (2, 2): 3, (1, 0): 0, (0, 3): 0})
    >>> is_invertible(M)
    True
    '''
    return len(M.D[0]) == len(M.D[1]) and is_independent([v for k,v in mat2coldict(M).items() ])
コード例 #53
0
ファイル: mat.py プロジェクト: Shubashree/matrix_coding
def matrix_vector_mul(M, v):
    "Returns the product of matrix M and vector v"
    assert M.D[1] == v.D
    from matutil import mat2coldict
    columns = mat2coldict(M)
    vector = Vec(M.D[0], {c: 0 for c in M.D[0]})
    for column, colVec in columns.items():
        mult = v[column] * colVec
        vector = vector + mult
    return vector
コード例 #54
0
ファイル: hw3.py プロジェクト: zezei/linearAlgebra-coursera
def Mv_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]

    col_dict = matutil.mat2coldict(B)

    foo_dict = {}
    for key, val in col_dict.items():
        foo_dict[key] = A * val

    return matutil.coldict2mat(foo_dict)