Ejemplo n.º 1
0
def direct_sum_decompose(U_basis, V_basis, w):
    '''
    input:  A list of Vecs, U_basis, containing a basis for a vector space, U.
    A list of Vecs, V_basis, containing a basis for a vector space, V.
    A Vec, w, that belongs to the direct sum of these spaces.
    output: A pair, (u, v), such that u+v=w and u is an element of U and
    v is an element of V.

    >>> 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})
    >>> direct_sum_decompose(U_basis, V_basis, w) == (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}))
    True
    '''
    joined_list = U_basis + V_basis
    u_vec = Vec(U_basis[0].D, {})
    v_vec = Vec(V_basis[0].D, {})
    rep = solve(coldict2mat(joined_list), w)
    for key in rep.f.keys():
        if (joined_list[key] in U_basis):
            u_vec = u_vec + rep.f[key] * joined_list[key]
        elif (joined_list[key] in V_basis):
            v_vec = v_vec + rep.f[key] * joined_list[key]

    return (u_vec, v_vec)
Ejemplo n.º 2
0
def read_training_data(fname, D=None):
    """Given a file in appropriate format, and given a set D of features,
    returns the pair (A, b) consisting of
    a P-by-D matrix A and a P-vector b,
    where P is a set of patient identification integers (IDs).

    For each patient ID p,
      - row p of A is the D-vector describing patient p's tissue sample,
      - entry p of b is +1 if patient p's tissue is malignant, and -1 if it is benign.

    The set D of features must be a subset of the features in the data (see text).
    """
    file = open(fname)
    params = ["radius", "texture", "perimeter","area","smoothness","compactness","concavity","concave points","symmetry","fractal dimension"];
    stats = ["(mean)", "(stderr)", "(worst)"]
    feature_labels = set([y+x for x in stats for y in params])
    feature_map = {params[i]+stats[j]:j*len(params)+i for i in range(len(params)) for j in range(len(stats))}
    if D is None: D = feature_labels
    feature_vectors = {}
    patient_diagnoses = {}
    for line in file:
        row = line.split(",")
        patient_ID = int(row[0])
        patient_diagnoses[patient_ID] = -1 if row[1]=='B' else +1

        tmp_Vec = Vec(D, {f:float(row[feature_map[f]+2]) for f in D})
        feature_vectors[patient_ID] = tmp_Vec.normalize()

    return rowdict2mat(feature_vectors), Vec(set(patient_diagnoses.keys()), patient_diagnoses)
Ejemplo n.º 3
0
def matrix_matrix_mul(A, B):
    """
    Returns the result of the matrix-matrix multiplication, A*B.
    >>> A = Mat(({0,1,2}, {0,1,2}), {(1,1):4, (0,0):0, (1,2):1, (1,0):5, (0,1):3, (0,2):2})
    >>> B = Mat(({0,1,2}, {0,1,2}), {(1,0):5, (2,1):3, (1,1):2, (2,0):0, (0,0):1, (0,1):4})
    >>> A*B == Mat(({0,1,2}, {0,1,2}), {(0,0):15, (0,1):12, (1,0):25, (1,1):31})
    True
    >>> C = Mat(({0,1,2}, {'a','b'}), {(0,'a'):4, (0,'b'):-3, (1,'a'):1, (2,'a'):1, (2,'b'):-2})
    >>> D = Mat(({'a','b'}, {'x','y'}), {('a','x'):3, ('a','y'):-2, ('b','x'):4, ('b','y'):-1})
    >>> C*D == Mat(({0,1,2}, {'x','y'}), {(0,'y'):-5, (1,'x'):3, (1,'y'):-2, (2,'x'):-5})
    True
    >>> M = Mat(({0, 1}, {'a', 'c', 'b'}), {})
    >>> N = Mat(({'a', 'c', 'b'}, {(1, 1), (2, 2)}), {})
    >>> M*N == Mat(({0,1}, {(1,1), (2,2)}), {})
    True
    >>> E = Mat(({'a','b'},{'A','B'}), {('a','A'):1,('a','B'):2,('b','A'):3,('b','B'):4})
    >>> F = Mat(({'A','B'},{'c','d'}),{('A','d'):5})
    >>> E*F == Mat(({'a', 'b'}, {'d', 'c'}), {('b', 'd'): 15, ('a', 'd'): 5})
    True
    >>> F.transpose()*E.transpose() == Mat(({'d', 'c'}, {'a', 'b'}), {('d', 'b'): 15, ('d', 'a'): 5})
    True
    """
    assert A.D[1] == B.D[0]
    M = Mat((A.D[0], B.D[1]), {})
    for col in B.D[1]:
        for row in A.D[0]:
            v_tmp = Vec(B.D[0], {})
            for row_t in B.D[0]:
                v_tmp[row_t] = getitem(B, (row_t, col))
            v = matrix_vector_mul(A, v_tmp)
            setitem(M, (row, col), v[row])
    return M
