Path: blob/master/sage/ext/interactive_constructors_c.pyx
4045 views
# Optional versions of certain ring constructors that automatically1# inject variables into the global module scope.23import sage.rings.all45_verbose=True6_inject_mode_off = False78def inject_verbose(mode):9global _verbose10_verbose= mode1112_original_constructors = None1314def inject_on(verbose=True):15"""16Replace several constructors by versions that inject their17variables into the global namespace.1819INPUT:20verbose -- (default: True) if True, print which constructors21become interactive, and also print variables as22they are implicitly defined.2324EXAMPLES:25sage: inject_on(verbose=True)26Redefining: FiniteField Frac FractionField FreeMonoid GF LaurentSeriesRing NumberField PolynomialRing quo quotient27sage: GF(9,'b')28Defining b29Finite Field in b of size 3^230sage: b^3312*b + 132sage: inject_off()33sage: GF(9,'c')34Finite Field in c of size 3^235sage: c^336Traceback (most recent call last):37...38NameError: name 'c' is not defined39sage: inject_on(verbose=False)40sage: GF(9,'c')41Finite Field in c of size 3^242sage: c^3432*c + 14445ROLL YOUR OWN: If a constructor you would like to auto inject46variables isn't made to do so by running this command your options47are:48(1) Make your own constructor (factory function) using the explicit49inject_variables() method. This is *very* easy:5051sage: def poly(*args, **kwds):52... R = PolynomialRing(*args, **kwds)53... R.inject_variables()54... return R55sage: R = poly(QQ, 'z')56Defining z57sage: z^3 + 358z^3 + 35960(2) Add code to do it to devel/sage/sage/ext/interactive_constructors_c.pyx,61rebuild Sage (with sage -br), and send William Stein a patch :-).62"""63global _verbose64_verbose = verbose65global _original_constructors66_original_constructors = {}67import sage.ext.interactive_constructors_c68G = globals()69if verbose:70print "Redefining:",71for X in sorted(sage.ext.interactive_constructors_c.__dict__.keys()):72if not 'inject' in X and X[0] != '_' and X[:4] != 'sage':73if verbose:74print X,75try:76_original_constructors[X] = G[X] #sage.ext.interactive_constructors_c.__dict__[X]77except KeyError:78pass79G[X] = sage.ext.interactive_constructors_c.__dict__[X]80if verbose:81print ""8283def inject_off():84global _original_constructors85if not _original_constructors is None:86for X in _original_constructors.keys():87globals()[X] = _original_constructors[X]8889cdef _inject(X, do):90if do:91X.inject_variables(verbose=_verbose)92return X9394cdef _do_inject(kwds):95if kwds.has_key('inject'):96s = kwds['inject']97del kwds['inject']98return s == True99return True100101def FiniteField(*args, **kwds):102"""103Construct a finite field and inject the variables of the104finite field to the global interactive interpreter. Use105inject=False to not inject the variables. This is a wrapper106around the following function: <<<FiniteField>>>107"""108t = _do_inject(kwds)109R = sage.rings.all.FiniteField(*args, **kwds)110return _inject(R, t)111112GF = FiniteField113114def FractionField(*args, **kwds):115"""116Construct the fraction field of a field and inject the generators117of the fraction field to the global interactive interpreter. Use118inject=False to not inject the variables. This is a wrapper119around the following function: <<<FractionField>>>120121EXAMPLES (that illustrate interactive injection of variables):122sage: inject_on(verbose=False)123sage: Frac(QQ['x'])124Fraction Field of Univariate Polynomial Ring in x over Rational Field125sage: parent(x)126Fraction Field of Univariate Polynomial Ring in x over Rational Field127"""128t = _do_inject(kwds)129R = sage.rings.all.FractionField(*args, **kwds)130return _inject(R, t)131132Frac = FractionField133134135def FreeMonoid(*args, **kwds):136"""137Construct a free monoid and inject the variables of the monoid138into the global interactive interpreter. Use inject=Fale to not139inject the variables. This is a wrapper around the following140function: <<<FreeMonoid>>>141142EXAMPLES:143We illustrate creating a free monoid with and without injecting144the variables into the interpreter.145146sage: inject_on(verbose=False)147sage: FreeMonoid(4,'x')148Free monoid on 4 generators (x0, x1, x2, x3)149sage: x2150x2151sage: FreeMonoid(4,'y', inject=False)152Free monoid on 4 generators (y0, y1, y2, y3)153sage: y0154Traceback (most recent call last):155...156NameError: name 'y0' is not defined157"""158t = _do_inject(kwds)159R = sage.monoids.free_monoid.FreeMonoid(*args, **kwds)160return _inject(R, t)161162def LaurentSeriesRing(*args, **kwds):163"""164Construct the Laurent series ring over a ring, and inject the165generator into the interpreter's global namespace. Use166inject=False to not inject the variables. This is a wrapper167around the following function:168169<<<LaurentSeries>>>170"""171t = _do_inject(kwds)172R = sage.rings.all.LaurentSeriesRing(*args, **kwds)173return _inject(R, t)174175def NumberField(*args, **kwds):176"""177Construct a number field, and inject the generator of the number178fraction field into the interpreters global namespace. Use179inject=False to not inject the variables. This is a wrapper180around the following function:181<<<NumberField>>>182"""183t = _do_inject(kwds)184R = sage.rings.all.NumberField(*args, **kwds)185return _inject(R, t)186187def quotient(R, I, names, inject=True):188"""189Construct the quotient R/I and name the generators, which are190then injected into the module scope (if inject=True).191192EXAMPLES:193sage: inject_on(verbose=False)194sage: R = PolynomialRing(QQ, 'x,y')195sage: S = quo(R, (x^3, x^2 + y^2), 'a,b')196sage: S197Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^3, x^2 + y^2)198sage: a^2199-b^2200sage: a^32010202sage: a^2 + b203-b^2 + b204sage: a^2 + b^22050206"""207Q = R.quotient(I, names)208return _inject(Q, inject)209210quo = quotient211212213def PolynomialRing(*args, **kwds):214"""215Construct a polynomial ring and inject the variables of the216polynomial ring to the global interactive interpreter. Use217inject=False to not inject the variables. This is a wrapper218around the following function: <<<PolynomialRing>>>219220MORE EXAMPLES:221We illustrate creating a polynomial ring without injecting the variables222into the interpreter.223224sage: inject_on(verbose=False)225sage: PolynomialRing(QQ,'w')226Univariate Polynomial Ring in w over Rational Field227sage: parent(w)228Univariate Polynomial Ring in w over Rational Field229sage: PolynomialRing(GF(17), 'w', inject=False)230Univariate Polynomial Ring in w over Finite Field of size 17231sage: parent(w)232Univariate Polynomial Ring in w over Rational Field233"""234t = _do_inject(kwds)235R = sage.rings.all.PolynomialRing(*args, **kwds)236return _inject(R, t)237238239240###################### need to add a bunch more ############################241242243244