Skip to content

Change linspace creation to match numpy when num equal to 0#8676

Merged
jsignell merged 4 commits intodask:mainfrom
peterpandelidis:linspace-divbyzero-error
Feb 24, 2022
Merged

Change linspace creation to match numpy when num equal to 0#8676
jsignell merged 4 commits intodask:mainfrom
peterpandelidis:linspace-divbyzero-error

Conversation

@peterpandelidis
Copy link
Copy Markdown
Contributor

@peterpandelidis peterpandelidis commented Feb 7, 2022

@GPUtester
Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@github-actions github-actions bot added the array label Feb 7, 2022
@tomwhite
Copy link
Copy Markdown
Contributor

tomwhite commented Feb 8, 2022

Thanks for the fix @peterpandelidis. There's another case that would be worth fixing too: if num is 1.

>>> import numpy as np
>>> np.linspace(0, 0, 1)
array([0.])
>>> import dask.array as da
>>> da.linspace(0, 0, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/tom/projects-workspace/dask/dask/array/creation.py", line 303, in linspace
    step = float(range_) / div
ZeroDivisionError: float division by zero

@peterpandelidis
Copy link
Copy Markdown
Contributor Author

Made a change for the above. Not the most elegant solution, but it works.

@jakirkham
Copy link
Copy Markdown
Member

@tomwhite do you have thoughts on the change above?

range_ = stop - start

div = (num - 1) if endpoint else num
div = (num - 1) if (endpoint and num != 1) or num == 0 else num
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a bit odd that if num is zero then div is negative - even if the tests all pass. Would this be better?

div = (num - 1) if endpoint else num
if div == 0:
    div = 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find that first statement hard to read, so yeah I like your suggestion.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterpandelidis What do you think about this suggestion?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree with the suggestion.
I've been away for a bit so haven't updated the PR.
Will update tonight.

@jsignell
Copy link
Copy Markdown
Member

ok to test

@tomwhite tomwhite mentioned this pull request Feb 23, 2022
3 tasks
@jakirkham jakirkham requested a review from tomwhite February 24, 2022 03:35
Copy link
Copy Markdown
Contributor

@tomwhite tomwhite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for updating this @peterpandelidis.

@jsignell jsignell added the enhancement Improve existing functionality or make things work better label Feb 24, 2022
@jsignell jsignell merged commit 7e83e72 into dask:main Feb 24, 2022
@jsignell
Copy link
Copy Markdown
Member

Thanks for working on this @peterpandelidis and thanks @tomwhite for reviewing!

@jakirkham
Copy link
Copy Markdown
Member

Thanks all! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array enhancement Improve existing functionality or make things work better

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linspace division by zero error

6 participants