Skip to content

Commit 496c294

Browse files
authored
Merge pull request #455 from JuliaArrays/fe/goodbeye
Deprecate eye
2 parents b97d834 + 6a393b8 commit 496c294

17 files changed

+141
-81
lines changed

src/MArray.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,15 @@ end
7878

7979
# Some more advanced constructor-like functions
8080
@inline one(::Type{MArray{S}}) where {S} = one(MArray{S,Float64,tuple_length(S)})
81-
@inline eye(::Type{MArray{S}}) where {S} = eye(MArray{S,Float64,tuple_length(S)})
8281
@inline one(::Type{MArray{S,T}}) where {S,T} = one(MArray{S,T,tuple_length(S)})
83-
@inline eye(::Type{MArray{S,T}}) where {S,T} = eye(MArray{S,T,tuple_length(S)})
82+
83+
# MArray(I::UniformScaling) methods to replace eye
84+
(::Type{MA})(I::UniformScaling) where {MA<:MArray} = _eye(Size(MA), MA, I)
85+
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
86+
@static if isdefined(LinearAlgebra, :eye)
87+
@deprecate eye(::Type{MArray{S}}) where {S} MArray{S}(1.0I)
88+
@deprecate eye(::Type{MArray{S,T}}) where {S,T} MArray{S,T}(I)
89+
end
8490

8591
####################
8692
## MArray methods ##
@@ -265,29 +271,33 @@ macro MArray(ex)
265271
$(esc(ex.args[1]))($(esc(ex.args[2])), MArray{$(esc(Expr(:curly, Tuple, ex.args[3:end]...)))})
266272
end
267273
end
268-
elseif ex.args[1] == :eye
274+
elseif ex.args[1] == :eye # deprecated
269275
if length(ex.args) == 2
270276
return quote
271-
eye(MArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[2]))}})
277+
Base.depwarn("`@MArray eye(m)` is deprecated, use `MArray{m,m}(1.0I)` instead", :eye)
278+
MArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[2]))},Float64}(I)
272279
end
273280
elseif length(ex.args) == 3
274281
# We need a branch, depending if the first argument is a type or a size.
275282
return quote
276283
if isa($(esc(ex.args[2])), DataType)
277-
eye(MArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[3]))}, $(esc(ex.args[2]))})
284+
Base.depwarn("`@MArray eye(T, m)` is deprecated, use `MArray{m,m,T}(I)` instead", :eye)
285+
MArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[3]))}, $(esc(ex.args[2]))}(I)
278286
else
279-
eye(MArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[3]))}})
287+
Base.depwarn("`@MArray eye(m, n)` is deprecated, use `MArray{m,n}(1.0I)` instead", :eye)
288+
MArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[3]))}, Float64}(I)
280289
end
281290
end
282291
elseif length(ex.args) == 4
283292
return quote
284-
eye(MArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[4]))}, $(esc(ex.args[2]))})
293+
Base.depwarn("`@MArray eye(T, m, n)` is deprecated, use `MArray{m,n,T}(I)` instead", :eye)
294+
MArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[4]))}, $(esc(ex.args[2]))}(I)
285295
end
286296
else
287297
error("Bad eye() expression for @MArray")
288298
end
289299
else
290-
error("@MArray only supports the zeros(), ones(), rand(), randn(), randexp(), and eye() functions.")
300+
error("@MArray only supports the zeros(), ones(), rand(), randn(), and randexp() functions.")
291301
end
292302
else
293303
error("Bad input for @MArray")

src/MMatrix.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ end
6161

6262
# Some more advanced constructor-like functions
6363
@inline one(::Type{MMatrix{N}}) where {N} = one(MMatrix{N,N})
64-
@inline eye(::Type{MMatrix{N}}) where {N} = eye(MMatrix{N,N})
64+
65+
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
66+
@static if isdefined(LinearAlgebra, :eye)
67+
@deprecate eye(::Type{MMatrix{N}}) where {N} MMatrix{N,N}(1.0I)
68+
end
6569

