Пример #1
0
def SVD_solve(U, Sigma, V, b):
    '''
    Input:
      - U: orthogonal matrix
      - Sigma: diagonal matrix with non-negative elements
      - V: orthogonal matrix
      - b: vector
    Output:
      - x: a vector such that U*Sigma*V.tranpose()*x = b
      - 'FAIL': if U*Sigma*V.transpose() has no inverse

    Example:
      >>> U = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): -0.44072022797538285, (1, 2): -0.4580160039142736, (0, 0): -0.15323906505773385, (2, 0): -0.8716906349733183, (1, 0): -0.4654817137547351, (2, 2): 0.08909472804179724, (0, 2): 0.8844679019577585, (2, 1): 0.4818895789856551, (1, 1): -0.7573295942443791})
      >>> Sigma = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 0): 39.37043356298421, (1, 1): 2.2839722460456144, (2, 2): 0.867428292102265})
      >>> V = Mat(({0, 1, 2}, {0, 1, 2}), {(0, 1): 0.8797721734901444, (1, 2): -0.7977287698474189, (0, 0): -0.46693900110435005, (2, 0): -0.682398941975231, (1, 0): -0.5624052393414894, (2, 2): 0.5963722979461945, (0, 2): 0.08926865071288784, (2, 1): -0.42269583181462916, (1, 1): -0.21755265229127096})
      >>> b = Vec({0,1,2}, {0:0, 1:1, 2:2})
      >>> x = SVD_solve(U, Sigma, V, b)
      >>> res = b -U*(Sigma*(V.transpose()*x))
      >>> res*res < 1e-20
      True
    '''
    Sigma_inv = listlist2mat([[1/Sigma[r,c] if Sigma[r,c] != 0 else 0 for r in Sigma.D[0]] for c in Sigma.D[1]])
    try:
        I = (U * (Sigma * (V.transpose()))) * (V * (Sigma_inv * (U.transpose())))
    except:
        return 'FAIL'
    return V * (Sigma_inv * (U.transpose() * b))
Пример #2
0
def rotation(angle):
    angle_degree = angle
    angle_matrix = [[1,0,0],[0, cos(angle_degree), sin(angle_degree)],[0, -sin(angle_degree), cos(angle_degree)]]
    Imatrix = identity(labels = {'x','y','u'})
    list_rc = [(r,c) for r in {'x','y','u'} for c in {'x','y','u'} ]
    mat_angle = listlist2mat(angle_matrix)
    result = Mat((labels, labels), {('u','u'):1, ('x','y'): -sin(angle_degree), ('y','x'): sin(angle_degree), ('x','x'):cos(angle_degree), ('y','y'): cos(angle_degree)})
    return result
    '''
def listimage2vecdict(images):
    vec_dict = {}
    for i, pixels in images.items():
        F = listlist2mat(pixels)
        vec_dict.update({
            i:
            Vec({(x, y)
                 for x in range(189)
                 for y in range(166)}, {(x, y): F[x, y]
                                        for x in range(189)
                                        for y in range(166)})
        })
    return vec_dict
Пример #4
0
def problem9():
    vlist = read_vectors('age-height.txt')
    agelist = []
    heightlist = []
    for v in vlist:
        agelist.append(v['age'])
        heightlist.append(v['height'])
    oneslist = [1] * len(agelist)
    print(agelist)
    print(heightlist)
    colsA = [[a, o] for a, o in zip(agelist, oneslist)]
    A = listlist2mat(colsA)
    print(A)
    b = list2vec(heightlist)
    print(b)
    x = QR_solve(A, b)
    print(x)
    result = A.transpose()*(b-A*x)
    print(result.is_almost_zero())
from mat import Mat
from matutil import listlist2mat, mat2coldict, coldict2mat
from vecutil import list2vec
from bitutil import str2bits, bits2str, bits2mat, mat2bits, noise
from vec import dot
from GF2 import One

# Task 4.14.1 Create instance of Mat representing the generator matrix G
G = listlist2mat([[One(), 0, One(), One()],
                  [One(), One(), 0, One()],
                  [0, 0, 0, One()],
                  [One(), One(), One(), 0],
                  [0, 0, One(), 0],
                  [0, One(), 0, 0],
                  [One(), 0, 0, 0]])

# Task 4.14.2 what is the encoding of the message [1,0,0,1]
p = list2vec([One(), 0, 0, One()])
encoded_p = G * p

# Task 4.14.3
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]])

# Task 4.14.4
H = listlist2mat([
                  [One(), 0, One(), 0, One(), 0, One()],
                  [0, One(), One(), 0, 0, One(), One()],
                  [0, 0, 0, One(), One(), One(), One()],
Пример #6
0
print(G)

G1 = G.transpose()
print(G1)

#task 4.14.2 encode [one, 0, 0, one]
p = Vec({0,1,2,3},{0:one, 3: one})
print(p, "p")
c = G*p

#4.13.3 going from codewords to words
#because rows a, b, and d of g are parity bits, and we dont need them to go
#from codewords to words b/c we already checked against H, a,b and d can be all
#zeros.  this leaves us with a 4x4 identity matrix

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]])
print("R",R)
print("R*c",R*c)
print("R*G", R*G)


#4.14.4 Decoding
#In my head I calulated that p = [0000] for [0111100]*G because four of the
#rows of G are standard generators for gf2^4, (positions 3 5 6 and 7), any codeword with a 1 there
#will evaulate to a 1 in the linear combination step.  The other three rows have 3 ones, don't know what that means
#^^this note from day before is super interesting. I was sooo close just didnt' understand what a parity bit was :).  They are just tests to make sure other bits have not been corrupted.  what happpens if parity bits get corrupted?
#in this case the normal bits act as parity bits for the parity bits themselves :)

#4.14.3 built a 4x7 matrix R such that, for any codeword c, the matrix vector prodct R*c equals
#ANGER!  this seems impossible, b/c non square matrices aren't invertible. if it was however, it seems
#thinking that I don't understand this question
Пример #7
0
    return {(i, j) for i in range(n) for j in range(n)}


def button_vectors(n):
    return {(i, j): Vec(
        D(n),
        dict([((x, j), one) for x in range(max(i - 1, 0), min(i + 2, n))] +
             [((i, y), one) for y in range(max(j - 1, 0), min(j + 2, n))]))
            for (i, j) in D(n)}


m = matutil.listlist2mat([[0, one, one, one, 0, one, 0, one, 0],
                          [one, one, 0, one, 0, one, one, one, 0],
                          [one, one, 0, one, 0, 0, one, 0, one],
                          [one, one, one, one, one, one, one, 0, one],
                          [one, one, one, one, one, one, 0, one, one],
                          [0, one, one, one, 0, one, one, one, one],
                          [one, one, 0, 0, 0, 0, one, one, 0],
                          [0, one, 0, one, 0, one, one, 0, 0],
                          [one, one, one, 0, one, 0, one, 0, one]])

b1 = Vec(
    D(9), {
        (7, 8): one,
        (7, 7): one,
        (6, 2): one,
        (3, 7): one,
        (2, 5): one,
        (8, 5): one,
        (1, 2): one,
        (7, 2): one,
    Input:
        - A: a Mat with rows as feature vectors
        - b: a Vec of actual diagnoses
        - w: hypothesis Vec
    Output:
        - Fraction (as a decimal in [0,1]) of vectors incorrectly
          classified by w 
    '''
    z = signum(A*w)
    ##print(w,z,b)
    return 1-(z*signum(b) + len(b.D))/(2*len(b.D))




