Skip to content

Commit d171d4d

Browse files
committed
Add constructors for Matrix from UniformScaling.
1 parent d4b80e3 commit d171d4d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

base/linalg/uniformscaling.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,10 @@ UniformScaling{Float64}
351351
```
352352
"""
353353
chol(J::UniformScaling, args...) = ((C, info) = _chol!(J, nothing); @assertposdef C info)
354+
355+
356+
## Matrix construction from UniformScaling
357+
Matrix{T}(s::UniformScaling, dims::Dims{2}) where {T} = setindex!(zeros(T, dims), T(s.λ), diagind(dims...))
358+
Matrix{T}(s::UniformScaling, m::Integer, n::Integer) where {T} = Matrix{T}(s, Dims((m, n)))
359+
Matrix(s::UniformScaling, m::Integer, n::Integer) = Matrix(s, Dims((m, n)))
360+
Matrix(s::UniformScaling, dims::Dims{2}) = Matrix{eltype(s)}(s, dims)

test/linalg/uniformscaling.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,12 @@ end
188188
@test_throws LinAlg.PosDefException chol(-λ*I)
189189
end
190190
end
191+
192+
@testset "Matrix construction from UniformScaling" begin
193+
@test Matrix(2I, 3, 3)::Matrix{Int} == 2*eye(3)
194+
@test Matrix(2I, 3, 4)::Matrix{Int} == 2*eye(3, 4)
195+
@test Matrix(2I, 4, 3)::Matrix{Int} == 2*eye(4, 3)
196+
@test Matrix(2.0I, 3, 3)::Matrix{Float64} == 2*eye(3)
197+
@test Matrix{Real}(2I, 3, 3)::Matrix{Real} == 2*eye(3)
198+
@test Matrix{Float64}(2I, 3, 3)::Matrix{Float64} == 2*eye(3)
199+
end

0 commit comments

Comments
 (0)