def dist(v, w): """Returns the distance between 'v' and 'w'""" if not iterable(v) or not iterable(w) or len(v) != len(w): raise TypeError('Vectors must be iterable, of length 2') u = Vector(v) - Vector(w) return u.norm()
def __rmul__(self, c): """Represents a scalar multiplication""" if iterable(c): return self * c else: return Vector([c * coord for coord in self.coords])
def __add__(self, w): if iterable(w) and len(w) == len(self): coords = [] for i in range(len(self)): coords.append(self[i] + w[i]) return Vector(coords)
def right_perpendicular(vector): """Returns a vector perpendicular to and pointing to the right of a given vector""" if not iterable(vector) or len(vector) != 2: raise TypeError("Argument 'vector' must be iterable, of length 2") return Vector((vector[1], -vector[0]))
def __mul__(self, w): """Returns a dot product""" if iterable(w) and len(self) == len(w): result = 0 for i in range(len(self)): result += self[i] * w[i] return result else: return Vector([c * coord for coord in self.coords])
def __init__(self, vertices, edges=None): #Do everything needed to create an ordinary graph Graph.__init__(self, vertices, edges) #Check if vertices are points on a surface if len(vertices) >= 1: v = vertices[0] if not iterable(v) and len(v) != 2: raise ValueError( """Vertices (elements of 'vertices' argument) must be iterable, of length 2""")
def find_circles(v, w, radius, type='open'): """Returns a tuple containing two circles of given radius, such that 'v' and 'w' are contained in their edges The circles are closed if argument 'type' equals to '"closed"', open if it eqals to '"open"'""" if not iterable(v) or not iterable(w) or len(v) != len(w): raise TypeError('Vectors must be iterable, of length 2') try: float(radius) if radius < 0: raise TypeError("Argument 'radius' must be greater or equal to 0") except TypeError: raise TypeError("Argument 'radius' must be a number") distance = dist(v, w) if distance > 2 * radius: return None #The following works because of the Pythagorean theorem pointer = (1 / 2) * (Vector(w) - Vector(v)) d = pointer.norm() h = sqrt(abs(radius**2 - (d**2))) #Theese are centers of returned circles center_left = Vector(v) + pointer + (h / d) * left_perpendicular(pointer) center_right = Vector(v) + pointer + (h / d) * right_perpendicular(pointer) if type == 'open': res_left = Circle(center_left, radius) res_right = Circle(center_right, radius) elif type == 'closed': res_left = ClosedCircle(center_left, radius) res_right = ClosedCircle(center_right, radius) return (res_left, res_right)
def __sub__(self, other): """Returns a graph that is formed by removing vertices given in 'other' argument""" if not iterable(other): other = [oher] vertices = self.vertices.copy() for v in other: vertices.remove(v) """If 'self' is an instance of a class that inherits from 'Graph', an instance of the same class will be returned""" return type(self)(vertices, self.edges)
def __init__(self, center, radius): if not iterable(center) or len(center) != 2: raise TypeError("Argument 'center' must be iterable, of legth 2") self.center = center try: self.radius = float(radius) if self.radius < 0: raise TypeError( "Argument 'radius' must be greater or equal to 0") except TypeError: raise TypeError("Argument 'radius' must be a number")
def _written(self, data, source): if not misc.iterable(data): raise TypeError('`data` should be iterable for splitting') if len(data) != len(self._sinks): raise ValueError('`data` size should match `sink` count') # compatibility with Sampled Nodes if ('channels' in self._params and self._params['channels'] != len(self._sinks)): raise ValueError('`channels` should match `sink` count') # mask Node behavior for sink, channelData in zip(self._sinks, data): sink.write(channelData, self)
def cond(self, edge): """This is a condition that must be satisfied by an edge to be included in creating a graph The condition will be modified in classes that inherit from this class """ if iterable(edge) and len(edge) == 2: ed_list = list(edge) if (ed_list[0] != ed_list[1] and ed_list[0] in self.vertices and ed_list[1] in self.vertices): return True else: return False else: return False
def __sub__(self, w): if iterable(w) and len(self) == len(w): return self + (-w) else: raise ValueError
def __iadd__(self, w): if iterable(w) and len(w) == len(self): return self + w else: raise ValueError
def __init__(self, coords): if iterable(coords, TypeError('coords must be iterable')): self.coords = tuple(coords)