A n-dimensional diagonal array type in Julia.
This package resides in the ITensor/ITensorRegistry
local registry.
In order to install, simply add that registry through your package manager.
This step is only required once.
julia> using Pkg: Pkg
julia> Pkg.Registry.add(url="https://github.com/ITensor/ITensorRegistry")
or:
julia> Pkg.Registry.add(url="[email protected]:ITensor/ITensorRegistry.git")
if you want to use SSH credentials, which can make it so you don't have to enter your Github ursername and password when registering packages.
Then, the package can be added as usual through the package manager:
julia> Pkg.add("DiagonalArrays")
using DiagonalArrays:
DiagonalArray, DiagonalMatrix, DiagIndex, DiagIndices, diaglength, isdiagindex
using Test: @test
function main()
d = DiagonalMatrix([1.0, 2.0, 3.0])
@test eltype(d) == Float64
@test diaglength(d) == 3
@test size(d) == (3, 3)
@test d[1, 1] == 1
@test d[2, 2] == 2
@test d[3, 3] == 3
@test d[1, 2] == 0
d = DiagonalArray([1.0, 2.0, 3.0], 3, 4, 5)
@test eltype(d) == Float64
@test diaglength(d) == 3
@test d[1, 1, 1] == 1
@test d[2, 2, 2] == 2
@test d[3, 3, 3] == 3
@test d[1, 2, 1] == 0
d[2, 2, 2] = 22
@test d[2, 2, 2] == 22
d_r = reshape(d, 3, 20)
@test size(d_r) == (3, 20)
@test all(I -> d_r[I] == d[I], LinearIndices(d))
@test length(d[DiagIndices(:)]) == 3
@test Array(d) == d
@test d[DiagIndex(2)] == d[2, 2, 2]
d[DiagIndex(2)] = 222
@test d[2, 2, 2] == 222
a = randn(3, 4, 5)
new_diag = randn(3)
a[DiagIndices(:)] = new_diag
d[DiagIndices(:)] = a[DiagIndices(:)]
@test a[DiagIndices(:)] == new_diag
@test d[DiagIndices(:)] == new_diag
permuted_d = permutedims(d, (3, 2, 1))
@test permuted_d isa DiagonalArray
@test permuted_d[DiagIndices(:)] == d[DiagIndices(:)]
@test size(d) == (3, 4, 5)
@test size(permuted_d) == (5, 4, 3)
for I in eachindex(d)
if !isdiagindex(d, I)
@test iszero(d[I])
else
@test !iszero(d[I])
end
end
mapped_d = map(x -> 2x, d)
@test mapped_d isa DiagonalArray
@test mapped_d == map(x -> 2x, Array(d))
return nothing
end
main()
This page was generated using Literate.jl.