Skip to content

Commit 7d86a5b

Browse files
committed
Deprecate eye.
1 parent e28d1d5 commit 7d86a5b

File tree

8 files changed

+57
-103
lines changed

8 files changed

+57
-103
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ Deprecated or removed
398398
* `fill!(A::Diagonal, x)` and `fill!(A::AbstractTriangular, x)` have been deprecated
399399
in favor of `Base.LinAlg.fillslots!(A, x)` ([#24413]).
400400

401+
* `eye` has been deprecated in favor of `I` and `Matrix` constructors. Please see the
402+
deprecation warnings for replacement details ([#24438]).
403+
401404
* Using Bool values directly as indices is now deprecated and will be an error in the future. Convert
402405
them to `Int` before indexing if you intend to access index `1` for `true` and `0` for `false`.
403406

base/array.jl

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -432,91 +432,6 @@ for (fname, felt) in ((:zeros, :zero), (:ones, :one))
432432
end
433433
end
434434

435-
"""
436-
eye([T::Type=Float64,] m::Integer, n::Integer)
437-
438-
`m`-by-`n` identity matrix.
439-
The default element type is [`Float64`](@ref).
440-
441-
# Examples
442-
```jldoctest
443-
julia> eye(3, 4)
444-
3×4 Array{Float64,2}:
445-
1.0 0.0 0.0 0.0
446-
0.0 1.0 0.0 0.0
447-
0.0 0.0 1.0 0.0
448-
449-
julia> eye(2, 2)
450-
2×2 Array{Float64,2}:
451-
1.0 0.0
452-
0.0 1.0
453-
454-
julia> eye(Int, 2, 2)
455-
2×2 Array{Int64,2}:
456-
1 0
457-
0 1
458-
```
459-
"""
460-
function eye(::Type{T}, m::Integer, n::Integer) where T
461-
a = zeros(T,m,n)
462-
for i = 1:min(m,n)
463-
a[i,i] = oneunit(T)
464-
end
465-
return a
466-
end
467-
468-
"""
469-
eye(m, n)
470-
471-
`m`-by-`n` identity matrix.
472-
"""
473-
eye(m::Integer, n::Integer) = eye(Float64, m, n)
474-
eye(::Type{T}, n::Integer) where {T} = eye(T, n, n)
475-
"""
476-
eye([T::Type=Float64,] n::Integer)
477-
478-
`n`-by-`n` identity matrix.
479-
The default element type is [`Float64`](@ref).
480-
481-
# Examples
482-
```jldoctest
483-
julia> eye(Int, 2)
484-
2×2 Array{Int64,2}:
485-
1 0
486-
0 1
487-
488-
julia> eye(2)
489-
2×2 Array{Float64,2}:
490-
1.0 0.0
491-
0.0 1.0
492-
```
493-
"""
494-
eye(n::Integer) = eye(Float64, n)
495-
496-
"""
497-
eye(A)
498-
499-
Constructs an identity matrix of the same dimensions and type as `A`.
500-
501-
# Examples
502-
```jldoctest
503-
julia> A = [1 2 3; 4 5 6; 7 8 9]
504-
3×3 Array{Int64,2}:
505-
1 2 3
506-
4 5 6
507-
7 8 9
508-
509-
julia> eye(A)
510-
3×3 Array{Int64,2}:
511-
1 0 0
512-
0 1 0
513-
0 0 1
514-
```
515-
516-
Note the difference from [`ones`](@ref).
517-
"""
518-
eye(x::AbstractMatrix{T}) where {T} = eye(typeof(one(T)), size(x, 1), size(x, 2))
519-
520435
function _one(unit::T, x::AbstractMatrix) where T
521436
m,n = size(x)
522437
m==n || throw(DimensionMismatch("multiplicative identity defined only for square matrices"))

base/deprecated.jl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,53 @@ end
17921792
@deprecate get_creds!(cache::CachedCredentials, credid, default) get!(cache, credid, default)
17931793
end
17941794

1795-
@deprecate eye(::Type{Diagonal{T}}, n::Int) where {T} Diagonal{T}(I, n)
1795+
## goodbeye, eye!
1796+
export eye
1797+
function eye(m::Integer)
1798+
depwarn(string("`eye(m::Integer)` has been deprecated in favor of `I` and `Matrix` ",
1799+
"constructors. For a direct replacement, consider `Matrix(1.0I, m, m)` or ",
1800+
"`Matrix{Float64}(I, m, m)`. If `Float64` element type is not necessary, ",
1801+
"consider the shorter `Matrix(I, m, m)` (with default `eltype(I)` `Bool`)."), :eye)
1802+
return Matrix{Float64}(I, m, m)
1803+
end
1804+
function eye(::Type{T}, m::Integer) where T
1805+
depwarn(string("`eye(T::Type, m::Integer)` has been deprecated in favor of `I` and ",
1806+
"`Matrix` constructors. For a direct replacement, consider `Matrix{T}(I, m, m)`. If ",
1807+
"`T` element type is not necessary, consider the shorter `Matrix(I, m, m)`",
1808+
"(with default `eltype(I)` `Bool`)"), :eye)
1809+
return Matrix{T}(I, m, m)
1810+
end
1811+
function eye(m::Integer, n::Integer)
1812+
depwarn(string("`eye(m::Integer, n::Integer)` has been deprecated in favor of `I` and ",
1813+
"`Matrix` constructors. For a direct replacement, consider `Matrix(1.0I, m, n)` ",
1814+
"or `Matrix{Float64}(I, m, n)`. If `Float64` element type is not necessary, ",
1815+
"consider the shorter `Matrix(I, m, n)` (with default `eltype(I)` `Bool`)."), :eye)
1816+
return Matrix{Float64}(I, m, n)
1817+
end
1818+
function eye(::Type{T}, m::Integer, n::Integer) where T
1819+
depwarn(string("`eye(T::Type, m::Integer, n::Integer)` has been deprecated in favor of ",
1820+
"`I` and `Matrix` constructors. For a direct replacement, consider `Matrix{T}(I, m, n)`.",
1821+
"If `T` element type is not necessary, consider the shorter `Matrix(I, m, n)` ",
1822+
"(with default `eltype(I)` `Bool`)."), :eye)
1823+
return Matrix{T}(I, m, n)
1824+
end
1825+
function eye(A::AbstractMatrix{T}) where T
1826+
depwarn(string("`eye(A::AbstractMatrix{T})` has been deprecated in favor of `I` and ",
1827+
"`Matrix` constructors. For a direct replacement, consider `Matrix{eltype(A)}(I, size(A))`.",
1828+
"If `eltype(A)` element type is not necessary, consider the shorter `Matrix(I, size(A))` ",
1829+
"(with default `eltype(I)` `Bool`)."), :eye)
1830+
return Matrix(one(T)I, size(A))
1831+
end
1832+
function eye(::Type{Diagonal{T}}, n::Int) where T
1833+
depwarn(string("`eye(DT::Type{Diagonal{T}}, n::Int)` has been deprecated in favor of `I` ",
1834+
"and `Diagonal` constructors. For a direct replacement, consider `Diagonal{T}(I, n)`. ",
1835+
"If `T` element type is not necessary, consider the shorter `Diagonal(I, n)` ",
1836+
"(with default `eltype(I)` `Bool`)."), :eye)
1837+
return Diagonal{T}(I, n)
1838+
end
1839+
@eval Base.LinAlg import Base.eye
1840+
@eval Base.SparseArrays import Base.eye
1841+
17961842

17971843
export tic, toq, toc
17981844
function tic()

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ export
560560
eigvals,
561561
eigvals!,
562562
eigvecs,
563-
eye,
564563
factorize,
565564
givens,
566565
hessfact!,

base/linalg/linalg.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1212
Ac_ldiv_B, Ac_ldiv_Bc, At_mul_Bt, A_rdiv_Bt, At_mul_B
1313
import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, asec, asech, asin,
1414
asinh, atan, atanh, big, broadcast, ceil, conj, convert, copy, copy!, cos, cosh, cot, coth, csc,
15-
csch, eltype, exp, eye, findmax, findmin, fill!, floor, getindex, hcat, imag, indices,
15+
csch, eltype, exp, findmax, findmin, fill!, floor, getindex, hcat, imag, indices,
1616
inv, isapprox, isone, IndexStyle, kron, length, log, map, ndims, oneunit, parent,
1717
power_by_squaring, print_matrix, promote_rule, real, round, sec, sech, setindex!, show, similar,
1818
sin, sincos, sinh, size, sqrt, tan, tanh, transpose, trunc, typed_hcat, vec
@@ -87,7 +87,6 @@ export
8787
eigvals,
8888
eigvals!,
8989
eigvecs,
90-
eye,
9190
factorize,
9291
givens,
9392
hessfact,

base/sparse/sparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Base.LinAlg: At_ldiv_B!, Ac_ldiv_B!, A_rdiv_B!, A_rdiv_Bc!
1818
import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
1919
atan, atand, atanh, broadcast!, chol, conj!, cos, cosc, cosd, cosh, cospi, cot,
2020
cotd, coth, count, csc, cscd, csch, adjoint!, diag, diff, done, dot, eig,
21-
exp10, exp2, eye, findn, floor, hash, indmin, inv, issymmetric, istril, istriu,
21+
exp10, exp2, findn, floor, hash, indmin, inv, issymmetric, istril, istriu,
2222
log10, log2, lu, next, sec, secd, sech, show, sin,
2323
sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,
2424
tand, tanh, trace, transpose!, tril!, triu!, trunc, vecnorm, abs, abs2,

doc/src/manual/arrays.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ omitted it will default to [`Float64`](@ref).
6666
| [`reinterpret(T, A)`](@ref) | an array with the same binary data as `A`, but with element type `T` |
6767
| [`rand(T, dims...)`](@ref) | an `Array` with random, iid [^1] and uniformly distributed values in the half-open interval ``[0, 1)`` |
6868
| [`randn(T, dims...)`](@ref) | an `Array` with random, iid and standard normally distributed values |
69-
| [`eye(T, n)`](@ref) | `n`-by-`n` identity matrix |
70-
| [`eye(T, m, n)`](@ref) | `m`-by-`n` identity matrix |
69+
| [`Matrix{T}(I, m, n)`](@ref) | `m`-by-`n` identity matrix |
7170
| [`linspace(start, stop, n)`](@ref) | range of `n` linearly spaced elements from `start` to `stop` |
7271
| [`fill!(A, x)`](@ref) | fill the array `A` with the value `x` |
7372
| [`fill(x, dims...)`](@ref) | an `Array` filled with the value `x` |
@@ -782,18 +781,12 @@ stored zeros. (See [Sparse Matrix Storage](@ref man-csc).).
782781
### Sparse Vector and Matrix Constructors
783782

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

788787
```jldoctest
789788
julia> spzeros(3)
790789
3-element SparseVector{Float64,Int64} with 0 stored entries
791-
792-
julia> speye(3,5)
793-
3×5 SparseMatrixCSC{Float64,Int64} with 3 stored entries:
794-
[1, 1] = 1.0
795-
[2, 2] = 1.0
796-
[3, 3] = 1.0
797790
```
798791

799792
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
849842
the [`sparse`](@ref) function:
850843

851844
```jldoctest
852-
julia> sparse(eye(5))
845+
julia> sparse(Matrix(1.0I, 5, 5))
853846
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
854847
[1, 1] = 1.0
855848
[2, 2] = 1.0
@@ -895,7 +888,7 @@ section of the standard library reference.
895888
|:-------------------------- |:---------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
896889
| [`spzeros(m,n)`](@ref) | [`zeros(m,n)`](@ref) | Creates a *m*-by-*n* matrix of zeros. ([`spzeros(m,n)`](@ref) is empty.) |
897890
| [`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*. |
898-
| [`speye(n)`](@ref) | [`eye(n)`](@ref) | Creates a *n*-by-*n* identity matrix. |
891+
| [`speye(n)`](@ref) | [`Matrix(I,n,n)`](@ref)| Creates a *n*-by-*n* identity matrix. |
899892
| [`Array(S)`](@ref) | [`sparse(A)`](@ref) | Interconverts between dense and sparse formats. |
900893
| [`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)``. |
901894
| [`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. |

doc/src/stdlib/arrays.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Base.fill
2424
Base.fill!
2525
Base.similar(::AbstractArray)
2626
Base.similar(::Any, ::Tuple)
27-
Base.eye
2827
Base.linspace
2928
Base.logspace
3029
Base.Random.randsubseq

0 commit comments

Comments
 (0)