Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
0
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
		'''
		(v,k)=(list(mat2rowdict(A).values()),list(mat2rowdict(A).keys()))
		return coldict2mat([triangular_solve(v,k,Vec(A.D[1], {e:1})) for e in A.D[1]])
Beispiel #4
0
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)
        },
    )
Beispiel #5
0
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
		'''
    (v, k) = (list(mat2rowdict(A).values()), list(mat2rowdict(A).keys()))
    return coldict2mat(
        [triangular_solve(v, k, Vec(A.D[1], {e: 1})) for e in A.D[1]])
Beispiel #6
0
def find_triangular_matrix_inverse(A):
	L=[]
	label_list = []
	I = identity(A.D[0],1)
	R = mat2rowdict(A)
	w = mat2rowdict(I)
	label_list.extend(range(len(R)))
	for k,v in w.items():
		print(k,v,)
		s = triangular_solve(R, label_list, v)
		L.append(s)
	return coldict2mat(L)
Beispiel #7
0
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
    '''
    solution = identity(A.D[0],1)
    solutionrowdict = mat2rowdict(solution)
    Arowveclist = list(mat2rowdict(A).values())
    label_list = list(range(len(Arowveclist)))
    return coldict2mat({key:triangular_solve(Arowveclist, label_list, vec) for key,vec in solutionrowdict.items()})
Beispiel #8
0
def basis_AT(A):
    basis = []
    M = transformation(A)
    U = M * A
    M_rowdict = mat2rowdict(M)
    U_rowdict = mat2rowdict(U)
    M_rowlist = [M_rowdict[i] for i in sorted(M_rowdict)]
    U_rowlist = [U_rowdict[i] for i in sorted(U_rowdict)]
    zero_vector = zero_vec(U_rowlist[0].D)
    for i in range(len(U_rowlist)):
        if (U_rowlist[i] == zero_vector):
            basis.append(M_rowlist[i])
    return basis
Beispiel #9
0
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
    '''
    cols = []
    i = identity(A.D[0], 1)
    i_cols = mat2coldict(i)
    for k in A.D[0]:
        cols.append( triangular_solve([ mat2rowdict(A)[k] for k in mat2rowdict(A) ], list(range(len(A.D[0]))), i_cols[k]) )
    return coldict2mat(cols)