6670
#####################
6771
## MMatrix methods ##
@@ -205,29 +209,33 @@ macro MMatrix(ex)
205209
else
206210
error("@MMatrix expected a 2-dimensional array expression")
207211
end
208-
elseif ex.args[1] == :eye
212+
elseif ex.args[1] == :eye # deprecated
209213
if length(ex.args) == 2
210214
return quote
211-
eye(MMatrix{$(esc(ex.args[2]))})
215+
Base.depwarn("`@MMatrix eye(m)` is deprecated, use `MMatrix{m,m}(1.0I)` instead", :eye)
216+
MMatrix{$(esc(ex.args[2])),$(esc(ex.args[2])),Float64}(I)
212217
end
213218
elseif length(ex.args) == 3
214219
# We need a branch, depending if the first argument is a type or a size.
215220
return quote
216221
if isa($(esc(ex.args[2])), DataType)
217-
eye(MMatrix{$(esc(ex.args[3])), $(esc(ex.args[3])), $(esc(ex.args[2]))})
222+
Base.depwarn("`@MMatrix eye(T, m)` is deprecated, use `MMatrix{m,m,T}(I)` instead", :eye)
223+
MMatrix{$(esc(ex.args[3])), $(esc(ex.args[3])), $(esc(ex.args[2]))}(I)
218224
else
219-
eye(MMatrix{$(esc(ex.args[2])), $(esc(ex.args[3]))})
225+
Base.depwarn("`@MMatrix eye(m, n)` is deprecated, use `MMatrix{m,n}(1.0I)` instead", :eye)
226+
MMatrix{$(esc(ex.args[2])), $(esc(ex.args[3])), Float64}(I)
220227
end
221228
end
222229
elseif length(ex.args) == 4
223230
return quote
224-
eye(MMatrix{$(esc(ex.args[3])), $(esc(ex.args[4])), $(esc(ex.args[2]))})
231+
Base.depwarn("`@MMatrix eye(T, m, n)` is deprecated, use `MMatrix{m,n,T}(I)` instead", :eye)
232+
MMatrix{$(esc(ex.args[3])), $(esc(ex.args[4])), $(esc(ex.args[2]))}(I)
225233
end
226234
else
227235
error("Bad eye() expression for @MMatrix")
228236
end
229237
else
230-
error("@MMatrix only supports the zeros(), ones(), rand(), randn(), randexp(), and eye() functions.")
238+
error("@MMatrix only supports the zeros(), ones(), rand(), randn(), and randexp() functions.")
231239
end
232240
else
233241
error("Bad input for @MMatrix")

src/SArray.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ end
5757

5858
# Some more advanced constructor-like functions
5959
@inline one(::Type{SArray{S}}) where {S} = one(SArray{S, Float64, tuple_length(S)})
60-
@inline eye(::Type{SArray{S}}) where {S} = eye(SArray{S, Float64, tuple_length(S)})
6160
@inline one(::Type{SArray{S, T}}) where {S, T} = one(SArray{S, T, tuple_length(S)})
62-
@inline eye(::Type{SArray{S, T}}) where {S, T} = eye(SArray{S, T, tuple_length(S)})
61+
62+
# SArray(I::UniformScaling) methods to replace eye
63+
(::Type{SA})(I::UniformScaling) where {SA<:SArray} = _eye(Size(SA), SA, I)
64+
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
65+
@static if isdefined(LinearAlgebra, :eye)
66+
@deprecate eye(::Type{SArray{S}}) where {S} SArray{S}(1.0I)
67+
@deprecate eye(::Type{SArray{S,T}}) where {S,T} SArray{S,T}(I)
68+
end
6369