Ejemplo n.º 4
0
def echelon_form(rowlist):
    rowlist = rowlist[:]
    col_label_list = sorted(rowlist[0].D, key=hash)
    new_rowlist = []
    rows_left = set(range(len(rowlist)))

    row_label_list = col_label_list
    row_labels = set(range(len(rowlist)))
    M_rowlist = [
        Vec(row_labels, {row_label_list[i]: 1}) for i in range(len(rowlist))
    ]

    new_M_rowlist = []
    for c in col_label_list:
        rows_with_nonzero = [r for r in rows_left if rowlist[r][c] != 0]
        if rows_with_nonzero != []:
            pivot = rows_with_nonzero[0]
            rows_left.remove(pivot)
            new_M_rowlist.append(M_rowlist[pivot])
            new_rowlist.append(rowlist[pivot])

            for r in rows_with_nonzero[1:]:
                multiplier = rowlist[r][c] / rowlist[pivot][c]
                rowlist[r] -= multiplier * rowlist[pivot]
                M_rowlist[r] -= multiplier * M_rowlist[pivot]

    for r in rows_left:
        new_M_rowlist.append(M_rowlist[r])

    return new_M_rowlist, new_rowlist
Ejemplo n.º 5
0
def transformation_rows(rowlist_input, col_label_list = None):
    """Given a matrix A represented by a list of rows
        optionally given the unit field element (1 by default),
        and optionally given a list of the domain elements of the rows,
        return a matrix M represented by a list of rows such that
        M A is in echelon form
    """
    # one = GF2.one # replace this with 1 if working over R or C
    rowlist = list(rowlist_input)
    if col_label_list == None: col_label_list = sorted(rowlist[0].D, key=repr)
    m = len(rowlist)
    row_labels = set(range(m))
    M_rowlist = [Vec(row_labels, {i:one}) for i in range(m)]
    new_M_rowlist = []
    rows_left = set(range(m))
    for c in col_label_list:
        rows_with_nonzero = [r for r in rows_left if rowlist[r][c] != 0]
        if rows_with_nonzero != []:
            pivot = rows_with_nonzero[0]
            rows_left.remove(pivot)
            new_M_rowlist.append(M_rowlist[pivot])
            for r in rows_with_nonzero[1:]:
                multiplier = rowlist[r][c]/rowlist[pivot][c]
                rowlist[r] -= multiplier*rowlist[pivot]
                M_rowlist[r] -= multiplier*M_rowlist[pivot]
    for r in rows_left: new_M_rowlist.append(M_rowlist[r])
    return new_M_rowlist
Ejemplo n.º 6
0
def list2vec(L):
    """Given a list L of field elements, return a Vec with domain {0...len(L)-1}
    whose entry i is L[i]

    >>> list2vec([10, 20, 30])
    Vec({0, 1, 2},{0: 10, 1: 20, 2: 30})
    """
    return Vec(set(range(len(L))), {k: L[k] for k in range(len(L))})