A1 = listlist2mat([[10, 7, 11, 10, 14], [1, 1, 13, 3, 2], [6, 13, 3, 2, 6],[10, 10, 12, 1, 2], [2, 1, 5, 7, 10]])
#3print(A1)
b1 = list2vec([1, 1, -1, -1, 1])
A2 = Mat((set(range(97,123)),set(range(65,91))),{(x,y): 301-(7*((x-97)+26*(y-65))%761) for x in range(97,123) for y in range(65,91)})
b2 = Vec(A2.D[0], {x:(-1)**i for i, x in enumerate(sorted(A2.D[0]))})

##print(1,fraction_wrong(A1, b1, Vec(A1.D[1], {})))
##print(2,fraction_wrong(A1, b1, Vec(A1.D[1], {x:-2 for x in A1.D[1]})))
##print(3,fraction_wrong(A1, b1, Vec(A1.D[1], {x: (-1)**i for i, x in enumerate(sorted(A1.D[1]))})))
##print(4,fraction_wrong(A2, b2, Vec(A2.D[1], {})))
##print(5,fraction_wrong(A2, b2, Vec(A2.D[1], {x:-2 for x in A2.D[1]})))
##print(6,fraction_wrong(A2, b2, Vec(A2.D[1], {x: (-1)**i for i, x in enumerate(sorted(A2.D[1]))})))



## Task 3 ##
Пример #9
0
def fraction_wrong(A, b, w):
    '''
    Input:
        - A: a Mat with rows as feature vectors
        - b: a Vec of actual diagnoses
        - w: hypothesis Vec
    Output:
        - Fraction (as a decimal in [0,1]) of vectors incorrectly
          classified by w 
    '''
    z = signum(A * w)
    ##print(w,z,b)
    return 1 - (z * signum(b) + len(b.D)) / (2 * len(b.D))


A1 = listlist2mat([[10, 7, 11, 10, 14], [1, 1, 13, 3, 2], [6, 13, 3, 2, 6],
                   [10, 10, 12, 1, 2], [2, 1, 5, 7, 10]])
#3print(A1)
b1 = list2vec([1, 1, -1, -1, 1])
A2 = Mat(
    (set(range(97, 123)), set(range(65, 91))),
    {(x, y): 301 - (7 * ((x - 97) + 26 * (y - 65)) % 761)
     for x in range(97, 123) for y in range(65, 91)})
b2 = Vec(A2.D[0], {x: (-1)**i for i, x in enumerate(sorted(A2.D[0]))})

