示例#1
0
def test_mod_inverse_correctness():
    assert_equal(utils.mod_inverse(a=3, n=37), 25)
    assert_equal(utils.mod_inverse(a=7, n=26), 15)
    assert_equal(utils.mod_inverse(a=1, n=10), 1)
示例#2
0
def outer_loop(params,
               x_f,
               filter_location,
               filter_estimation,
               simulation=None):
    permute = np.empty(params.total_loops)
    permute_b = np.empty(params.total_loops)
    x_samp = []

    # for i in xrange(params.total_loops):
    #     if i < params.location_loops:
    #         x_samp.append(np.zeros(params.B_location, dtype=np.complex128))
    #     else:
    #         x_samp.append(np.zeros(params.B_estimation, dtype=np.complex128))

    hits_found = 0
    hits = np.zeros(params.n)
    scores = np.zeros(params.n)

    # Inner loop
    for i in xrange(params.total_loops):
        a = 0
        b = 0

        # GCD test
        # http://en.wikipedia.org/wiki/GCD_test
        while utils.gcd(a, params.n) != 1:
            a = np.random.randint(params.n)
            # print 'check', a, params.n, utils.gcd(a, params.n)
        ai = utils.mod_inverse(a, params.n)

        permute[i] = ai
        permute_b[i] = b

        perform_location = i < params.location_loops

        if perform_location:
            current_filter = filter_location
            current_B = params.B_location
        else:
            current_filter = filter_estimation
            current_B = params.B_estimation

        inner_loop_locate_result = inner_loop_locate(
            x=x_f,
            n=params.n,
            filt=current_filter,
            B=current_B,
            B_threshold=params.B_threshold,
            a=a,
            ai=ai,
            b=b)

        x_samp.append(inner_loop_locate_result['x_samp'])
        # assert x_samp[i].shape == inner_loop_locate_result['x_samp'].shape
        # print i, x_samp[i].shape, inner_loop_locate_result['x_samp'].shape
        assert inner_loop_locate_result['J'].size == params.B_threshold

        if perform_location:
            inner_loop_filter_result = inner_loop_filter(
                J=inner_loop_locate_result['J'],
                B=current_B,
                B_threshold=params.B_threshold,
                loop_threshold=params.loop_threshold,
                n=params.n,
                a=a,
                hits_found=hits_found,
                hits=hits,
                scores=scores)

            hits_found = inner_loop_filter_result['hits_found']
            hits = inner_loop_filter_result['hits']
            scores = inner_loop_filter_result['scores']

        # print params.B_threshold, inner_loop_locate_result['J'][0], inner_loop_locate_result['J'][1], hits_found

    print('Number of candidates: {0}'.format(hits_found))

    # Estimate values
    answers = estimate_values(hits=hits,
                              hits_found=hits_found,
                              x_samp=x_samp,
                              loops=params.total_loops,
                              n=params.n,
                              permute=permute,
                              B_location=params.B_location,
                              B_estimation=params.B_estimation,
                              filter_location=filter_location,
                              filter_estimation=filter_estimation,
                              location_loops=params.location_loops)

    # Reconstructed signal
    x_f = np.zeros(params.n)
    for location, value in answers.iteritems():
        print 'got', int(location), np.abs(value)
        x_f[int(location)] = np.abs(value)

    # Counts
    xc = np.zeros(params.n)
    for i in xrange(params.n):
        xc[i] = scores[i] * 1. / params.total_loops

    # debug
    if simulation is not None:
        fig = plt.figure()
        ax = fig.gca()
        ax.plot(
            simulation.t,
            xc,
            '-x',
            simulation.t,
            simulation.x_f * params.n,
            '-x',
            simulation.t,
            x_f * params.n,
            '-.x',
        )
        ax.legend(('counts', 'true signal', 'reconstruction'))
        ax.set_xlim(right=simulation.t.shape[-1] - 1)

    return {'x_f': x_f, 'answers': answers}
