Skip to content

Commit

Permalink
Update diagonal to support specifying axes
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos committed Jan 10, 2025
1 parent 772f223 commit 3b34fc7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/diaginterface/diaginterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,19 @@ function setdiagindices!(a::AbstractArray, v, i::Colon)
end

"""
diagonal(v::AbstractVector) -> AbstractMatrix
diagonal(v::AbstractVector, [axes]) -> AbstractMatrix
Return a diagonal matrix from a vector `v`. This is a replacement of `LinearAlgebra.Diagonal`
that does not imply an output type. Defaults to `Diagonal(v)`.
Return a diagonal matrix from a vector `v`, optionally providing the `axes` of the resulting matrix.
This is an extension of `LinearAlgebra.Diagonal`, designed to avoid the implication of the output type,
as well as allowing the option of specifying the `axes`.
Defaults to `Diagonal(v)`.
"""
diagonal(A::AbstractVector) = LinearAlgebra.Diagonal(A)
function diagonal(v::AbstractVector)
ax = only(axes(v, 1)) # Base.axes1 is private
return diagonal(v, (ax, ax))
end
function diagonal(v::AbstractVector, axes)
ax = only(axes(v, 1))
axes == (ax, ax) || throw(ArgumentError(lazy"Cannot create a `Diagonal` with axes $axes"))
return LinearAlgebra.Diagonal(v)
end

0 comments on commit 3b34fc7

Please sign in to comment.