##print(1,fraction_wrong(A1, b1, Vec(A1.D[1], {})))
##print(2,fraction_wrong(A1, b1, Vec(A1.D[1], {x:-2 for x in A1.D[1]})))
##print(3,fraction_wrong(A1, b1, Vec(A1.D[1], {x: (-1)**i for i, x in enumerate(sorted(A1.D[1]))})))
##print(4,fraction_wrong(A2, b2, Vec(A2.D[1], {})))
##print(5,fraction_wrong(A2, b2, Vec(A2.D[1], {x:-2 for x in A2.D[1]})))
##print(6,fraction_wrong(A2, b2, Vec(A2.D[1], {x: (-1)**i for i, x in enumerate(sorted(A2.D[1]))})))
Пример #10
0
        >>> 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
    '''
    pass


## 7: (Problem 9.11.13) Least Squares Problem
# 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 = ...

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 = ...

## 8: (Problem 9.11.14) Small examples of least squares
#Find the vector minimizing (Ax-b)^2
Пример #11
0
part_7_number_cols = 3

## 6: (Problem 4.17.6) Matrix-matrix multiplication practice with small matrices
# Please represent your answer as a list of row lists.
# Example: [[1,1],[2,2]]
small_mat_mult_1 = [[8, 13], [8, 14]]
small_mat_mult_2 = [[24, 11, 4], [1, 3, 0]]
small_mat_mult_3 = [[3, 13]]
small_mat_mult_4 = [[14]]
small_mat_mult_5 = [[1, 2, 3], [2, 4, 6], [3, 6, 9]]
small_mat_mult_6 = [[-2, 4], [1, 1], [1, -3]]

## 7: (Problem 4.17.7) Matrix-matrix multiplication practice with a permutation matrix
# Please represent your solution as a list of row lists.

A = listlist2mat([[2, 0, 1, 5], [1, -4, 6, 2], [3, 0, -4, 2], [3, 4, 0, -2]])
B1 = listlist2mat([[0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0]])
B2 = listlist2mat([[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]])
B3 = listlist2mat([[0, 0, 0, 1], [0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]])

part_1_AB = m2ll(A * B1)
part_1_BA = m2ll(B1 * A)

part_2_AB = m2ll(A * B2)
part_2_BA = m2ll(B2 * A)

part_3_AB = m2ll(A * B3)
part_3_BA = m2ll(B3 * A)

## 8: (Problem 4.17.9) Matrix-matrix multiplication practice with very sparse matrices
# Please represent your answer as a list of row lists.
Пример #12
0
from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, noise
from GF2 import one
from matutil import listlist2mat
from matutil import mat2coldict
from matutil import coldict2mat

## 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_list = [[one,0,one,one],[one,one,0,one],[0,0,0,one],[one,one,one,0],[0,0,one,0],[0,one,0,0],[one,0,0,0]]
G = listlist2mat(G_list)

## 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.
Grev_list2=[[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]]
R = listlist2mat(Grev_list2)

## Task 4
# Create an instance of Mat representing the check matrix H.
H_list=[[0,0,0,one,one,one,one],[0,one,one,0,0,one,one],[one,0,one,0,one,0,one]]
H = listlist2mat(H_list)
Пример #13
0
from vec import Vec
from matutil import listlist2mat
from mat import Mat
from bitutil import noise
from GF2 import one

## Task 1 part 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 = listlist2mat([[one, 0, one, one], [one, one, 0, one], [0, 0, 0, one],
                  [one, one, one, 0], [0, 0, one, 0], [0, one, 0, 0],
                  [one, 0, 0, 0]])
#print(G)

## Task 1 part 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
encoding_1001 = G * Vec({0, 1, 2, 3}, {0: one, 3: one})
#print(encoding_1001)

## Task 2
# Express your answer as an instance of the Mat class.
#R = listlist2mat( [[1.0/8,1.0/8,-1.0/4,1.0/8,-1.0/4,-1.0/4,5.0/8],[-1.0/4,1.0/4,0,1.0/4,0,1.0/2,-1.0/4],[1.0/4,-1.0/4,0,1.0/4,1.0/2,0,-1.0/4],[1.0/4,1.0/4,1.0/2,-1.0/4,0,0,-1.0/4]])
R = listlist2mat([[0.125, 0.125, -0.25, 0.125, -0.25, -0.25, 0.625],
                  [-0.25, 0.25, 0, 0.25, 0, 0.5, -0.25],
                  [0.25, -0.25, 0, 0.25, 0.5, 0, -0.25],
                  [0.25, 0.25, 0.5, -0.25, 0, 0, -0.25]])
#print(R*G)

## Task 3
Пример #14
0
    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)


A = listlist2mat([[1, 0.5, 0.2, 4], [0, 1, 0.3, 0.9], [0, 0, 1, 0.1], [0, 0, 0, 1]])
print(
    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,
Пример #15
0
# Please fill out this stencil and submit using the provided submission script.

from vec import Vec, neg
from mat import Mat
from bitutil import noise
from GF2 import one
import bitutil
import matutil
import vecutil

## Task 1 part 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 = matutil.listlist2mat([[one,0,one,one],[one,one,0,one],[0,0,0,one],[one,one,one,0],[0,0,one,0],[0,one,0,0],[one,0,0,0]])


## Task 1 part 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 2
# Express your answer as an instance of the Mat class.
R = matutil.listlist2mat([[0,0,0,0],[0,0,0,0],[0,0,0,one],[0,0,0,0],[0,0,one,0],[0,one,0,0],[one,0,0,0]]).transpose()
## Task 3
# Create an instance of Mat representing the check matrix H.
H = matutil.listlist2mat([[0,0,0,one,one,one,one],[0,one,one,0,0,one,one],[one,0,one,0,one,0,one]])
from python_lab import dict2list, list2dict

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

## 5: (Problem 5) QR factorization of small matrices
#Compute the QR factorization

#Please represent your solution as a list of rows, such as [[1,0,0],[0,1,0],[0,0,1]]
part_1_Q, part_1_R = QR_factor(listlist2mat([[6,6],[2,0],[3,3]]))
part_1_Q = [list(x.f.values()) for x in mat2rowdict(part_1_Q).values()]
part_1_R = [list(x.f.values()) for x in mat2rowdict(part_1_R).values()]
part_2_Q, part_2_R = QR_factor(listlist2mat([[2,3],[2,1],[1,1]]))
part_2_Q = [list(x.f.values()) for x in mat2rowdict(part_2_Q).values()]
part_2_R = [list(x.f.values()) for x in mat2rowdict(part_2_R).values()]

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
Пример #17
0
coursera = 1
# Please fill out this stencil and submit using the provided submission script.

from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, noise
from GF2 import one
import matutil

## 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 = matutil.listlist2mat([[one, 0, one, one], [one, one, 0, one],
                          [0, 0, 0, one], [one, one, one, 0], [0, 0, one, 0],
                          [0, one, 0, 0], [one, 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 = matutil.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]])

## Task 4
# Create an instance of Mat representing the check matrix H.
H = matutil.listlist2mat([[0, 0, 0, one, one, one, one],
                          [0, one, one, 0, 0, one, one],
Пример #18
0
# Please fill out this stencil and submit using the provided submission script.

from GF2 import one
from matutil import listlist2mat, mat2coldict, coldict2mat
from vecutil import list2vec, zero_vec
from bitutil import str2bits, bits2mat
from vec import Vec
from mat import Mat


## 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 = listlist2mat([[one, 0, one, one], [one, one, 0, one], [0, 0, 0, one], [one, one, one, 0], [0, 0, one, 0], [0, one, 0, 0], [one, 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] * 4, [0] * 4, [0, 0, 0, one], [0] * 4, [0, 0, one, 0], [0, one, 0, 0], [one, 0, 0, 0]]).transpose()


## Task 4
# Create an instance of Mat representing the check matrix H.
H = listlist2mat([[0, 0, 0, one, one, one, one], [0, one, one, 0, 0, one, one], [one, 0, one, 0, one, 0, one]])
Пример #19
0
    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

    >>> M1 = Mat(({0,1,2},{0,1,2}),{(0,0):1,(0,2):2,(1,2):3,(2,2):4})
    >>> is_invertible(M1)
    False
    '''
    return rank(list(mat2coldict(M).values())) == len(M.D[0]) == len(M.D[1])

