def test_linear_solves_equivalent(): """solve(a == L, out) should return the same as solving with the assembled objects. This relies on two different code paths agreeing on the same set of solver parameters.""" mesh = UnitSquareMesh(50, 50) V = FunctionSpace(mesh, "CG", 1) f = Function(V) f.assign(1) f.vector()[:] = 1. t = TestFunction(V) q = TrialFunction(V) a = inner(t, q)*dx L = inner(f, t)*dx # Solve the system using forms sol = Function(V) solve(a == L, sol) # And again sol2 = Function(V) solve(a == L, sol2) assert np_norm(sol.vector()[:] - sol2.vector()[:]) == 0 # Solve the system using preassembled objects sol3 = Function(V) solve(assemble(a), sol3, assemble(L)) assert np_norm(sol.vector()[:] - sol3.vector()[:]) < 5e-14
def __init__( self, latitude, longitude, plummet = ( 0, 0, -1. ), reference_frame = default_reference_frame ) : ''' Create a projection frame. Parameters ---------- latitude : float Orientation of the viewer with respect to vertical. longitude : float Orientation of the viewer around z-axis. plummet : tuple of length 3, optional Defines the vertical direction. reference_frame : Frame dictionary, optional ''' self.latitude = latitude self.longitude = longitude self.plummet = np.array( plummet )/np_norm( plummet ) # the vertical direction self.reference_frame = reference_frame self.n_view = np.array( [ -np.sin( self.latitude )*np.cos( self.longitude ), -np.sin( self.latitude )*np.sin( self.longitude ), -np.cos( self.latitude ) ] ) n_X = np.cross( self.plummet, self.n_view ) self.n_X = n_X/np_norm( n_X ) self.n_Y = np.array( np.cross( self.n_X, self.n_view ) )
def getAngBetween(self, P1, P2): """Return the angle between two points (in radians)""" # find the existing angle between them theta c = np_dot(P1,P2)/np_norm(P1)/np_norm(P2) # rounding errors hurt everyone... if(c > 1): c = 1 elif(c < -1): c = -1 return np_arccos(c) # in radians
def transformCP(self, silent=False, nolog=False, min=None, max=None): """Do the main ransformation on the coverage profile data""" shrinkFn = np_log10 if(nolog): shrinkFn = lambda x:x s = (self.numContigs,3) self.transformedCP = np_zeros(s) if(not silent): print " Dimensionality reduction" # get the median distance from the origin unit_vectors = [(np_cos(i*2*np_pi/self.numStoits),np_sin(i*2*np_pi/self.numStoits)) for i in range(self.numStoits)] for i in range(len(self.indices)): norm = np_norm(self.covProfiles[i]) if(norm != 0): radial = shrinkFn(norm) else: radial = norm shifted_vector = np_array([0.0,0.0]) flat_vector = (self.covProfiles[i] / sum(self.covProfiles[i])) for j in range(self.numStoits): shifted_vector[0] += unit_vectors[j][0] * flat_vector[j] shifted_vector[1] += unit_vectors[j][1] * flat_vector[j] # log scale it towards the centre scaling_vector = shifted_vector * self.scaleFactor sv_size = np_norm(scaling_vector) if(sv_size > 1): shifted_vector /= shrinkFn(sv_size) self.transformedCP[i,0] = shifted_vector[0] self.transformedCP[i,1] = shifted_vector[1] self.transformedCP[i,2] = radial if(not silent): print " Reticulating splines" # finally scale the matrix to make it equal in all dimensions if(min is None): min = np_amin(self.transformedCP, axis=0) max = np_amax(self.transformedCP, axis=0) max = max - min max = max / (self.scaleFactor-1) for i in range(0,3): self.transformedCP[:,i] = (self.transformedCP[:,i] - min[i])/max[i] return(min,max)
def approach_goal(y, dy, goal): # based on Hoffmann (2009) but instead of # avoiding obstacles, we approach a goal gamma = 10 # 1/5 beta = 1 / np.pi p = np.zeros(2) if np_norm(dy) > 1e-5: # calculate current heading phi_dy = np.arctan2(dy[1], dy[0]) # calc vector to goal goal_vec = goal - y phi_goal = np.arctan2(goal_vec[1], goal_vec[0]) # angle diff phi = phi_goal - phi_dy # tuned inverse sigmoid to create force towards goal dphi = gamma * phi * np_exp(-beta * np_abs(phi)) pval = goal_vec * dphi # print("force vector:", pval, dy) p += pval return p
def sph_means(data, cent, init=None, K=0, it=1000, toy=False, only_direct=True): M, V = data.shape if init is None: ctopics = data[np.random.choice(range(M), K, replace=False),:].toarray()- cent else: ctopics = np.copy(init) - cent K = ctopics.shape[0] norms = sparse_norm(data, cent) for i in range(it): D = [] ## Get clustering for k in range(K): D.append(sparse_cos_sim(data, ctopics[k], cent, norms)) clusters = np.argmin(D, axis=0) ## Update centers for k in range(K): c = np.where(clusters == k)[0] if len(c) > 0: ctopics[k] = data[c,:].mean(axis=0).A.flatten() - cent if i == it-1 and not only_direct: ctopics[k] = ctopics[k]/np_norm(ctopics[k]) ctopics[k] *= max(data[c,:].dot(ctopics[k]) - np.dot(ctopics[k], cent)) if toy: plot_clust(data.toarray()-cent, ctopics, clusters) ctopics = ctopics + cent ctopics[ctopics<0] = 0 ctopics = normalize(ctopics, 'l1') return ctopics, clusters
def angle_update(data, cent, norms, min_cos, algo0 = False, plot=False): cur = np.argmax(norms) new_t = data[cur,:].A.flatten() - cent if algo0: cos_dist = sparse_cos_sim(data, new_t, cent, norms) if plot: clust = np.array(np.repeat('blue', len(norms)), dtype='|S5') clust[cos_dist<min_cos] = 'red' plot_clust(data.toarray()-cent, np.array([new_t]), clust) return new_t, cos_dist alpha_old = 1. alpha_new = 0. it = 0 while np.abs(alpha_old-alpha_new)>1e-03 and it<50: it += 1 alpha_old = alpha_new old_t = new_t cos_dist = sparse_cos_sim(data, old_t, cent, norms) near_doc = cos_dist < min_cos new_t = data[near_doc,:].mean(axis=0).A.flatten() - cent alpha_new = cosine(old_t, new_t) new_t = new_t/np_norm(new_t) new_t *= max(data[near_doc,:].dot(new_t) - np.dot(new_t, cent)) cos_dist = sparse_cos_sim(data, new_t, cent, norms) if plot: clust = np.array(np.repeat('blue', len(norms)), dtype='|S5') clust[cos_dist<min_cos] = 'red' plot_clust(data.toarray()-cent, np.array([new_t]), clust) return new_t, cos_dist
def compute_distances_np(line, pts, result, gates, tolerance): '''calculate all distances with NumPy''' # Adapted from https://math.stackexchange.com/questions/1905533/find-perpendicular-distance-from-point-to-line-in-3d np_pts = array(pts) segment = V(line[-1]) - V(line[0]) segment_length = segment.length line_direction = segment / segment_length vect = np_pts - line[0] vect_proy = vect.dot(line_direction) closest_point = line[0] + vect_proy[:, newaxis] * line_direction dif_v = closest_point - np_pts dist = np_norm(dif_v, axis=1) is_in_segment = [] is_in_line = [] closest_in_segment = [] if gates[4] or gates[1]: closest_in_segment = np_all( [vect_proy >= 0, vect_proy <= segment_length], axis=0) if gates[1] or gates[2]: np_tolerance = array(tolerance) is_in_line = dist < tolerance if gates[1]: is_in_segment = np_all([closest_in_segment, is_in_line], axis=0) local_result = [ dist, is_in_segment, is_in_line, closest_point, closest_in_segment ] for i, res in enumerate(result): if gates[i]: res.append( local_result[i].tolist() if not gates[5] else local_result[i])
def compute_distances_np(line, pts, result, gates, tolerance): '''calculate all distances with NumPy''' # Adapted from https://math.stackexchange.com/questions/1905533/find-perpendicular-distance-from-point-to-line-in-3d np_pts = array(pts) segment = V(line[-1]) - V(line[0]) segment_length = segment.length line_direction = segment / segment_length vect = np_pts - line[0] vect_proy = vect.dot(line_direction) closest_point = line[0] + vect_proy[:, newaxis] * line_direction dif_v = closest_point - np_pts dist = np_norm(dif_v, axis=1) is_in_segment = [] is_in_line = [] closest_in_segment = [] if gates[4] or gates[1]: closest_in_segment = np_all([vect_proy >= 0, vect_proy <= segment_length], axis=0) if gates[1] or gates[2]: np_tolerance = array(tolerance) is_in_line = dist < tolerance if gates[1]: is_in_segment = np_all([closest_in_segment, is_in_line], axis=0) local_result = [dist, is_in_segment, is_in_line, closest_point, closest_in_segment] for i, res in enumerate(result): if gates[i]: res.append(local_result[i].tolist() if not gates[5] else local_result[i])
def normalized(a, axis=-1, order=2): """ http://stackoverflow.com/questions/21030391/how-to-normalize-array-numpy """ l2 = np_atleast_1d(np_norm(a, order, axis)) l2[l2 == 0] = 1 return a / np_expand_dims(l2, axis)
def transformCP(self, timer, silent=False, nolog=False): """Do the main transformation on the coverage profile data""" if(not silent): print " Reticulating splines" self.transformedCP = self.dataManager.getTransformedCoverageProfiles(self.dbFileName, indices=self.indices) self.corners = self.dataManager.getTransformedCoverageCorners(self.dbFileName) self.TCentre = np_mean(self.corners, axis=0) self.transRadius = np_norm(self.corners[0] - self.TCentre)
def __getitem__(self, index): """This function returns a tuple that is further passed to collate_fn """ ix, it_pos_now, wrapped = index # self.split_ix[index] if self.use_att: att_feat = self.att_loader.get(str(self.info["images"][ix]["id"])) # Reshape to K x C att_feat = att_feat.reshape(-1, att_feat.shape[-1]) if self.norm_att_feat: att_feat = att_feat / np_norm(att_feat, 2, 1, keepdims=True) if self.use_box: box_feat = self.box_loader.get( str(self.info["images"][ix]["id"])) # devided by image width and height x1, y1, x2, y2 = np_hsplit(box_feat, 4) h, w = ( self.info["images"][ix]["height"], self.info["images"][ix]["width"], ) box_feat = np_hstack( (x1 / w, y1 / h, x2 / w, y2 / h, (x2 - x1) * (y2 - y1) / (w * h))) # question? x2-x1+1?? if self.norm_box_feat: box_feat = box_feat / np_norm( box_feat, 2, 1, keepdims=True) att_feat = np_hstack([att_feat, box_feat]) # sort the features by the size of boxes att_feat = np_stack( sorted(att_feat, key=lambda x: x[-1], reverse=True)) else: att_feat = np_zeros((0, 0), dtype="float32") if self.use_fc: try: fc_feat = self.fc_loader.get(str( self.info["images"][ix]["id"])) except: # Use average of attention when there is no fc provided (For bottomup feature) fc_feat = att_feat.mean(0) else: fc_feat = np_zeros((0), dtype="float32") if hasattr(self, "h5_label_file"): seq = self.get_captions(ix, self.necessary_num_img_captions) else: seq = None return (fc_feat, att_feat, seq, ix, it_pos_now, wrapped)
def most_similar_sentence(self, vec, num, candidate_list=None): sims = empty(self.sents_len,dtype=REAL) if FAST_VERSION: sentvec_sim(self,vec,num,sims) else: vec_len = np_norm(vec) for idx in xrange(self.sents_len): vec2 = self.sents[idx] vec2_len = np_norm(vec2) sims[idx] = dot(vec,vec2) / vec_len / vec2_len nearest = [] topN = argsort(sims)[::-1] for top_sent in topN: sent_id = self.sent_id_list[top_sent] if candidate_list is not None and not sent_id in candidate_list: continue nearest.append((sent_id,float(sims[top_sent]))) if len(nearest) == num: break return nearest
def norm(self): if self.isnone: return None if self.mode == MODE_NP or (self.mode == MODE_SP and self.type == 'vector'): return np_norm(self.x) if self.mode == MODE_TT: return self.x.norm() if self.mode == MODE_SP and self.type == 'matrix': return sp_norm(self.x)
def most_similar_sentence(self, vec, num, candidate_list=None): sims = empty(self.sents_len, dtype=REAL) if FAST_VERSION: sentvec_sim(self, vec, num, sims) else: vec_len = np_norm(vec) for idx in xrange(self.sents_len): vec2 = self.sents[idx] vec2_len = np_norm(vec2) sims[idx] = dot(vec, vec2) / vec_len / vec2_len nearest = [] topN = argsort(sims)[::-1] for top_sent in topN: sent_id = self.sent_id_list[top_sent] if candidate_list is not None and not sent_id in candidate_list: continue nearest.append((sent_id, float(sims[top_sent]))) if len(nearest) == num: break return nearest
def show_reference_frame(self, center=None, color='black', axes_names={ 'x': 'x', 'y': 'y', 'z': 'z' }, with_arrows=True, ax=None, adjust_ax_lims=True, text_pad=.15, **kwargs): if center is None: center = self.reference_frame['center'] if ax is None: ax = matplotlib_pyplot.gca() for xyz, direction in self.reference_frame['direction'].items(): xy = self.project_on_screen(center) xytext = self.project_on_screen(center + direction) try: direction_proj = self.project_on_screen(direction) / np_norm( self.project_on_screen(direction)) except: direction_proj = np.array([-1, -1]) / sqrt(2) xytext_wp = xytext + text_pad * direction_proj if with_arrows: if adjust_ax_lims: ax.plot(*np.array([xy, xytext_wp]).T, linestyle='none' ) # ), marker = 'o', color = 'red', alpha = .1 ) ax.annotate( s='', #axes_names[xyz], xy=xy, xytext=xytext, arrowprops=dict(arrowstyle='<-', shrinkB=0, shrinkA=0), annotation_clip=False, ha='center', va='center') else: self.plot_points(np.array([xy, xytext]), color=color, **kwargs) ax.text(*xytext_wp, axes_names[xyz], color=color, ha='center', va='center')
def __init__( self, points ) : ''' Parameters ---------- points: list of three points These points define the plane. Only the three first elements of the list are used. ''' self.origin = points[1] v1, v2 = points[0] - self.origin, points[2] - self.origin n = np.cross( v1, v2 ) self.normal = n/np_norm(n) n1 = v1/np_norm(v1) n2 = np.cross( self.normal, n1 ) self.base = n1, n2
def edges_direction(vertices, edges, out_numpy=False): '''calculate edges direction ''' np_verts = np.array(vertices) if type(edges[0]) in (list, tuple): np_edges = np.array(edges) else: np_edges = edges[:len(vertices) - 1, :] vect = np_verts[np_edges[:, 1], :] - np_verts[np_edges[:, 0], :] dist = np_norm(vect, axis=1) vect_norm = vect / dist[:, np.newaxis] return vect_norm if out_numpy else vect_norm.tolist()
def theend_hook(u_, p_, uv, mesh, testing, **NS_namespace): if not testing: assign(uv.sub(0), u_[0]) assign(uv.sub(1), u_[1]) plot(uv, title='Velocity') plot(p_, title='Pressure') if MPI.rank(mpi_comm_world()) == 0 and testing: from numpy.linalg import norm as np_norm print "Velocity norm = {0:2.6e}".format(np_norm(u_[0].vector().array())) if not testing: try: from fenicstools import StreamFunction psi = StreamFunction(uv, [], mesh, use_strong_bc=True) plot(psi, title='Streamfunction', interactive=True) except: pass
def theend_hook(u_, p_, uv, mesh, testing, **NS_namespace): if not testing: assign(uv.sub(0), u_[0]) assign(uv.sub(1), u_[1]) plot(uv, title='Velocity') plot(p_, title='Pressure') if MPI.rank(mpi_comm_world()) == 0 and testing: from numpy.linalg import norm as np_norm print "Velocity norm = {0:2.6e}".format(np_norm( u_[0].vector().array())) if not testing: try: from fenicstools import StreamFunction psi = StreamFunction(uv, [], mesh, use_strong_bc=True) plot(psi, title='Streamfunction', interactive=True) except: pass
def edges_direction(vertices, edges, out_numpy=False): ''' calculate edges direction vertices: list as [vertex, vertex, ...], being each vertex [float, float, float]. Also accepts numpy arrays with two axis edges: list as [edge, edge,..], being each edge [int, int]. Also accept numpy arrays with one axis. out_numpy: boolean to determine if outputtig np_array or regular python list returns edges direction as [vertex, vertex,...] or numpy array with two axis ''' np_verts = np.array(vertices) if type(edges[0]) in (list, tuple): np_edges = np.array(edges) else: np_edges = edges[:len(vertices) - 1, :] vect = np_verts[np_edges[:, 1], :] - np_verts[np_edges[:, 0], :] dist = np_norm(vect, axis=1) vect_norm = vect / dist[:, np.newaxis] return vect_norm if out_numpy else vect_norm.tolist()
def rotateVectorAndScale(self, point, las, centerVector, delta_max=0.25): """ Move a vector closer to the center of the positive quadrant Find the co-ordinates of its projection onto the surface of a hypersphere with radius R What?... ...First some definitions: For starters, think in 3 dimensions, then take it out to N. Imagine all points (x,y,z) on the surface of a sphere such that all of x,y,z > 0. ie trapped within the positive quadrant. Consider the line x = y = z which passes through the origin and the point on the surface at the "center" of this quadrant. Call this line the "main mapping axis". Let the unit vector coincident with this line be called A. Now think of any other vector V also located in the positive quadrant. The goal of this function is to move this vector closer to the MMA. Specifically, if we think about the plane which contains both V and A, we'd like to rotate V within this plane about the origin through phi degrees in the direction of A. Once this has been done, we'd like to project the rotated co-ords onto the surface of a hypersphere with radius R. This is a simple scaling operation. The idea is that vectors closer to the corners should be pertubed more than those closer to the center. Set delta_max as the max percentage of the existing angle to be removed """ theta = self.getAngBetween(point, centerVector) A = delta_max/((las)**2) B = delta_max/las delta = 2*B*theta - A *(theta**2) # the amount to shift V_p = point*(1-delta) + centerVector*delta return V_p/np_norm(V_p)
def sanity_check(self): veclens = empty(self.cat_len, dtype=REAL) for i in xrange(self.cat_len): veclens[i] = np_norm(self.cats[i]) max_len = amax(veclens) logger.info("max vector length: %f" % max_len) if max_len > self.sane_vec_len: return False, "insane max vector length > %f" % (self.sane_vec_len) if self.sg: return True, None rand_indices = random.randint(len(self.w2v.vocab),size=10) sim_top10_avg = 0 for idx in rand_indices: w = self.w2v.index2word[idx] sim_words = self.w2v.most_similar(positive=[w],topn=10) sim_top10_avg += sim_words[9][1] sim_top10_avg /= len(rand_indices) logger.info("average similarity: %f"% sim_top10_avg) if sim_top10_avg > self.sane_max_sim10: return False, "insane average similarity > %f" % (self.sane_max_sim10) return True, None
def sanity_check(self): veclens = empty(self.cat_len, dtype=REAL) for i in xrange(self.cat_len): veclens[i] = np_norm(self.cats[i]) max_len = amax(veclens) logger.info("max vector length: %f" % max_len) if max_len > self.sane_vec_len: return False, "insane max vector length > %f" % (self.sane_vec_len) if self.sg: return True, None rand_indices = random.randint(len(self.w2v.vocab), size=10) sim_top10_avg = 0 for idx in rand_indices: w = self.w2v.index2word[idx] sim_words = self.w2v.most_similar(positive=[w], topn=10) sim_top10_avg += sim_words[9][1] sim_top10_avg /= len(rand_indices) logger.info("average similarity: %f" % sim_top10_avg) if sim_top10_avg > self.sane_max_sim10: return False, "insane average similarity > %f" % ( self.sane_max_sim10) return True, None
def _norm(v): return np_norm(np_array([v.x, v.y, v.z]))
def norm(c0, c1, s): sig0 = logsig(c0, s) sig1 = logsig(c1, s) return np_norm(sig0 - sig1)
def hedging_criteria(port_mat: np_ndarray, rsk_tgt: np_ndarray, rsk_msk: np_ndarray) -> float: # ['d_p', 'delta', 'gamma', 'rho', 'theta', 'vega'] return np_norm(port_mat[rsk_msk] - rsk_tgt, 2)
def compute(x, y): return np_norm(x - y, axis=1)
def start(self, config, agent_fn): verbose = self.verbose = config['debug_verbose'] n_agents = config['n_workers'] scale_norm = config['scale_norm'] self.config = config self.n_agents = n_agents self.baseline_mask = config['baseline_mask'] self.graph_names = config['mul_graphs'] assert self.graph_names is not None if self.baseline_mask is None: self.baseline_mask = [0] * n_agents print("Number of agents: %d" % n_agents) gpus = config['use_gpus'] if config['shuffle_gpu_order']: random.seed(config['seed']) random.shuffle(gpus) assert n_agents > 0 params_send_qs = [Queue(1) for _ in range(n_agents)] params_recv_qs = [Queue(1) for _ in range(n_agents)] grad_send_qs = [Queue(1) for _ in range(n_agents)] grad_recv_qs = [Queue(1) for _ in range(n_agents)] summ_send_qs = [Queue(1) for _ in range(n_agents)] summ_recv_qs = [Queue(1) for _ in range(n_agents)] send_baseline_qs = [Queue(1) for _ in range(n_agents)] recv_baseline_qs = [Queue(1) for _ in range(n_agents)] configs = [] for i in range(n_agents): c = copy.deepcopy(config) c['id'] = i c['name'] += '/%s-%d' % (c['name'], i) c['params_send_q'] = params_recv_qs[i] c['params_recv_q'] = params_send_qs[i] c['grads_send_q'] = grad_recv_qs[i] c['grads_recv_q'] = grad_send_qs[i] c['summ_send_q'] = summ_recv_qs[i] c['summ_recv_q'] = summ_send_qs[i] c['send_baseline_q'] = recv_baseline_qs[i] c['recv_baseline_q'] = send_baseline_qs[i] if len(c['pickled_inp_file']) > 0: c['pickled_inp_file'] = \ [c['pickled_inp_file'][i % len(c['pickled_inp_file'])]] if c['remote_async_start_ports'] is not None: for j, p in enumerate(c['remote_async_start_ports']): c['remote_async_start_ports'][ j] = p + c['remote_async_n_sims'][j] * i configs.append(c) self.summ_recv_qs, self.summ_send_qs = summ_recv_qs, summ_send_qs threading.Thread(target=self.handle_summaries).start() self.send_baseline_qs, self.recv_baseline_qs = send_baseline_qs, recv_baseline_qs threading.Thread(target=self.handle_baselines).start() agents = [] for i in range(n_agents): runnable = mp.Process if config['use_threads']: runnable = threading.Thread agents.append(runnable(target=agent_fn, args=(configs[i], ))) for i in range(n_agents): if gpus is not None: g = gpus[i % len(gpus)] os.environ['CUDA_VISIBLE_DEVICES'] = g agents[i].start() if self.config['restore_from'] is None or self.config[ 'dont_restore_softmax']: for i in range(n_agents): init_ws = params_recv_qs[i].get() for i in range(n_agents): params_send_qs[i].put(init_ws) print("Cordinator initialization sequence finished") while True: a_ws, a_gs, a_norm = [], [], [] for i in range(n_agents): if verbose: print('Coordinator: Getting gradients from %d' % i) sys.stdout.flush() gs = grad_recv_qs[i].get() for k, v in gs.items(): gs[k] = np.float64(v) if config['dont_share_classifier']: assert 'classifier' not in k if scale_norm: norms = {} for k, v in gs.items(): norm = norms[k] = np.float64(np.linalg.norm(v)) if norm > 0: gs[k] /= norm a_norm.append(norms) a_gs.append(gs) grad_out = a_gs[0] for gs in a_gs[1:]: assert gs.keys() == grad_out.keys() for k in grad_out: grad_out[k] += gs[k] if scale_norm: for k, v in grad_out.items(): grad_out[k] = v * np.sum([norm[k] for norm in a_norm], dtype=np.float64) for i in range(n_agents): if verbose: print('Coordinator: Putting gradients into %d' % i) sys.stdout.flush() grad_send_qs[i].put(grad_out) for i in range(n_agents): if verbose: print('Coordinator: Getting params from %d' % i) sys.stdout.flush() a_ws.append(params_recv_qs[i].get()) if verbose: print('Coordinator: Episode sequence finished') sys.stdout.flush() any_agent_violation = False for i, w in enumerate(a_ws[1:]): assert w.keys() == a_ws[0].keys() violation = False for k, v in w.items(): if not (v == a_ws[0][k]).all(): violation = True any_agent_violation = True if np_norm(v.flatten() - a_ws[0][k].flatten() ) / np_norm(v.flatten()) >= 1e-3: print('ERROR: Weights diverged too far') import pdb pdb.set_trace() raise Exception('Weights not consistent') if violation: params_send_qs[i + 1].put(a_ws[0]) else: params_send_qs[i + 1].put(None) if any_agent_violation: params_send_qs[0].put(a_ws[0]) else: params_send_qs[0].put(None)
def unit_vector(vector): """ Returns the unit vector of the vector. """ div = np_norm(vector) if div == 0.0: return vector return vector / div
def sparse_cos_sim(data, t, cent, norms): t_cent = np.dot(t, cent) data_dot = data.dot(t) cos_dist = 1 - (data_dot-t_cent)/(np_norm(t) * norms) return cos_dist
def compute_intersect_edges_sphere_np(verts_in, edges_in, sphere_loc, radius, result, gates): ''' Calculate all intersections of a sphere with one edges mesh with NumPy and in case of none intersection returns closest point of line and over the sphere. Adapted from Marco13 answer in https://math.stackexchange.com/questions/1905533/ segments are calculated from verts_in and edges_in (regular lists sphere_loc and radius as regular lists or tuples result as a [[], [], [], [], [], [], []] to append the data and gates as a boolean list to return: [mask: valid intersection, inter_a: the intersection nearer to the end point of the segment, inter_b: the intersection nearer to the start point of the segment, inter_a_in_segment: if A intersection is over the segment, inter_b_in_segment: if B intersection is over the segment, first_inter_in_segment: returns the first valid value between Int. A, Int. B and Closest point, inter_with_segment: returns true if there is any intersection in the segment all_inter: returns a flat list of all the intersections out_numpy: return NumPy arrays or regular lists] ''' np_verts = array(verts_in) if not edges_in: edges_in = [[0, -1]] np_edges = array(edges_in) np_centers = array(sphere_loc) np_rad = array(radius) segment_orig = np_verts[np_edges[:, 0]] segment = np_verts[np_edges[:, 1]] - segment_orig segment_mag = np_norm(segment, axis=1) segment_dir = segment / segment_mag[:, newaxis] join_vect = np_centers[:, newaxis] - segment_orig join_vect_proy = np_sum(join_vect * segment_dir, axis=2) closest_point = segment_orig + join_vect_proy[:, :, newaxis] * segment_dir dif_v = closest_point - np_centers[:, newaxis, :] dist = np_norm(dif_v, axis=2) mask = dist > np_rad[:, newaxis] ang = arccos(dist / np_rad[:, newaxis]) offset = np_rad[:, newaxis] * sin(ang) inter_a, inter_b = [], [] inter_a_in_segment, inter_b_in_segment = [], [] first_inter_in_segment, inter_with_segment = [], [] all_inter = [] any_inter = any(gates[5:8]) if gates[1] or any_inter: inter_a = closest_point + segment_dir * offset[:, :, newaxis] inter_a[mask] = closest_point[mask] if gates[2] or any_inter: inter_b = closest_point - segment_dir * offset[:, :, newaxis] inter_b[mask] = closest_point[mask] if gates[3] or any_inter: inter_a_in_segment = np_all( [ join_vect_proy + offset >= 0, join_vect_proy + offset <= segment_mag ], axis=0, ) if gates[4] or any_inter: inter_b_in_segment = np_all( [ join_vect_proy - offset >= 0, join_vect_proy - offset <= segment_mag ], axis=0, ) if gates[5]: first_inter_in_segment = closest_point first_inter_in_segment[inter_b_in_segment] = inter_b[ inter_b_in_segment] first_inter_in_segment[inter_a_in_segment] = inter_a[ inter_a_in_segment] if gates[6]: inter_with_segment = np_any([inter_a_in_segment, inter_b_in_segment], axis=0) if gates[7]: all_inter = concatenate( (inter_a[inter_a_in_segment, :], inter_b[inter_b_in_segment, :]), axis=0)[newaxis, :, :] local_result = [ invert(mask), inter_a, inter_b, inter_a_in_segment, inter_b_in_segment, first_inter_in_segment, inter_with_segment, all_inter, ] for i, res in enumerate(result): if gates[i]: if not gates[8]: for subres in local_result[i].tolist(): res.append(subres) else: res.append(local_result[i])
def linear_metric(x, y): return np_norm(x - y)
def curve_concat_log_metric(x, y, s): return np_norm(logsig(concatenate(x, y), s))
def sparse_norm(data, cent): cent_norm = np_norm(cent) dot_p = data.dot(cent) raw_norms = norm(data, axis=1) norms = np.sqrt(raw_norms**2 - 2*dot_p + cent_norm**2) return norms
fx_dofs = V.sub(0).dofmap().dofs() fy_dofs = V.sub(1).dofmap().dofs() x = coor[:, 0] # x for fx and fy y = coor[:, 1] # y for fx and fy fx_x, fx_y = x[fx_dofs], y[fx_dofs] # x, y of components fy_x, fy_y = x[fy_dofs], y[fy_dofs] print V.dim()/2, len(fx_x) # x and y components of vector function fx = fx_x*fx_y fy = 100+0*fy_x # Insert values of fx and fy into the function fe fe.vector()[fx_dofs] = fx fe.vector()[fy_dofs] = fy # Function in fenics code for testing purposes func = Expression(("x[0]*x[1]","100")) f = interpolate(func, V) ue.assign(f) # Check that the methods give the same result ufunc = fe.vector().array() uexpr = ue.vector().array() print ufunc print uexpr print ufunc - uexpr print 'Match?', near(np_norm(ufunc-uexpr), DOLFIN_EPS)
coor = V.dofmap().tabulate_all_coordinates(mesh).reshape(dim, N) fx_dofs = V.sub(0).dofmap().dofs() fy_dofs = V.sub(1).dofmap().dofs() x = coor[:, 0] # x for fx and fy y = coor[:, 1] # y for fx and fy fx_x, fx_y = x[fx_dofs], y[fx_dofs] # x, y of components fy_x, fy_y = x[fy_dofs], y[fy_dofs] print V.dim() / 2, len(fx_x) # x and y components of vector function fx = fx_x * fx_y fy = 100 + 0 * fy_x # Insert values of fx and fy into the function fe fe.vector()[fx_dofs] = fx fe.vector()[fy_dofs] = fy # Function in fenics code for testing purposes func = Expression(("x[0]*x[1]", "100")) f = interpolate(func, V) ue.assign(f) # Check that the methods give the same result ufunc = fe.vector().array() uexpr = ue.vector().array() print ufunc print uexpr print ufunc - uexpr print 'Match?', near(np_norm(ufunc - uexpr), DOLFIN_EPS)