-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdpnp_iface_logic.py
More file actions
2091 lines (1658 loc) · 57.7 KB
/
dpnp_iface_logic.py
File metadata and controls
2091 lines (1658 loc) · 57.7 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
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (c) 2016, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
"""
Interface of the Logic part of the DPNP
Notes
-----
This module is a face or public interface file for the library
it contains:
- Interface functions
- documentation for the functions
- The functions parameters check
"""
# pylint: disable=protected-access
# pylint: disable=duplicate-code
# pylint: disable=no-name-in-module
import dpctl.tensor as dpt
import dpctl.tensor._tensor_elementwise_impl as ti
import dpctl.utils as dpu
import numpy
import dpnp
import dpnp.backend.extensions.ufunc._ufunc_impl as ufi
from dpnp.dpnp_algo.dpnp_elementwise_common import DPNPBinaryFunc, DPNPUnaryFunc
from .dpnp_utils import get_usm_allocations
__all__ = [
"all",
"allclose",
"any",
"array_equal",
"array_equiv",
"equal",
"greater",
"greater_equal",
"isclose",
"iscomplex",
"iscomplexobj",
"isfinite",
"isfortran",
"isinf",
"isnan",
"isneginf",
"isposinf",
"isreal",
"isrealobj",
"isscalar",
"less",
"less_equal",
"logical_and",
"logical_not",
"logical_or",
"logical_xor",
"not_equal",
]
def _isclose_scalar_tol(a, b, rtol, atol, equal_nan):
"""
Specialized implementation of dpnp.isclose() for scalar rtol and atol
using a dedicated SYCL kernel.
"""
dt = dpnp.result_type(a, b, 1.0)
if dpnp.isscalar(a):
usm_type = b.usm_type
exec_q = b.sycl_queue
a = dpnp.array(
a,
dt,
usm_type=usm_type,
sycl_queue=exec_q,
)
elif dpnp.isscalar(b):
usm_type = a.usm_type
exec_q = a.sycl_queue
b = dpnp.array(
b,
dt,
usm_type=usm_type,
sycl_queue=exec_q,
)
else:
usm_type, exec_q = get_usm_allocations([a, b])
a = dpnp.astype(a, dt, casting="same_kind", copy=False)
b = dpnp.astype(b, dt, casting="same_kind", copy=False)
# Convert complex rtol/atol to their real parts
# to avoid pybind11 cast errors and match NumPy behavior
if isinstance(rtol, complex):
rtol = rtol.real
if isinstance(atol, complex):
atol = atol.real
# Convert equal_nan to bool to avoid pybind11 cast errors
# and match NumPy behavior
if not isinstance(equal_nan, bool):
equal_nan = bool(equal_nan)
a, b = dpnp.broadcast_arrays(a, b)
output = dpnp.empty(
a.shape, dtype=dpnp.bool, sycl_queue=exec_q, usm_type=usm_type
)
_manager = dpu.SequentialOrderManager[exec_q]
ht_ev, isclose_ev = ufi._isclose_scalar(
a.get_array(),
b.get_array(),
rtol,
atol,
equal_nan,
output.get_array(),
exec_q,
depends=_manager.submitted_events,
)
_manager.add_event_pair(ht_ev, isclose_ev)
return output
def all(a, /, axis=None, out=None, keepdims=False, *, where=True):
"""
Test whether all array elements along a given axis evaluate to ``True``.
For full documentation refer to :obj:`numpy.all`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray}
Input array.
axis : {None, int, tuple of ints}, optional
Axis or axes along which a logical AND reduction is performed.
The default is to perform a logical AND over all the dimensions
of the input array.`axis` may be negative, in which case it counts
from the last to the first axis.
Default: ``None``.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Alternative output array in which to place the result. It must have
the same shape as the expected output but the type (of the returned
values) will be cast if necessary.
Default: ``None``.
keepdims : {None, bool}, optional
If ``True``, the reduced axes (dimensions) are included in the result
as singleton dimensions, so that the returned array remains
compatible with the input array according to Array Broadcasting
rules. Otherwise, if ``False``, the reduced axes are not included in
the returned array.
Default: ``False``.
Returns
-------
out : dpnp.ndarray of bool dtype
An array containing the results of the logical AND reduction is
returned unless `out` is specified. Otherwise, a reference to `out` is
returned. The result has the same shape as `a` if `axis` is not ``None``
or `a` is a 0-d array.
Limitations
-----------
Parameters `where` is only supported with its default value.
Otherwise ``NotImplementedError`` exception will be raised.
See Also
--------
:obj:`dpnp.ndarray.all` : Equivalent method.
:obj:`dpnp.any` : Test whether any element along a given axis evaluates
to ``True``.
Notes
-----
Not a Number (NaN), positive infinity and negative infinity
evaluate to ``True`` because these are not equal to zero.
Examples
--------
>>> import dpnp as np
>>> x = np.array([[True, False], [True, True]])
>>> np.all(x)
array(False)
>>> np.all(x, axis=0)
array([ True, False])
>>> x2 = np.array([-1, 4, 5])
>>> np.all(x2)
array(True)
>>> x3 = np.array([1.0, np.nan])
>>> np.all(x3)
array(True)
>>> o = np.array(False)
>>> z = np.all(x2, out=o)
>>> z, o
(array(True), array(True))
>>> # Check now that `z` is a reference to `o`
>>> z is o
True
>>> id(z), id(o) # identity of `z` and `o`
(139884456208480, 139884456208480) # may vary
"""
dpnp.check_limitations(where=where)
usm_a = dpnp.get_usm_ndarray(a)
usm_res = dpt.all(usm_a, axis=axis, keepdims=keepdims)
# TODO: temporary solution until dpt.all supports out parameter
return dpnp.get_result_array(usm_res, out)
def allclose(a, b, rtol=1.0e-5, atol=1.0e-8, equal_nan=False):
"""
Returns ``True`` if two arrays are element-wise equal within a tolerance.
The tolerance values are positive, typically very small numbers. The
relative difference (`rtol` * abs(`b`)) and the absolute difference `atol`
are added together to compare against the absolute difference between `a`
and `b`.
``NaNs`` are treated as equal if they are in the same place and if
``equal_nan=True``. ``Infs`` are treated as equal if they are in the same
place and of the same sign in both arrays.
For full documentation refer to :obj:`numpy.allclose`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray, scalar}
First input array, expected to have a numeric data type.
b : {dpnp.ndarray, usm_ndarray, scalar}
Second input array, also expected to have a numeric data type.
rtol : {dpnp.ndarray, usm_ndarray, scalar}, optional
The relative tolerance parameter.
Default: ``1e-05``.
atol : {dpnp.ndarray, usm_ndarray, scalar}, optional
The absolute tolerance parameter.
Default: ``1e-08``.
equal_nan : bool
Whether to compare ``NaNs`` as equal. If ``True``, ``NaNs`` in `a` will
be considered equal to ``NaNs`` in `b` in the output array.
Default: ``False``.
Returns
-------
out : dpnp.ndarray of bool dtype
A 0-d array with ``True`` value if the two arrays are equal within the
given tolerance; with ``False`` otherwise.
See Also
--------
:obj:`dpnp.isclose` : Test whether two arrays are element-wise equal.
:obj:`dpnp.all` : Test whether all elements evaluate to True.
:obj:`dpnp.any` : Test whether any element evaluates to True.
:obj:`dpnp.equal` : Return (x1 == x2) element-wise.
Notes
-----
At least one of `x1` or `x2` must be an array.
The comparison of `a` and `b` uses standard broadcasting, which means that
`a` and `b` need not have the same shape in order for
``dpnp.allclose(a, b)`` to evaluate to ``True``.
The same is true for :obj:`dpnp.equal` but not :obj:`dpnp.array_equal`.
Examples
--------
>>> import dpnp as np
>>> a = np.array([1e10, 1e-7])
>>> b = np.array([1.00001e10, 1e-8])
>>> np.allclose(a, b)
array(False)
>>> a = np.array([1.0, np.nan])
>>> b = np.array([1.0, np.nan])
>>> np.allclose(a, b)
array(False)
>>> np.allclose(a, b, equal_nan=True)
array(True)
>>> a = np.array([1.0, np.inf])
>>> b = np.array([1.0, np.inf])
>>> np.allclose(a, b)
array(True)
"""
return all(isclose(a, b, rtol=rtol, atol=atol, equal_nan=equal_nan))
def any(a, /, axis=None, out=None, keepdims=False, *, where=True):
"""
Test whether any array element along a given axis evaluates to ``True``.
For full documentation refer to :obj:`numpy.any`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray}
Input array.
axis : {None, int, tuple of ints}, optional
Axis or axes along which a logical OR reduction is performed.
The default is to perform a logical OR over all the dimensions
of the input array.`axis` may be negative, in which case it counts
from the last to the first axis.
Default: ``None``.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Alternative output array in which to place the result. It must have
the same shape as the expected output but the type (of the returned
values) will be cast if necessary.
Default: ``None``.
keepdims : {None, bool}, optional
If ``True``, the reduced axes (dimensions) are included in the result
as singleton dimensions, so that the returned array remains
compatible with the input array according to Array Broadcasting
rules. Otherwise, if ``False``, the reduced axes are not included in
the returned array.
Default: ``False``.
Returns
-------
out : dpnp.ndarray of bool dtype
An array containing the results of the logical OR reduction is returned
unless `out` is specified. Otherwise, a reference to `out` is returned.
The result has the same shape as `a` if `axis` is not ``None`` or `a`
is a 0-d array.
Limitations
-----------
Parameters `where` is only supported with its default value.
Otherwise ``NotImplementedError`` exception will be raised.
See Also
--------
:obj:`dpnp.ndarray.any` : Equivalent method.
:obj:`dpnp.all` : Test whether all elements along a given axis evaluate
to ``True``.
Notes
-----
Not a Number (NaN), positive infinity and negative infinity evaluate
to ``True`` because these are not equal to zero.
Examples
--------
>>> import dpnp as np
>>> x = np.array([[True, False], [True, True]])
>>> np.any(x)
array(True)
>>> np.any(x, axis=0)
array([ True, True])
>>> x2 = np.array([-1, 0, 5])
>>> np.any(x2)
array(True)
>>> x3 = np.array([1.0, np.nan])
>>> np.any(x3)
array(True)
>>> o = np.array(False)
>>> z = np.any(x2, out=o)
>>> z, o
(array(True), array(True))
>>> # Check now that `z` is a reference to `o`
>>> z is o
True
>>> id(z), id(o) # identity of `z` and `o`
>>> (140053638309840, 140053638309840) # may vary
"""
dpnp.check_limitations(where=where)
usm_a = dpnp.get_usm_ndarray(a)
usm_res = dpt.any(usm_a, axis=axis, keepdims=keepdims)
# TODO: temporary solution until dpt.any supports out parameter
return dpnp.get_result_array(usm_res, out)
def array_equal(a1, a2, equal_nan=False):
"""
``True`` if two arrays have the same shape and elements, ``False``
otherwise.
For full documentation refer to :obj:`numpy.array_equal`.
Parameters
----------
a1 : {dpnp.ndarray, usm_ndarray, scalar}
First input array.
a2 : {dpnp.ndarray, usm_ndarray, scalar}
Second input array.
equal_nan : bool, optional
Whether to compare ``NaNs`` as equal. If the dtype of `a1` and `a2` is
complex, values will be considered equal if either the real or the
imaginary component of a given value is ``NaN``.
Default: ``False``.
Returns
-------
out : dpnp.ndarray of bool dtype
A 0-d array with ``True`` value if the arrays are equal.
See Also
--------
:obj:`dpnp.allclose`: Returns ``True`` if two arrays are element-wise equal
within a tolerance.
:obj:`dpnp.array_equiv`: Returns ``True`` if input arrays are shape
consistent and all elements equal.
Notes
-----
At least one of `x1` or `x2` must be an array.
Examples
--------
>>> import dpnp as np
>>> a = np.array([1, 2])
>>> b = np.array([1, 2])
>>> np.array_equal(a, b)
array(True)
>>> b = np.array([1, 2, 3])
>>> np.array_equal(a, b)
array(False)
>>> b = np.array([1, 4])
>>> np.array_equal(a, b)
array(False)
>>> a = np.array([1, np.nan])
>>> np.array_equal(a, a)
array(False)
>>> np.array_equal(a, a, equal_nan=True)
array(True)
When ``equal_nan`` is ``True``, complex values with NaN components are
considered equal if either the real *or* the imaginary components are
``NaNs``.
>>> a = np.array([1 + 1j])
>>> b = a.copy()
>>> a.real = np.nan
>>> b.imag = np.nan
>>> np.array_equal(a, b, equal_nan=True)
array(True)
"""
dpnp.check_supported_arrays_type(a1, a2, scalar_type=True)
if dpnp.isscalar(a1):
usm_type_alloc = a2.usm_type
sycl_queue_alloc = a2.sycl_queue
a1 = dpnp.array(
a1,
dtype=dpnp.result_type(a1, a2),
usm_type=usm_type_alloc,
sycl_queue=sycl_queue_alloc,
)
elif dpnp.isscalar(a2):
usm_type_alloc = a1.usm_type
sycl_queue_alloc = a1.sycl_queue
a2 = dpnp.array(
a2,
dtype=dpnp.result_type(a1, a2),
usm_type=usm_type_alloc,
sycl_queue=sycl_queue_alloc,
)
else:
usm_type_alloc, sycl_queue_alloc = get_usm_allocations([a1, a2])
if a1.shape != a2.shape:
return dpnp.array(
False, usm_type=usm_type_alloc, sycl_queue=sycl_queue_alloc
)
if not equal_nan:
return (a1 == a2).all()
if a1 is a2:
# NaN will compare equal so an array will compare equal to itself
return dpnp.array(
True, usm_type=usm_type_alloc, sycl_queue=sycl_queue_alloc
)
if not (
dpnp.issubdtype(a1, dpnp.inexact) or dpnp.issubdtype(a2, dpnp.inexact)
):
return (a1 == a2).all()
# Handling NaN values if equal_nan is True
a1nan, a2nan = isnan(a1), isnan(a2)
# NaNs occur at different locations
if not (a1nan == a2nan).all():
return dpnp.array(
False, usm_type=usm_type_alloc, sycl_queue=sycl_queue_alloc
)
# Shapes of a1, a2 and masks are guaranteed to be consistent by this point
return (a1[~a1nan] == a2[~a1nan]).all()
def array_equiv(a1, a2):
"""
Returns ``True`` if input arrays are shape consistent and all elements
equal.
Shape consistent means they are either the same shape, or one input array
can be broadcasted to create the same shape as the other one.
For full documentation refer to :obj:`numpy.array_equiv`.
Parameters
----------
a1 : {dpnp.ndarray, usm_ndarray, scalar}
First input array.
a2 : {dpnp.ndarray, usm_ndarray, scalar}
Second input array.
Returns
-------
out : dpnp.ndarray of bool dtype
A 0-d array with ``True`` value if the arrays are equivalent, ``False``
otherwise.
Notes
-----
At least one of `x1` or `x2` must be an array.
Examples
--------
>>> import dpnp as np
>>> a = np.array([1, 2])
>>> b = np.array([1, 2])
>>> c = np.array([1, 3])
>>> np.array_equiv(a, b)
array(True)
>>> np.array_equiv(a, c)
array(False)
Showing the shape equivalence:
>>> b = np.array([[1, 2], [1, 2]])
>>> c = np.array([[1, 2, 1, 2], [1, 2, 1, 2]])
>>> np.array_equiv(a, b)
array(True)
>>> np.array_equiv(a, c)
array(False)
>>> b = np.array([[1, 2], [1, 3]])
>>> np.array_equiv(a, b)
array(False)
"""
dpnp.check_supported_arrays_type(a1, a2, scalar_type=True)
if not dpnp.isscalar(a1) and not dpnp.isscalar(a2):
usm_type_alloc, sycl_queue_alloc = get_usm_allocations([a1, a2])
try:
dpnp.broadcast_arrays(a1, a2)
except ValueError:
return dpnp.array(
False, usm_type=usm_type_alloc, sycl_queue=sycl_queue_alloc
)
return (a1 == a2).all()
_EQUAL_DOCSTRING = """
Calculates equality test results for each element :math:`x1_i` of the input
array `x1` with the respective element :math:`x2_i` of the input array `x2`.
For full documentation refer to :obj:`numpy.equal`.
Parameters
----------
x1 : {dpnp.ndarray, usm_ndarray, scalar}
First input array, may have any data type.
x2 : {dpnp.ndarray, usm_ndarray, scalar}
Second input array, also may have any data type.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Output array to populate.
Array have the correct shape and the expected data type.
Default: ``None``.
order : {None, "C", "F", "A", "K"}, optional
Memory layout of the newly output array, if parameter `out` is ``None``.
Default: ``"K"``.
Returns
-------
out : dpnp.ndarray of bool dtype
An array containing the result of element-wise equality comparison.
Limitations
-----------
Parameters `where` and `subok` are supported with their default values.
Otherwise ``NotImplementedError`` exception will be raised.
See Also
--------
:obj:`dpnp.not_equal` : Return (x1 != x2) element-wise.
:obj:`dpnp.greater_equal` : Return the truth value of (x1 >= x2) element-wise.
:obj:`dpnp.less_equal` : Return the truth value of (x1 =< x2) element-wise.
:obj:`dpnp.greater` : Return the truth value of (x1 > x2) element-wise.
:obj:`dpnp.less` : Return the truth value of (x1 < x2) element-wise.
Notes
-----
At least one of `x1` or `x2` must be an array.
If ``x1.shape != x2.shape``, they must be broadcastable to a common shape
(which becomes the shape of the output).
Examples
--------
>>> import dpnp as np
>>> x1 = np.array([0, 1, 3])
>>> x2 = np.arange(3)
>>> np.equal(x1, x2)
array([ True, True, False])
What is compared are values, not types. So an int (1) and an array of
length one can evaluate as True:
>>> np.equal(1, np.ones(1))
array([ True])
The ``==`` operator can be used as a shorthand for ``equal`` on
:class:`dpnp.ndarray`.
>>> a = np.array([2, 4, 6])
>>> b = np.array([2, 4, 2])
>>> a == b
array([ True, True, False])
"""
equal = DPNPBinaryFunc(
"equal",
ti._equal_result_type,
ti._equal,
_EQUAL_DOCSTRING,
)
_GREATER_DOCSTRING = """
Computes the greater-than test results for each element :math:`x1_i` of the
input array `x1` with the respective element :math:`x2_i` of the input array
`x2`.
For full documentation refer to :obj:`numpy.greater`.
Parameters
----------
x1 : {dpnp.ndarray, usm_ndarray, scalar}
First input array, may have any data type.
x2 : {dpnp.ndarray, usm_ndarray, scalar}
Second input array, also may have any data type.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Output array to populate.
Array must have the correct shape and the expected data type.
Default: ``None``.
order : {None, "C", "F", "A", "K"}, optional
Memory layout of the newly output array, if parameter `out` is ``None``.
Default: ``"K"``.
Returns
-------
out : dpnp.ndarray of bool dtype
An array containing the result of element-wise greater-than comparison.
Limitations
-----------
Parameters `where` and `subok` are supported with their default values.
Otherwise ``NotImplementedError`` exception will be raised.
See Also
--------
:obj:`dpnp.greater_equal` : Return the truth value of (x1 >= x2) element-wise.
:obj:`dpnp.less` : Return the truth value of (x1 < x2) element-wise.
:obj:`dpnp.less_equal` : Return the truth value of (x1 =< x2) element-wise.
:obj:`dpnp.equal` : Return (x1 == x2) element-wise.
:obj:`dpnp.not_equal` : Return (x1 != x2) element-wise.
Notes
-----
At least one of `x1` or `x2` must be an array.
If ``x1.shape != x2.shape``, they must be broadcastable to a common shape
(which becomes the shape of the output).
Examples
--------
>>> import dpnp as np
>>> x1 = np.array([4, 2])
>>> x2 = np.array([2, 2])
>>> np.greater(x1, x2)
array([ True, False])
The ``>`` operator can be used as a shorthand for ``greater`` on
:class:`dpnp.ndarray`.
>>> a = np.array([4, 2])
>>> b = np.array([2, 2])
>>> a > b
array([ True, False])
"""
greater = DPNPBinaryFunc(
"greater",
ti._greater_result_type,
ti._greater,
_GREATER_DOCSTRING,
)
_GREATER_EQUAL_DOCSTRING = """
Computes the greater-than or equal-to test results for each element :math:`x1_i`
of the input array `x1` with the respective element :math:`x2_i` of the input
array `x2`.
For full documentation refer to :obj:`numpy.greater_equal`.
Parameters
----------
x1 : {dpnp.ndarray, usm_ndarray, scalar}
First input array, may have any data type.
x2 : {dpnp.ndarray, usm_ndarray, scalar}
Second input array, also may have any data type.
out : {None, dpnp.ndarray, usm_ndarray}, optional
Output array to populate.
Array must have the correct shape and the expected data type.
Default: ``None``.
order : {None, "C", "F", "A", "K"}, optional
Memory layout of the newly output array, if parameter `out` is ``None``.
Default: ``"K"``.
Returns
-------
out : dpnp.ndarray of bool dtype
An array containing the result of element-wise greater-than or equal-to
comparison.
Limitations
-----------
Parameters `where` and `subok` are supported with their default values.
Otherwise ``NotImplementedError`` exception will be raised.
See Also
--------
:obj:`dpnp.greater` : Return the truth value of (x1 > x2) element-wise.
:obj:`dpnp.less` : Return the truth value of (x1 < x2) element-wise.
:obj:`dpnp.less_equal` : Return the truth value of (x1 =< x2) element-wise.
:obj:`dpnp.equal` : Return (x1 == x2) element-wise.
:obj:`dpnp.not_equal` : Return (x1 != x2) element-wise.
Notes
-----
At least one of `x1` or `x2` must be an array.
If ``x1.shape != x2.shape``, they must be broadcastable to a common shape
(which becomes the shape of the output).
Examples
--------
>>> import dpnp as np
>>> x1 = np.array([4, 2, 1])
>>> x2 = np.array([2, 2, 2])
>>> np.greater_equal(x1, x2)
array([ True, True, False])
The ``>=`` operator can be used as a shorthand for ``greater_equal`` on
:class:`dpnp.ndarray`.
>>> a = np.array([4, 2, 1])
>>> b = np.array([2, 2, 2])
>>> a >= b
array([ True, True, False])
"""
greater_equal = DPNPBinaryFunc(
"greater_equal",
ti._greater_equal_result_type,
ti._greater_equal,
_GREATER_EQUAL_DOCSTRING,
)
def isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False):
"""
Returns a boolean array where two arrays are element-wise equal within
a tolerance.
The tolerance values are positive, typically very small numbers. The
relative difference (`rtol` * abs(`b`)) and the absolute difference `atol`
are added together to compare against the absolute difference between `a`
and `b`.
``NaNs`` are treated as equal if they are in the same place and if
``equal_nan=True``. ``Infs`` are treated as equal if they are in the same
place and of the same sign in both arrays.
For full documentation refer to :obj:`numpy.isclose`.
Parameters
----------
a : {dpnp.ndarray, usm_ndarray, scalar}
First input array, expected to have a numeric data type.
b : {dpnp.ndarray, usm_ndarray, scalar}
Second input array, also expected to have a numeric data type.
rtol : {dpnp.ndarray, usm_ndarray, scalar}, optional
The relative tolerance parameter.
Default: ``1e-05``.
atol : {dpnp.ndarray, usm_ndarray, scalar}, optional
The absolute tolerance parameter.
Default: ``1e-08``.
equal_nan : bool, optional
Whether to compare ``NaNs`` as equal. If ``True``, ``NaNs`` in `a` will
be considered equal to ``NaNs`` in `b` in the output array.
Default: ``False``.
Returns
-------
out : dpnp.ndarray of bool dtype
Returns a boolean array of where `a` and `b` are equal within the given
tolerance.
See Also
--------
:obj:`dpnp.allclose` : Returns ``True`` if two arrays are element-wise
equal within a tolerance.
Notes
-----
At least one of `x1` or `x2` must be an array.
Examples
--------
>>> import dpnp as np
>>> a = np.array([1e10, 1e-7])
>>> b = np.array([1.00001e10, 1e-8])
>>> np.isclose(a, b)
array([ True, False])
>>> a = np.array([1e10, 1e-8])
>>> b = np.array([1.00001e10, 1e-9])
>>> np.isclose(a, b)
array([ True, True])
>>> a = np.array([1e10, 1e-8])
>>> b = np.array([1.0001e10, 1e-9])
>>> np.isclose(a, b)
array([False, True])
>>> a = np.array([1.0, np.nan])
>>> b = np.array([1.0, np.nan])
>>> np.isclose(a, b)
array([ True, False])
>>> np.isclose(a, b, equal_nan=True)
array([ True, True])
>>> a = np.array([0.0, 0.0])
>>> b = np.array([1e-8, 1e-7])
>>> np.isclose(a, b)
array([ True, False])
>>> b = np.array([1e-100, 1e-7])
>>> np.isclose(a, b, atol=0.0)
array([False, False])
>>> a = np.array([1e-10, 1e-10])
>>> b = np.array([1e-20, 0.0])
>>> np.isclose(a, b)
array([ True, True])
>>> b = np.array([1e-20, 0.999999e-10])
>>> np.isclose(a, b, atol=0.0)
array([False, True])
"""
dpnp.check_supported_arrays_type(a, b, scalar_type=True)
dpnp.check_supported_arrays_type(
rtol, atol, scalar_type=True, all_scalars=True
)
# Use own SYCL kernel for scalar rtol/atol
if dpnp.isscalar(rtol) and dpnp.isscalar(atol):
return _isclose_scalar_tol(a, b, rtol, atol, equal_nan)
# make sure b is an inexact type to avoid bad behavior on abs(MIN_INT)
if dpnp.isscalar(b):
dt = dpnp.result_type(a, b, 1.0, rtol, atol)
b = dpnp.asarray(
b, dtype=dt, sycl_queue=a.sycl_queue, usm_type=a.usm_type
)
elif dpnp.issubdtype(b, dpnp.integer):
dt = dpnp.result_type(b, 1.0, rtol, atol)
b = dpnp.astype(b, dt)
# Firstly handle finite values:
# result = absolute(a - b) <= atol + rtol * absolute(b)
dt = dpnp.result_type(b, rtol, atol)
_b = dpnp.abs(b, dtype=dt)
_b *= rtol
_b += atol
result = less_equal(dpnp.abs(a - b), _b)
# Handle "inf" values: they are treated as equal if they are in the same
# place and of the same sign in both arrays
result &= isfinite(b)
result |= a == b
if equal_nan:
result |= isnan(a) & isnan(b)
return result
def iscomplex(x):
"""
Returns a bool array, where ``True`` if input element is complex.
What is tested is whether the input has a non-zero imaginary part, not if
the input type is complex.
For full documentation refer to :obj:`numpy.iscomplex`.
Parameters
----------
x : {dpnp.ndarray, usm_ndarray}
Input array.
Returns
-------
out : dpnp.ndarray of bool dtype
Output array.
See Also
--------
:obj:`dpnp.isreal` : Returns a bool array, where ``True`` if input element
is real.
:obj:`dpnp.iscomplexobj` : Return ``True`` if `x` is a complex type or an
array of complex numbers.