if BOOK_TESTS_ENABLED:
    print("== Test 6.7.12 ==")
    print(is_invertible(listlist2mat([[1, 2, 3], [3, 1, 1]])),
          is_invertible(listlist2mat([[1, 0, 1, 0],
                                      [0, 2, 1, 0],
                                      [0, 0, 3, 1],
                                      [0, 0, 0, 4]])),
          is_invertible(listlist2mat([[1, 0], [0, 1], [2, 1]])),
          is_invertible(listlist2mat([[1, 0], [0, 1]])),
          is_invertible(listlist2mat([[1, 0, 1], [0, 1, 1], [1, 1, 0]])))
    print(is_invertible(listlist2mat([[one, 0, one],
                                      [0, one, one],
                                      [one, one, 0]])),
          is_invertible(listlist2mat([[one, one], [0, one]])))
                        
            

## 10: (Problem 6.7.13) Inverse of a Matrix over GF(2)
Пример #20
0
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})
print(hw5.is_invertible(B))
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})
print(hw5.is_invertible(C))
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})
print(hw5.is_invertible(D))
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})
print(hw5.is_invertible(E))


print('\nfind inverse')
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})
N = hw5.find_matrix_inverse(M) 
NRef = 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})
print(N == NRef)

print('\ntriangular inverse')
A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
N = hw5.find_triangular_matrix_inverse(A) 
NRef = 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})
print(N == NRef)


A = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 2, (1, 2): 3, (3, 2): 0, (0, 0): 1, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 3, (2, 0): 0, (1, 3): 4, (2, 3): 4, (2, 2): 3, (1, 0): 0, (0, 3): 4})
Ainv = hw5.find_triangular_matrix_inverse(A)
B = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 4, (1, 2): 5, (3, 2): 0, (0, 0): 7, (3, 3): 4, (3, 0): 0, (3, 1): 0, (1, 1): 2, (2, 1): 0, (0, 2): 2, (2, 0): 0, (1, 3): 6, (2, 3): 4, (2, 2): 7, (1, 0): 0, (0, 3): 5})
Binv = hw5.find_triangular_matrix_inverse(B)
B = Mat(({0, 1, 2, 3}, {0, 1, 2, 3}), {(0, 1): 3, (1, 2): 6, (3, 2): 0, (0, 0): 6, (3, 3): 2345, (3, 0): 0, (3, 1): 0, (1, 1): 4, (2, 1): 0, (0, 2): 4, (2, 0): 0, (1, 3): 23, (2, 3): 2, (2, 2): 6, (1, 0): 0, (0, 3): 8})
Cinv = hw5.find_triangular_matrix_inverse(C)

Пример #21
0
print(root_method(77))
print(root_method(146771))
# print(root_method(118))

print(gcd(23, 15))
N = 367160330145890434494322103
a = 67469780066325164
b = 9429601150488992
c = a * a - b * b
print(gcd(a - b, N))

print(make_Vec({2, 3, 5, 7, 11}, [(3, 1)]))
print(make_Vec({2, 3, 5, 7, 11}, [(2, 17), (3, 0), (5, 1), (11, 3)]))
print(find_candidates(2419, primes(32))[1])

A = listlist2mat([[0, 2, 3, 4, 5], [0, 0, 0, 3, 2], [1, 2, 3, 4, 5],
                  [0, 0, 0, 6, 7], [0, 0, 0, 9, 8]])
print(A)
M = transformation(A)
print(M)
# print(M * A)
print(M * A)
# print()
# print(listlist2mat([[0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 0, 2, 1, 0], [0, 0, 3.004, 0.667, 1]]) * (M * A))
print(
    row_reduce([
        list2vec([one, one, 0, 0]),
        list2vec([one, 0, one, 0]),
        list2vec([0, one, one, one]),
        list2vec([one, 0, 0, 0])
    ]))
Пример #22
0
	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)
           


####### triangular solve ######
A = listlist2mat([[1, .5, .2, 4],[0, 1, .3, .9],[0,0,1,.1],[0,0,0,1]])
print(find_triangular_matrix_inverse(A))
####### triangular solve ######

####### matrix inverse #######
# 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})
# print(matrix_inverse(M))
####### matrix inverse #######

##### 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})
# print(is_invertible(M))
##### is_invertible #####
Пример #23
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
Пример #24
0
from matutil import listlist2mat
from vecutil import list2vec
from independence import rank
from independence import is_independent
from GF2 import one


def my_is_independent(L):
    return rank(L) == len(L)


L = [[2, 4, 0], [8, 16, 4], [0, 0, 7]]
M = [[2, 4, 0], [8, 16, 4]]
N = [[1, 3, 0, 0], [2, 1, 1, 0], [0, 0, 1, 0], [1, 1, 4, -1]]
O = [[one, 0, one, 0], [0, one, 0, 0], [one, one, one, one], [one, 0, 0, one]]
print(listlist2mat(L))
print("Matrix is Independent: ")
print(my_is_independent([list2vec(v) for v in L]))
print(listlist2mat(M))
print("Matrix is Independent: ")
print(my_is_independent([list2vec(v) for v in M]))
print(listlist2mat(N))
print("Matrix is Independent: ")
print(my_is_independent([list2vec(v) for v in N]))
print(listlist2mat(O))
print("Matrix is Independent: ")
print(my_is_independent([list2vec(v) for v in O]))


# Problem #6.7.7
def my_rank(L):
Пример #25
0
## 1: (Problem 11.8.1) Procedure for computing squared Frobenius norm
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([A[i,j]**2 for i in A.D[0] for j in A.D[1]])



## 2: (Problem 11.8.2) Frobenius_norm_counterexample
#Give a numerical counterxample.
A = listlist2mat([[2,3],[5,1]])
Q = listlist2mat([[3,0],[0,2]])

print("A: ", sqrt(squared_Frob(A)))
print("AQ: ", sqrt(squared_Frob(A*Q)))


## 3: (Problem 11.8.3) Multiplying a vector by a matrix in terms of the SVD of the matrix
# Use lists instead of Vecs
# Part 1
vT_x_1 = [[2],[1]]
Sigma_vT_x_1 = [[4],[1]]
U_Sigma_vT_x_1 = [[1],[4],[0]]

# Part 2
vT_x_2 = [[2],[0]]
Пример #26
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


