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
add np.sign & np.abs #6
Conversation
Hello. I think you’re confusing what The second part is due to the product rule where the input is a function of the input. So you apply the chain rule |
What you’re suggesting is true for |
Sorry for that. I can't guess the usage of the two functions and I should open an issue for this before the pr. I will try to complete the register_diff(np.exp2, lambda x: x[1] * np.log(2) * np.exp(x[0])) It should be: register_diff(np.exp2, lambda x: x[1] * np.log(2) * np.power(2, x[0])) |
Ah, yes. That’s a mistake on my part, same for |
Hi, when I complete the register_diff(np.sign, lambda x: np.broadcast_to(np.where(x[0] == 0, float('nan'), 0), x[1].shape))
register_diff(np.absolute, lambda x: x[1] * np.where(np.sign(x[0]) == 0, float('nan'), np.sign(x[0]))) The diff of np.abs is fine. But the diff of PS F:\OneDrive - sjtu.edu.cn\Project\GSOC\udiff\src> python .\test.py
<DiffArray, name=unbound, arr=
[2. 3. 0.]
>
<DiffArray, name=unbound, arr=
[ 1. -1. nan]
>
Traceback (most recent call last):
File ".\test.py", line 12, in <module>
y = np.sign(x)
File "F:\OneDrive - sjtu.edu.cn\Project\GSOC\udiff\src\udiff\_uarray_plug.py", line 65, in __ua_function__
diff_arr = global_registry[a[0]](*a[1:], **kw)
File "F:\OneDrive - sjtu.edu.cn\Project\GSOC\udiff\src\udiff\_builtin_diffs.py", line 32, in <lambda>
register_diff(np.sign, lambda x: np.broadcast_to(np.where(x[0] == 0, float('nan'), 0), x[1].shape))
File "D:\Python\lib\site-packages\unumpy\_multimethods.py", line 105, in f
return globals()[name](self, other)
File "F:\OneDrive - sjtu.edu.cn\Project\GSOC\udiff\src\udiff\_uarray_plug.py", line 65, in __ua_function__
diff_arr = global_registry[a[0]](*a[1:], **kw)
KeyError: <ufunc 'equal'> But I don't know how to complete the diff of |
Hmm. Try |
Thx, the pr have committed. |
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.
One small comment, then I think this is good to go.
src/udiff/_builtin_diffs.py
Outdated
register_diff(np.negative, lambda x: -x[1]) | ||
register_diff(np.conj, lambda x: np.conj(x[1])) | ||
register_diff(np.absolute, lambda x: x[1] * np.where(np.sign(x[0]) == 0, float('nan'), np.sign(x[0]))) | ||
register_diff(np.positive, lambda x: x[1] * np.sign(x[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.
Can you revert these two lines?
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 you mean the np.negative
and np.positive
?
Thanks @sangyx. This is in! |
Hi, I found a bug in np.positive and np.negative. The diff calculate in the source is incorrect:
If we suppose that
x = [1, -1]
, then they = np.positive(x)=[1, 1]
. We can see thaty = x
if x > 0 andy=-x
if x < 0. So the diff of y should bey=sign(x)=[1, -1]