Example #1
0
 def get_visible_cells(self, index, row=True):
     result = []
     if row and index in self.visible_rows():
         result = cartesian_product(self.visible_columns(), [index])
     elif not row and index in self.visible_columns():
         result = cartesian_product([index], self.visible_rows())
     result = map(lambda t: t[0] + str(t[1]), result)
     return result
Example #2
0
def _get_himawari(timestamp, zoom, product, rangex, rangey, boundaries, crop):
    sat_urls = {(x, y): _get_product_url(timestamp, zoom, product, x, y)
                for x, y in cartesian_product(rangex, rangey)}
    sat_img = stitch(sat_urls, 'RGB')

    if boundaries:
        coastline_urls = {(x, y): _get_coastline_url(zoom, product, x, y)
                          for x, y in cartesian_product(rangex, rangey)}
        coastline_img = stitch(coastline_urls, 'RGBA')
        sat_img = overlay(sat_img, coastline_img)

    postprocessor = NICTPostProcessor(sat_img, timestamp)
    if crop:
        postprocessor.crop_relative(*crop)
    return postprocessor
Example #3
0
        def __init__(self, truth_problem, **kwargs):
            # Call to parent
            EllipticCoerciveReducedProblem_DerivedClass.__init__(
                self, truth_problem, **kwargs)

            # Copy of greedy snapshots
            self.snapshots_mu = GreedySelectedParametersList(
            )  # the difference between this list and greedy_selected_parameters in the reduction method is that this one also stores the initial parameter
            self.snapshots = SnapshotsMatrix(truth_problem.V)

            # Extend allowed keywords argument in solve
            self._online_solve_default_kwargs["online_rectification"] = True
            self.OnlineSolveKwargs = OnlineSolveKwargsGenerator(
                **self._online_solve_default_kwargs)

            # Generate all combinations of allowed keyword arguments in solve
            online_solve_kwargs_with_rectification = list()
            online_solve_kwargs_without_rectification = list()
            for other_args in cartesian_product(
                (True, False),
                    repeat=len(self._online_solve_default_kwargs) - 1):
                args_with_rectification = self.OnlineSolveKwargs(*(other_args +
                                                                   (True, )))
                args_without_rectification = self.OnlineSolveKwargs(
                    *(other_args + (False, )))
                online_solve_kwargs_with_rectification.append(
                    args_with_rectification)
                online_solve_kwargs_without_rectification.append(
                    args_without_rectification)
            self.online_solve_kwargs_with_rectification = online_solve_kwargs_with_rectification
            self.online_solve_kwargs_without_rectification = online_solve_kwargs_without_rectification

            # Flag to disable error estimation after rectification has been setup
            self._disable_error_estimation = False
def measure_performance(block_collection, ground_truth):
    print(f"Dataset 1 has {len(dataset1)} entities.")
    print(f"Dataset 2 has {len(dataset2)} entities.")
    comparisons = []
    for block in block_collection:
        inner_block_1 = []
        inner_block_2 = []
        for entity in block_collection[block]:
            if entity[0] == 1:
                inner_block_1.append(entity)
            else:
                inner_block_2.append(entity)
        comparisons.append(cartesian_product(inner_block_1, inner_block_2))

    print("Ground truth (duplicates):", len(ground_truth))

    allcomps = [comp for comparison in comparisons for comp in comparison]
    print("Suggested comparisons:", len(allcomps))
    print("Reduction Ratio: 1 - (", len(allcomps), "/",
          len(dataset1) * len(dataset2), ") =",
          (1 - (len(allcomps) / (len(dataset1) * len(dataset2)))) * 100, "%")

    correct = 0
    for duplicate in ground_truth:
        if tuple(duplicate) in allcomps:
            correct += 1
    print("Duplicates found (PC):", correct, "/", len(ground_truth), "=",
          (correct / len(ground_truth)) * 100, "%")
    print("Precision (PQ):", correct, "/", len(allcomps), "=",
          (correct / len(allcomps)) * 100, "%")
