def approximate(xs, ys, power):
    assert len(xs) == len(ys)

    matrix_size = power + 1
    variables = 2 * power + 1

    xs = map(float, xs)
    ys = map(float, ys)

    xs = reduce(
        lambda x, y: x + [list(starmap(operator.mul, izip(x[-1], y)))], repeat(xs, variables - 1), [[1] * len(xs)]
    )
    assert len(xs) == variables

    s = map(sum, xs)
    assert s[0] == len(ys)

    b = [sum(starmap(operator.mul, izip(ys, x))) for x in xs[:matrix_size]]
    a = [s[i : i + matrix_size] for i in xrange(matrix_size)]

    # So, we have a*x = b and we are looking for x
    matr = [ai + [bi] for ai, bi in izip(a, b)]

    def unify_row(i, j):
        matr[i] = [cell / matr[i][j] for cell in matr[i]]
        assert matr[i][j] == 1

    def subtract_row(i, j, row):
        assert matr[i][j] == 1

        matr[row] = [matr[row][k] - matr[i][k] * matr[row][j] for k in xrange(len(matr[i]))]

        assert matr[row][j] == 0

    # NOTE: Example for matrix_size = 3
    #    unify_row(0, 0)
    #    subtract_row(0, 0, 1)
    #    subtract_row(0, 0, 2)
    #    unify_row(1, 1)
    #    subtract_row(1, 1, 2)
    #    unify_row(2, 2)
    #    subtract_row(2, 2, 1)
    #    subtract_row(2, 2, 0)
    #    subtract_row(1, 1, 0)

    for i in xrange(matrix_size):
        unify_row(i, i)
        for j in xrange(matrix_size - i - 1):
            subtract_row(i, i, i + j + 1)

    for i in xrange(matrix_size):
        for j in xrange(matrix_size - i - 1):
            subtract_row(matrix_size - i - 1, matrix_size - i - 1, j)

    assert all(matr[i][:matrix_size] == ([0] * i) + [1] + ([0] * (matrix_size - 1 - i)) for i in xrange(matrix_size))

    ret = map(operator.itemgetter(matrix_size), matr)

    return ret
Пример #2
0
def approximate(xs, ys, power):
    assert len(xs) == len(ys)

    matrix_size = power + 1
    variables = 2 * power + 1

    xs = map(float, xs)
    ys = map(float, ys)

    xs = reduce(lambda x, y: x + [list(starmap(operator.mul, izip(x[-1], y)))], repeat(xs, variables - 1), [[1] * len(xs)])
    assert len(xs) == variables

    s = map(sum, xs)
    assert s[0] == len(ys)

    b = [sum(starmap(operator.mul, izip(ys, x))) for x in xs[:matrix_size]]
    a = [s[i:i + matrix_size] for i in xrange(matrix_size)]

    # So, we have a*x = b and we are looking for x
    matr = [ai + [bi] for ai, bi in izip(a, b)]

    def unify_row(i, j):
        matr[i] = [cell / matr[i][j] for cell in matr[i]]
        assert matr[i][j] == 1

    def subtract_row(i, j, row):
        assert matr[i][j] == 1

        matr[row] = [matr[row][k] - matr[i][k] * matr[row][j] for k in xrange(len(matr[i]))]

        assert matr[row][j] == 0

# NOTE: Example for matrix_size = 3
#    unify_row(0, 0)
#    subtract_row(0, 0, 1)
#    subtract_row(0, 0, 2)
#    unify_row(1, 1)
#    subtract_row(1, 1, 2)
#    unify_row(2, 2)
#    subtract_row(2, 2, 1)
#    subtract_row(2, 2, 0)
#    subtract_row(1, 1, 0)

    for i in xrange(matrix_size):
        unify_row(i, i)
        for j in xrange(matrix_size - i - 1):
            subtract_row(i, i, i + j + 1)

    for i in xrange(matrix_size):
        for j in xrange(matrix_size - i - 1):
            subtract_row(matrix_size - i - 1, matrix_size - i - 1, j)

    assert all(matr[i][:matrix_size] == ([0] * i) + [1] + ([0] * (matrix_size - 1 - i)) for i in xrange(matrix_size))

    ret = map(operator.itemgetter(matrix_size), matr)

    return ret
 def __init__(self, personid_records):
     '''
     @param personid_records:
         A list of tuples: (personid, bibrefrec, flag).
         Notice that all bibrefrecs should be the same
         since the Blob represents only one bibrefrec.
     '''
     self.bib = personid_records[0][1]
     assert all(p[1] == self.bib for p in personid_records), \
         "All cluster sets should share the bibrefrec"
     self.claimed = set()
     self.assigned = set()
     self.rejected = set()
     for pid, _, flag in personid_records:
         if flag > 1:
             self.claimed.add(pid)
         elif flag >= -1:
             self.assigned.add(pid)
         else:
             self.rejected.add(pid)
Пример #4
0
 def __init__(self, personid_records):
     '''
     @param personid_records:
         A list of tuples: (personid, bibrefrec, flag).
         Notice that all bibrefrecs should be the same
         since the Blob represents only one bibrefrec.
     '''
     self.bib = personid_records[0][1]
     assert all(p[1] == self.bib for p in personid_records), \
         "All cluster sets should share the bibrefrec"
     self.claimed = set()
     self.assigned = set()
     self.rejected = set()
     for pid, _, flag in personid_records:
         if flag > 1:
             self.claimed.add(pid)
         elif flag >= -1:
             self.assigned.add(pid)
         else:
             self.rejected.add(pid)
Пример #5
0
                pid = os.fork()
                if pid == 0: # child
                    os.nice(int((float(sizs[idx]) * 20.0 / biggest)))
                    run_job(job_idx)
                else: # parent
                    pid_2_idx[pid] = job_idx
                    assert free > sizs[job_idx]
                    free -= sizs[job_idx]
                    del free_idxs[idx]
            else:
                break

        pid, status = os.wait()
        assert pid in pid_2_idx
        idx = pid_2_idx[pid]
        freed = sizs[idx]
        done += freed
        ret_status[idx] = status
        free += freed
        del pid_2_idx[pid]
        update_status(done / total, "%d / %d" % (len(jobs) - len(free_idxs) - len(pid_2_idx), len(jobs)))

    update_status_final("%d / %d" % (len(jobs), len(jobs)))
    assert is_eq(free, initial)
    assert not pid_2_idx
    assert not free_idxs
    assert len(jobs) == len(sizs) == len(ret_status) == len(bibs)
    assert all(stat != None for stat in ret_status)

    return ret_status
Пример #6
0
                    os.nice(int((float(sizs[idx]) * 20.0 / biggest)))
                    run_job(job_idx)
                else:  # parent
                    pid_2_idx[pid] = job_idx
                    assert free > sizs[job_idx]
                    free -= sizs[job_idx]
                    del free_idxs[idx]
            else:
                break

        pid, status = os.wait()
        assert pid in pid_2_idx
        idx = pid_2_idx[pid]
        freed = sizs[idx]
        done += freed
        ret_status[idx] = status
        free += freed
        del pid_2_idx[pid]
        update_status(
            done / total, "%d / %d" %
            (len(jobs) - len(free_idxs) - len(pid_2_idx), len(jobs)))

    update_status_final("%d / %d" % (len(jobs), len(jobs)))
    assert is_eq(free, initial)
    assert not pid_2_idx
    assert not free_idxs
    assert len(jobs) == len(sizs) == len(ret_status) == len(bibs)
    assert all(stat != None for stat in ret_status)

    return ret_status