-
Notifications
You must be signed in to change notification settings - Fork 194
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
rewrite _momentX methods in more functional style #900
base: master
Are you sure you want to change the base?
Conversation
a681d13
to
8cbd195
Compare
I think this is a good change. It requires the minimum Julia version to be 1.6 but I think that is okay. |
s += z * z | ||
end | ||
varcorrection(n, corrected) * s | ||
s = sum(x->abs2(x-m), v, init=zero(m)) |
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 we should use an improved init
value if possible - in many cases the result of sum(...)
won't be of the same type as zero(m)
.
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.
For instance, we could use
s = sum(x->abs2(x-m), v, init=zero(m)) | |
init = (zero(eltype(v)) - zero(m))^2 | |
s = sum(x->(x-m)^2, v; init=init) |
end | ||
|
||
varcorrection(wv, corrected) * s | ||
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m)) |
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.
Similarly, I think we need a different init
here. Additionally, @inbounds
is unsafe here as i
might not be an actual index of wv
.
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.
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m)) | |
init = (zero(eltype(v)) - zero(m))^2 * zero(eltype(wv)) | |
s = sum(i -> (v[i] - m)^2 * wv[i], eachindex(v, wv); init=init) |
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.
@devmotion if v
and wv
do not have the same indices eachindex(v, wv)
will throw and therefore it seems that @inbounds
here should be safe. How about this:
s = sum(i -> (@inbounds abs2(v[i] - m) * wv[i]), eachindex(v), init=zero(m)) | |
s = if isempty(v) | |
(zero(eltype(v)) - zero(m))^2*zero(eltype(wv)) | |
elseif eachindex(v) == eachindex(wv) | |
sum(i -> (@inbounds (v[i] - m)^2 * wv[i]), eachindex(v, wv)) | |
else | |
sum(i -> ((v_ - m)^2 * wv_) for (v_, wv_) in zip(v, wv)) | |
end |
s += z * z * z | ||
end | ||
s / n | ||
s = sum(x->(x-m)^3, v, init=zero(m)) |
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.
Here as well.
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.
s = sum(x->(x-m)^3, v, init=zero(m)) | |
init = (zero(eltype(v)) - zero(m))^3 | |
s = sum(x->(x-m)^3, v; init=init) |
s = sum( | ||
i -> (@inbounds (z = (v[i] - m); z * z * z * wv[i])), | ||
eachindex(v), | ||
init=zero(m), | ||
) |
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.
Same comments as above:
s = sum( | |
i -> (@inbounds (z = (v[i] - m); z * z * z * wv[i])), | |
eachindex(v), | |
init=zero(m), | |
) | |
init = (zero(eltype(v)) - zero(m))^3 * zero(eltype(wv)) | |
s = sum( | |
i -> (v[i] - m)^3 * wv[i], | |
eachindex(v, wv); | |
init=init, | |
) |
s += abs2(z * z) | ||
end | ||
s / n | ||
s = sum(x-> (z = x-m; abs2(z*z)), v, init=zero(m)) |
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.
s = sum(x-> (z = x-m; abs2(z*z)), v, init=zero(m)) | |
init = (zero(eltype(v)) - zero(m))^4 | |
s = sum(x->(x-m)^4, v; init=init) |
s = sum( | ||
i -> (@inbounds (z = (v[i] - m); abs2(z * z) * wv[i])), | ||
eachindex(v), | ||
init=zero(m), | ||
) |
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.
s = sum( | |
i -> (@inbounds (z = (v[i] - m); abs2(z * z) * wv[i])), | |
eachindex(v), | |
init=zero(m), | |
) | |
init = (zero(eltype(v)) - zero(m))^4 * zero(eltype(wv)) | |
s = sum( | |
i -> (v[i] - m)^4 * wv[i], | |
eachindex(v, wv); | |
init=init, | |
) |
s += (z ^ k) | ||
end | ||
s / n | ||
s = sum(x -> (x - m)^k, v, init=zero(m)) |
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.
s = sum(x -> (x - m)^k, v, init=zero(m)) | |
init = (zero(eltype(v)) - zero(m))^k | |
s = sum(x -> (x - m)^k, v; init=init) |
s = sum( | ||
i -> (@inbounds (z = (v[i] - m); z^k * wv[i])), | ||
eachindex(v), | ||
init=zero(m), | ||
) |
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.
s = sum( | |
i -> (@inbounds (z = (v[i] - m); z^k * wv[i])), | |
eachindex(v), | |
init=zero(m), | |
) | |
init = (zero(eltype(v)) - zero(m))^k * zero(eltype(wv)) | |
s = sum( | |
i -> (v[i] - m)^k * wv[i], | |
eachindex(v, wv); | |
init=init, | |
) |
Since it's unspecified whether |
I'd proceed with this version then. let me know what do you think about the suggestion I made above |
depending on the width of vector instruction this is up to 4× faster (N=100), the old implementation matches for k = 6;
@devmotion