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

Implement zero! for arrays #19912

Closed
wants to merge 1 commit into from

Conversation

fredrikekre
Copy link
Member

@fredrikekre fredrikekre commented Jan 7, 2017

Implements zero! to set all elements of an array to 0.

I was really surprised this wasn't a thing already, and since it is a very common thing to do I think it might be nice to have a special version of fill! for this purpose. I think it makes the code very readable and it is obvious what the function does.

julia> A = rand(2,2)
2×2 Array{Float64,2}:
 0.885381  0.234   
 0.943264  0.777748

julia> zero!(A)
2×2 Array{Float64,2}:
 0.0  0.0
 0.0  0.0

(I also really like in-place ! functions 😄 )

Comments?

"""
zero!(x::AbstractArray)

Sets all elements of array `x` to 0. Equivalent to `fill!(x, 0)`.
Copy link
Member

@ararslan ararslan Jan 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically imperative grammar is used when describing how functions act. So in this case it would be "Set all elements..." rather than "Sets all elements..." Should also probably be "the array" rather than just "array".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads pretty clearly to me, tbh.

@stevengj
Copy link
Member

stevengj commented Jan 7, 2017

I'm not sure we need a separate function for this; it's pretty easy to type fill!(A, 0)

@ararslan
Copy link
Member

ararslan commented Jan 7, 2017

@stevengj That's a fair point, though for complete generality wouldn't it need to be something like fill!(A, zero(eltype(A)))? That's more annoying (though not entirely unreasonable).

@stevengj
Copy link
Member

stevengj commented Jan 7, 2017

In most practical applications for this I suspect you will know that you have numbers compatible with 0 and can do x .= 0

@stevengj
Copy link
Member

stevengj commented Jan 7, 2017

Or you will have some other scalar variable y and can do zero(y)

It would be useful to survey the packages to see where filling with zero actually shows up

@fredrikekre
Copy link
Member Author

I'm not saying its difficult to use fill!(x, 0), but rather that it feels like zero is missing its eqivalent in-place version. If people don't think this is worth implementing then feel free to close this PR.

@stevengj
Copy link
Member

stevengj commented Jan 8, 2017

The need for a lot of in-place operations is greatly reduced by the .= assignment syntax (new in 0.5). In fact, we might consider getting rid of fill!(x, v) (in favor of x .= v).

@johnmyleswhite
Copy link
Member

I'm not saying its difficult to use fill!(x, 0), but rather that it feels like zero is missing its eqivalent in-place version.

You mean zeros, not zero, right? zero would be hard to remove, but I wish we would remove zeros at some point (along with trues, falses and ones). Don't think many others agree, though.

@fredrikekre
Copy link
Member Author

You mean zeros, not zero, right?

Well, either one. Here I implemented zero! but could as well have been zeros! I guess. But as pointed out in comments above maybe this is not a good idea after all.

@fredrikekre
Copy link
Member Author

fredrikekre commented Jan 11, 2017

  1. Would this make more sense as an implementation of zeros!(in-place version of zeros) instead of zero! as this PR currently implements? I start to think that would make more sense, since AFAICT zeros is only implemented for arrays. (While zero is used for many types.) eg:
julia> A = rand(2,2)
2×2 Array{Float64,2}:
ze r0.56361   0.904141
 0.723398  0.87581 

julia> zeros!(A)
2×2 Array{Float64,2}:
 0.0  0.0
 0.0  0.0

(It should be noted that both zero(A) and zeros(A) return a zero'd copy of A, so in that perspective both zero! and zeros! would make sense as in-place implementations.)

  1. Do people feel like this specialization of fill! is useful to implement in Base? If not, this PR can be closed.

Thanks for the input!

@JeffBezanson
Copy link
Member

I don't think we need to add this function. I agree with @johnmyleswhite that we should rather go in the direction of minimizing this kind of redundancy. zeros and ones are sort of grandfathered in, but that's a debate for another day.

@fredrikekre
Copy link
Member Author

I guess that settles this then. Thanks for the input!

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

Successfully merging this pull request may close these issues.

6 participants