Skip to content

Commit b8dd7ad

Browse files
authored
Merge pull request sympy#14130 from ArighnaIITG/14000_issue
Added evaluate flags in miscellaneous.py
2 parents b5ff416 + fbe8fcf commit b8dd7ad

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

sympy/core/operations.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def __new__(cls, *args, **options):
3131
args = list(map(_sympify, args))
3232
args = [a for a in args if a is not cls.identity]
3333

34-
if not options.pop('evaluate', global_evaluate[0]):
34+
evaluate = options.get('evaluate')
35+
if evaluate is None:
36+
evaluate = global_evaluate[0]
37+
if not evaluate:
3538
return cls._from_args(args)
3639

3740
if len(args) == 0:

sympy/functions/elementary/miscellaneous.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ def __new__(cls):
6161
###############################################################################
6262

6363

64-
def sqrt(arg):
64+
def sqrt(arg, evaluate=None):
6565
"""The square root function
6666
6767
sqrt(x) -> Returns the principal square root of x.
6868
69+
The parameter evaluate determines if the expression should be evaluated.
70+
If None, its value is taken from global_evaluate
71+
6972
Examples
7073
========
7174
@@ -126,13 +129,16 @@ def sqrt(arg):
126129
.. [2] http://en.wikipedia.org/wiki/Principal_value
127130
"""
128131
# arg = sympify(arg) is handled by Pow
129-
return Pow(arg, S.Half)
132+
return Pow(arg, S.Half, evaluate=evaluate)
130133

131134

132-
def cbrt(arg):
135+
def cbrt(arg, evaluate=None):
133136
"""This function computes the principal cube root of `arg`, so
134137
it's just a shortcut for `arg**Rational(1, 3)`.
135138
139+
The parameter evaluate determines if the expression should be evaluated.
140+
If None, its value is taken from global_evaluate.
141+
136142
Examples
137143
========
138144
@@ -176,13 +182,15 @@ def cbrt(arg):
176182
* http://en.wikipedia.org/wiki/Principal_value
177183
178184
"""
179-
return Pow(arg, Rational(1, 3))
185+
return Pow(arg, Rational(1, 3), evaluate=evaluate)
180186

181187

182-
def root(arg, n, k=0):
188+
def root(arg, n, k=0, evaluate=None):
183189
"""root(x, n, k) -> Returns the k-th n-th root of x, defaulting to the
184190
principal root (k=0).
185191
192+
The parameter evaluate determines if the expression should be evaluated.
193+
If None, its value is taken from global_evaluate.
186194
187195
Examples
188196
========
@@ -265,16 +273,19 @@ def root(arg, n, k=0):
265273
"""
266274
n = sympify(n)
267275
if k:
268-
return Pow(arg, S.One/n)*S.NegativeOne**(2*k/n)
269-
return Pow(arg, 1/n)
276+
return Mul(Pow(arg, S.One/n, evaluate=evaluate), S.NegativeOne**(2*k/n), evaluate=evaluate)
277+
return Pow(arg, 1/n, evaluate=evaluate)
270278

271279

272-
def real_root(arg, n=None):
280+
def real_root(arg, n=None, evaluate=None):
273281
"""Return the real nth-root of arg if possible. If n is omitted then
274282
all instances of (-n)**(1/odd) will be changed to -n**(1/odd); this
275283
will only create a real root of a principal root -- the presence of
276284
other factors may cause the result to not be real.
277285
286+
The parameter evaluate determines if the expression should be evaluated.
287+
If None, its value is taken from global_evaluate.
288+
278289
Examples
279290
========
280291
@@ -308,10 +319,10 @@ def real_root(arg, n=None):
308319
from sympy.functions.elementary.piecewise import Piecewise
309320
if n is not None:
310321
return Piecewise(
311-
(root(arg, n), Or(Eq(n, S.One), Eq(n, S.NegativeOne))),
312-
(sign(arg)*root(Abs(arg), n), And(Eq(im(arg), S.Zero),
313-
Eq(Mod(n, 2), S.One))),
314-
(root(arg, n), True))
322+
(root(arg, n, evaluate=evaluate), Or(Eq(n, S.One), Eq(n, S.NegativeOne))),
323+
(Mul(sign(arg), root(Abs(arg), n, evaluate=evaluate), evaluate=evaluate),
324+
And(Eq(im(arg), S.Zero), Eq(Mod(n, 2), S.One))),
325+
(root(arg, n, evaluate=evaluate), True))
315326
rv = sympify(arg)
316327
n1pow = Transform(lambda x: -(-x.base)**x.exp,
317328
lambda x:

sympy/functions/elementary/tests/test_miscellaneous.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from sympy.core.function import Function
55
from sympy.core.numbers import I, oo, Rational
6+
from sympy.core.power import Pow
67
from sympy.core.singleton import S
78
from sympy.core.symbol import Symbol
89
from sympy.functions.elementary.miscellaneous import (sqrt, cbrt, root, Min,
@@ -312,7 +313,7 @@ def test_root():
312313
assert root(x, n) == x**(1/n)
313314
assert root(x, -n) == x**(-1/n)
314315

315-
assert root(x, n, k) == x**(1/n)*(-1)**(2*k/n)
316+
assert root(x, n, k) == (-1)**(2*k/n)*x**(1/n)
316317

317318

318319
def test_real_root():
@@ -427,3 +428,15 @@ def test(e):
427428
test(Max(x, y))
428429
test(Min(x, y, z))
429430
test(Min(Max(w, x), Max(y, z)))
431+
432+
def test_issue_14000():
433+
assert isinstance(sqrt(4, evaluate=False), Pow) == True
434+
assert isinstance(cbrt(3.5, evaluate=False), Pow) == True
435+
assert isinstance(root(16, 4, evaluate=False), Pow) == True
436+
437+
assert sqrt(4, evaluate=False) == Pow(4, S.Half, evaluate=False)
438+
assert cbrt(3.5, evaluate=False) == Pow(3.5, Rational(1, 3), evaluate=False)
439+
assert root(4, 2, evaluate=False) == Pow(4, Rational(1, 2), evaluate=False)
440+
441+
assert root(16, 4, 2, evaluate=False).has(Pow) == True
442+
assert real_root(-8, 3, evaluate=False).has(Pow) == True

0 commit comments

Comments
 (0)