Skip to content

Commit

Permalink
Merge pull request #24438 from Sacha0/goodbeye
Browse files Browse the repository at this point in the history
eye see deprecated methods
  • Loading branch information
Sacha0 authored Nov 20, 2017
2 parents a05a249 + 40cd410 commit 06a6afb
Show file tree
Hide file tree
Showing 49 changed files with 308 additions and 328 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
87 changes: 1 addition & 86 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,95 +432,10 @@ 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"))
eye(T, m)
Matrix{T}(I, m, m)
end

one(x::AbstractMatrix{T}) where {T} = _one(one(T), x)
Expand Down
56 changes: 51 additions & 5 deletions 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 Expand Up @@ -1986,8 +2032,8 @@ function full(Q::LinAlg.LQPackedQ; thin::Bool = true)
"`full(Q::LQPackedQ; thin::Bool = true)` (and `full` in general) ",
"has been deprecated. To replace `full(Q::LQPackedQ, true)`, ",
"consider `Matrix(Q)` or `Array(Q)`. To replace `full(Q::LQPackedQ, false)`, ",
"consider `Base.LinAlg.A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 2)))`."), :full)
return thin ? Array(Q) : A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 2)))
"consider `Base.LinAlg.A_mul_B!(Q, Matrix{eltype(Q)}(I, size(Q.factors, 2), size(Q.factors, 2)))`."), :full)
return thin ? Array(Q) : A_mul_B!(Q, Matrix{eltype(Q)}(I, size(Q.factors, 2), size(Q.factors, 2)))
end
function full(Q::Union{LinAlg.QRPackedQ,LinAlg.QRCompactWYQ}; thin::Bool = true)
qtypestr = isa(Q, LinAlg.QRPackedQ) ? "QRPackedQ" :
Expand All @@ -1997,8 +2043,8 @@ function full(Q::Union{LinAlg.QRPackedQ,LinAlg.QRCompactWYQ}; thin::Bool = true)
"`full(Q::$(qtypestr); thin::Bool = true)` (and `full` in general) ",
"has been deprecated. To replace `full(Q::$(qtypestr), true)`, ",
"consider `Matrix(Q)` or `Array(Q)`. To replace `full(Q::$(qtypestr), false)`, ",
"consider `Base.LinAlg.A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 1)))`."), :full)
return thin ? Array(Q) : A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 1)))
"consider `Base.LinAlg.A_mul_B!(Q, Matrix{eltype(Q)}(I, size(Q.factors, 1), size(Q.factors, 1)))`."), :full)
return thin ? Array(Q) : A_mul_B!(Q, Matrix{eltype(Q)}(I, size(Q.factors, 1), size(Q.factors, 1)))
end

# full for symmetric / hermitian / triangular wrappers
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
2 changes: 1 addition & 1 deletion base/linalg/bunchkaufman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function getindex(B::BunchKaufman{T}, d::Symbol) where {T<:BlasFloat}
if d == :p
return _ipiv2perm_bk(B.ipiv, n, B.uplo)
elseif d == :P
return eye(T, n)[:,invperm(B[:p])]
return Matrix{T}(I, n, n)[:,invperm(B[:p])]
elseif d == :L || d == :U || d == :D
if B.rook
LUD, od = LAPACK.syconvf_rook!(B.uplo, 'C', copy(B.LD), B.ipiv)
Expand Down
14 changes: 7 additions & 7 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ used, otherwise the scaling and squaring algorithm (see [^H05]) is chosen.
# Examples
```jldoctest
julia> A = eye(2, 2)
julia> A = Matrix(1.0I, 2, 2)
2×2 Array{Float64,2}:
1.0 0.0
0.0 1.0
Expand All @@ -508,7 +508,7 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
end
ilo, ihi, scale = LAPACK.gebal!('B', A) # modifies A
nA = norm(A, 1)
I = eye(T,n)
Inn = Matrix{T}(I, n, n)
## For sufficiently small nA, use lower order Padé-Approximations
if (nA <= 2.1)
if nA > 0.95
Expand All @@ -525,7 +525,7 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
C = T[120.,60.,12.,1.]
end
A2 = A * A
P = copy(I)
P = copy(Inn)
U = C[2] * P
V = C[1] * P
for k in 1:(div(size(C, 1), 2) - 1)
Expand All @@ -552,9 +552,9 @@ function exp!(A::StridedMatrix{T}) where T<:BlasFloat
A4 = A2 * A2
A6 = A2 * A4
U = A * (A6 * (CC[14]*A6 + CC[12]*A4 + CC[10]*A2) +
CC[8]*A6 + CC[6]*A4 + CC[4]*A2 + CC[2]*I)
CC[8]*A6 + CC[6]*A4 + CC[4]*A2 + CC[2]*Inn)
V = A6 * (CC[13]*A6 + CC[11]*A4 + CC[9]*A2) +
CC[7]*A6 + CC[5]*A4 + CC[3]*A2 + CC[1]*I
CC[7]*A6 + CC[5]*A4 + CC[3]*A2 + CC[1]*Inn

X = V + U
LAPACK.gesv!(V-U, X)
Expand Down Expand Up @@ -614,7 +614,7 @@ triangular factor.
# Examples
```jldoctest
julia> A = 2.7182818 * eye(2)
julia> A = Matrix(2.7182818*I, 2, 2)
2×2 Array{Float64,2}:
2.71828 0.0
0.0 2.71828
Expand Down Expand Up @@ -1331,7 +1331,7 @@ julia> nullspace(M)
"""
function nullspace(A::StridedMatrix{T}) where T
m, n = size(A)
(m == 0 || n == 0) && return eye(T, n)
(m == 0 || n == 0) && return Matrix{T}(I, n, n)
SVD = svdfact(A, full = true)
indstart = sum(SVD.S .> max(m,n)*maximum(SVD.S)*eps(eltype(SVD.S))) + 1
return SVD.Vt[indstart:end,:]'
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ end
#Eigensystem
eigvals(D::Diagonal{<:Number}) = D.diag
eigvals(D::Diagonal) = [eigvals(x) for x in D.diag] #For block matrices, etc.
eigvecs(D::Diagonal) = eye(D)
eigvecs(D::Diagonal) = Matrix{eltype(D)}(I, size(D))
eigfact(D::Diagonal) = Eigen(eigvals(D), eigvecs(D))

#Singular system
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end

### General promotion rules
convert(::Type{Factorization{T}}, F::Factorization{T}) where {T} = F
inv(F::Factorization{T}) where {T} = A_ldiv_B!(F, eye(T, size(F,1)))
inv(F::Factorization{T}) where {T} = (n = size(F, 1); A_ldiv_B!(F, Matrix{T}(I, n, n)))

Base.hash(F::Factorization, h::UInt) = mapreduce(f -> hash(getfield(F, f)), hash, h, 1:nfields(F))
Base.:(==)( F::T, G::T) where {T<:Factorization} = all(f -> getfield(F, f) == getfield(G, f), 1:nfields(F))
Expand Down
10 changes: 6 additions & 4 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ of the [`eltype`](@ref) of `A`.
# Examples
```jldoctest
julia> rank(eye(3))
julia> rank(Matrix(I, 3, 3))
3
julia> rank(diagm(0 => [1, 0, 2]))
Expand Down Expand Up @@ -802,14 +802,16 @@ julia> N = inv(M)
3.0 -5.0
-1.0 2.0
julia> M*N == N*M == eye(2)
julia> M*N == N*M == Matrix(I, 2, 2)
true
```
"""
function inv(A::AbstractMatrix{T}) where T
n = checksquare(A)
S = typeof(zero(T)/one(T)) # dimensionful
S0 = typeof(zero(T)/oneunit(T)) # dimensionless
A_ldiv_B!(factorize(convert(AbstractMatrix{S}, A)), eye(S0, checksquare(A)))
dest = Matrix{S0}(I, n, n)
A_ldiv_B!(factorize(convert(AbstractMatrix{S}, A)), dest)
end

function pinv(v::AbstractVector{T}, tol::Real=real(zero(T))) where T
Expand Down Expand Up @@ -1347,7 +1349,7 @@ julia> M = [1 0; 2 2]
julia> logdet(M)
0.6931471805599453
julia> logdet(eye(3))
julia> logdet(Matrix(I, 3, 3))
0.0
```
"""
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/linalg/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function lq(A::Union{Number,AbstractMatrix}; full::Bool = false, thin::Union{Boo
end
F = lqfact(A)
L, Q = F[:L], F[:Q]
return L, !full ? Array(Q) : A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 2)))
return L, !full ? Array(Q) : A_mul_B!(Q, Matrix{eltype(Q)}(I, size(Q.factors, 2), size(Q.factors, 2)))
end

copy(A::LQ) = LQ(copy(A.factors), copy(A.τ))
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function getindex(F::LU{T,<:StridedMatrix}, d::Symbol) where T
elseif d == :p
return ipiv2perm(F.ipiv, m)
elseif d == :P
return eye(T, m)[:,invperm(F[:p])]
return Matrix{T}(I, m, m)[:,invperm(F[:p])]
else
throw(KeyError(d))
end
Expand Down Expand Up @@ -337,7 +337,7 @@ end
inv!(A::LU{<:BlasFloat,<:StridedMatrix}) =
@assertnonsingular LAPACK.getri!(A.factors, A.ipiv) A.info
inv!(A::LU{T,<:StridedMatrix}) where {T} =
@assertnonsingular A_ldiv_B!(A.factors, copy(A), eye(T, size(A, 1))) A.info
@assertnonsingular A_ldiv_B!(A.factors, copy(A), Matrix{T}(I, size(A, 1), size(A, 1))) A.info
inv(A::LU{<:BlasFloat,<:StridedMatrix}) = inv!(copy(A))

function _cond1Inf(A::LU{<:BlasFloat,<:StridedMatrix}, p::Number, normA::Real)
Expand Down Expand Up @@ -437,7 +437,7 @@ function getindex(F::LU{T,Tridiagonal{T,V}}, d::Symbol) where {T,V}
elseif d == :p
return ipiv2perm(F.ipiv, m)
elseif d == :P
return eye(T, m)[:,invperm(F[:p])]
return Matrix{T}(I, m, m)[:,invperm(F[:p])]
end
throw(KeyError(d))
end
Expand Down
Loading

1 comment on commit 06a6afb

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

Please sign in to comment.