## 5: (Problem 5) QR factorization of small matrices
#Compute the QR factorization

#Please represent your solution as a list of rows, such as [[1,0,0],[0,1,0],[0,0,1]]
part_1_Q, part_1_R = QR_factor(listlist2mat([[6, 6], [2, 0], [3, 3]]))
part_1_Q = [list(x.f.values()) for x in mat2rowdict(part_1_Q).values()]
part_1_R = [list(x.f.values()) for x in mat2rowdict(part_1_R).values()]
part_2_Q, part_2_R = QR_factor(listlist2mat([[2, 3], [2, 1], [1, 1]]))
part_2_Q = [list(x.f.values()) for x in mat2rowdict(part_2_Q).values()]
part_2_R = [list(x.f.values()) for x in mat2rowdict(part_2_R).values()]


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.
Пример #27
0
from GF2 import one
from matutil import listlist2mat, coldict2mat, mat2coldict

## 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_as_list=[[one,0,one,one],
           [one,one,0,one],
           [0,0,0,one],
           [one,one,one,0],
           [0,0,one,0],
           [0,one,0,0],
           [one,0,0,0]]
G = listlist2mat(G_as_list)

## 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 = Mat((set([0, 1, 2, 3]), set([0, 1, 2, 3, 4, 5, 6])), {(1, 3): 0, (3, 0): 0, (2, 1): 0, (2, 6): 0, (1, 6): 0, (2, 5): 0, (0, 3): 0, (1, 2): 0, (3, 3): 0, (2, 0): 0, (1, 5): one, (3, 6): 0, (2, 2): 0, (1, 1): 0, (3, 2): one, (0, 0): 0, (0, 4): 0, (1, 4): 0, (2, 3): 0, (1, 0): 0, (3, 5): 0, (0, 1): 0, (3, 1): 0, (0, 2): 0, (0, 6): one, (0, 5): 0, (3, 4): 0, (2, 4): one})

## Task 4
# Create an instance of Mat representing the check matrix H.
H = Mat((set([0, 1, 2]), set([0, 1, 2, 3, 4, 5, 6])), {(0, 1): 0, (1, 2): one, (2, 4): one, (0, 0): 0, (2, 6): one, (1, 5): one, (2, 3): 0, (2, 2): one, (1, 4): 0, (1, 1): one, (0, 6): one, (1, 3): 0, (0, 5): one, (2, 1): 0, (2, 5): 0, (0, 4): one, (1, 0): 0, (1, 6): one, (0, 3): one, (2, 0): one, (0, 2): 0})

## Task 5
Пример #28
0
#!/usr/bin/env python3
import sys
from vecutil import list2vec, vec2list
from matutil import listlist2mat
from GF2 import one

golay24mat = listlist2mat([[one, one, 0, 0, 0, one, one, one, 0, one, 0, one],
              [0, one, one, 0, 0, 0, one, one, one, 0, one, one],
              [one, one, one, one, 0, one, one, 0, one, 0, 0, 0],
              [0, one, one, one, one, 0, one, one, 0, one, 0, 0],
              [0, 0, one, one, one, one, 0, one, one, 0, one, 0],
              [one, one, 0, one, one, 0, 0, one, one, 0, 0, one],
              [0, one, one, 0, one, one, 0, 0, one, one, 0, one],
              [0, 0, one, one, 0, one, one, 0, 0, one, one, one],
              [one, one, 0, one, one, one, 0, 0, 0, one, one, 0],
              [one, 0, one, 0, one, 0, 0, one, 0, one, one, one],
              [one, 0, 0, one, 0, 0, one, one, one, one, one, 0],
              [one, 0, 0, 0, one, one, one, 0, one, 0, one, one]])

golay23mat = listlist2mat([[one, one, 0, 0, 0, one, one, one, 0, one, 0],
              [0, one, one, 0, 0, 0, one, one, one, 0, one],
              [one, one, one, one, 0, one, one, 0, one, 0, 0],
              [0, one, one, one, one, 0, one, one, 0, one, 0],
              [0, 0, one, one, one, one, 0, one, one, 0, one],
              [one, one, 0, one, one, 0, 0, one, one, 0, 0],
              [0, one, one, 0, one, one, 0, 0, one, one, 0],
              [0, 0, one, one, 0, one, one, 0, 0, one, one],
              [one, one, 0, one, one, one, 0, 0, 0, one, one],
              [one, 0, one, 0, one, 0, 0, one, 0, one, one],
              [one, 0, 0, one, 0, 0, one, one, one, one, one],
              [one, 0, 0, 0, one, one, one, 0, one, 0, one]])
Пример #29
0
# I = identity({ 'a', 'b', 'c' })
print(mat2rowdict(M))
print(mat2coldict(M))
print(mat2vec(M))
print(mat2vec(transpose(M)))

# print('==========')
# B = coldict2mat(button_vectors(5))
# print(B)

print(diag({"a", "b", "c"}, {"a": 1, "b": 2, "c": 3}).f)

print("%%%%%%%%%%%%%%%%%%%%%%")
print("HAMMING CODE")
G = listlist2mat([[one, 0, one, one], [one, one, 0, one], [0, 0, 0, one],
                  [one, one, one, 0], [0, 0, one, 0], [0, one, 0, 0],
                  [one, 0, 0, 0]])

word = list2vec([one, 0, 0, one])
codeword = G * word
print("Codeword:")
print(codeword)

R = Mat((G.D[1], G.D[0]), {(0, 6): one, (1, 5): one, (2, 4): one, (3, 2): one})
print("Decoded:")
print(R * codeword)
print("R * G:")
print(R * G)

H = listlist2mat([[0, 0, 0, one, one, one, one], [0, one, one, 0, 0, one, one],
                  [one, 0, one, 0, one, 0, one]])
from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, noise
from GF2 import one
import matutil 

