Repository navigation
Expand file tree
/
Copy path_elementwise_functions.py
More file actions
2291 lines (1800 loc) · 76.1 KB
/
Copy path_elementwise_functions.py
File metadata and controls
2291 lines (1800 loc) · 76.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Elementwise functions."""
from __future__ import annotations
from typing import TYPE_CHECKING, Protocol
from array_api._namespace import get_namespace
if TYPE_CHECKING:
from array_api._array import Array
__all__ = [
"abs",
"acos",
"acosh",
"add",
"asin",
"asinh",
"atan",
"atan2",
"atanh",
"bitwise_and",
"bitwise_left_shift",
"bitwise_invert",
"bitwise_or",
"bitwise_right_shift",
"bitwise_xor",
"ceil",
"cos",
"cosh",
"divide",
"equal",
"exp",
"expm1",
"floor",
"floor_divide",
"greater",
"greater_equal",
"isfinite",
"isinf",
"isnan",
"less",
"less_equal",
"log",
"log1p",
"log2",
"log10",
"logaddexp",
"logical_and",
"logical_not",
"logical_or",
"logical_xor",
"multiply",
"negative",
"not_equal",
"positive",
"pow",
"remainder",
"round",
"sign",
"sin",
"sinh",
"square",
"sqrt",
"subtract",
"tan",
"tanh",
"trunc",
]
def abs(x: Array, /) -> Array:
"""
Calculates the absolute value for each element ``x_i`` of the input array
``x`` (i.e., the element-wise result has the same magnitude as the
respective element in ``x`` but has positive sign).
.. note::
For signed integer data types, the absolute value of the minimum
representable integer is implementation-dependent.
**Special Cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``-0``, the result is ``+0``.
- If ``x_i`` is ``-infinity``, the result is ``+infinity``.
Parameters
----------
x: array
input array. Should have a numeric data type.
Returns
-------
out: array
an array containing the absolute value of each element in ``x``. The
returned array must have the same data type as ``x``.
"""
return get_namespace(x).abs(x)
def acos(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation of the principal
value of the inverse cosine, having domain ``[-1, +1]`` and codomain
``[+0, +π]``, for each element ``x_i`` of the input array ``x``. Each
element-wise result is expressed in radians.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is greater than ``1``, the result is ``NaN``.
- If ``x_i`` is less than ``-1``, the result is ``NaN``.
- If ``x_i`` is ``1``, the result is ``+0``.
Parameters
----------
x: array
input array. Should have a floating-point data type.
Returns
-------
out: array
an array containing the inverse cosine of each element in ``x``. The
returned array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x).acos(x)
def acosh(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to the inverse
hyperbolic cosine, having domain ``[+1, +infinity]`` and codomain ``[+0,
+infinity]``, for each element ``x_i`` of the input array ``x``.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is less than ``1``, the result is ``NaN``.
- If ``x_i`` is ``1``, the result is ``+0``.
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
Parameters
----------
x: array
input array whose elements each represent the area of a hyperbolic
sector. Should have a floating-point data type.
Returns
-------
out: array
an array containing the inverse hyperbolic cosine of each element in
``x``. The returned array must have a floating-point data type
determined by :ref:`type-promotion`.
"""
return get_namespace(x).acosh(x)
def add(x1: Array, x2: Array, /) -> Array:
"""
Calculates the sum for each element ``x1_i`` of the input array ``x1`` with
the respective element ``x2_i`` of the input array ``x2``.
**Special cases**
For floating-point operands,
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``-infinity``, the result is
``NaN``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is ``+infinity``, the result is
``NaN``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``+infinity``, the result is
``+infinity``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is ``-infinity``, the result is
``-infinity``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is a finite number, the result
is ``+infinity``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is a finite number, the result
is ``-infinity``.
- If ``x1_i`` is a finite number and ``x2_i`` is ``+infinity``, the result
is ``+infinity``.
- If ``x1_i`` is a finite number and ``x2_i`` is ``-infinity``, the result
is ``-infinity``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is ``-0``, the result is ``-0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is ``+0``, the result is ``+0``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is ``-0``, the result is ``+0``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is ``+0``, the result is ``+0``.
- If ``x1_i`` is either ``+0`` or ``-0`` and ``x2_i`` is a nonzero finite
number, the result is ``x2_i``.
- If ``x1_i`` is a nonzero finite number and ``x2_i`` is either ``+0`` or
``-0``, the result is ``x1_i``.
- If ``x1_i`` is a nonzero finite number and ``x2_i`` is ``-x1_i``, the
result is ``+0``.
- In the remaining cases, when neither ``infinity``, ``+0``, ``-0``, nor a
``NaN`` is involved, and the operands have the same mathematical sign or
have different magnitudes, the sum must be computed and rounded to the
nearest representable value according to IEEE 754-2019 and a supported
round mode. If the magnitude is too large to represent, the operation
overflows and the result is an `infinity` of appropriate mathematical
sign.
.. note::
Floating-point addition is a commutative operation, but not always
associative.
Parameters
----------
x1: array
first input array. Should have a numeric data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have a numeric data type.
Returns
-------
out: array
an array containing the element-wise sums. The returned array must have
a data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).add(x1, x2)
def asin(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation of the principal value
of the inverse sine, having domain ``[-1, +1]`` and codomain ``[-π/2,
+π/2]`` for each element ``x_i`` of the input array ``x``. Each element-wise
result is expressed in radians.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is greater than ``1``, the result is ``NaN``.
- If ``x_i`` is less than ``-1``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
Parameters
----------
x: array
input array. Should have a floating-point data type.
Returns
-------
out: array
an array containing the inverse sine of each element in ``x``. The
returned array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x).asin(x)
def asinh(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to the inverse
hyperbolic sine, having domain ``[-infinity, +infinity]`` and codomain
``[-infinity, +infinity]``, for each element ``x_i`` in the input array
``x``.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
- If ``x_i`` is ``-infinity``, the result is ``-infinity``.
Parameters
----------
x: array
input array whose elements each represent the area of a hyperbolic
sector. Should have a floating-point data type.
Returns
-------
out: array
an array containing the inverse hyperbolic sine of each element in
``x``. The returned array must have a floating-point data type
determined by :ref:`type-promotion`.
"""
return get_namespace(x).asinh(x)
def atan(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation of the principal value
of the inverse tangent, having domain ``[-infinity, +infinity]`` and
codomain ``[-π/2, +π/2]``, for each element ``x_i`` of the input array
``x``. Each element-wise result is expressed in radians.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
- If ``x_i`` is ``+infinity``, the result is an implementation-dependent
approximation to ``+π/2``.
- If ``x_i`` is ``-infinity``, the result is an implementation-dependent
approximation to ``-π/2``.
Parameters
----------
x: array
input array. Should have a floating-point data type.
Returns
-------
out: array
an array containing the inverse tangent of each element in ``x``. The
returned array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x).atan(x)
def atan2(x1: Array, x2: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation of the inverse tangent
of the quotient ``x1/x2``, having domain ``[-infinity, +infinity] x
[-infinity, +infinity]`` (where the ``x`` notation denotes the set of
ordered pairs of elements ``(x1_i, x2_i)``) and codomain ``[-π, +π]``, for
each pair of elements ``(x1_i, x2_i)`` of the input arrays ``x1`` and
``x2``, respectively. Each element-wise result is expressed in radians.
The mathematical signs of ``x1_i`` and ``x2_i`` determine the quadrant of
each element-wise result. The quadrant (i.e., branch) is chosen such that
each element-wise result is the signed angle in radians between the ray
ending at the origin and passing through the point ``(1,0)`` and the ray
ending at the origin and passing through the point ``(x2_i, x1_i)``.
.. note::
Note the role reversal: the "y-coordinate" is the first function
parameter; the "x-coordinate" is the second function parameter. The
parameter order is intentional and traditional for the two-argument
inverse tangent function where the y-coordinate argument is first and
the x-coordinate argument is second.
By IEEE 754 convention, the inverse tangent of the quotient ``x1/x2`` is
defined for ``x2_i`` equal to positive or negative zero and for either or
both of ``x1_i`` and ``x2_i`` equal to positive or negative ``infinity``.
**Special cases**
For floating-point operands,
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
- If ``x1_i`` is greater than ``0`` and ``x2_i`` is ``+0``, the result is an
implementation-dependent approximation to ``+π/2``.
- If ``x1_i`` is greater than ``0`` and ``x2_i`` is ``-0``, the result is an
implementation-dependent approximation to ``+π/2``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is greater than ``0``, the result is
``+0``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is ``+0``, the result is ``+0``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is ``-0``, the result is an
implementation-dependent approximation to ``+π``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is less than ``0``, the result is an
implementation-dependent approximation to ``+π``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is greater than ``0``, the result is
``-0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is ``+0``, the result is ``-0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is ``-0``, the result is an
implementation-dependent approximation to ``-π``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is less than ``0``, the result is an
implementation-dependent approximation to ``-π``.
- If ``x1_i`` is less than ``0`` and ``x2_i`` is ``+0``, the result is an
implementation-dependent approximation to ``-π/2``.
- If ``x1_i`` is less than ``0`` and ``x2_i`` is ``-0``, the result is an
implementation-dependent approximation to ``-π/2``.
- If ``x1_i`` is greater than ``0``, ``x1_i`` is a finite number, and
``x2_i`` is ``+infinity``, the result is ``+0``.
- If ``x1_i`` is greater than ``0``, ``x1_i`` is a finite number, and
``x2_i`` is ``-infinity``, the result is an implementation-dependent
approximation to ``+π``.
- If ``x1_i`` is less than ``0``, ``x1_i`` is a finite number, and ``x2_i``
is ``+infinity``, the result is ``-0``.
- If ``x1_i`` is less than ``0``, ``x1_i`` is a finite number, and ``x2_i``
is ``-infinity``, the result is an implementation-dependent approximation
to ``-π``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is finite, the result is an
implementation-dependent approximation to ``+π/2``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is finite, the result is an
implementation-dependent approximation to ``-π/2``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``+infinity``, the result is
an implementation-dependent approximation to ``+π/4``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is ``-infinity``, the result is
an implementation-dependent approximation to ``+3π/4``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is ``+infinity``, the result is
an implementation-dependent approximation to ``-π/4``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is ``-infinity``, the result is
an implementation-dependent approximation to ``-3π/4``.
Parameters
----------
x1: array
input array corresponding to the y-coordinates. Should have a
floating-point data type.
x2: array
input array corresponding to the x-coordinates. Must be compatible with
``x1`` (see :ref:`broadcasting`). Should have a floating-point data
type.
Returns
-------
out: array
an array containing the inverse tangent of the quotient ``x1/x2``. The
returned array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x1, x2).atan2(x1, x2)
def atanh(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to the inverse
hyperbolic tangent, having domain ``[-1, +1]`` and codomain ``[-infinity,
+infinity]``, for each element ``x_i`` of the input array ``x``.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is less than ``-1``, the result is ``NaN``.
- If ``x_i`` is greater than ``1``, the result is ``NaN``.
- If ``x_i`` is ``-1``, the result is ``-infinity``.
- If ``x_i`` is ``+1``, the result is ``+infinity``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
Parameters
----------
x: array
input array whose elements each represent the area of a hyperbolic
sector. Should have a floating-point data type.
Returns
-------
out: array
an array containing the inverse hyperbolic tangent of each element in
``x``. The returned array must have a floating-point data type
determined by :ref:`type-promotion`.
"""
return get_namespace(x).atanh(x)
def bitwise_and(x1: Array, x2: Array, /) -> Array:
"""
Computes the bitwise AND of the underlying binary representation of each
element ``x1_i`` of the input array ``x1`` with the respective element
``x2_i`` of the input array ``x2``.
Parameters
----------
x1: array
first input array. Should have an integer or boolean data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have an integer or boolean data type.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).bitwise_and(x1, x2)
def bitwise_left_shift(x1: Array, x2: Array, /) -> Array:
"""
Shifts the bits of each element ``x1_i`` of the input array ``x1`` to the
left by appending ``x2_i`` (i.e., the respective element in the input array
``x2``) zeros to the right of ``x1_i``.
Parameters
----------
x1: array
first input array. Should have an integer data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have an integer data type. Each element
must be greater than or equal to ``0``.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).bitwise_left_shift(x1, x2)
def bitwise_invert(x: Array, /) -> Array:
"""
Inverts (flips) each bit for each element ``x_i`` of the input array ``x``.
Parameters
----------
x: array
input array. Should have an integer or boolean data type.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have the same data type as ``x``.
"""
return get_namespace(x).bitwise_invert(x)
def bitwise_or(x1: Array, x2: Array, /) -> Array:
"""
Computes the bitwise OR of the underlying binary representation of each
element ``x1_i`` of the input array ``x1`` with the respective element
``x2_i`` of the input array ``x2``.
Parameters
----------
x1: array
first input array. Should have an integer or boolean data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have an integer or boolean data type.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).bitwise_or(x1, x2)
def bitwise_right_shift(x1: Array, x2: Array, /) -> Array:
"""
Shifts the bits of each element ``x1_i`` of the input array ``x1`` to the
right according to the respective element ``x2_i`` of the input array
``x2``.
.. note::
This operation must be an arithmetic shift (i.e., sign-propagating) and
thus equivalent to floor division by a power of two.
Parameters
----------
x1: array
first input array. Should have an integer data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have an integer data type. Each element
must be greater than or equal to ``0``.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).bitwise_right_shift(x1, x2)
def bitwise_xor(x1: Array, x2: Array, /) -> Array:
"""
Computes the bitwise XOR of the underlying binary representation of each
element ``x1_i`` of the input array ``x1`` with the respective element
``x2_i`` of the input array ``x2``.
Parameters
----------
x1: array
first input array. Should have an integer or boolean data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have an integer or boolean data type.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).bitwise_xor(x1, x2)
def ceil(x: Array, /) -> Array:
"""
Rounds each element ``x_i`` of the input array ``x`` to the smallest (i.e.,
closest to ``-infinity``) integer-valued number that is not less than
``x_i``.
**Special cases**
- If ``x_i`` is already integer-valued, the result is ``x_i``.
For floating-point operands,
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
- If ``x_i`` is ``-infinity``, the result is ``-infinity``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
- If ``x_i`` is ``NaN``, the result is ``NaN``.
Parameters
----------
x: array
input array. Should have a numeric data type.
Returns
-------
out: array
an array containing the rounded result for each element in ``x``. The
returned array must have the same data type as ``x``.
"""
return get_namespace(x).ceil(x)
def cos(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to the cosine, having
domain ``(-infinity, +infinity)`` and codomain ``[-1, +1]``, for each
element ``x_i`` of the input array ``x``. Each element ``x_i`` is assumed to
be expressed in radians.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``1``.
- If ``x_i`` is ``-0``, the result is ``1``.
- If ``x_i`` is ``+infinity``, the result is ``NaN``.
- If ``x_i`` is ``-infinity``, the result is ``NaN``.
Parameters
----------
x: array
input array whose elements are each expressed in radians. Should have a
floating-point data type.
Returns
-------
out: array
an array containing the cosine of each element in ``x``. The returned
array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x).cos(x)
def cosh(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to the hyperbolic
cosine, having domain ``[-infinity, +infinity]`` and codomain ``[-infinity,
+infinity]``, for each element ``x_i`` in the input array ``x``.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``1``.
- If ``x_i`` is ``-0``, the result is ``1``.
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
- If ``x_i`` is ``-infinity``, the result is ``+infinity``.
Parameters
----------
x: array
input array whose elements each represent a hyperbolic angle. Should
have a floating-point data type.
Returns
-------
out: array
an array containing the hyperbolic cosine of each element in ``x``. The
returned array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x).cosh(x)
def divide(x1: Array, x2: Array, /) -> Array:
"""
Calculates the division for each element ``x1_i`` of the input array ``x1``
with the respective element ``x2_i`` of the input array ``x2``.
.. note::
If one or both of the input arrays have integer data types, the result
is implementation-dependent, as type promotion between data type "kinds"
(e.g., integer versus floating-point) is unspecified.
Specification-compliant libraries may choose to raise an error or return
an array containing the element-wise results. If an array is returned,
the array must have a floating-point data type.
**Special cases**
For floating-point operands,
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
- If ``x1_i`` is either ``+infinity`` or ``-infinity`` and ``x2_i`` is
either ``+infinity`` or ``-infinity``, the result is ``NaN``.
- If ``x1_i`` is either ``+0`` or ``-0`` and ``x2_i`` is either ``+0`` or
``-0``, the result is ``NaN``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is greater than ``0``, the result is
``+0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is greater than ``0``, the result is
``-0``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is less than ``0``, the result is
``-0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is less than ``0``, the result is
``+0``.
- If ``x1_i`` is greater than ``0`` and ``x2_i`` is ``+0``, the result is
``+infinity``.
- If ``x1_i`` is greater than ``0`` and ``x2_i`` is ``-0``, the result is
``-infinity``.
- If ``x1_i`` is less than ``0`` and ``x2_i`` is ``+0``, the result is
``-infinity``.
- If ``x1_i`` is less than ``0`` and ``x2_i`` is ``-0``, the result is
``+infinity``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is a positive (i.e., greater
than ``0``) finite number, the result is ``+infinity``.
- If ``x1_i`` is ``+infinity`` and ``x2_i`` is a negative (i.e., less than
``0``) finite number, the result is ``-infinity``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is a positive (i.e., greater
than ``0``) finite number, the result is ``-infinity``.
- If ``x1_i`` is ``-infinity`` and ``x2_i`` is a negative (i.e., less than
``0``) finite number, the result is ``+infinity``.
- If ``x1_i`` is a positive (i.e., greater than ``0``) finite number and
``x2_i`` is ``+infinity``, the result is ``+0``.
- If ``x1_i`` is a positive (i.e., greater than ``0``) finite number and
``x2_i`` is ``-infinity``, the result is ``-0``.
- If ``x1_i`` is a negative (i.e., less than ``0``) finite number and
``x2_i`` is ``+infinity``, the result is ``-0``.
- If ``x1_i`` is a negative (i.e., less than ``0``) finite number and
``x2_i`` is ``-infinity``, the result is ``+0``.
- If ``x1_i`` and ``x2_i`` have the same mathematical sign and are both
nonzero finite numbers, the result has a positive mathematical sign.
- If ``x1_i`` and ``x2_i`` have different mathematical signs and are both
nonzero finite numbers, the result has a negative mathematical sign.
- In the remaining cases, where neither ``-infinity``, ``+0``, ``-0``, nor
``NaN`` is involved, the quotient must be computed and rounded to the
nearest representable value according to IEEE 754-2019 and a supported
rounding mode. If the magnitude is too large to represent, the operation
overflows and the result is an ``infinity`` of appropriate mathematical
sign. If the magnitude is too small to represent, the operation underflows
and the result is a zero of appropriate mathematical sign.
Parameters
----------
x1: array
dividend input array. Should have a numeric data type.
x2: array
divisor input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). Should have a numeric data type.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a floating-point data type determined by :ref:`type-promotion`.
"""
return get_namespace(x1, x2).divide(x1, x2)
def equal(x1: Array, x2: Array, /) -> Array:
"""
Computes the truth value of ``x1_i == x2_i`` for each element ``x1_i`` of
the input array ``x1`` with the respective element ``x2_i`` of the input
array ``x2``.
Parameters
----------
x1: array
first input array. May have any data type.
x2: array
second input array. Must be compatible with ``x1`` (see
:ref:`broadcasting`). May have any data type.
Returns
-------
out: array
an array containing the element-wise results. The returned array must
have a data type of ``bool``.
"""
return get_namespace(x1, x2).equal(x1, x2)
def exp(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to the exponential
function, having domain ``[-infinity, +infinity]`` and codomain ``[+0,
+infinity]``, for each element ``x_i`` of the input array ``x`` (``e``
raised to the power of ``x_i``, where ``e`` is the base of the natural
logarithm).
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``1``.
- If ``x_i`` is ``-0``, the result is ``1``.
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
- If ``x_i`` is ``-infinity``, the result is ``+0``.
Parameters
----------
x: array
input array. Should have a floating-point data type.
Returns
-------
out: array
an array containing the evaluated exponential function result for each
element in ``x``. The returned array must have a floating-point data
type determined by :ref:`type-promotion`.
"""
return get_namespace(x).exp(x)
def expm1(x: Array, /) -> Array:
"""
Calculates an implementation-dependent approximation to ``exp(x)-1``, having
domain ``[-infinity, +infinity]`` and codomain ``[-1, +infinity]``, for each
element ``x_i`` of the input array ``x``.
.. note::
The purpose of this function is to calculate ``exp(x)-1.0`` more
accurately when `x` is close to zero. Accordingly, conforming
implementations should avoid implementing this function as simply
``exp(x)-1.0``. See FDLIBM, or some other IEEE 754-2019 compliant
mathematical library, for a potential reference implementation.
**Special cases**
For floating-point operands,
- If ``x_i`` is ``NaN``, the result is ``NaN``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
- If ``x_i`` is ``-infinity``, the result is ``-1``.
Parameters
----------
x: array
input array. Should have a floating-point data type.
Returns
-------
out: array
an array containing the evaluated result for each element in ``x``. The
returned array must have a floating-point data type determined by
:ref:`type-promotion`.
"""
return get_namespace(x).expm1(x)
def floor(x: Array, /) -> Array:
"""
Rounds each element ``x_i`` of the input array ``x`` to the greatest (i.e.,
closest to ``+infinity``) integer-valued number that is not greater than
``x_i``.
**Special cases**
- If ``x_i`` is already integer-valued, the result is ``x_i``.
For floating-point operands,
- If ``x_i`` is ``+infinity``, the result is ``+infinity``.
- If ``x_i`` is ``-infinity``, the result is ``-infinity``.
- If ``x_i`` is ``+0``, the result is ``+0``.
- If ``x_i`` is ``-0``, the result is ``-0``.
- If ``x_i`` is ``NaN``, the result is ``NaN``.
Parameters
----------
x: array
input array. Should have a numeric data type.
Returns
-------
out: array
an array containing the rounded result for each element in ``x``. The
returned array must have the same data type as ``x``.
"""
return get_namespace(x).floor(x)
def floor_divide(x1: Array, x2: Array, /) -> Array:
r"""
Rounds the result of dividing each element ``x1_i`` of the input array
``x1`` by the respective element ``x2_i`` of the input array ``x2`` to the
greatest (i.e., closest to `+infinity`) integer-value number that is not
greater than the division result.
.. note::
For input arrays which promote to an integer data type, the result of
division by zero is unspecified and thus implementation-defined.
**Special cases**
.. note::
Floor division was introduced in Python via `PEP 238
<https://www.python.org/dev/peps/pep-0238/>`_ with the goal to
disambiguate "true division" (i.e., computing an approximation to the
mathematical operation of division) from "floor division" (i.e.,
rounding the result of division toward negative infinity). The former
was computed when one of the operands was a ``float``, while the latter
was computed when both operands were ``int``\s. Overloading the ``/``
operator to support both behaviors led to subtle numerical bugs when
integers are possible, but not expected.
To resolve this ambiguity, ``/`` was designated for true division, and
``//`` was designated for floor division. Semantically, floor division
was `defined
<https://www.python.org/dev/peps/pep-0238/#semantics-of-floor-division>`_
as equivalent to ``a // b == floor(a/b)``; however, special
floating-point cases were left ill-defined.
Accordingly, floor division is not implemented consistently across array
libraries for some of the special cases documented below. Namely, when
one of the operands is ``infinity``, libraries may diverge with some
choosing to strictly follow ``floor(a/b)`` and others choosing to pair
``//`` with ``%`` according to the relation ``b = a % b + b * (a //
b)``. The special cases leading to divergent behavior are documented
below.
This specification prefers floor division to match ``floor(divide(x1,
x2))`` in order to avoid surprising and unexpected results; however,
array libraries may choose to more strictly follow Python behavior.
For floating-point operands,
- If either ``x1_i`` or ``x2_i`` is ``NaN``, the result is ``NaN``.
- If ``x1_i`` is either ``+infinity`` or ``-infinity`` and ``x2_i`` is
either ``+infinity`` or ``-infinity``, the result is ``NaN``.
- If ``x1_i`` is either ``+0`` or ``-0`` and ``x2_i`` is either ``+0`` or
``-0``, the result is ``NaN``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is greater than ``0``, the result is
``+0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is greater than ``0``, the result is
``-0``.
- If ``x1_i`` is ``+0`` and ``x2_i`` is less than ``0``, the result is
``-0``.
- If ``x1_i`` is ``-0`` and ``x2_i`` is less than ``0``, the result is
``+0``.
- If ``x1_i`` is greater than ``0`` and ``x2_i`` is ``+0``, the result is
``+infinity``.
- If ``x1_i`` is greater than ``0`` and ``x2_i`` is ``-0``, the result is
``-infinity``.
- If ``x1_i`` is less than ``0`` and ``x2_i`` is ``+0``, the result is
``-infinity``.