def grid(self, ngrid=30): x1 = np.linspace(self.lim['x1'][0], self.lim['x1'][1], ngrid) x2 = np.linspace(self.lim['x2'][0], self.lim['x2'][1], ngrid) (x1, x2) = np.meshgrid(x1, x2) x1r = x1.reshape((-1, 1)) x2r = x2.reshape((-1, 1)) x = np.concatenate((x1r, x2r), 1) if self.type == 'fun': y = self.fun.eval(x) else: y = self.model.sample(x) y = y.reshape((ngrid, ngrid)) return (x1, x2, y)
def grid(self, ngrid=30): x1 = np.linspace(self.lim['x1'][0], self.lim['x1'][1], ngrid) x2 = np.linspace(self.lim['x2'][0], self.lim['x2'][1], ngrid) (x1, x2) = np.meshgrid(x1, x2) x1r = x1.reshape((-1, 1)); x2r = x2.reshape((-1, 1)); x = np.concatenate((x1r, x2r), 1) if self.type == 'fun': y = self.fun.eval(x) else: y = self.model.sample(x) y = y.reshape((ngrid, ngrid)) return (x1, x2, y)
def draw_polygon_binary_num(radius, num_list): x = [] y = [] i_zeroes = [i for i in range(0, len(num_list)) if num_list[i] == 0] x_zeroes = [] y_zeroes = [] val_to_move_num = 1 angles = np.linspace(0, 2 * math.pi, len(num_list) + 1) for i, cur_angle in enumerate(angles): y.append(radius * math.cos(cur_angle)) x.append(radius * math.sin(cur_angle)) if i in i_zeroes: x_zeroes.append(x[-1]) y_zeroes.append(y[-1]) for i in range(len(num_list)): plt.text(x[i] + val_to_move_num * my_sign(x[i]), y[i] + val_to_move_num * my_sign(y[i]), str(num_list[i])) plt.plot(x, y, '-', color='#4CB2DC', linewidth=3) plt.plot(x, y, '.', color='black', markersize=10) plt.plot(x_zeroes, y_zeroes, '.', color='white', markersize=8) plt.ylim([-radius - val_to_move_num, radius + val_to_move_num]) plt.xlim([-radius - val_to_move_num, radius + val_to_move_num]) plt.gca().set_aspect('equal', adjustable='box') plt.axis('off') plt.title(str(num_list).strip('[]'), loc='left')
def plot1(gp, ngrid=100, lim=None, k=range(-3, 4)): k = sorted(k) if lim is None: lim = (np.amin(gp.x[:, 1]), np.amax(gp.x[:, 1])) x = np.linspace(lim[0], lim[1], ngrid).T (m, v) = gp.inf(x) m = np.asarray(m).squeeze() v = np.asarray(v).squeeze() plt.plot(x, m, color=DARKBLUE, linewidth=2) for i in k: if i == 0: continue lo = m - i*np.sqrt(v) hi = m + i*np.sqrt(v) plt.fill_between(x, lo, hi, linestyle='solid', edgecolor=DARKGRAY, facecolor=LIGHTGRAY, alpha=0.2) plt.plot(gp.x, gp.y, 'o', markersize=8, markeredgewidth=1, markeredgecolor=DARKGRAY, markerfacecolor=LIGHTBLUE) plt.xlim(lim)
def curve(c, offset): if c <= 0: return None if c <= 1: (a, b) = dom(c) (a1, b1) = (a + offset, b + offset) cur = [] for x in np.arange(a1, b1, .1): cur.append((x, lsy(x, c))) for x in np.arange(b1, a1, -.1): cur.append((x, -lsy(x, c))) return cur else: return [(x, lsy(x, c) * offset) for x in np.linspace(-3, 3, 15)]
def _find_threshold(self, feature, scores, space=25): ''' Given a feature test result, find the best threshold for future feature tests. :param feature: The feature to find a good threshold for :param scores: The score data to search for the threshold :returns: The best computed threshold ''' cs, ct = 0, 0 thresholds = M.linspace(scores.min(), scores.max(), space) for thresh in thresholds: _log.debug("Testing next threshold...") classifier = (scores - thresh).T result = classifier * self.alldesired * self.weights result = result.sum() if result > cs: cs,ct = result,thresh self.classifier = classifier return cs,ct
def _find_threshold(self, feature, scores, space=25): ''' Given a feature test result, find the best threshold for future feature tests. :param feature: The feature to find a good threshold for :param scores: The score data to search for the threshold :returns: The best computed threshold ''' cs, ct = 0, 0 thresholds = M.linspace(scores.min(), scores.max(), space) for thresh in thresholds: _log.debug("Testing next threshold...") classifier = (scores - thresh).T result = classifier * self.alldesired * self.weights result = result.sum() if result > cs: cs, ct = result, thresh self.classifier = classifier return cs, ct
def draw_sym_axis_binary_num(radius, num_list, sym_list): angles = np.linspace(0, 2 * math.pi, len(num_list) + 1) for cur_sym_node in sym_list: if isinstance(cur_sym_node, int): x = radius * math.sin(angles[cur_sym_node]) y = radius * math.cos(angles[cur_sym_node]) if isinstance(cur_sym_node, tuple): angle = (angles[cur_sym_node[1]] + angles[cur_sym_node[0]]) / 2 x = radius * math.sin(angle) y = radius * math.cos(angle) if x == 0: x_line = 0 y1_line = 50 y2_line = -50 else: x_line = 50 y1_line = straight_line_centre_and_point(x_line, x, y) y2_line = straight_line_centre_and_point(-x_line, x, y) plt.plot((-x_line, x_line), (y2_line, y1_line), '--', color='black', linewidth=1)
# p_end = [0.8, 0.5, 1.35] # Nice one for hitting vertical board. p_end = [0.7, 0.4, 1.45] # p_end = [0.99, 0.0, 1.24] # THIS IS VERY VERY CLOSE TO SINGULARITY # p_end = [0.7, 0.0, 1.0] # BOARD ON HIP LEVEL CLOSE TO TORSO cont, q_end = invKyn.invKin(p_des=p_end, fk=forKin, frame=end_effector, j_str=joint_str, q_init=q_0, animate=False, T=2) # INITIAL GUESS ON JOINT POSITION p_constraint = True initial_guess = True q_0_vec = ml.linspace(q_0, q_end, N_stage[0] + 1).transpose() # cont = True if not cont: sys.exit('####### RUNTIME ERROR: Invalid desired p_end defined! #######\n') if not fn.inWorkspace(p_0, p_end, n_hat): sys.exit( '####### RUNTIME ERROR: Initial position is not in workspace! #######\n' ) # HOMING init_state_centauro.homing(pose=init_pose) # ============================================================================= # NONLINEAR PROGRAM --> FIND A SOLUTION FOR THE OPTIMAL CONTROL INPUT # =============================================================================
(a1, b1) = (a + offset, b + offset) cur = [] for x in np.arange(a1, b1, .1): cur.append((x, lsy(x, c))) for x in np.arange(b1, a1, -.1): cur.append((x, -lsy(x, c))) return cur else: return [(x, lsy(x, c) * offset) for x in np.linspace(-3, 3, 15)] glops = "red, thick" for c in [1 / 3, 2 / 3, 1]: print(drawPlot(curve(c, 0), cycle=True)) print(drawPlot(curve(c, math.pi), cycle=True)) print(drawPlot(curve(c, -math.pi), cycle=True)) for c in [1.333333, 1.666666]: print(drawPlot(curve(c, 1))) print(drawPlot(curve(c, -1))) #draw the image of w def w(t): return (t, 1 + t) glops = "thick,orange,->" print(drawPlot(f(*w(t)) for t in np.linspace(0, 1, 10)))
def from_testcase(cls, tc, fclass=None): yres = _GRAPH_YRES_MLP*_GRAPH_YRES_BASIC + 1 x = np.linspace(tc.lim['x1'][0], tc.lim['x1'][1], _GRAPH_XRES) y = np.linspace(tc.lim['x2'][0], tc.lim['x2'][1], yres) (x, y) = np.meshgrid(x, y) return Graph(x.T, y.T, fclass)
def from_testcase(cls, tc, fclass=None): yres = _GRAPH_YRES_MLP * _GRAPH_YRES_BASIC + 1 x = np.linspace(tc.lim['x1'][0], tc.lim['x1'][1], _GRAPH_XRES) y = np.linspace(tc.lim['x2'][0], tc.lim['x2'][1], yres) (x, y) = np.meshgrid(x, y) return Graph(x.T, y.T, fclass)
import math import numpy as np import numpy.matlib as nm import matplotlib.pyplot as plt np.seterr(divide='ignore', invalid='ignore') n = 200 a = nm.linspace(0, nm.pi, n / 2) x_u = np.c_[nm.cos(a) + 0.5, nm.cos(a) - 0.5].reshape(n, 1) u = -10 * x_u + nm.randn(n, 1) x_v = np.c_[nm.sin(a), -nm.sin(a)].reshape(n, 1) v = 10 * x_v + nm.randn(n, 1) x = np.c_[u, v] y = np.zeros((n, 1)) y[0] = 1 y[n - 1] = -1 x2 = np.sum(np.power(x, 2), 1) hh = 2 * 1**2 k = nm.exp(-(nm.repmat(x2, 1, n) + nm.repmat(x2.T, n, 1) - 2 * x * x.T) / hh) w = k t_tmp1 = k**2 + 1 * np.eye(n) + 10 * k * (nm.diag(sum(w)) - w) * k t = np.linalg.inv(t_tmp1) * (k * y) m = 100 X = nm.linspace(-20, 20, m).T X2 = np.power(X, 2) U = nm.exp( -(nm.repmat(np.power(u, 2), 1, m) + nm.repmat(X2.T, n, 1) - 2 * u * X.T) / hh) V = nm.exp(
import math import numpy as np import numpy.matlib as nm import matplotlib.pyplot as plt n = 100 u = nm.randn(n, 1) / 4 + 2 x = nm.randn(n, 1) / 2 + 1 w = 2 * nm.exp(-8 * np.power((x - 2), 2) + 2 * np.power((x - 1), 2)) y = nm.sin(nm.pi * x) / (nm.pi * x) + 0.1 * nm.randn(n, 1) x2 = np.ones(len(x)).reshape(len(x), 1) # 生成一个维度为(n,1),元素全是1的数组 x = np.c_[x, x2] # x(:,2)=1;加第二行为1 t1 = np.multiply(nm.repmat(w, 1, 2), x) # np.multiply为矩阵对应元素相乘 t = np.linalg.inv(x.T * t1) * (x.T * (np.multiply(w, y))) # np.linalg.inv(.)求逆矩阵 X = nm.linspace(-1, 3, 100) # 生成数据节点 Y = nm.sin(nm.pi * X) / (nm.pi * X) # 根据节点计算Y u = np.c_[u, x2] v = u * t print(u.shape) plt.figure() plt.plot(x[:, 0], y, 'bo', label='xi,yi') # 绘制原始数据点 plt.plot(X, Y, 'r-', label='f(x)') # 绘制重要度加权的最小二乘曲线 plt.plot(u[:, 0], v, 'kx', label='xi1,yi1') # 绘制最小二乘曲线 plt.legend() # 显示绘制的legend plt.show() # 显示绘制的画布
def flow(p0): (x, y) = p0 return [(xx, y) for xx in np.linspace(x, 3 + .2 * x, num=N)]
options, smooth, " ".join(map(showPt, points))) def distort(p): (x, y) = p (x2, y2) = (x - math.sin(y + .1) * .2, y + math.exp(-x**2) ) #change as needed a = -.2 return (math.cos(a) * x2 + math.sin(a) * y2 + 2, -math.sin(a) * x2 + math.cos(a) * y2 + 1.6) N = 30 circle = [(math.cos(t), math.sin(t)) for t in np.linspace(0, 2 * math.pi, num=N, endpoint=False)] startpoints = [(-.3, -.3), (.2, -1.3), (-1.2, .7)] def flow(p0): (x, y) = p0 return [(xx, y) for xx in np.linspace(x, 3 + .2 * x, num=N)] def nodes(p0): (x, y) = p0 if abs(y) > 1: return minx = -math.sqrt(1 - y**2) maxx = -minx if x < minx: