Skip to content

Commit 02d2d38

Browse files
committed
Deprecate eye.
1 parent 55e2a6f commit 02d2d38

File tree

8 files changed

+58
-103
lines changed

8 files changed

+58
-103
lines changed

NEWS.md

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

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

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
@@ -1788,7 +1788,53 @@ end
17881788
@deprecate get_creds!(cache::CachedCredentials, credid, default) get!(cache, credid, default)
17891789
end
17901790

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

17931839
export tic, toq, toc
17941840
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: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ 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, n)`](@ref) | `n`-by-`n` identity matrix |
70+
| [`Matrix{T}(I, m, n)`](@ref) | `m`-by-`n` identity matrix |
7171
| [`linspace(start, stop, n)`](@ref) | range of `n` linearly spaced elements from `start` to `stop` |
7272
| [`fill!(A, x)`](@ref) | fill the array `A` with the value `x` |
7373
| [`fill(x, dims...)`](@ref) | an `Array` filled with the value `x` |
@@ -782,18 +782,12 @@ stored zeros. (See [Sparse Matrix Storage](@ref man-csc).).
782782
### Sparse Vector and Matrix Constructors
783783

784784
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:
785+
function that Julia provides for working with dense arrays. To produce
786+
a sparse array instead, you can use the same names with an `sp` prefix:
787787

788788
```jldoctest
789789
julia> spzeros(3)
790790
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
797791
```
798792

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

851845
```jldoctest
852-
julia> sparse(eye(5))
846+
julia> sparse(Matrix(1.0I, 5))
853847
5×5 SparseMatrixCSC{Float64,Int64} with 5 stored entries:
854848
[1, 1] = 1.0
855849
[2, 2] = 1.0
@@ -895,7 +889,7 @@ section of the standard library reference.
895889
|:-------------------------- |:---------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
896890
| [`spzeros(m,n)`](@ref) | [`zeros(m,n)`](@ref) | Creates a *m*-by-*n* matrix of zeros. ([`spzeros(m,n)`](@ref) is empty.) |
897891
| [`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. |
892+
| [`speye(n)`](@ref) | [`Matrix(I, n)`](@ref) | Creates a *n*-by-*n* identity matrix. |
899893
| [`Array(S)`](@ref) | [`sparse(A)`](@ref) | Interconverts between dense and sparse formats. |
900894
| [`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)``. |
901895
| [`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)