示例#3
0
def test_mod_inverse_correctness():
    assert_equal(utils.mod_inverse(a=3, n=37), 25)
    assert_equal(utils.mod_inverse(a=7, n=26), 15)
    assert_equal(utils.mod_inverse(a=1, n=10), 1)
示例#4
0
def outer_loop(params, x_f, filter_location, filter_estimation, simulation=None):
    permute = np.empty(params.total_loops)
    permute_b = np.empty(params.total_loops)
    x_samp = []

    # for i in xrange(params.total_loops):
    #     if i < params.location_loops:
    #         x_samp.append(np.zeros(params.B_location, dtype=np.complex128))
    #     else:
    #         x_samp.append(np.zeros(params.B_estimation, dtype=np.complex128))

    hits_found = 0
    hits = np.zeros(params.n)
    scores = np.zeros(params.n)

    # Inner loop
    for i in xrange(params.total_loops):
        a = 0
        b = 0

        # GCD test
        # http://en.wikipedia.org/wiki/GCD_test
        while utils.gcd(a, params.n) != 1:
            a = np.random.randint(params.n)
            # print 'check', a, params.n, utils.gcd(a, params.n)
        ai = utils.mod_inverse(a, params.n)

        permute[i] = ai
        permute_b[i] = b

        perform_location = i < params.location_loops

        if perform_location:
            current_filter = filter_location
            current_B = params.B_location
        else:
            current_filter = filter_estimation
            current_B = params.B_estimation

        inner_loop_locate_result = inner_loop_locate(
            x=x_f,
            n=params.n,
            filt=current_filter,
            B=current_B,
            B_threshold=params.B_threshold,
            a=a,
            ai=ai,
            b=b
        )

        x_samp.append(inner_loop_locate_result['x_samp'])
        # assert x_samp[i].shape == inner_loop_locate_result['x_samp'].shape
        # print i, x_samp[i].shape, inner_loop_locate_result['x_samp'].shape
        assert inner_loop_locate_result['J'].size == params.B_threshold

        if perform_location:
            inner_loop_filter_result = inner_loop_filter(
                J=inner_loop_locate_result['J'],
                B=current_B,
                B_threshold=params.B_threshold,
                loop_threshold=params.loop_threshold,
                n=params.n,
                a=a,
                hits_found=hits_found,
                hits=hits,
                scores=scores
            )

            hits_found = inner_loop_filter_result['hits_found']
            hits = inner_loop_filter_result['hits']
            scores = inner_loop_filter_result['scores']

        # print params.B_threshold, inner_loop_locate_result['J'][0], inner_loop_locate_result['J'][1], hits_found

    print('Number of candidates: {0}'.format(hits_found))

    # Estimate values
    answers = estimate_values(
        hits=hits,
        hits_found=hits_found,
        x_samp=x_samp,
        loops=params.total_loops,
        n=params.n,
        permute=permute,
        B_location=params.B_location,
        B_estimation=params.B_estimation,
        filter_location=filter_location,
        filter_estimation=filter_estimation,
        location_loops=params.location_loops
    )

    # Reconstructed signal
    x_f = np.zeros(params.n)
    for location, value in answers.iteritems():
        print 'got', int(location), np.abs(value)
        x_f[int(location)] = np.abs(value)

    # Counts
    xc = np.zeros(params.n)
    for i in xrange(params.n):
        xc[i] = scores[i] * 1./ params.total_loops

    # debug
    if simulation is not None:
        fig = plt.figure()
        ax = fig.gca()
        ax.plot(
            simulation.t, xc, '-x',
            simulation.t, simulation.x_f * params.n, '-x',
            simulation.t, x_f * params.n, '-.x',
        )
        ax.legend(
            (
                'counts',
                'true signal',
                'reconstruction'
            )
        )
        ax.set_xlim(right=simulation.t.shape[-1]-1)

    return {
        'x_f': x_f,
        'answers': answers
    }