6470
####################
6571
## SArray methods ##
@@ -228,29 +234,33 @@ macro SArray(ex)
228234
$(esc(ex.args[1]))($(esc(ex.args[2])), SArray{$(esc(Expr(:curly, Tuple, ex.args[3:end]...)))})
229235
end
230236
end
231-
elseif ex.args[1] == :eye
237+
elseif ex.args[1] == :eye # deprecated
232238
if length(ex.args) == 2
233239
return quote
234-
eye(SArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[2]))}})
240+
Base.depwarn("`@SArray eye(m)` is deprecated, use `SArray{m,m}(1.0I)` instead", :eye)
241+
SArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[2]))},Float64}(I)
235242
end
236243
elseif length(ex.args) == 3
237244
# We need a branch, depending if the first argument is a type or a size.
238245
return quote
239246
if isa($(esc(ex.args[2])), DataType)
240-
eye(SArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[3]))}, $(esc(ex.args[2]))})
247+
Base.depwarn("`@SArray eye(T, m)` is deprecated, use `SArray{m,m,T}(I)` instead", :eye)
248+
SArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[3]))}, $(esc(ex.args[2]))}(I)
241249
else
242-
eye(SArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[3]))}})
250+
Base.depwarn("`@SArray eye(m, n)` is deprecated, use `SArray{m,n}(1.0I)` instead", :eye)
251+
SArray{Tuple{$(esc(ex.args[2])), $(esc(ex.args[3]))}, Float64}(I)
243252
end
244253
end
245254
elseif length(ex.args) == 4
246255
return quote
247-
eye(SArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[4]))}, $(esc(ex.args[2]))})
256+
Base.depwarn("`@SArray eye(T, m, n)` is deprecated, use `SArray{m,n,T}(I)` instead", :eye)
257+
SArray{Tuple{$(esc(ex.args[3])), $(esc(ex.args[4]))}, $(esc(ex.args[2]))}(I)
248258
end
249259
else
250260
error("Bad eye() expression for @SArray")
251261
end
252262
else
253-
error("@SArray only supports the zeros(), ones(), rand(), randn(), randexp(), and eye() functions.")
263+
error("@SArray only supports the zeros(), ones(), rand(), randn(), and randexp() functions.")
254264
end
255265
else
256266
error("Bad input for @SArray")

src/SDiagonal.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ factorize(D::SDiagonal) = D
5555
-(A::SDiagonal) = SDiagonal(-A.diag)
5656
+(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag + Db.diag)
5757
-(Da::SDiagonal, Db::SDiagonal) = SDiagonal(Da.diag - Db.diag)
58-
-(A::SDiagonal, B::SMatrix) = eye(typeof(B))*A - B
58+
-(A::SDiagonal, B::SMatrix) = typeof(B)(I)*A - B
5959

6060
*(x::T, D::SDiagonal) where {T<:Number} = SDiagonal(x * D.diag)
6161
*(D::SDiagonal, x::T) where {T<:Number} = SDiagonal(D.diag * x)
@@ -81,7 +81,13 @@ function logdet(D::SDiagonal{N,T}) where {N,T<:Complex} #Make sure branch cut is
8181
-pi<imag(x)<pi ? x : real(x)+(mod2pi(imag(x)+pi)-pi)*im
8282
end
8383

84-
eye(::Type{SDiagonal{N,T}}) where {N,T} = SDiagonal(ones(SVector{N,T}))
84+
# SDiagonal(I::UniformScaling) methods to replace eye
85+
(::Type{SD})(I::UniformScaling) where {N,SD<:SDiagonal{N}} = SD(ntuple(x->I.λ, Val(N)))
86+
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
87+
@static if isdefined(LinearAlgebra, :eye)
88+
@deprecate eye(::Type{SDiagonal{N,T}}) where {N,T} SDiagonal{N,T}(I)
89+
end
90+
8591
one(::Type{SDiagonal{N,T}}) where {N,T} = SDiagonal(ones(SVector{N,T}))
8692
one(::SDiagonal{N,T}) where {N,T} = SDiagonal(ones(SVector{N,T}))
8793
Base.zero(::SDiagonal{N,T}) where {N,T} = SDiagonal(zeros(SVector{N,T}))

src/SMatrix.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ end
6161

6262
# Some more advanced constructor-like functions
6363
@inline one(::Type{SMatrix{N}}) where {N} = one(SMatrix{N,N})
64-
@inline eye(::Type{SMatrix{N}}) where {N} = eye(SMatrix{N,N})
64+
65+
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
66+
@static if isdefined(LinearAlgebra, :eye)
67+
@deprecate eye(::Type{SMatrix{N}}) where {N} SMatrix{N,N}(1.0I)
68+
end
6569

