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

question about Arange function #12

Open
ledao opened this issue Jan 6, 2017 · 2 comments
Open

question about Arange function #12

ledao opened this issue Jan 6, 2017 · 2 comments
Labels

Comments

@ledao
Copy link

ledao commented Jan 6, 2017

Hi,
In numgo, fmt.Println(numgo.Arange(0, 10)) => [0 1 2 3 4 5 6 7 8 9 10].
In numpy, print(np.arange(0, 10)) => [0 1 2 3 4 5 6 7 8 9].
Both are ok, but I prefer to drop 'stop', like numpy.
Would you think about it?
Tks!

@Kunde21
Copy link
Owner

Kunde21 commented Jan 8, 2017

FYI: Reviewing this issue revealed an inconsistency that was fixed in b9dc613.

Some of the numpy arange logic has been a pain point for me and those I've taught/worked with. The issue shows up when working with negative-stepping ranges:

// Dropping stop number (numpy)
np.arange(0, 5)        => [0 1 2 3 4]
np.arange(5, 0, -1)  => [5 4 3 2 1]
np.arange(-5, 0)      => [-5 -4 -3 -2 -1]
np.arange(0, -5, -1) => [0 -1 -2 -3 -4]

// Keeping the stop number (numgo)
numgo.Arange(0, 5)   => [0 1 2 3 4 5]
numgo.Arange(5, 0)   => [5 4 3 2 1 0]
numgo.Arange(-5, 0)  => [-5 -4 -3 -2 -1 0]
numgo.Arange(0, -5)  => [0 -1 -2 -3 -4 -5]

The other part of changing the Arange(0, 5) result is deciding how the Arange(5, 0) result will be changed. There are a couple options that would work like np.arange(0,x), each with related issues:

  • Drop the stop number, which means flipping l/r Arange(0,5) is the less logical call Arange(4, -1). This trips up beginners and experienced numpy users alike.
  • Drop the number with the greatest absolute value, which makes for another question about handling Arange(5, -5) vs Arange(-5, 5). A smaller corner case, meaning a lot more warnings and explanations in the documentation.

While I would like to mirror as much of the numpy API as possible, the handling of negative-stepping ranges is something I felt needed to be re-thought. If you have suggestions on how to improve the API, please keep in mind that numgo.Arange handles negative ranges and increments, which complicates some things. This implementation is just my first attempt at making arange less confusing with negative steps.

@ledao
Copy link
Author

ledao commented Jan 8, 2017

Thanks for your consideration.
To be honest, I am a numpy user. Keeping or droping 'stop' is not a question, the question is consistency. In len(vals) == 1 condition, numgo drops 'stop', but in len(vals) == 2 condition, numgo keeps 'stop'.
Here is my idea.
I didn't notice that, numgo.Arange(-10) => [-9 -8 -7 -6 -5 -4 -3 -2 -1 0]. In my view, Since -10 is 'stop', and the default 'start' should be 0, then the sequence must be [0, -1, -2, -3, -4, -5, -6, -7, -8, -9] (desc), not [-9 -8 -7 -6 -5 -4 -3 -2 -1 0].
If you agree, we only need to handle 'stop'.
Dropping 'stop': 'stop' -1 in ascending order sequence; 'stop'+1 in descending order sequence.
Keeping 'stop': 'stop' is unchanged in all conditions.
For example:

	case 2:
		if vals[1] < vals[0] {
			step = -1
			start, stop = vals[0], vals[1]+1
		} else {
			start, stop = vals[0], vals[1]-1
		}

Now we just need to handle 'stop', leaving 'start' unchanged.

Thanks.
(Sorry for my English)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants