Пример #1
0
def find_triangles(radius):
    radius2 = radius ** 2
    # generate points in circle, except origin
    r = range(-radius + 1, radius)
    points = set()
    for x in r:
        for y in r:
            if mag2(x, y) < radius2:
                points.add((x, y))
    points.remove((0,0))

    #print(points)
    triangles = set()
    for p1, p2 in all_pairs(points):
        # test if p1-p2 crosses the origin
        if cmp_line(p1, p2, (0,0)) == 0:
            continue
        # get the set of points p3 that is on the side of the line p1-(0,0)
        # farthest from p2, and likewise for p2-(0,0). each of these points
        # completes a triangle (p1, p2, p3) that contains the origin.
        #TODO prevent duplication of triangles, reducing reliance on sets
        p3_set = (partition(points, p1, (0,0), p2)[1] &
                  partition(points, p2, (0,0), p1)[1])
        for p3 in p3_set:
            triangles.add(frozenset((p1, p2, p3)))
    return len(triangles)
Пример #2
0
 def _cutout2(self, itemType, manager):
     if self.flagged:
         inner, outer = partition(lambda item: item.flagged, self)
         if itemType + 1 == self.itemType:
             cutSubitem = self.newSubitem()
             for subitem in inner:
                 subinner, subouter = partition(lambda item: item.flagged, subitem)
                 cutSubitem.addSubitems(subinner)
                 if subouter:
                     restSubitem = self.newSubitem()
                     restSubitem.addSubitems(subouter)
                     outer.append(restSubitem)
             outer.append(cutSubitem)
             self.setSubitems(outer, False, manager)
         elif itemType + 1 < self.itemType:
             if len(inner) == 1:
                 inner[0]._cutout2(itemType, manager)
             else:
                 newSubitem = self.newSubitem()
                 for subitem in inner:
                     newSubitem.addSubitems(subitem)
                     newSubitem.flagged += subitem.flagged
                 newSubitem._cutout2(itemType, manager)
                 outer.append(newSubitem)
                 self.setSubitems(outer, False, manager)
Пример #3
0
 def _cutout2(self, itemType, manager):
     if self.flagged:
         inner, outer = partition(lambda item: item.flagged, self)
         if itemType + 1 == self.itemType:
             cutSubitem = self.newSubitem()
             for subitem in inner:
                 subinner, subouter = partition(lambda item: item.flagged,
                                                subitem)
                 cutSubitem.addSubitems(subinner)
                 if subouter:
                     restSubitem = self.newSubitem()
                     restSubitem.addSubitems(subouter)
                     outer.append(restSubitem)
             outer.append(cutSubitem)
             self.setSubitems(outer, False, manager)
         elif itemType + 1 < self.itemType:
             if len(inner) == 1:
                 inner[0]._cutout2(itemType, manager)
             else:
                 newSubitem = self.newSubitem()
                 for subitem in inner:
                     newSubitem.addSubitems(subitem)
                     newSubitem.flagged += subitem.flagged
                 newSubitem._cutout2(itemType, manager)
                 outer.append(newSubitem)
                 self.setSubitems(outer, False, manager)
Пример #4
0
    def _cutout(self, itemType, cutItem, manager):
        if self.flagged:
            if itemType == self.itemType:
                inner, outer = partition(lambda item: item.flagged, self)
                cutItem.addSubitems(inner)
                self.setSubitems(outer, True, manager)
            elif itemType < self.itemType:
                newSubitems = []
                if cutItem is None:
                    if itemType + 1 == self.itemType or self.flagged > 1:
                        hints = [subitem for subitem in self if subitem.flagged]
                        it = cutSubitem = self.newSubitem(hints)
                        while it.itemType != itemType:
                            it.addSubitem(it.newSubitem(), False)
                            it = it[0]
                        newSubitems.append(cutSubitem)
                    else:
                        cutSubitem = None
                else:
                    cutSubitem = cutItem[0]

                for subitem in self:
                    subitem._cutout(itemType, cutSubitem, manager)
                    if subitem:
                        newSubitems.append(subitem)

                if cutItem is None and cutSubitem is not None:
                    def computeBounds(item):
                        if item.itemType != itemType:
                            for subitem in item:
                                computeBounds(subitem)
                            item.recomputeBounds()
                    computeBounds(cutSubitem)

                self.setSubitems(newSubitems, True, manager)