## 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 = matutil.listlist2mat([
    [one, 0, one, one], 
    [one, one, 0, one], 
    [0, 0, 0, one], 
    [one, one, one, 0], 
    [0, 0, one, 0], 
    [0, one, 0, 0], 
    [one, 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 = matutil.listlist2mat([
    [0, 0, 0, 0, 0, 0, one], 
    [0, 0, 0, 0, 0, one, 0], 
    [0, 0, 0, 0, one, 0, 0], 
Пример #31
0
from vec import Vec
from matutil import listlist2mat
from mat import Mat
from bitutil import noise
from GF2 import one

## Task 1 part 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 = listlist2mat([[one,0,one,one], [one,one,0,one], [0,0,0,one], [one,one,one,0], [0,0,one,0], [0,one,0,0], [one,0,0,0]])
#print(G)

## Task 1 part 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
encoding_1001 = G * Vec({0,1,2,3}, {0:one,  3:one})
#print(encoding_1001)


## Task 2
# Express your answer as an instance of the Mat class.
#R = listlist2mat( [[1.0/8,1.0/8,-1.0/4,1.0/8,-1.0/4,-1.0/4,5.0/8],[-1.0/4,1.0/4,0,1.0/4,0,1.0/2,-1.0/4],[1.0/4,-1.0/4,0,1.0/4,1.0/2,0,-1.0/4],[1.0/4,1.0/4,1.0/2,-1.0/4,0,0,-1.0/4]])
R = listlist2mat([[0.125,0.125,-0.25,0.125,-0.25,-0.25,0.625],[-0.25,0.25,0,0.25,0,0.5,-0.25],[0.25,-0.25,0,0.25,0.5,0,-0.25],[0.25,0.25,0.5,-0.25,0,0,-0.25]])
#print(R*G)

## Task 3
# Create an instance of Mat representing the check matrix H.
H = None

## Task 4 part 1
Пример #32
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 = Vec({0, 1},{0: 1.0832236842105263, 1: 0.9838815789473685})


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 = Vec({0, 1},{0: 2.5010988382075188, 1: 2.658959537572257})

Пример #33
0
    for v in [[2, 1, 0, 0, 6, 0], [11, 5, 0, 0, 1, 0], [3, 1.5, 0, 0, 7.5, 0]]
]
V_basis = [list2vec(v) for v in [[0, 0, 7, 0, 0, 1], [0, 0, 15, 0, 0, 2]]]

res = direct_sum_decompose(U_basis, V_basis, list2vec([2, 5, 0, 0, 1, 0]))
print(direct_sum_decompose(U_basis, V_basis, list2vec([2, 5, 0, 0, 1, 0])))
print(res[0] + res[1])

res = direct_sum_decompose(U_basis, V_basis, list2vec([0, 0, 3, 0, 0, -4]))
print(direct_sum_decompose(U_basis, V_basis, list2vec([0, 0, 3, 0, 0, -4])))
print(res[0] + res[1])

res = direct_sum_decompose(U_basis, V_basis, list2vec([1, 2, 0, 0, 2, 1]))
print(direct_sum_decompose(U_basis, V_basis, list2vec([1, 2, 0, 0, 2, 1])))
print(res[0] + res[1])

res = direct_sum_decompose(U_basis, V_basis, list2vec([-6, 2, 4, 0, 4, 5]))
print(direct_sum_decompose(U_basis, V_basis, list2vec([-6, 2, 4, 0, 4, 5])))
print(res[0] + res[1])

print(
    is_invertible(
        listlist2mat([[1, 0, 1, 0], [0, 2, 1, 0], [0, 0, 3, 1], [0, 0, 0,
                                                                 4]])))
print(is_invertible(listlist2mat([[1, 2, 3], [3, 1, 1]])))

print(
    find_matrix_inverse(
        listlist2mat([[1, 0, 1, 0], [0, 2, 1, 0], [0, 0, 3, 1], [0, 0, 0,
                                                                 4]])))
Пример #34
0
## 1: (Problem 11.8.1) Procedure for computing squared Frobenius norm
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()])



## 2: (Problem 11.8.2) Frobenius_norm_counterexample
#Give a numerical counterxample.
A = listlist2mat([[3,2], [4,5], [-1,2]])
Q = listlist2mat([[1,0], [1,2]])



## 3: (Problem 11.8.3) Multiplying a vector by a matrix in terms of the SVD of the matrix
# Use lists instead of Vecs
# Part 1
vT_x_1 = [2, 1]
Sigma_vT_x_1 = [4, 1]
U_Sigma_vT_x_1 = [1, 4, 0]

