Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/functions/hyperbolic.py
4045 views
1
"""
2
Hyperbolic Functions
3
"""
4
from sage.symbolic.function import GinacFunction, BuiltinFunction
5
import math
6
7
class HyperbolicFunction(BuiltinFunction):
8
r"""
9
Abstract base class for the functions defined in this file.
10
11
EXAMPLES::
12
13
sage: from sage.functions.hyperbolic import HyperbolicFunction
14
sage: f = HyperbolicFunction('foo', latex_name='\\foo', conversions={'mathematica':'Foo'},evalf_float=lambda x: 2*x)
15
sage: f(x)
16
foo(x)
17
sage: f(0.5r)
18
1.0
19
sage: latex(f(x))
20
\foo\left(x\right)
21
sage: f(x)._mathematica_init_()
22
'Foo[x]'
23
"""
24
_eval_ = BuiltinFunction._eval_default
25
def __init__(self, name, latex_name=None, conversions=None,
26
evalf_float=None):
27
"""
28
Note that subclasses of HyperbolicFunction should be instantiated only
29
once, since they inherit from BuiltinFunction which only uses the
30
name and class to check if a function was already registered.
31
32
EXAMPLES::
33
34
sage: from sage.functions.hyperbolic import HyperbolicFunction
35
sage: class Barh(HyperbolicFunction):
36
... def __init__(self):
37
... HyperbolicFunction.__init__(self, 'barh')
38
sage: barh = Barh()
39
sage: barh(x)
40
barh(x)
41
"""
42
self._evalf_float = evalf_float
43
BuiltinFunction.__init__(self, name, latex_name=latex_name,
44
conversions=conversions)
45
46
def _evalf_(self, x, parent):
47
"""
48
EXAMPLES::
49
50
sage: from sage.functions.hyperbolic import HyperbolicFunction
51
sage: class Fooh(HyperbolicFunction):
52
... def __init__(self):
53
... HyperbolicFunction.__init__(self, 'fooh',evalf_float=lambda x: 2*x)
54
sage: fooh = Fooh()
55
sage: fooh(float(5))
56
10.0
57
sage: fooh(0.5r)
58
1.0
59
sage: fooh(x).subs(x=.5r)
60
1.0
61
sage: fooh(x).n()
62
Traceback (most recent call last):
63
...
64
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'fooh'
65
"""
66
if parent is float:
67
return self._evalf_float(x)
68
return getattr(x, self.name())()
69
70
class Function_sinh(GinacFunction):
71
def __init__(self):
72
r"""
73
The hyperbolic sine function.
74
75
EXAMPLES::
76
77
sage: sinh(pi)
78
sinh(pi)
79
sage: sinh(3.1415)
80
11.5476653707437
81
sage: float(sinh(pi))
82
11.54873935725774...
83
sage: RR(sinh(pi))
84
11.5487393572577
85
86
sage: latex(sinh(x))
87
\sinh\left(x\right)
88
89
To prevent automatic evaluation, use the ``hold`` parameter::
90
91
sage: sinh(arccosh(x),hold=True)
92
sinh(arccosh(x))
93
94
To then evaluate again, we currently must use Maxima via
95
:meth:`sage.symbolic.expression.Expression.simplify`::
96
97
sage: sinh(arccosh(x),hold=True).simplify()
98
sqrt(x - 1)*sqrt(x + 1)
99
100
"""
101
GinacFunction.__init__(self, "sinh", latex_name=r"\sinh")
102
103
sinh = Function_sinh()
104
105
class Function_cosh(GinacFunction):
106
def __init__(self):
107
r"""
108
The hyperbolic cosine function.
109
110
EXAMPLES::
111
112
sage: cosh(pi)
113
cosh(pi)
114
sage: cosh(3.1415)
115
11.5908832931176
116
sage: float(cosh(pi))
117
11.591953275521519
118
sage: RR(cosh(1/2))
119
1.12762596520638
120
121
sage: latex(cosh(x))
122
\cosh\left(x\right)
123
124
To prevent automatic evaluation, use the ``hold`` parameter::
125
126
sage: cosh(arcsinh(x),hold=True)
127
cosh(arcsinh(x))
128
129
To then evaluate again, we currently must use Maxima via
130
:meth:`sage.symbolic.expression.Expression.simplify`::
131
132
sage: cosh(arcsinh(x),hold=True).simplify()
133
sqrt(x^2 + 1)
134
135
"""
136
GinacFunction.__init__(self, "cosh", latex_name=r"\cosh")
137
138
cosh = Function_cosh()
139
140
141
class Function_tanh(GinacFunction):
142
def __init__(self):
143
r"""
144
The hyperbolic tangent function.
145
146
EXAMPLES::
147
148
sage: tanh(pi)
149
tanh(pi)
150
sage: tanh(3.1415)
151
0.996271386633702
152
sage: float(tanh(pi))
153
0.99627207622075
154
sage: tan(3.1415/4)
155
0.999953674278156
156
sage: tanh(pi/4)
157
tanh(1/4*pi)
158
sage: RR(tanh(1/2))
159
0.462117157260010
160
161
::
162
163
sage: CC(tanh(pi + I*e))
164
0.997524731976164 - 0.00279068768100315*I
165
sage: ComplexField(100)(tanh(pi + I*e))
166
0.99752473197616361034204366446 - 0.0027906876810031453884245163923*I
167
sage: CDF(tanh(pi + I*e))
168
0.997524731976 - 0.002790687681*I
169
170
To prevent automatic evaluation, use the ``hold`` parameter::
171
172
sage: tanh(arcsinh(x),hold=True)
173
tanh(arcsinh(x))
174
175
To then evaluate again, we currently must use Maxima via
176
:meth:`sage.symbolic.expression.Expression.simplify`::
177
178
sage: tanh(arcsinh(x),hold=True).simplify()
179
x/sqrt(x^2 + 1)
180
181
TESTS::
182
183
sage: latex(tanh(x))
184
\tanh\left(x\right)
185
"""
186
GinacFunction.__init__(self, "tanh", latex_name=r"\tanh")
187
188
tanh = Function_tanh()
189
190
class Function_coth(HyperbolicFunction):
191
def __init__(self):
192
r"""
193
The hyperbolic cotangent function.
194
195
EXAMPLES::
196
197
sage: coth(pi)
198
coth(pi)
199
sage: coth(3.1415)
200
1.00374256795520
201
sage: float(coth(pi))
202
1.0037418731973213
203
sage: RR(coth(pi))
204
1.00374187319732
205
206
sage: latex(coth(x))
207
\coth\left(x\right)
208
"""
209
HyperbolicFunction.__init__(self, "coth", latex_name=r"\coth",
210
evalf_float=lambda x: 1/math.tanh(x))
211
212
def _eval_numpy_(self, x):
213
"""
214
EXAMPLES::
215
216
sage: import numpy
217
sage: a = numpy.arange(2, 5)
218
sage: coth(a)
219
array([ 1.03731472, 1.00496982, 1.00067115])
220
"""
221
return 1 / tanh(x)
222
223
def _derivative_(self, *args, **kwds):
224
"""
225
EXAMPLES::
226
227
sage: bool(diff(coth(x), x) == diff(1/tanh(x), x))
228
True
229
sage: diff(coth(x), x)
230
-csch(x)^2
231
"""
232
x = args[0]
233
return -csch(x)**2
234
235
coth = Function_coth()
236
237
class Function_sech(HyperbolicFunction):
238
def __init__(self):
239
r"""
240
The hyperbolic secant function.
241
242
EXAMPLES::
243
244
sage: sech(pi)
245
sech(pi)
246
sage: sech(3.1415)
247
0.0862747018248192
248
sage: float(sech(pi))
249
0.0862667383340544...
250
sage: RR(sech(pi))
251
0.0862667383340544
252
253
sage: latex(sech(x))
254
{\rm sech}\left(x\right)
255
"""
256
HyperbolicFunction.__init__(self, "sech", latex_name=r"{\rm sech}",
257
evalf_float=lambda x: 1/math.cosh(x))
258
259
def _eval_numpy_(self, x):
260
"""
261
EXAMPLES::
262
263
sage: import numpy
264
sage: a = numpy.arange(2, 5)
265
sage: sech(a)
266
array([ 0.26580223, 0.09932793, 0.03661899])
267
"""
268
return 1 / cosh(x)
269
270
def _derivative_(self, *args, **kwds):
271
"""
272
EXAMPLES::
273
274
sage: bool(diff(sech(x), x) == diff(1/cosh(x), x))
275
True
276
sage: diff(sech(x), x)
277
-tanh(x)*sech(x)
278
"""
279
x = args[0]
280
return -sech(x)*tanh(x)
281
282
sech = Function_sech()
283
284
285
class Function_csch(HyperbolicFunction):
286
def __init__(self):
287
r"""
288
The hyperbolic cosecant function.
289
290
EXAMPLES::
291
292
sage: csch(pi)
293
csch(pi)
294
sage: csch(3.1415)
295
0.0865975907592133
296
sage: float(csch(pi))
297
0.0865895375300469...
298
sage: RR(csch(pi))
299
0.0865895375300470
300
301
sage: latex(csch(x))
302
{\rm csch}\left(x\right)
303
"""
304
HyperbolicFunction.__init__(self, "csch", latex_name=r"{\rm csch}",
305
evalf_float=lambda x: 1/math.sinh(x))
306
307
def _eval_numpy_(self, x):
308
"""
309
EXAMPLES::
310
311
sage: import numpy
312
sage: a = numpy.arange(2, 5)
313
sage: csch(a)
314
array([ 0.27572056, 0.09982157, 0.03664357])
315
"""
316
return 1 / sinh(x)
317
318
def _derivative_(self, *args, **kwds):
319
"""
320
EXAMPLES::
321
322
sage: bool(diff(csch(x), x) == diff(1/sinh(x), x))
323
True
324
sage: diff(csch(x), x)
325
-coth(x)*csch(x)
326
"""
327
x = args[0]
328
return -csch(x)*coth(x)
329
330
csch = Function_csch()
331
332
333
################################
334
# Inverse hyperbolic functions #
335
################################
336
337
class Function_arcsinh(GinacFunction):
338
def __init__(self):
339
r"""
340
The inverse of the hyperbolic sine function.
341
342
EXAMPLES::
343
344
sage: arcsinh
345
arcsinh
346
sage: arcsinh(0.5)
347
0.481211825059603
348
sage: arcsinh(1/2)
349
arcsinh(1/2)
350
sage: arcsinh(1 + I*1.0)
351
1.06127506190504 + 0.666239432492515*I
352
353
To prevent automatic evaluation use the ``hold`` argument::
354
355
sage: arcsinh(-2,hold=True)
356
arcsinh(-2)
357
358
To then evaluate again, we currently must use Maxima via
359
:meth:`sage.symbolic.expression.Expression.simplify`::
360
361
sage: arcsinh(-2,hold=True).simplify()
362
-arcsinh(2)
363
364
``conjugate(arcsinh(x))==arcsinh(conjugate(x))`` unless on the branch
365
cuts which run along the imaginary axis outside the interval [-I, +I].::
366
367
sage: conjugate(arcsinh(x))
368
conjugate(arcsinh(x))
369
sage: var('y', domain='positive')
370
y
371
sage: conjugate(arcsinh(y))
372
arcsinh(y)
373
sage: conjugate(arcsinh(y+I))
374
conjugate(arcsinh(y + I))
375
sage: conjugate(arcsinh(1/16))
376
arcsinh(1/16)
377
sage: conjugate(arcsinh(I/2))
378
arcsinh(-1/2*I)
379
sage: conjugate(arcsinh(2*I))
380
conjugate(arcsinh(2*I))
381
382
TESTS::
383
384
sage: arcsinh(x).operator()
385
arcsinh
386
sage: latex(arcsinh(x))
387
{\rm arcsinh}\left(x\right)
388
"""
389
GinacFunction.__init__(self, "arcsinh", latex_name=r"{\rm arcsinh}",
390
conversions=dict(maxima='asinh', sympy='asinh'))
391
392
arcsinh = asinh = Function_arcsinh()
393
394
class Function_arccosh(GinacFunction):
395
def __init__(self):
396
r"""
397
The inverse of the hyperbolic cosine function.
398
399
EXAMPLES::
400
401
sage: arccosh(1/2)
402
arccosh(1/2)
403
sage: arccosh(1 + I*1.0)
404
1.06127506190504 + 0.904556894302381*I
405
sage: float(arccosh(2))
406
1.3169578969248168
407
sage: cosh(float(arccosh(2)))
408
2.0
409
410
.. warning::
411
412
If the input is in the complex field or symbolic (which
413
includes rational and integer input), the output will
414
be complex. However, if the input is a real decimal, the
415
output will be real or `NaN`. See the examples for details.
416
417
::
418
419
sage: arccosh(0.5)
420
NaN
421
sage: arccosh(1/2)
422
arccosh(1/2)
423
sage: arccosh(1/2).n()
424
NaN
425
sage: arccosh(CC(0.5))
426
1.04719755119660*I
427
sage: arccosh(0)
428
1/2*I*pi
429
sage: arccosh(-1)
430
I*pi
431
432
To prevent automatic evaluation use the ``hold`` argument::
433
434
sage: arccosh(-1,hold=True)
435
arccosh(-1)
436
437
To then evaluate again, we currently must use Maxima via
438
:meth:`sage.symbolic.expression.Expression.simplify`::
439
440
sage: arccosh(-1,hold=True).simplify()
441
I*pi
442
443
``conjugate(arccosh(x))==arccosh(conjugate(x))`` unless on the branch
444
cut which runs along the real axis from +1 to -inf.::
445
446
sage: conjugate(arccosh(x))
447
conjugate(arccosh(x))
448
sage: var('y', domain='positive')
449
y
450
sage: conjugate(arccosh(y))
451
conjugate(arccosh(y))
452
sage: conjugate(arccosh(y+I))
453
conjugate(arccosh(y + I))
454
sage: conjugate(arccosh(1/16))
455
conjugate(arccosh(1/16))
456
sage: conjugate(arccosh(2))
457
arccosh(2)
458
sage: conjugate(arccosh(I/2))
459
arccosh(-1/2*I)
460
461
TESTS::
462
463
sage: arccosh(x).operator()
464
arccosh
465
sage: latex(arccosh(x))
466
{\rm arccosh}\left(x\right)
467
"""
468
GinacFunction.__init__(self, "arccosh", latex_name=r"{\rm arccosh}",
469
conversions=dict(maxima='acosh', sympy='acosh'))
470
471
arccosh = acosh = Function_arccosh()
472
473
class Function_arctanh(GinacFunction):
474
def __init__(self):
475
r"""
476
The inverse of the hyperbolic tangent function.
477
478
EXAMPLES::
479
480
sage: arctanh(0.5)
481
0.549306144334055
482
sage: arctanh(1/2)
483
arctanh(1/2)
484
sage: arctanh(1 + I*1.0)
485
0.402359478108525 + 1.01722196789785*I
486
487
To prevent automatic evaluation use the ``hold`` argument::
488
489
sage: arctanh(-1/2,hold=True)
490
arctanh(-1/2)
491
492
To then evaluate again, we currently must use Maxima via
493
:meth:`sage.symbolic.expression.Expression.simplify`::
494
495
sage: arctanh(-1/2,hold=True).simplify()
496
-arctanh(1/2)
497
498
``conjugate(arctanh(x))==arctanh(conjugate(x))`` unless on the branch
499
cuts which run along the real axis outside the interval [-1, +1].::
500
501
sage: conjugate(arctanh(x))
502
conjugate(arctanh(x))
503
sage: var('y', domain='positive')
504
y
505
sage: conjugate(arctanh(y))
506
conjugate(arctanh(y))
507
sage: conjugate(arctanh(y+I))
508
conjugate(arctanh(y + I))
509
sage: conjugate(arctanh(1/16))
510
arctanh(1/16)
511
sage: conjugate(arctanh(I/2))
512
arctanh(-1/2*I)
513
sage: conjugate(arctanh(-2*I))
514
arctanh(2*I)
515
516
TESTS::
517
518
sage: arctanh(x).operator()
519
arctanh
520
sage: latex(arctanh(x))
521
{\rm arctanh}\left(x\right)
522
"""
523
GinacFunction.__init__(self, "arctanh", latex_name=r"{\rm arctanh}",
524
conversions=dict(maxima='atanh', sympy='atanh'))
525
526
arctanh = atanh = Function_arctanh()
527
528
class Function_arccoth(HyperbolicFunction):
529
def __init__(self):
530
r"""
531
The inverse of the hyperbolic cotangent function.
532
533
EXAMPLES::
534
535
sage: arccoth(2.0)
536
0.549306144334055
537
sage: arccoth(2)
538
arccoth(2)
539
sage: arccoth(1 + I*1.0)
540
0.402359478108525 - 0.553574358897045*I
541
sage: arccoth(2).n(200)
542
0.54930614433405484569762261846126285232374527891137472586735
543
sage: float(arccoth(2))
544
0.5493061443340549
545
546
sage: latex(arccoth(x))
547
{\rm arccoth}\left(x\right)
548
"""
549
HyperbolicFunction.__init__(self, "arccoth",
550
latex_name=r"{\rm arccoth}",
551
conversions=dict(maxima='acoth', sympy='acoth'),
552
evalf_float=lambda x: atanh(float(1/x)))
553
554
def _eval_numpy_(self, x):
555
"""
556
EXAMPLES::
557
558
sage: import numpy
559
sage: a = numpy.arange(2,5)
560
sage: arccoth(a)
561
array([ 0.54930614, 0.34657359, 0.25541281])
562
"""
563
return arctanh(1.0 / x)
564
565
def _derivative_(self, *args, **kwds):
566
"""
567
EXAMPLES::
568
sage: bool(diff(acoth(x), x) == diff(atanh(x), x))
569
True
570
sage: diff(acoth(x), x)
571
-1/(x^2 - 1)
572
"""
573
x = args[0]
574
return -1/(x**2 - 1)
575
576
arccoth = acoth = Function_arccoth()
577
578
class Function_arcsech(HyperbolicFunction):
579
def __init__(self):
580
r"""
581
The inverse of the hyperbolic secant function.
582
583
EXAMPLES::
584
585
sage: arcsech(0.5)
586
1.31695789692482
587
sage: arcsech(1/2)
588
arcsech(1/2)
589
sage: arcsech(1 + I*1.0)
590
0.530637530952518 - 1.11851787964371*I
591
sage: arcsech(1/2).n(200)
592
1.3169578969248167086250463473079684440269819714675164797685
593
sage: float(arcsech(1/2))
594
1.3169578969248168
595
596
sage: latex(arcsech(x))
597
{\rm arcsech}\left(x\right)
598
"""
599
HyperbolicFunction.__init__(self, "arcsech",
600
latex_name=r"{\rm arcsech}",
601
evalf_float=lambda x: acosh(float(1/x)),
602
conversions=dict(maxima='asech'))
603
604
def _eval_numpy_(self, x):
605
"""
606
EXAMPLES::
607
608
sage: import numpy
609
sage: a = numpy.linspace(0,1,3)
610
sage: arcsech(a)
611
array([ inf, 1.3169579, 0. ])
612
"""
613
return arccosh(1.0 / x)
614
615
def _derivative_(self, *args, **kwds):
616
"""
617
EXAMPLES::
618
619
sage: diff(asech(x), x)
620
-1/((x + 1)*x*sqrt(-(x - 1)/(x + 1)))
621
"""
622
x = args[0]
623
return -1/(x * (x+1) * ( (1-x)/(1+x) ).sqrt())
624
625
arcsech = asech = Function_arcsech()
626
627
class Function_arccsch(HyperbolicFunction):
628
def __init__(self):
629
r"""
630
The inverse of the hyperbolic cosecant function.
631
632
EXAMPLES::
633
634
sage: arccsch(2.0)
635
0.481211825059603
636
sage: arccsch(2)
637
arccsch(2)
638
sage: arccsch(1 + I*1.0)
639
0.530637530952518 - 0.452278447151191*I
640
sage: arccsch(1).n(200)
641
0.88137358701954302523260932497979230902816032826163541075330
642
sage: float(arccsch(1))
643
0.881373587019543
644
645
sage: latex(arccsch(x))
646
{\rm arccsch}\left(x\right)
647
"""
648
HyperbolicFunction.__init__(self, "arccsch",
649
latex_name=r"{\rm arccsch}",
650
evalf_float=lambda x: arcsinh(float(1/x)),
651
conversions=dict(maxima='acsch'))
652
653
def _eval_numpy_(self, x):
654
"""
655
EXAMPLES::
656
657
sage: import numpy
658
sage: a = numpy.linspace(0,1,3)
659
sage: arccsch(a)
660
array([ inf, 1.44363548, 0.88137359])
661
"""
662
return arcsinh(1.0 / x)
663
664
def _derivative_(self, *args, **kwds):
665
"""
666
EXAMPLES::
667
668
sage: diff(acsch(x), x)
669
-1/(sqrt(1/x^2 + 1)*x^2)
670
"""
671
x = args[0]
672
return -1/(x**2 * (1 + x**(-2)).sqrt())
673
674
arccsch = acsch = Function_arccsch()
675
676