From 72f0d7136b830cbd859bb811aa993e937a76f18e Mon Sep 17 00:00:00 2001 From: mtfishman Date: Wed, 27 Nov 2024 00:13:33 -0500 Subject: [PATCH] Try fixing tests --- Project.toml | 20 +++++++ docs/Project.toml | 8 ++- examples/Project.toml | 6 ++ examples/example_readme.jl | 29 --------- .../{src => }/NamedDimsArraysAdaptExt.jl | 0 .../{src => }/adapt_structure.jl | 2 +- ext/NamedDimsArraysAdaptExt/test/Project.toml | 4 -- ext/NamedDimsArraysAdaptExt/test/runtests.jl | 13 ---- .../NamedDimsArraysSparseArraysBaseExt.jl | 0 .../{src => }/densearray.jl | 2 +- .../test/Project.toml | 4 -- .../test/runtests.jl | 12 ---- .../NamedDimsArraysTensorAlgebraExt.jl | 2 - .../{src => }/contract.jl | 2 +- .../{src => }/fusedims.jl | 5 +- .../{src => }/qr.jl | 3 +- .../src/eigen.jl | 47 --------------- .../src/svd.jl | 53 ---------------- .../test/Project.toml | 3 - .../test/runtests.jl | 59 ------------------ generate_readme.jl | 10 ---- src/NamedDimsArrays.jl | 13 ++-- src/abstractnameddimsarray.jl | 6 +- src/broadcast.jl | 2 +- test/Project.toml | 15 ++++- test/test_NDTensorsNamedDimsArraysExt.jl | 33 ---------- test/test_NamedDimsArraysAdaptExt.jl | 16 +++-- ...test_NamedDimsArraysSparseArraysBaseExt.jl | 15 +++-- test/test_NamedDimsArraysTensorAlgebraExt.jl | 60 ++++++++++++++++++- test/test_basic.jl | 2 +- test/test_tensoralgebra.jl | 2 +- 31 files changed, 146 insertions(+), 302 deletions(-) delete mode 100644 examples/example_readme.jl rename ext/NamedDimsArraysAdaptExt/{src => }/NamedDimsArraysAdaptExt.jl (100%) rename ext/NamedDimsArraysAdaptExt/{src => }/adapt_structure.jl (64%) delete mode 100644 ext/NamedDimsArraysAdaptExt/test/Project.toml delete mode 100644 ext/NamedDimsArraysAdaptExt/test/runtests.jl rename ext/NamedDimsArraysSparseArraysBaseExt/{src => }/NamedDimsArraysSparseArraysBaseExt.jl (100%) rename ext/NamedDimsArraysSparseArraysBaseExt/{src => }/densearray.jl (85%) delete mode 100644 ext/NamedDimsArraysSparseArraysBaseExt/test/Project.toml delete mode 100644 ext/NamedDimsArraysSparseArraysBaseExt/test/runtests.jl rename ext/NamedDimsArraysTensorAlgebraExt/{src => }/NamedDimsArraysTensorAlgebraExt.jl (73%) rename ext/NamedDimsArraysTensorAlgebraExt/{src => }/contract.jl (90%) rename ext/NamedDimsArraysTensorAlgebraExt/{src => }/fusedims.jl (93%) rename ext/NamedDimsArraysTensorAlgebraExt/{src => }/qr.jl (83%) delete mode 100644 ext/NamedDimsArraysTensorAlgebraExt/src/eigen.jl delete mode 100644 ext/NamedDimsArraysTensorAlgebraExt/src/svd.jl delete mode 100644 ext/NamedDimsArraysTensorAlgebraExt/test/Project.toml delete mode 100644 ext/NamedDimsArraysTensorAlgebraExt/test/runtests.jl delete mode 100644 generate_readme.jl delete mode 100644 test/test_NDTensorsNamedDimsArraysExt.jl diff --git a/Project.toml b/Project.toml index 2e54b80..d2adbb2 100644 --- a/Project.toml +++ b/Project.toml @@ -3,8 +3,28 @@ uuid = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" authors = ["ITensor developers and contributors"] version = "0.1.0" +[deps] +Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" +BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +NestedPermutedDimsArrays = "2c2a8ec4-3cfc-4276-aa3e-1307b4294e58" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" +TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" +TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" + +[sources] +BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} +NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"} +SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"} +TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"} +TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} + [compat] +Adapt = "4.1.1" Aqua = "0.8.9" +LinearAlgebra = "1.10" +Random = "1.10" Test = "1.10" julia = "1.10" diff --git a/docs/Project.toml b/docs/Project.toml index 1093c4c..46b9a39 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,4 +1,10 @@ [deps] -NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" +BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" +NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" +TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" + +[sources] +BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} +TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} diff --git a/examples/Project.toml b/examples/Project.toml index e93948f..96eef19 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -1,2 +1,8 @@ [deps] +BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2" NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" +TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" + +[sources] +BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} +TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} diff --git a/examples/example_readme.jl b/examples/example_readme.jl deleted file mode 100644 index 11e9b9e..0000000 --- a/examples/example_readme.jl +++ /dev/null @@ -1,29 +0,0 @@ -# # NamedDimsArrays.jl - -using NamedDimsArrays: align, dimnames, named, unname -using TensorAlgebra: contract - -## Named dimensions -i = named(2, "i") -j = named(2, "j") -k = named(2, "k") - -## Arrays with named dimensions -na1 = randn(i, j) -na2 = randn(j, k) - -@show dimnames(na1) == ("i", "j") - -## Indexing -@show na1[j => 2, i => 1] == na1[1, 2] - -## Tensor contraction -na_dest = contract(na1, na2) - -@show issetequal(dimnames(na_dest), ("i", "k")) -## `unname` removes the names and returns an `Array` -@show unname(na_dest, (i, k)) ≈ unname(na1) * unname(na2) - -## Permute dimensions (like `ITensors.permute`) -na1 = align(na1, (j, i)) -@show na1[i => 1, j => 2] == na1[2, 1] diff --git a/ext/NamedDimsArraysAdaptExt/src/NamedDimsArraysAdaptExt.jl b/ext/NamedDimsArraysAdaptExt/NamedDimsArraysAdaptExt.jl similarity index 100% rename from ext/NamedDimsArraysAdaptExt/src/NamedDimsArraysAdaptExt.jl rename to ext/NamedDimsArraysAdaptExt/NamedDimsArraysAdaptExt.jl diff --git a/ext/NamedDimsArraysAdaptExt/src/adapt_structure.jl b/ext/NamedDimsArraysAdaptExt/adapt_structure.jl similarity index 64% rename from ext/NamedDimsArraysAdaptExt/src/adapt_structure.jl rename to ext/NamedDimsArraysAdaptExt/adapt_structure.jl index 97c075d..23de652 100644 --- a/ext/NamedDimsArraysAdaptExt/src/adapt_structure.jl +++ b/ext/NamedDimsArraysAdaptExt/adapt_structure.jl @@ -1,5 +1,5 @@ using Adapt: Adapt, adapt -using NDTensors.NamedDimsArrays: AbstractNamedDimsArray, dimnames, named, unname +using ..NamedDimsArrays: AbstractNamedDimsArray, dimnames, named, unname function Adapt.adapt_structure(to, na::AbstractNamedDimsArray) return named(adapt(to, unname(na)), dimnames(na)) diff --git a/ext/NamedDimsArraysAdaptExt/test/Project.toml b/ext/NamedDimsArraysAdaptExt/test/Project.toml deleted file mode 100644 index a970d0f..0000000 --- a/ext/NamedDimsArraysAdaptExt/test/Project.toml +++ /dev/null @@ -1,4 +0,0 @@ -[deps] -Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/ext/NamedDimsArraysAdaptExt/test/runtests.jl b/ext/NamedDimsArraysAdaptExt/test/runtests.jl deleted file mode 100644 index 733da91..0000000 --- a/ext/NamedDimsArraysAdaptExt/test/runtests.jl +++ /dev/null @@ -1,13 +0,0 @@ -@eval module $(gensym()) -using Test: @test, @testset -using Adapt: adapt -using NDTensors.NamedDimsArrays: named -@testset "NamedDimsArraysAdaptExt (eltype=$elt)" for elt in ( - Float32, Float64, Complex{Float32}, Complex{Float64} -) - na = named(randn(2, 2), ("i", "j")) - na_complex = adapt(Array{complex(elt)}, na) - @test na ≈ na_complex - @test eltype(na_complex) === complex(elt) -end -end diff --git a/ext/NamedDimsArraysSparseArraysBaseExt/src/NamedDimsArraysSparseArraysBaseExt.jl b/ext/NamedDimsArraysSparseArraysBaseExt/NamedDimsArraysSparseArraysBaseExt.jl similarity index 100% rename from ext/NamedDimsArraysSparseArraysBaseExt/src/NamedDimsArraysSparseArraysBaseExt.jl rename to ext/NamedDimsArraysSparseArraysBaseExt/NamedDimsArraysSparseArraysBaseExt.jl diff --git a/ext/NamedDimsArraysSparseArraysBaseExt/src/densearray.jl b/ext/NamedDimsArraysSparseArraysBaseExt/densearray.jl similarity index 85% rename from ext/NamedDimsArraysSparseArraysBaseExt/src/densearray.jl rename to ext/NamedDimsArraysSparseArraysBaseExt/densearray.jl index db9da0c..9370ad0 100644 --- a/ext/NamedDimsArraysSparseArraysBaseExt/src/densearray.jl +++ b/ext/NamedDimsArraysSparseArraysBaseExt/densearray.jl @@ -1,5 +1,5 @@ using ..NamedDimsArrays: AbstractNamedDimsArray, dimnames, named, unname -using ...SparseArraysBase: SparseArraysBase, densearray +using SparseArraysBase: SparseArraysBase, densearray # TODO: Use `Adapt` or some kind of rewrap function like in # ArrayInterface.jl (https://github.com/JuliaArrays/ArrayInterface.jl/issues/136) diff --git a/ext/NamedDimsArraysSparseArraysBaseExt/test/Project.toml b/ext/NamedDimsArraysSparseArraysBaseExt/test/Project.toml deleted file mode 100644 index c13dac3..0000000 --- a/ext/NamedDimsArraysSparseArraysBaseExt/test/Project.toml +++ /dev/null @@ -1,4 +0,0 @@ -[deps] -LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" -NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/ext/NamedDimsArraysSparseArraysBaseExt/test/runtests.jl b/ext/NamedDimsArraysSparseArraysBaseExt/test/runtests.jl deleted file mode 100644 index bc87d64..0000000 --- a/ext/NamedDimsArraysSparseArraysBaseExt/test/runtests.jl +++ /dev/null @@ -1,12 +0,0 @@ -@eval module $(gensym()) -using LinearAlgebra: Diagonal -using Test: @test, @testset -using NDTensors.SparseArraysBase: densearray -using NDTensors.NamedDimsArrays: named, unname -@testset "NamedDimsArraysSparseArraysBaseExt (eltype=$elt)" for elt in (Float32, Float64) - na = named(Diagonal(randn(2)), ("i", "j")) - na_dense = densearray(na) - @test na ≈ na_dense - @test unname(na_dense) isa Array -end -end diff --git a/ext/NamedDimsArraysTensorAlgebraExt/src/NamedDimsArraysTensorAlgebraExt.jl b/ext/NamedDimsArraysTensorAlgebraExt/NamedDimsArraysTensorAlgebraExt.jl similarity index 73% rename from ext/NamedDimsArraysTensorAlgebraExt/src/NamedDimsArraysTensorAlgebraExt.jl rename to ext/NamedDimsArraysTensorAlgebraExt/NamedDimsArraysTensorAlgebraExt.jl index a397507..42000e0 100644 --- a/ext/NamedDimsArraysTensorAlgebraExt/src/NamedDimsArraysTensorAlgebraExt.jl +++ b/ext/NamedDimsArraysTensorAlgebraExt/NamedDimsArraysTensorAlgebraExt.jl @@ -2,6 +2,4 @@ module NamedDimsArraysTensorAlgebraExt include("contract.jl") include("fusedims.jl") include("qr.jl") -include("eigen.jl") -include("svd.jl") end diff --git a/ext/NamedDimsArraysTensorAlgebraExt/src/contract.jl b/ext/NamedDimsArraysTensorAlgebraExt/contract.jl similarity index 90% rename from ext/NamedDimsArraysTensorAlgebraExt/src/contract.jl rename to ext/NamedDimsArraysTensorAlgebraExt/contract.jl index f45391e..598aa3a 100644 --- a/ext/NamedDimsArraysTensorAlgebraExt/src/contract.jl +++ b/ext/NamedDimsArraysTensorAlgebraExt/contract.jl @@ -1,5 +1,5 @@ using ..NamedDimsArrays: AbstractNamedDimsArray, dimnames, named, unname -using ...TensorAlgebra: TensorAlgebra, blockedperms, contract, contract! +using TensorAlgebra: TensorAlgebra, blockedperms, contract, contract! function TensorAlgebra.contract!( na_dest::AbstractNamedDimsArray, diff --git a/ext/NamedDimsArraysTensorAlgebraExt/src/fusedims.jl b/ext/NamedDimsArraysTensorAlgebraExt/fusedims.jl similarity index 93% rename from ext/NamedDimsArraysTensorAlgebraExt/src/fusedims.jl rename to ext/NamedDimsArraysTensorAlgebraExt/fusedims.jl index 9b02479..50d6627 100644 --- a/ext/NamedDimsArraysTensorAlgebraExt/src/fusedims.jl +++ b/ext/NamedDimsArraysTensorAlgebraExt/fusedims.jl @@ -1,5 +1,6 @@ -using ...NDTensors.TensorAlgebra: TensorAlgebra, blockedperm, fusedims, splitdims -using ...NDTensors.TensorAlgebra.BaseExtensions: BaseExtensions +using ..NamedDimsArrays: name +using TensorAlgebra: TensorAlgebra, blockedperm, fusedims, splitdims +using TensorAlgebra.BaseExtensions: BaseExtensions function TensorAlgebra.blockedperm(na::AbstractNamedDimsArray, nameddim_blocks::Tuple...) # Extract names if named dimensions or axes were passed diff --git a/ext/NamedDimsArraysTensorAlgebraExt/src/qr.jl b/ext/NamedDimsArraysTensorAlgebraExt/qr.jl similarity index 83% rename from ext/NamedDimsArraysTensorAlgebraExt/src/qr.jl rename to ext/NamedDimsArraysTensorAlgebraExt/qr.jl index 329b985..660e245 100644 --- a/ext/NamedDimsArraysTensorAlgebraExt/src/qr.jl +++ b/ext/NamedDimsArraysTensorAlgebraExt/qr.jl @@ -1,6 +1,5 @@ -# using ..ITensors: IndexID using LinearAlgebra: LinearAlgebra, qr -using ...NDTensors.NamedDimsArrays: AbstractNamedDimsArray, dimnames, name, randname, unname +using ..NamedDimsArrays: AbstractNamedDimsArray, dimnames, name, randname, unname function LinearAlgebra.qr(na::AbstractNamedDimsArray; positive=nothing) return qr(na, (dimnames(na, 1),), (dimnames(na, 2),); positive) diff --git a/ext/NamedDimsArraysTensorAlgebraExt/src/eigen.jl b/ext/NamedDimsArraysTensorAlgebraExt/src/eigen.jl deleted file mode 100644 index 31a7e6d..0000000 --- a/ext/NamedDimsArraysTensorAlgebraExt/src/eigen.jl +++ /dev/null @@ -1,47 +0,0 @@ -## using ..ITensors: IndexID -using LinearAlgebra: LinearAlgebra, Diagonal, Hermitian, eigen -## using ..NDTensors.DiagonalArrays: DiagonalMatrix -using ...NDTensors.NamedDimsArrays: AbstractNamedDimsArray, dimnames, name, unname -using ...NDTensors.RankFactorization: Spectrum, truncate!! -function LinearAlgebra.eigen( - na::Hermitian{T,<:AbstractNamedDimsArray{T}}; - mindim=nothing, - maxdim=nothing, - cutoff=nothing, - use_absolute_cutoff=nothing, - use_relative_cutoff=nothing, -) where {T<:Union{Real,Complex}} - # TODO: Handle array wrappers around - # `AbstractNamedDimsArray` more elegantly. - d, u = eigen(Hermitian(unname(parent(na)))) - - # Sort by largest to smallest eigenvalues - # TODO: Replace `cpu` with `Expose` dispatch. - p = sortperm(d; rev=true, by=abs) - d = d[p] - u = u[:, p] - - length_d = length(d) - truncerr = zero(Float64) # Make more generic - if any(!isnothing, (maxdim, cutoff)) - d, truncerr, _ = truncate!!( - d; mindim, maxdim, cutoff, use_absolute_cutoff, use_relative_cutoff - ) - length_d = length(d) - if length_d < size(u, 2) - u = u[:, 1:length_d] - end - end - spec = Spectrum(d, truncerr) - - # TODO: Handle array wrappers more generally. - names_a = dimnames(parent(na)) - # TODO: Make this more generic, handle `dag`, etc. - l = randname(names_a[1]) # IndexID(rand(UInt64), "", 0) - r = randname(names_a[2]) # IndexID(rand(UInt64), "", 0) - names_d = (l, r) - nd = named(Diagonal(d), names_d) - names_u = (names_a[2], r) - nu = named(u, names_u) - return nd, nu, spec -end diff --git a/ext/NamedDimsArraysTensorAlgebraExt/src/svd.jl b/ext/NamedDimsArraysTensorAlgebraExt/src/svd.jl deleted file mode 100644 index 787f89e..0000000 --- a/ext/NamedDimsArraysTensorAlgebraExt/src/svd.jl +++ /dev/null @@ -1,53 +0,0 @@ -using LinearAlgebra: LinearAlgebra, svd -using ...NDTensors.RankFactorization: Spectrum, truncate!! -function LinearAlgebra.svd( - na::AbstractNamedDimsArray; - mindim=nothing, - maxdim=nothing, - cutoff=nothing, - use_absolute_cutoff=nothing, - use_relative_cutoff=nothing, - alg=nothing, - min_blockdim=nothing, -) - # TODO: Handle array wrappers around - # `AbstractNamedDimsArray` more elegantly. - USV = svd(unname(na)) - u, s, v = USV.U, USV.S, USV.Vt - - # Sort by largest to smallest eigenvalues - # TODO: Replace `cpu` with `Expose` dispatch. - p = sortperm(s; rev=true, by=abs) - u = u[:, p] - s = s[p] - v = v[p, :] - - s² = s .^ 2 - length_s = length(s) - truncerr = zero(Float64) # Make more generic - if any(!isnothing, (maxdim, cutoff)) - s², truncerr, _ = truncate!!( - s²; mindim, maxdim, cutoff, use_absolute_cutoff, use_relative_cutoff - ) - length_s = length(s²) - # TODO: Avoid this if they are already the - # correct size. - u = u[:, 1:length_s] - s = s[1:length_s] - v = v[1:length_s, :] - end - spec = Spectrum(s², truncerr) - - # TODO: Handle array wrappers more generally. - names_a = dimnames(na) - # TODO: Make this more generic, handle `dag`, etc. - l = randname(names_a[1]) # IndexID(rand(UInt64), "", 0) - r = randname(names_a[2]) # IndexID(rand(UInt64), "", 0) - names_u = (names_a[1], l) - nu = named(u, names_u) - names_s = (l, r) - ns = named(Diagonal(s), names_s) - names_v = (r, names_a[2]) - nv = named(v, names_v) - return nu, ns, nv, spec -end diff --git a/ext/NamedDimsArraysTensorAlgebraExt/test/Project.toml b/ext/NamedDimsArraysTensorAlgebraExt/test/Project.toml deleted file mode 100644 index ef491a5..0000000 --- a/ext/NamedDimsArraysTensorAlgebraExt/test/Project.toml +++ /dev/null @@ -1,3 +0,0 @@ -[deps] -NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" -Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" diff --git a/ext/NamedDimsArraysTensorAlgebraExt/test/runtests.jl b/ext/NamedDimsArraysTensorAlgebraExt/test/runtests.jl deleted file mode 100644 index a73edfd..0000000 --- a/ext/NamedDimsArraysTensorAlgebraExt/test/runtests.jl +++ /dev/null @@ -1,59 +0,0 @@ -@eval module $(gensym()) -using Test: @test, @testset, @test_broken -using NDTensors.NamedDimsArrays: named, unname -using NDTensors.TensorAlgebra: TensorAlgebra, contract, fusedims, splitdims -using LinearAlgebra: qr -elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) -@testset "NamedDimsArraysTensorAlgebraExt (eltype=$(elt))" for elt in elts - @testset "contract" begin - i = named(2, "i") - j = named(2, "j") - k = named(2, "k") - na1 = randn(elt, i, j) - na2 = randn(elt, j, k) - na_dest = TensorAlgebra.contract(na1, na2) - @test eltype(na_dest) === elt - @test unname(na_dest, (i, k)) ≈ unname(na1) * unname(na2) - end - @testset "fusedims" begin - i, j, k, l = named.((2, 3, 4, 5), ("i", "j", "k", "l")) - na = randn(elt, i, j, k, l) - na_fused = fusedims(na, (k, i) => "a", (j, l) => "b") - # Fuse all dimensions. - @test unname(na_fused, ("a", "b")) ≈ - reshape(unname(na, (k, i, j, l)), (unname(k) * unname(i), unname(j) * unname(l))) - na_fused = fusedims(na, (k, i) => "a") - # Fuse a subset of dimensions. - @test unname(na_fused, ("a", "j", "l")) ≈ - reshape(unname(na, (k, i, j, l)), (unname(k) * unname(i), unname(j), unname(l))) - end - @testset "splitdims" begin - a, b = named.((6, 20), ("a", "b")) - i, j, k, l = named.((2, 3, 4, 5), ("i", "j", "k", "l")) - na = randn(elt, a, b) - # Split all dimensions. - na_split = splitdims(na, "a" => (k, i), "b" => (j, l)) - @test unname(na_split, ("k", "i", "j", "l")) ≈ - reshape(unname(na, ("a", "b")), (unname(k), unname(i), unname(j), unname(l))) - # Split a subset of dimensions. - na_split = splitdims(na, "a" => (j, i)) - @test unname(na_split, ("j", "i", "b")) ≈ - reshape(unname(na, ("a", "b")), (unname(j), unname(i), unname(b))) - end - @testset "qr" begin - dims = (2, 2, 2, 2) - i, j, k, l = named.(dims, ("i", "j", "k", "l")) - - na = randn(elt, i, j) - # TODO: Should this be allowed? - # TODO: Add support for specifying new name. - q, r = qr(na) - @test q * r ≈ na - - na = randn(elt, i, j, k, l) - # TODO: Add support for specifying new name. - q, r = qr(na, (i, k), (j, l)) - @test contract(q, r) ≈ na - end -end -end diff --git a/generate_readme.jl b/generate_readme.jl deleted file mode 100644 index 889a169..0000000 --- a/generate_readme.jl +++ /dev/null @@ -1,10 +0,0 @@ -using Literate -using NDTensors.NamedDimsArrays: NamedDimsArrays -Literate.markdown( - joinpath( - pkgdir(NamedDimsArrays), "src", "NamedDimsArrays", "examples", "example_readme.jl" - ), - joinpath(pkgdir(NamedDimsArrays), "src", "NamedDimsArrays"); - flavor=Literate.CommonMarkFlavor(), - name="README", -) diff --git a/src/NamedDimsArrays.jl b/src/NamedDimsArrays.jl index 71d1a78..f776a2f 100644 --- a/src/NamedDimsArrays.jl +++ b/src/NamedDimsArrays.jl @@ -1,4 +1,5 @@ module NamedDimsArrays + include("traits.jl") include("name.jl") include("randname.jl") @@ -18,10 +19,10 @@ include("map.jl") include("broadcast_shape.jl") include("broadcast.jl") -# Extensions -include("../ext/NamedDimsArraysAdaptExt/src/NamedDimsArraysAdaptExt.jl") -include( - "../ext/NamedDimsArraysSparseArraysBaseExt/src/NamedDimsArraysSparseArraysBaseExt.jl" -) -include("../ext/NamedDimsArraysTensorAlgebraExt/src/NamedDimsArraysTensorAlgebraExt.jl") +# Extensions. +# TODO: Turn these into actual package extensions. +include("../ext/NamedDimsArraysAdaptExt/NamedDimsArraysAdaptExt.jl") +include("../ext/NamedDimsArraysSparseArraysBaseExt/NamedDimsArraysSparseArraysBaseExt.jl") +include("../ext/NamedDimsArraysTensorAlgebraExt/NamedDimsArraysTensorAlgebraExt.jl") + end diff --git a/src/abstractnameddimsarray.jl b/src/abstractnameddimsarray.jl index a73763b..411d146 100644 --- a/src/abstractnameddimsarray.jl +++ b/src/abstractnameddimsarray.jl @@ -1,5 +1,4 @@ -using ..BaseExtensions: BaseExtensions -using NDTensors.TypeParameterAccessors: TypeParameterAccessors, parenttype +using TypeParameterAccessors: TypeParameterAccessors, parenttype # Some of the interface is inspired by: # https://github.com/invenia/NamedDims.jl @@ -70,8 +69,7 @@ rename(a::AbstractNamedDimsArray, names) = named(unname(a), names) # `rename` in `NamedPlus.jl`. # TODO: Define for `AbstractArray`. function replacenames(na::AbstractNamedDimsArray, replacements::Pair...) - # `BaseExtension.replace` needed for `Tuple` support on Julia 1.6 and older. - return named(unname(na), BaseExtensions.replace(dimnames(na), replacements...)) + return named(unname(na), replace(dimnames(na), replacements...)) end # Either define new names or replace names diff --git a/src/broadcast.jl b/src/broadcast.jl index d2c1198..1749a94 100644 --- a/src/broadcast.jl +++ b/src/broadcast.jl @@ -1,5 +1,5 @@ using Base.Broadcast: BroadcastStyle, AbstractArrayStyle, DefaultArrayStyle, Broadcasted -using ..BroadcastMapConversion: map_function, map_args +using BroadcastMapConversion: map_function, map_args struct NamedDimsArrayStyle{N} <: AbstractArrayStyle{N} end diff --git a/test/Project.toml b/test/Project.toml index 18bbc81..fb350ac 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,3 +1,16 @@ [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" -NDTensors = "23ae76d9-e61a-49c4-8f12-3f1a16adf9cf" +BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2" +NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" +NestedPermutedDimsArrays = "2c2a8ec4-3cfc-4276-aa3e-1307b4294e58" +SparseArraysBase = "0d5efcca-f356-4864-8770-e1ed8d78f208" +TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" +TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" + +[sources] +BroadcastMapConversion = {url = "https://github.com/ITensor/BroadcastMapConversion.jl"} +NamedDimsArrays = {path = ".."} +NestedPermutedDimsArrays = {url = "https://github.com/ITensor/NestedPermutedDimsArrays.jl"} +SparseArraysBase = {url = "https://github.com/ITensor/SparseArraysBase.jl"} +TensorAlgebra = {url = "https://github.com/ITensor/TensorAlgebra.jl"} +TypeParameterAccessors = {url = "https://github.com/ITensor/TypeParameterAccessors.jl"} diff --git a/test/test_NDTensorsNamedDimsArraysExt.jl b/test/test_NDTensorsNamedDimsArraysExt.jl deleted file mode 100644 index 06eeb0c..0000000 --- a/test/test_NDTensorsNamedDimsArraysExt.jl +++ /dev/null @@ -1,33 +0,0 @@ -@eval module $(gensym()) -using NDTensors.NamedDimsArrays: NamedDimsArray, dimnames -using NDTensors: NDTensors -using Test: @test, @testset - -@testset "NDTensorsNamedDimsArraysExt" begin - elt = Float64 - - a = NamedDimsArray(randn(elt, 2, 2), ("i", "j")) - b = NDTensors.similar(a) - @test b isa NamedDimsArray{elt} - @test eltype(b) === elt - @test dimnames(b) == ("i", "j") - @test size(b) == (2, 2) - - a = NamedDimsArray(randn(elt, 2, 2), ("i", "j")) - b = NDTensors.similar(a, Float32) - @test b isa NamedDimsArray{Float32} - @test eltype(b) === Float32 - @test dimnames(b) == ("i", "j") - @test size(b) == (2, 2) - - a = NamedDimsArray(randn(elt, 2, 2), ("i", "j")) - b = copy(a) - α = randn(elt) - b = NDTensors.fill!!(b, α) - @test b isa NamedDimsArray{elt} - @test eltype(b) === elt - @test dimnames(b) == ("i", "j") - @test size(b) == (2, 2) - @test all(==(α), b) -end -end diff --git a/test/test_NamedDimsArraysAdaptExt.jl b/test/test_NamedDimsArraysAdaptExt.jl index 10ffe6f..4276f71 100644 --- a/test/test_NamedDimsArraysAdaptExt.jl +++ b/test/test_NamedDimsArraysAdaptExt.jl @@ -1,5 +1,13 @@ -using Test: @testset - -@testset "NamedDimsArrays $(@__FILE__)" begin - include("../ext/NamedDimsArraysAdaptExt/test/runtests.jl") +@eval module $(gensym()) +using Test: @test, @testset +using Adapt: adapt +using NamedDimsArrays: named +@testset "NamedDimsArraysAdaptExt (eltype=$elt)" for elt in ( + Float32, Float64, Complex{Float32}, Complex{Float64} +) + na = named(randn(2, 2), ("i", "j")) + na_complex = adapt(Array{complex(elt)}, na) + @test na ≈ na_complex + @test eltype(na_complex) === complex(elt) +end end diff --git a/test/test_NamedDimsArraysSparseArraysBaseExt.jl b/test/test_NamedDimsArraysSparseArraysBaseExt.jl index 5cd5ef6..176971f 100644 --- a/test/test_NamedDimsArraysSparseArraysBaseExt.jl +++ b/test/test_NamedDimsArraysSparseArraysBaseExt.jl @@ -1,5 +1,12 @@ -using Test: @testset - -@testset "NamedDimsArrays $(@__FILE__)" begin - include("../ext/NamedDimsArraysSparseArraysBaseExt/test/runtests.jl") +@eval module $(gensym()) +using LinearAlgebra: Diagonal +using Test: @test, @testset +using SparseArraysBase: densearray +using NamedDimsArrays: named, unname +@testset "NamedDimsArraysSparseArraysBaseExt (eltype=$elt)" for elt in (Float32, Float64) + na = named(Diagonal(randn(2)), ("i", "j")) + na_dense = densearray(na) + @test na ≈ na_dense + @test unname(na_dense) isa Array +end end diff --git a/test/test_NamedDimsArraysTensorAlgebraExt.jl b/test/test_NamedDimsArraysTensorAlgebraExt.jl index da5e5dd..d1af3d2 100644 --- a/test/test_NamedDimsArraysTensorAlgebraExt.jl +++ b/test/test_NamedDimsArraysTensorAlgebraExt.jl @@ -1,5 +1,59 @@ -using Test: @testset +@eval module $(gensym()) +using Test: @test, @testset, @test_broken +using NamedDimsArrays: named, unname +using TensorAlgebra: TensorAlgebra, contract, fusedims, splitdims +using LinearAlgebra: qr +elts = (Float32, Float64, Complex{Float32}, Complex{Float64}) +@testset "NamedDimsArraysTensorAlgebraExt (eltype=$(elt))" for elt in elts + @testset "contract" begin + i = named(2, "i") + j = named(2, "j") + k = named(2, "k") + na1 = randn(elt, i, j) + na2 = randn(elt, j, k) + na_dest = TensorAlgebra.contract(na1, na2) + @test eltype(na_dest) === elt + @test unname(na_dest, (i, k)) ≈ unname(na1) * unname(na2) + end + @testset "fusedims" begin + i, j, k, l = named.((2, 3, 4, 5), ("i", "j", "k", "l")) + na = randn(elt, i, j, k, l) + na_fused = fusedims(na, (k, i) => "a", (j, l) => "b") + # Fuse all dimensions. + @test unname(na_fused, ("a", "b")) ≈ + reshape(unname(na, (k, i, j, l)), (unname(k) * unname(i), unname(j) * unname(l))) + na_fused = fusedims(na, (k, i) => "a") + # Fuse a subset of dimensions. + @test unname(na_fused, ("a", "j", "l")) ≈ + reshape(unname(na, (k, i, j, l)), (unname(k) * unname(i), unname(j), unname(l))) + end + @testset "splitdims" begin + a, b = named.((6, 20), ("a", "b")) + i, j, k, l = named.((2, 3, 4, 5), ("i", "j", "k", "l")) + na = randn(elt, a, b) + # Split all dimensions. + na_split = splitdims(na, "a" => (k, i), "b" => (j, l)) + @test unname(na_split, ("k", "i", "j", "l")) ≈ + reshape(unname(na, ("a", "b")), (unname(k), unname(i), unname(j), unname(l))) + # Split a subset of dimensions. + na_split = splitdims(na, "a" => (j, i)) + @test unname(na_split, ("j", "i", "b")) ≈ + reshape(unname(na, ("a", "b")), (unname(j), unname(i), unname(b))) + end + @testset "qr" begin + dims = (2, 2, 2, 2) + i, j, k, l = named.(dims, ("i", "j", "k", "l")) -@testset "NamedDimsArrays $(@__FILE__)" begin - include("../ext/NamedDimsArraysTensorAlgebraExt/test/runtests.jl") + na = randn(elt, i, j) + # TODO: Should this be allowed? + # TODO: Add support for specifying new name. + q, r = qr(na) + @test q * r ≈ na + + na = randn(elt, i, j, k, l) + # TODO: Add support for specifying new name. + q, r = qr(na, (i, k), (j, l)) + @test contract(q, r) ≈ na + end +end end diff --git a/test/test_basic.jl b/test/test_basic.jl index 480e9a2..57b16c9 100644 --- a/test/test_basic.jl +++ b/test/test_basic.jl @@ -1,6 +1,6 @@ @eval module $(gensym()) using Test: @test, @testset -using NDTensors.NamedDimsArrays: +using NamedDimsArrays: NamedDimsArrays, NamedDimsArray, align, diff --git a/test/test_tensoralgebra.jl b/test/test_tensoralgebra.jl index 464f855..6e5810d 100644 --- a/test/test_tensoralgebra.jl +++ b/test/test_tensoralgebra.jl @@ -1,6 +1,6 @@ @eval module $(gensym()) using Test: @test, @testset -using NDTensors.NamedDimsArrays: dimnames, named, unname, isnamed +using NamedDimsArrays: dimnames, named, unname, isnamed @testset "NamedDimsArrays $(@__FILE__) (eltype=$elt)" for elt in ( Float32, ComplexF32, Float64, ComplexF64 )