Skip to content
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

linspace with 1 or 0 elements is sorted #15245

Merged
merged 2 commits into from
Oct 3, 2016

Conversation

gustafsson
Copy link
Contributor

linspace of length 1 or 0 causes a step length of NaN which makes "issorted" return false.

I expected the same return value as the flattened data (collect of such a linspace) which has a true "issorted".

@@ -744,7 +744,7 @@ reverse(r::LinSpace) = LinSpace(r.stop, r.start, r.len, r.divisor)
## sorting ##

issorted(r::UnitRange) = true
issorted(r::Range) = step(r) >= zero(step(r))
issorted(r::Range) = length(r) <= 1 ? true : step(r) >= zero(step(r))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think issorted(r::Range) = length(r) <= 1 || step(r) >= zero(step(r)) would look better.

@gustafsson
Copy link
Contributor Author

@pabloferz agreed

@JeffBezanson
Copy link
Member

+1

Looks like this causes a dates test failure? Maybe that test is invalid.

@pabloferz
Copy link
Contributor

Yeah. It seems that the test in dates was relying on the previous incorrect behaviour.

@gustafsson Seems that you would have to remove that test.

@gustafsson gustafsson force-pushed the short-linspace-is-sorted branch 3 times, most recently from 3273b6a to 1fba161 Compare March 9, 2016 05:05
@gustafsson
Copy link
Contributor Author

To clarify, this applies to any Range, not just linspace. For instance issorted(1:-1:1) changes from false to true with the intent that issorted(r) == issorted(collect(r)) should be true for any Range r.

@gustafsson
Copy link
Contributor Author

bump, this still applies in master, any comments?

@tkelman tkelman merged commit 5a2553f into JuliaLang:master Oct 3, 2016
@gustafsson gustafsson deleted the short-linspace-is-sorted branch October 3, 2016 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants