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

rewrite all calls to ones(...) #25087

Merged
merged 1 commit into from
Jan 4, 2018
Merged

Conversation

fredrikekre
Copy link
Member

@fredrikekre fredrikekre commented Dec 14, 2017

Some joint work between me and @Sacha0. I have to say, rewriting all of the ones call to fill is a real pleasure. I was skeptic at first, but now I love fill just as much as @Sacha0.

Please read JuliaLang/LinearAlgebra.jl#484 and #24595 and the linked discussion therein.

This PR has also been rebased for hours through the large changes of eye, and uninitialized array constructors, so it better get merged! 😄

@fredrikekre fredrikekre added arrays [a, r, r, a, y, s] deprecation This change introduces or involves a deprecation labels Dec 14, 2017
@fredrikekre fredrikekre requested a review from Sacha0 December 14, 2017 23:22
@Sacha0
Copy link
Member

Sacha0 commented Dec 14, 2017

I was skeptic at first, but now I love fill just as much as @Sacha0.

Oh gosh 😂

@Sacha0
Copy link
Member

Sacha0 commented Dec 14, 2017

Thanks for picking this work up @fredrikekre! :) Please feel welcome to squash out / remove / * my early commits as you see fit.

@fredrikekre fredrikekre force-pushed the reones branch 4 times, most recently from 8037c99 to 7da6987 Compare December 18, 2017 12:15
NEWS.md Outdated
@@ -563,6 +563,11 @@ Deprecated or removed
and/or shape depending on `opts...`. For an algebraic multiplicative identity,
consider `one(A)` ([#24656]).

* `ones(dims...)` `and ones(T, dims)` have been deprecated. The replacements are
Copy link
Member

@Sacha0 Sacha0 Dec 18, 2017

Choose a reason for hiding this comment

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

"and ones(T, dims)" -> "and ones(T, dims)?

NEWS.md Outdated
@@ -563,6 +563,11 @@ Deprecated or removed
and/or shape depending on `opts...`. For an algebraic multiplicative identity,
consider `one(A)` ([#24656]).

* `ones(dims...)` `and ones(T, dims)` have been deprecated. The replacements are
`fill(1.0, dims...)` and `fill(one(T), dims...)`, but other common patterns, such as
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps mention using literals also for other common types, e.g. fill(1, dims...) for Ints, fill(1f0, dims...) for Float32, e.g. fill(1+0im, dims...) for complex types, fill(0x1, dims...) for UInt8s, e.g. fill(1m, dims...) for unitful types, et cetera? And perhaps fill(T(1), dims...) as another alternative alongside fill(one(T), dims...)?

base/array.jl Outdated
@@ -343,7 +343,7 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(uninitialized, dims...), v)
zeros([T=Float64,] dims...)

Create an `Array`, with element type `T`, of all zeros with size specified by `dims`.
See also [`ones`](@ref), [`similar`](@ref).
See also [`similar`](@ref).
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps add a reference to fill?

xmin = ones(T, 1)
xmax = ones(T, 1)
xmin = fill(one(T), 1)
xmax = fill(one(T), 1)
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps vector literals?

@@ -211,7 +211,7 @@ For users coming to Julia from R, these are some noteworthy differences:
by default. To get optimal performance when looping over arrays, the order of the loops should
be reversed in Julia relative to NumPy (see relevant section of [Performance Tips](@ref man-performance-tips)).
* Julia's updating operators (e.g. `+=`, `-=`, ...) are *not in-place* whereas NumPy's are. This
means `A = ones(4); B = A; B += 3` doesn't change values in `A`, it rather rebinds the name `B`
means `A = fill(1, 4); B = A; B .+= 3` doesn't change values in `A`, it rather rebinds the name `B`
Copy link
Member

Choose a reason for hiding this comment

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

Good catch re. +=!

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I hadn't read the rest of the sentence :). += is indeed correct, rather than .+=?

Copy link
Member Author

@fredrikekre fredrikekre Jan 3, 2018

Choose a reason for hiding this comment

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

Right, the reason I changed was that vector + scalar is deprecated, so perhaps we should just rewrite the example completely.
Edit: Done.

Copy link
Member

Choose a reason for hiding this comment

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

Perfect! :)

global mrefs = TestSerCnt(ones(10))
@test remotecall_fetch(()->(mrefs.v, 2*mrefs.v, 3*mrefs.v), id_other) == (ones(10), 2*ones(10), 3*ones(10))
global mrefs = TestSerCnt(fill(1.,10))
@test remotecall_fetch(()->(mrefs.v, 2*mrefs.v, 3*mrefs.v), id_other) == (fill(1.,10), fill(2.,10), fill(3.,10))
Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -1180,7 +1179,7 @@ end
X = [ i+2j for i=1:5, j=1:5 ]
@test X[2,3] == 8
@test X[4,5] == 14
@test isequal(ones(2,3) * ones(2,3)', [3. 3.; 3. 3.])
@test isequal(fill(3.,2,2), [3. 3.; 3. 3.])
Copy link
Member

Choose a reason for hiding this comment

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

I wonder why this test exists under the heading "comprehensions" and what purpose it serves :).

@fredrikekre fredrikekre force-pushed the reones branch 3 times, most recently from 8cf78a2 to 6db0c9b Compare December 21, 2017 17:12
@Sacha0 Sacha0 added the triage This should be discussed on a triage call label Dec 21, 2017
@@ -31,7 +31,7 @@ lengths of dimensions you asked for.

# Examples
```jldoctest
julia> A = ones(2,3,4);
julia> A = fill(1,2,3,4);
Copy link
Member

Choose a reason for hiding this comment

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

fill(1,2,3,4) -> fill(1, (2,3,4)) for these examples? :)

@StefanKarpinski
Copy link
Member

Sure, give it a shot. We'll see how much kicking and screaming there is.

@JeffBezanson
Copy link
Member

JeffBezanson commented Dec 28, 2017

Triage likes replacing the uses of ones in Base code and documentation, but is less sure about the deprecation. Let's at least get that commit merged.

@fredrikekre
Copy link
Member Author

Rebased and dropped the last commit for later.

@fredrikekre fredrikekre force-pushed the reones branch 3 times, most recently from eec1872 to 273c6c3 Compare January 1, 2018 00:15
@fredrikekre fredrikekre changed the title use fill (aka deprecate ones) rewrite all calls to ones(...) Jan 2, 2018
@@ -343,7 +343,7 @@ fill(v, dims::Integer...) = fill!(Array{typeof(v)}(uninitialized, dims...), v)
zeros([T=Float64,] dims...)

Create an `Array`, with element type `T`, of all zeros with size specified by `dims`.
See also [`ones`](@ref), [`similar`](@ref).
See also [`fill`](@ref), [`ones`](@ref).
Copy link
Member

@Sacha0 Sacha0 Jan 3, 2018

Choose a reason for hiding this comment

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

Why ref fill and ones (rather than fill and similar)?

Copy link
Member Author

Choose a reason for hiding this comment

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

The reference to similar here should have been removed as part of #24656/#24863. I assume the reason we referenced it here was because previously we had both zeros(A) and similar(A) doing similar things for A::AbstractArray.

@@ -363,7 +363,7 @@ function zeros end
ones([T=Float64,] dims...)

Create an `Array`, with element type `T`, of all ones with size specified by `dims`.
See also [`zeros`](@ref), [`similar`](@ref).
See also: [`fill`](@ref), [`zeros`](@ref).
Copy link
Member

Choose a reason for hiding this comment

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

Similarly here, why ref fill and zeros (rather than fill and similar)?

Copy link
Member Author

Choose a reason for hiding this comment

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

See above.

x = fill(1., 5)
@test cholfact(sparse(Float16(1)I, 5, 5))\x == x
@test cholfact(Symmetric(sparse(Float16(1)I, 5, 5)))\x == x
@test cholfact(Hermitian(sparse(Complex{Float16}(1)I, 5, 5)))\x == fill(1.0+0im, 5)
Copy link
Member

Choose a reason for hiding this comment

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

Could simplify the RHS to x I think?

@fredrikekre fredrikekre force-pushed the reones branch 2 times, most recently from f67d64b to 2065aa7 Compare January 3, 2018 06:59
Copy link
Member

@Sacha0 Sacha0 left a comment

Choose a reason for hiding this comment

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

Looks great! Much thanks for seeing this work through @fredrikekre! :)

@fredrikekre
Copy link
Member Author

Linking #25392 (comment) but with zeros replace with ones.

@JeffBezanson JeffBezanson merged commit b5d4f3c into JuliaLang:master Jan 4, 2018
@JeffBezanson JeffBezanson removed the triage This should be discussed on a triage call label Jan 4, 2018
@fredrikekre fredrikekre deleted the reones branch January 4, 2018 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] deprecation This change introduces or involves a deprecation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants