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

Commit a68d57f

Browse files
committed
23923: Interface cases function with SymPy's piecewise
1 parent 8dd5f88 commit a68d57f

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

src/sage/functions/other.py

+20
Original file line numberDiff line numberDiff line change
@@ -2674,5 +2674,25 @@ def _print_latex_(self, l, **kwargs):
26742674
str += r"{%s} & {%s}\\" % (latex(left), latex(right))
26752675
print(str[:-2] + r"\end{cases}")
26762676

2677+
def _sympy_(self, l):
2678+
"""
2679+
Convert this cases expression to its SymPy equivalent.
2680+
2681+
EXAMPLES::
2682+
2683+
sage: ex = cases(((x<0, pi), (x==1, 1), (True, 0)))
2684+
sage: assert ex == ex._sympy()._sage_()
2685+
"""
2686+
from sage.symbolic.ring import SR
2687+
from sympy import Piecewise as pw
2688+
args = []
2689+
for tup in l.operands():
2690+
cond,expr = tup.operands()
2691+
if SR(cond).is_numeric():
2692+
args.append((SR(expr)._sympy_(), bool(SR(cond)._sympy_())))
2693+
else:
2694+
args.append((SR(expr)._sympy_(), SR(cond)._sympy_()))
2695+
return pw(*args)
2696+
26772697
cases = Function_cases()
26782698

src/sage/interfaces/sympy.py

+45
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,24 @@ def _sympysage_kronecker_delta(self):
425425
from sage.functions.generalized import kronecker_delta
426426
return kronecker_delta(self.args[0]._sage_(), self.args[1]._sage_())
427427

428+
def _sympysage_piecewise(self):
429+
"""
430+
EXAMPLES::
431+
432+
sage: from sympy import Symbol, pi as spi, Eq, Lt, Piecewise
433+
sage: sx = Symbol('x')
434+
sage: sp = Piecewise((spi, Lt(sx,0)), (1, Eq(sx,1)), (0, True))
435+
sage: ex = cases(((x<0, pi), (x==1, 1), (True, 0)))
436+
sage: assert ex._sympy_() == sp
437+
sage: assert ex == sp._sage_()
438+
439+
sage: _ = var('y, z')
440+
sage: (x^y - z).integrate(y, algorithm="sympy")
441+
-y*z + cases(((log(x) == 0, y), (1, x^y/log(x))))
442+
"""
443+
from sage.functions.other import cases
444+
return cases([(p.cond._sage_(),p.expr._sage_()) for p in self.args])
445+
428446
def _sympysage_besselj(self):
429447
"""
430448
EXAMPLES::
@@ -550,6 +568,29 @@ def _sympysage_relational(self):
550568
ops = {Eq : eq, Ne : ne, Gt : gt, Lt : lt, Ge : ge, Le : le}
551569
return ops.get(self.func)(self.lhs._sage_(), self.rhs._sage_())
552570

571+
def _sympysage_false(self):
572+
"""
573+
EXAMPLES::
574+
575+
sage: from sympy.logic.boolalg import BooleanFalse
576+
sage: assert SR(False)._sympy_() == BooleanFalse() # known bug
577+
sage: assert SR(False) == BooleanFalse()._sage_()
578+
"""
579+
from sage.symbolic.ring import SR
580+
return SR(False)
581+
582+
def _sympysage_true(self):
583+
"""
584+
EXAMPLES::
585+
586+
sage: from sympy.logic.boolalg import BooleanTrue
587+
sage: assert SR(True)._sympy_() == BooleanTrue() # known bug
588+
sage: assert SR(True) == BooleanTrue()._sage_()
589+
"""
590+
from sage.symbolic.ring import SR
591+
return SR(True)
592+
593+
553594
#------------------------------------------------------------------
554595
from sage.repl.ipython_extension import run_once
555596

@@ -594,6 +635,7 @@ def sympy_init():
594635
from sympy.functions.special.hyper import hyper
595636
from sympy.functions.special.spherical_harmonics import Ynm
596637
from sympy.functions.special.tensor_functions import KroneckerDelta
638+
from sympy.logic.boolalg import BooleanTrue, BooleanFalse
597639
from sympy.integrals.integrals import Integral
598640
from sympy.series.order import Order
599641

@@ -627,6 +669,7 @@ def sympy_init():
627669
hyper._sage_ = _sympysage_hyp
628670
elliptic_k._sage_ = _sympysage_elliptic_k
629671
KroneckerDelta._sage_ = _sympysage_kronecker_delta
672+
Piecewise._sage_ = _sympysage_piecewise
630673
besselj._sage_ = _sympysage_besselj
631674
bessely._sage_ = _sympysage_bessely
632675
besseli._sage_ = _sympysage_besseli
@@ -635,6 +678,8 @@ def sympy_init():
635678
re._sage_ = _sympysage_re
636679
im._sage_ = _sympysage_im
637680
Abs._sage_ = _sympysage_abs
681+
BooleanFalse._sage_ = _sympysage_false
682+
BooleanTrue._sage_ = _sympysage_true
638683

639684
def check_expression(expr, var_symbols, only_from_sympy=False):
640685
"""

src/sage/symbolic/expression_conversions.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,12 @@ def composition(self, ex, operator):
781781
sage: s.composition(f, f.operator())
782782
asin(2)
783783
"""
784-
f = operator._sympy_init_()
785784
g = ex.operands()
785+
try:
786+
return operator._sympy_(*g)
787+
except (AttributeError, TypeError):
788+
pass
789+
f = operator._sympy_init_()
786790
import sympy
787791

788792
f_sympy = getattr(sympy, f, None)

0 commit comments

Comments
 (0)