Skip to content

Commit

Permalink
Add a warning about inadvertent Diagonal(::Matrix) (#46864)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott authored Nov 30, 2022
1 parent 41b73e2 commit 5da8d5f
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ end
"""
Diagonal(V::AbstractVector)
Construct a matrix with `V` as its diagonal.
Construct a lazy matrix with `V` as its diagonal.
See also [`diag`](@ref), [`diagm`](@ref).
See also [`UniformScaling`](@ref) for the lazy identity matrix `I`,
[`diagm`](@ref) to make a dense matrix, and [`diag`](@ref) to extract diagonal elements.
# Examples
```jldoctest
julia> Diagonal([1, 10, 100])
julia> d = Diagonal([1, 10, 100])
3×3 Diagonal{$Int, Vector{$Int}}:
1 ⋅ ⋅
⋅ 10 ⋅
Expand All @@ -40,6 +41,30 @@ julia> diagm([7, 13])
2×2 Matrix{$Int}:
7 0
0 13
julia> ans + I
2×2 Matrix{Int64}:
8 0
0 14
julia> I(2)
2×2 Diagonal{Bool, Vector{Bool}}:
1 ⋅
⋅ 1
```
Note that a one-column matrix is not treated like a vector, but instead calls the
method `Diagonal(A::AbstractMatrix)` which extracts 1-element `diag(A)`:
```jldoctest
julia> A = transpose([7.0 13.0])
2×1 transpose(::Matrix{Float64}) with eltype Float64:
7.0
13.0
julia> Diagonal(A)
1×1 Diagonal{Float64, Vector{Float64}}:
7.0
```
"""
Diagonal(V::AbstractVector)
Expand Down

0 comments on commit 5da8d5f

Please sign in to comment.