Skip to content

Commit fa39f45

Browse files
authored
Add diagonal as a generalization of LinearAlgebra.Diagonal (#4)
1 parent 2bd0713 commit fa39f45

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
name = "DiagonalArrays"
22
uuid = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
33
authors = ["ITensor developers <support@itensor.org> and contributors"]
4-
version = "0.2.1"
4+
version = "0.2.2"
55

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

1112
[compat]
1213
ArrayLayouts = "1.10.4"
1314
DerivableInterfaces = "0.3.7"
15+
LinearAlgebra = "1.10.0"
1416
SparseArraysBase = "0.2.1"
1517
julia = "1.10"

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ makedocs(;
1414
edit_link="main",
1515
assets=String[],
1616
),
17-
pages=["Home" => "index.md"],
17+
pages=["Home" => "index.md", "Library" => "library.md"],
1818
)
1919

2020
deploydocs(;

docs/src/library.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Library
2+
3+
```@autodocs
4+
Modules = [DiagonalArrays]
5+
```

src/diaginterface/diaginterface.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# TODO: Turn these into `@interface ::AbstractDiagonalArrayInterface` functions.
22

3+
using LinearAlgebra: LinearAlgebra
4+
35
diaglength(a::AbstractArray{<:Any,0}) = 1
46

57
function diaglength(a::AbstractArray)
@@ -88,3 +90,12 @@ function setdiagindices!(a::AbstractArray, v, i::Colon)
8890
diagview(a) .= v
8991
return a
9092
end
93+
94+
"""
95+
diagonal(v::AbstractVector) -> AbstractMatrix
96+
97+
Return a diagonal matrix from a vector `v`.
98+
This is an extension of `LinearAlgebra.Diagonal`, designed to avoid the implication of the output type.
99+
Defaults to `Diagonal(v)`.
100+
"""
101+
diagonal(v::AbstractVector) = LinearAlgebra.Diagonal(v)

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[deps]
22
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
DiagonalArrays = "74fd4be6-21e2-4f6f-823a-4360d37c7a77"
4+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
45
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
56
SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208"
67
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"

test/test_basics.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using Test: @test, @testset, @test_broken
2-
using DiagonalArrays: DiagonalArrays, DiagonalArray, DiagonalMatrix, diaglength
1+
using Test: @test, @testset, @test_broken, @inferred
2+
using DiagonalArrays: DiagonalArrays, DiagonalArray, DiagonalMatrix, diaglength, diagonal
33
using SparseArraysBase: SparseArrayDOK, storedlength
4+
using LinearAlgebra: Diagonal
5+
46
@testset "Test DiagonalArrays" begin
57
@testset "DiagonalArray (eltype=$elt)" for elt in (
68
Float32, Float64, Complex{Float32}, Complex{Float64}
@@ -46,5 +48,9 @@ using SparseArraysBase: SparseArrayDOK, storedlength
4648
@test storedlength(a_dest) == 2
4749
@test a_dest isa SparseArrayDOK{elt,2}
4850
end
51+
@testset "diagonal" begin
52+
@test @inferred(diagonal(rand(2))) isa AbstractMatrix
53+
@test diagonal(zeros(Int, 2)) isa Diagonal
54+
end
4955
end
5056
end

0 commit comments

Comments
 (0)