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

Commit

Permalink
convert real sets and piecewise functions to sympy
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Sep 11, 2019
1 parent bdf4b23 commit 052bed2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/sage/functions/piecewise.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ def is_piecewise(ex):
return result
return is_piecewise(ex)


@staticmethod
def simplify(ex):
"""
Expand Down Expand Up @@ -1353,4 +1352,30 @@ def fourier_series_partial_sum(self, parameters, variable, N,
for n in srange(1, N+1)])
return SR(result).expand()

def _sympy_(self, parameters, variable):
"""
Convert this piecewise expression to its SymPy equivalent.
EXAMPLES::
sage: ex = piecewise([((0, 1), pi), ([1, 2], x)])
sage: f = ex._sympy_(); f
Piecewise((pi, (x > 0) & (x < 1)), (x, (x >= 1) & (x <= 2)))
sage: f.diff()
Piecewise((0, (x > 0) & (x < 1)), (1, (x >= 1) & (x <= 2)))
sage: ex = piecewise([((-100, -2), 1/x), ((1, +oo), cos(x))])
sage: g = ex._sympy_(); g
Piecewise((1/x, (x > -100) & (x < -2)), (cos(x), x > 1))
sage: g.diff()
Piecewise((-1/x**2, (x > -100) & (x < -2)), (-sin(x), x > 1))
"""
from sage.symbolic.ring import SR
from sympy import Piecewise as pw
args = [(func._sympy_(),
domain._sympy_condition_(variable))
for domain, func in parameters]
return pw(*args)


piecewise = PiecewiseFunction()
71 changes: 71 additions & 0 deletions src/sage/sets/real_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,39 @@ def _repr_(self):
s += ']' if self._upper_closed else ')'
return s

def _sympy_condition_(self, variable):
"""
Convert to a sympy conditional expression.
INPUT:
- ``variable`` -- a symbolic variable
EXAMPLES::
sage: RealSet(0, 4)._sympy_condition_(x)
(0 < x) & (x < 4)
"""
x = variable
if self.is_point():
return (x == self.lower())._sympy_()
true = (x == 0)._sympy_() | True # trick to get sympy's True
if self.lower() is not minus_infinity:
if self._lower_closed:
lower_condition = (self.lower() <= x)._sympy_()
else:
lower_condition = (self.lower() < x)._sympy_()
else:
lower_condition = true
if self.upper() is not infinity:
if self._upper_closed:
upper_condition = (x <= self.upper())._sympy_()
else:
upper_condition = (x < self.upper())._sympy_()
else:
upper_condition = true
return lower_condition & upper_condition

def closure(self):
"""
Return the closure
Expand Down Expand Up @@ -1060,6 +1093,44 @@ def _repr_(self):
else:
# Switch to u'\u222A' (cup sign) with Python 3
return ' + '.join(map(repr, self._intervals))
# return u' ∪ '.join(map(repr, self._intervals)) # py3 only


def _sympy_condition_(self, variable):
"""
Convert to a sympy conditional expression.
INPUT:
- ``variable`` -- a symbolic variable
EXAMPLES::
sage: RealSet(0, 1)._sympy_condition_(x)
(0 < x) & (x < 1)
sage: RealSet((0,1), [2,3])._sympy_condition_(x)
((2 <= x) & (x <= 3)) | ((0 < x) & (x < 1))
sage: RealSet.unbounded_below_open(0)._sympy_condition_(x)
x < 0
sage: RealSet.unbounded_above_closed(2)._sympy_condition_(x)
2 <= x
TESTS::
sage: RealSet(6,6)._sympy_condition_(x)
False
sage: RealSet([6,6])._sympy_condition_(x)
Eq(x, 6)
"""
x = variable
false = (x == 0)._sympy_() & False # trick to get sympy's False
if self.n_components() == 0:
return false
else:
cond = false
for it in self._intervals:
cond = cond | it._sympy_condition_(x)
return cond

@staticmethod
def _prep(lower, upper=None):
Expand Down

0 comments on commit 052bed2

Please sign in to comment.