Ejemplo n.º 7
0
def mat2rowdict(A):
    """Given a matrix, return a dictionary mapping row labels of A to rows of A
           e.g.:
       >>> M = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 1, (2, 0): 8, (1, 0): 4, (0, 0): 3, (2, 1): -2})
           >>> mat2rowdict(M)
           {0: Vec({0, 1},{0: 3, 1: 1}), 1: Vec({0, 1},{0: 4, 1: 0}), 2: Vec({0, 1},{0: 8, 1: -2})}
           >>> mat2rowdict(Mat(({0,1},{0,1}),{}))
           {0: Vec({0, 1},{0: 0, 1: 0}), 1: Vec({0, 1},{0: 0, 1: 0})}
           """
    return {row:Vec(A.D[1], {col:A[row,col] for col in A.D[1]}) for row in A.D[0]}
Ejemplo n.º 8
0
def mat2coldict(A):
    """Given a matrix, return a dictionary mapping column labels of A to columns of A
           e.g.:
           >>> M = Mat(({0, 1, 2}, {0, 1}), {(0, 1): 1, (2, 0): 8, (1, 0): 4, (0, 0): 3, (2, 1): -2})
           >>> mat2coldict(M)
           {0: Vec({0, 1, 2},{0: 3, 1: 4, 2: 8}), 1: Vec({0, 1, 2},{0: 1, 1: 0, 2: -2})}
           >>> mat2coldict(Mat(({0,1},{0,1}),{}))
           {0: Vec({0, 1},{0: 0, 1: 0}), 1: Vec({0, 1},{0: 0, 1: 0})}
    """
    return {col:Vec(A.D[0], {row:A[row,col] for row in A.D[0]}) for col in A.D[1]}
Ejemplo n.º 9
0
def aug_orthogonalize(vlist):
    '''
    Input: a list of Vecs
    Output: a list of orthonormal Vecs spanning the same space as the input Vecs
    '''
    assert isinstance(vlist, list)
    vstarlist = []
    sigma_vecs = []
    D = set(range(len(vlist)))
    for v in vlist:
        (vstar, sigmadict) = aug_project_orthogonal(v, vstarlist)
        vstarlist.append(vstar)
        sigma_vecs.append(Vec(D, sigmadict))
    return vstarlist, sigma_vecs
Ejemplo n.º 10
0
def solve(a, b):
    mat_list = []
    vec_list = []
    D = list(a.D[1])

    for r in a.D[0]:
        row_list = []
        for c in a.D[1]:
            row_list.append(a[r, c])
        mat_list.append(row_list)
        vec_list.append(b[r])

    solution = _solve(mat_list, vec_list)[0]

    return Vec(set(D), {D[i]: solution[i] for i in range(len(D))})
Ejemplo n.º 11
0
def read_training_data(fname, D=None):
    """Given a file in appropriate format, and given a set D of features,
    returns the pair (A, b) consisting of
    a P-by-D matrix A and a P-vector b,
    where P is a set of patient identification integers (IDs).

    For each patient ID p,
      - row p of A is the D-vector describing patient p's tissue sample,
      - entry p of b is +1 if patient p's tissue is malignant, and -1 if it is benign.

    The set D of features must be a subset of the features in the data (see text).
    """
    file = open(fname)
    params = [
        "radius", "texture", "perimeter", "area", "smoothness", "compactness",
        "concavity", "concave points", "symmetry", "fractal dimension"
    ]
    stats = ["(mean)", "(stderr)", "(worst)"]
    feature_labels = set([y + x for x in stats for y in params])
    feature_map = {
        params[i] + stats[j]: j * len(params) + i
        for i in range(len(params)) for j in range(len(stats))
    }
    if D is None: D = feature_labels
    feature_vectors = {}
    patient_diagnoses = {}
    for line in file:
        row = line.split(",")
        patient_ID = int(row[0])
        patient_diagnoses[patient_ID] = -1 if row[1] == 'B' else +1

        tmp_Vec = Vec(D, {f: float(row[feature_map[f] + 2]) for f in D})
        feature_vectors[patient_ID] = tmp_Vec.normalize()

    return rowdict2mat(feature_vectors), Vec(set(patient_diagnoses.keys()),
                                             patient_diagnoses)