Example #5
0
def generate_tactus_table(model, rhythm):
    print('generating tactus analyses.')
    probabilities, analyses = {}, {}

    for begin, end, _, _ in model.tactus_spans(len(rhythm)):
        overflow = end - len(rhythm)
        padding = (- begin - 1 if begin < 0 else 0)
        left = (0 if begin < 0 else begin + 1)
        if overflow < 0: 
            overflow = 0
        exerpt = (False, ) * padding + rhythm[left:end] + overflow * (False, )
        interval = end - begin
        for subdivision in model.subdivisions[0]:
            #print('[%d to %d)\tsubdivided by %d.' % (end-interval, end, subdivision))
            beat_dists = [model.beat_location(interval, subdivision, b) for b in range(1, subdivision)]
            # All possible sequences of beat locations
            beat_locations = list(cartesian_product(*(d.symbols for d in beat_dists)))
            beats_prob_f = lambda beats: sum([np.log(d.probability(b)) for b, d in zip(beats, beat_dists)])
            probability_f = lambda beats: beats_prob_f(beats) + interval_probability(model, exerpt, beats)
            # Find and store the best analysis
            i =  argmax_index(beat_locations, probability_f)
            analyses[subdivision, begin, end] = beat_locations[i]
            probabilities[subdivision, begin, end] = probability_f(beat_locations[i])

    return probabilities, analyses
Example #6
0
def alpha_enum():
    letters = list(ascii_lowercase)
    digits = 1
    while True:
        for i in cartesian_product(*[letters for _ in range(digits)]):
            yield "".join(i)
        digits += 1
Example #7
0
def tabulate(func, num_args=None):
    if num_args is None:
        num_args = func.__code__.co_argcount
    return {
        args
        for args in cartesian_product(*(num_args * [(0, 1)])) if func(*args)
    }
Example #8
0
 def __call__(self, thetas, operators, thetas2):
     order = operators.order()
     first_operator = None
     assert order in (1, 2)
     if order == 1: # vector storage of affine expansion online data structures (e.g. reduced matrix/vector expansions)
         first_operator = operators[0]
         assert isinstance(first_operator, (backend.Matrix.Type(), backend.Vector.Type(), backend.Function.Type(), Number))
         assert thetas2 is None
         assert len(thetas) == len(operators)
         for (index, (theta, operator)) in enumerate(zip(thetas, operators)):
             if index == 0:
                 output = theta*operator
             elif theta != 0.:
                 output += theta*operator
     elif order == 2: # matrix storage of affine expansion online data structures (e.g. error estimation ff/af/aa products)
         first_operator = operators[0, 0]
         assert isinstance(first_operator, (backend.Matrix.Type(), backend.Vector.Type(), Number))
         assert thetas2 is not None
         # no checks here on the first dimension of operators should be equal to len(thetas), and
         # similarly that the second dimension should be equal to len(thetas2), because the
         # current operator interface does not provide a 2D len method
         for (i, j) in cartesian_product(range(len(thetas)), range(len(thetas2))):
             if i == 0 and j == 0:
                 output = thetas[0]*operators[0, 0]*thetas2[0]
             elif thetas[i] != 0. and thetas2[j] != 0.:
                 output += thetas[i]*operators[i, j]*thetas2[j]
     else:
         raise ValueError("product(): invalid operands.")
     # Return
     return ProductOutput(output)