Пример #5
0
 def inner(fn, args):
     models, params = partition(is_model, args)
     return Model(
         fn,
         list(models)
         + [filter_for_required_args(fn, p, sanitize_label) for p in params],
     )
Пример #6
0
 def search(points):
     for a, b in all_cuts(points, size):
         left, right = partition(points, a, b)
         for c in search(left):
             yield c
         yield (a, b)
         for c in search(right):
             yield c
Пример #7
0
 def merge(self, itemType, manager):
     if itemType < self.itemType and self.flagged:
         inner, outer = partition(lambda item: item.flagged, self)
         if len(inner) == 1:
             inner[0].merge(itemType, manager)
         else:
             newSubitem = self.newSubitem(hints=inner)
             for subitem in inner:
                 newSubitem.addSubitems(subitem)
                 newSubitem.flagged += subitem.flagged
             newSubitem.merge(itemType, manager)
             outer.append(newSubitem)
             self.setSubitems(outer, False, manager)
Пример #8
0
 def merge(self, itemType, manager):
     if itemType < self.itemType and self.flagged:
         inner, outer = partition(lambda item: item.flagged, self)
         if len(inner) == 1:
             inner[0].merge(itemType, manager)
         else:
             newSubitem = self.newSubitem(hints=inner)
             for subitem in inner:
                 newSubitem.addSubitems(subitem)
                 newSubitem.flagged += subitem.flagged
             newSubitem.merge(itemType, manager)
             outer.append(newSubitem)
             self.setSubitems(outer, False, manager)
Пример #9
0
 def build_tree(self, D, questions, depth, t_col):
     """
     Recursion function of the decision tree. It figures out the decision nodes and leaf nodes.
 
     Parameters
     ----------
     D : numpy.array
         Dataset
     questions : list
         List of questions for each feature in the data set
     Returns
     -------
     Decision Node or Leaf Node
         Node of the tree.
 
     """
     # index of which question should be asked in order to split the tree
     split_col = 0
     max_gain = 0
     question = questions[0]
     for i in range(0, len(questions)):
         # calculate information gain for a certain feature
         gain = information_gain(D=D, question=questions[i], t_col=t_col)
         # TODO determine column where you want to make split
         # TODO assign that column to split_col variable
         if gain > max_gain:
             max_gain = gain
             split_col = i
     # end of the tree reached
     if max_gain == 0:
         return leaf_node(D=D, depth=depth, t_col=t_col)
     # analyze true and false examples
     self.depth += 1
     # grow tree in depth
     (true_rows, false_rows) = partition(D=D, question=questions[split_col])
     true_branch = self.build_tree(D=true_rows,
                                   questions=questions,
                                   depth=depth + 1,
                                   t_col=t_col)
     false_branch = self.build_tree(D=false_rows,
                                    questions=questions,
                                    depth=depth + 1,
                                    t_col=t_col)
     return decision_node(question=questions[split_col],
                          true_branch=true_branch,
                          false_branch=false_branch,
                          depth=depth,
                          D=D,
                          t_col=t_col)
Пример #10
0
    def _cutout(self, itemType, cutItem, manager):
        if self.flagged:
            if itemType == self.itemType:
                inner, outer = partition(lambda item: item.flagged, self)
                cutItem.addSubitems(inner)
                self.setSubitems(outer, True, manager)
            elif itemType < self.itemType:
                newSubitems = []
                if cutItem is None:
                    if itemType + 1 == self.itemType or self.flagged > 1:
                        hints = [
                            subitem for subitem in self if subitem.flagged
                        ]
                        it = cutSubitem = self.newSubitem(hints)
                        while it.itemType != itemType:
                            it.addSubitem(it.newSubitem(), False)
                            it = it[0]
                        newSubitems.append(cutSubitem)
                    else:
                        cutSubitem = None
                else:
                    cutSubitem = cutItem[0]

                for subitem in self:
                    subitem._cutout(itemType, cutSubitem, manager)
                    if subitem:
                        newSubitems.append(subitem)

                if cutItem is None and cutSubitem is not None:

                    def computeBounds(item):
                        if item.itemType != itemType:
                            for subitem in item:
                                computeBounds(subitem)
                            item.recomputeBounds()

                    computeBounds(cutSubitem)

                self.setSubitems(newSubitems, True, manager)