-
Notifications
You must be signed in to change notification settings - Fork 10
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
Multimethods for mathematical functions #64
Multimethods for mathematical functions #64
Conversation
88cf895
to
c7da055
Compare
unumpy/_multimethods.py
Outdated
diffs = ary[1:] - ary[:-1] | ||
|
||
if to_end is None: | ||
to_end = asarray([], dtype=ary.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.
What's going on here? Concatenating an empty array does nothing.
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.
You are right, I could do the other way around like:
if to_end is not None:
diffs = concatenate([diffs, ravel(to_end)])
Seems better this way!
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.
Append to a list, check for trivial case and return early.
There's a type error arising from the call to |
Yes, just add |
The default for I'm still not sure how to add the |
Instead of clipping # Where we return currently
result = (y0 * x1_dist + y1 * x0_dist) / x_dist
left = fp[0] if left is None else left
right = fp[-1] if right is None else right
result = where(x < xp[0], left, result)
return where(x > xp[-1], right, result) You will need to fixup |
I've pushed the changes to |
NumPy's implementation for |
I'd recommend you first do the calculation as if I'd recommend you replace that line with a concatenate in the appropriate axis with a sub-array in the appropriate axis. A 1D version is below, I'll leave it as an exercise to come up with N-D (hint: tuple with
|
unumpy/_multimethods.py
Outdated
dd = diff(p, axis=axis) | ||
|
||
slice0 = [slice(None)] * nd | ||
slice0[axis] = 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.
You can use slice(0, 1)
to grab the 0 value without also collapsing the dimension. That will avoid the need to use expand_dims
later.
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.
Good idea, that way we avoid an unnecessary multimethod call.
unumpy/_multimethods.py
Outdated
round_ = around | ||
|
||
|
||
def _fix_default(x, out=None): |
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 this is the same as trunc
.
The truncated value of the scalar x is the nearest integer i which is closer to zero than x is.
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.
They seem to be the same, yes. Refactor the default in terms of trunc
then? Something like:
def _fix_default(x, out=None):
x = trunc(x)
if out is None:
return x
else:
copyto(out, x)
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.
Why not just return trunc(x, out=out)
?
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.
You are right! Forgot about the out
kwarg in ufuncs. 😅
We've merged the fix we talked about in the meeting in Quansight-Labs/uarray#246. Pushing an empty commit with |
I think this can be merged now. |
Thanks, @joaosferreira! |
This pull request further adds multimethods that target mathematical functions. I'll name the multimethods added according to the NumPy docs sectioning:
Trigonometric functions
degrees
radians
unwrap
Rounding
around
(and its aliaseround_
)fix
Sums, products, differences
cumprod
cumsum
nancumprod
nancumsum
ediff1d
cross
trapz
Other special functions
i0
sinc
Arithmetic operations
float_power
Handling complex numbers
angle
real
imag
conjugate
Miscellaneous
convolve
clip
nan_to_num
real_if_close
interp
Some thoughts about things to address moving forward with the PR:
diff
has a different signature from the NumPy's function (the latter has more arguments). I'm wondering if these were added after the unumpy's multimethod was written.