Example #9
0
 def _compute_theta_EIM(self, term):
     original_thetas = ParametrizedDifferentialProblem_DerivedClass.compute_theta(
         self, term)
     eim_thetas = list()
     assert len(self.separated_forms[term]) == len(original_thetas)
     if self._N_EIM is not None:
         assert term in self._N_EIM
         assert len(self.separated_forms[term]) == len(
             self._N_EIM[term])
     for (q, (form, original_theta)) in enumerate(
             zip(self.separated_forms[term], original_thetas)):
         # Append coefficients computed with EIM, if applicable
         for addend in form.coefficients:
             eim_thetas__list = list()
             for factor in addend:
                 N_EIM = None
                 if self._N_EIM is not None:
                     N_EIM = self._N_EIM[term][q]
                 eim_thetas__list.append(
                     self.EIM_approximations[factor].
                     compute_interpolated_theta(N_EIM))
             eim_thetas__cartesian_product = cartesian_product(
                 *eim_thetas__list)
             for tuple_ in eim_thetas__cartesian_product:
                 eim_thetas_tuple = original_thetas[q]
                 for eim_thata_factor in tuple_:
                     eim_thetas_tuple *= eim_thata_factor
                 eim_thetas.append(eim_thetas_tuple)
         # Append coefficients which did not require EIM, if applicable
         for _ in form.unchanged_forms:
             eim_thetas.append(original_theta)
     return tuple(eim_thetas)
    def basis_state(self, index_or_label):
        """Return the basis state with the given index or label.

        Raises:
            .BasisNotSetError: if the Hilbert space has no defined basis
            IndexError: if there is no basis state with the given index
            KeyError: if there is not basis state with the given label
        """
        from qalgebra.core.state_algebra import BasisKet, TensorKet

        if isinstance(index_or_label, int):  # index
            ls_bases = [ls.basis_labels for ls in self.local_factors]
            label_tuple = list(cartesian_product(*ls_bases))[index_or_label]
            try:
                return TensorKet(*[
                    BasisKet(label, hs=ls)
                    for (ls, label) in zip(self.local_factors, label_tuple)
                ])
            except ValueError as exc_info:
                raise IndexError(str(exc_info))
        else:  # label
            local_labels = index_or_label.split(",")
            if len(local_labels) != len(self.local_factors):
                raise KeyError(
                    "label %s for Hilbert space %s must be comma-separated "
                    "concatenation of local labels" % (index_or_label, self))
            try:
                return TensorKet(*[
                    BasisKet(label, hs=ls)
                    for (ls, label) in zip(self.local_factors, local_labels)
                ])
            except ValueError as exc_info:
                raise KeyError(str(exc_info))
Example #11
0
    def return_six_healthy_prods(self, search_term):

        # i take the result of the search
        initial_product = self.search_product(search_term)
        categories_list = []
        healthy_products_list = []
        nutriscores = ['a', 'b', 'c']
        # if a have a result
        if initial_product:
            # i take the categories in hierachy's reversed order
            # and put all that in a list
            for category in reversed(initial_product['categories_hierarchy']):
                cat_name = category.split(':')[-1]
                categories_list.append(cat_name.replace('-', ' '))

            for score, category in cartesian_product(nutriscores,
                                                     categories_list):

                for product in self.search_by_cat_and_score(score, category):
                    if self.check_has_needed_infos(product):
                        cleaned_prod = self.clean_prod_info(product)

                        if cleaned_prod:
                            healthy_products_list.append(cleaned_prod)
                        if len(healthy_products_list) > 5:
                            break

                if len(healthy_products_list) > 5:
                    break

            return healthy_products_list

        # if not result return false
        else:
            return []
Example #12
0
def plot_confusion_matrix(cm, classes=['Loan Not Approved','Loan Approved'],
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in cartesian_product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt),
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
Example #13
0
def combinations_with_replacement(iterable, r):
    """combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"""
    pool = tuple(iterable)
    n = len(pool)
    for indices in cartesian_product(range(n), repeat=r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)
Example #14
0
 def cell_span(self, i):
     cell_span_list = []
     indx = np.unravel_index(i, self.shape)
     output = []
     cell_for_axis = [ self.space_list[j].cell_span(indx[j]) for j in range(len(indx))]
     for multi_indx in cartesian_product(*cell_for_axis):
         output.append(np.ravel_multi_index(multi_indx, self.shape))
     return output
