Skip to content

Commit

Permalink
Add diagonal as a generalization of LinearAlgebra.Diagonal (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkdvos authored Jan 10, 2025
1 parent 2bd0713 commit fa39f45
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
name = "DiagonalArrays"
uuid = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
authors = ["ITensor developers <support@itensor.org> and contributors"]
version = "0.2.1"
version = "0.2.2"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
DerivableInterfaces = "6c5e35bf-e59e-4898-b73c-732dcc4ba65f"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"

[compat]
ArrayLayouts = "1.10.4"
DerivableInterfaces = "0.3.7"
LinearAlgebra = "1.10.0"
SparseArraysBase = "0.2.1"
julia = "1.10"
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ makedocs(;
edit_link="main",
assets=String[],
),
pages=["Home" => "index.md"],
pages=["Home" => "index.md", "Library" => "library.md"],
)

deploydocs(;
Expand Down
5 changes: 5 additions & 0 deletions docs/src/library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Library

```@autodocs
Modules = [DiagonalArrays]
```
11 changes: 11 additions & 0 deletions src/diaginterface/diaginterface.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# TODO: Turn these into `@interface ::AbstractDiagonalArrayInterface` functions.

using LinearAlgebra: LinearAlgebra

diaglength(a::AbstractArray{<:Any,0}) = 1

function diaglength(a::AbstractArray)
Expand Down Expand Up @@ -88,3 +90,12 @@ function setdiagindices!(a::AbstractArray, v, i::Colon)
diagview(a) .= v
return a
end

"""
diagonal(v::AbstractVector) -> AbstractMatrix
Return a diagonal matrix from a vector `v`.
This is an extension of `LinearAlgebra.Diagonal`, designed to avoid the implication of the output type.
Defaults to `Diagonal(v)`.
"""
diagonal(v::AbstractVector) = LinearAlgebra.Diagonal(v)
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[deps]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Expand Down
10 changes: 8 additions & 2 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Test: @test, @testset, @test_broken
using DiagonalArrays: DiagonalArrays, DiagonalArray, DiagonalMatrix, diaglength
using Test: @test, @testset, @test_broken, @inferred
using DiagonalArrays: DiagonalArrays, DiagonalArray, DiagonalMatrix, diaglength, diagonal
using SparseArraysBase: SparseArrayDOK, storedlength
using LinearAlgebra: Diagonal

@testset "Test DiagonalArrays" begin
@testset "DiagonalArray (eltype=$elt)" for elt in (
Float32, Float64, Complex{Float32}, Complex{Float64}
Expand Down Expand Up @@ -46,5 +48,9 @@ using SparseArraysBase: SparseArrayDOK, storedlength
@test storedlength(a_dest) == 2
@test a_dest isa SparseArrayDOK{elt,2}
end
@testset "diagonal" begin
@test @inferred(diagonal(rand(2))) isa AbstractMatrix
@test diagonal(zeros(Int, 2)) isa Diagonal
end
end
end

0 comments on commit fa39f45

Please sign in to comment.