Skip to content

Commit 08b92f3

Browse files
Release Managervbraun
Release Manager
authored andcommitted
Trac #18084: Fix bad library uses of var()
Sage library code should '''not''' use `var()`! URL: http://trac.sagemath.org/18084 Reported by: jdemeyer Ticket author(s): Jeroen Demeyer Reviewer(s): Ralf Stephan, Karl-Dieter Crisman
2 parents 0ac015f + 52c15f5 commit 08b92f3

File tree

22 files changed

+56
-74
lines changed

22 files changed

+56
-74
lines changed

src/sage/calculus/desolvers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,12 +1188,12 @@ def desolve_rk4(de, dvar, ics=None, ivar=None, end_points=None, step=0.1, output
11881188
ivar = ivars[0]
11891189

11901190
if not is_SymbolicVariable(dvar):
1191-
from sage.calculus.var import var
1191+
from sage.symbolic.ring import SR
11921192
from sage.calculus.all import diff
11931193
from sage.symbolic.relation import solve
11941194
if is_SymbolicEquation(de):
11951195
de = de.lhs() - de.rhs()
1196-
dummy_dvar=var('dummy_dvar')
1196+
dummy_dvar = SR.var('dummy_dvar')
11971197
# consider to add warning if the solution is not unique
11981198
de=solve(de,diff(dvar,ivar),solution_dict=True)
11991199
if len(de) != 1:
@@ -1493,12 +1493,12 @@ def desolve_odeint(des, ics, times, dvars, ivar=None, compute_jac=False, args=()
14931493
if len(ivars)==1:
14941494
ivar = ivars.pop()
14951495
elif not ivars:
1496-
from sage.symbolic.ring import var
14971496
try:
14981497
safe_names = [ 't_' + str(dvar) for dvar in dvars ]
14991498
except TypeError: # not iterable
15001499
safe_names = [ 't_' + str(dvars) ]
1501-
ivar = map(var, safe_names)
1500+
from sage.symbolic.ring import SR
1501+
ivar = [SR.var(name) for name in safe_names]
15021502
else:
15031503
raise ValueError("Unable to determine independent variable, please specify.")
15041504

src/sage/calculus/var.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,23 @@ def var(*args, **kwds):
77
88
INPUT:
99
10-
- ``args`` - A single string ``var('x y')``, a list of strings
10+
- ``args`` -- A single string ``var('x y')``, a list of strings
1111
``var(['x','y'])``, or multiple strings ``var('x', 'y')``. A
1212
single string can be either a single variable name, or a space
1313
or comma separated list of variable names. In a list or tuple of
1414
strings, each entry is one variable. If multiple arguments are
1515
specified, each argument is taken to be one variable. Spaces
1616
before or after variable names are ignored.
1717
18-
- ``kwds`` - keyword arguments can be given to specify domain and
18+
- ``kwds`` -- keyword arguments can be given to specify domain and
1919
custom latex_name for variables. See EXAMPLES for usage.
2020
2121
.. note::
2222
2323
The new variable is both returned and automatically injected
24-
into the global namespace. If you need symbolic variable in
25-
library code, it is better to use either SR.var() or SR.symbol().
24+
into the global namespace. If you need a symbolic variable in
25+
library code, you must use either ``SR.var()``
26+
or ``SR.symbol()``.
2627
2728
OUTPUT:
2829

src/sage/combinat/cluster_algebra_quiver/cluster_seed.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ def interact(self, fig_size=1, circular=True):
231231
from sage.plot.plot import EMBEDDED_MODE
232232
from sagenb.notebook.interact import interact, selector
233233
from sage.misc.all import html,latex
234-
from sage.all import var
235234

236235
if not EMBEDDED_MODE:
237236
return "The interactive mode only runs in the Sage notebook."
@@ -269,8 +268,7 @@ def player(k=selector(values=range(self._n),nrows = 1,label='Mutate at: '), show
269268
html( "Cluster variables:" )
270269
table = "$\\begin{align*}\n"
271270
for i in xrange(self._n):
272-
v = var('v%s'%i)
273-
table += "\t" + latex( v ) + " &= " + latex( self._cluster[i] ) + "\\\\ \\\\\n"
271+
table += "\tv_{%s} &= "%i + latex( self._cluster[i] ) + "\\\\ \\\\\n"
274272
table += "\\end{align*}$"
275273
html( "$ $" )
276274
html( table )

src/sage/combinat/finite_state_machine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,13 +843,13 @@
843843
from sage.rings.integer_ring import ZZ
844844
from sage.rings.real_mpfr import RR
845845
from sage.symbolic.ring import SR
846-
from sage.calculus.var import var
847846
from sage.misc.cachefunc import cached_function
848847
from sage.misc.latex import latex
849848
from sage.misc.misc import verbose
850849
from sage.misc.misc import srange
851850
from sage.functions.trig import cos, sin, atan2
852851
from sage.symbolic.constants import pi
852+
from sage.symbolic.ring import SR
853853

854854
from copy import copy
855855
from copy import deepcopy
@@ -4858,7 +4858,7 @@ def adjacency_matrix(self, input=None,
48584858
48594859
"""
48604860
def default_function(transitions):
4861-
var('x')
4861+
x = SR.var('x')
48624862
return x**sum(transition.word_out)
48634863

48644864
if entry is None:

src/sage/combinat/ribbon_tableau.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,9 +698,9 @@ def spin_polynomial(part, weight, length):
698698
sage: spin_polynomial([[6]*6, [3,3]], [4,4,2], 3)
699699
3*t^9 + 5*t^8 + 9*t^7 + 6*t^6 + 3*t^5
700700
"""
701-
from sage.symbolic.ring import var
701+
from sage.symbolic.ring import SR
702702
sp = spin_polynomial_square(part,weight,length)
703-
t = var('t')
703+
t = SR.var('t')
704704
c = sp.coefficients(sparse=False)
705705
return sum([c[i]*t**(QQ(i)/2) for i in range(len(c))])
706706

src/sage/combinat/sf/hall_littlewood.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#*****************************************************************************
2626

2727
from sage.structure.unique_representation import UniqueRepresentation
28-
from sage.calculus.var import var
2928
from sage.libs.symmetrica.all import hall_littlewood
3029
import sfa
3130
import sage.combinat.partition
@@ -90,8 +89,6 @@ def __init__(self, Sym, t = 't'):
9089
sage: TestSuite(HL).run()
9190
"""
9291
self._sym = Sym
93-
if not (t in Sym.base_ring() or var(t) in Sym.base_ring()):
94-
raise ValueError("parameter t must be in the base ring")
9592
self.t = Sym.base_ring()(t)
9693
self._name_suffix = ""
9794
if str(t) !='t':

src/sage/combinat/sf/jack.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
# http://www.gnu.org/licenses/
3636
#*****************************************************************************
3737
from sage.structure.unique_representation import UniqueRepresentation
38-
from sage.calculus.var import var
3938
import sage.categories.all
4039
from sage.rings.all import Integer, gcd, lcm, QQ
4140
from sage.rings.fraction_field import is_FractionField
@@ -70,8 +69,6 @@ def __init__(self, Sym, t='t'):
7069
Jack polynomials with t=1 over Rational Field
7170
"""
7271
self._sym = Sym
73-
if not (t in Sym.base_ring() or var(t) in Sym.base_ring()):
74-
raise ValueError("parameter t must be in the base ring")
7572
self.t = Sym.base_ring()(t)
7673
self._name_suffix = ""
7774
if str(t) !='t':

src/sage/combinat/sf/llt.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
# http://www.gnu.org/licenses/
3131
#*****************************************************************************
3232
from sage.structure.unique_representation import UniqueRepresentation
33-
from sage.calculus.var import var
3433
import sfa
3534
import sage.combinat.ribbon_tableau as ribbon_tableau
3635
import sage.combinat.skew_partition
@@ -121,8 +120,6 @@ def __init__(self, Sym, k, t='t'):
121120
self._k = k
122121
self._sym = Sym
123122
self._name = "level %s LLT polynomials"%self._k
124-
if not (t in Sym.base_ring() or var(t) in Sym.base_ring()):
125-
raise ValueError("parameter t must be in the base ring")
126123
self.t = Sym.base_ring()(t)
127124
self._name_suffix = ""
128125
if str(t) !='t':

src/sage/combinat/sf/macdonald.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
#*****************************************************************************
4747

4848
from sage.structure.unique_representation import UniqueRepresentation
49-
from sage.calculus.var import var
5049
from sage.categories.morphism import SetMorphism
5150
from sage.categories.homset import Hom
5251
import sfa
@@ -116,15 +115,11 @@ def __init__(self, Sym, q='q', t='t'):
116115
sage: Sym = SymmetricFunctions(FractionField(QQ['t'])).macdonald()
117116
Traceback (most recent call last):
118117
...
119-
ValueError: parameter q must be in the base ring
118+
TypeError: unable to evaluate 'q' in Fraction Field of Univariate Polynomial Ring in t over Rational Field
120119
"""
121120
self._sym = Sym
122121
self._s = Sym.s()
123-
if not (q in Sym.base_ring() or var(q) in Sym.base_ring()):
124-
raise ValueError("parameter q must be in the base ring")
125122
self.q = Sym.base_ring()(q)
126-
if not (t in Sym.base_ring() or var(t) in Sym.base_ring()):
127-
raise ValueError("parameter t must be in the base ring")
128123
self.t = Sym.base_ring()(t)
129124
self._name_suffix = ""
130125
if str(q) !='q':

src/sage/crypto/lwe.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
2013/164. 2013. 2013/164. http://eprint.iacr.org/2013/164
9797
"""
9898

99-
from sage.calculus.var import var
10099
from sage.functions.log import exp, log
101100
from sage.functions.other import sqrt, floor, ceil
102101
from sage.misc.functional import cyclotomic_polynomial
@@ -111,7 +110,7 @@
111110
from sage.structure.element import parent
112111
from sage.structure.sage_object import SageObject
113112
from sage.symbolic.constants import pi
114-
113+
from sage.symbolic.ring import SR
115114
from sage.stats.distributions.discrete_gaussian_integer import DiscreteGaussianDistributionIntegerSampler
116115
from sage.stats.distributions.discrete_gaussian_polynomial import DiscreteGaussianDistributionPolynomialSampler
117116

@@ -428,7 +427,7 @@ def __init__(self, n, delta=0.01, m=None):
428427
# 2*n*(log(c)+(1-c**2)/2) == -40*log(2)
429428
# 2*n*log(c)+n*(1-c**2) == -40*log(2)
430429
# 2*n*log(c)+n*(1-c**2) + 40*log(2) == 0
431-
c = var('c')
430+
c = SR.var('c')
432431
c = find_root(2*n*log(c)+n*(1-c**2) + 40*log(2) == 0, 1, 10)
433432
# Upper bound on s**2/t
434433
s_t_bound = (sqrt(2) * pi / c / sqrt(2*n*log(2/delta))).n()
@@ -627,7 +626,7 @@ def __init__(self, N, delta=0.01, m=None):
627626
m = 3*n
628627
# Find c>=1 such that c*exp((1-c**2)/2))**(2*n) == 2**-40
629628
# i.e c>=1 such that 2*n*log(c)+n*(1-c**2) + 40*log(2) == 0
630-
c = var('c')
629+
c = SR.var('c')
631630
c = find_root(2*n*log(c)+n*(1-c**2) + 40*log(2) == 0, 1, 10)
632631
# Upper bound on s**2/t
633632
s_t_bound = (sqrt(2) * pi / c / sqrt(2*n*log(2/delta))).n()
@@ -821,4 +820,4 @@ def balance_sample(s, q=None):
821820
if scalar:
822821
return vector(ZZ, len(a), [e if e <= q2 else e-q for e in a]), c[0] if c[0] <= q2 else c[0]-q
823822
else:
824-
return vector(ZZ, len(a), [e if e <= q2 else e-q for e in a]), vector(ZZ, len(c), [e if e <= q2 else e-q for e in c])
823+
return vector(ZZ, len(a), [e if e <= q2 else e-q for e in a]), vector(ZZ, len(c), [e if e <= q2 else e-q for e in c])

src/sage/functions/piecewise.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
from sage.symbolic.assumptions import assume, forget
7777

7878
from sage.calculus.calculus import SR, maxima
79-
from sage.calculus.all import var
8079

8180
def piecewise(list_of_pairs, var=None):
8281
"""
@@ -732,7 +731,7 @@ def default_variable(self):
732731
# pass if fun is lambda function
733732
pass
734733
# default to x
735-
v = var('x')
734+
v = SR.var('x')
736735
self.__default_value = v
737736
return v
738737

@@ -1114,7 +1113,7 @@ def fourier_series_cosine_coefficient(self,n,L):
11141113
-3/5/pi
11151114
"""
11161115
from sage.all import cos, pi
1117-
x = var('x')
1116+
x = SR.var('x')
11181117
result = sum([(f(x)*cos(pi*x*n/L)/L).integrate(x, a, b)
11191118
for (a,b), f in self.list()])
11201119
if is_Expression(result):
@@ -1147,7 +1146,7 @@ def fourier_series_sine_coefficient(self,n,L):
11471146
0
11481147
"""
11491148
from sage.all import sin, pi
1150-
x = var('x')
1149+
x = SR.var('x')
11511150
result = sum([(f(x)*sin(pi*x*n/L)/L).integrate(x, a, b)
11521151
for (a,b), f in self.list()])
11531152
if is_Expression(result):
@@ -1515,7 +1514,7 @@ def cosine_series_coefficient(self,n,L):
15151514
15161515
"""
15171516
from sage.all import cos, pi
1518-
x = var('x')
1517+
x = SR.var('x')
15191518
result = sum([(2*f(x)*cos(pi*x*n/L)/L).integrate(x, a, b)
15201519
for (a,b), f in self.list()])
15211520
if is_Expression(result):
@@ -1556,7 +1555,7 @@ def sine_series_coefficient(self,n,L):
15561555
4/3/pi
15571556
"""
15581557
from sage.all import sin, pi
1559-
x = var('x')
1558+
x = SR.var('x')
15601559
result = sum([(2*f(x)*sin(pi*x*n/L)/L).integrate(x, a, b)
15611560
for (a,b), f in self.list()])
15621561
if is_Expression(result):
@@ -1606,8 +1605,8 @@ def laplace(self, x='x', s='t'):
16061605
(s + 1)*e^(-s)/s^2 + 2*e^(-s)/s - 1/s^2
16071606
"""
16081607
from sage.all import assume, exp, forget
1609-
x = var(x)
1610-
s = var(s)
1608+
x = SR.var(x)
1609+
s = SR.var(s)
16111610
assume(s>0)
16121611
result = sum([(SR(f)*exp(-s*x)).integral(x,a,b)
16131612
for (a,b),f in self.list()])

src/sage/geometry/hyperbolic_space/hyperbolic_geodesic.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from sage.functions.trig import sin, cos, arccos
5656
from sage.functions.log import exp
5757
from sage.functions.hyperbolic import sinh, cosh, arcsinh
58-
58+
from sage.symbolic.ring import SR
5959
from sage.geometry.hyperbolic_space.hyperbolic_constants import EPSILON
6060

6161
from sage.misc.lazy_import import lazy_import
@@ -816,9 +816,8 @@ def show(self, boundary=True, **options):
816816
if abs(theta1 - theta2) < EPSILON:
817817
theta2 += pi
818818
[theta1, theta2] = sorted([theta1, theta2])
819-
from sage.calculus.var import var
820819
from sage.plot.plot import parametric_plot
821-
x = var('x')
820+
x = SR.var('x')
822821
pic = parametric_plot((radius*cos(x) + real(center),
823822
radius*sin(x) + imag(center)),
824823
(x, theta1, theta2), **opts)
@@ -1250,9 +1249,8 @@ def show(self, boundary=True, **options):
12501249
theta2 = CC(end_2 - center).arg()
12511250
if theta2 < theta1:
12521251
theta1, theta2 = theta2, theta1
1253-
from sage.calculus.var import var
12541252
from sage.plot.plot import parametric_plot
1255-
x = var('x')
1253+
x = SR.var('x')
12561254
mid = (theta1 + theta2)/2.0
12571255
if (radius*cos(mid) + real(center))**2 + \
12581256
(radius*sin(mid) + imag(center))**2 > 1.0:
@@ -1341,8 +1339,9 @@ def show(self, show_hyperboloid=True, **graphics_options):
13411339
sage: g.show()
13421340
Graphics3d Object
13431341
"""
1344-
from sage.calculus.var import var
1345-
(x,y,z) = var('x,y,z')
1342+
x = SR.var('x')
1343+
y = SR.var('y')
1344+
z = SR.var('z')
13461345
opts = self.graphics_options()
13471346
opts.update(graphics_options)
13481347
v1,u2 = [vector(k.coordinates()) for k in self.endpoints()]

src/sage/geometry/hyperbolic_space/hyperbolic_model.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,11 +1476,12 @@ def get_background_graphic(self, **bdry_options):
14761476
sage: H = HyperbolicPlane().HM().get_background_graphic()
14771477
"""
14781478
from sage.plot.plot3d.all import plot3d
1479-
from sage.all import var
1479+
from sage.all import SR
14801480
hyperboloid_opacity = bdry_options.get('hyperboloid_opacity', .1)
14811481
z_height = bdry_options.get('z_height', 7.0)
14821482
x_max = sqrt((z_height ** 2 - 1) / 2.0)
1483-
(x, y) = var('x,y')
1483+
x = SR.var('x')
1484+
y = SR.var('y')
14841485
return plot3d((1 + x ** 2 + y ** 2).sqrt(),
14851486
(x, -x_max, x_max), (y,-x_max, x_max),
14861487
opacity=hyperboloid_opacity, **bdry_options)

src/sage/geometry/riemannian_manifolds/parametrized_surface3d.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,12 +1505,12 @@ def _create_geodesic_ode_system(self):
15051505
15061506
"""
15071507
from sage.ext.fast_eval import fast_float
1508-
from sage.calculus.var import var
15091508
from sage.gsl.ode import ode_solver
15101509

15111510
u1 = self.variables[1]
15121511
u2 = self.variables[2]
1513-
v1, v2 = var('v1, v2', domain='real')
1512+
v1 = SR.var('v1', domain='real')
1513+
v2 = SR.var('v2', domain='real')
15141514

15151515
C = self.connection_coefficients()
15161516

@@ -1618,12 +1618,12 @@ def _create_pt_ode_system(self, curve, t):
16181618
"""
16191619

16201620
from sage.ext.fast_eval import fast_float
1621-
from sage.calculus.var import var
16221621
from sage.gsl.ode import ode_solver
16231622

16241623
u1 = self.variables[1]
16251624
u2 = self.variables[2]
1626-
v1, v2 = var('v1, v2', domain='real')
1625+
v1 = SR.var('v1', domain='real')
1626+
v2 = SR.var('v2', domain='real')
16271627

16281628
du1 = diff(curve[0], t)
16291629
du2 = diff(curve[1], t)

0 commit comments

Comments
 (0)