def __init__(self, num_filters, classes, hidden_units=256):

        self.params = {}
        self.num_filters = num_filters
        self.classes = classes
        self.hidden_units = hidden_units
        self.step = Steps()
        self.initialize_model()
Ejemplo n.º 2
0
def gcd(A, B):
    A, B = int(A), int(B)
    steps = Steps()
    while B != A and B != 0:
        temp = B
        steps.add_mod()
        B = A % B
        A = temp
    return A, steps
Ejemplo n.º 3
0
def pollards_pminus1_wrapper(N):
    steps = Steps()
    primes_list = get_primes()
    unfactored, factors = [N], []

    while len(unfactored) > 0:
        n = unfactored.pop()
        if n in primes_list:
            factors.append(n)
        elif n == 1:
            continue
        else:
            steps.add_cuberoot(n)
            B = math.pow(n, 1.0 / 3.0)

            f1, f2, hsteps = pollards_pminus1(n, B)
            steps.append(hsteps)
            if f1 == None:  # p-1 wont work, trial division
                flist, tsteps, _ = trial_division(n)
                steps.append(tsteps)
                factors.extend(flist)
            else:
                unfactored.append(f1)
                unfactored.append(f2)

    return factors, steps, 0
Ejemplo n.º 4
0
    def __init__(self,
                 num_filters,
                 classes,
                 hidden_units=256,
                 batch_norm=True,
                 eps=1e-8):

        self.params = {}
        self.num_filters = num_filters
        self.classes = classes
        self.hidden_units = hidden_units
        self.step = Steps()
        self.batch_norm = batch_norm
        self.eps = eps
        self.initialize_model()
Ejemplo n.º 5
0
def StartReconstruction(datastore):
    reconstructor = Reconstructor()
    project_ini = reconstructor.load_project_ini(
        path.realpath(datastore.ini_path))
    reconstructor.project_ini = project_ini

    rp = path.realpath(datastore.root_path)
    if datastore.recursive == True:
        filemanager = FileManager(rp)
    else:
        filemanager = [rp]

    for cnt, rel_path in enumerate(filemanager):
        current_project_path = path.join(
            datastore.root_path,
            rel_path) if datastore.root_path != "./" else "./"
        reconstructor.root_dir = current_project_path
        print("#" + (cnt + 1).__str__() + "# " + "Working on " +
              current_project_path)
        print(datastore.steps)
        steps = Steps.fromBinaryString(datastore.steps)

        if Steps.matcher in steps:
            matcher_index = steps.index(Steps.matcher)
            steps[matcher_index].matching_strategy = MatchingStrategy[
                datastore.matching_strategy]

        [reconstructor.execute_step(s) for s in steps]
Ejemplo n.º 6
0
def wrapper(N, factoring_method):
    steps = Steps()

    if factoring_method == pollards_pminus1:
        return pollards_pminus1_wrapper(N)

    # prewrappers recommended in joy of factoring
    if factoring_method == fermats_diff_of_squares:
        factors, psteps, unfactored = fermats_diff_of_squares_prewrapper(N)
    elif factoring_method == harts_one_line:
        factors, psteps, unfactored = harts_one_line_prewrapper(N)
    elif factoring_method == lehmans_var_of_fermat:
        factors, psteps, unfactored = lehmans_var_of_fermat_prewrapper(N)
    elif factoring_method == pollards_rho:
        factors, psteps, unfactored = [], Steps(), [N]  #no prewrapper
    steps.append(psteps)

    primes_list = get_primes()
    while len(unfactored) > 0:
        n = unfactored.pop()
        if n in primes_list:
            factors.append(n)
        elif n == 1:
            continue
        else:
            f1, f2, fsteps = factoring_method(n)
            steps.append(fsteps)
            unfactored.append(f1)
            unfactored.append(f2)

    return factors, steps, 0
Ejemplo n.º 7
0
def fermats_diff_of_squares(N):
    if N % 2 == 0:
        raise ValueError("Must be given odd N")
    steps = Steps()
    steps.add_sqrt(N)
    x = int(math.floor(math.sqrt(N)))
    t, r = 2 * x + 1, x * x - N
    isq, isq_steps = is_square(r)
    steps.append(isq_steps)
    while (not isq):
        r += t
        t += 2
        isq, isq_steps = is_square(r)
        steps.append(isq_steps)
    x = (t - 1) / 2
    steps.add_sqrt(r)
    y = int(math.sqrt(r))
    return x - y, x + y, steps
