-
Notifications
You must be signed in to change notification settings - Fork 22
Update dpnp.geomspace and dpnp.logspace functions #1603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+614
−247
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a09549b
Update dpnp.geomspace and dpnp.logspace functions
npolina4 b82ba6c
Merge branch 'master' into logspace_geomspace
npolina4 1f559fb
Added more test for geomspace and logspace functions
npolina4 0b5f250
Merge branch 'logspace_geomspace' of https://github.com/IntelPython/d…
npolina4 008bc6b
Merge branch 'master' into logspace_geomspace
npolina4 e64576f
Added tests for geomspace zero error
npolina4 9491037
Merge branch 'logspace_geomspace' of https://github.com/IntelPython/d…
npolina4 3470248
address comments
npolina4 0ff2baf
Merge branch 'master' into logspace_geomspace
npolina4 ce2ccff
Added tests
npolina4 9a48b7d
Merge branch 'master' into logspace_geomspace
npolina4 61241cc
skip test logspace base
npolina4 296102b
address comments
npolina4 71895f9
Merge branch 'master' into logspace_geomspace
npolina4 1f6ee26
Fix linspace docs
npolina4 6c94a9d
Update dpnp/dpnp_iface_arraycreation.py
npolina4 6e792b0
Merge branch 'master' into logspace_geomspace
npolina4 ce12203
Merge branch 'master' into logspace_geomspace
npolina4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,258 @@ | ||
import operator | ||
|
||
import numpy | ||
|
||
import dpnp | ||
import dpnp.dpnp_container as dpnp_container | ||
import dpnp.dpnp_utils as utils | ||
|
||
__all__ = [ | ||
"dpnp_geomspace", | ||
"dpnp_linspace", | ||
"dpnp_logspace", | ||
] | ||
|
||
|
||
def dpnp_geomspace( | ||
start, | ||
stop, | ||
num, | ||
dtype=None, | ||
device=None, | ||
usm_type=None, | ||
sycl_queue=None, | ||
endpoint=True, | ||
axis=0, | ||
): | ||
usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations([start, stop]) | ||
|
||
if sycl_queue is None and device is None: | ||
sycl_queue = sycl_queue_alloc | ||
sycl_queue_normalized = dpnp.get_normalized_queue_device( | ||
sycl_queue=sycl_queue, device=device | ||
) | ||
|
||
if usm_type is None: | ||
_usm_type = "device" if usm_type_alloc is None else usm_type_alloc | ||
else: | ||
_usm_type = usm_type | ||
|
||
if not dpnp.is_supported_array_type(start): | ||
start = dpnp.asarray( | ||
start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized | ||
) | ||
if not dpnp.is_supported_array_type(stop): | ||
stop = dpnp.asarray( | ||
stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized | ||
) | ||
|
||
dt = numpy.result_type(start, stop, float(num)) | ||
dt = utils.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device) | ||
if dtype is None: | ||
dtype = dt | ||
|
||
if dpnp.any(start == 0) or dpnp.any(stop == 0): | ||
raise ValueError("Geometric sequence cannot include zero") | ||
|
||
out_sign = dpnp.ones( | ||
dpnp.broadcast_arrays(start, stop)[0].shape, | ||
dtype=dt, | ||
usm_type=_usm_type, | ||
sycl_queue=sycl_queue_normalized, | ||
) | ||
# Avoid negligible real or imaginary parts in output by rotating to | ||
# positive real, calculating, then undoing rotation | ||
if dpnp.issubdtype(dt, dpnp.complexfloating): | ||
all_imag = (start.real == 0.0) & (stop.real == 0.0) | ||
if dpnp.any(all_imag): | ||
start[all_imag] = start[all_imag].imag | ||
stop[all_imag] = stop[all_imag].imag | ||
out_sign[all_imag] = 1j | ||
|
||
both_negative = (dpnp.sign(start) == -1) & (dpnp.sign(stop) == -1) | ||
if dpnp.any(both_negative): | ||
dpnp.negative(start[both_negative], out=start[both_negative]) | ||
dpnp.negative(stop[both_negative], out=stop[both_negative]) | ||
dpnp.negative(out_sign[both_negative], out=out_sign[both_negative]) | ||
|
||
log_start = dpnp.log10(start) | ||
log_stop = dpnp.log10(stop) | ||
result = dpnp_logspace( | ||
log_start, | ||
log_stop, | ||
num=num, | ||
endpoint=endpoint, | ||
base=10.0, | ||
dtype=dtype, | ||
usm_type=_usm_type, | ||
sycl_queue=sycl_queue_normalized, | ||
) | ||
|
||
if num > 0: | ||
npolina4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result[0] = start | ||
if num > 1 and endpoint: | ||
result[-1] = stop | ||
|
||
result = out_sign * result | ||
|
||
if axis != 0: | ||
result = dpnp.moveaxis(result, 0, axis) | ||
|
||
return result.astype(dtype, copy=False) | ||
|
||
|
||
def dpnp_linspace( | ||
start, | ||
stop, | ||
num, | ||
dtype=None, | ||
device=None, | ||
usm_type=None, | ||
sycl_queue=None, | ||
endpoint=True, | ||
retstep=False, | ||
axis=0, | ||
): | ||
usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations([start, stop]) | ||
|
||
if sycl_queue is None and device is None: | ||
sycl_queue = sycl_queue_alloc | ||
sycl_queue_normalized = dpnp.get_normalized_queue_device( | ||
sycl_queue=sycl_queue, device=device | ||
) | ||
|
||
if usm_type is None: | ||
_usm_type = "device" if usm_type_alloc is None else usm_type_alloc | ||
else: | ||
_usm_type = usm_type | ||
|
||
if not hasattr(start, "dtype") and not dpnp.isscalar(start): | ||
start = dpnp.asarray( | ||
start, usm_type=_usm_type, sycl_queue=sycl_queue_normalized | ||
) | ||
if not hasattr(stop, "dtype") and not dpnp.isscalar(stop): | ||
stop = dpnp.asarray( | ||
stop, usm_type=_usm_type, sycl_queue=sycl_queue_normalized | ||
) | ||
|
||
dt = numpy.result_type(start, stop, float(num)) | ||
dt = utils.map_dtype_to_device(dt, sycl_queue_normalized.sycl_device) | ||
if dtype is None: | ||
dtype = dt | ||
|
||
num = operator.index(num) | ||
if num < 0: | ||
raise ValueError("Number of points must be non-negative") | ||
step_num = (num - 1) if endpoint else num | ||
|
||
step_nan = False | ||
if step_num == 0: | ||
step_nan = True | ||
step = dpnp.nan | ||
|
||
if dpnp.isscalar(start) and dpnp.isscalar(stop): | ||
# Call linspace() function for scalars. | ||
res = dpnp_container.linspace( | ||
start, | ||
stop, | ||
num, | ||
dtype=dt, | ||
usm_type=_usm_type, | ||
sycl_queue=sycl_queue_normalized, | ||
endpoint=endpoint, | ||
) | ||
if retstep is True and step_nan is False: | ||
step = (stop - start) / step_num | ||
else: | ||
_start = dpnp.asarray( | ||
start, | ||
dtype=dt, | ||
usm_type=_usm_type, | ||
sycl_queue=sycl_queue_normalized, | ||
) | ||
_stop = dpnp.asarray( | ||
stop, dtype=dt, usm_type=_usm_type, sycl_queue=sycl_queue_normalized | ||
) | ||
|
||
res = dpnp_container.arange( | ||
0, | ||
stop=num, | ||
step=1, | ||
dtype=dt, | ||
usm_type=_usm_type, | ||
sycl_queue=sycl_queue_normalized, | ||
) | ||
|
||
if step_nan is False: | ||
step = (_stop - _start) / step_num | ||
res = res.reshape((-1,) + (1,) * step.ndim) | ||
res = res * step + _start | ||
|
||
if endpoint and num > 1: | ||
res[-1] = dpnp_container.full(step.shape, _stop) | ||
|
||
if axis != 0: | ||
res = dpnp.moveaxis(res, 0, axis) | ||
|
||
if numpy.issubdtype(dtype, dpnp.integer): | ||
dpnp.floor(res, out=res) | ||
|
||
res = res.astype(dtype, copy=False) | ||
|
||
if retstep is True: | ||
if dpnp.isscalar(step): | ||
step = dpnp.asarray( | ||
step, usm_type=res.usm_type, sycl_queue=res.sycl_queue | ||
) | ||
return (res, step) | ||
|
||
return res | ||
|
||
|
||
def dpnp_logspace( | ||
start, | ||
stop, | ||
num=50, | ||
device=None, | ||
usm_type=None, | ||
sycl_queue=None, | ||
endpoint=True, | ||
base=10.0, | ||
dtype=None, | ||
axis=0, | ||
): | ||
if not dpnp.isscalar(base): | ||
usm_type_alloc, sycl_queue_alloc = utils.get_usm_allocations( | ||
[start, stop, base] | ||
) | ||
|
||
if sycl_queue is None and device is None: | ||
sycl_queue = sycl_queue_alloc | ||
sycl_queue = dpnp.get_normalized_queue_device( | ||
sycl_queue=sycl_queue, device=device | ||
) | ||
|
||
if usm_type is None: | ||
usm_type = "device" if usm_type_alloc is None else usm_type_alloc | ||
else: | ||
usm_type = usm_type | ||
start = dpnp.asarray(start, usm_type=usm_type, sycl_queue=sycl_queue) | ||
stop = dpnp.asarray(stop, usm_type=usm_type, sycl_queue=sycl_queue) | ||
base = dpnp.asarray(base, usm_type=usm_type, sycl_queue=sycl_queue) | ||
[start, stop, base] = dpnp.broadcast_arrays(start, stop, base) | ||
base = dpnp.expand_dims(base, axis=axis) | ||
|
||
res = dpnp_linspace( | ||
start, | ||
stop, | ||
num=num, | ||
device=device, | ||
usm_type=usm_type, | ||
sycl_queue=sycl_queue, | ||
endpoint=endpoint, | ||
axis=axis, | ||
) | ||
|
||
if dtype is None: | ||
return dpnp.power(base, res) | ||
return dpnp.power(base, res).astype(dtype, copy=False) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.