Beispiel #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
    '''
    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
Beispiel #11
0
def lin_comb_vec_mat_mult(v, M):
    assert v.D == M.D[0]
    result = Vec(M.D[1], {r: 0 for r in M.D[1]})
    rows = mat2rowdict(M)
    for k, value in v.f.items():
        result = result + value * rows[k]
    return result
Beispiel #12
0
def dot_product_mat_vec_mult(M, v):
    assert M.D[1] == v.D
    result = Vec(M.D[0], {c: 0 for c in M.D[0]})
    rows = mat2rowdict(M)
    for k in M.D[0]:
        result.f[k] = rows[k] * v
    return result
Beispiel #13
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    res = Vec(M.D[1], {})
    dct = matutil.mat2rowdict(M)
    for k in v.D:
        res = res + v[k]*dct[k]
    return res
Beispiel #14
0
def dot_product_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    res = Vec(M.D[0], {})
    dct = matutil.mat2rowdict(M)
    for k,n in dct.items():
        res[k] = n * v
    return res
Beispiel #15
0
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)
Beispiel #16
0
def lin_comb_vec_mat_mult(v, M):
    '''
    Input:
      -v: a vector
      -M: a matrix
    Output: v*M
    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 vector-matrix 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':2, 'b':-1})
      >>> lin_comb_vec_mat_mult(v,M) == Vec({'A', 'B'},{'A': 19, 'B': 0})
      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_vec_mat_mult(v1,M1) == Vec({'A', 'B'},{'A': 26, 'B': 11})
      True
    '''
    assert (v.D == M.D[0])

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

    # sums up into a vector
    return sum(coef * row for coef, row in comb)
def vM_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    from matutil import mat2rowdict, rowdict2mat

    a = mat2rowdict(A)
    m = rowdict2mat({k: v * B for (k, v) in a.items()})
    return m
Beispiel #18
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
Beispiel #19
0
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
    """
    I_col_dict = {i: Vec(A.D[0], {i: 1}) for i in A.D[1]}
    # def triangular_solve(rowlist, label_list, b):
    rowlist = list()
    label_list = list()
    from matutil import mat2rowdict

    rowdict = mat2rowdict(A)
    for key in rowdict.keys():
        label_list.append(key)
        rowlist.append(rowdict[key])

    B_coldict = dict()
    from triangular import triangular_solve

    for key in label_list:
        B_coldict[key] = triangular_solve(rowlist, label_list, I_col_dict[key])
    return coldict2mat(B_coldict)
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)
Beispiel #21
0
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
    """
    cd = list()
    label_list = list(mat2rowdict(A).keys())
    rowlist = list(mat2rowdict(A).values())
    for j in label_list:
        c = triangular_solve(rowlist, label_list, list2vec([1 if i == j else 0 for i in label_list]))
        cd.append(c)

    return coldict2mat(cd)
Beispiel #22
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
Beispiel #23
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
Beispiel #24
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 = Q.transpose() * b
    rowlist = mat2rowdict(R)
    labelslist = sorted(A.D[1], key=repr)
    return triangular_solve(rowlist, labelslist, c)
def lin_comb_vec_mat_mult(v, M):
    '''
    Input:
      -v: a vector
      -M: a matrix
    Output: v*M
    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 vector-matrix multiplication. 
    Examples:
      >>> M=Mat(({'a','b'},{0,1}), {('a',0):7, ('a',1):1, ('b',0):-5, ('b',1):2})
      >>> v=Vec({'a','b'},{'a':2, 'b':-1})
      >>> lin_comb_vec_mat_mult(v,M) == Vec({0, 1},{0: 19, 1: 0})
      True
      >>> M1=Mat(({'a','b'},{0,1}), {('a',0):8, ('a',1):2, ('b',0):-2, ('b',1):1})
      >>> v1=Vec({'a','b'},{'a':4,'b':3})
      >>> lin_comb_vec_mat_mult(v1,M1) == Vec({0, 1},{0: 26, 1: 11})
      True
    '''
    assert (v.D == M.D[0])
    rows = matutil.mat2rowdict(M)
    result = Vec(M.D[1], {})
    for j in M.D[0]:
        result += v[j] * rows[j]

    return result
Beispiel #26
0
def lin_comb_vec_mat_mult(v, M):
    assert v.D == M.D[0]
    import matutil
    from matutil import mat2rowdict

    mat2row = mat2rowdict(M)
    return sum([getitem(v, key) * mat2row[key] for key in v.D])
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)
    R_rows = mat2rowdict(R)
    R_rows = [R_rows[r] for r in
              sorted(R_rows.keys(), key=repr)]
    return triangular_solve(R_rows,
                            sorted(A.D[1],key=repr),
                            Q.transpose()*b)
def least_squares(Q, R, b):
    R_rows = mat2rowdict(R)
    R_rows = [R_rows[r] for r in
              sorted(R_rows.keys(), key=repr)]
    return triangular_solve(R_rows,
                            sorted(R.D[1],key=repr),
                            Q.transpose()*b)
Beispiel #29
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    vc= matutil.mat2rowdict(M)
    nvc= {c: v[c] * vc[c] for c in v.D}
    snvc= {r: sum([nvc[c][r] for c in v.D]) for r in M.D[1]}
    return Vec(set(snvc.keys()), snvc)
    pass
Beispiel #30
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
Beispiel #31
0
def solve2(A, b):
    M = transformation(A)
    U = M * A
    U_rowdict = mat2rowdict(U)
    rowlist = [U_rowdict[i] for i in sorted(U_rowdict)]
    label_list = (A.D[1])
    return echelon_solve(rowlist, label_list, M * b)
Beispiel #32
0
def find_triangular_matrix_inverse(A):
    '''
    Supporting GF2 is not required.

    Input:
        - A: an upper triangular Mat with nonzero diagonal elements
    Output:
        - Mat that is the inverse of A
    
    Example:
        >>> 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
    '''
    I = {i: Vec(A.D[0], {i: 1}) for i in A.D[1]}
    rl = list()
    ll = list()
    cd = dict()
    rd = mat2rowdict(A)
    for k, i in rd.items():
        ll.append(k)
        rl.append(rd[k])
    for k in ll:
        cd[k] = triangular_solve(rl, ll, I[k])
    return coldict2mat(cd)
Beispiel #33
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    rows = mat2rowdict(M)
    s = Vec(M.D[1],{})
    for k,vec in rows.items():
        s=s+v[k]*vec
    return s
Beispiel #34
0
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]})
Beispiel #35
0
def find_triangular_matrix_inverse(A):
    '''
    Supporting GF2 is not required.

    Input:
        - A: an upper triangular Mat with nonzero diagonal elements
    Output:
        - Mat that is the inverse of A
    
    Example:
        >>> 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
    '''
    I = {i:Vec(A.D[0],{i:1}) for i in A.D[1]}
    rl = list()
    ll = list()
    cd = dict()
    rd = mat2rowdict(A)
    for k,i in rd.items():
        ll.append(k)
        rl.append(rd[k])
    for k in ll:
        cd[k]= triangular_solve(rl,ll,I[k])
    return coldict2mat(cd)
Beispiel #36
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, 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]})
def vM_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    row_dict = mat2rowdict(A)
    res = dict()
    for r in row_dict.keys():
        res[r] = row_dict[r] * B
    return rowdict2mat(res)
Beispiel #38
0
def lin_comb_vec_mat_mult(v, M):
    assert (v.D == M.D[0])
    cols = mat2rowdict(M)
    new_dict = Vec(M.D[1], {k: 0 for k in M.D[1]})
    for k, col_vec in cols.items():
        tmp = v[k] * col_vec
        new_dict = new_dict + tmp
    return new_dict
Beispiel #39
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 })
Beispiel #40
0
def vM_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    import matutil
    from matutil import mat2rowdict
    from matutil import rowdict2mat

    mat2row_A = mat2rowdict(A)
    return rowdict2mat({key: v * B for (key, v) in zip(mat2row_A.keys(), mat2row_A.values())})
Beispiel #41
0
def vM_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    from matutil import mat2rowdict
    from matutil import rowdict2mat
    rows = mat2rowdict(A)
    for row,rowVec in rows.items():
    	rows[row] = rowVec*B
    return rowdict2mat(rows)
Beispiel #42
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    cols = mat2rowdict(M)
    new_dict = Vec(M.D[1],{k:0 for k in M.D[1]})
    for k,col_vec in cols.items():
	    tmp = v[k] * col_vec
	    new_dict = new_dict + tmp
    return new_dict
Beispiel #43
0
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})
Beispiel #44
0
def dot_product_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    output = Vec(M.D[0], {})
    from matutil import mat2rowdict
    M_row = mat2rowdict(M)
    for i in M.D[0]:
        output[i] = output[i] + M_row[i]*v
    return output
Beispiel #45
0
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
    '''

    R = len(A.D[0])
    C = len(A.D[1])
    coldict = {}
    for i in range(C):
        b = [0] * R
        b[i] = 1
        s = triangular_solve(list(matutil.mat2rowdict(A).values()), list(A.D[0]), list2vec(b))
        coldict[i] = s

    return coldict2mat(coldict)



