Skip to content

Commit

Permalink
Eliminate all eye calls from base.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Nov 20, 2017
1 parent 39dbf4d commit 6013448
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ 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
8 changes: 4 additions & 4 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1986,8 +1986,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 +1997,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
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
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
14 changes: 8 additions & 6 deletions base/linalg/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The object has two fields:
triu(F.factors)` for a `QR` object `F`.
- The subdiagonal part contains the reflectors ``v_i`` stored in a packed format where
``v_i`` is the ``i``th column of the matrix `V = eye(m,n) + tril(F.factors,-1)`.
``v_i`` is the ``i``th column of the matrix `V = I + tril(F.factors, -1)`.
* `τ` is a vector of length `min(m,n)` containing the coefficients ``\tau_i``.
Expand Down Expand Up @@ -70,7 +70,7 @@ The object has two fields:
triu(F.factors)` for a `QR` object `F`.
- The subdiagonal part contains the reflectors ``v_i`` stored in a packed format such
that `V = eye(m,n) + tril(F.factors,-1)`.
that `V = I + tril(F.factors, -1)`.
* `T` is a square matrix with `min(m,n)` columns, whose upper triangular part gives the
matrix ``T`` above (the subdiagonal elements are ignored).
Expand Down Expand Up @@ -117,7 +117,7 @@ The object has three fields:
triu(F.factors)` for a `QR` object `F`.
- The subdiagonal part contains the reflectors ``v_i`` stored in a packed format where
``v_i`` is the ``i``th column of the matrix `V = eye(m,n) + tril(F.factors,-1)`.
``v_i`` is the ``i``th column of the matrix `V = I + tril(F.factors, -1)`.
* `τ` is a vector of length `min(m,n)` containing the coefficients ``\tau_i``.
Expand Down Expand Up @@ -328,12 +328,14 @@ end
function _qr(A::Union{Number,AbstractMatrix}, ::Val{false}; full::Bool = false)
F = qrfact(A, Val(false))
Q, R = getq(F), F[:R]::Matrix{eltype(F)}
return (!full ? Array(Q) : A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 1)))), R
sQf1 = size(Q.factors, 1)
return (!full ? Array(Q) : A_mul_B!(Q, Matrix{eltype(Q)}(I, sQf1, sQf1))), R
end
function _qr(A::Union{Number, AbstractMatrix}, ::Val{true}; full::Bool = false)
F = qrfact(A, Val(true))
Q, R, p = getq(F), F[:R]::Matrix{eltype(F)}, F[:p]::Vector{BlasInt}
return (!full ? Array(Q) : A_mul_B!(Q, eye(eltype(Q), size(Q.factors, 1)))), R, p
sQf1 = size(Q.factors, 1)
return (!full ? Array(Q) : A_mul_B!(Q, Matrix{eltype(Q)}(I, sQf1, sQf1))), R, p
end

"""
Expand Down Expand Up @@ -506,7 +508,7 @@ convert(::Type{AbstractMatrix{T}}, Q::QRPackedQ) where {T} = convert(QRPackedQ{T
convert(::Type{QRCompactWYQ{S}}, Q::QRCompactWYQ) where {S} = QRCompactWYQ(convert(AbstractMatrix{S}, Q.factors), convert(AbstractMatrix{S}, Q.T))
convert(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ{S}) where {S} = Q
convert(::Type{AbstractMatrix{S}}, Q::QRCompactWYQ) where {S} = convert(QRCompactWYQ{S}, Q)
convert(::Type{Matrix}, A::AbstractQ{T}) where {T} = A_mul_B!(A, eye(T, size(A.factors, 1), min(size(A.factors)...)))
convert(::Type{Matrix}, A::AbstractQ{T}) where {T} = A_mul_B!(A, Matrix{T}(I, size(A.factors, 1), min(size(A.factors)...)))
convert(::Type{Array}, A::AbstractQ) = convert(Matrix, A)

size(A::Union{QR,QRCompactWY,QRPivoted}, dim::Integer) = size(A.factors, dim)
Expand Down
10 changes: 6 additions & 4 deletions base/linalg/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function svdfact!(A::StridedMatrix{T}; full::Bool = false, thin::Union{Bool,Void
end
m,n = size(A)
if m == 0 || n == 0
u,s,vt = (eye(T, m, full ? m : n), real(zeros(T,0)), eye(T,n,n))
u,s,vt = (Matrix{T}(I, m, full ? m : n), real(zeros(T,0)), Matrix{T}(I, n, n))
else
u,s,vt = LAPACK.gesdd!(full ? 'A' : 'S', A)
end
Expand Down Expand Up @@ -312,17 +312,19 @@ function getindex(obj::GeneralizedSVD{T}, d::Symbol) where T
elseif d == :D1
m = size(obj.U, 1)
if m - obj.k - obj.l >= 0
return [eye(T, obj.k) zeros(T, obj.k, obj.l); zeros(T, obj.l, obj.k) Diagonal(obj.a[obj.k + 1:obj.k + obj.l]); zeros(T, m - obj.k - obj.l, obj.k + obj.l)]
return [Matrix{T}(I, obj.k, obj.k) zeros(T, obj.k, obj.l) ;
zeros(T, obj.l, obj.k) Diagonal(obj.a[obj.k + 1:obj.k + obj.l]) ;
zeros(T, m - obj.k - obj.l, obj.k + obj.l) ]
else
return [eye(T, m, obj.k) [zeros(T, obj.k, m - obj.k); Diagonal(obj.a[obj.k + 1:m])] zeros(T, m, obj.k + obj.l - m)]
return [Matrix{T}(I, m, obj.k) [zeros(T, obj.k, m - obj.k); Diagonal(obj.a[obj.k + 1:m])] zeros(T, m, obj.k + obj.l - m)]
end
elseif d == :D2
m = size(obj.U, 1)
p = size(obj.V, 1)
if m - obj.k - obj.l >= 0
return [zeros(T, obj.l, obj.k) Diagonal(obj.b[obj.k + 1:obj.k + obj.l]); zeros(T, p - obj.l, obj.k + obj.l)]
else
return [zeros(T, p, obj.k) [Diagonal(obj.b[obj.k + 1:m]); zeros(T, obj.k + p - m, m - obj.k)] [zeros(T, m - obj.k, obj.k + obj.l - m); eye(T, obj.k + p - m, obj.k + obj.l - m)]]
return [zeros(T, p, obj.k) [Diagonal(obj.b[obj.k + 1:m]); zeros(T, obj.k + p - m, m - obj.k)] [zeros(T, m - obj.k, obj.k + obj.l - m); Matrix{T}(I, obj.k + p - m, obj.k + obj.l - m)]]
end
elseif d == :R
return obj.R
Expand Down
10 changes: 5 additions & 5 deletions base/linalg/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -537,14 +537,14 @@ end

function inv(A::LowerTriangular{T}) where T
S = typeof((zero(T)*one(T) + zero(T))/one(T))
LowerTriangular(A_ldiv_B!(convert(AbstractArray{S}, A), eye(S, size(A, 1))))
LowerTriangular(A_ldiv_B!(convert(AbstractArray{S}, A), Matrix{S}(I, size(A, 1), size(A, 1))))
end
function inv(A::UpperTriangular{T}) where T
S = typeof((zero(T)*one(T) + zero(T))/one(T))
UpperTriangular(A_ldiv_B!(convert(AbstractArray{S}, A), eye(S, size(A, 1))))
UpperTriangular(A_ldiv_B!(convert(AbstractArray{S}, A), Matrix{S}(I, size(A, 1), size(A, 1))))
end
inv(A::UnitUpperTriangular{T}) where {T} = UnitUpperTriangular(A_ldiv_B!(A, eye(T, size(A, 1))))
inv(A::UnitLowerTriangular{T}) where {T} = UnitLowerTriangular(A_ldiv_B!(A, eye(T, size(A, 1))))
inv(A::UnitUpperTriangular{T}) where {T} = UnitUpperTriangular(A_ldiv_B!(A, Matrix{T}(I, size(A, 1), size(A, 1))))
inv(A::UnitLowerTriangular{T}) where {T} = UnitLowerTriangular(A_ldiv_B!(A, Matrix{T}(I, size(A, 1), size(A, 1))))

errorbounds(A::AbstractTriangular{T,<:StridedMatrix}, X::StridedVecOrMat{T}, B::StridedVecOrMat{T}) where {T<:Union{BigFloat,Complex{BigFloat}}} =
error("not implemented yet! Please submit a pull request.")
Expand Down Expand Up @@ -2156,7 +2156,7 @@ function sqrt(A::UnitUpperTriangular{T}) where T
B = A.data
n = checksquare(B)
t = typeof(sqrt(zero(T)))
R = eye(t, n, n)
R = Matrix{t}(I, n, n)
tt = typeof(zero(t)*zero(t))
half = inv(R[1,1]+R[1,1]) # for general, algebraic cases. PR#20214
@inbounds for j = 1:n
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ Convert an AbstractMatrix `A` into a sparse matrix.
# Examples
```jldoctest
julia> A = eye(3)
julia> A = Matrix(1.0I, 3, 3)
3×3 Array{Float64,2}:
1.0 0.0 0.0
0.0 1.0 0.0
Expand Down

0 comments on commit 6013448

Please sign in to comment.