Skip to content

Commit a9aed04

Browse files
committed
Address review, fix tests
1 parent 7a1e709 commit a9aed04

File tree

2 files changed

+74
-74
lines changed

2 files changed

+74
-74
lines changed

src/sage/modules/free_module_element.pyx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,7 +4271,7 @@ cdef class FreeModuleElement(Vector): # abstract base class
42714271
return vector(ring, coeffs)
42724272
return vector(coeffs)
42734273
4274-
def compositional_inverse(self, allow_inverse_multivalued=True, **kwargs):
4274+
def compositional_inverse(self, allow_multivalued_inverse=True, **kwargs):
42754275
"""
42764276
Find the compositional inverse of this symbolic function.
42774277

@@ -4306,8 +4306,8 @@ cdef class FreeModuleElement(Vector): # abstract base class
43064306
l = solve([a == b for a, b in zip(self.change_ring(SR), tmp_vars)], input_vars, solution_dict=True, **kwargs)
43074307
if not l:
43084308
raise ValueError("cannot find an inverse")
4309-
if len(l) > 1 and not allow_inverse_multivalued:
4310-
raise ValueError("inverse is multivalued, pass allow_inverse_multivalued=True to bypass")
4309+
if len(l) > 1 and not allow_multivalued_inverse:
4310+
raise ValueError("inverse is multivalued, pass allow_multivalued_inverse=True to bypass")
43114311
d = l[0]
43124312
subs_dict = dict(zip(tmp_vars, input_vars))
43134313
for x in input_vars:

src/sage/symbolic/expression.pyx

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13441,77 +13441,6 @@ cdef class Expression(Expression_abc):
1344113441
raise TypeError("this expression must be a relation")
1344213442
return self / x
1344313443

13444-
def implicit_derivative(self, Y, X, n=1):
13445-
"""
13446-
Return the `n`-th derivative of `Y` with respect to `X` given
13447-
implicitly by this expression.
13448-
13449-
INPUT:
13450-
13451-
- ``Y`` -- the dependent variable of the implicit expression
13452-
13453-
- ``X`` -- the independent variable with respect to which the
13454-
derivative is taken
13455-
13456-
- ``n`` -- (default: 1) the order of the derivative
13457-
13458-
EXAMPLES::
13459-
13460-
sage: var('x, y')
13461-
(x, y)
13462-
sage: f = cos(x)*sin(y)
13463-
sage: f.implicit_derivative(y, x)
13464-
sin(x)*sin(y)/(cos(x)*cos(y))
13465-
sage: g = x*y^2
13466-
sage: g.implicit_derivative(y, x, 3)
13467-
-1/4*(y + 2*y/x)/x^2 + 1/4*(2*y^2/x - y^2/x^2)/(x*y) - 3/4*y/x^3
13468-
13469-
It is an error to not include an independent variable term
13470-
in the expression::
13471-
13472-
sage: (cos(x)*sin(x)).implicit_derivative(y, x)
13473-
Traceback (most recent call last):
13474-
...
13475-
ValueError: Expression cos(x)*sin(x) contains no y terms
13476-
13477-
13478-
TESTS:
13479-
13480-
Check that the symbols registry is not polluted::
13481-
13482-
sage: var('x,y')
13483-
(x, y)
13484-
sage: psr = copy(SR.symbols)
13485-
sage: (x^6*y^5).implicit_derivative(y, x, 3)
13486-
-792/125*y/x^3 + 12/25*(15*x^4*y^5 + 28*x^3*y^5)/(x^6*y^4) - 36/125*(20*x^5*y^4 + 43*x^4*y^4)/(x^7*y^3)
13487-
sage: psr == SR.symbols
13488-
True
13489-
"""
13490-
from sage.symbolic.ring import SR
13491-
from sage.symbolic.function import SymbolicFunction
13492-
13493-
if not self.has(Y):
13494-
raise ValueError("Expression {} contains no {} terms".format(self, Y))
13495-
with SR.temp_var() as x:
13496-
with SR.temp_var() as yy:
13497-
y = SymbolicFunction('y', 1)(x)
13498-
f = SymbolicFunction('f', 2)(x, yy)
13499-
Fx = f.diff(x)
13500-
Fy = f.diff(yy)
13501-
G = -(Fx/Fy)
13502-
G = G.subs({yy: y})
13503-
di = {y.diff(x): -self.diff(X)/self.diff(Y)}
13504-
R = G
13505-
S = G.diff(x, n - 1)
13506-
for i in range(n + 1):
13507-
di[y.diff(x, i + 1).subs({x: x})] = R
13508-
S = S.subs(di)
13509-
R = G.diff(x, i)
13510-
for j in range(n + 1 - i):
13511-
di[f.diff(x, i, yy, j).subs({x: x, yy: y})] = self.diff(X, i, Y, j)
13512-
S = S.subs(di)
13513-
return S
13514-
1351513444
def compositional_inverse(self, allow_inverse_multivalued=True, **kwargs):
1351613445
"""
1351713446
Find the compositional inverse of this symbolic function.
@@ -13589,6 +13518,77 @@ cdef class Expression(Expression_abc):
1358913518
from sage.modules.free_module_element import vector
1359013519
return vector([self]).compositional_inverse(allow_inverse_multivalued=allow_inverse_multivalued, **kwargs)[0]
1359113520

