@@ -1035,3 +1035,68 @@ def linspace(
1035
1035
)
1036
1036
hev .wait ()
1037
1037
return res
1038
+
1039
+ def eye (
1040
+ n_rows ,
1041
+ n_cols = None ,
1042
+ / ,
1043
+ * ,
1044
+ k = 0 ,
1045
+ dtype = None ,
1046
+ order = "C" ,
1047
+ device = None ,
1048
+ usm_type = "device" ,
1049
+ sycl_queue = None
1050
+ ):
1051
+ """
1052
+ eye(n_rows, n_cols = None, /, *, k = 0, dtype = None, \
1053
+ device = None, usm_type="device", sycl_queue=None) -> usm_ndarray
1054
+
1055
+ Creates `usm_ndarray` where the `k`th diagonal elements are one and others are zero.
1056
+
1057
+ Args:
1058
+ n_rows: number of rows in the output array.
1059
+ n_cols (optional): number of columns in the output array. If None,
1060
+ n_cols = n_rows. Default: `None`.
1061
+ k: index of the diagonal, with 0 as the main diagonal. A positive value of k
1062
+ is an upper diagonal, a negative value is a low diagonal. Default: `0`.
1063
+ dtype (optional): data type of the array. Can be typestring,
1064
+ a `numpy.dtype` object, `numpy` char string, or a numpy
1065
+ scalar type. Default: None
1066
+ order ("C" or F"): memory layout for the array. Default: "C"
1067
+ device (optional): array API concept of device where the output array
1068
+ is created. `device` can be `None`, a oneAPI filter selector string,
1069
+ an instance of :class:`dpctl.SyclDevice` corresponding to a
1070
+ non-partitioned SYCL device, an instance of
1071
+ :class:`dpctl.SyclQueue`, or a `Device` object returnedby
1072
+ `dpctl.tensor.usm_array.device`. Default: `None`.
1073
+ usm_type ("device"|"shared"|"host", optional): The type of SYCL USM
1074
+ allocation for the output array. Default: `"device"`.
1075
+ sycl_queue (:class:`dpctl.SyclQueue`, optional): The SYCL queue to use
1076
+ for output array allocation and copying. `sycl_queue` and `device`
1077
+ are exclusive keywords, i.e. use one or another. If both are
1078
+ specified, a `TypeError` is raised unless both imply the same
1079
+ underlying SYCL queue to be used. If both are `None`, the
1080
+ `dpctl.SyclQueue()` is used for allocation and copying.
1081
+ Default: `None`.
1082
+ """
1083
+ if n_cols is None :
1084
+ n_cols = n_rows
1085
+ #allocate a 1D array of zeros, length equal to n_cols * n_rows
1086
+ x = zeros ((n_rows * n_cols ,), dtype = dtype , order = order , device = device , usm_type = usm_type , sycl_queue = sycl_queue )
1087
+ if k > - n_rows and k < n_cols :
1088
+ #find the length of an arbitrary diagonal
1089
+ l = min (n_cols , n_rows , n_cols - k , n_rows + k )
1090
+ #i is the first element of the diagonal, j is the last, s is the step size
1091
+ if order == "C" :
1092
+ s = n_cols + 1
1093
+ i = k if k >= 0 else n_cols * - k
1094
+ else :
1095
+ s = n_rows + 1
1096
+ i = n_rows * k if k > 0 else - k
1097
+ #last index + 1 prevents slice from excluding the last element
1098
+ j = i + ((l - 1 )* s )+ 1
1099
+ x [i :j :s ] = 1
1100
+ #copy=False ensures no wasted memory copying the array
1101
+ #and as the order parameter is the same, a copy should never be necessary
1102
+ return dpt .reshape (x , (n_rows , n_cols ), order = order , copy = False )
0 commit comments