Ejemplo n.º 12
0
def transformation(A, col_label_list = None):
    """Given a matrix A, and optionally the unit field element (1 by default),
       compute matrix M such that M is invertible and
       U = M*A is in echelon form.
    """
    row_labels, col_labels = A.D
    m = len(row_labels)
    row_label_list = sorted(row_labels, key=repr)
    rowlist = [Vec(col_labels, {c:A[r,c] for c in col_labels}) for r in row_label_list]
    M_rows = transformation_rows(rowlist, col_label_list)
    M = Mat((set(range(m)), row_labels), {})
    for r in range(m):
        for (i,value) in M_rows[r].f.items():
            M[r,row_label_list[i]] = value
    return M
Ejemplo n.º 13
0
def signum(u):
    '''
    Input:
        - u: Vec
    Output:
        - v: Vec such that:
            if u[d] >= 0, then v[d] =  1
            if u[d] <  0, then v[d] = -1
    Example:
        >>> signum(Vec({1,2,3},{1:2, 2:-1})) == Vec({1,2,3},{1:1,2:-1,3:1})
        True
    '''
    v = Vec( u.D , {} )
    for entry in u.D:
        if u[entry] >= 0: v[entry] = 1
        else: v[entry] = -1
    return v
Ejemplo n.º 14
0
def matrix_vector_mul(M, v):
    """
    Returns the product of matrix M and vector v.
    >>> N1 = Mat(({1, 3, 5, 7}, {'a', 'b'}), {(1, 'a'): -1, (1, 'b'): 2, (3, 'a'): 1, (3, 'b'):4, (7, 'a'): 3, (5, 'b'):-1})
    >>> u1 = Vec({'a', 'b'}, {'a': 1, 'b': 2})
    >>> N1*u1 == Vec({1, 3, 5, 7},{1: 3, 3: 9, 5: -2, 7: 3})
    True
    >>> N1 == Mat(({1, 3, 5, 7}, {'a', 'b'}), {(1, 'a'): -1, (1, 'b'): 2, (3, 'a'): 1, (3, 'b'):4, (7, 'a'): 3, (5, 'b'):-1})
    True
    >>> u1 == Vec({'a', 'b'}, {'a': 1, 'b': 2})
    True
    >>> N2 = Mat(({('a', 'b'), ('c', 'd')}, {1, 2, 3, 5, 8}), {})
    >>> u2 = Vec({1, 2, 3, 5, 8}, {})
    >>> N2*u2 == Vec({('a', 'b'), ('c', 'd')},{})
    True
    """
    assert M.D[1] == v.D
    v_tmp = Vec(M.D[0], {})
    for row in v_tmp.D:
        for col in M.D[1]:
            v_tmp[row] = v_tmp[row] + getitem(M, (row, col)) * v[col]
    return v_tmp
Ejemplo n.º 15
0
def vector_matrix_mul(v, M):
    """
    returns the product of vector v and matrix M
    >>> v1 = Vec({1, 2, 3}, {1: 1, 2: 8})
    >>> M1 = Mat(({1, 2, 3}, {'a', 'b', 'c'}), {(1, 'b'): 2, (2, 'a'):-1, (3, 'a'): 1, (3, 'c'): 7})
    >>> v1*M1 == Vec({'a', 'b', 'c'},{'a': -8, 'b': 2, 'c': 0})
    True
    >>> v1 == Vec({1, 2, 3}, {1: 1, 2: 8})
    True
    >>> M1 == Mat(({1, 2, 3}, {'a', 'b', 'c'}), {(1, 'b'): 2, (2, 'a'):-1, (3, 'a'): 1, (3, 'c'): 7})
    True
    >>> v2 = Vec({'a','b'}, {})
    >>> M2 = Mat(({'a','b'}, {0, 2, 4, 6, 7}), {})
    >>> v2*M2 == Vec({0, 2, 4, 6, 7},{})
    True
    """
    assert M.D[0] == v.D
    v_tmp = Vec(M.D[1], {})
    for col in v_tmp.D:
        for row in M.D[0]:
            v_tmp[col] = v_tmp[col] + getitem(M, (row, col)) * v[row]
    return v_tmp
