Example #1
0
 def __mul__(self, other):
     if isinstance(other, int):
         if other < 0:
             raise ValueError("can only multiply ordinals and positive integers")
         elif other == 0:
             return 0
         else:
             terms = self._multiply_by_integer(other)
             return Ordinal(terms.terms)
     else:
         s_lead_term = self.terms[0]
         terms = []
         indexes = [getattr(x, "index", -1) for x in s_lead_term]
         for o_term in other.terms:
             try:
                 n = hi_lo_bisect_right(indexes, o_term[0].index)
                 if indexes[n - 1] == o_term[0].index:
                     new_stack = OrdinalStack.add_stack_powers(s_lead_term[n - 1], o_term[0])
                     term = s_lead_term[: n - 1] + [new_stack] + o_term[1:]
                 else:
                     term = s_lead_term[:n] + o_term
                 terms.append(term)
             except AttributeError:
                 # no .index attribute so we have an integer
                 term = self._multiply_by_integer(o_term[0])
                 terms.extend(term.terms)
         return Ordinal(terms)
Example #2
0
    def __add__(self, other):
        if type(other) is int:
            if other < 0:
                raise ValueError("can only add positive integers to ordinal")
            terms = self.terms[:]
            if self.is_successor:
                terms[-1][0] += other
            else:
                terms.append([other])
            return Ordinal(terms)
        else:
            s_terms_no_mult = [term[:-1] for term in self.terms]
            o_lead_term_no_mult = other.terms[0][:-1]
            o_lead_term_mult = other.terms[0][-1]

            n = hi_lo_bisect_right(s_terms_no_mult, o_lead_term_no_mult)

            if s_terms_no_mult[n - 1] == o_lead_term_no_mult:
                s_last_term_mult = self.terms[n - 1][-1]
                if type(s_last_term_mult) is int and type(o_lead_term_mult) is int:
                    s_terms = copy.deepcopy(self.terms[:n])
                    s_terms[n - 1][-1] += o_lead_term_mult
                    terms = s_terms + other.terms[1:]
                elif a < o_lead_term_mult:
                    s_terms = copy.deepcopy(self.terms[: n - 1])
                    terms = s_terms + other.terms
                elif a > o_lead_term_mult:
                    s_terms = copy.deepcopy(self.terms[:n])
                    terms = s_terms + other.terms[1:]
            else:
                s_terms = self.terms[:n]
                o_terms = other.terms
                terms = s_terms + o_terms
            return Ordinal(terms)