def forward(self,distance): if distance <= 0: return self.h = distance if self.down: if self.rotVector is not None: t = self.position() m = self.rotMatrix if self.__mode == "standard": # move initial cylinder to x axis. m = matrix.dot(m, matrix.rotate(90,0,1,0)) m = matrix.dot(matrix.translate(t[0],t[1],t[2]), m).tolist() self.addNode(m) if self.showAxes: self.__drawAxes() else: if self.dir == 'Z': self.addNode2(ang = [-90, 0, self.curAng]) else: self.addNode2() # update current position self.curPoint = np.add(self.curPoint, self.h * self.heading()) if turtle.__fullDebug__: print("curPoint = %s" % self.position()) if turtle.__toDebug__: print(" forward = %d -> heading = %s" % (self.h, self.heading().tolist()[:-1]))
def camera_transform(location, line_of_sight, parallel, deltaz, point): r = [point[i] - location[i] for i in range(len(point))] dist_to_cam = matrix.dot(r, line_of_sight) horiz_coord = deltaz * matrix.dot(r, parallel) / dist_to_cam z = matrix.cross(parallel, line_of_sight) vert_coord = deltaz * matrix.dot(r, matrix.cross(line_of_sight, parallel)) / dist_to_cam return [horiz_coord, vert_coord]
def fire(self): # X = αX + (1-α)[WI*I + W*X] X = plus(times(X, self.alpha), times(plus(dot(WI, I), dot(W, X)), 1 - self.alpha)) X = tanh(X) #sigmoid(X) # Y = WO*X Y = dot(WO, X)
def fire(self): # X = αX + (1-α)[WI*I + W*X] self.X = plus( times(self.X, self.alpha), times(plus(dot(self.WI, [1] + self.I), dot(self.W, [1] + self.X)), 1 - self.alpha)) self.X = tanh(self.X) # Y = WO*X self.Y = dot(self.WO, [1] + self.X)
def __z_compare(self, polyview1, polyview2): if self.camera == None: centroidz1 = sum([v[2] for v in polyview1.poly3d.vertices]) / len(polyview1.poly3d.vertices) centroidz2 = sum([v[2] for v in polyview2.poly3d.vertices]) / len(polyview2.poly3d.vertices) else: los = self.camera['line_of_sight'] centroidz1 = sum([matrix.dot(v, los) for v in polyview1.poly3d.vertices]) / len(polyview1.poly3d.vertices) centroidz2 = sum([matrix.dot(v, los) for v in polyview2.poly3d.vertices]) / len(polyview2.poly3d.vertices) if centroidz1 < centroidz2: return -1 elif centroidz1 == centroidz2: return 0 elif centroidz1 > centroidz2: return 1
def addLeaf(self, c=None, r=None): if c is None and self.leafCol is None: cols = [colors["medium orchid"], colors["magenta"], colors["pastel pink"], colors["orange red"], colors["cyan"]] rand = random.randint(0, len(cols) - 1) c = cols[rand] self.leafCol = c if self.leafCol is not None: c = self.leafCol if r is None: r = self.r - 3 t = self.position() m = self.rotMatrix m = matrix.dot(matrix.translate(t[0],t[1],t[2]), m).tolist() flower = union()( translate([0, 0.5, 0])(sphere(r)), translate([0, -0.5, 0])(sphere(r)), translate([0, 0, 0.5])(sphere(r)), translate([0, 0, -0.5])(sphere(r)) ) self.nodes.append( (multmatrix(m) (color(c) (flower) ) ) )
def power_method(k, epsilon, matrix): ev = [] # list of eigenvectors vals = [] # list of eigenvalues v = 0 # index for which eigenvector we're computing if k == 1: iterations = 0 while v < k: r = Matrix(matrix.rows, 1) # initialize default vector for i in range(r.rows): r.data[i][0] += 1 # deflation for additional eigenvectors if v > 0: matrix = matrix - (ev[v-1]*ev[v-1].transpose())*vals[v-1] # orthogonalization of random vector if v > 0: for l in range(v): r = r - ev[l] * dot(ev[l], r) counter = 0 while True: if k == 1: iterations += 1 r_prime = iterate(matrix, r) e_plus = r - r_prime e_minus = r + r_prime if e_plus.norm() < epsilon or e_minus.norm() < epsilon: ev.append(r) lmb = dot(matrix*r, r)/dot(r, r) vals.append(lmb) v += 1 break r = r_prime """ This would occasionally make my code fail to converge, so you can comment it out if things hang. """ if counter%10 ==0: # orthogonalization of random vector if v > 0: for l in range(v): r = r - ev[l] * dot(ev[l], r) counter += 1 if k == 1: print "Iterations:",iterations return ev, vals
def main(): n = int(sys.argv[1]) dist = stdarray.readFloat1D() cx = stdarray.readFloat2D() cy = stdarray.readFloat2D() x = 0.0 y = 0.0 stddraw.setPenRadius(0.0) for i in range(n): r = stdrandom.discrete(dist) t = [x, y, 1] # x0 = cx[r][0]*x + cx[r][1]*y + cx[r][2] x0 = matrix.dot(cx[r], t) # y0 = cy[r][0]*x + cy[r][1]*y + cy[r][2] y0 = matrix.dot(cy[r], t) x = x0 y = y0 stddraw.point(x, y) stddraw.show()
def transformPoints(self, mat): """ Given a 4x4 projective matrix, return a list containing the transformed points of this polygon. """ if (mat is None): return transformed = [] for p in self.points: res = matrix.dot(mat, p.np()).tolist()[0] transformed.append(res) return transformed
def __drawAxes(self): r = self.r/8 if self.__mode == "standard": hx = self.h*0.8 hy = self.r*3 hz = self.r*3 else: hx = self.r*3 hy = self.r*3 hz = self.h*0.8 # draw the Z axis. c = np.add(self.curPoint, self.h*self.rotVector/2) m = matrix.dot(matrix.translate(c[0],c[1],c[2]), self.rotMatrix).tolist() self.addNode(m, [0,0,1], r, hz, False) # draw the Y axis. m = matrix.dot(self.rotMatrix, matrix.rotate(90,-1,0,0)) m = matrix.dot(matrix.translate(c[0],c[1],c[2]), m).tolist() self.addNode(m, [0,1,0], r, hy, False) # draw the X axis. m = matrix.dot(self.rotMatrix, matrix.rotate(90,0,1,0)) m = matrix.dot(matrix.translate(c[0],c[1],c[2]), m).tolist() self.addNode(m, [1,0,0], r, hx, False)
def output(self, nn_input: list) -> list: """ Calculate network output :param nn_input: values of input neurons :return: values of output neurons """ # convert input to matrix inputs = [[el] for el in nn_input] # add bias inputs_bias = matrix.add_bias(inputs) # calculate output # apply layer one weights to the inputs h_input = matrix.dot(self.wih, inputs_bias) # activate input layer h_output = matrix.activation(h_input) # add bias hidden_bias = matrix.add_bias(h_output) # apply layer two weights o_input = matrix.dot(self.who, hidden_bias) # activate hidden layer o_output = matrix.activation(o_input) output = o_output return matrix.col_to_row(output)
def Render( self ): """Render scene geometry""" # Clear Screen And Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.setCamera() self.drawCoordAxes(2.5) if self.arcBall: self.drawEulerAxis(5, 3) if not self.arcBall: glMatrixMode (GL_MODELVIEW) # If the last control scheme to be used was ArcBall, # then extract the euler angles from the last rotation matrix, # and initialize angleList to use such angles to allow for a smooth transition. if not self.rotating: self.angleList[0], self.angleList[1], self.angleList[2] = rotationMatrixToEulerAngles(self.g_Transform.T) else: self.angleList[self.index] = (self.angleList[self.index] + self.angInc) % 360 if self.rotationDirection == "ZYX": m = numpy.asarray(matrix.getRotationMatrix(self.angInc, self.index)) theta, self.ex, self.ey, self.ez = matrixToEulerAxisAngle(m) # rotation about index 0 (x) is intrinsic. # rotations about indexes 1 (y) and 2 (z) are extrinsic, so it seems... if self.index > 0: # use just the camera transformation self.drawEulerAxis(5,3, True) self.indexChanged = False #theta, self.ex, self.ey, self.ez = matrixToEulerAxisAngle(matrix.rotateZYX(self.angleList)) #theta, self.ex, self.ey, self.ez = quatToEulerAxisAngle(EulerAnglesToQuaternion(self.angleList)) #a, b, c, self.ex, self.ey, self.ez = quatToEulerAngles(EulerAnglesToQuaternion(self.angleList)) #self.drawEulerAxis(5,3, True) self.modelView = matrix.rotateZYX(self.angleList) glMultMatrixf(self.modelView) else: m = numpy.asarray(matrix.getRotationMatrix(self.angInc, self.index)) if self.modelView is None: self.modelView = self.g_Transform.T theta, self.ex, self.ey, self.ez = matrixToEulerAxisAngle(m) if self.rotationDirection == "Extrinsic": # use just the camera transformation self.drawEulerAxis(5,3,True) # extrinsic rotation (external or global axes) self.modelView = matrix.dot(self.modelView,m.T) glMultMatrixf(self.modelView) else: # intrinsic rotation (internal or local axes) self.modelView = matrix.dot(self.modelView,m) glMultMatrixf(self.modelView.T) self.rotating = True else: if self.rotating: if self.modelView is not None: if self.rotationDirection == "Intrinsic": self.g_ThisRot = self.matrix4To3(self.modelView.T) self.g_Transform = self.modelView.T else: self.g_ThisRot = self.matrix4To3(self.modelView) self.g_Transform = self.modelView glMultMatrixf(self.g_Transform) self.rotating = False else: glMatrixMode (GL_MODELVIEW) glMultMatrixf(self.g_Transform) if not self.arcBall: # use all three transformations self.drawEulerAxis(5,3) self.setupTexture() self.drawCube() self.drawCoordAxes(1.5, 3.0)
def test_model(self): if not self.trained: print("You have not trained your model") y = matrix.dot(self.Theta, matrix.transpose(self.X)) return y
def error_function_der(self): """Returns the derivative of the error function with a dimension of (1 x (n+1))""" ser = matrix.dot(matrix.diff(self.h, self.y), self.X) J = matrix.dot_scalar(ser, 1 / self.M) return J
def roll(self, ang): self.setDirection('X') self.rotMatrix = matrix.dot(self.rotMatrix, matrix.rotate(ang,1,0,0)) self.rotVector = np.dot (self.initialVector, self.rotMatrix.T.tolist()) if turtle.__toDebug__: print ("roll(%d)" % ang)
def open_like_BFS(self,alpha): Q = [] visite1 = [False for i in self.faces] transf_vec = [None for i in self.faces] altura = [0 for i in self.faces] q0 = self.face_selected Q.append(q0) visite1[q0] = True transf_vec[q0] = matrix.identity() altura[q0] = 1. self.points_per_face[q0] = copy.deepcopy(self.points_per_face_orig[q0]) while len(Q) > 0: q1 = Q.pop(0) for v in self.adjacences_list[q1]: if visite1[v] == False : visite1[v] = True Q.append(v) altura[v] = altura[q0] + 1 N1 = self.polygons[q1].normal N2 = self.polygons[v].normal (np1,np2) = self.edge_between_faces[(q1,v)] v0 = self.vertex[np1] v1 = self.vertex[np2] edge = Line(v0,v1) ang = np.rad2deg(math.acos(N1.dotProd(N2))) axis = edge.dir if axis.tripleProd(N1,N2) > 0: ang = -ang ang = alpha*ang R = matrix.translateAndRotate(ang,v1,edge.dir) transf_vec[v] = matrix.dot(transf_vec[q1],R) for i in xrange(len(self.points_per_face[v])): self.points_per_face[v][i] =\ self.points_per_face_orig[v][i].transform(transf_vec[v]) if self.useTexture: axis_z = Point(0.,0.,1.) angle_z = axis_z.dotProd(self.polygons[q0].normal) axis_r = axis_z.crossProd(self.polygons[q0].normal) self.mapRotate = matrix.translateAndRotate(angle_z,axis_r,self.points_per_face[q0][0]) for points in self.points_per_face: for point in points: self.box.add(point.transform(self.mapRotate)) self.box.setParameters()
def hypothesis_function(self): """Returns the hypothesis function with a dimension of (1 x M)""" return matrix.dot(self.Theta, matrix.transpose(self.X))
def fire(self): # X = αX + (1-α)[WI*I + W*X] self.X = plus(times(self.X, self.alpha), times(plus( dot(self.WI, [1]+self.I), dot(self.W,[1]+self.X)), 1 - self.alpha)) self.X = tanh(self.X) # Y = WO*X self.Y = dot(self.WO, [1]+self.X)
import matrix print(matrix.dot(2, [[1, 2], [2, 3]], [[1, 4], [5, 6]]))
def directional_variance_gradient_i(x_i, w): # вклад строки x_i в градиент w- направленой дисперсии protection_lenght = mat.dot(x_i, direction(w)) return [2 * protection_lenght * x_ij for x_ij in x_i]
X1 = [1.1221582582951433, -0.5248577355590887] W1 = [[-0.11334536547519446, -0.05975668760448366, 0.277655212701116], [0.10863945739135879, -0.08341075090238026, -0.022536355171462602]] V1 = [-0.14313246384799153, -0.017217359990103844] X2 = [1.1321582582951433, -0.5248577355590887] W2 = [[-0.11334536547519446, -0.05975668760448366, 0.277655212701116], [0.10863945739135879, -0.08341075090238026, -0.022536355171462602]] V2 = [-0.14313246384799153, -0.017217359990103844] malo = W1[0][0] * 1 + W1[0][1] * X1[0] + W1[0][2] * X1[1] #print(malo) #print(V1[0]) #print(V2[0]) from matrix import dot mat = [[1, 2], [3, 4]] vec = [5, 6] res = dot(mat, vec) print(res)
X1 = [1.1221582582951433, -0.5248577355590887] W1 = [[-0.11334536547519446, -0.05975668760448366, 0.277655212701116], [0.10863945739135879, -0.08341075090238026, -0.022536355171462602]] V1 = [-0.14313246384799153, -0.017217359990103844] X2 = [1.1321582582951433, -0.5248577355590887] W2 = [[-0.11334536547519446, -0.05975668760448366, 0.277655212701116], [0.10863945739135879, -0.08341075090238026, -0.022536355171462602]] V2 = [-0.14313246384799153, -0.017217359990103844] malo = W1[0][0]*1 + W1[0][1]*X1[0] + W1[0][2]*X1[1] #print(malo) #print(V1[0]) #print(V2[0]) from matrix import dot mat = [[1,2],[3,4]] vec = [5,6] res = dot(mat,vec) print(res)
import matrix A = [[1, 2], [3, 4]] B = [[1, 2], [3]] R = matrix.dot(2, A, B) print(R)