Example #15
0
 def cell_span(self, i):
     cell_span_list = []
     indx = np.unravel_index(i, self.shape)
     output = []
     cell_for_axis = [ self.space_list[j].cell_span(indx[j]) for j in range(len(indx))]
     for multi_indx in cartesian_product(*cell_for_axis):
         output.append(np.ravel_multi_index(multi_indx, self.shape))
     return output
Example #16
0
 def all_painters(self, m: Model) -> Iterable[RPQP]:
     return (
         RPQP(rp, qp)
             for rp, qp in cartesian_product(
                 self.rpainters, m.wsoup.qpainters
             )
                 if rp.is_match(qp)
     )
Example #17
0
def test_stitch_limit_number_of_tiles(load):
    load.side_effect = _dummy_load()

    too_many_tiles = {(x, y): 'http://dummy.com/({}_{}).png'.format(x, y)
                      for x, y in cartesian_product(range(10), range(5))}

    with pytest.raises(StitchException):
        stitch(too_many_tiles, 'RGB')
Example #18
0
def test_stitch_with_no_tiles(load):
    load.side_effect = lambda paths: dict()

    dummy_paths = {(x, y): 'http://dummy.com/({}_{}).png'.format(x, y)
                   for x, y in cartesian_product(range(0, 3), range(0, 4))}

    with pytest.raises(StitchException):
        stitch(dummy_paths, 'RGB')
Example #19
0
 def truncatable_get_primes():
     for ndigits in count(2):
         digit_groups = [[2, 3, 5, 7]] + [[1, 3, 7, 9]]*(ndigits-2) + [[3, 7]]
         for ds in cartesian_product(*digit_groups):
             x = num_from_digits(ds)
             if is_prime(x) and all(is_prime(num_from_digits(ds[n:])) and
                     is_prime(num_from_digits(ds[:-n])) for n in range(1, len(ds))):
                 yield x
def similarity(names1, names2):
    df = pd.DataFrame(columns=[0, 1, 2])
    row = 0
    for name1, name2 in cartesian_product(names1, names2):
        sim = SequenceMatcher(None, name1, name2).ratio()
        df.loc[row] = [name1, name2, sim]
        row += 1

    return df
Example #21
0
def divisors(n):
    '''divisors(number) -> list

    Positive divisors of the number, e.g. divisors(15) -> [1, 3, 5, 15]'''

    if not n:
        return []
    factor_set = ({p**i for i in range(k+1)} for p, k in group(factorized(n)))
    return sorted(product(c) for c in cartesian_product(*factor_set))
Example #22
0
 def _expand(self):
     k, b = self.ket, self.bra.ket
     be, ke = b.expand(), k.expand()
     kesummands = ke.operands if isinstance(ke, KetPlus) else (ke, )
     besummands = be.operands if isinstance(be, KetPlus) else (be, )
     res_summands = []
     for (k, b) in cartesian_product(kesummands, besummands):
         res_summands.append(KetBra.create(k, b))
     return OperatorPlus.create(*res_summands)
Example #23
0
def problem35():
    """How many circular primes are there below one million?"""
    def is_circular_prime(digits):
        return all(is_prime(num_from_digits(digits[r:] + digits[:r])) 
            for r in xrange(len(digits)))
    # We will use only 4 digits (1, 3, 7, and 9) to generate candidates, so we 
    # must consider the four one-digit primes separately.
    circular_primes = (num_from_digits(ds) for n in xrange(2, 6+1) 
        for ds in cartesian_product([1, 3, 7, 9], repeat=n) if is_circular_prime(ds))
    return ilen(chain([2, 3, 5, 7], circular_primes))
