-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: allow arrays for start and stop in {lin,log,geom}space #12388
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
Conversation
# Ideally should be `'shape': ()` but the current interface | ||
# does not allow that | ||
return {'shape': (1,), 'typestr': '<i4', 'data': self._data, | ||
return {'shape': (), 'typestr': '<i4', 'data': self._data, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently, the interface is now better, since shape=()
works at least on my python installation.
|
||
# these should not crash | ||
np.histogram([np.array([0.5]) for i in range(10)] + [.500000000000001]) | ||
np.histogram([np.array([0.5]) for i in range(10)] + [.5]) | ||
np.histogram([np.array(0.5) for i in range(10)] + [.500000000000001]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test, as written, made no sense; there is no reason you should be allowed to mix 1-d arrays with scalars, even if they are single-element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I wrote these mostly to test that they didn't blow up in a non-sensical internal crash. The thinking was that this is just doing a histogram on object arrays.
If we could commit to making bool(np.array([1]) < 0)
an error, then this would no longer be allowed either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. But just to be sure: is the change here fine? If we want object arrays, it might be better to just create them explicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm +0.2 on this change. I agree that it could make a lot of sense.
Adding the axis
kwarg would be nice, it would be a simple transpose operation at the end. :)
numpy/core/function_base.py
Outdated
@@ -34,9 +34,9 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): | |||
|
|||
Parameters | |||
---------- | |||
start : scalar | |||
start : scalar or array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would array_like
be better?
numpy/core/function_base.py
Outdated
The starting value of the sequence. | ||
stop : scalar | ||
stop : scalar or array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
I wrote a small PR to add dispatching to these functions: #12471 |
The request for If this is intended for 1.16 it needs a release note. I'd like to branch 1.16.x soon. |
And which failed with the change to linspace.
78cf869
to
099dda6
Compare
@charris - OK, I added the Note that I did not add an |
@@ -54,6 +54,18 @@ def test_basic(self): | |||
y = logspace(0, 6, num=7) | |||
assert_array_equal(y, [1, 10, 100, 1e3, 1e4, 1e5, 1e6]) | |||
|
|||
def test_array(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_start_stop_are_arrays
or some such would be a more descriptive name here and below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was because the test above was just called test_array_scalar
- but really both are unclear, so I changed both now.
Just a suggestion for a name change for the new tests. |
numpy/core/function_base.py
Outdated
@@ -72,7 +72,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): | |||
samples : ndarray | |||
There are `num` equally spaced samples in the closed interval | |||
``[start, stop]`` or the half-open interval ``[start, stop)`` | |||
(depending on whether `endpoint` is True or False). | |||
(depending on whether `endpoint` is True or False). If start or | |||
stop are array-like, then the samples will be on the first axis. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps "will be along a new axis inserted at the beginning" would be more accurate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adopted.
numpy/core/function_base.py
Outdated
start = start.astype(dt, copy=True) | ||
stop = stop.astype(dt, copy=True) | ||
|
||
out_sign = _nx.ones((start + stop).shape, dt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addition here is pretty wasteful to get np.broadcast(start, stop).shape
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and clearer in intent too. Done.
numpy/core/function_base.py
Outdated
start = start + 0j | ||
stop = stop + 0j | ||
if _nx.issubdtype(dt, _nx.complexfloating): | ||
all_imag = (start.real == 0.) & (stop.real == 0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: spell 0.
as 0
to make the two terms consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
raise ValueError('Geometric sequence cannot include zero') | ||
|
||
dt = result_type(start, stop, float(num)) | ||
dt = result_type(start, stop, float(num), _nx.zeros((), dtype)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this behave differently than just passing dtype
in directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dtype
can be None
- I actually tried thinking of a quicker way.
099dda6
to
58ebb6a
Compare
OK, thanks. Adopted the suggestions. |
I really do this we should add an Note: from an implementation perspective, you could do this just by adding |
OK, I'll do it - I was trying to avoid the extra work of also writing tests... I guess little choice but to put it at the end of the argument list? |
Now added the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor doc question.
@hameerabbasi - good point. I also added a |
Thanks Marten. |
As discussed on the mailing list, perhaps it would be good for
linspace
to allowstart
andstop
to be arrays, which are broadcast against each other. For now, this is just a proof of concept - which presumes the final axis that is counted is the first one. With it, two tests failed, which seem both false positives (in that the tests were not written very well, in one case with a note stating that).EDIT: TODO from mailing: add
axis
argument (probably with0
as a default; should be axis in output, so-1
is last). Could be simple transpose at end, or use reshaping of inputs.Anyway, with this: