Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/ext/interactive_constructors_c.pyx
4045 views
1
# Optional versions of certain ring constructors that automatically
2
# inject variables into the global module scope.
3
4
import sage.rings.all
5
6
_verbose=True
7
_inject_mode_off = False
8
9
def inject_verbose(mode):
10
global _verbose
11
_verbose= mode
12
13
_original_constructors = None
14
15
def inject_on(verbose=True):
16
"""
17
Replace several constructors by versions that inject their
18
variables into the global namespace.
19
20
INPUT:
21
verbose -- (default: True) if True, print which constructors
22
become interactive, and also print variables as
23
they are implicitly defined.
24
25
EXAMPLES:
26
sage: inject_on(verbose=True)
27
Redefining: FiniteField Frac FractionField FreeMonoid GF LaurentSeriesRing NumberField PolynomialRing quo quotient
28
sage: GF(9,'b')
29
Defining b
30
Finite Field in b of size 3^2
31
sage: b^3
32
2*b + 1
33
sage: inject_off()
34
sage: GF(9,'c')
35
Finite Field in c of size 3^2
36
sage: c^3
37
Traceback (most recent call last):
38
...
39
NameError: name 'c' is not defined
40
sage: inject_on(verbose=False)
41
sage: GF(9,'c')
42
Finite Field in c of size 3^2
43
sage: c^3
44
2*c + 1
45
46
ROLL YOUR OWN: If a constructor you would like to auto inject
47
variables isn't made to do so by running this command your options
48
are:
49
(1) Make your own constructor (factory function) using the explicit
50
inject_variables() method. This is *very* easy:
51
52
sage: def poly(*args, **kwds):
53
... R = PolynomialRing(*args, **kwds)
54
... R.inject_variables()
55
... return R
56
sage: R = poly(QQ, 'z')
57
Defining z
58
sage: z^3 + 3
59
z^3 + 3
60
61
(2) Add code to do it to devel/sage/sage/ext/interactive_constructors_c.pyx,
62
rebuild Sage (with sage -br), and send William Stein a patch :-).
63
"""
64
global _verbose
65
_verbose = verbose
66
global _original_constructors
67
_original_constructors = {}
68
import sage.ext.interactive_constructors_c
69
G = globals()
70
if verbose:
71
print "Redefining:",
72
for X in sorted(sage.ext.interactive_constructors_c.__dict__.keys()):
73
if not 'inject' in X and X[0] != '_' and X[:4] != 'sage':
74
if verbose:
75
print X,
76
try:
77
_original_constructors[X] = G[X] #sage.ext.interactive_constructors_c.__dict__[X]
78
except KeyError:
79
pass
80
G[X] = sage.ext.interactive_constructors_c.__dict__[X]
81
if verbose:
82
print ""
83
84
def inject_off():
85
global _original_constructors
86
if not _original_constructors is None:
87
for X in _original_constructors.keys():
88
globals()[X] = _original_constructors[X]
89
90
cdef _inject(X, do):
91
if do:
92
X.inject_variables(verbose=_verbose)
93
return X
94
95
cdef _do_inject(kwds):
96
if kwds.has_key('inject'):
97
s = kwds['inject']
98
del kwds['inject']
99
return s == True
100
return True
101
102
def FiniteField(*args, **kwds):
103
"""
104
Construct a finite field and inject the variables of the
105
finite field to the global interactive interpreter. Use
106
inject=False to not inject the variables. This is a wrapper
107
around the following function: <<<FiniteField>>>
108
"""
109
t = _do_inject(kwds)
110
R = sage.rings.all.FiniteField(*args, **kwds)
111
return _inject(R, t)
112
113
GF = FiniteField
114
115
def FractionField(*args, **kwds):
116
"""
117
Construct the fraction field of a field and inject the generators
118
of the fraction field to the global interactive interpreter. Use
119
inject=False to not inject the variables. This is a wrapper
120
around the following function: <<<FractionField>>>
121
122
EXAMPLES (that illustrate interactive injection of variables):
123
sage: inject_on(verbose=False)
124
sage: Frac(QQ['x'])
125
Fraction Field of Univariate Polynomial Ring in x over Rational Field
126
sage: parent(x)
127
Fraction Field of Univariate Polynomial Ring in x over Rational Field
128
"""
129
t = _do_inject(kwds)
130
R = sage.rings.all.FractionField(*args, **kwds)
131
return _inject(R, t)
132
133
Frac = FractionField
134
135
136
def FreeMonoid(*args, **kwds):
137
"""
138
Construct a free monoid and inject the variables of the monoid
139
into the global interactive interpreter. Use inject=Fale to not
140
inject the variables. This is a wrapper around the following
141
function: <<<FreeMonoid>>>
142
143
EXAMPLES:
144
We illustrate creating a free monoid with and without injecting
145
the variables into the interpreter.
146
147
sage: inject_on(verbose=False)
148
sage: FreeMonoid(4,'x')
149
Free monoid on 4 generators (x0, x1, x2, x3)
150
sage: x2
151
x2
152
sage: FreeMonoid(4,'y', inject=False)
153
Free monoid on 4 generators (y0, y1, y2, y3)
154
sage: y0
155
Traceback (most recent call last):
156
...
157
NameError: name 'y0' is not defined
158
"""
159
t = _do_inject(kwds)
160
R = sage.monoids.free_monoid.FreeMonoid(*args, **kwds)
161
return _inject(R, t)
162
163
def LaurentSeriesRing(*args, **kwds):
164
"""
165
Construct the Laurent series ring over a ring, and inject the
166
generator into the interpreter's global namespace. Use
167
inject=False to not inject the variables. This is a wrapper
168
around the following function:
169
170
<<<LaurentSeries>>>
171
"""
172
t = _do_inject(kwds)
173
R = sage.rings.all.LaurentSeriesRing(*args, **kwds)
174
return _inject(R, t)
175
176
def NumberField(*args, **kwds):
177
"""
178
Construct a number field, and inject the generator of the number
179
fraction field into the interpreters global namespace. Use
180
inject=False to not inject the variables. This is a wrapper
181
around the following function:
182
<<<NumberField>>>
183
"""
184
t = _do_inject(kwds)
185
R = sage.rings.all.NumberField(*args, **kwds)
186
return _inject(R, t)
187
188
def quotient(R, I, names, inject=True):
189
"""
190
Construct the quotient R/I and name the generators, which are
191
then injected into the module scope (if inject=True).
192
193
EXAMPLES:
194
sage: inject_on(verbose=False)
195
sage: R = PolynomialRing(QQ, 'x,y')
196
sage: S = quo(R, (x^3, x^2 + y^2), 'a,b')
197
sage: S
198
Quotient of Multivariate Polynomial Ring in x, y over Rational Field by the ideal (x^3, x^2 + y^2)
199
sage: a^2
200
-b^2
201
sage: a^3
202
0
203
sage: a^2 + b
204
-b^2 + b
205
sage: a^2 + b^2
206
0
207
"""
208
Q = R.quotient(I, names)
209
return _inject(Q, inject)
210
211
quo = quotient
212
213
214
def PolynomialRing(*args, **kwds):
215
"""
216
Construct a polynomial ring and inject the variables of the
217
polynomial ring to the global interactive interpreter. Use
218
inject=False to not inject the variables. This is a wrapper
219
around the following function: <<<PolynomialRing>>>
220
221
MORE EXAMPLES:
222
We illustrate creating a polynomial ring without injecting the variables
223
into the interpreter.
224
225
sage: inject_on(verbose=False)
226
sage: PolynomialRing(QQ,'w')
227
Univariate Polynomial Ring in w over Rational Field
228
sage: parent(w)
229
Univariate Polynomial Ring in w over Rational Field
230
sage: PolynomialRing(GF(17), 'w', inject=False)
231
Univariate Polynomial Ring in w over Finite Field of size 17
232
sage: parent(w)
233
Univariate Polynomial Ring in w over Rational Field
234
"""
235
t = _do_inject(kwds)
236
R = sage.rings.all.PolynomialRing(*args, **kwds)
237
return _inject(R, t)
238
239
240
241
###################### need to add a bunch more ############################
242
243
244