Пример #1
0
def lin_comb_mat_vec_mult(M, v):
    assert M.D[1] == v.D
    import matutil
    from matutil import mat2coldict

    mat2col = mat2coldict(M)
    return sum([getitem(v, key) * mat2col[key] for key in v.D])
Пример #2
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])
Пример #3
0
def find_a_and_b(v, roots, N):
    '''
    Input:
     - a {0,1,..., n-1}-vector v over GF(2) where n = len(roots)
     - a list roots of integers
     - an integer N to factor
    Output:
      a pair (a,b) of integers
      such that a*a-b*b is a multiple of N
      (if v is correctly chosen)
    Example:
        >>> roots = [51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79]
        >>> N = 2419
        >>> v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{1: one, 2: one, 11: one, 5: one})  
        >>> find_a_and_b(v, roots, N)
        (13498888, 778050)
        >>> v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: 0, 10: one, 2: one})
        >>> find_a_and_b(v, roots, N)
        (4081, 1170)
    '''
    alist = [roots[i] for i in v.D if getitem(v, i) == one]
    a = prod(alist)
    c = prod([x * x - N for x in alist])
    b = intsqrt(c)
    assert b * b == c
    return (a, b)
Пример #4
0
def find_a_and_b(v, roots, N):
    """
    Input:
     - a {0,1,..., n-1}-vector v over GF(2) where n = len(roots)
     - a list roots of integers
     - an integer N to factor
    Output:
      a pair (a,b) of integers
      such that a*a-b*b is a multiple of N
      (if v is correctly chosen)
    Example:
        >>> roots = [51, 52, 53, 58, 61, 62, 63, 67, 68, 71, 77, 79]
        >>> N = 2419
        >>> v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{1: one, 2: one, 11: one, 5: one})  
        >>> find_a_and_b(v, roots, N)
        (13498888, 778050)
        >>> v = Vec({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},{0: 0, 1: 0, 10: one, 2: one})
        >>> find_a_and_b(v, roots, N)
        (4081, 1170)
    """
    alist = [roots[i] for i in v.D if getitem(v, i) == one]
    a = prod(alist)
    c = prod([x * x - N for x in alist])
    b = intsqrt(c)
    assert b * b == c
    return (a, b)
Пример #5
0
def vec_select(veclist, k):
    '''
    >>> D = {'a','b','c'}
    >>> v1 = Vec(D, {'a': 1})
    >>> v2 = Vec(D, {'a': 0, 'b': 1})
    >>> v3 = Vec(D, {        'b': 2})
    >>> v4 = Vec(D, {'a': 10, 'b': 10})
    >>> vec_select([v1, v2, v3, v4], 'a') == [Vec(D,{'b': 1}), Vec(D,{'b': 2})]
    True
    '''
    return [v for v in veclist if getitem(v, k) == 0]
def vec_select(veclist, k):
    '''
    >>> D = {'a','b','c'}
    >>> v1 = Vec(D, {'a': 1})
    >>> v2 = Vec(D, {'a': 0, 'b': 1})
    >>> v3 = Vec(D, {        'b': 2})
    >>> v4 = Vec(D, {'a': 10, 'b': 10})
    >>> vec_select([v1, v2, v3, v4], 'a') == [Vec(D,{'b': 1}), Vec(D,{'b': 2})]
    True
    '''
    return [v for v in veclist if getitem(v,k)==0]
Пример #7
0
def lin_comb_vec_mat_mult(v, M):
    assert (v.D == M.D[0])
    w = mat2rowdict(M)
    s = 0
    l = []
    for (i, j) in w.items():
        scalar = vec.getitem(v, i)
        s += scalar * j
        l.append(s)
        s = 0
    r = sum(l)
    return r
Пример #8
0
def lin_comb_mat_vec_mult(M, v):
    assert (M.D[1] == v.D)
    w = mat2coldict(M)
    s = 0
    l = []
    for (i, j) in w.items():
        scalar = vec.getitem(v, i)
        s += scalar * j
        l.append(s)
        s = 0
    r = sum(l)
    return r
Пример #9
0
def vec_select(veclist, k): 
    '''
    >>> D = {'a','b','c'}
    >>> v1 = Vec(D, {'a': 1})
    >>> v2 = Vec(D, {'a': 0, 'b': 1})
    >>> v3 = Vec(D, {        'b': 2})
    >>> v4 = Vec(D, {'a': 10, 'b': 10})
    >>> vec_select([v1, v2, v3, v4], 'a') == [Vec(D,{'b': 1}), Vec(D,{'b': 2})]
    True
    '''
    from vec import getitem
    return [x for x in veclist if getitem(x,k)==0]
Пример #10
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    w = mat2rowdict(M)
    s=0
    l = []
    for (i,j) in w.items():
        scalar = vec.getitem(v, i)
        s += scalar * j
        l.append(s)
        s=0
    r = sum(l)
    return r
Пример #11
0
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    w = mat2coldict(M)
    s=0
    l = []
    for (i,j) in w.items():
        scalar = vec.getitem(v, i)
        s += scalar * j
        l.append(s)
        s=0
    r = sum(l)
    return r
Пример #12
0
def find_a_and_b(v, roots, N):
    '''
    Input: 
     - a {0,1,..., n-1}-vector v over GF(2) where n = len(roots)
     - a list roots of integers
     - an integer N to factor
    Output:
      a pair (a,b) of integers
      such that a*a-b*b is a multiple of N
      (if v is correctly chosen)
    '''
    alist = [roots[s] for s in v.D if getitem(v,s) != 0]
    a = prod(alist)
    c = prod([k**2 - N for k in alist])
    b = intsqrt(c)
    assert b**2 == c
    return (a,b)
Пример #13
0
def matrix_vector_mul(M, v):
    "Returns the product of matrix M and vector v"
    assert M.D[1] == v.D
    return Vec(M.D[0], { i : sum(getitem(M, (i, j)) * vec.getitem(v, j) for j in M.D[1]) for i in M.D[0] })
Пример #14
0
def vector_matrix_mul(v, M):
    "Returns the product of vector v and matrix M"
    assert M.D[0] == v.D
    return Vec(M.D[1], { i : sum(vec.getitem(v, j) * getitem(M, (j, i)) for j in M.D[0]) for i in M.D[1] })
Пример #15
0
def lin_comb_vec_mat_mult(v, M):
    assert(v.D == M.D[0])
    return sum(vec.getitem(v,i) * mat2rowdict(M)[i] for i in v.D)
Пример #16
0
def lin_comb_mat_vec_mult(M, v):
    assert(M.D[1] == v.D)
    return sum(vec.getitem(v,i) * mat2coldict(M)[i] for i in v.D)