UniformScaling to identity matrix

Sometimes, it is necessary to convert a UniformScaling to an actual matrix of values. In these cases, I call some form of Matrix(I, n, n).
While this is not that bad, it can make otherwise very readable code slightly less so. However with call overloading, it is possible to emulate the old functionality of eye when necessary:

(I::UniformScaling)(T::DataType, n::Integer, m::Integer) = Matrix(one(T)*I, n, m)
(I::UniformScaling)(T::DataType, n::Integer) = I(T, n, n)
(I::UniformScaling)(n::Integer, m::Integer) = I(Bool, n, m)
(I::UniformScaling)(n::Integer) = I(n, n)

julia> I(3)
3Ɨ3 Array{Bool,2}:
  true  false  false
 false   true  false
 false  false   true

julia> I(Int8, 2)
2Ɨ2 Array{Int8,2}:
 1  0
 0  1

I am not aware of all of the reasons for eliminating eye in favor of I (not that Iā€™m complaining!) but since it is inevitable to sometimes need hard values, I think this could be a convenient addition to LinearAgebra.


Yep, this is being discussed https://github.com/JuliaLang/julia/issues/23156

1 Like

Well would you look at that!
Thanks for the link.