Skip to content

Commit

Permalink
Make copyto!(A,I) work for rectangular matrices (#28790)
Browse files Browse the repository at this point in the history
* Make copyto!(A,I) work for rectangular matrices

* add tests for `copyto!`

add tests for `copyto!(::Matrix, ::UniformScaling)`

* fix whitespace
  • Loading branch information
Jutho authored and mbauman committed Aug 22, 2018
1 parent 85cc84f commit 6a2dcc6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 1 addition & 2 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,9 @@ isapprox(A::AbstractMatrix, J::UniformScaling; kwargs...) = isapprox(J, A; kwarg

function copyto!(A::AbstractMatrix, J::UniformScaling)
@assert !has_offset_axes(A)
size(A,1)==size(A,2) || throw(DimensionMismatch("a UniformScaling can only be copied to a square matrix"))
fill!(A, 0)
λ = J.λ
for i = 1:size(A,1)
for i = 1:min(size(A,1),size(A,2))
@inbounds A[i,i] = λ
end
return A
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ let
@test cond(J) == zero(λ) ? one(real(λ)) : oftype(real(λ), Inf))
end

@testset "copyto!" begin
A = Matrix{Int}(undef, (3,3))
@test copyto!(A, I) == one(A)
B = Matrix{ComplexF64}(undef, (1,2))
@test copyto!(B, J) ==zero(λ)]
end

@testset "binary ops with matrices" begin
B = bitrand(2, 2)
@test B + I == B + Matrix(I, size(B))
Expand Down

0 comments on commit 6a2dcc6

Please sign in to comment.