0
# Elementary Functions
1
2
Standard mathematical functions including exponentials, logarithms, trigonometric functions, hyperbolic functions, and basic operations on complex numbers. All functions support both real and complex arguments where mathematically meaningful.
3
4
## Capabilities
5
6
### Powers and Roots
7
8
Basic power and root functions with arbitrary precision.
9
10
```python { .api }
11
def sqrt(x):
12
"""
13
Square root of x.
14
15
Args:
16
x: Input number
17
18
Returns:
19
Square root of x
20
"""
21
22
def cbrt(x):
23
"""
24
Cube root of x.
25
26
Args:
27
x: Input number
28
29
Returns:
30
Cube root of x
31
"""
32
33
def root(x, n):
34
"""
35
nth root of x.
36
37
Args:
38
x: Input number
39
n: Root index
40
41
Returns:
42
nth root of x
43
"""
44
45
def nthroot(x, n):
46
"""
47
nth root of x (alias for root).
48
49
Args:
50
x: Input number
51
n: Root index
52
53
Returns:
54
nth root of x
55
"""
56
57
def power(x, y):
58
"""
59
Power function x^y.
60
61
Args:
62
x: Base
63
y: Exponent
64
65
Returns:
66
x raised to power y
67
"""
68
69
def hypot(x, y):
70
"""
71
Euclidean distance sqrt(x^2 + y^2).
72
73
Args:
74
x, y: Input numbers
75
76
Returns:
77
Euclidean distance
78
"""
79
```
80
81
### Exponential Functions
82
83
Exponential functions and variants.
84
85
```python { .api }
86
def exp(x):
87
"""
88
Exponential function e^x.
89
90
Args:
91
x: Input number
92
93
Returns:
94
e raised to power x
95
"""
96
97
def expm1(x):
98
"""
99
exp(x) - 1, computed accurately for small x.
100
101
Args:
102
x: Input number
103
104
Returns:
105
exp(x) - 1
106
"""
107
108
109
def expj(x):
110
"""
111
exp(j*x) where j is the imaginary unit.
112
113
Args:
114
x: Input number
115
116
Returns:
117
exp(j*x) = cos(x) + j*sin(x)
118
"""
119
120
def expjpi(x):
121
"""
122
exp(j*π*x) where j is the imaginary unit.
123
124
Args:
125
x: Input number
126
127
Returns:
128
exp(j*π*x)
129
"""
130
131
def powm1(x, y):
132
"""
133
x^y - 1, computed accurately when x^y is close to 1.
134
135
Args:
136
x: Base
137
y: Exponent
138
139
Returns:
140
x^y - 1
141
"""
142
```
143
144
### Logarithmic Functions
145
146
Logarithmic functions with various bases.
147
148
```python { .api }
149
def ln(x):
150
"""
151
Natural logarithm (base e).
152
153
Args:
154
x: Input number (x > 0 for real result)
155
156
Returns:
157
Natural logarithm of x
158
"""
159
160
def log(x, b=None):
161
"""
162
Logarithm with specified base (default: natural log).
163
164
Args:
165
x: Input number
166
b: Base (optional, default is e)
167
168
Returns:
169
Logarithm of x to base b
170
"""
171
172
def log10(x):
173
"""
174
Base-10 logarithm.
175
176
Args:
177
x: Input number
178
179
Returns:
180
Base-10 logarithm of x
181
"""
182
183
def log1p(x):
184
"""
185
ln(1 + x), computed accurately for small x.
186
187
Args:
188
x: Input number
189
190
Returns:
191
ln(1 + x)
192
"""
193
194
def ldexp(x, n):
195
"""
196
Compute x * 2^n efficiently.
197
198
Args:
199
x: Input number
200
n: Integer exponent
201
202
Returns:
203
x * 2^n
204
"""
205
206
def frexp(x):
207
"""
208
Extract mantissa and exponent: x = mantissa * 2^exponent.
209
210
Args:
211
x: Input number
212
213
Returns:
214
tuple: (mantissa, exponent) where 0.5 <= |mantissa| < 1
215
"""
216
```
217
218
### Trigonometric Functions
219
220
Standard trigonometric functions with high precision.
221
222
```python { .api }
223
def sin(x):
224
"""
225
Sine function.
226
227
Args:
228
x: Angle in radians
229
230
Returns:
231
sin(x)
232
"""
233
234
def cos(x):
235
"""
236
Cosine function.
237
238
Args:
239
x: Angle in radians
240
241
Returns:
242
cos(x)
243
"""
244
245
def tan(x):
246
"""
247
Tangent function.
248
249
Args:
250
x: Angle in radians
251
252
Returns:
253
tan(x)
254
"""
255
256
def sec(x):
257
"""
258
Secant function (1/cos(x)).
259
260
Args:
261
x: Angle in radians
262
263
Returns:
264
sec(x) = 1/cos(x)
265
"""
266
267
def csc(x):
268
"""
269
Cosecant function (1/sin(x)).
270
271
Args:
272
x: Angle in radians
273
274
Returns:
275
csc(x) = 1/sin(x)
276
"""
277
278
def cot(x):
279
"""
280
Cotangent function (1/tan(x)).
281
282
Args:
283
x: Angle in radians
284
285
Returns:
286
cot(x) = 1/tan(x)
287
"""
288
289
def sinpi(x):
290
"""
291
sin(π*x), computed accurately.
292
293
Args:
294
x: Input number
295
296
Returns:
297
sin(π*x)
298
"""
299
300
def cospi(x):
301
"""
302
cos(π*x), computed accurately.
303
304
Args:
305
x: Input number
306
307
Returns:
308
cos(π*x)
309
"""
310
311
def cos_sin(x):
312
"""
313
Compute cos(x) and sin(x) simultaneously.
314
315
Args:
316
x: Angle in radians
317
318
Returns:
319
tuple: (cos(x), sin(x))
320
"""
321
322
def cospi_sinpi(x):
323
"""
324
Compute cos(π*x) and sin(π*x) simultaneously.
325
326
Args:
327
x: Input number
328
329
Returns:
330
tuple: (cos(π*x), sin(π*x))
331
"""
332
```
333
334
### Inverse Trigonometric Functions
335
336
Inverse trigonometric functions.
337
338
```python { .api }
339
def asin(x):
340
"""
341
Inverse sine (arcsine).
342
343
Args:
344
x: Input number (-1 <= x <= 1 for real result)
345
346
Returns:
347
arcsin(x) in radians
348
"""
349
350
def acos(x):
351
"""
352
Inverse cosine (arccosine).
353
354
Args:
355
x: Input number (-1 <= x <= 1 for real result)
356
357
Returns:
358
arccos(x) in radians
359
"""
360
361
def atan(x):
362
"""
363
Inverse tangent (arctangent).
364
365
Args:
366
x: Input number
367
368
Returns:
369
arctan(x) in radians
370
"""
371
372
def asec(x):
373
"""
374
Inverse secant.
375
376
Args:
377
x: Input number (|x| >= 1 for real result)
378
379
Returns:
380
arcsec(x) in radians
381
"""
382
383
def acsc(x):
384
"""
385
Inverse cosecant.
386
387
Args:
388
x: Input number (|x| >= 1 for real result)
389
390
Returns:
391
arccsc(x) in radians
392
"""
393
394
def acot(x):
395
"""
396
Inverse cotangent.
397
398
Args:
399
x: Input number
400
401
Returns:
402
arccot(x) in radians
403
"""
404
405
def atan2(y, x):
406
"""
407
Two-argument inverse tangent.
408
409
Args:
410
y: y-coordinate
411
x: x-coordinate
412
413
Returns:
414
Angle in radians from positive x-axis to point (x, y)
415
"""
416
```
417
418
### Hyperbolic Functions
419
420
Hyperbolic functions and their inverses.
421
422
```python { .api }
423
def sinh(x):
424
"""
425
Hyperbolic sine.
426
427
Args:
428
x: Input number
429
430
Returns:
431
sinh(x) = (e^x - e^(-x)) / 2
432
"""
433
434
def cosh(x):
435
"""
436
Hyperbolic cosine.
437
438
Args:
439
x: Input number
440
441
Returns:
442
cosh(x) = (e^x + e^(-x)) / 2
443
"""
444
445
def tanh(x):
446
"""
447
Hyperbolic tangent.
448
449
Args:
450
x: Input number
451
452
Returns:
453
tanh(x) = sinh(x) / cosh(x)
454
"""
455
456
def sech(x):
457
"""
458
Hyperbolic secant.
459
460
Args:
461
x: Input number
462
463
Returns:
464
sech(x) = 1 / cosh(x)
465
"""
466
467
def csch(x):
468
"""
469
Hyperbolic cosecant.
470
471
Args:
472
x: Input number
473
474
Returns:
475
csch(x) = 1 / sinh(x)
476
"""
477
478
def coth(x):
479
"""
480
Hyperbolic cotangent.
481
482
Args:
483
x: Input number
484
485
Returns:
486
coth(x) = cosh(x) / sinh(x)
487
"""
488
```
489
490
### Inverse Hyperbolic Functions
491
492
Inverse hyperbolic functions.
493
494
```python { .api }
495
def asinh(x):
496
"""
497
Inverse hyperbolic sine.
498
499
Args:
500
x: Input number
501
502
Returns:
503
arcsinh(x)
504
"""
505
506
def acosh(x):
507
"""
508
Inverse hyperbolic cosine.
509
510
Args:
511
x: Input number (x >= 1 for real result)
512
513
Returns:
514
arccosh(x)
515
"""
516
517
def atanh(x):
518
"""
519
Inverse hyperbolic tangent.
520
521
Args:
522
x: Input number (|x| < 1 for real result)
523
524
Returns:
525
arctanh(x)
526
"""
527
528
def asech(x):
529
"""
530
Inverse hyperbolic secant.
531
532
Args:
533
x: Input number (0 < x <= 1 for real result)
534
535
Returns:
536
arcsech(x)
537
"""
538
539
def acsch(x):
540
"""
541
Inverse hyperbolic cosecant.
542
543
Args:
544
x: Input number (x != 0)
545
546
Returns:
547
arccsch(x)
548
"""
549
550
def acoth(x):
551
"""
552
Inverse hyperbolic cotangent.
553
554
Args:
555
x: Input number (|x| > 1 for real result)
556
557
Returns:
558
arccoth(x)
559
"""
560
```
561
562
### Rounding and Integer Functions
563
564
Functions for rounding and integer operations.
565
566
```python { .api }
567
def floor(x):
568
"""
569
Floor function (round down to integer).
570
571
Args:
572
x: Input number
573
574
Returns:
575
Largest integer <= x
576
"""
577
578
def ceil(x):
579
"""
580
Ceiling function (round up to integer).
581
582
Args:
583
x: Input number
584
585
Returns:
586
Smallest integer >= x
587
"""
588
589
def nint(x):
590
"""
591
Round to nearest integer.
592
593
Args:
594
x: Input number
595
596
Returns:
597
Nearest integer to x
598
"""
599
600
def frac(x):
601
"""
602
Fractional part of x.
603
604
Args:
605
x: Input number
606
607
Returns:
608
x - floor(x)
609
"""
610
611
def fmod(x, y):
612
"""
613
Floating-point remainder of x/y.
614
615
Args:
616
x: Dividend
617
y: Divisor
618
619
Returns:
620
Remainder of x/y
621
"""
622
```
623
624
### Absolute Value and Sign Functions
625
626
Functions for absolute values and signs.
627
628
```python { .api }
629
def fabs(x):
630
"""
631
Absolute value.
632
633
Args:
634
x: Input number
635
636
Returns:
637
|x|
638
"""
639
640
def sign(x):
641
"""
642
Sign function.
643
644
Args:
645
x: Input number
646
647
Returns:
648
-1 if x < 0, 0 if x = 0, 1 if x > 0
649
"""
650
```
651
652
### Angle Conversion
653
654
Functions for converting between degrees and radians.
655
656
```python { .api }
657
def degrees(x):
658
"""
659
Convert radians to degrees.
660
661
Args:
662
x: Angle in radians
663
664
Returns:
665
Angle in degrees
666
"""
667
668
def radians(x):
669
"""
670
Convert degrees to radians.
671
672
Args:
673
x: Angle in degrees
674
675
Returns:
676
Angle in radians
677
"""
678
```
679
680
### Sinc Functions
681
682
Sinc functions and variants.
683
684
```python { .api }
685
def sinc(x):
686
"""
687
Sinc function sin(x)/x.
688
689
Args:
690
x: Input number
691
692
Returns:
693
sin(x)/x with proper limit at x=0
694
"""
695
696
def sincpi(x):
697
"""
698
Sinc function sin(π*x)/(π*x).
699
700
Args:
701
x: Input number
702
703
Returns:
704
sin(π*x)/(π*x) with proper limit at x=0
705
"""
706
```
707
708
### Usage Examples
709
710
```python
711
import mpmath
712
from mpmath import mp
713
714
# Set precision
715
mp.dps = 25
716
717
# Basic functions
718
x = mp.mpf('1.5')
719
print(f"sqrt({x}) = {mp.sqrt(x)}")
720
print(f"exp({x}) = {mp.exp(x)}")
721
print(f"ln({x}) = {mp.ln(x)}")
722
723
# Trigonometric functions
724
angle = mp.pi / 4 # 45 degrees
725
print(f"sin(π/4) = {mp.sin(angle)}")
726
print(f"cos(π/4) = {mp.cos(angle)}")
727
print(f"tan(π/4) = {mp.tan(angle)}")
728
729
# Complex arguments work too
730
z = mp.mpc(1, 1) # 1 + j
731
print(f"exp(1+j) = {mp.exp(z)}")
732
print(f"sin(1+j) = {mp.sin(z)}")
733
734
# High precision constants
735
print(f"π = {mp.pi}")
736
print(f"e = {mp.e}")
737
738
# Inverse functions
739
print(f"asin(1) = {mp.asin(1)}") # Should be π/2
740
print(f"atan(1) = {mp.atan(1)}") # Should be π/4
741
742
# Hyperbolic functions
743
print(f"sinh(1) = {mp.sinh(1)}")
744
print(f"cosh(1) = {mp.cosh(1)}")
745
print(f"tanh(1) = {mp.tanh(1)}")
746
747
# Special accuracy functions
748
small_x = mp.mpf('1e-10')
749
print(f"expm1({small_x}) = {mp.expm1(small_x)}") # More accurate than exp(x)-1
750
print(f"log1p({small_x}) = {mp.log1p(small_x)}") # More accurate than ln(1+x)
751
```