diff --git a/Project.toml b/Project.toml index 23b1034..7b5957b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,15 +1,17 @@ name = "DiagonalArrays" uuid = "74fd4be6-21e2-4f6f-823a-4360d37c7a77" authors = ["ITensor developers 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" diff --git a/docs/make.jl b/docs/make.jl index 928d37f..e33ea5f 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -14,7 +14,7 @@ makedocs(; edit_link="main", assets=String[], ), - pages=["Home" => "index.md"], + pages=["Home" => "index.md", "Library" => "library.md"], ) deploydocs(; diff --git a/docs/src/library.md b/docs/src/library.md new file mode 100644 index 0000000..922e191 --- /dev/null +++ b/docs/src/library.md @@ -0,0 +1,5 @@ +# Library + +```@autodocs +Modules = [DiagonalArrays] +``` diff --git a/src/diaginterface/diaginterface.jl b/src/diaginterface/diaginterface.jl index da448a0..0d96692 100644 --- a/src/diaginterface/diaginterface.jl +++ b/src/diaginterface/diaginterface.jl @@ -1,5 +1,7 @@ # TODO: Turn these into `@interface ::AbstractDiagonalArrayInterface` functions. +using LinearAlgebra: LinearAlgebra + diaglength(a::AbstractArray{<:Any,0}) = 1 function diaglength(a::AbstractArray) @@ -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) diff --git a/test/Project.toml b/test/Project.toml index d3e461d..ce7dbf9 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -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" diff --git a/test/test_basics.jl b/test/test_basics.jl index 93afe96..54bef21 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -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} @@ -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