# Part 2
vT_x_2 = [0, 2]
Sigma_vT_x_2 = [0, 2]
U_Sigma_vT_x_2 = [2, 0, 0]
Пример #35
0
        >>> x = QR_solve(A, b)
        >>> result = A.transpose()*(b-A*x)
        >>> result.is_almost_zero()
        True
    '''
    Q, R    = QR_factor(A)
    rowlist = [x for x in mat2rowdict(R).values()]
    c       = Q.transpose() * b
    return triangular_solve(rowlist, sorted(A.D[1], key=repr), c)



## 7: (Problem 7) Least Squares Problem
# 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 = QR_solve(least_squares_A1, 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 = QR_solve(least_squares_A2, least_squares_b2)


Пример #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

## Task 4
Пример #37
0
    assert v.D == M.D[1]
    rd = mat2rowdict(M)
    return Vec(M.D[0], {k: rd[k] * v for k in M.D[0]})


def dot_product_vec_mat_mult(v, M):
    '''
    >>> M2 = listlist2mat([[-5, 10], [-4, 8], [-3, 6], [-2, 4]])
    >>> v3 = Vec({0, 1, 2, 3}, {0: 4, 1: 3, 2: 2, 3: 1})
    >>> dot_product_vec_mat_mult(v3, M2) == Vec({0, 1},{0: -40, 1: 80})
    True
    '''
    assert v.D == M.D[0]
    cd = mat2coldict(M)
    return Vec(M.D[1], {k: v * cd[k] for k in M.D[1]})


if __name__ == '__main__':
    l = [[-1, 1, 2], [1, 2, 3], [2, 2, 1]]
    M = listlist2mat(l)
    v = Vec({0, 1, 2}, {0: 1, 1: 2, 2: 0})
    v2 = lin_comb_mat_vec_mult(M, v)
    M2 = listlist2mat([[-5, 10], [-4, 8], [-3, 6], [-2, 4]])
    v3 = Vec({0, 1, 2, 3}, {0: 4, 1: 3, 2: 2, 3: 1})
    v4 = dot_product_vec_mat_mult(v3, M2)
    A = Mat(({'a', 'b'}, {'@', '#', '?'}),
            {('a', '@'): 2, ('a', '#'): 1, ('a', '?'): 3, ('b', '@'): 20,
             ('b', '#'): 10, ('b', '?'): 30})
    b = Vec({'@', '#', '?'}, {'@': 0.5, '#': 5, '?': -1})
    v5 = lin_comb_mat_vec_mult(A, b)
Пример #38
0
from vec import Vec
from mat import Mat
from bitutil import noise
from GF2 import one

## Task 1 part 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"""
import matutil
G_lists= [[one, 0, one, one],[one, one, 0, one], [0,0,0, one],[one, one, one, 0],[0, 0, one, 0], [0, one, 0, 0],[one, 0, 0, 0]]

G = matutil.listlist2mat(G_lists)

## Task 1 part 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 2
# Express your answer as an instance of the Mat class.
Grev_list2=[[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]]
R = matutil.listlist2mat(Grev_list2)

## Task 3
# Create an instance of Mat representing the check matrix H.
H_list=[[0,0,0,one,one,one,one],[0,one,one,0,0,one,one],[one,0,one,0,one,0,one]]

H = matutil.listlist2mat(H_list)
Пример #39
0
from vec import Vec
from mat import Mat
from bitutil import bits2mat, str2bits, noise, mat2bits, bits2str
from matutil import coldict2mat, mat2coldict

## 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"""
from matutil import listlist2mat

G = listlist2mat([[one, zero, one, one],
                  [one, one, zero, one],
                  [zero, zero, zero, one],
                  [one, one, one, zero],
                  [zero, zero, one, zero],
                  [zero, one, zero, zero],
                  [one, zero, zero, zero]])
print(G)

## Task 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
from vecutil import list2vec

p = list2vec([one, zero, one, zero])
print(G * p)
encoding_1001 = [0, 0, one, one, 0, 0, one]

## Task 3
# Express your answer as an instance of the Mat class.
Пример #40
0
## 1: (Problem 11.8.1) Procedure for computing squared Frobenius norm
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(A[(r, c)]**2 for r in A.D[0] for c in A.D[1])



## 2: (Problem 11.8.2) Frobenius_norm_counterexample
#Give a numerical counterxample.
A = listlist2mat([[1, 2, 3], [1, 2, 3]])
Q = listlist2mat([[1, 0], [0, 1], [0, 0]])



## 3: (Problem 11.8.3) Multiplying a vector by a matrix in terms of the SVD of the matrix
# Use lists instead of Vecs
# Part 1
vT_x_1 = [2, 1]
Sigma_vT_x_1 = [4, 1]
U_Sigma_vT_x_1 = [1, 4, 0]

# Part 2
vT_x_2 = [0, 2]
Sigma_vT_x_2 = [0, 2]
U_Sigma_vT_x_2 = [2, 0, 0]
Пример #41
0
from mat import Mat
from bitutil import bits2mat, str2bits, noise
from bitutil import bits2str, mat2bits
from GF2 import one, zero
from matutil import listlist2mat, coldict2mat, mat2coldict


## 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 = listlist2mat(
    [[one,  zero, one,  one ],
     [one,  one,  zero, one ],
     [zero, zero, zero, one ],
     [one,  one,  one,  zero],
     [zero, zero, one,  zero],
     [zero, one,  zero, zero],
     [one,  zero, zero, zero]])

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


## Task 3
# Express your answer as an instance of the Mat class.
R = listlist2mat(
    [[zero, zero, zero, zero, zero, zero, one ],
     [zero, zero, zero, zero, zero, one,  zero],
     [zero, zero, zero, zero, one,  zero, zero],
Пример #42
0
from mat import Mat
from mat import matrix_vector_mul
from bitutil import noise
from bitutil import str2bits, bits2str, bits2mat, mat2bits
from GF2 import one
from matutil import listlist2mat
from matutil import mat2coldict
from matutil import coldict2mat

## Task 1 part 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"""
Gl = [ [ one, 0, one, one ], [ one, one, 0, one ], [ 0, 0, 0, one ], [ one, one, one, 0 ], [ 0, 0, one, 0 ], [ 0, one, 0, 0 ], [ one, 0, 0, 0 ] ]
G = listlist2mat(Gl)

## Task 1 part 2
# Please write your answer as a list. Use one from GF2 and 0 as the elements.
res = G * Vec( {0,1,2,3}, {0:one,1:0,2:0,3:one} )
encoding_1001 = [0, 0, one, one, 0, 0, one]

## Task 2
# Express your answer as an instance of the Mat class.
R = Mat( ( {0,1,2,3}, {0,1,2,3,4,5,6} ), {(0,6): one,(1,5): one,(2,4): one,(3,2): one} )

## Task 3
# Create an instance of Mat representing the check matrix H.
Hl = [ [0,0,0,one,one,one,one], [ 0,one,one,0,0,one,one], [one,0,one,0,one,0,one] ]
H = listlist2mat(Hl)
Пример #43
0
from vec import Vec
from mat import Mat
from bitutil import *
from GF2 import one
from matutil import listlist2mat, mat2coldict, coldict2mat

## Task 1 part 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 = listlist2mat([[one,0,one,one],[one,one,0,one], [0,0,0,one], [one, one, one, 0], [0,0,one,0], [0,one,0,0], [one, 0,0, 0]])

## Task 1 part 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 2
# Express your answer as an instance of the Mat class.
R = Mat(({0, 1, 2, 3}, {0, 1, 2, 3, 4, 5, 6}), {(0,6):one, (1,5):one, (2,4):one, (3,2):one})

## Task 3
# Create an instance of Mat representing the check matrix H.
H = listlist2mat([[0,0,0,one,one,one,one],[0,one,one,0,0,one,one],[one,0,one,0,one,0,one]])

## Task 4 part 1
def find_error(e):
    """
Input: an error syndrome as an instance of Vec
Output: the corresponding error vector e
Пример #44
0
Point_list1 = [(1.55, 0.9) , (-0.87, -2.42) , (0.61, -1.84) , (0.72, -0.1) , (0.16, 0.48) , (0.46, 0.81) , (-0.71, -1.69) , (1.26, 1.11),
(-2.14, -5.34) , (-1.09, -4.2) , (-0.61, 0.14) , (-0.33, -0.59) , (-0.64, -0.76) , (1.73, -0.67) , (2.38, 1.38) , (-0.34, -0.97),
(-2.17, -2.51) , (0.42, -0.93) , (2.43, -0.43) , (1.9, 1.22) , (2.24, 1.98) , (-1.83, -1.6) , (-0.17, 1.22) , (0.64, -0.61),
(-2.07, -3.11) , (0.59, 1.77) , (1.96, -0.38) , (-0.05, -2.64) , (0.49, 1.6) , (-0.23, 2.43) , (1.62, 1.06) , (2.06, -0.03),
(1.6, 0.41) , (1.38, 2.29) , (-0.96, -0.8) , (-0.4, -2.42) , (2.97, 1.56) , (-0.03, 1.46) , (-0.1, 0.94) , (1.29, -2.39)]

Point_list2 = [(1.38, 0.44), (-0.59, -0.26), (-1.24, 0.07), (-1.42, 1.99), (1.2, 0.01), (-0.88, -2.63), (-1.35, -0.83), (-1.09, 2.34), (-0.22, 1.21), (0.77, -0.32), (-0.15, 1.88), (-0.86, -0.38), (0.91, -2.46), (0.71, 1.06), (-1.98, -0.26), (-0.71, 1.71), (0.0, 0.15), (-0.94, 1.25), (0.55, 0.85), (-1.06, -0.58), (-1.06, -1.55), (-0.49, 0.65), (-0.37, 1.2), (-0.78, 1.02), (0.5, 0.48), (-0.38, -0.28), (-2.29, 0.2), (-1.27, 1.14), (-0.47, 1.68), (-0.65, 0.15), (2.24, -0.33), (0.6, -0.95), (-0.2, 1.85), (-0.53, -0.7), (0.23, -1.68), (0.58, 0.05), (0.81, 0.38), (-1.87, -0.1), (0.08, 0.63), (0.63, 0.05)]

plot(Point_list1)

A_list = []
b_list = []
for point in Point_list1:
    A_list.append([point[0]])
    b_list.append(point[1])
A = listlist2mat(A_list)
b = list2vec(b_list)

x_hat = QR_solve(A, b)[0]
print("X_hat: ", x_hat)

Solution_list1 = [(x, x*x_hat) for x in frange(-2.0,2.0,0.05)]

v1 = first_right_singular_vector(A)
print("v1: ", v1)
V_list1 = [(x, x * v1[0]) for x in frange(-2.0, 2.0, 0.05)]
plot(Point_list1 + Solution_list1 + V_list1)

#4.b: v1 has higher slope because it's minimizing square distance of both x and y axes, whereas linear regression only
# minimizes square distance along y (prediction error).
Пример #45
0
            if highest < column_zeroes[j]:
                highest_index = j
                highest = column_zeroes[j]
        if (highest < i):
            return ([], [])

        ret_columns.append(column_zeroes[highest_index])
        column_zeroes.pop(highest_index)

    matrix.pp(ret_rows.reverse(), ret_columns.reverse())
    return (ret_rows.reverse(), ret_columns.reverse())


#Task 4.14.1
G = listlist2mat([[one, zero, one, one], [one, one, zero, one],
                  [zero, zero, zero, one], [one, one, one, zero],
                  [zero, zero, one, zero], [zero, one, zero, zero],
                  [one, zero, zero, zero]])

#Task 4.14.2
#print(G * Vec({0,1,2,3}, {0: one, 1: zero, 2: zero, 3: one})) # 0 0 one one 0 0 one

#Task 4.14.3
G_R = listlist2mat([[zero, zero, zero, zero, zero, zero, one],
                    [zero, zero, zero, zero, zero, one, zero],
                    [zero, zero, zero, zero, one, zero, zero],
                    [zero, zero, one, zero, zero, zero, zero]])
#print(G_R * G) #Identity 4x4 matrix

#Task 4.14.4
H = listlist2mat([[zero, zero, zero, one, one, one, one],
                  [zero, one, one, zero, zero, one, one],
Пример #46
0
prod=ecl.R*ecl.G
print(prod.D)
print(prod.f)
print ('\n')

prod=ecl.H*ecl.G
print(prod.D)
print(prod.f)
print ('\n')


v1 = ecl.find_error(Vec({0,1,2}, {0:one}))
print (v1 == Vec({0, 1, 2, 3, 4, 5, 6},{3: one}))
v2 = ecl.find_error(Vec({0,1,2}, {2:one}))
print (v2 == Vec({0, 1, 2, 3, 4, 5, 6},{0: one}))
v3 = ecl.find_error(Vec({0,1,2}, {1:one, 2:one}))
print (v3 == Vec({0, 1, 2, 3, 4, 5, 6},{2: one}))    
print ('\n')

S = util.listlist2mat([[0,one,one,one],[0,one,0,0],[0,0,0,one]])
em = ecl.find_error_matrix(S)
tm = 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})
print (em == tm)
print ('\n')






Пример #47
0
def orthogonal_vec2rep(Q, b):
    Q = listlist2mat(Q)
    print(Q)
    b = list2vec(b)
    print(Q * b)
Пример #48
0
from vec import Vec, neg
from mat import Mat
from bitutil import noise
from GF2 import one
import bitutil
import matutil
import vecutil

## Task 1 part 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 = matutil.listlist2mat([[one, 0, one, one], [one, one, 0, one],
                          [0, 0, 0, one], [one, one, one, 0], [0, 0, one, 0],
                          [0, one, 0, 0], [one, 0, 0, 0]])

## Task 1 part 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 2
# Express your answer as an instance of the Mat class.
R = matutil.listlist2mat([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, one],
                          [0, 0, 0, 0], [0, 0, one, 0], [0, one, 0, 0],
                          [one, 0, 0, 0]]).transpose()
## Task 3
# Create an instance of Mat representing the check matrix H.
H = matutil.listlist2mat([[0, 0, 0, one, one, one, one],