Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add diagonal as a generalization of LinearAlgebra.Diagonal #4

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]> 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]
```
20 changes: 20 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,21 @@ function setdiagindices!(a::AbstractArray, v, i::Colon)
diagview(a) .= v
return a
end

"""
diagonal(v::AbstractVector, [axes]) -> 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`.
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
lkdvos marked this conversation as resolved.
Show resolved Hide resolved
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
Loading