Skip to content

Commit 98bf731

Browse files
author
Release Manager
committed
gh-35450: Replace remaining uses of `is_SymbolicEquation`, `is_SymbolicVariable` <!-- Please provide a concise, informative and self-explanatory title. --> <!-- Don't put issue numbers in the title. Put it in the Description below. --> <!-- For example, instead of "Fixes #12345", use "Add a new method to multiply two integers" --> ### 📚 Description <!-- Describe your changes here in detail. --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes #12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> - Follow up from #32665 - Part of #32414 ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. It should be `[x]` not `[x ]`. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - #12345: short description why this is a dependency - #34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: #35450 Reported by: Matthias Köppe Reviewer(s):
2 parents 358d64d + ae7db7e commit 98bf731

File tree

13 files changed

+68
-60
lines changed

13 files changed

+68
-60
lines changed

src/sage/calculus/calculus.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,9 @@
424424

425425
from sage.misc.latex import latex
426426
from sage.misc.parser import Parser, LookupNameMaker
427-
428-
from sage.symbolic.ring import var, SR, is_SymbolicVariable
429-
from sage.symbolic.expression import Expression, symbol_table
427+
from sage.structure.element import Expression
428+
from sage.symbolic.ring import var, SR
429+
from sage.symbolic.expression import symbol_table
430430
from sage.symbolic.function import Function
431431
from sage.symbolic.function_factory import function_factory
432432
from sage.symbolic.integration.integral import (indefinite_integral,
@@ -613,7 +613,7 @@ def symbolic_sum(expression, v, a, b, algorithm='maxima', hold=False):
613613
the summation the result might not be convertible into a Sage
614614
expression.
615615
"""
616-
if not is_SymbolicVariable(v):
616+
if not (isinstance(v, Expression) and v.is_symbol()):
617617
if isinstance(v, str):
618618
v = var(v)
619619
else:
@@ -879,7 +879,7 @@ def symbolic_product(expression, v, a, b, algorithm='maxima', hold=False):
879879
(-1)^n*factorial(n)^2
880880
881881
"""
882-
if not is_SymbolicVariable(v):
882+
if not (isinstance(v, Expression) and v.is_symbol()):
883883
if isinstance(v, str):
884884
v = var(v)
885885
else:

src/sage/calculus/desolvers.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@
7777

7878
from sage.interfaces.maxima import Maxima
7979
from sage.misc.lazy_import import lazy_import
80-
lazy_import("sage.plot.all", "line")
81-
from sage.symbolic.expression import is_SymbolicEquation
82-
from sage.symbolic.ring import SR, is_SymbolicVariable
83-
from sage.calculus.functional import diff
8480
from sage.misc.functional import N
8581
from sage.rings.real_mpfr import RealField
82+
from sage.structure.element import Expression
83+
84+
from .functional import diff
8685

8786

8887
maxima = Maxima()
@@ -550,9 +549,9 @@ def desolve(de, dvar, ics=None, ivar=None, show_method=False, contrib_ode=False,
550549
- Robert Bradshaw (10-2008)
551550
- Robert Marik (10-2009)
552551
"""
553-
if is_SymbolicEquation(de):
552+
if isinstance(de, Expression) and de.is_relational():
554553
de = de.lhs() - de.rhs()
555-
if is_SymbolicVariable(dvar):
554+
if isinstance(dvar, Expression) and dvar.is_symbol():
556555
raise ValueError("You have to declare dependent variable as a function evaluated at the independent variable, eg. y=function('y')(x)")
557556
# for backwards compatibility
558557
if isinstance(dvar, list):
@@ -600,8 +599,8 @@ def sanitize_var(exprs):
600599
if show_method:
601600
maxima_method=P("method")
602601

603-
if (ics is not None):
604-
if not is_SymbolicEquation(soln.sage()):
602+
if ics is not None:
603+
if not (isinstance(soln.sage(), Expression) and soln.sage().is_relational()):
605604
if not show_method:
606605
maxima_method=P("method")
607606
raise NotImplementedError("Unable to use initial condition for this equation (%s)."%(str(maxima_method).strip()))
@@ -649,8 +648,8 @@ def sanitize_var(exprs):
649648
if str(soln).strip() == 'false':
650649
raise NotImplementedError("Maxima was unable to solve this BVP. Remove the initial condition to get the general solution.")
651650

652-
soln=soln.sage()
653-
if is_SymbolicEquation(soln) and soln.lhs() == dvar:
651+
soln = soln.sage()
652+
if isinstance(soln, Expression) and soln.is_relational() and soln.lhs() == dvar:
654653
# Remark: Here we do not check that the right hand side does not depend on dvar.
655654
# This probably will not happen for solutions obtained via ode2, anyway.
656655
soln = soln.rhs()
@@ -796,9 +795,9 @@ def desolve_laplace(de, dvar, ics=None, ivar=None):
796795
#return maxima(cmd).rhs()._maxima_init_()
797796

798797
## verbatim copy from desolve - begin
799-
if is_SymbolicEquation(de):
798+
if isinstance(de, Expression) and de.is_relational():
800799
de = de.lhs() - de.rhs()
801-
if is_SymbolicVariable(dvar):
800+
if isinstance(dvar, Expression) and dvar.is_symbol():
802801
raise ValueError("You have to declare dependent variable as a function evaluated at the independent variable, eg. y=function('y')(x)")
803802
# for backwards compatibility
804803
if isinstance(dvar, list):
@@ -959,7 +958,7 @@ def desolve_system(des, vars, ics=None, ivar=None, algorithm="maxima"):
959958
return desolve_laplace(des[0], vars[0], ics=ics, ivar=ivar)
960959
ivars = set([])
961960
for i, de in enumerate(des):
962-
if not is_SymbolicEquation(de):
961+
if not (isinstance(de, Expression) and de.is_relational()):
963962
des[i] = de == 0
964963
ivars = ivars.union(set(de.variables()))
965964
if ivar is None:
@@ -1206,6 +1205,8 @@ def eulers_method_2x2_plot(f,g, t0, x0, y0, h, t1):
12061205
sage: f = lambda z : z[2]; g = lambda z : -sin(z[1])
12071206
sage: P = eulers_method_2x2_plot(f,g, 0.0, 0.75, 0.0, 0.1, 1.0)
12081207
"""
1208+
from sage.plot.line import line
1209+
12091210
n = int((1.0)*(t1-t0)/h)
12101211
t00 = t0
12111212
x00 = x0
@@ -1409,11 +1410,11 @@ def desolve_rk4_inner(de, dvar):
14091410
YMIN = t
14101411
return plot_slope_field(de, (ivar,XMIN,XMAX), (dvar,YMIN,YMAX))+R
14111412

1412-
if not is_SymbolicVariable(dvar):
1413+
if not (isinstance(dvar, Expression) and dvar.is_symbol()):
14131414
from sage.symbolic.ring import SR
14141415
from sage.calculus.all import diff
14151416
from sage.symbolic.relation import solve
1416-
if is_SymbolicEquation(de):
1417+
if isinstance(de, Expression) and de.is_relational():
14171418
de = de.lhs() - de.rhs()
14181419
# consider to add warning if the solution is not unique
14191420
de=solve(de,diff(dvar,ivar),solution_dict=True)
@@ -1706,7 +1707,7 @@ def Dfun(y, t):
17061707
mxhnil=mxhnil, mxordn=mxordn, mxords=mxords, printmessg=printmessg)
17071708
return sol
17081709

1709-
if is_SymbolicVariable(dvars):
1710+
if isinstance(dvars, Expression) and dvars.is_symbol():
17101711
dvars = [dvars]
17111712

17121713
if not isinstance(des, (list, tuple)):
@@ -1719,6 +1720,7 @@ def Dfun(y, t):
17191720
if len(ivars)==1:
17201721
return desolve_odeint_inner(next(iter(ivars)))
17211722
elif not ivars:
1723+
from sage.symbolic.ring import SR
17221724
with SR.temp_var() as ivar:
17231725
return desolve_odeint_inner(ivar)
17241726
else:

src/sage/calculus/functional.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
-a^3 + 3*a^2*x - 3*a*x^2 + x^3
2727
"""
2828

29-
from .calculus import SR
30-
from sage.symbolic.expression import Expression
29+
from sage.structure.element import Expression
30+
3131

3232
def simplify(f):
3333
r"""
@@ -147,6 +147,7 @@ def derivative(f, *args, **kwds):
147147
except AttributeError:
148148
pass
149149
if not isinstance(f, Expression):
150+
from sage.symbolic.ring import SR
150151
f = SR(f)
151152
return f.derivative(*args, **kwds)
152153

@@ -296,6 +297,7 @@ def integral(f, *args, **kwds):
296297
pass
297298

298299
if not isinstance(f, Expression):
300+
from sage.symbolic.ring import SR
299301
f = SR(f)
300302
return f.integral(*args, **kwds)
301303

@@ -349,6 +351,7 @@ def limit(f, dir=None, taylor=False, **argv):
349351
-limit((erf(x) - 1)*e^(x^2), x, +Infinity)
350352
"""
351353
if not isinstance(f, Expression):
354+
from sage.symbolic.ring import SR
352355
f = SR(f)
353356
return f.limit(dir=dir, taylor=taylor, **argv)
354357

@@ -392,6 +395,7 @@ def taylor(f, *args):
392395
(x - 1)*(y + 1)^3 - 3*(x - 1)*(y + 1)^2 + (y + 1)^3 + 3*(x - 1)*(y + 1) - 3*(y + 1)^2 - x + 3*y + 3
393396
"""
394397
if not isinstance(f, Expression):
398+
from sage.symbolic.ring import SR
395399
f = SR(f)
396400
return f.taylor(*args)
397401

src/sage/calculus/functions.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
Calculus functions
33
"""
44
from sage.matrix.constructor import matrix
5-
from sage.structure.element import is_Matrix
6-
from sage.structure.element import is_Vector
7-
from sage.symbolic.ring import is_SymbolicVariable
5+
from sage.structure.element import is_Matrix, is_Vector, Expression
6+
87
from .functional import diff
98

109

@@ -82,7 +81,7 @@ def wronskian(*args):
8281
# a 1x1 Wronskian is just its argument
8382
return args[0]
8483
else:
85-
if is_SymbolicVariable(args[-1]):
84+
if isinstance(args[-1], Expression) and args[-1].is_symbol():
8685
# if last argument is a variable, peel it off and
8786
# differentiate the other args
8887
v = args[-1]

src/sage/calculus/var.pyx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ def clear_vars():
384384
sage: k
385385
15
386386
"""
387+
from sage.structure.element import Expression
388+
387389
G = globals()
388-
from sage.symbolic.ring import is_SymbolicVariable
389390
for i in list(range(65, 65 + 26)) + list(range(97, 97 + 26)):
390-
if chr(i) in G and is_SymbolicVariable(G[chr(i)]):
391+
chr_i = chr(i)
392+
if chr_i in G and isinstance(G[chr_i], Expression) and G[chr_i].is_symbol():
391393
# We check to see if there is a corresponding pyobject
392394
# associated with the expression. This will work for
393395
# constants which we want to keep, but will fail for

src/sage/interfaces/maxima_lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
# https://www.gnu.org/licenses/
115115
# ****************************************************************************
116116

117+
from sage.structure.element import Expression
117118
from sage.symbolic.ring import SR
118119

119120
from sage.libs.ecl import EclObject, ecl_eval
@@ -1615,9 +1616,8 @@ def sr_to_max(expr):
16151616
# For that, we should change the API of the functions there
16161617
# (we need to have access to op, not only to expr.operands()
16171618
if isinstance(op, FDerivativeOperator):
1618-
from sage.symbolic.ring import is_SymbolicVariable
16191619
args = expr.operands()
1620-
if (not all(is_SymbolicVariable(v) for v in args) or
1620+
if (not all(isinstance(v, Expression) and v.is_symbol() for v in args) or
16211621
len(args) != len(set(args))):
16221622
# An evaluated derivative of the form f'(1) is not a
16231623
# symbolic variable, yet we would like to treat it

src/sage/symbolic/assumptions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
from sage.rings.rational_field import QQ
7676
from sage.rings.real_mpfr import RR
7777
from sage.rings.cc import CC
78-
from sage.symbolic.ring import is_SymbolicVariable
78+
from sage.structure.element import Expression
7979
from sage.structure.unique_representation import UniqueRepresentation
8080

8181
# #30074: We use the keys of a dict to store the assumptions.
@@ -427,7 +427,8 @@ def preprocess_assumptions(args):
427427
if isinstance(x, str):
428428
del args[i]
429429
last = x
430-
elif ((not hasattr(x, 'assume') or is_SymbolicVariable(x))
430+
elif ((not hasattr(x, 'assume')
431+
or (isinstance(x, Expression) and x.is_symbol()))
431432
and last is not None):
432433
args[i] = GenericDeclaration(x, last)
433434
else:

src/sage/symbolic/callable.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,11 @@ def create_key(self, args, check=True):
490490
(x, y)
491491
"""
492492
if check:
493-
from sage.symbolic.ring import is_SymbolicVariable
493+
from sage.structure.element import Expression
494494
if len(args) == 1 and isinstance(args[0], (list, tuple)):
495495
args, = args
496496
for arg in args:
497-
if not is_SymbolicVariable(arg):
497+
if not (isinstance(arg, Expression) and arg.is_symbol()):
498498
raise TypeError("must construct a function with a tuple (or list) of variables")
499499
args = tuple(args)
500500
return args

src/sage/symbolic/expression.pyx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ def _subs_make_dict(s):
613613
"""
614614
if isinstance(s, dict):
615615
return s
616-
elif is_SymbolicEquation(s):
616+
elif isinstance(s, Expression) and s.is_relational():
617617
if s.operator() is not operator.eq:
618618
msg = "can only substitute equality, not inequalities; got {}"
619619
raise TypeError(msg.format(s))
@@ -692,7 +692,7 @@ def _subs_fun_make_dict(s):
692692
"""
693693
if isinstance(s, dict):
694694
return dict((k, v) if not isinstance(k, Expression) else (k.operator(), v.function(*k.operands())) for k, v in s.items())
695-
elif is_SymbolicEquation(s):
695+
elif isinstance(s, Expression) and s.is_relational():
696696
if s.operator() is not operator.eq:
697697
msg = "can only substitute equality, not inequalities; got {}"
698698
raise TypeError(msg.format(s))
@@ -6810,12 +6810,11 @@ cdef class Expression(Expression_abc):
68106810
# we override type checking in CallableSymbolicExpressionRing,
68116811
# since it checks for old SymbolicVariable's
68126812
# and do the check here instead
6813-
from sage.symbolic.callable import CallableSymbolicExpressionRing
6814-
from sage.symbolic.ring import is_SymbolicVariable
68156813
for i in args:
6816-
if not is_SymbolicVariable(i):
6814+
if not (isinstance(i, Expression) and i.is_symbol()):
68176815
break
68186816
else:
6817+
from sage.symbolic.callable import CallableSymbolicExpressionRing
68196818
R = CallableSymbolicExpressionRing(args, check=False)
68206819
return R(self)
68216820
raise TypeError(f"must construct a function with symbolic variables as arguments, got {args}.")
@@ -12881,7 +12880,6 @@ cdef class Expression(Expression_abc):
1288112880
sage: plot(f,0,1)
1288212881
Graphics object consisting of 1 graphics primitive
1288312882
"""
12884-
from sage.symbolic.ring import is_SymbolicVariable
1288512883
from sage.plot.plot import plot
1288612884

1288712885
# see if the user passed a variable in.
@@ -12890,7 +12888,7 @@ cdef class Expression(Expression_abc):
1289012888
else:
1289112889
param = None
1289212890
for i, arg in enumerate(args):
12893-
if is_SymbolicVariable(arg):
12891+
if isinstance(arg, Expression) and arg.is_symbol():
1289412892
param = arg
1289512893
args = args[:i] + args[i+1:]
1289612894
break

src/sage/symbolic/expression_conversions.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,10 @@ def derivative(self, ex, operator):
579579
"""
580580
# This code should probably be moved into the interface
581581
# object in a nice way.
582-
from sage.symbolic.ring import is_SymbolicVariable
583582
if self.name_init != "_maxima_init_":
584583
raise NotImplementedError
585584
args = ex.operands()
586-
if (not all(is_SymbolicVariable(v) for v in args) or
585+
if (not all(isinstance(v, Expression) and v.is_symbol() for v in args) or
587586
len(args) != len(set(args))):
588587
# An evaluated derivative of the form f'(1) is not a
589588
# symbolic variable, yet we would like to treat it like
@@ -1103,12 +1102,11 @@ def derivative(self, ex, operator):
11031102
,1,1,2
11041103
11051104
"""
1106-
from sage.symbolic.ring import is_SymbolicVariable
11071105
args = ex.operands() # the arguments the derivative is evaluated at
11081106
params = operator.parameter_set()
11091107
params_set = set(params)
11101108
mult = ",".join(str(params.count(i)) for i in params_set)
1111-
if (not all(is_SymbolicVariable(v) for v in args) or
1109+
if (not all(isinstance(v, Expression) and v.is_symbol() for v in args) or
11121110
len(args) != len(set(args))):
11131111
# An evaluated derivative of the form f'(1) is not a
11141112
# symbolic variable, yet we would like to treat it like

0 commit comments

Comments
 (0)