13521+
def implicit_derivative(self, Y, X, n=1):
13522+
"""
13523+
Return the `n`-th derivative of `Y` with respect to `X` given
13524+
implicitly by this expression.
13525+
13526+
INPUT:
13527+
13528+
- ``Y`` -- the dependent variable of the implicit expression
13529+
13530+
- ``X`` -- the independent variable with respect to which the
13531+
derivative is taken
13532+
13533+
- ``n`` -- (default: 1) the order of the derivative
13534+
13535+
EXAMPLES::
13536+
13537+
sage: var('x, y')
13538+
(x, y)
13539+
sage: f = cos(x)*sin(y)
13540+
sage: f.implicit_derivative(y, x)
13541+
sin(x)*sin(y)/(cos(x)*cos(y))
13542+
sage: g = x*y^2
13543+
sage: g.implicit_derivative(y, x, 3)
13544+
-1/4*(y + 2*y/x)/x^2 + 1/4*(2*y^2/x - y^2/x^2)/(x*y) - 3/4*y/x^3
13545+
13546+
It is an error to not include an independent variable term
13547+
in the expression::
13548+
13549+
sage: (cos(x)*sin(x)).implicit_derivative(y, x)
13550+
Traceback (most recent call last):
13551+
...
13552+
ValueError: Expression cos(x)*sin(x) contains no y terms
13553+
13554+
13555+
TESTS:
13556+
13557+
Check that the symbols registry is not polluted::
13558+
13559+
sage: var('x,y')
13560+
(x, y)
13561+
sage: psr = copy(SR.symbols)
13562+
sage: (x^6*y^5).implicit_derivative(y, x, 3)
13563+
-792/125*y/x^3 + 12/25*(15*x^4*y^5 + 28*x^3*y^5)/(x^6*y^4) - 36/125*(20*x^5*y^4 + 43*x^4*y^4)/(x^7*y^3)
13564+
sage: psr == SR.symbols
13565+
True
13566+
"""
13567+
from sage.symbolic.ring import SR
13568+
from sage.symbolic.function import SymbolicFunction
13569+
13570+
if not self.has(Y):
13571+
raise ValueError("Expression {} contains no {} terms".format(self, Y))
13572+
with SR.temp_var() as x:
13573+
with SR.temp_var() as yy:
13574+
y = SymbolicFunction('y', 1)(x)
13575+
f = SymbolicFunction('f', 2)(x, yy)
13576+
Fx = f.diff(x)
13577+
Fy = f.diff(yy)
13578+
G = -(Fx/Fy)
13579+
G = G.subs({yy: y})
13580+
di = {y.diff(x): -self.diff(X)/self.diff(Y)}
13581+
R = G
13582+
S = G.diff(x, n - 1)
13583+
for i in range(n + 1):
13584+
di[y.diff(x, i + 1).subs({x: x})] = R
13585+
S = S.subs(di)
13586+
R = G.diff(x, i)
13587+
for j in range(n + 1 - i):
13588+
di[f.diff(x, i, yy, j).subs({x: x, yy: y})] = self.diff(X, i, Y, j)
13589+
S = S.subs(di)
13590+
return S
13591+
1359213592

1359313593
cpdef _repr_Expression(x):
1359413594
r"""

0 commit comments

Comments
 (0)