Ejemplo n.º 8
0
def pollards_rho(N, steps=None):
    if steps == None:
        steps = Steps()
    if N == 4:
        return 2, 2, steps

    b = random.randint(1, N - 3)
    s = random.randint(0, N - 1)
    A, B, g = s, s, 1
    while (g == 1):
        A = (A * A + b) % N
        B = (((B * B + b) % N) * ((B * B + b) % N) + b) % N
        g, g_steps = gcd(A - B, N)
        steps.append(g_steps)
    if g < N:
        return g, N / g, steps
    else:  #continue until get a result
        return pollards_rho(N, steps)
Ejemplo n.º 9
0
def harts_one_line_prewrapper(N):
    steps = Steps()
    factors, unfactored = [], [N]

    isq, isq_steps = is_square(N)
    steps.append(isq_steps)
    if not isq:
        steps.add_cuberoot(N)
        when_to_stop = math.pow(N, 1.0 / 3.0)
        factors, tsteps, num_unfactored = trial_division(N, when_to_stop)
        steps.append(tsteps)
        unfactored = []
        if num_unfactored == 1:
            unfactored.append(factors[-1])
            factors = factors[:-1]

    return factors, steps, unfactored
Ejemplo n.º 10
0
def fermats_diff_of_squares_prewrapper(N):
    steps = Steps()
    factors = []

    steps.add_mod()
    while N % 2 == 0:
        factors.append(2)
        N = N / 2
        steps.add_mod()
    unfactored = [N]
    return factors, steps, unfactored
Ejemplo n.º 11
0
def lehmans_var_of_fermat_prewrapper(N):
    steps = Steps()

    steps.add_cuberoot(N)
    s3n = int(math.ceil(N**(1.0 / 3.0)))

    factors, tsteps, num_unfactored = trial_division(N, s3n)
    steps.append(tsteps)

    unfactored = []
    if num_unfactored == 1:
        unfactored.append(factors[-1])
        factors = factors[:-1]
    return factors, steps, unfactored
Ejemplo n.º 12
0
def trial_division(N, when_to_stop=None):
    primes_list = get_primes()
    m, pi, p = N, 0, 2
    steps = Steps()
    factors = []
    if when_to_stop == None:
        when_to_stop = math.sqrt(N)
        steps.add_sqrt(N)

    while (p <= when_to_stop):
        steps.add_mod()
        if m % p == 0:
            m = m / p
            factors.append(p)
        else:
            pi += 1
            p = primes_list[pi]
    num_unfactored = 0
    if m != 1:
        factors.append(m)
        if m not in primes_list:
            num_unfactored = 1
    return factors, steps, num_unfactored
