Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit 91a08d2

Browse files
committed
changes affecting Sage behaviour or interface
1 parent b996f4f commit 91a08d2

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/sage/symbolic/expression.pyx

+35-15
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ cdef class Expression(CommutativeRingElement):
11911191
except TypeError as err:
11921192
# try the evaluation again with the complex field
11931193
# corresponding to the parent R
1194-
if R is float:
1194+
if R in (float, complex):
11951195
R_complex = complex
11961196
else:
11971197
try:
@@ -1387,10 +1387,19 @@ cdef class Expression(CommutativeRingElement):
13871387
...
13881388
TypeError: unable to simplify to float approximation
13891389
"""
1390+
from sage.functions.other import real, imag
13901391
try:
1391-
return float(self._eval_self(float))
1392+
ret = float(self._eval_self(float))
13921393
except TypeError:
1393-
raise TypeError("unable to simplify to float approximation")
1394+
try:
1395+
c = (self._eval_self(complex))
1396+
if imag(c) == 0:
1397+
ret = real(c)
1398+
else:
1399+
raise
1400+
except TypeError:
1401+
raise TypeError("unable to simplify to float approximation")
1402+
return ret
13941403

13951404
def __complex__(self):
13961405
"""
@@ -3552,8 +3561,6 @@ cdef class Expression(CommutativeRingElement):
35523561
INPUT:
35533562
35543563
- ``exp`` -- something that coerces to a symbolic expression.
3555-
- ``ignored`` -- the second argument that should accept a modulus
3556-
is actually ignored.
35573564
35583565
OUTPUT:
35593566
@@ -8350,11 +8357,14 @@ cdef class Expression(CommutativeRingElement):
83508357
else:
83518358
return v[0]
83528359

8353-
def combine(self):
8360+
def combine(self, bint deep=False):
83548361
r"""
83558362
Return a simplified version of this symbolic expression
8356-
by combining all terms with the same denominator into a single
8357-
term.
8363+
by combining all toplevel terms with the same denominator into
8364+
a single term.
8365+
8366+
Please use the keyword ``deep=True`` to apply the process
8367+
recursively.
83588368
83598369
EXAMPLES::
83608370
@@ -8364,8 +8374,19 @@ cdef class Expression(CommutativeRingElement):
83648374
(x - 1)*x/(x^2 - 7) + y^2/(x^2 - 7) + b/a + c/a + 1/(x + 1)
83658375
sage: f.combine()
83668376
((x - 1)*x + y^2)/(x^2 - 7) + (b + c)/a + 1/(x + 1)
8377+
sage: (1/x + 1/x^2 + (x+1)/x).combine()
8378+
(x + 2)/x + 1/x^2
8379+
sage: ex = 1/x + ((x + 1)/x - 1/x)/x^2 + (x+1)/x; ex
8380+
(x + 1)/x + 1/x + ((x + 1)/x - 1/x)/x^2
8381+
sage: ex.combine()
8382+
(x + 2)/x + ((x + 1)/x - 1/x)/x^2
8383+
sage: ex.combine(deep=True)
8384+
(x + 2)/x + 1/x^2
8385+
sage: (1+sin((x + 1)/x - 1/x)).combine(deep=True)
8386+
sin(1) + 1
83678387
"""
8368-
return new_Expression_from_GEx(self._parent, self._gobj.combine_fractions())
8388+
return new_Expression_from_GEx(self._parent,
8389+
self._gobj.combine_fractions(deep))
83698390

83708391
def normalize(self):
83718392
"""
@@ -8408,7 +8429,7 @@ cdef class Expression(CommutativeRingElement):
84088429
ALGORITHM: Uses GiNaC.
84098430
84108431
"""
8411-
return new_Expression_from_GEx(self._parent, self._gobj.normal())
8432+
return new_Expression_from_GEx(self._parent, self._gobj.normal(0, False, True))
84128433

84138434
def numerator(self, bint normalize = True):
84148435
"""
@@ -9351,9 +9372,8 @@ cdef class Expression(CommutativeRingElement):
93519372
ALIAS: :meth:`rational_simplify` and :meth:`simplify_rational`
93529373
are the same
93539374
9354-
DETAILS: We call Maxima functions ratsimp, fullratsimp and
9355-
xthru. If each part of the expression has to be simplified
9356-
separately, we use Maxima function map.
9375+
DETAILS: We call the Maxima function ``fullratsimp`` and
9376+
and Pynac's ``normal``, depending on the ``algorithm`` keyword.
93579377
93589378
EXAMPLES::
93599379
@@ -9407,9 +9427,9 @@ cdef class Expression(CommutativeRingElement):
94079427
if algorithm == 'full':
94089428
maxima_method = 'fullratsimp'
94099429
elif algorithm == 'simple':
9410-
maxima_method = 'ratsimp'
9430+
return new_Expression_from_GEx(self._parent, self._gobj.normal(0, False, False))
94119431
elif algorithm == 'noexpand':
9412-
maxima_method = 'xthru'
9432+
return new_Expression_from_GEx(self._parent, self._gobj.normal(0, True, True))
94139433
else:
94149434
raise NotImplementedError("unknown algorithm, see the help for available algorithms")
94159435
P = self_m.parent()

src/sage/symbolic/ginac.pxd

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ cdef extern from "sage/symbolic/ginac_wrap.h":
106106
GEx lcoeff(GEx expr) except +
107107
GEx tcoeff(GEx expr) except +
108108
void coefficients(GEx s, vector[pair[GEx,GEx]]) except +
109-
GEx combine_fractions() except +
110-
GEx normal() except +
109+
GEx combine_fractions(bint deep) except +
110+
GEx normal(int level, bint noexpand_combined, bint noexpand_frac) except +
111111
GEx numer() except +
112112
GEx denom() except +
113113
GEx numer_denom() except +

0 commit comments

Comments
 (0)