Ejemplo n.º 16
0
def mat2vec(M):
    return Vec({(r,s) for r in M.D[0] for s in M.D[1]}, M.f)
Ejemplo n.º 17
0
def make_Vec(primeset, list):
    res = {}
    for k, v in list:
        res[k] = int2GF2(v)

    return Vec(primeset, res)
Ejemplo n.º 18
0
def zero_vec(D):
    """Returns a zero vector with the given domain
    """
    return Vec(D, {})
Ejemplo n.º 19
0
def make_equations(x1, x2, w1, w2):
    '''
    Input:
        - x1, x2: pixel coordinates of a point q in the image plane
        - w1, w2: w1=y1/y3 and w=y2/y3 where y1,y2,y3 are the whiteboard coordinates of q.
    Output:
        - List [u,v] of D-vectors that define linear equations u*h = 0 and v*h = 0
    For example, suppose we have an image of the whiteboard in which
       the top-left whiteboard corner appears at pixel coordinates 9, 18
       the bottom-left whiteboard corner appears at pixel coordinates 8,25
       the top-right whiteboard corner appears at pixel coordinates 20,20
       the bottom-right whiteboard corner appears at pixel coordinates 18,23
    Let q be the point in the image plane with pixel coordinates x=8,y=25, i.e. camera coordinates (8,25,1).
    Let y1,y2,y3 be the whiteboard coordinates of the same point.  The line that goes through the
    origin and p intersects the whiteboard at a point p.  That point p is the bottom-left corner of
    the whiteboard, so its whiteboard coordinates are 1,0,1.  Therefore (y1/y3,y2/y3,y3/y3) = (1,0,1).
    We define w1=y1/y3 and w2=y2/y3, so w1 = 1 and w2 = 0.  Given this input-output pair, let's find
    two linear equations u*h=0 and v*h=0 constraining the unknown vector h whose entries are the entries
    of the matrix H.
>>> for v in make_equations(8,25,1,0): print(v)
<BLANKLINE>
 ('y1', 'x1') ('y1', 'x2') ('y1', 'x3') ('y2', 'x1') ('y2', 'x2') ('y2', 'x3') ('y3', 'x1') ('y3', 'x2') ('y3', 'x3')
---------------------------------------------------------------------------------------------------------------------
           -8          -25           -1            0            0            0            8           25            1
<BLANKLINE>
 ('y1', 'x1') ('y1', 'x2') ('y1', 'x3') ('y2', 'x1') ('y2', 'x2') ('y2', 'x3') ('y3', 'x1') ('y3', 'x2') ('y3', 'x3')
---------------------------------------------------------------------------------------------------------------------
            0            0            0           -8          -25           -1            0            0            0
    Note that the negations of these vectors form an equally valid solution.
    Similarly, consider the point q in the image plane with pixel coordinates 18, 23.  Let y1,y2,y3 be the whiteboard
    coordinates of p.  The corresponding point p in the whiteboard plane is the bottom-right corner, and the whiteboard
    coordinates of p are 1,1,1, so (y1/y3,y2/y3,y3/y3)=(1,1,1).  We define w1=y1/y3 and w2=y2/y3, so w1=1 and w2=1.
    We obtain the vectors u and v defining equations u*h=0 and v*h=0 as follows:
>>> for v in make_equations(18,23,1,1): print(v)
<BLANKLINE>
 ('y1', 'x1') ('y1', 'x2') ('y1', 'x3') ('y2', 'x1') ('y2', 'x2') ('y2', 'x3') ('y3', 'x1') ('y3', 'x2') ('y3', 'x3')
---------------------------------------------------------------------------------------------------------------------
          -18          -23           -1            0            0            0           18           23            1
<BLANKLINE>
 ('y1', 'x1') ('y1', 'x2') ('y1', 'x3') ('y2', 'x1') ('y2', 'x2') ('y2', 'x3') ('y3', 'x1') ('y3', 'x2') ('y3', 'x3')
---------------------------------------------------------------------------------------------------------------------
            0            0            0          -18          -23           -1           18           23            1
    Again, the negations of these vectors form an equally valid solution.
    '''
    u = Vec(
        D, {
            ('y3', 'x1'): w1 * x1,
            ('y3', 'x2'): w1 * x2,
            ('y3', 'x3'): w1,
            ('y1', 'x1'): -x1,
            ('y1', 'x2'): -x2,
            ('y1', 'x3'): -1
        })
    v = Vec(
        D, {
            ('y3', 'x1'): w2 * x1,
            ('y3', 'x2'): w2 * x2,
            ('y3', 'x3'): w2,
            ('y2', 'x1'): -x1,
            ('y2', 'x2'): -x2,
            ('y2', 'x3'): -1
        })
    return [u, v]
