51
51
#
52
52
##############################################################################
53
53
54
- from sage .symbolic .function import BuiltinFunction
54
+ from sage .symbolic .function import ( BuiltinFunction , GinacFunction )
55
55
from sage .rings .all import ComplexIntervalField , ZZ
56
56
57
57
class FunctionDiracDelta (BuiltinFunction ):
@@ -166,7 +166,7 @@ def _evalf_(self, x, **kwds):
166
166
167
167
dirac_delta = FunctionDiracDelta ()
168
168
169
- class FunctionHeaviside (BuiltinFunction ):
169
+ class FunctionHeaviside (GinacFunction ):
170
170
r"""
171
171
The Heaviside step function, `H(x)` (``heaviside(x)``).
172
172
@@ -191,10 +191,29 @@ class FunctionHeaviside(BuiltinFunction):
191
191
sage: heaviside(x)
192
192
heaviside(x)
193
193
194
+ sage: heaviside(-1/2)
195
+ 0
196
+ sage: heaviside(exp(-1000000000000000000000))
197
+ 1
198
+
194
199
TESTS::
195
200
196
201
sage: heaviside(x)._sympy_()
197
202
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
198
217
199
218
REFERENCES:
200
219
@@ -225,74 +244,16 @@ def __init__(self):
225
244
Heaviside(x)
226
245
sage: heaviside(x)._giac_()
227
246
Heaviside(x)
247
+ sage: h(x) = heaviside(x)
248
+ sage: h(pi).numerical_approx()
249
+ 1.00000000000000
228
250
"""
229
- BuiltinFunction .__init__ (self , "heaviside" , latex_name = "H" ,
251
+ GinacFunction .__init__ (self , "heaviside" , latex_name = "H" ,
230
252
conversions = dict (maxima = 'hstep' ,
231
253
mathematica = 'HeavisideTheta' ,
232
254
sympy = 'Heaviside' ,
233
255
giac = 'Heaviside' ))
234
256
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
-
296
257
def _derivative_ (self , x , diff_param = None ):
297
258
"""
298
259
Derivative of Heaviside step function
@@ -306,7 +267,7 @@ def _derivative_(self, x, diff_param=None):
306
267
307
268
heaviside = FunctionHeaviside ()
308
269
309
- class FunctionUnitStep (BuiltinFunction ):
270
+ class FunctionUnitStep (GinacFunction ):
310
271
r"""
311
272
The unit step function, `\mathrm{u}(x)` (``unit_step(x)``).
312
273
@@ -330,6 +291,18 @@ class FunctionUnitStep(BuiltinFunction):
330
291
1
331
292
sage: unit_step(x)
332
293
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
333
306
"""
334
307
def __init__ (self ):
335
308
r"""
@@ -359,60 +332,9 @@ def __init__(self):
359
332
sage: t.subs(x=0)
360
333
2
361
334
"""
362
- BuiltinFunction .__init__ (self , "unit_step" , latex_name = r"\mathrm{u}" ,
335
+ GinacFunction .__init__ (self , "unit_step" , latex_name = r"\mathrm{u}" ,
363
336
conversions = dict (mathematica = 'UnitStep' ))
364
337
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
-
416
338
def _derivative_ (self , x , diff_param = None ):
417
339
"""
418
340
Derivative of unit step function
0 commit comments