Ejemplo n.º 13
0
class Conv(object):
    def __init__(self,
                 num_filters,
                 classes,
                 hidden_units=256,
                 batch_norm=True,
                 eps=1e-8):

        self.params = {}
        self.num_filters = num_filters
        self.classes = classes
        self.hidden_units = hidden_units
        self.step = Steps()
        self.batch_norm = batch_norm
        self.eps = eps
        self.initialize_model()

    def initialize_model(self):

        if self.batch_norm:
            self.params = dict(
                w1=np.random.randn(self.num_filters, 1, 3, 3) /
                np.sqrt(self.num_filters / 2.0),
                w2=np.random.randn(self.num_filters * 14 * 14,
                                   self.hidden_units) /
                np.sqrt(self.num_filters * 14 * 14 / 2.0),
                w3=np.random.randn(self.hidden_units, self.classes) /
                np.sqrt(self.hidden_units / 2.0),
                b1=np.zeros((self.num_filters, 1)) + 0.002,
                b2=np.zeros((1, self.hidden_units)) + 0.001,
                b3=np.zeros((1, self.classes)) + 0.06,
                gamma=0.2,
                beta=0.3)
        else:
            self.params = dict(
                w1=np.random.randn(self.num_filters, 1, 3, 3) /
                np.sqrt(self.num_filters / 2.0),
                w2=np.random.randn(self.num_filters * 14 * 14,
                                   self.hidden_units) /
                np.sqrt(self.num_filters * 14 * 14 / 2.0),
                w3=np.random.randn(self.hidden_units, self.classes) /
                np.sqrt(self.hidden_units / 2.0),
                b1=np.zeros((self.num_filters, 1)) + 0.002,
                b2=np.zeros((1, self.hidden_units)) + 0.001,
                b3=np.zeros((1, self.classes)) + 0.06)

        print("Model Params")
        for i, j in self.params.items():
            if i in ["gamma", "beta"]:
                print(i, ": ", j)
            else:
                print(i, ": ", j.shape)

    def train(self, x_train, y_train):

        y_pred, cache = self.forward(x_train)
        loss = cross_entropy(y_pred, y_train)
        grad = self.backward(y_pred, y_train, cache)

        return grad, loss

    def forward(self, x):

        num_samples = x.shape[0]
        h_1, h_1_cache = self.step.conv_forward(x,
                                                self.params["w1"],
                                                self.params["b1"],
                                                stride=1,
                                                padding=1)
        h_1, relu_cache_1 = self.step.relu_forward(h_1)
        pool, pool_cache = self.step.maxpool_forward(h_1, size=2, stride=2)

        h_2 = pool.ravel().reshape(num_samples, -1)

        fc_1, fc_cache_1 = self.step.fc_forward(h_2, self.params["w2"],
                                                self.params["b2"])
        if self.batch_norm:

            fc_1, bn_cache = self.step.batchnorm_forward(
                fc_1, self.params["gamma"], self.params["beta"], self.eps)

        fc_1, relu_cache_2 = self.step.relu_forward(fc_1)

        fc_2, fc_cache_2 = self.step.fc_forward(fc_1, self.params["w3"],
                                                self.params["b3"])
        if self.batch_norm:
            cache = (x, h_1_cache, relu_cache_1, pool, pool_cache, fc_cache_1,
                     relu_cache_2, fc_cache_2, bn_cache)
        else:
            cache = (x, h_1_cache, relu_cache_1, pool, pool_cache, fc_cache_1,
                     relu_cache_2, fc_cache_2)
        return fc_2, cache

    def backward(self, y_pred, y_train, cache):

        if self.batch_norm:
            x, h_1_cache, relu_cache_1, pool, pool_cache, fc_cache_1, relu_cache_2, fc_cache_2, bn_cache = cache
        else:
            x, h_1_cache, relu_cache_1, pool, pool_cache, fc_cache_1, relu_cache_2, fc_cache_2 = cache

        grad = dcross_entropy(y_pred, y_train)

        dh3, dw3, db3 = self.step.fc_backward(grad, fc_cache_2)
        dh3 = self.step.relu_backward(dh3, relu_cache_2)

        if self.batch_norm:
            dh3, dgamma, dbeta = self.step.batchnorm_backward(dh3, bn_cache)

        dh2, dw2, db2 = self.step.fc_backward(dh3, fc_cache_1)

        dh2 = dh2.ravel().reshape(pool.shape)

        dpool = self.step.maxpool_backward(dh2, pool_cache)

        dh1 = self.step.relu_backward(dpool, relu_cache_1)

        dh, dw1, db1 = self.step.conv_backward(dh1, h_1_cache)

        if self.batch_norm:
            grads = dict(w1=dw1,
                         w2=dw2,
                         w3=dw3,
                         b1=db1,
                         b2=db2,
                         b3=db3,
                         gamma=dgamma,
                         beta=dbeta)
        else:
            grads = dict(w1=dw1, w2=dw2, w3=dw3, b1=db1, b2=db2, b3=db3)

        return grads

    def predict(self, x):
        pred, _ = self.forward(x)
        return np.argmax(self.softmax(pred), axis=1)

    def softmax(self, x):

        reg = (x.T - np.max(x, axis=1)).T
        ex = np.exp(reg)
        ex = ex / np.sum(ex, axis=1).reshape(-1, 1)
        return ex
Ejemplo n.º 14
0
def lehmans_var_of_fermat(N):
    s3n = int(math.ceil(N**(1.0 / 3.0)))
    steps = Steps()
    for k in xrange(1, s3n + 1):
        steps.add_sqrt(k * N)
        sk = 2.0 * math.sqrt(k * N)

        steps.add_exp(sk + N, 1.0 / 6.0)
        steps.add_sqrt(k)
        for a in xrange(
                int(math.ceil(sk)),
                int(
                    math.floor(sk + N**(1.0 / 6.0) / (4.0 * math.sqrt(k))) +
                    1)):
            b = a * a - 4 * k * N

            isq, isq_steps = is_square(b)
            steps.append(isq_steps)
            if isq:
                my_gcd, gcd_steps = gcd(a + math.sqrt(b), N)
                steps.append(gcd_steps)
                return my_gcd, N / my_gcd, steps
    raise Exception("should not get here ")