Example #24
0
def test_stitch_some_missing_tiles(load):
    load.side_effect = _dummy_load(exclude_tiles=((0, 0), (1, 2)))

    dummy_paths = {(x, y): 'http://dummy.com/({}_{}).png'.format(x, y)
                   for x, y in cartesian_product(range(0, 3), range(0, 4))}

    actual = stitch(dummy_paths, 'RGB')
    expected = Image.open(
        path_of_test_resource('imgs/missingtiles_stitch_result.jpg'), 'r')
    return actual, expected
def default_channel_ops(nqubits):
    """
    Generate the tomographic pre- and post-rotations of any number of qubits as qutip operators.

    :param int nqubits: The number of qubits to perform tomography on.
    :return: Qutip object corresponding to the tomographic rotation.
    :rtype: Qobj
    """
    for gates in cartesian_product(TOMOGRAPHY_GATES.values(), repeat=nqubits):
        yield qt.tensor(*gates)
Example #26
0
def divisors(n):
    '''divisors(number) -> list

    Positive divisors of the number, e.g. divisors(15) -> [1, 3, 5, 15]'''

    if not n:
        return []
    factor_set = ({p**i
                   for i in range(k + 1)} for p, k in group(factorized(n)))
    return sorted(product(c) for c in cartesian_product(*factor_set))
Example #27
0
    def __composeIgnoringEpsilon(self, other):  # イプシロン遷移を考慮しない
        prod_init_states = set(
            cartesian_product(self.init_states, other.init_states))
        prod_final_states = set(
            cartesian_product(self.final_states, other.final_states))
        states = copy.deepcopy(prod_init_states)
        init_states = set()
        final_states = set()
        transitions = []
        init_weights = {}
        final_weights = {}
        queue = copy.deepcopy(list(prod_init_states))

        while not queue == []:
            q = queue.pop(0)
            q1, q2 = q
            if q in prod_init_states:
                init_states.add(q)
                init_weights[
                    q] = self.init_weights[q1] * other.init_weights[q2]
            if q in prod_final_states:
                final_states.add(q)
                final_weights[
                    q] = self.final_weights[q1] * other.final_weights[q2]

            for arc1 in self.__arcsFrom(q1):
                for arc2 in self.__arcsFrom(q2):
                    (prev_q1, i1, o1, w1, next_q1) = arc1
                    (prev_q2, i2, o2, w2, next_q2) = arc2
                    if not o1 == i2:
                        continue
                    prev_q = (prev_q1, prev_q2)
                    next_q = (next_q1, next_q2)
                    transitions.append((prev_q, i1, o2, w1 * w2, next_q))
                    if next_q not in states:
                        states.add(next_q)
                        queue.append(next_q)

        return WeightedFiniteStateTransducer(self.in_alphabet,
                                             other.out_alphabet, states,
                                             init_states, final_states,
                                             transitions, init_weights,
                                             final_weights)
Example #28
0
def test_stitch_inmemory(load):
    load.side_effect = _dummy_load()

    dummy_paths = {(x, y): 'http://dummy.com/({}_{}).png'.format(x, y)
                   for x, y in cartesian_product(range(2, 5), range(3, 7))}

    actual = stitch(dummy_paths, 'RGB')
    expected = Image.open(
        path_of_test_resource('imgs/happypath_stitch_result.jpg'), 'r')
    return actual, expected
Example #29
0
def test_stitch_tempdir_intermediate(save):
    save.side_effect = _dummy_save()

    dummy_paths = {(x, y): 'http://dummy.com/({}_{}).png'.format(x, y)
                   for x, y in cartesian_product(range(2, 5), range(3, 7))}

    actual = stitch(dummy_paths, 'RGB', tempfiles=True)
    expected = Image.open(
        path_of_test_resource('imgs/happypath_stitch_result.jpg'), 'r')
    return actual, expected
