0
# Mathematical Functions
1
2
Comprehensive mathematical function library providing GPU-accelerated implementations of trigonometric, hyperbolic, exponential, logarithmic, arithmetic, and special functions. All functions support universal function (ufunc) behavior with broadcasting, element-wise operations, and advanced indexing.
3
4
## Capabilities
5
6
### Trigonometric Functions
7
8
Standard trigonometric functions and their inverses, optimized for GPU execution.
9
10
```python { .api }
11
def sin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
12
"""
13
Trigonometric sine, element-wise.
14
15
Parameters:
16
- x: array_like, input array in radians
17
- out: ndarray, optional, output array
18
- where: array_like of bool, optional, condition
19
- casting: {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, casting rule
20
- order: {'K', 'A', 'C', 'F'}, memory layout
21
- dtype: data type, output type
22
- subok: bool, whether to return subclass
23
24
Returns:
25
- ndarray: Sine of each element
26
"""
27
28
def cos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
29
"""
30
Trigonometric cosine, element-wise.
31
32
Parameters:
33
- x: array_like, input array in radians
34
- out: ndarray, optional, output array
35
- where: array_like of bool, optional, condition
36
- casting: casting rule
37
- order: memory layout
38
- dtype: data type, output type
39
- subok: bool, whether to return subclass
40
41
Returns:
42
- ndarray: Cosine of each element
43
"""
44
45
def tan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
46
"""
47
Trigonometric tangent, element-wise.
48
49
Parameters:
50
- x: array_like, input array in radians
51
- out: ndarray, optional, output array
52
- where: array_like of bool, optional, condition
53
- casting: casting rule
54
- order: memory layout
55
- dtype: data type, output type
56
- subok: bool, whether to return subclass
57
58
Returns:
59
- ndarray: Tangent of each element
60
"""
61
62
def arcsin(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
63
"""
64
Inverse sine, element-wise.
65
66
Parameters:
67
- x: array_like, input array in [-1, 1]
68
- out: ndarray, optional, output array
69
- where: array_like of bool, optional, condition
70
- casting: casting rule
71
- order: memory layout
72
- dtype: data type, output type
73
- subok: bool, whether to return subclass
74
75
Returns:
76
- ndarray: Arcsine in radians [-pi/2, pi/2]
77
"""
78
79
def arccos(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
80
"""
81
Inverse cosine, element-wise.
82
83
Parameters:
84
- x: array_like, input array in [-1, 1]
85
- out: ndarray, optional, output array
86
- where: array_like of bool, optional, condition
87
- casting: casting rule
88
- order: memory layout
89
- dtype: data type, output type
90
- subok: bool, whether to return subclass
91
92
Returns:
93
- ndarray: Arccosine in radians [0, pi]
94
"""
95
96
def arctan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
97
"""
98
Inverse tangent, element-wise.
99
100
Parameters:
101
- x: array_like, input array
102
- out: ndarray, optional, output array
103
- where: array_like of bool, optional, condition
104
- casting: casting rule
105
- order: memory layout
106
- dtype: data type, output type
107
- subok: bool, whether to return subclass
108
109
Returns:
110
- ndarray: Arctangent in radians [-pi/2, pi/2]
111
"""
112
113
def arctan2(y, x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
114
"""
115
Element-wise arc tangent of y/x choosing quadrant correctly.
116
117
Parameters:
118
- y: array_like, Y-coordinates
119
- x: array_like, X-coordinates
120
- out: ndarray, optional, output array
121
- where: array_like of bool, optional, condition
122
- casting: casting rule
123
- order: memory layout
124
- dtype: data type, output type
125
- subok: bool, whether to return subclass
126
127
Returns:
128
- ndarray: Angle in radians [-pi, pi]
129
"""
130
```
131
132
### Hyperbolic Functions
133
134
Hyperbolic functions and their inverses for mathematical analysis.
135
136
```python { .api }
137
def sinh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
138
"""
139
Hyperbolic sine, element-wise.
140
141
Parameters:
142
- x: array_like, input array
143
- out: ndarray, optional, output array
144
- where: array_like of bool, optional, condition
145
- casting: casting rule
146
- order: memory layout
147
- dtype: data type, output type
148
- subok: bool, whether to return subclass
149
150
Returns:
151
- ndarray: Hyperbolic sine of each element
152
"""
153
154
def cosh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
155
"""
156
Hyperbolic cosine, element-wise.
157
158
Parameters:
159
- x: array_like, input array
160
- out: ndarray, optional, output array
161
- where: array_like of bool, optional, condition
162
- casting: casting rule
163
- order: memory layout
164
- dtype: data type, output type
165
- subok: bool, whether to return subclass
166
167
Returns:
168
- ndarray: Hyperbolic cosine of each element
169
"""
170
171
def tanh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
172
"""
173
Hyperbolic tangent, element-wise.
174
175
Parameters:
176
- x: array_like, input array
177
- out: ndarray, optional, output array
178
- where: array_like of bool, optional, condition
179
- casting: casting rule
180
- order: memory layout
181
- dtype: data type, output type
182
- subok: bool, whether to return subclass
183
184
Returns:
185
- ndarray: Hyperbolic tangent of each element
186
"""
187
188
def arcsinh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
189
"""
190
Inverse hyperbolic sine, element-wise.
191
192
Parameters:
193
- x: array_like, input array
194
- out: ndarray, optional, output array
195
- where: array_like of bool, optional, condition
196
- casting: casting rule
197
- order: memory layout
198
- dtype: data type, output type
199
- subok: bool, whether to return subclass
200
201
Returns:
202
- ndarray: Inverse hyperbolic sine of each element
203
"""
204
205
def arccosh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
206
"""
207
Inverse hyperbolic cosine, element-wise.
208
209
Parameters:
210
- x: array_like, input array (x >= 1)
211
- out: ndarray, optional, output array
212
- where: array_like of bool, optional, condition
213
- casting: casting rule
214
- order: memory layout
215
- dtype: data type, output type
216
- subok: bool, whether to return subclass
217
218
Returns:
219
- ndarray: Inverse hyperbolic cosine of each element
220
"""
221
222
def arctanh(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
223
"""
224
Inverse hyperbolic tangent, element-wise.
225
226
Parameters:
227
- x: array_like, input array (|x| < 1)
228
- out: ndarray, optional, output array
229
- where: array_like of bool, optional, condition
230
- casting: casting rule
231
- order: memory layout
232
- dtype: data type, output type
233
- subok: bool, whether to return subclass
234
235
Returns:
236
- ndarray: Inverse hyperbolic tangent of each element
237
"""
238
```
239
240
### Exponential and Logarithmic Functions
241
242
Exponential, logarithmic, and power functions for scientific computing.
243
244
```python { .api }
245
def exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
246
"""
247
Calculate exponential of all elements, element-wise.
248
249
Parameters:
250
- x: array_like, input array
251
- out: ndarray, optional, output array
252
- where: array_like of bool, optional, condition
253
- casting: casting rule
254
- order: memory layout
255
- dtype: data type, output type
256
- subok: bool, whether to return subclass
257
258
Returns:
259
- ndarray: Element-wise exponential of x
260
"""
261
262
def exp2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
263
"""
264
Calculate 2**x for all x in input array.
265
266
Parameters:
267
- x: array_like, input array
268
- out: ndarray, optional, output array
269
- where: array_like of bool, optional, condition
270
- casting: casting rule
271
- order: memory layout
272
- dtype: data type, output type
273
- subok: bool, whether to return subclass
274
275
Returns:
276
- ndarray: Element-wise 2**x
277
"""
278
279
def expm1(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
280
"""
281
Calculate exp(x) - 1 for all elements.
282
283
Parameters:
284
- x: array_like, input array
285
- out: ndarray, optional, output array
286
- where: array_like of bool, optional, condition
287
- casting: casting rule
288
- order: memory layout
289
- dtype: data type, output type
290
- subok: bool, whether to return subclass
291
292
Returns:
293
- ndarray: Element-wise exp(x) - 1
294
"""
295
296
def log(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
297
"""
298
Natural logarithm, element-wise.
299
300
Parameters:
301
- x: array_like, input array
302
- out: ndarray, optional, output array
303
- where: array_like of bool, optional, condition
304
- casting: casting rule
305
- order: memory layout
306
- dtype: data type, output type
307
- subok: bool, whether to return subclass
308
309
Returns:
310
- ndarray: Natural logarithm of x
311
"""
312
313
def log2(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
314
"""
315
Base-2 logarithm, element-wise.
316
317
Parameters:
318
- x: array_like, input array
319
- out: ndarray, optional, output array
320
- where: array_like of bool, optional, condition
321
- casting: casting rule
322
- order: memory layout
323
- dtype: data type, output type
324
- subok: bool, whether to return subclass
325
326
Returns:
327
- ndarray: Base-2 logarithm of x
328
"""
329
330
def log10(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
331
"""
332
Base-10 logarithm, element-wise.
333
334
Parameters:
335
- x: array_like, input array
336
- out: ndarray, optional, output array
337
- where: array_like of bool, optional, condition
338
- casting: casting rule
339
- order: memory layout
340
- dtype: data type, output type
341
- subok: bool, whether to return subclass
342
343
Returns:
344
- ndarray: Base-10 logarithm of x
345
"""
346
347
def log1p(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
348
"""
349
Return natural logarithm of one plus input array.
350
351
Parameters:
352
- x: array_like, input array
353
- out: ndarray, optional, output array
354
- where: array_like of bool, optional, condition
355
- casting: casting rule
356
- order: memory layout
357
- dtype: data type, output type
358
- subok: bool, whether to return subclass
359
360
Returns:
361
- ndarray: Natural logarithm of 1 + x
362
"""
363
```
364
365
### Arithmetic Operations
366
367
Basic arithmetic operations with broadcasting and advanced features.
368
369
```python { .api }
370
def add(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
371
"""
372
Add arguments element-wise.
373
374
Parameters:
375
- x1, x2: array_like, input arrays
376
- out: ndarray, optional, output array
377
- where: array_like of bool, optional, condition
378
- casting: casting rule
379
- order: memory layout
380
- dtype: data type, output type
381
- subok: bool, whether to return subclass
382
383
Returns:
384
- ndarray: Element-wise sum of x1 and x2
385
"""
386
387
def subtract(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
388
"""
389
Subtract arguments element-wise.
390
391
Parameters:
392
- x1, x2: array_like, input arrays
393
- out: ndarray, optional, output array
394
- where: array_like of bool, optional, condition
395
- casting: casting rule
396
- order: memory layout
397
- dtype: data type, output type
398
- subok: bool, whether to return subclass
399
400
Returns:
401
- ndarray: Element-wise difference x1 - x2
402
"""
403
404
def multiply(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
405
"""
406
Multiply arguments element-wise.
407
408
Parameters:
409
- x1, x2: array_like, input arrays
410
- out: ndarray, optional, output array
411
- where: array_like of bool, optional, condition
412
- casting: casting rule
413
- order: memory layout
414
- dtype: data type, output type
415
- subok: bool, whether to return subclass
416
417
Returns:
418
- ndarray: Element-wise product x1 * x2
419
"""
420
421
def divide(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
422
"""
423
Divide arguments element-wise.
424
425
Parameters:
426
- x1, x2: array_like, input arrays
427
- out: ndarray, optional, output array
428
- where: array_like of bool, optional, condition
429
- casting: casting rule
430
- order: memory layout
431
- dtype: data type, output type
432
- subok: bool, whether to return subclass
433
434
Returns:
435
- ndarray: Element-wise division x1 / x2
436
"""
437
438
def power(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
439
"""
440
First array elements raised to powers from second array.
441
442
Parameters:
443
- x1: array_like, base array
444
- x2: array_like, exponent array
445
- out: ndarray, optional, output array
446
- where: array_like of bool, optional, condition
447
- casting: casting rule
448
- order: memory layout
449
- dtype: data type, output type
450
- subok: bool, whether to return subclass
451
452
Returns:
453
- ndarray: Element-wise x1**x2
454
"""
455
456
def sqrt(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
457
"""
458
Return non-negative square-root of array, element-wise.
459
460
Parameters:
461
- x: array_like, input array
462
- out: ndarray, optional, output array
463
- where: array_like of bool, optional, condition
464
- casting: casting rule
465
- order: memory layout
466
- dtype: data type, output type
467
- subok: bool, whether to return subclass
468
469
Returns:
470
- ndarray: Element-wise square root of x
471
"""
472
473
def square(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
474
"""
475
Return element-wise square of input.
476
477
Parameters:
478
- x: array_like, input array
479
- out: ndarray, optional, output array
480
- where: array_like of bool, optional, condition
481
- casting: casting rule
482
- order: memory layout
483
- dtype: data type, output type
484
- subok: bool, whether to return subclass
485
486
Returns:
487
- ndarray: Element-wise x**2
488
"""
489
```
490
491
### Rounding Functions
492
493
Functions for rounding, ceiling, and floor operations.
494
495
```python { .api }
496
def around(a, decimals=0, out=None):
497
"""
498
Round to given number of decimals.
499
500
Parameters:
501
- a: array_like, input array
502
- decimals: int, number of decimals to round to
503
- out: ndarray, optional, output array
504
505
Returns:
506
- ndarray: Rounded array
507
"""
508
509
def round_(a, decimals=0, out=None):
510
"""
511
Round to given number of decimals.
512
513
Parameters:
514
- a: array_like, input array
515
- decimals: int, number of decimals to round to
516
- out: ndarray, optional, output array
517
518
Returns:
519
- ndarray: Rounded array
520
"""
521
522
def ceil(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
523
"""
524
Return ceiling of input, element-wise.
525
526
Parameters:
527
- x: array_like, input array
528
- out: ndarray, optional, output array
529
- where: array_like of bool, optional, condition
530
- casting: casting rule
531
- order: memory layout
532
- dtype: data type, output type
533
- subok: bool, whether to return subclass
534
535
Returns:
536
- ndarray: Ceiling of each element
537
"""
538
539
def floor(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
540
"""
541
Return floor of input, element-wise.
542
543
Parameters:
544
- x: array_like, input array
545
- out: ndarray, optional, output array
546
- where: array_like of bool, optional, condition
547
- casting: casting rule
548
- order: memory layout
549
- dtype: data type, output type
550
- subok: bool, whether to return subclass
551
552
Returns:
553
- ndarray: Floor of each element
554
"""
555
556
def trunc(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
557
"""
558
Return truncated value of input, element-wise.
559
560
Parameters:
561
- x: array_like, input array
562
- out: ndarray, optional, output array
563
- where: array_like of bool, optional, condition
564
- casting: casting rule
565
- order: memory layout
566
- dtype: data type, output type
567
- subok: bool, whether to return subclass
568
569
Returns:
570
- ndarray: Truncated value of each element
571
"""
572
```
573
574
### Sums, Products, and Differences
575
576
Aggregation functions and cumulative operations.
577
578
```python { .api }
579
def sum(a, axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True):
580
"""
581
Sum of array elements over given axis.
582
583
Parameters:
584
- a: array_like, input array
585
- axis: None or int or tuple of ints, axis to sum over
586
- dtype: data type, type of returned array
587
- out: ndarray, optional, output array
588
- keepdims: bool, keep reduced dimensions
589
- initial: scalar, starting value for sum
590
- where: array_like of bool, elements to include
591
592
Returns:
593
- ndarray: Sum of array elements
594
"""
595
596
def prod(a, axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True):
597
"""
598
Product of array elements over given axis.
599
600
Parameters:
601
- a: array_like, input array
602
- axis: None or int or tuple of ints, axis to multiply over
603
- dtype: data type, type of returned array
604
- out: ndarray, optional, output array
605
- keepdims: bool, keep reduced dimensions
606
- initial: scalar, starting value for product
607
- where: array_like of bool, elements to include
608
609
Returns:
610
- ndarray: Product of array elements
611
"""
612
613
def cumsum(a, axis=None, dtype=None, out=None):
614
"""
615
Cumulative sum of elements along axis.
616
617
Parameters:
618
- a: array_like, input array
619
- axis: int, optional, axis along which to compute
620
- dtype: data type, type of returned array
621
- out: ndarray, optional, output array
622
623
Returns:
624
- ndarray: Cumulative sum
625
"""
626
627
def cumprod(a, axis=None, dtype=None, out=None):
628
"""
629
Cumulative product of elements along axis.
630
631
Parameters:
632
- a: array_like, input array
633
- axis: int, optional, axis along which to compute
634
- dtype: data type, type of returned array
635
- out: ndarray, optional, output array
636
637
Returns:
638
- ndarray: Cumulative product
639
"""
640
641
def diff(a, n=1, axis=-1, prepend=None, append=None):
642
"""
643
Calculate n-th discrete difference along axis.
644
645
Parameters:
646
- a: array_like, input array
647
- n: int, number of times to difference
648
- axis: int, axis along which to difference
649
- prepend: array_like, values to prepend
650
- append: array_like, values to append
651
652
Returns:
653
- ndarray: n-th differences
654
"""
655
```
656
657
### Miscellaneous Functions
658
659
Additional mathematical utilities and special functions.
660
661
```python { .api }
662
def absolute(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
663
"""
664
Calculate absolute value element-wise.
665
666
Parameters:
667
- x: array_like, input array
668
- out: ndarray, optional, output array
669
- where: array_like of bool, optional, condition
670
- casting: casting rule
671
- order: memory layout
672
- dtype: data type, output type
673
- subok: bool, whether to return subclass
674
675
Returns:
676
- ndarray: Absolute value of each element
677
"""
678
679
def sign(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
680
"""
681
Returns element-wise indication of sign of number.
682
683
Parameters:
684
- x: array_like, input array
685
- out: ndarray, optional, output array
686
- where: array_like of bool, optional, condition
687
- casting: casting rule
688
- order: memory layout
689
- dtype: data type, output type
690
- subok: bool, whether to return subclass
691
692
Returns:
693
- ndarray: Sign of each element (-1, 0, 1)
694
"""
695
696
def maximum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
697
"""
698
Element-wise maximum of array elements.
699
700
Parameters:
701
- x1, x2: array_like, input arrays
702
- out: ndarray, optional, output array
703
- where: array_like of bool, optional, condition
704
- casting: casting rule
705
- order: memory layout
706
- dtype: data type, output type
707
- subok: bool, whether to return subclass
708
709
Returns:
710
- ndarray: Element-wise maximum
711
"""
712
713
def minimum(x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True):
714
"""
715
Element-wise minimum of array elements.
716
717
Parameters:
718
- x1, x2: array_like, input arrays
719
- out: ndarray, optional, output array
720
- where: array_like of bool, optional, condition
721
- casting: casting rule
722
- order: memory layout
723
- dtype: data type, output type
724
- subok: bool, whether to return subclass
725
726
Returns:
727
- ndarray: Element-wise minimum
728
"""
729
730
def clip(a, a_min, a_max, out=None, **kwargs):
731
"""
732
Clip values in array to specified range.
733
734
Parameters:
735
- a: array_like, input array
736
- a_min: scalar or array_like, minimum value
737
- a_max: scalar or array_like, maximum value
738
- out: ndarray, optional, output array
739
740
Returns:
741
- ndarray: Array with values clipped to [a_min, a_max]
742
"""
743
```
744
745
## Usage Examples
746
747
### Basic Mathematical Operations
748
749
```python
750
import cupy as cp
751
import numpy as np
752
753
# Create test data
754
x = cp.linspace(0, 2*cp.pi, 100)
755
y = cp.array([1, 4, 9, 16, 25])
756
757
# Trigonometric functions
758
sin_x = cp.sin(x)
759
cos_x = cp.cos(x)
760
tan_x = cp.tan(x)
761
762
# Exponential and logarithm
763
exp_y = cp.exp(y)
764
log_y = cp.log(y)
765
sqrt_y = cp.sqrt(y)
766
767
# Element-wise arithmetic
768
a = cp.array([1, 2, 3, 4])
769
b = cp.array([5, 6, 7, 8])
770
sum_ab = cp.add(a, b)
771
prod_ab = cp.multiply(a, b)
772
power_ab = cp.power(a, b)
773
```
774
775
### Advanced Mathematical Operations
776
777
```python
778
import cupy as cp
779
780
# Create complex operations
781
x = cp.linspace(-5, 5, 1000)
782
783
# Hyperbolic functions
784
sinh_x = cp.sinh(x)
785
cosh_x = cp.cosh(x)
786
tanh_x = cp.tanh(x)
787
788
# Special combinations
789
y = cp.exp(-x**2/2) / cp.sqrt(2*cp.pi) # Gaussian
790
z = cp.sin(x) * cp.exp(-x/10) # Damped sine
791
792
# Aggregation operations
793
data = cp.random.random((1000, 1000))
794
total_sum = cp.sum(data)
795
column_means = cp.mean(data, axis=0)
796
row_products = cp.prod(data, axis=1)
797
```
798
799
### Broadcasting and Universal Functions
800
801
```python
802
import cupy as cp
803
804
# Broadcasting with mathematical functions
805
a = cp.array([[1, 2, 3], [4, 5, 6]]) # Shape: (2, 3)
806
b = cp.array([10, 20, 30]) # Shape: (3,)
807
808
# Broadcasting in arithmetic operations
809
result = cp.add(a, b) # Broadcasting: (2,3) + (3,) -> (2,3)
810
power_result = cp.power(a, 2) # Element-wise squaring
811
812
# Universal function features
813
x = cp.array([1, 2, 3, 4, 5])
814
where_condition = x > 2
815
816
# Conditional operations with where parameter
817
conditional_sqrt = cp.sqrt(x, where=where_condition)
818
```