def prep_raw(inp, names={}): """ Prepare an input string for being passed as a ``$raw`` value to the database search. INPUT: - ``inp`` -- a string from the website. Aleady split up by commas and .. range indicators - ``names`` -- a dictionary providing a translation from user input to column names. Only keys in the dictionary are accepted. OUTPUT: A string with implicit multiplications inserted and full column names substituted for short names This function will raise a SearchParsingError if there is a syntax error or if there is a variable that's not in the names list """ inp = implicit_mul(inp, level=10) # level = 10 includes (a+b)(c+d) -> (a+b)*(c+d) which isn't safe in Sage but should be okay for us def filtered_var(s): if s not in names: raise SearchParsingError("%s is not a column of this table" % s) return var(s) # We use Sage's parser to make sure that the user input is well formed P = Parser(make_var=filtered_var) try: P.parse_expression(inp) except SyntaxError: raise SearchParsingError("syntax error") pieces = re.split(r'([A-Za-z_]+)', inp) processed = [] for piece in pieces: if piece in names: processed.append(names[piece]) else: processed.append(piece) return {'$raw': "".join(processed)}
def _element_constructor_(self, a=None, check=True, construct=False, **kwds): r""" Convert a base ring element ``a`` into a constant of this univariate skew polynomial ring, possibly non-canonically. INPUT: - ``a`` -- (default: ``None``) an element of the base ring of ``self`` or a ring that has a coerce map from ``self`` - ``check`` -- boolean (default: ``True``) - ``construct`` -- boolean (default: ``False``) OUTPUT: An zero-degree skew polynomial in ``self``, equal to ``a``. EXAMPLES:: sage: R.<t> = ZZ[] sage: sigma = R.hom([t+1]) sage: S.<x> = SkewPolynomialRing(R,sigma) sage: S(1 + x + x^2 + x^3) x^3 + x^2 + x + 1 sage: S(1 + t) t + 1 sage: S(1 + t).degree() 0 sage: S(0).list() [] """ C = self._polynomial_class if isinstance(a, list): return C(self, a, check=check, construct=construct) if isinstance(a, Element): P = a.parent() def build(check): if a.is_zero(): return P.zero() else: return C(self, [a], check=check, construct=construct) if P is self: return a elif P is self.base_ring(): build(False) elif P == self.base_ring() or self.base_ring().has_coerce_map_from(P): build(True) try: return a._polynomial_(self) except AttributeError: pass if isinstance(a, str): try: from sage.misc.parser import Parser, LookupNameMaker R = self.base_ring() p = Parser(Integer, R, LookupNameMaker({self.variable_name(): self.gen()}, R)) return self(p.parse(a)) except NameError: raise TypeError("unable to coerce string") return C(self, a, check, construct=construct, **kwds)
def _element_constructor_(self, a=None, check=True, construct=False, **kwds): r""" Convert a base ring element ``a`` into a constant of this univariate skew polynomial ring, possibly non-canonically. INPUT: - ``a`` -- (default: ``None``) an element of the base ring of ``self`` or a ring that has a coerce map from ``self`` - ``check`` -- boolean (default: ``True``) - ``construct`` -- boolean (default: ``False``) OUTPUT: An zero-degree skew polynomial in ``self``, equal to ``a``. EXAMPLES:: sage: R.<t> = ZZ[] sage: sigma = R.hom([t+1]) sage: S.<x> = SkewPolynomialRing(R,sigma) sage: S(1 + x + x^2 + x^3) x^3 + x^2 + x + 1 sage: S(1 + t) t + 1 sage: S(1 + t).degree() 0 sage: S(0).list() [] """ C = self._polynomial_class if isinstance(a, list): return C(self, a, check=check, construct=construct) if isinstance(a, Element): P = a.parent() def build(check): if a.is_zero(): return P.zero() else: return C(self, [a], check=check, construct=construct) if P is self: return a elif P is self.base_ring(): build(False) elif P == self.base_ring() or self.base_ring().has_coerce_map_from( P): build(True) try: return a._polynomial_(self) except AttributeError: pass if isinstance(a, str): try: from sage.misc.parser import Parser, LookupNameMaker R = self.base_ring() p = Parser( Integer, R, LookupNameMaker({self.variable_name(): self.gen()}, R)) return self(p.parse(a)) except NameError: raise TypeError("unable to coerce string") return C(self, a, check, construct=construct, **kwds)
tr = works.popitem() comm.send((tr[0], tr[1]['PD'], tr[1]['homfly']), dest=nodo) else: comm.send('End',dest=nodo) unfinishednodes.remove(nodo) print "the polynomials don't match in the following cases" print [res for res in results if not res[-1]] print "Finished" else: from sage.misc.parser import Parser R = LaurentPolynomialRing(ZZ,2, 'a,z') (a,z) = R.gens() parser = Parser(make_var={'z':z, 'a':a}) while True: link = comm.recv() if link == 'End': break K = Knot(link[1]) f1 = parser.parse(link[2]) f2 = K.mirror_image().homfly_polynomial('a','z', normalization='az') igual = bool(f1==f2) comm.send(([link[0], f1, f2, igual],rank), dest=0) comm.Barrier()