Skip to content

Commit fb0ae66

Browse files
author
Release Manager
committed
gh-35789: more arguments for simplify <!-- 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 We allow a few more arguments to the simplify method. fixes #32472 <!-- 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. --> ### 📝 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. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] 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: #35789 Reported by: Frédéric Chapoton Reviewer(s): Travis Scrimshaw
2 parents f3d389d + 561d611 commit fb0ae66

File tree

2 files changed

+57
-27
lines changed

2 files changed

+57
-27
lines changed

src/sage/calculus/functional.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,40 @@
2525
sage: expand((x - a)^3)
2626
-a^3 + 3*a^2*x - 3*a*x^2 + x^3
2727
"""
28-
2928
from sage.structure.element import Expression
3029

3130

32-
def simplify(f):
31+
def simplify(f, algorithm="maxima", **kwds):
3332
r"""
3433
Simplify the expression `f`.
3534
36-
EXAMPLES: We simplify the expression `i + x - x`.
35+
See the documentation of the
36+
:meth:`~sage.symbolic.expression.Expression.simplify` method of symbolic
37+
expressions for details on options.
3738
38-
::
39+
EXAMPLES:
40+
41+
We simplify the expression `i + x - x`::
3942
4043
sage: f = I + x - x; simplify(f)
4144
I
4245
4346
In fact, printing `f` yields the same thing - i.e., the
4447
simplified form.
48+
49+
Some simplifications are algorithm-specific::
50+
51+
sage: x, t = var("x, t")
52+
sage: ex = 1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/2/(I*x + I*sqrt(x^2 - 1))
53+
sage: simplify(ex)
54+
1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/(2*I*x + 2*I*sqrt(x^2 - 1))
55+
sage: simplify(ex, algorithm="giac")
56+
I*sqrt(x^2 - 1)
4557
"""
58+
try:
59+
return f.simplify(algorithm=algorithm, **kwds)
60+
except (TypeError, AttributeError):
61+
pass
4662
try:
4763
return f.simplify()
4864
except AttributeError:

src/sage/symbolic/expression.pyx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10617,22 +10617,26 @@ cdef class Expression(Expression_abc):
1061710617
else:
1061810618
return self
1061910619

10620-
def simplify(self, algorithm='maxima'):
10620+
def simplify(self, algorithm='maxima', **kwds):
1062110621
"""
1062210622
Return a simplified version of this symbolic expression.
1062310623
10624-
.. NOTE::
10624+
INPUT:
1062510625
10626-
When using ``algorithm='maxima'``, this just sends the expression to Maxima
10627-
and converts it back to Sage. When ``algorithm='sympy'``,
10628-
sympy's simplify method is used.
10626+
- ``algorithm`` -- one of :
1062910627
10630-
INPUT:
10628+
- ``maxima`` : (default) sends the expression to
10629+
``maxima`` and converts it back to Sage
1063110630
10632-
- ``self`` -- an expression with held operations
10633-
- ``algorithm`` - (default: ``'maxima'``) one of
10634-
- ``'maxima'`` - use Maxima (the default)
10635-
- ``'sympy'`` - use SymPy
10631+
- ``sympy`` : converts the expression to ``sympy``,
10632+
simplifies it (passing any optional keyword(s)), and
10633+
converts the result to Sage
10634+
10635+
- ``giac`` : converts the expression to ``giac``,
10636+
simplifies it, and converts the result to Sage
10637+
10638+
- ``fricas`` : converts the expression to ``fricas``,
10639+
simplifies it, and converts the result to Sage
1063610640
1063710641
.. SEEALSO::
1063810642
@@ -10649,11 +10653,21 @@ cdef class Expression(Expression_abc):
1064910653
sage: f.simplify()
1065010654
x^(-a + 1)*sin(2)
1065110655
10652-
::
10653-
10654-
sage: expr = (-1/5*(2*sqrt(6)*(sqrt(5) - 5) + 11*sqrt(5) - 11)/(2*sqrt(6)*sqrt(5) - 11))
10655-
sage: expr.simplify(algorithm='sympy')
10656-
1/5*sqrt(5) - 1/5
10656+
Some simplifications are quite algorithm-specific::
10657+
10658+
sage: x, t = var("x, t")
10659+
sage: ex = cos(t).exponentialize()
10660+
sage: ex = ex.subs((sin(t).exponentialize()==x).solve(t)[0])
10661+
sage: ex
10662+
1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/2/(I*x + I*sqrt(x^2 - 1))
10663+
sage: ex.simplify()
10664+
1/2*I*x + 1/2*I*sqrt(x^2 - 1) + 1/(2*I*x + 2*I*sqrt(x^2 - 1))
10665+
sage: ex.simplify(algorithm="sympy")
10666+
I*(x^2 + sqrt(x^2 - 1)*x - 1)/(x + sqrt(x^2 - 1))
10667+
sage: ex.simplify(algorithm="giac")
10668+
I*sqrt(x^2 - 1)
10669+
sage: ex.simplify(algorithm="fricas") # optional - fricas
10670+
(I*x^2 + I*sqrt(x^2 - 1)*x - I)/(x + sqrt(x^2 - 1))
1065710671
1065810672
TESTS:
1065910673
@@ -10669,16 +10683,16 @@ cdef class Expression(Expression_abc):
1066910683
sage: expr = (-1/5*(2*sqrt(6)*(sqrt(5) - 5) + 11*sqrt(5) - 11)/(2*sqrt(6)*sqrt(5) - 11))
1067010684
sage: expr.simplify(algorithm='sympy')
1067110685
1/5*sqrt(5) - 1/5
10672-
10673-
1067410686
"""
10675-
if algorithm == 'maxima':
10687+
if algorithm == "maxima":
1067610688
return self._parent(self._maxima_())
10677-
elif algorithm == 'sympy':
10678-
return self._sympy_().simplify()._sage_()
10679-
else:
10680-
raise NotImplementedError(
10681-
"unknown algorithm: '{}'".format(algorithm))
10689+
if algorithm == "sympy":
10690+
return self._sympy_().simplify(**kwds)._sage_()
10691+
if algorithm == "giac":
10692+
return self._giac_().simplify()._sage_()
10693+
if algorithm == "fricas":
10694+
return self._fricas_().simplify()._sage_()
10695+
raise ValueError(f"algorithm {algorithm} unknown to simplify")
1068210696

1068310697
def simplify_full(self):
1068410698
"""

0 commit comments

Comments
 (0)