# import hw5
# import hw4
# from mat import Mat
# import vecutil
# import matutil
# from vec import Vec
# from GF2 import one
# L = [Vec({0, 1, 2},{0: 1, 1: 0, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 1})]
# hw5.my_is_independent(L)
# hw5.my_is_independent(L[:2])
# S = [vecutil.list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
# B = [vecutil.list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
# ff = hw5.morph(S, B)
# ff == [(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))]
# a0 = Vec({'a','b','c','d'}, {'a':1})
# a1 = Vec({'a','b','c','d'}, {'b':1})
# a2 = Vec({'a','b','c','d'}, {'c':1})
# a3 = Vec({'a','b','c','d'}, {'a':1,'c':3})
# sb = hw5.subset_basis([a0,a1,a2,a3])
# sb == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})]
# U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
# V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
# w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
# ss = hw5.direct_sum_decompose(U_basis, V_basis, w)
# ss == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0}))
# 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})
# hw5.is_invertible(M)
# A = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 2, (1, 2): 1, (0, 0): 1, (1, 0): 3, (0, 2): 3, (1, 1): 1})
# B = 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})
# C = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 0, (2, 0): 2, (0, 0): 1, (1, 0): 0, (1, 1): 1, (2, 1): 1})
# D = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 5, (1, 2): 2, (0, 0): 1, (2, 0): 4, (1, 0): 2, (2, 2): 7, (0, 2): 8, (2, 1): 6, (1, 1): 5})
# E = Mat(({0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}), {(1, 2): 7, (3, 2): 7, (0, 0): 3, (3, 0): 1, (0, 4): 3, (1, 4): 2, (1, 3): 4, (2, 3): 0, (2, 1): 56, (2, 4): 5, (4, 2): 6, (1, 0): 2, (0, 3): 7, (4, 0): 2, (0, 1): 5, (3, 3): 4, (4, 1): 4, (3, 1): 23, (4, 4): 5, (0, 2): 7, (2, 0): 2, (4, 3): 8, (2, 2): 9, (3, 4): 2, (1, 1): 4})
# M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0})
# hw5.find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0})
# A = matutil.listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
# hw5.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})
Beispiel #46
0
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
    '''

    R = len(A.D[0])
    C = len(A.D[1])
    coldict = {}
    for i in range(C):
        b = [0] * R
        b[i] = 1
        s = triangular_solve(list(matutil.mat2rowdict(A).values()),
                             list(A.D[0]), list2vec(b))
        coldict[i] = s

    return coldict2mat(coldict)


# import hw5
# import hw4
# from mat import Mat
# import vecutil
# import matutil
# from vec import Vec
# from GF2 import one
# L = [Vec({0, 1, 2},{0: 1, 1: 0, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 1})]
# hw5.my_is_independent(L)
# hw5.my_is_independent(L[:2])
# S = [vecutil.list2vec(v) for v in [[1,0,0],[0,1,0],[0,0,1]]]
# B = [vecutil.list2vec(v) for v in [[1,1,0],[0,1,1],[1,0,1]]]
# ff = hw5.morph(S, B)
# ff == [(Vec({0, 1, 2},{0: 1, 1: 1, 2: 0}), Vec({0, 1, 2},{0: 1, 1: 0, 2: 0})), (Vec({0, 1, 2},{0: 0, 1: 1, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 1, 2: 0})), (Vec({0, 1, 2},{0: 1, 1: 0, 2: 1}), Vec({0, 1, 2},{0: 0, 1: 0, 2: 1}))]
# a0 = Vec({'a','b','c','d'}, {'a':1})
# a1 = Vec({'a','b','c','d'}, {'b':1})
# a2 = Vec({'a','b','c','d'}, {'c':1})
# a3 = Vec({'a','b','c','d'}, {'a':1,'c':3})
# sb = hw5.subset_basis([a0,a1,a2,a3])
# sb == [Vec({'c', 'b', 'a', 'd'},{'a': 1}), Vec({'c', 'b', 'a', 'd'},{'b': 1}), Vec({'c', 'b', 'a', 'd'},{'c': 1})]
# U_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 1, 2: 0, 3: 0, 4: 6, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 11, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0}), Vec({0, 1, 2, 3, 4, 5},{0: 3, 1: 1.5, 2: 0, 3: 0, 4: 7.5, 5: 0})]
# V_basis = [Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 7, 3: 0, 4: 0, 5: 1}), Vec({0, 1, 2, 3, 4, 5},{0: 0, 1: 0, 2: 15, 3: 0, 4: 0, 5: 2})]
# w = Vec({0, 1, 2, 3, 4, 5},{0: 2, 1: 5, 2: 0, 3: 0, 4: 1, 5: 0})
# ss = hw5.direct_sum_decompose(U_basis, V_basis, w)
# ss == (Vec({0, 1, 2, 3, 4, 5},{0: 2.0, 1: 4.999999999999972, 2: 0.0, 3: 0.0, 4: 1.0, 5: 0.0}), Vec({0, 1, 2, 3, 4, 5},{0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0}))
# 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})
# hw5.is_invertible(M)
# A = Mat(({0, 1}, {0, 1, 2}), {(0, 1): 2, (1, 2): 1, (0, 0): 1, (1, 0): 3, (0, 2): 3, (1, 1): 1})
# B = 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})
# C = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 0, (2, 0): 2, (0, 0): 1, (1, 0): 0, (1, 1): 1, (2, 1): 1})
# D = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 5, (1, 2): 2, (0, 0): 1, (2, 0): 4, (1, 0): 2, (2, 2): 7, (0, 2): 8, (2, 1): 6, (1, 1): 5})
# E = Mat(({0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}), {(1, 2): 7, (3, 2): 7, (0, 0): 3, (3, 0): 1, (0, 4): 3, (1, 4): 2, (1, 3): 4, (2, 3): 0, (2, 1): 56, (2, 4): 5, (4, 2): 6, (1, 0): 2, (0, 3): 7, (4, 0): 2, (0, 1): 5, (3, 3): 4, (4, 1): 4, (3, 1): 23, (4, 4): 5, (0, 2): 7, (2, 0): 2, (4, 3): 8, (2, 2): 9, (3, 4): 2, (1, 1): 4})
# M = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (1, 2): 0, (0, 0): 0, (2, 0): 0, (1, 0): one, (2, 2): one, (0, 2): 0, (2, 1): 0, (1, 1): 0})
# hw5.find_matrix_inverse(M) == Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): one, (2, 0): 0, (0, 0): 0, (2, 2): one, (1, 0): one, (1, 2): 0, (1, 1): 0, (2, 1): 0, (0, 2): 0})
# A = matutil.listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
# hw5.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})
Beispiel #47
0
def dot_product_mat_vec_mult(M, v):

    assert M.D[1] == v.D
    import matutil
    from matutil import mat2rowdict
    from vec import dot

    mat2row = mat2rowdict(M)
    return Vec(M.D[0], {key: u * v for (key, u) in zip(mat2row.keys(), mat2row.values())})
Beispiel #48
0
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})
Beispiel #49
0
def matrix_vector_mul(M, v):
    "Returns the product of matrix M and vector v"
    assert M.D[1] == v.D
    import matutil
    from matutil import mat2rowdict
    from vec import dot

    mat2row = mat2rowdict(M)
    return Vec(M.D[0], {key: dot(mat2row[key], v) for key in M.D[0]})
Beispiel #50
0
def squared_Frob(A):
    '''
    Computes the square of the frobenius norm of A.

    Example:
    >>> squared_Frob(Mat(({1, 2}, {1, 2, 3, 4}), {(1, 1): 1, (1, 2): 2, (1, 3): 3, (1, 4): 4, (2, 1): -4, (2, 2): 2, (2, 3): -1}))
    51
    '''
    return sum([v*v for k,v in mat2rowdict(A).items()])
Beispiel #51
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    from matutil import mat2rowdict
    rows = mat2rowdict(M)
    vector = Vec(M.D[1], {r:0 for r in M.D[1]})
    for r, row in rows.items():
    	altered = v[r] * row
    	vector = vector + altered
    return vector
Beispiel #52
0
def vector_matrix_mul(v, M):
    "Returns the product of vector v and matrix M"
    assert M.D[0] == v.D
    from matutil import mat2rowdict
    rows = mat2rowdict(M)
    vector = Vec(M.D[1], {r: 0 for r in M.D[1]})
    for row, rowVec in rows.items():
        mult = v[row] * rowVec
        vector = vector + mult
    return vector
Beispiel #53
0
def vM_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]

    row_dict = matutil.mat2rowdict(A)

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

    return matutil.rowdict2mat(foo_dict)
Beispiel #54
0
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
    '''
    R = mat2rowdict(identity(A.D[0], 1))
    return coldict2mat([solve(A, R[i]) for i in range(len(R))])
Beispiel #55
0
def vM_mat_mat_mult(A, B):
    assert A.D[1] == B.D[0]
    v = mat2rowdict(A)
    l = []
    d = {}
    for (i, j) in v.items():
        s = j * B
        l.append(s)
        for e in l:
            d[i] = e
    return rowdict2mat(d)
Beispiel #56
0
def dot_product_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)

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

    row_dict = matutil.mat2rowdict(M)

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

    return new_v