6670
#####################
6771
## SMatrix methods ##
@@ -179,29 +183,33 @@ macro SMatrix(ex)
179183
else
180184
error("@SMatrix expected a 2-dimensional array expression")
181185
end
182-
elseif ex.args[1] == :eye
186+
elseif ex.args[1] == :eye # deprecated
183187
if length(ex.args) == 2
184188
return quote
185-
eye(SMatrix{$(esc(ex.args[2]))})
189+
Base.depwarn("`@SMatrix eye(m)` is deprecated, use `SMatrix{m,m}(1.0I)` instead", :eye)
190+
SMatrix{$(esc(ex.args[2])),$(esc(ex.args[2])),Float64}(I)
186191
end
187192
elseif length(ex.args) == 3
188193
# We need a branch, depending if the first argument is a type or a size.
189194
return quote
190195
if isa($(esc(ex.args[2])), DataType)
191-
eye(SMatrix{$(esc(ex.args[3])), $(esc(ex.args[3])), $(esc(ex.args[2]))})
196+
Base.depwarn("`@SMatrix eye(T, m)` is deprecated, use `SMatrix{m,m,T}(I)` instead", :eye)
197+
SMatrix{$(esc(ex.args[3])), $(esc(ex.args[3])), $(esc(ex.args[2]))}(I)
192198
else
193-
eye(SMatrix{$(esc(ex.args[2])), $(esc(ex.args[3]))})
199+
Base.depwarn("`@SMatrix eye(m, n)` is deprecated, use `SMatrix{m,n}(1.0I)` instead", :eye)
200+
SMatrix{$(esc(ex.args[2])), $(esc(ex.args[3])), Float64}(I)
194201
end
195202
end
196203
elseif length(ex.args) == 4
197204
return quote
198-
eye(SMatrix{$(esc(ex.args[3])), $(esc(ex.args[4])), $(esc(ex.args[2]))})
205+
Base.depwarn("`@SMatrix eye(T, m, n)` is deprecated, use `SMatrix{m,n,T}(I)` instead", :eye)
206+
SMatrix{$(esc(ex.args[3])), $(esc(ex.args[4])), $(esc(ex.args[2]))}(I)
199207
end
200208
else
201209
error("Bad eye() expression for @SMatrix")
202210
end
203211
else
204-
error("@SMatrix only supports the zeros(), ones(), rand(), randn(), randexp(), and eye() functions.")
212+
error("@SMatrix only supports the zeros(), ones(), rand(), randn(), and randexp() functions.")
205213
end
206214
else
207215
error("Bad input for @SMatrix")

src/StaticArrays.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ import Random: rand, randn, randexp, rand!, randn!, randexp!
2121
using Core.Compiler: return_type
2222
import Base: sqrt, exp, log
2323
using LinearAlgebra
24-
import LinearAlgebra: transpose, adjoint, eye, vecdot, eigvals, eigen, lyap, tr,
24+
import LinearAlgebra: transpose, adjoint, vecdot, eigvals, eigen, lyap, tr,
2525
kron, diag, norm, dot, diagm, lu, svd, svdvals, svdfact,
2626
factorize, ishermitian, issymmetric, isposdef, normalize,
2727
normalize!, Eigen, det, logdet, cross, diff, qr
28+
# import eye for deprecation warnings
29+
@static if isdefined(LinearAlgebra, :eye)
30+
import LinearAlgebra: eye
31+
end
2832

2933
export StaticScalar, StaticArray, StaticVector, StaticMatrix
3034
export Scalar, SArray, SVector, SMatrix

src/eigen.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ end
128128

129129

130130
@inline function _eig(::Size{(1,1)}, A::LinearAlgebra.RealHermSymComplexHerm{T}, permute, scale) where {T <: Real}
131-
@inbounds return (SVector{1,T}((real(A[1]),)), eye(SMatrix{1,1,eltype(A)}))
131+
@inbounds return (SVector{1,T}((real(A[1]),)), SMatrix{1,1,eltype(A)}(I))
132132
end
133133