Ejemplo n.º 15
0
def harts_one_line(N, L=-1):
    steps = Steps()
    if L == -1:
        L = N
    for i in xrange(1, L):
        steps.add_sqrt(N * i)
        s = int(math.ceil(math.sqrt(N * i)))
        steps.add_mod()
        m = (s * s) % N

        isq, isq_steps = is_square(m)
        steps.append(isq_steps)
        if (isq):
            break

    steps.add_sqrt(m)
    t = math.sqrt(m)
    f1, gcd_steps = gcd(s - t, N)
    f2 = N / f1
    steps.append(gcd_steps)
    return f1, f2, steps
Ejemplo n.º 16
0
    def __init__(self, initfile):

        self.cfg = ConfigParser.ConfigParser(
            {'aux_channel_source': '/virgoData/ffl/rds.ffl',
             'data_source': '/virgoData/ffl/raw.ffl'},
            allow_no_value=True)
        self.cfg.read(initfile)

        # get the BRMS parameters

        self.data_source = self.cfg.get('BRMS', 'data_source')
        self.max_freq = self.cfg.getfloat('BRMS', 'max_freq')
        self.n_points = self.cfg.getint('BRMS', 'n_points')
        self.overlap = self.cfg.getfloat('BRMS', 'overlap')
        self.steps = self.cfg.get('BRMS', 'steps').split('\n')
        self.channels_bands = {}
        steps = Steps()
        self.step_dict = {}
        for step_name, step in steps.step_dict.iteritems():
            if step_name in self.steps:
                self.step_dict[step_name] = {'class': step}
                for param in step.parameters:
                    self.step_dict[step_name][param] = self.cfg.get(step_name,
                                                                    param)

        # get the post_processing parameters
        self.aux_source = self.cfg.get('GENERAL', 'aux_channel_source')
        self.n_groups = self.cfg.getint('GENERAL', 'n_groups')
        self.all_aux = self.cfg.getboolean('GENERAL', 'all_aux')
        self.aux = self.cfg.get('GENERAL', 'aux_channels').split(
            '\n') if self.cfg.get('GENERAL', 'aux_channels') else None
        self.excluded = self.cfg.get('GENERAL', 'exclude').split(
            '\n') if self.cfg.get('GENERAL', 'exclude') else None
        self.nav = self.cfg.getint('GENERAL', 'averages')
        self.group_dict = {}
        self.aux_dict = {}
        for group_n in xrange(self.n_groups):
            sctn = 'GROUP' + str(group_n + 1)
            self.group_dict[sctn] = {'channel': self.cfg.get(sctn, 'channel'),
                                     'units': self.cfg.get(sctn, 'units'),
                                     'bands': self.cfg.get(sctn, 'bands'),
                                     'aux': self.cfg.get(sctn,
                                                         'aux_channels').split(
                                         '\n') if self.cfg.get(sctn,
                                                               'aux_channels') else None,
                                     'excl': self.cfg.get(sctn,
                                                          'exclusions').split(
                                         '\n') if self.cfg.get(sctn,
                                                               'exclusions') else None
                                     }
        for _, g_dict in self.group_dict.iteritems():
            self.extractbands(g_dict)
        if self.cfg.has_section('RESULTS'):
            self.res_param = {}
            for option in self.cfg.options('RESULTS'):

                self.res_param[option] = self.cfg.get('RESULTS', option)
                # Try to convert them to integers
                # todo: is there a better way to check if they can be converted?
                try:
                    self.res_param[option] = int(self.res_param[option])
                except ValueError:
                    pass
Ejemplo n.º 17
0
def is_square(x):
    steps = Steps()
    steps.add_sqrt(x)
    return x >= 0 and int(math.sqrt(x)) == math.sqrt(x), steps
Ejemplo n.º 18
0
def pollards_pminus1(N, B=None, steps=None):
    if steps == None:
        steps = Steps()
    if B == None:
        steps.add_cuberoot(N)
        B = math.pow(N, 1.0 / 3.0)

    primes_list = get_primes()
    a, i = 2, 0
    a_set = set([])
    while True:
        pi = primes_list[i]
        if pi > B:
            break

        steps.add_log(B)
        steps.add_log(pi)
        e = int(math.floor(math.log(B) / math.log(pi)))

        steps.add_exp(pi, e)
        f = pi**e

        steps.add_exp(a, f)
        steps.add_mod()
        a_hold = powering.power_mod(a, f, N)  #(a**f) % N

        if a_hold == 0:
            break
        a = a_hold
        a_set.add(a)
        i += 1

    for a in a_set:
        g, g_steps = gcd(a - 1, N)
        steps.append(g_steps)
        if 1 < g and g < N:
            return g, N / g, steps
    return None, None, steps
 def ReadPrevious(self, file_path):
     with open(file_path, "r") as bin:
         return Steps.fromString(bin.readline())
