|
| 1 | +import operator |
| 2 | + |
| 3 | +import numpy |
| 4 | + |
| 5 | +import dpnp |
| 6 | +import dpnp.dpnp_container as dpnp_container |
| 7 | +import dpnp.dpnp_utils as utils |
| 8 | + |
| 9 | +__all__ = [ |
| 10 | + "dpnp_geomspace", |
| 11 | + "dpnp_linspace", |
| 12 | + "dpnp_logspace", |
| 13 | +] |
| 14 | + |
| 15 | + |
| 16 | +def dpnp_geomspace( |
| 17 | + start, |
| 18 | + stop, |
| 19 | + num, |
| 20 | + dtype=None, |
| 21 | + device=None, |
| 22 | + usm_type=None, |
| 23 | + sycl_queue=None, |
| 24 | + endpoint=True, |
| 25 | + axis=0, |
| 26 | +): |
| 27 | + usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations([start, stop]) |
| 28 | + |
| 29 | + if sycl_queue is None and device is None: |
| 30 | + sycl_queue = sycl_queue_alloc |
| 31 | + sycl_queue_normalized = dpnp.get_normalized_queue_device( |
| 32 | + sycl_queue=sycl_queue, device=device |
| 33 | + ) |
| 34 | + |
| 35 | + if usm_type is None: |
| 36 | + _usm_type = "device" if usm_type_alloc is None else usm_type_alloc |
| 37 | + else: |
| 38 | + _usm_type = usm_type |
| 39 | + |
| 40 | + if not dpnp.is_supported_array_type(start): |
| 41 | + start = dpnp.asarray( |
| 42 | + start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized |
| 43 | + ) |
| 44 | + if not dpnp.is_supported_array_type(stop): |
| 45 | + stop = dpnp.asarray( |
| 46 | + stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized |
| 47 | + ) |
| 48 | + |
| 49 | + dt = numpy.result_type(start, stop, float(num)) |
| 50 | + dt = utils.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device) |
| 51 | + if dtype is None: |
| 52 | + dtype = dt |
| 53 | + |
| 54 | + if dpnp.any(start == 0) or dpnp.any(stop == 0): |
| 55 | + raise ValueError("Geometric sequence cannot include zero") |
| 56 | + |
| 57 | + out_sign = dpnp.ones( |
| 58 | + dpnp.broadcast_arrays(start, stop)[0].shape, |
| 59 | + dtype=dt, |
| 60 | + usm_type=_usm_type, |
| 61 | + sycl_queue=sycl_queue_normalized, |
| 62 | + ) |
| 63 | + # Avoid negligible real or imaginary parts in output by rotating to |
| 64 | + # positive real, calculating, then undoing rotation |
| 65 | + if dpnp.issubdtype(dt, dpnp.complexfloating): |
| 66 | + all_imag = (start.real == 0.0) & (stop.real == 0.0) |
| 67 | + if dpnp.any(all_imag): |
| 68 | + start[all_imag] = start[all_imag].imag |
| 69 | + stop[all_imag] = stop[all_imag].imag |
| 70 | + out_sign[all_imag] = 1j |
| 71 | + |
| 72 | + both_negative = (dpnp.sign(start) == -1) & (dpnp.sign(stop) == -1) |
| 73 | + if dpnp.any(both_negative): |
| 74 | + dpnp.negative(start[both_negative], out=start[both_negative]) |
| 75 | + dpnp.negative(stop[both_negative], out=stop[both_negative]) |
| 76 | + dpnp.negative(out_sign[both_negative], out=out_sign[both_negative]) |
| 77 | + |
| 78 | + log_start = dpnp.log10(start) |
| 79 | + log_stop = dpnp.log10(stop) |
| 80 | + result = dpnp_logspace( |
| 81 | + log_start, |
| 82 | + log_stop, |
| 83 | + num=num, |
| 84 | + endpoint=endpoint, |
| 85 | + base=10.0, |
| 86 | + dtype=dtype, |
| 87 | + usm_type=_usm_type, |
| 88 | + sycl_queue=sycl_queue_normalized, |
| 89 | + ) |
| 90 | + |
| 91 | + if num > 0: |
| 92 | + result[0] = start |
| 93 | + if num > 1 and endpoint: |
| 94 | + result[-1] = stop |
| 95 | + |
| 96 | + result = out_sign * result |
| 97 | + |
| 98 | + if axis != 0: |
| 99 | + result = dpnp.moveaxis(result, 0, axis) |
| 100 | + |
| 101 | + return result.astype(dtype, copy=False) |
| 102 | + |
| 103 | + |
| 104 | +def dpnp_linspace( |
| 105 | + start, |
| 106 | + stop, |
| 107 | + num, |
| 108 | + dtype=None, |
| 109 | + device=None, |
| 110 | + usm_type=None, |
| 111 | + sycl_queue=None, |
| 112 | + endpoint=True, |
| 113 | + retstep=False, |
| 114 | + axis=0, |
| 115 | +): |
| 116 | + usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations([start, stop]) |
| 117 | + |
| 118 | + if sycl_queue is None and device is None: |
| 119 | + sycl_queue = sycl_queue_alloc |
| 120 | + sycl_queue_normalized = dpnp.get_normalized_queue_device( |
| 121 | + sycl_queue=sycl_queue, device=device |
| 122 | + ) |
| 123 | + |
| 124 | + if usm_type is None: |
| 125 | + _usm_type = "device" if usm_type_alloc is None else usm_type_alloc |
| 126 | + else: |
| 127 | + _usm_type = usm_type |
| 128 | + |
| 129 | + if not hasattr(start, "dtype") and not dpnp.isscalar(start): |
| 130 | + start = dpnp.asarray( |
| 131 | + start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized |
| 132 | + ) |
| 133 | + if not hasattr(stop, "dtype") and not dpnp.isscalar(stop): |
| 134 | + stop = dpnp.asarray( |
| 135 | + stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized |
| 136 | + ) |
| 137 | + |
| 138 | + dt = numpy.result_type(start, stop, float(num)) |
| 139 | + dt = utils.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device) |
| 140 | + if dtype is None: |
| 141 | + dtype = dt |
| 142 | + |
| 143 | + num = operator.index(num) |
| 144 | + if num < 0: |
| 145 | + raise ValueError("Number of points must be non-negative") |
| 146 | + step_num = (num - 1) if endpoint else num |
| 147 | + |
| 148 | + step_nan = False |
| 149 | + if step_num == 0: |
| 150 | + step_nan = True |
| 151 | + step = dpnp.nan |
| 152 | + |
| 153 | + if dpnp.isscalar(start) and dpnp.isscalar(stop): |
| 154 | + # Call linspace() function for scalars. |
| 155 | + res = dpnp_container.linspace( |
| 156 | + start, |
| 157 | + stop, |
| 158 | + num, |
| 159 | + dtype=dt, |
| 160 | + usm_type=_usm_type, |
| 161 | + sycl_queue=sycl_queue_normalized, |
| 162 | + endpoint=endpoint, |
| 163 | + ) |
| 164 | + if retstep is True and step_nan is False: |
| 165 | + step = (stop - start) / step_num |
| 166 | + else: |
| 167 | + _start = dpnp.asarray( |
| 168 | + start, |
| 169 | + dtype=dt, |
| 170 | + usm_type=_usm_type, |
| 171 | + sycl_queue=sycl_queue_normalized, |
| 172 | + ) |
| 173 | + _stop = dpnp.asarray( |
| 174 | + stop, dtype=dt, usm_type=_usm_type, sycl_queue=sycl_queue_normalized |
| 175 | + ) |
| 176 | + |
| 177 | + res = dpnp_container.arange( |
| 178 | + 0, |
| 179 | + stop=num, |
| 180 | + step=1, |
| 181 | + dtype=dt, |
| 182 | + usm_type=_usm_type, |
| 183 | + sycl_queue=sycl_queue_normalized, |
| 184 | + ) |
| 185 | + |
| 186 | + if step_nan is False: |
| 187 | + step = (_stop - _start) / step_num |
| 188 | + res = res.reshape((-1,) + (1,) * step.ndim) |
| 189 | + res = res * step + _start |
| 190 | + |
| 191 | + if endpoint and num > 1: |
| 192 | + res[-1] = dpnp_container.full(step.shape, _stop) |
| 193 | + |
| 194 | + if axis != 0: |
| 195 | + res = dpnp.moveaxis(res, 0, axis) |
| 196 | + |
| 197 | + if numpy.issubdtype(dtype, dpnp.integer): |
| 198 | + dpnp.floor(res, out=res) |
| 199 | + |
| 200 | + res = res.astype(dtype, copy=False) |
| 201 | + |
| 202 | + if retstep is True: |
| 203 | + if dpnp.isscalar(step): |
| 204 | + step = dpnp.asarray( |
| 205 | + step, usm_type=res.usm_type, sycl_queue=res.sycl_queue |
| 206 | + ) |
| 207 | + return (res, step) |
| 208 | + |
| 209 | + return res |
| 210 | + |
| 211 | + |
| 212 | +def dpnp_logspace( |
| 213 | + start, |
| 214 | + stop, |
| 215 | + num=50, |
| 216 | + device=None, |
| 217 | + usm_type=None, |
| 218 | + sycl_queue=None, |
| 219 | + endpoint=True, |
| 220 | + base=10.0, |
| 221 | + dtype=None, |
| 222 | + axis=0, |
| 223 | +): |
| 224 | + if not dpnp.isscalar(base): |
| 225 | + usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations( |
| 226 | + [start, stop, base] |
| 227 | + ) |
| 228 | + |
| 229 | + if sycl_queue is None and device is None: |
| 230 | + sycl_queue = sycl_queue_alloc |
| 231 | + sycl_queue = dpnp.get_normalized_queue_device( |
| 232 | + sycl_queue=sycl_queue, device=device |
| 233 | + ) |
| 234 | + |
| 235 | + if usm_type is None: |
| 236 | + usm_type = "device" if usm_type_alloc is None else usm_type_alloc |
| 237 | + else: |
| 238 | + usm_type = usm_type |
| 239 | + start = dpnp.asarray(start, usm_type=usm_type, sycl_queue=sycl_queue) |
| 240 | + stop = dpnp.asarray(stop, usm_type=usm_type, sycl_queue=sycl_queue) |
| 241 | + base = dpnp.asarray(base, usm_type=usm_type, sycl_queue=sycl_queue) |
| 242 | + [start, stop, base] = dpnp.broadcast_arrays(start, stop, base) |
| 243 | + base = dpnp.expand_dims(base, axis=axis) |
| 244 | + |
| 245 | + res = dpnp_linspace( |
| 246 | + start, |
| 247 | + stop, |
| 248 | + num=num, |
| 249 | + device=device, |
| 250 | + usm_type=usm_type, |
| 251 | + sycl_queue=sycl_queue, |
| 252 | + endpoint=endpoint, |
| 253 | + axis=axis, |
| 254 | + ) |
| 255 | + |
| 256 | + if dtype is None: |
| 257 | + return dpnp.power(base, res) |
| 258 | + return dpnp.power(base, res).astype(dtype, copy=False) |
0 commit comments