Example #30
0
    def _create_variables(self) -> None:
        """Creates the set of variables Y* and X* of the PuLP problem.

        Override it if you need to create extra variables (first use
        ``super().create_variables()`` to call the base class method)."""
        if self.relaxed:
            kind = LpContinuous
        else:
            kind = LpInteger

        # List all combinations of apps and instances and workloads
        comb_res = cartesian_product(self.system.apps,
                                     self.cooked.instances_res)
        comb_dem = cartesian_product(self.system.apps,
                                     self.cooked.instances_dem,
                                     self.load_hist.keys())
        map_res = LpVariable.dicts("Y", comb_res, 0, None, kind)
        map_dem = LpVariable.dicts("X", comb_dem, 0, None, kind)
        self.cooked = self.cooked._replace(map_res=map_res, map_dem=map_dem)
Example #31
0
def test_stitch_with_missing_column(load):
    exclude = [(0, i) for i in range(4)]
    load.side_effect = _dummy_load(exclude_tiles=exclude)

    dummy_paths = {(x, y): 'http://dummy.com/({}_{}).png'.format(x, y)
                   for x, y in cartesian_product(range(0, 3), range(0, 4))}

    actual = stitch(dummy_paths, 'RGB')
    expected = Image.open(
        path_of_test_resource('imgs/missingcolumn_stitch_result.jpg'), 'r')
    return actual, expected
Example #32
0
def default_rotations(*qubits):
    """
    Generates the Quil programs for the tomographic pre- and post-rotations of any number of qubits.

    :param list qubits: A list of qubits to perform tomography on.
    """
    for gates in cartesian_product(TOMOGRAPHY_GATES.keys(), repeat=len(qubits)):
        tomography_program = Program()
        for qubit, gate in izip(qubits, gates):
            tomography_program.inst(gate(qubit))
        yield tomography_program
Example #33
0
def DuoBA_from_ltls(hard_spec, soft_spec):
    hard_buchi = buchi_from_ltl(hard_spec, 'hard_buchi')
    soft_buchi = buchi_from_ltl(soft_spec, 'soft_buchi')
    hard_symbols = hard_buchi.graph['symbols']
    soft_symbols = soft_buchi.graph['symbols']
    symbols = set(hard_symbols).union(set(soft_symbols))
    DuoBA = DiGraph(type='safe_buchi', hard=hard_buchi, soft=soft_buchi, symols=symbols)
    initial = set()
    accept = set()
    for (h_node, s_node, l) in cartesian_product(hard_buchi.nodes(), soft_buchi.nodes(), [1, 2]):
        DuoNode = (h_node, s_node, l)
        DuoBA.add_node(DuoNode,hard=h_node, soft=s_node, level=l)
        if (h_node in hard_buchi.graph['initial'] and 
            s_node in soft_buchi.graph['initial'] and l == 1):
            initial.add(DuoNode)
        if (h_node in hard_buchi.graph['accept'] and l == 1):
            accept.add(DuoNode)
    DuoBA.graph['accept'] = accept
    DuoBA.graph['initial'] = initial
    for f_duonode in DuoBA.nodes():
        for t_duonode in DuoBA.nodes():
            f_h_node, f_s_node, f_level = check_duo_attr(DuoBA, f_duonode)
            t_h_node, t_s_node, t_level = check_duo_attr(DuoBA, t_duonode)
            if (t_h_node not in DuoBA.graph['hard'].neighbors(f_h_node) or 
                t_s_node not in DuoBA.graph['soft'].neighbors(f_s_node)):
                continue
                # relaxed because no common input alphabets are enabled
            hardguard = DuoBA.graph['hard'].edges[f_h_node,t_h_node]['guard']
            softguard = DuoBA.graph['soft'].edges[f_s_node,t_s_node]['guard']
            if ((f_h_node not in DuoBA.graph['hard'].graph['accept'] and 
                f_level == 1 and t_level == 1) or 
                (f_h_node in DuoBA.graph['hard'].graph['accept'] and 
                f_level == 1 and t_level == 2) or 
                (f_s_node not in DuoBA.graph['soft'].graph['accept'] and 
                f_level == 2 and t_level == 2) or 
                (f_s_node in DuoBA.graph['soft'].graph['accept'] and 
                f_level == 2 and t_level == 1)):
                DuoBA.add_edge(f_duonode, t_duonode, hardguard=hardguard, softguard=softguard)
    return DuoBA