Ejemplo n.º 20
0
    v = Vec(
        D, {
            ('y3', 'x1'): w2 * x1,
            ('y3', 'x2'): w2 * x2,
            ('y3', 'x3'): w2,
            ('y2', 'x1'): -x1,
            ('y2', 'x2'): -x2,
            ('y2', 'x3'): -1
        })
    return [u, v]


## 3: () Scaling row
# This is the vector defining the scaling equation
w = Vec({(a, b)
         for a in {'y1', 'y2', 'y3'}
         for b in {'x1', 'x2', 'x3'}}, {('y1', 'x1'): 1})

## 4: () Right-hand side
# Now construct the Vec b that serves as the right-hand side for the matrix-vector equation L*hvec=b
# This is the {0, ..., 8}-Vec whose entries are all zero except for a 1 in position 8
b = Vec({x for x in range(9)}, {x: 0 if x != 8 else 1 for x in range(9)})


## 5: () Rows of constraint matrix
def make_nine_equations(corners):
    '''
    input: a list of four tuples:
           [(i0,j0),(i1,j1),(i2,j2),(i3,j3)]
           where i0,j0 are the pixel coordinates of the top-left corner,
                 i1,j1 are the pixel coordinates of the bottom-left corner,
Ejemplo n.º 21
0
def mat2vec(M):
    return Vec({(r,s) for r in M.D[0] for s in M.D[1]}, M.f)
print("\nQuiz 5.3.1")
mv = mat2vec(M)
print(mv.f)

# Quiz 5.4.2
def transpose(M):
    return Mat((M.D[1], M.D[0]), {(q,p):v for (p,q), v in M.f.items()})
print("\nQuiz 5.4.2")
M2 = transpose(M)
print(M2)

# Example 5.5.10
D = { 'metal', 'concrete', 'plastic', 'water', 'electricity' }
v_gnome = Vec(D, {'concrete':1.3, 'plastic':0.2, 'water':0.8, 'electricity':0.4})
v_hoop = Vec(D, {'plastic':1.5, 'water':0.4, 'electricity':0.3})
v_slinky = Vec(D, {'metal':0.25, 'water':0.2, 'electricity':0.3})
v_putty = Vec(D, {'plastic':0.3, 'water':0.7, 'electricity':0.5})
v_shooter = Vec(D, {'metal':0.15, 'plastic':0.5, 'water':0.4, 'electricity':0.8})

rowdict = {'gnome':v_gnome, 'hoop':v_hoop, 'slinky':v_slinky, 'putty':v_putty, 'shooter':v_shooter}
M = rowdict2mat(rowdict)
print(M)

R = {'gnome', 'hoop', 'slinky', 'putty', 'shooter'}
u = Vec(R, {'putty':133, 'gnome':240, 'slinky':150, 'hoop':55, 'shooter':90})
print(u*M)

# Example 5.5.15
# C = {'metal','concrete','plastic','water','electricity'}
Ejemplo n.º 22
0
S = [
    list2vec(v)
    for v in [[0, 0, 5, 3], [2, 0, 1, 3], [0, 0, 1, 0], [1, 2, 3, 4]]
]
A = [list2vec(v) for v in [[0, 0, 5, 3], [2, 0, 1, 3]]]
z = list2vec([0, 2, 1, 1])
#print(exchange(S, A, z))

S = [list2vec(v) for v in [[2, 4, 0], [1, 0, 3], [0, 4, 4], [1, 1, 1]]]
B = [list2vec(v) for v in [[1, 0, 0], [0, 1, 0], [0, 0, 1]]]
# for (z,w) in morph(S,B):
#     print("injecting ", z)
#     print("ejecting ", w)
#     print()

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})
#print(is_superfluous([a0, a1, a2, a3], 0))
#print(is_superfluous([a0, a1, a2, a3], 1))
#print(is_superfluous([a0, a1, a2, a3], 2))
#print(is_superfluous([a0, a1, a2, a3], 3))
#print("--------")
#print(is_independent([a0, a3, a1, a2]))
#print(is_independent([a0, a1, a2]))
#print(is_independent([a0, a2, a3]))
#print(is_independent([a0, a1, a3]))
#print(is_independent([a0, a1, a2, a3]))

#print(subset_basis([ a0, a1, a2, a3 ]))
Ejemplo n.º 23
0
def mat2rowdict(A):
    return {r: Vec(A.D[1], {c: A[r, c] for c in A.D[1]}) for r in A.D[0]}
Ejemplo n.º 24
0
def mat2coldict(A):
    return {c: Vec(A.D[0], {c: A[r, c] for r in A.D[0]}) for c in A.D[1]}
Ejemplo n.º 25
0
from alvin.util.vec import Vec
from alvin.util.mat import Mat
from alvin.util.matutil import rowdict2mat

# Example 5.5.10
D = {'metal', 'concrete', 'plastic', 'water', 'electricity'}
v_gnome = Vec(D, {
    'concrete': 1.3,
    'plastic': 0.2,
    'water': 0.8,
    'electricity': 0.4
})

rowdict = {'gnome': v_gnome}
M = rowdict2mat(rowdict)
print(M)

R = {'gnome'}
u = Vec(R, {'gnome': 100})
print(100 * v_gnome)

# Example 5.5.15
C = {'metal', 'concrete', 'plastic', 'water', 'electricity'}
b = Vec(
    C, {
        'water': 373.1,
        'concrete': 312.0,
        'plastic': 215.4,
        'metal': 51.0,
        'electricity': 356.0
    })
Ejemplo n.º 26
0
from alvin.util.vec import Vec
from alvin.util.mat import Mat
from alvin.util.matutil import listlist2mat

# Quiz 5.1.1
r1 = [[0 for j in range(4)] for i in range(3)]
print("\nQuiz 5.1.1")
print(r1)

# Quiz 5.1.2
r2 = [[i - j for i in range(3)] for j in range(4)]
print("\nQuiz 5.1.2")
print(r2)

# Quiz 5.1.3
r3 = Vec({'a', 'b'}, {'a': 3, 'b': 30})
print("\nQuiz 5.1.3")
print(r3)

# Quiz 5.1.5
r4 = {
    '#': Vec({'a', 'b'}, {
        'a': 2,
        'b': 20
    }),
    '@': Vec({'a', 'b'}, {
        'a': 1,
        'b': 10
    }),
    '?': Vec({'a', 'b'}, {
        'a': 3,