Ejemplo n.º 20
0
def time_algos(prime_digits, num_iter=10, file_name_ending="results.p"):
    # Loading Old Data if Any
    f_algo_steps, h_algo_steps, lf_algo_steps, pr_algo_steps, p1_algo_steps = AlgoSteps(
    ), AlgoSteps(), AlgoSteps(), AlgoSteps(), AlgoSteps()
    if os.path.isfile("results/f_%s" % file_name_ending):
        f_algo_steps = pickle.load(
            open("results/f_%s", "rb" % file_name_ending))
    if os.path.isfile("results/h_%s" % file_name_ending):
        h_algo_steps = pickle.load(
            open("results/h_%s", "rb" % file_name_ending))
    if os.path.isfile("results/lf_%s" % file_name_ending):
        lf_algo_steps = pickle.load(
            open("results/lf_%s", "rb" % file_name_ending))
    if os.path.isfile("results/pr_%s" % file_name_ending):
        pr_algo_steps = pickle.load(
            open("results/pr_%s", "rb" % file_name_ending))
    if os.path.isfile("results/p1_%s" % file_name_ending):
        p1_algo_steps = pickle.load(
            open("results/p1_%s", "rb" % file_name_ending))

    for i in xrange(0, num_iter):
        # Number Gen
        p1, _ = prime_generators.prime_gen1(prime_digits)
        p2, _ = prime_generators.prime_gen1(prime_digits)
        my_prime = p1 * p2
        print "%d, %d" % (p1, p2)

        # Factoring (into 2, not whole)
        f_res1, f_res2, f_steps = algos.fermats_diff_of_squares(my_prime)
        h_res1, h_res2, h_steps = algos.harts_one_line(my_prime)
        lf_res1, lf_res2, lf_steps = algos.lehmans_var_of_fermat(my_prime)
        pr_res1, pr_res2, pr_steps = algos.pollards_rho(my_prime)
        p1_res1, p1_res2, p1_steps = algos.pollards_pminus1(my_prime)

        # Checking for Errors
        if f_res1 != p1 and f_res2 != p1:
            raise Exception(
                "fermat problem, should get %d, %d, but got %d, %d" %
                (p1, p2, f_res1, f_res2))
        if h_res1 != p1 and h_res2 != p1:
            raise Exception(
                "harts problem, should get %d, %d, but got %d, %d" %
                (p1, p2, h_res1, h_res2))
        if lf_res1 != p1 and lf_res2 != p1:
            raise Exception(
                "lehmans var problem, should get %d, %d, but got %d, %d" %
                (p1, p2, lf_res1, lf_res2))
        if pr_res1 != p1 and pr_res2 != p1:
            raise Exception(
                "pollards rho problem, should get %d, %d, but got %d, %d" %
                (p1, p2, pr_res1, pr_res2))
        if p1_res1 == None:
            p1_steps = Steps()
        elif p1_res1 != p1 and p1_res2 != p1:
            raise Exception(
                "pollards pminus1, should get %d, %d, but got %d, %d" %
                (p1, p2, p1_res1, p1_res2))

        print str(f_steps)
        print pr_steps
        #Saving Steps
        f_algo_steps.add(my_prime, [p1, p2], f_steps)
        h_algo_steps.add(my_prime, [p1, p2], h_steps)
        lf_algo_steps.add(my_prime, [p1, p2], lf_steps)
        pr_algo_steps.add(my_prime, [p1, p2], pr_steps)
        p1_algo_steps.add(my_prime, [p1, p2], p1_steps)

    # Dumping New Data
    pickle.dump(f_algo_steps, open("results/f_%s" % file_name_ending, "wb"))
    pickle.dump(h_algo_steps, open("results/h_%s" % file_name_ending, "wb"))
    pickle.dump(lf_algo_steps, open("results/lf_%s" % file_name_ending, "wb"))
    pickle.dump(pr_algo_steps, open("results/pr_%s" % file_name_ending, "wb"))
    pickle.dump(p1_algo_steps, open("results/p1_%s" % file_name_ending, "wb"))