def draw_rectangle(width, height, color, line_width=2): glColor3f(*color) glLineWidth(line_width) glBegin(GL_LINE_LOOP) for coords in [[1, 1], [1, -1], [-1, -1], [-1, 1]]: glVertex2f(*shift_scale_point( vec.Vec([width / 2, height / 2]) * vec.Vec(coords))) glEnd()
def rvec(): import vec D = {'radio', 'sensor', 'memory', 'cpu'} v0 = vec.Vec(D, {'radio': 1, 'cpu': 3}) v1 = vec.Vec(D, {'sensor': 2, 'cpu': 4}) v2 = vec.Vec(D, {'memory': 3, 'cpu': 1}) VT = [v0, v1, v2] return VT
def as_vec(self): """ This function returns the value of the vector as a Vec class :return: Vec instance """ return vec.Vec(self.as_list())
def init(size, draw_origin=vec.Vec([0.0, 0.0, -15.0]), focal=4, stereo=True): this.size = size this.size = size this.width, this.height = size this.screen = pygame.display.set_mode( this.size, pygame.HWSURFACE | pygame.OPENGL | pygame.DOUBLEBUF) initGL() resize(*size)
def signum(u): """ input: a Vec u output: the Vec v with the same domain as u such that +1 if u[d] >= 0 v[d]= { -1 if u[d] < 0 """ return vec.Vec(u.D, {d: 1 if u[d] >= 0 else -1 for d in u.D})
def draw_points_2d(points, color, line_width=2): glColor3f(*color) #draw each point as a line with (almost) identical start and end points glLineWidth(line_width) for point in points: glBegin(GL_LINES) glVertex2f(*shift_scale_point(point)) glVertex2f(*(shift_scale_point(point) + vec.Vec([1, 0]))) glEnd()
def image2vector(image): D = set() F = {} for i in range(len(image)): row = image[i] for j in range(len(row)): pixel = row[j] coord = (i, j) D.add(coord) F[coord] = pixel return vec.Vec(D, F)
def draw_circle_2d(radius, color, n_points=60, line_width=2, draw_origin=vec.zero_vec(2)): glColor3f(*color) glLineWidth(line_width) glBegin(GL_LINE_LOOP) for i in range(n_points): x = math.cos(math.pi * 2 * i / n_points) * radius y = math.sin(math.pi * 2 * i / n_points) * radius p = vec.Vec([x, y]) + draw_origin glVertex2f(*shift_scale_point(p)) glEnd()
def draw_points_3d(points, color, draw_origin, draw_angles, line_width=2): gl.glLoadIdentity() # reset position gl.glTranslatef(*draw_origin) #origin of plotting gl.glRotatef(draw_angles[1], 1, 0, 0) gl.glRotatef(draw_angles[0], 0, 1, 0) glColor3f(*color) #draw each point as a line with (almost) identical start and end points glLineWidth(line_width) for point in points: glBegin(GL_LINES) glVertex3f(*point) glVertex3f(*(point + vec.Vec([0.01, 0, 0]))) glEnd()
def image2vec(image): """ Converts an image in list of lists format to a vector. Will work with either color or grayscale. """ if isgray(image): D = {(x, y) for x in range(len(image[0])) for y in range(len(image))} F = {(x, y): image[y][x] for (x, y) in D} else: D = {(x, y, c) for c in ['r', 'g', 'b'] for x in range(len(image[0])) for y in range(len(image))} F = dict() for y in range(len(image)): for x in range(len(image[y])): F[(x, y, 'r')] = image[y][x][0] F[(x, y, 'g')] = image[y][x][1] F[(x, y, 'b')] = image[y][x][2] return vec.Vec(D, F)
return cancer_data.read_training_data('inner_product/train.data') def signum(u): """ input: a Vec u output: the Vec v with the same domain as u such that +1 if u[d] >= 0 v[d]= { -1 if u[d] < 0 """ return vec.Vec(u.D, {d: 1 if u[d] >= 0 else -1 for d in u.D}) assert (signum(vec.Vec({'A', 'B'}, { 'A': 3, 'B': -2 })) == vec.Vec({'A', 'B'}, { 'A': 1, 'B': -1 })) def fraction_wrong(A, b, w): """ input: An R x C matrix A whose rows are feature vectors, an R-vector b whose entries are +1 and -1, and a C-vector w. output: The fraction of row labels r of A such that the sign of (row r of A) * w differs from that of b[r] """
for r in range(m) for c in range(n)}) def sps2sparseMat(M): S = M.tolil() elems = {(i, S.rows[i][j]): S.data[i][j] for i in range(len(S.data)) for j in range(len(S.data[i]))} return (sparse_Mat((set(range(S.shape[0])), set(range(S.shape[1]))), elems)) if __name__ == '__main__': a = np.random.randint(10, 20, (1000)) b = np.random.randint(10, 20, (1000)) c = vec.Vec(a.tolist()) d = vec.Vec(b.tolist()) add_numpy(a, b) add_vec(c, d) M = np.random.randint(10, 20, (30, 10)) N = np.random.randint(10, 20, (10, 30)) u = np.random.randint(10, 20, (30)) v = np.random.randint(10, 20, (10)) mult_numpy(M, v) mult_mat(mat.Mat(M.tolist()), vec.Vec(v.tolist())) mult_numpy(u, M) mult_mat(vec.Vec(u.tolist()), mat.Mat(M.tolist()))
def draw( self, api ): cols = [ (1,0,0), (0,1,0), (0,0,1) ] for v,c in zip( self, cols ): api.line( vec.Vec( 0,0,0 ), v, c )
import draw_gl as dg from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * def draw(self, api): # save old state and load identity matrix if self.opt['style'] is 'dot': api.sphere(self, self.opt['r'], self.opt['color'], invis=self.opt['invis']) if self.opt['style'] is 'line': api.line(self.opt['start'], self, self.opt['color']) import vec vec.Vec.draw = draw vec.Vec.opt = { 'style': 'dot', 'r': 0.3, 'color': (0.8, 0.8, 0.8), 'invis': False, 'start': vec.Vec(0, 0, 0) }