134134
@inline function _eig(::Size{(2,2)}, A::LinearAlgebra.RealHermSymComplexHerm{T}, permute, scale) where {T <: Real}
@@ -143,7 +143,7 @@ end
143143
vals = SVector(t_half - tmp, t_half + tmp)
144144

145145
@inbounds if a[3] == 0
146-
vecs = eye(SMatrix{2,2,eltype(A)})
146+
vecs = SMatrix{2,2,eltype(A)}(I)
147147
else
148148
@inbounds v11 = vals[1]-a[4]
149149
@inbounds n1 = sqrt(v11'*v11 + a[3]'*a[3])
@@ -168,7 +168,7 @@ end
168168
vals = SVector(t_half - tmp, t_half + tmp)
169169

170170
@inbounds if a[2] == 0
171-
vecs = eye(SMatrix{2,2,eltype(A)})
171+
vecs = SMatrix{2,2,eltype(A)}(I)
172172
else
173173
@inbounds v11 = vals[1]-a[4]
174174
@inbounds n1 = sqrt(v11'*v11 + a[2]'*a[2])

src/inv.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ end
6161
quote
6262
@_inline_meta
6363
LUp = lu(A)
64-
LUp.U \ (LUp.L \ eye(A)[LUp.p,:])
64+
LUp.U \ (LUp.L \ typeof(A)(I)[LUp.p,:])
6565
end
6666
else
6767
:(@_inline_meta; similar_type(A)(inv(Matrix(A))))

src/linalg.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,22 @@ end
164164
end
165165
end
166166

167-
#if VERSION < v"0.7-"
168-
@inline eye(::SM) where {SM <: StaticMatrix} = _eye(Size(SM), SM)
169-
@inline eye(::Type{SM}) where {SM <: StaticMatrix} = _eye(Size(SM), SM)
170-
@generated function _eye(::Size{S}, ::Type{SM}) where {S, SM <: StaticArray}
171-
T = eltype(SM) # should be "hyperpure"
172-
if T == Any
173-
T = Float64
174-
end
175-
exprs = [i == j ? :(one($T)) : :(zero($T)) for i 1:S[1], j 1:S[2]]
176-
return quote
177-
$(Expr(:meta, :inline))
178-
SM(tuple($(exprs...)))
179-
end
167+
# deprecate eye, keep around for as long as LinearAlgebra.eye exists
168+
@static if isdefined(LinearAlgebra, :eye)
169+
@deprecate eye(A::SM) where {SM<:StaticMatrix} typeof(A)(I)
170+
@deprecate eye(::Type{SM}) where {SM<:StaticMatrix} SM(1.0I)
171+
end
172+
173+
# StaticMatrix(I::UniformScaling) methods to replace eye
174+
(::Type{SM})(I::UniformScaling) where {N,M,SM<:StaticMatrix{N,M}} = _eye(Size(SM), SM, I)
175+
176+
@generated function _eye(::Size{S}, ::Type{SM}, I::UniformScaling{T}) where {S, SM <: StaticArray, T}
177+
exprs = [i == j ? :(I.λ) : :(zero($T)) for i 1:S[1], j 1:S[2]]
178+
return quote
179+
$(Expr(:meta, :inline))
180+
SM(tuple($(exprs...)))
180181
end
181-
#end
182+
end
182183

183184
@generated function diagm(kvs::Pair{<:Val,<:StaticVector}...)
184185
N = maximum(abs(kv.parameters[1].parameters[1]) + length(kv.parameters[2]) for kv in kvs)

src/qr.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ end
174174
#function qr_unrolled(A::StaticMatrix{<:Any, <:Any, TA}) where {TA}
175175
# m, n = size(A)
176176
# T = _qreltype(TA)
177-
# Q = eye(MMatrix{m,m,T,m*m})
177+
# Q = MMatrix{m,m,T,m*m}(I)
178178
# R = MMatrix{m,n,T,m*n}(A)
179179
# for k = 1:min(m - 1 + !(TA<:Real), n)
180180
# #x = view(R, k:m, k)

0 commit comments

Comments
 (0)