@@ -866,9 +866,14 @@ def diag(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
866
866
v : array_like
867
867
Input data, in any form that can be converted to an array. This
868
868
includes scalars, lists, lists of tuples, tuples, tuples of tuples,
869
- tuples of lists, and ndarrays. If `v` is a 2-D array, return a copy of
870
- its k-th diagonal. If `v` is a 1-D array, return a 2-D array with `v`
871
- on the k-th diagonal.
869
+ tuples of lists, and ndarrays.
870
+ If `v` is a 1-D array, return a 2-D array with `v`
871
+ on the `k`-th diagonal.
872
+ If `v` is a 2-D array and is an instance of
873
+ {dpnp.ndarray, usm_ndarray}, then:
874
+ - If `device`, `usm_type`, and `sycl_queue` are set to their
875
+ default values, returns a read/write view of its k-th diagonal.
876
+ - Otherwise, returns a copy of its k-th diagonal.
872
877
k : int, optional
873
878
Diagonal in question. The default is 0. Use k > 0 for diagonals above
874
879
the main diagonal, and k < 0 for diagonals below the main diagonal.
@@ -894,79 +899,62 @@ def diag(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
894
899
--------
895
900
:obj:`diagonal` : Return specified diagonals.
896
901
:obj:`diagflat` : Create a 2-D array with the flattened input as a diagonal.
897
- :obj:`trace` : Return sum along diagonals.
898
- :obj:`triu` : Return upper triangle of an array.
899
- :obj:`tril` : Return lower triangle of an array.
902
+ :obj:`trace` : Return the sum along diagonals of the array .
903
+ :obj:`triu` : Upper triangle of an array.
904
+ :obj:`tril` : Lower triangle of an array.
900
905
901
906
Examples
902
907
--------
903
908
>>> import dpnp as np
904
- >>> x0 = np.arange(9).reshape((3, 3))
905
- >>> x0
909
+ >>> x = np.arange(9).reshape((3, 3))
910
+ >>> x
906
911
array([[0, 1, 2],
907
912
[3, 4, 5],
908
913
[6, 7, 8]])
909
914
910
- >>> np.diag(x0 )
915
+ >>> np.diag(x )
911
916
array([0, 4, 8])
912
- >>> np.diag(x0 , k=1)
917
+ >>> np.diag(x , k=1)
913
918
array([1, 5])
914
- >>> np.diag(x0 , k=-1)
919
+ >>> np.diag(x , k=-1)
915
920
array([3, 7])
916
921
917
- >>> np.diag(np.diag(x0 ))
922
+ >>> np.diag(np.diag(x ))
918
923
array([[0, 0, 0],
919
924
[0, 4, 0],
920
925
[0, 0, 8]])
921
926
922
927
Creating an array on a different device or with a specified usm_type
923
928
924
- >>> x = np.diag(x0 ) # default case
925
- >>> x, x .device, x .usm_type
929
+ >>> res = np.diag(x ) # default case
930
+ >>> res, res .device, res .usm_type
926
931
(array([0, 4, 8]), Device(level_zero:gpu:0), 'device')
927
932
928
- >>> y = np.diag(x0 , device="cpu")
929
- >>> y, y .device, y .usm_type
933
+ >>> res_cpu = np.diag(x , device="cpu")
934
+ >>> res_cpu, res_cpu .device, res_cpu .usm_type
930
935
(array([0, 4, 8]), Device(opencl:cpu:0), 'device')
931
936
932
- >>> z = np.diag(x0 , usm_type="host")
933
- >>> z, z .device, z .usm_type
937
+ >>> res_host = np.diag(x , usm_type="host")
938
+ >>> res_host, res_host .device, res_host .usm_type
934
939
(array([0, 4, 8]), Device(level_zero:gpu:0), 'host')
935
940
936
941
"""
937
942
938
943
if not isinstance (k , int ):
939
944
raise TypeError (f"An integer is required, but got { type (k )} " )
940
945
941
- v = dpnp .asarray (v , device = device , usm_type = usm_type , sycl_queue = sycl_queue )
946
+ v = dpnp .asanyarray (
947
+ v , device = device , usm_type = usm_type , sycl_queue = sycl_queue
948
+ )
942
949
943
- init0 = max (0 , - k )
944
- init1 = max (0 , k )
945
950
if v .ndim == 1 :
946
951
size = v .shape [0 ] + abs (k )
947
- m = dpnp .zeros (
948
- (size , size ),
949
- dtype = v .dtype ,
950
- usm_type = v .usm_type ,
951
- sycl_queue = v .sycl_queue ,
952
- )
953
- for i in range (v .shape [0 ]):
954
- m [(init0 + i ), init1 + i ] = v [i ]
955
- return m
952
+ ret = dpnp .zeros_like (v , shape = (size , size ))
953
+ ret .diagonal (k )[:] = v
954
+ return ret
956
955
957
956
if v .ndim == 2 :
958
- size = max (
959
- 0 , min (v .shape [0 ], v .shape [0 ] + k , v .shape [1 ], v .shape [1 ] - k )
960
- )
961
- m = dpnp .zeros (
962
- (size ,),
963
- dtype = v .dtype ,
964
- usm_type = v .usm_type ,
965
- sycl_queue = v .sycl_queue ,
966
- )
967
- for i in range (size ):
968
- m [i ] = v [(init0 + i ), init1 + i ]
969
- return m
957
+ return v .diagonal (k )
970
958
971
959
raise ValueError ("Input must be a 1-D or 2-D array." )
972
960
@@ -1008,9 +996,9 @@ def diagflat(v, /, k=0, *, device=None, usm_type=None, sycl_queue=None):
1008
996
1009
997
See Also
1010
998
--------
1011
- :obj:`diag` : Return the extracted diagonal or constructed diagonal array.
1012
- :obj:`diagonal` : Return specified diagonals.
1013
- :obj:`trace` : Return sum along diagonals.
999
+ :obj:`dpnp. diag` : Extract a diagonal or construct a diagonal array.
1000
+ :obj:`dpnp. diagonal` : Return specified diagonals.
1001
+ :obj:`dpnp. trace` : Return sum along diagonals.
1014
1002
1015
1003
Examples
1016
1004
--------
@@ -1324,6 +1312,11 @@ def eye(
1324
1312
Parameter `like` is supported only with default value ``None``.
1325
1313
Otherwise, the function raises `NotImplementedError` exception.
1326
1314
1315
+ See Also
1316
+ --------
1317
+ :obj:`dpnp.identity` : Return the identity array.
1318
+ :obj:`dpnp.diag` : Extract a diagonal or construct a diagonal array.
1319
+
1327
1320
Examples
1328
1321
--------
1329
1322
>>> import dpnp as np
@@ -2264,7 +2257,7 @@ def identity(
2264
2257
:obj:`dpnp.eye` : Return a 2-D array with ones on the diagonal and zeros
2265
2258
elsewhere.
2266
2259
:obj:`dpnp.ones` : Return a new array setting values to one.
2267
- :obj:`dpnp.diag` : Return diagonal 2-D array from an input 1-D array.
2260
+ :obj:`dpnp.diag` : Extract a diagonal or construct a diagonal array.
2268
2261
2269
2262
Examples
2270
2263
--------
0 commit comments