Skip to content

Commit 01c7555

Browse files
author
Release Manager
committed
sagemathgh-38876: minor details in piecewise functions just small changes for better speed maybe ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: sagemath#38876 Reported by: Frédéric Chapoton Reviewer(s): gmou3
2 parents 34f93ae + 770d5e8 commit 01c7555

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/sage/functions/piecewise.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -673,39 +673,40 @@ def piecewise_add(self, parameters, variable, other):
673673
funcs = []
674674
contains_lower = False
675675
contains_upper = False
676-
for i in range(len(points)-1):
676+
for i in range(len(points) - 1):
677+
a, b = points[i], points[i + 1]
677678
try:
678-
contains_lower = (self.domain().contains(points[i]) or
679-
other.domain().contains(points[i])) and not contains_upper
680-
contains_upper = (self.domain().contains(points[i+1]) or
681-
other.domain().contains(points[i+1]))
679+
contains_lower = (self.domain().contains(a) or
680+
other.domain().contains(a)) and not contains_upper
681+
contains_upper = (self.domain().contains(b) or
682+
other.domain().contains(b))
682683
if contains_lower:
683684
if contains_upper:
684-
rs = RealSet.closed(points[i], points[i+1])
685+
rs = RealSet.closed(a, b)
685686
else:
686-
rs = RealSet.closed_open(points[i], points[i+1])
687+
rs = RealSet.closed_open(a, b)
687688
else:
688689
if contains_upper:
689-
rs = RealSet.open_closed(points[i], points[i+1])
690+
rs = RealSet.open_closed(a, b)
690691
else:
691-
rs = RealSet.open(points[i], points[i+1])
692-
point = (points[i+1] + points[i])/2
692+
rs = RealSet.open(a, b)
693+
point = (b + a) / 2
693694
except ValueError:
694-
if points[i] == minus_infinity and points[i+1] == infinity:
695+
if a == minus_infinity and b == infinity:
695696
rs = RealSet.open(minus_infinity, infinity)
696697
point = 0
697-
elif points[i] == minus_infinity:
698+
elif a == minus_infinity:
698699
if contains_lower:
699-
rs = RealSet.unbounded_below_closed(points[i+1])
700+
rs = RealSet.unbounded_below_closed(b)
700701
else:
701-
rs = RealSet.unbounded_below_open(points[i+1])
702-
point = points[i+1]-1
703-
elif points[i+1] == infinity:
702+
rs = RealSet.unbounded_below_open(b)
703+
point = b - 1
704+
elif b == infinity:
704705
if contains_upper:
705-
rs = RealSet.unbounded_above_closed(points[i])
706+
rs = RealSet.unbounded_above_closed(a)
706707
else:
707-
rs = RealSet.unbounded_above_open(points[i])
708-
point = points[i]+1
708+
rs = RealSet.unbounded_above_open(a)
709+
point = a + 1
709710
else:
710711
raise
711712
try:
@@ -1160,17 +1161,17 @@ def laplace(self, parameters, variable, x='x', s='t'):
11601161
(s + 1)*e^(-s)/s^2 + 2*e^(-s)/s - 1/s^2
11611162
"""
11621163
from sage.symbolic.assumptions import assume, forget
1163-
from sage.functions.log import exp
11641164

11651165
x = SR.var(x)
11661166
s = SR.var(s)
11671167
assume(s > 0)
1168+
exp_sx = (-s * x).exp()
11681169
result = 0
11691170
for domain, f in parameters:
11701171
for interval in domain:
11711172
a = interval.lower()
11721173
b = interval.upper()
1173-
result += (SR(f)*exp(-s*x)).integral(x, a, b)
1174+
result += (SR(f) * exp_sx).integral(x, a, b)
11741175
forget(s > 0)
11751176
return result
11761177

0 commit comments

Comments
 (0)