From 8112fa916f13a2a4ec13ee9d708c6c004095fea3 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 9 Jan 2025 15:10:31 -0500 Subject: [PATCH 1/7] Add `diagonal` --- src/diaginterface/diaginterface.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/diaginterface/diaginterface.jl b/src/diaginterface/diaginterface.jl index da448a0..e2f89dc 100644 --- a/src/diaginterface/diaginterface.jl +++ b/src/diaginterface/diaginterface.jl @@ -88,3 +88,11 @@ 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 a replacement of `LinearAlgebra.Diagonal` +that does not imply an output type. Defaults to `Diagonal(v)`. +""" +diagonal(A::AbstractVector) = LinearAlgebra.Diagonal(A) From 791f2e21b20c749342a6799fccb6d777f249b9a7 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 9 Jan 2025 19:14:06 -0500 Subject: [PATCH 2/7] Add docs page --- docs/make.jl | 2 +- docs/src/library.md | 5 +++++ src/diaginterface/diaginterface.jl | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 docs/src/library.md 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 e2f89dc..609b4a0 100644 --- a/src/diaginterface/diaginterface.jl +++ b/src/diaginterface/diaginterface.jl @@ -90,7 +90,7 @@ function setdiagindices!(a::AbstractArray, v, i::Colon) end """ - diagonal(v::AbstractVector) -> AbstractMatrix + diagonal(v::AbstractVector) -> 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)`. From 72ef30698151e5690e679b5d5dc0e63133be37d4 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 9 Jan 2025 19:19:41 -0500 Subject: [PATCH 3/7] Add LinearAlgebra dependency --- Project.toml | 2 ++ src/diaginterface/diaginterface.jl | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Project.toml b/Project.toml index 23b1034..10e8d6f 100644 --- a/Project.toml +++ b/Project.toml @@ -6,10 +6,12 @@ version = "0.2.1" [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/src/diaginterface/diaginterface.jl b/src/diaginterface/diaginterface.jl index 609b4a0..230e301 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) From 772f22355892c293432834b5046f1ca1d6edb7e5 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Thu, 9 Jan 2025 19:19:50 -0500 Subject: [PATCH 4/7] Add (dummy) test --- test/Project.toml | 1 + test/test_basics.jl | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) 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 From 9a5355e9df53c668b3bb13f54b5828c1d332aa33 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Fri, 10 Jan 2025 09:09:48 -0500 Subject: [PATCH 5/7] Update `diagonal` to support specifying axes --- src/diaginterface/diaginterface.jl | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/diaginterface/diaginterface.jl b/src/diaginterface/diaginterface.jl index 230e301..a67548a 100644 --- a/src/diaginterface/diaginterface.jl +++ b/src/diaginterface/diaginterface.jl @@ -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 = axes(v, 1) # Base.axes1 is private + return diagonal(v, (ax, ax)) +end +function diagonal(v::AbstractVector, ax) + vax = axes(v, 1) + ax == (vax, vax) || throw(ArgumentError(lazy"Cannot create a `Diagonal` with axes $ax")) + return LinearAlgebra.Diagonal(v) +end From ea8d8e382783dc432f9646d3781ebca96e5e39a0 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Fri, 10 Jan 2025 09:42:19 -0500 Subject: [PATCH 6/7] Bump version v0.2.2 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 10e8d6f..7b5957b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ 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" From 99e3475f7cde20cae5791bf9eb1a8f0cf3275be6 Mon Sep 17 00:00:00 2001 From: Lukas Devos Date: Fri, 10 Jan 2025 12:18:24 -0500 Subject: [PATCH 7/7] revert `diagonal` extension to `axes` --- src/diaginterface/diaginterface.jl | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/diaginterface/diaginterface.jl b/src/diaginterface/diaginterface.jl index a67548a..0d96692 100644 --- a/src/diaginterface/diaginterface.jl +++ b/src/diaginterface/diaginterface.jl @@ -92,19 +92,10 @@ function setdiagindices!(a::AbstractArray, v, i::Colon) end """ - diagonal(v::AbstractVector, [axes]) -> AbstractMatrix + diagonal(v::AbstractVector) -> AbstractMatrix -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`. +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)`. """ -function diagonal(v::AbstractVector) - ax = axes(v, 1) # Base.axes1 is private - return diagonal(v, (ax, ax)) -end -function diagonal(v::AbstractVector, ax) - vax = axes(v, 1) - ax == (vax, vax) || throw(ArgumentError(lazy"Cannot create a `Diagonal` with axes $ax")) - return LinearAlgebra.Diagonal(v) -end +diagonal(v::AbstractVector) = LinearAlgebra.Diagonal(v)