def think(self, features): outputs = [] # add the first layer outputs.append(self.sigmoid(npdot(features, self.layers[0].weights))) for index in range(1, len(self.layers)): # calculate all successive output layers outputs.append( self.sigmoid( npdot(outputs[len(outputs) - 1], self.layers[index].weights))) return outputs
def test_dot_list(self): n = 10 a = rand(n,3) b = rand(n,3) np = zeros(n) for i in xrange(n): np[i] = npdot(a[i],b[i]) assert_allclose(np,dot(a,b))
def rigid_transform_3D(MatA, MatB): ''' Pass in 2 numpy arrays to get the R and t ''' assert len(MatA) == len(MatB) N = MatA.shape[0] comA = npmean(MatA, axis=0) comB = npmean(MatB, axis=0) A = MatA - nptile(comA, (N, 1)) B = MatB - nptile(comB, (N, 1)) H = npdot(nptranspose(A), B) U, S, V = nplinalgsvd(H) R = npdot(nptranspose(V), nptranspose(U)) if nplinalgdet(R) < 0: V[2, :] *= -1 R = npdot(nptranspose(V), nptranspose(U)) t = -npdot(R, nptranspose(comA)) + nptranspose(comB) return R, t
def compute(self, get): u1 = get(self.valuename1) u2 = get(self.valuename2) if u1 is None or u2 is None: return if not (isinstance(u1, GenericFunction) or isinstance(u2, GenericFunction)): return npdot(u1, u2) if not isinstance(u1, GenericFunction): u1 = Constant(u1) u1, u2 = u2, u1 if not isinstance(u2, GenericFunction): u2 = Constant(u2) if isinstance(u2, Function): u1, u2 = u2, u1 assert isinstance(u1, Function) assert isinstance(u2, GenericFunction) if u1.value_rank() == u2.value_rank(): if u1.value_rank() == 0: V = u1.function_space() else: V = u1.function_space().sub(0).collapse() elif u1.value_rank() > u2.value_rank(): assert u2.value_rank() == 0 V = u1.function_space() u1, u2 = u2, u1 else: assert isinstance(u2, Function) assert u1.value_rank() == 0 V = u2.function_space() #N = max([u1.value_rank(), u2.value_rank()]) if not hasattr(self, "u"): self.u = Function(V) if isinstance(u2, Function) and u1.function_space().dim( ) == u2.function_space().dim() and u1.value_rank() == 0: self.u.vector()[:] = u1.vector().array() * u2.vector().array() elif u1.value_rank() == u2.value_rank(): project(dot(u1, u2), function=self.u, V=self.u.function_space()) else: assert u1.value_rank() == 0 if isinstance(u1, Constant): self.u.vector()[:] = float(u1) * u2.vector().array() else: project(u1 * u2, function=self.u, V=self.u.function_space()) return self.u
def test_dot_matrix(self): n = 10 a = rand(n,n,3) b = rand(n,n,3) np = zeros((n,n)) for i in xrange(n): for j in xrange(n): np[i,j] = npdot(a[i,j],b[i,j]) assert_allclose(np,dot(a,b))
def forward(self): """ Set self.value to the value of the linear function output. Your code goes here! """ # from numpy import multiply as npmultiply # self.value = sum(npmultiply(self.inbound_nodes[0].value, self.inbound_nodes[1].value)) + self.inbound_nodes[2].value from numpy import dot as npdot self.value = npdot( self.inbound_nodes[0].value, self.inbound_nodes[1].value) + self.inbound_nodes[2].value
def tmalign(pose, ref_pose): ''' Takes a pose (with or without a ligand) and overlays it with the ref pose Assumes the ligand (if any) is in the pose to be moved input: pose, reference_pose output: atommap, and tm object use: atommap , tm = tmalign(model,crystal) ''' print 'Running tmalign on poses' print 'Starting xyz coords for res1 CA pose and reference pose' print pose.residue(1).xyz('CA') print ref_pose.residue(1).xyz('CA') ligs = False listlig = [] for i in range(1, pose.total_residue() + 1): if pose.residue(i).is_ligand(): listlig.append(i) if len(listlig) > 0: print 'Ligand detected, will also overlay' ligs = True copy = Pose() copy.assign(pose) offset = len(listlig) # construct the TMalign object tm = rosetta.protocols.hybridization.TMalign() tm.apply(pose, ref_pose) longest = max(pose.n_residue() + 1, ref_pose.n_residue() + 1) print 'TMScore = %s ' % tm.TMscore(longest) #print tm.TMscore(longest) # Now pull the atom mapping from tmalign # tmalign makes it's own alignment so we use that to do the ''partial'' threading # some setup for alignment2AtomMap method atommap = rosetta.core.id.AtomID_Map_T_core_id_AtomID_T() # atommap = rosetta.core.id.AtomID_Map_core_id_AtomID_t() rosetta.core.pose.initialize_atomid_map(atommap, pose) tm.alignment2AtomMap(pose, ref_pose, atommap) # some setup for partial thread aln_cutoff = rosetta.utility.vector1_Real() # aln_cutoff = rosetta.utility.vector1_double() for i in [2, 1.5, 1.0, .5]: aln_cutoff.append(i) # min_coverage = .2 min_coverage = .5 rosetta.protocols.hybridization.partial_align(pose, ref_pose, atommap, True, aln_cutoff, min_coverage) print 'Hopefully these coordinates have changed, use the PyMolMover / Observer to watch in realtime' print pose.residue(1).xyz('CA') print ref_pose.residue(1).xyz('CA') print "Checking for ligand, will move it too!" if ligs: precoord = [] postcoord = [] for i in range(1, pose.total_residue() + 1 - offset): try: pre_i = copy.residue(i).atom('CA').xyz() prexyz = [pre_i.x, pre_i.y, pre_i.z] precoord.append(prexyz) post_i = pose.residue(i).atom('CA').xyz() postxyz = [post_i.x, post_i.y, post_i.z] postcoord.append(postxyz) except: pass premat = nparray(precoord, dtype=npfloat64) postmat = nparray(postcoord, dtype=npfloat64) R, t = rigid_transform_3D(premat, postmat) for j in listlig: for k in range(1, pose.residue(j).natoms() + 1): ligatm = pose.residue(j).atom(k) prevec = nparray( [ligatm.xyz().x, ligatm.xyz().y, ligatm.xyz().z], dtype=npfloat64) postvec = npdot(R, prevec) + t newrosettavec = rosetta.numeric.xyzVector_Real( postvec[0], postvec[1], postvec[2]) pose.set_xyz(rosetta.core.id.AtomID(k, j), newrosettavec) return (atommap, tm)
def reflection(cls, s1, s2): v1 = s1.vector v2 = s2.vector v_reflected = (2 * npdot(v1, v2) * v2) - v1 return cls(v_reflected)
def dot(s1, s2): real = npdot(s1.vector, s2.vector) return real
def dot(matrices): res = matrices[0] for m in matrices[1:]: res = npdot(res, m) return res
L = f*v*dx # load vector form bc = DirichletBC(V, Constant(0), DomainBoundary()) # homog. boundary condition # check the matrices h = mesh.hmin() A = assemble(a) M = assemble(m) bc.apply(A); bc.apply(M) # the matrices include entries for phi_0, phi_10 # check the load vector first, for later we will cancel h b = assemble(L) bc.apply(b) F = interpolate(f, V).vector().array() b_hand = npdot(M.array(), F) b_hand[0] = 0; b_hand[-1] = 0 # set the bcs b[:] -= b_hand print "Is b okay", b.norm("l1") < 1E-15, "\n" # remove the phi_0, phi_10 entries for easier comparison h = mesh.hmin() A = A.array()[1:-1, 1:-1]*h M = M.array()[1:-1, 1:-1]*6/h print "A", "\n", A, "\n" print "M", "\n", M, "\n" # if visual comparison is not enough A_col = zeros(9); A_col[0] = 2; A_col[1] = -1;
u = TrialFunction(V) v = TestFunction(V) bc = DirichletBC(V, Constant(0), DomainBoundary()) A, _ = assemble_system(inner(grad(u), grad(v))*dx, Constant(0)*v*dx, bc) M, _ = assemble_system(u*v*dx, Constant(0)*v*dx, bc) e, l = octave.eig(A.array(), M.array()) e = matrix(e) l = matrix(l) u = interpolate(Expression("sin(k*pi*x[0])", k=1), V) U = matrix(u.vector().array()) # norm from assemble aL2 = sqrt(assemble(u**2*dx)) aH10 = sqrt(assemble(inner(grad(u), grad(u))*dx)) M = matrix(M.array()) # cast M so that numpy can work with it eL2, eH10 = 0, 0 for k in range(V.dim()): F = matrix(e[:, k]) # select k-th eigenvector lmbda = l[k, k] # select k-th eigenvalue eL2 += npdot(npdot(U, M), F)**2 eH10 += lmbda*(npdot(npdot(U, M), F))**2 eL2 = msqrt(eL2); eH10 = msqrt(eH10) diffL2 = abs(eL2 - aL2); diffH10 = abs(eH10 - aH10) print "L2 assemble %g, eigenvalues %g, diff %g" % (aL2, eL2, diffL2) print "H10 assemble %g, eigenvalues %g, diff %g" % (aH10, eH10, diffH10)
for i in range(n): if abs(eign[i].real) < 10*DOLFIN_EPS: null_space_dim += 1 basis_vectors.append(eigv[:, i]) print "Nullspace has dim", null_space_dim, "\n" # orthogonalize the eigenvectors basis_vectors, R = qr(array(basis_vectors).T) basis_vectors = basis_vectors.real # test the vectors and build the nullspace for Krylov print "Basis test" null_vectors = [] for i in range(null_space_dim): print "Basis vector %d of nullspace ?" % i, print all(abs(npdot(A.array(), basis_vectors[:, i])) < 100*DOLFIN_EPS) fun = Function(V) fun_v = fun.vector() fun_v[:] = array(basis_vectors[:, i].tolist()) plot(fun, interactive=True, title="QR basis") null_vectors.append(fun.vector()) null_space = VectorSpaceBasis(null_vectors) null_space.orthogonalize(b) solver.set_nullspace(null_space) solver.solve(u_QR.vector(), b) # ONES
def _compute(x): return npdot(x, w) / total_weight
def dot( self, vector ): """ Returns the dot product of this vector and the given vector """ return npdot( self, vector ) # homogenous doesn't matter