Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
gh-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: #38876
Reported by: Frédéric Chapoton
Reviewer(s): gmou3
  • Loading branch information
Release Manager committed Nov 2, 2024
2 parents 34f93ae + 770d5e8 commit 01c7555
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/sage/functions/piecewise.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,39 +673,40 @@ def piecewise_add(self, parameters, variable, other):
funcs = []
contains_lower = False
contains_upper = False
for i in range(len(points)-1):
for i in range(len(points) - 1):
a, b = points[i], points[i + 1]
try:
contains_lower = (self.domain().contains(points[i]) or
other.domain().contains(points[i])) and not contains_upper
contains_upper = (self.domain().contains(points[i+1]) or
other.domain().contains(points[i+1]))
contains_lower = (self.domain().contains(a) or
other.domain().contains(a)) and not contains_upper
contains_upper = (self.domain().contains(b) or
other.domain().contains(b))
if contains_lower:
if contains_upper:
rs = RealSet.closed(points[i], points[i+1])
rs = RealSet.closed(a, b)
else:
rs = RealSet.closed_open(points[i], points[i+1])
rs = RealSet.closed_open(a, b)
else:
if contains_upper:
rs = RealSet.open_closed(points[i], points[i+1])
rs = RealSet.open_closed(a, b)
else:
rs = RealSet.open(points[i], points[i+1])
point = (points[i+1] + points[i])/2
rs = RealSet.open(a, b)
point = (b + a) / 2
except ValueError:
if points[i] == minus_infinity and points[i+1] == infinity:
if a == minus_infinity and b == infinity:
rs = RealSet.open(minus_infinity, infinity)
point = 0
elif points[i] == minus_infinity:
elif a == minus_infinity:
if contains_lower:
rs = RealSet.unbounded_below_closed(points[i+1])
rs = RealSet.unbounded_below_closed(b)
else:
rs = RealSet.unbounded_below_open(points[i+1])
point = points[i+1]-1
elif points[i+1] == infinity:
rs = RealSet.unbounded_below_open(b)
point = b - 1
elif b == infinity:
if contains_upper:
rs = RealSet.unbounded_above_closed(points[i])
rs = RealSet.unbounded_above_closed(a)
else:
rs = RealSet.unbounded_above_open(points[i])
point = points[i]+1
rs = RealSet.unbounded_above_open(a)
point = a + 1
else:
raise
try:
Expand Down Expand Up @@ -1160,17 +1161,17 @@ def laplace(self, parameters, variable, x='x', s='t'):
(s + 1)*e^(-s)/s^2 + 2*e^(-s)/s - 1/s^2
"""
from sage.symbolic.assumptions import assume, forget
from sage.functions.log import exp

x = SR.var(x)
s = SR.var(s)
assume(s > 0)
exp_sx = (-s * x).exp()
result = 0
for domain, f in parameters:
for interval in domain:
a = interval.lower()
b = interval.upper()
result += (SR(f)*exp(-s*x)).integral(x, a, b)
result += (SR(f) * exp_sx).integral(x, a, b)
forget(s > 0)
return result

Expand Down

0 comments on commit 01c7555

Please sign in to comment.