Skip to content

Commit

Permalink
Deprecate eye.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Nov 19, 2017
1 parent 9f6f80f commit 6a763af
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 103 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ Deprecated or removed
* `fill!(A::Diagonal, x)` and `fill!(A::AbstractTriangular, x)` have been deprecated
in favor of `Base.LinAlg.fillslots!(A, x)` ([#24413]).

* `eye` has been deprecated in favor of `I` and `Matrix` constructors. Please see the
deprecation warnings for replacement details ([#24438]).

* Using Bool values directly as indices is now deprecated and will be an error in the future. Convert
them to `Int` before indexing if you intend to access index `1` for `true` and `0` for `false`.

Expand Down
85 changes: 0 additions & 85 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,91 +432,6 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one))
end
end

"""
eye([T::Type=Float64,] m::Integer, n::Integer)
`m`-by-`n` identity matrix.
The default element type is [`Float64`](@ref).
# Examples
```jldoctest
julia> eye(3, 4)
3×4 Array{Float64,2}:
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
julia> eye(2, 2)
2×2 Array{Float64,2}:
1.0 0.0
0.0 1.0
julia> eye(Int, 2, 2)
2×2 Array{Int64,2}:
1 0
0 1
```
"""
function eye(::Type{T}, m::Integer, n::Integer) where T
a = zeros(T,m,n)
for i = 1:min(m,n)
a[i,i] = oneunit(T)
end
return a
end

"""
eye(m, n)
`m`-by-`n` identity matrix.
"""
eye(m::Integer, n::Integer) = eye(Float64, m, n)
eye(::Type{T}, n::Integer) where {T} = eye(T, n, n)
"""
eye([T::Type=Float64,] n::Integer)
`n`-by-`n` identity matrix.
The default element type is [`Float64`](@ref).
# Examples
```jldoctest
julia> eye(Int, 2)
2×2 Array{Int64,2}:
1 0
0 1
julia> eye(2)
2×2 Array{Float64,2}:
1.0 0.0
0.0 1.0
```
"""
eye(n::Integer) = eye(Float64, n)

"""
eye(A)
Constructs an identity matrix of the same dimensions and type as `A`.
# Examples
```jldoctest
julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> eye(A)
3×3 Array{Int64,2}:
1 0 0
0 1 0
0 0 1
```
Note the difference from [`ones`](@ref).
"""
eye(x::AbstractMatrix{T}) where {T} = eye(typeof(one(T)), size(x, 1), size(x, 2))

function _one(unit::T, x::AbstractMatrix) where T
m,n = size(x)
m==n || throw(DimensionMismatch("multiplicative identity defined only for square matrices"))
Expand Down
48 changes: 47 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,53 @@ end
@deprecate get_creds!(cache::CachedCredentials, credid, default) get!(cache, credid, default)
end

@deprecate eye(::Type{Diagonal{T}}, n::Int) where {T} Diagonal{T}(I, n)
## goodbeye, eye!
export eye
function eye(m::Integer)
depwarn(string("`eye(m::Integer)` has been deprecated in favor of `I` and `Matrix` ",
"constructors. For a direct replacement, consider `Matrix(1.0I, m, m)` or ",
"`Matrix{Float64}(I, m, m)`. If `Float64` element type is not necessary, ",
"consider the shorter `Matrix(I, m, m)` (with default `eltype(I)` `Bool`)."), :eye)
return Matrix{Float64}(I, m, m)
end
function eye(::Type{T}, m::Integer) where T
depwarn(string("`eye(T::Type, m::Integer)` has been deprecated in favor of `I` and ",
"`Matrix` constructors. For a direct replacement, consider `Matrix{T}(I, m, m)`. If ",
"`T` element type is not necessary, consider the shorter `Matrix(I, m, m)`",
"(with default `eltype(I)` `Bool`)"), :eye)
return Matrix{T}(I, m, m)
end
function eye(m::Integer, n::Integer)
depwarn(string("`eye(m::Integer, n::Integer)` has been deprecated in favor of `I` and ",
"`Matrix` constructors. For a direct replacement, consider `Matrix(1.0I, m, n)` ",
"or `Matrix{Float64}(I, m, n)`. If `Float64` element type is not necessary, ",
"consider the shorter `Matrix(I, m, n)` (with default `eltype(I)` `Bool`)."), :eye)
return Matrix{Float64}(I, m, n)
end
function eye(::Type{T}, m::Integer, n::Integer) where T
depwarn(string("`eye(T::Type, m::Integer, n::Integer)` has been deprecated in favor of ",
"`I` and `Matrix` constructors. For a direct replacement, consider `Matrix{T}(I, m, n)`.",
"If `T` element type is not necessary, consider the shorter `Matrix(I, m, n)` ",
"(with default `eltype(I)` `Bool`)."), :eye)
return Matrix{T}(I, m, n)
end
function eye(A::AbstractMatrix{T}) where T
depwarn(string("`eye(A::AbstractMatrix{T})` has been deprecated in favor of `I` and ",
"`Matrix` constructors. For a direct replacement, consider `Matrix{eltype(A)}(I, size(A))`.",
"If `eltype(A)` element type is not necessary, consider the shorter `Matrix(I, size(A))` ",
"(with default `eltype(I)` `Bool`)."), :eye)
return Matrix(one(T)I, size(A))
end
function eye(::Type{Diagonal{T}}, n::Int) where T
depwarn(string("`eye(DT::Type{Diagonal{T}}, n::Int)` has been deprecated in favor of `I` ",
"and `Diagonal` constructors. For a direct replacement, consider `Diagonal{T}(I, n)`. ",
"If `T` element type is not necessary, consider the shorter `Diagonal(I, n)` ",
"(with default `eltype(I)` `Bool`)."), :eye)
return Diagonal{T}(I, n)
end
@eval Base.LinAlg import Base.eye
# @eval Base.SparseArrays import Base.eye # SparseArrays has an eye for things cholmod


export tic, toq, toc
function tic()
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ export
eigvals,
eigvals!,
eigvecs,
eye,
factorize,
givens,
hessfact!,
Expand Down
3 changes: 1 addition & 2 deletions base/linalg/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Base: A_mul_Bt, At_ldiv_Bt, A_rdiv_Bc, At_ldiv_B, Ac_mul_Bc, A_mul_Bc, Ac
Ac_ldiv_B, Ac_ldiv_Bc, At_mul_Bt, A_rdiv_Bt, At_mul_B
import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, asec, asech, asin,
asinh, atan, atanh, big, broadcast, ceil, conj, convert, copy, copy!, cos, cosh, cot, coth, csc,
csch, eltype, exp, eye, findmax, findmin, fill!, floor, getindex, hcat, imag, indices,
csch, eltype, exp, findmax, findmin, fill!, floor, getindex, hcat, imag, indices,
inv, isapprox, isone, IndexStyle, kron, length, log, map, ndims, oneunit, parent,
power_by_squaring, print_matrix, promote_rule, real, round, sec, sech, setindex!, show, similar,
sin, sincos, sinh, size, sqrt, tan, tanh, transpose, trunc, typed_hcat, vec
Expand Down Expand Up @@ -87,7 +87,6 @@ export
eigvals,
eigvals!,
eigvecs,
eye,
factorize,
givens,
hessfact,
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Base.LinAlg: At_ldiv_B!, Ac_ldiv_B!, A_rdiv_B!, A_rdiv_Bc!
import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
atan, atand, atanh, broadcast!, chol, conj!, cos, cosc, cosd, cosh, cospi, cot,
cotd, coth, count, csc, cscd, csch, adjoint!, diag, diff, done, dot, eig,
exp10, exp2, eye, findn, floor, hash, indmin, inv, issymmetric, istril, istriu,
exp10, exp2, findn, floor, hash, indmin, inv, issymmetric, istril, istriu,
log10, log2, lu, next, sec, secd, sech, show, sin,
sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,
tand, tanh, trace, transpose!, tril!, triu!, trunc, vecnorm, abs, abs2,
Expand Down
17 changes: 5 additions & 12 deletions doc/src/manual/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ omitted it will default to [`Float64`](@ref).
| [`reinterpret(T, A)`](@ref) | an array with the same binary data as `A`, but with element type `T` |
| [`rand(T, dims...)`](@ref) | an `Array` with random, iid [^1] and uniformly distributed values in the half-open interval ``[0, 1)`` |
| [`randn(T, dims...)`](@ref) | an `Array` with random, iid and standard normally distributed values |
| [`eye(T, n)`](@ref) | `n`-by-`n` identity matrix |
| [`eye(T, m, n)`](@ref) | `m`-by-`n` identity matrix |
| [`Matrix{T}(I, m, n)`](@ref) | `m`-by-`n` identity matrix |
| [`linspace(start, stop, n)`](@ref) | range of `n` linearly spaced elements from `start` to `stop` |
| [`fill!(A, x)`](@ref) | fill the array `A` with the value `x` |
| [`fill(x, dims...)`](@ref) | an `Array` filled with the value `x` |
Expand Down Expand Up @@ -782,18 +781,12 @@ stored zeros. (See [Sparse Matrix Storage](@ref man-csc).).
### Sparse Vector and Matrix Constructors

The simplest way to create sparse arrays is to use functions equivalent to the [`zeros`](@ref)
and [`eye`](@ref) functions that Julia provides for working with dense arrays. To produce
sparse arrays instead, you can use the same names with an `sp` prefix:
function that Julia provides for working with dense arrays. To produce
a sparse array instead, you can use the same names with an `sp` prefix:

```jldoctest
julia> spzeros(3)
3-element SparseVector{Float64,Int64} with 0 stored entries
julia> speye(3,5)
3×5 SparseMatrixCSC{Float64,Int64} with 3 stored entries:
[1, 1] = 1.0
[2, 2] = 1.0
[3, 3] = 1.0
```

The [`sparse`](@ref) function is often a handy way to construct sparse arrays. For
Expand Down Expand Up @@ -849,7 +842,7 @@ Another way to create a sparse array is to convert a dense array into a sparse a
the [`sparse`](@ref) function:

```jldoctest
julia> sparse(eye(5))
julia> sparse(Matrix(1.0I, 5, 5))
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
[1, 1] = 1.0
[2, 2] = 1.0
Expand Down Expand Up @@ -895,7 +888,7 @@ section of the standard library reference.
|:-------------------------- |:---------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`spzeros(m,n)`](@ref) | [`zeros(m,n)`](@ref) | Creates a *m*-by-*n* matrix of zeros. ([`spzeros(m,n)`](@ref) is empty.) |
| [`spones(S)`](@ref) | [`ones(m,n)`](@ref) | Creates a matrix filled with ones. Unlike the dense version, [`spones`](@ref) has the same sparsity pattern as *S*. |
| [`speye(n)`](@ref) | [`eye(n)`](@ref) | Creates a *n*-by-*n* identity matrix. |
| [`speye(n)`](@ref) | [`Matrix(I,n,n)`](@ref)| Creates a *n*-by-*n* identity matrix. |
| [`Array(S)`](@ref) | [`sparse(A)`](@ref) | Interconverts between dense and sparse formats. |
| [`sprand(m,n,d)`](@ref) | [`rand(m,n)`](@ref) | Creates a *m*-by-*n* random matrix (of density *d*) with iid non-zero elements distributed uniformly on the half-open interval ``[0, 1)``. |
| [`sprandn(m,n,d)`](@ref) | [`randn(m,n)`](@ref) | Creates a *m*-by-*n* random matrix (of density *d*) with iid non-zero elements distributed according to the standard normal (Gaussian) distribution. |
Expand Down
1 change: 0 additions & 1 deletion doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Base.fill
Base.fill!
Base.similar(::AbstractArray)
Base.similar(::Any, ::Tuple)
Base.eye
Base.linspace
Base.logspace
Base.Random.randsubseq
Expand Down

0 comments on commit 6a763af

Please sign in to comment.