Example #34
0
def analyze_composite_tau(n, x, component_taus):
     '''Helper to test_composite_tau(). Given an odd number n and a target tau(n) together with
     a list of the specific tau(p) to be assumed for each prime in n, test if
     the implied conditions on p_i mod 2^{x_i+1} are compatible with n. If such a
     compatibility is possible, return it, else return an empty value.'''
     # First, ignore the even part of n
     b = beta(n)
     n >>= b
     # First we create the list of conditions on the component primes. We use the
     # criterion tau(p) = x <=> p == 2^x-1 (mod 2^(x+1))
     # We store the conditions as a list of (residue, modulus) conditions
     conditions = [((1<<x) - 1, 1<<(x+1)) for x in component_taus]
     # Now what we do is combine all the conditions modulo the largest modulus, which
     # we can do since the moduli are all multiples of each other.
     m = max(conditions, key=lambda cond: cond[1])[1]
     promoted_rs = []
     for ri, mi in conditions:
          q, r = divmod(m, mi)
          if r != 0:
               raise ValueError("Moduli don't divide! {} {}".format(m, mi))
          promoted_rs.append([(ri+i*mi) for i in range(q)])
     out = []
     actual_r = n % m
     # Now we take all possible combinations of the possible individual residues, making
     # sure to eliminate duplicate conditions (e.g. [1, 3, 5] mod 8 is the same as [3, 1, 5])
     # Now even after this de-duplication, some unique combos may have the same multiplied
     # residue mod m, e.g. [3, 5] and [1, 7] (mod 8), but they're still separate possibilities
     # on each constituent prime
     all_combos = {tuple(sorted(rs)) for rs in cartesian_product(*promoted_rs)}
     for rs in all_combos:
          r = 1
          for ri in rs:
               r = (r*ri)%m
          if r == actual_r:
               out.append(rs)
     if out:
          return out, actual_r, m, component_taus
     else:
          return []
Example #35
0
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
'''
from __future__ import division
from fractions import Fraction
from itertools import product as cartesian_product
from operator import mul


def product(xs):
    return reduce(mul, xs)


if __name__ == '__main__':
    res = []
    for i, j, k in cartesian_product(range(1, 10), repeat=3):
        d, n = (i * 10 + j, j * 10 + k)
        if d / n == i / k:
            res.append(Fraction(d, n))
    print Fraction(product(res)).denominator

Example #36
0
def binary_nums(n):
    "Return iterator over all n-digit binary numbers"
    return cartesian_product([0, 1], repeat=n)
Example #37
0
 def get_numbers(rows):
     """Return groups of "columns" numbers, following all possible ways."""
     for moves in cartesian_product([0, +1], repeat=len(rows)-1):
         indexes = ireduce(operator.add, moves, 0)
         yield (row[index] for (row, index) in izip(rows, indexes))
Example #38
0
 def get_multiplicands(ndigits1, ndigits2):
     return cartesian_product(get_permutation(ndigits1), get_permutation(ndigits2))
Example #39
0
 def count_digits():
     """Like itertools.count, but returns digits instead. Starts at 1"""
     for nd in count(1):
         for digits in cartesian_product(*([range(1, 10)] + [range(10)]*(nd-1))):
             yield digits
Example #40
0
def ncombinations(n, k):
    """Combinations of k elements from a group of n"""
    return cartesian_product(xrange(n-k+1, n+1)) / factorial(k)
Example #41
0
def divisors(n):
    """Return all divisors of n: divisors(12) -> 1,2,3,6,12"""
    all_factors = [[f**p for p in range(fp+1)] for (f, fp) in factorize(n)]
    return (product(ns) for ns in cartesian_product(*all_factors))