Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload UniformScaling to make I(n) create a Diagonal matrix. #30298

Merged
merged 6 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ Standard library changes

#### LinearAlgebra

* Added keyword arguments `rtol`, `atol` to `pinv` and `nullspace` ([#29998]).
* Added keyword arguments `rtol`, `atol` to `pinv` and `nullspace` ([#29998]).
* `isdiag` and `isposdef` for `Diagonal` and `UniformScaling` ([#29638]).
* `mul!`, `rmul!` and `lmul!` methods for `UniformScaling` ([#29506]).
* `Symmetric` and `Hermitian` matrices now preserve the wrapper when scaled with a number ([#29469]).
* Exponentiation operator `^` now supports raising a `Irrational` to an `AbstractMatrix` power ([#29782]).
* `UniformScaling` instances are now callable such that e.g. `I(3)` will produce a `Diagonal` matrix ([#30298]).
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes to NEWS unrelated?


#### SparseArrays

Expand Down
25 changes: 25 additions & 0 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,31 @@ julia> [1 2im 3; 1im 2 3] * I
"""
const I = UniformScaling(true)

"""
(I::UniformScaling)(n::Integer)

Construct a `Diagonal` matrix from a `UniformScaling`.

!!! compat "Julia 1.2"
This method is available as of Julia 1.2.

# Examples
```jldoctest
julia> I(3)
3×3 Diagonal{Bool,Array{Bool,1}}:
true ⋅ ⋅
⋅ true ⋅
⋅ ⋅ true
stevengj marked this conversation as resolved.
Show resolved Hide resolved

julia> (0.7*I)(3)
3×3 Diagonal{Float64,Array{Float64,1}}:
0.7 ⋅ ⋅
⋅ 0.7 ⋅
⋅ ⋅ 0.7
```
"""
(I::UniformScaling)(n::Integer) = Diagonal(fill(I.λ, n))

eltype(::Type{UniformScaling{T}}) where {T} = T
ndims(J::UniformScaling) = 2
Base.has_offset_axes(::UniformScaling) = false
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,9 @@ end
@test rmul!(copyto!(C, A), J) == target
end

@testset "Construct Diagonal from UniformScaling" begin
@test size(I(3)) === (3,3)
@test I(3) isa Diagonal
stevengj marked this conversation as resolved.
Show resolved Hide resolved
end

end # module TestUniformscaling