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

Commit 79ab0ac

Browse files
committed
10070: heaviside, unit_step revamp
1 parent 7d3fd60 commit 79ab0ac

File tree

1 file changed

+39
-117
lines changed

1 file changed

+39
-117
lines changed

src/sage/functions/generalized.py

Lines changed: 39 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#
5252
##############################################################################
5353

54-
from sage.symbolic.function import BuiltinFunction
54+
from sage.symbolic.function import (BuiltinFunction, GinacFunction)
5555
from sage.rings.all import ComplexIntervalField, ZZ
5656

5757
class FunctionDiracDelta(BuiltinFunction):
@@ -166,7 +166,7 @@ def _evalf_(self, x, **kwds):
166166

167167
dirac_delta = FunctionDiracDelta()
168168

169-
class FunctionHeaviside(BuiltinFunction):
169+
class FunctionHeaviside(GinacFunction):
170170
r"""
171171
The Heaviside step function, `H(x)` (``heaviside(x)``).
172172
@@ -191,10 +191,29 @@ class FunctionHeaviside(BuiltinFunction):
191191
sage: heaviside(x)
192192
heaviside(x)
193193
194+
sage: heaviside(-1/2)
195+
0
196+
sage: heaviside(exp(-1000000000000000000000))
197+
1
198+
194199
TESTS::
195200
196201
sage: heaviside(x)._sympy_()
197202
Heaviside(x)
203+
sage: heaviside(x).subs(x=1)
204+
1
205+
sage: heaviside(x).subs(x=-1)
206+
0
207+
208+
::
209+
210+
sage: ex = heaviside(x)+1
211+
sage: t = loads(dumps(ex)); t
212+
heaviside(x) + 1
213+
sage: bool(t == ex)
214+
True
215+
sage: t.subs(x=1)
216+
2
198217
199218
REFERENCES:
200219
@@ -225,74 +244,16 @@ def __init__(self):
225244
Heaviside(x)
226245
sage: heaviside(x)._giac_()
227246
Heaviside(x)
247+
sage: h(x) = heaviside(x)
248+
sage: h(pi).numerical_approx()
249+
1.00000000000000
228250
"""
229-
BuiltinFunction.__init__(self, "heaviside", latex_name="H",
251+
GinacFunction.__init__(self, "heaviside", latex_name="H",
230252
conversions=dict(maxima='hstep',
231253
mathematica='HeavisideTheta',
232254
sympy='Heaviside',
233255
giac='Heaviside'))
234256

235-
def _eval_(self, x):
236-
"""
237-
INPUT:
238-
239-
- ``x`` - a real number or a symbolic expression
240-
241-
EXAMPLES::
242-
243-
sage: heaviside(-1/2)
244-
0
245-
sage: heaviside(1)
246-
1
247-
sage: heaviside(0)
248-
heaviside(0)
249-
sage: heaviside(x)
250-
heaviside(x)
251-
sage: heaviside(exp(-1000000000000000000000))
252-
1
253-
254-
Evaluation test::
255-
256-
sage: heaviside(x).subs(x=1)
257-
1
258-
sage: heaviside(x).subs(x=-1)
259-
0
260-
261-
::
262-
263-
sage: ex = heaviside(x)+1
264-
sage: t = loads(dumps(ex)); t
265-
heaviside(x) + 1
266-
sage: bool(t == ex)
267-
True
268-
sage: t.subs(x=1)
269-
2
270-
"""
271-
try:
272-
return self._evalf_(x)
273-
except (TypeError,ValueError): # x is symbolic
274-
pass
275-
return None
276-
277-
def _evalf_(self, x, **kwds):
278-
"""
279-
TESTS::
280-
281-
sage: h(x) = heaviside(x)
282-
sage: h(pi).numerical_approx()
283-
1.00000000000000
284-
"""
285-
approx_x = ComplexIntervalField()(x)
286-
if bool(approx_x.imag() == 0): # x is real
287-
if bool(approx_x.real() == 0): # x is zero
288-
return None
289-
# Now we have a non-zero real
290-
if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
291-
return 1
292-
else:
293-
return 0
294-
raise ValueError("Numeric evaluation of symbolic expression")
295-
296257
def _derivative_(self, x, diff_param=None):
297258
"""
298259
Derivative of Heaviside step function
@@ -306,7 +267,7 @@ def _derivative_(self, x, diff_param=None):
306267

307268
heaviside = FunctionHeaviside()
308269

309-
class FunctionUnitStep(BuiltinFunction):
270+
class FunctionUnitStep(GinacFunction):
310271
r"""
311272
The unit step function, `\mathrm{u}(x)` (``unit_step(x)``).
312273
@@ -330,6 +291,18 @@ class FunctionUnitStep(BuiltinFunction):
330291
1
331292
sage: unit_step(x)
332293
unit_step(x)
294+
sage: unit_step(-exp(-10000000000000000000))
295+
0
296+
297+
TESTS::
298+
299+
sage: unit_step(x).subs(x=1)
300+
1
301+
sage: unit_step(x).subs(x=0)
302+
1
303+
sage: h(x) = unit_step(x)
304+
sage: h(pi).numerical_approx()
305+
1.00000000000000
333306
"""
334307
def __init__(self):
335308
r"""
@@ -359,60 +332,9 @@ def __init__(self):
359332
sage: t.subs(x=0)
360333
2
361334
"""
362-
BuiltinFunction.__init__(self, "unit_step", latex_name=r"\mathrm{u}",
335+
GinacFunction.__init__(self, "unit_step", latex_name=r"\mathrm{u}",
363336
conversions=dict(mathematica='UnitStep'))
364337

365-
def _eval_(self, x):
366-
"""
367-
INPUT:
368-
369-
- ``x`` - a real number or a symbolic expression
370-
371-
EXAMPLES::
372-
373-
sage: unit_step(-1)
374-
0
375-
sage: unit_step(1)
376-
1
377-
sage: unit_step(0)
378-
1
379-
sage: unit_step(x)
380-
unit_step(x)
381-
sage: unit_step(-exp(-10000000000000000000))
382-
0
383-
384-
Evaluation test::
385-
386-
sage: unit_step(x).subs(x=1)
387-
1
388-
sage: unit_step(x).subs(x=0)
389-
1
390-
"""
391-
try:
392-
return self._evalf_(x)
393-
except (TypeError,ValueError): # x is symbolic
394-
pass
395-
return None
396-
397-
def _evalf_(self, x, **kwds):
398-
"""
399-
TESTS::
400-
401-
sage: h(x) = unit_step(x)
402-
sage: h(pi).numerical_approx()
403-
1.00000000000000
404-
"""
405-
approx_x = ComplexIntervalField()(x)
406-
if bool(approx_x.imag() == 0): # x is real
407-
if bool(approx_x.real() == 0): # x is zero
408-
return 1
409-
# Now we have a non-zero real
410-
if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
411-
return 1
412-
else:
413-
return 0
414-
raise ValueError("Numeric evaluation of symbolic expression")
415-
416338
def _derivative_(self, x, diff_param=None):
417339
"""
418340
Derivative of unit step function

0 commit comments

Comments
 (0)