diff --git a/NEWS.md b/NEWS.md index 495bc1015ea935..83134151a8317a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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`. diff --git a/base/array.jl b/base/array.jl index 35ffbfff79dcd2..d3e1afd56c2d0e 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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")) diff --git a/base/deprecated.jl b/base/deprecated.jl index 4c3915ae464479..d7137cf5411dfb 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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() diff --git a/base/exports.jl b/base/exports.jl index 84e7e3ed29790d..bed2956c884200 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -560,7 +560,6 @@ export eigvals, eigvals!, eigvecs, - eye, factorize, givens, hessfact!, diff --git a/base/linalg/linalg.jl b/base/linalg/linalg.jl index 410d81e14a8b43..a46682c8f354e2 100644 --- a/base/linalg/linalg.jl +++ b/base/linalg/linalg.jl @@ -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 @@ -87,7 +87,6 @@ export eigvals, eigvals!, eigvecs, - eye, factorize, givens, hessfact, diff --git a/base/sparse/sparse.jl b/base/sparse/sparse.jl index 4bffa183a8fbb9..e06d001538f4b4 100644 --- a/base/sparse/sparse.jl +++ b/base/sparse/sparse.jl @@ -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, diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index 0bc4a53a53f8fb..5352af4d55f15f 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -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` | @@ -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 @@ -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 @@ -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. | diff --git a/doc/src/stdlib/arrays.md b/doc/src/stdlib/arrays.md index d00334b3cf1a09..f71248cc4f44ac 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -24,7 +24,6 @@ Base.fill Base.fill! Base.similar(::AbstractArray) Base.similar(::Any, ::Tuple) -Base.eye Base.linspace Base.logspace Base.Random.randsubseq