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

Improve logic for determining AbstractNamedDimsArray type from dimension names #11

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NamedDimsArrays"
uuid = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.3.0"
version = "0.3.1"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
31 changes: 20 additions & 11 deletions src/abstractnameddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,34 @@

Base.copy(a::AbstractNamedDimsArray) = nameddims(copy(dename(a)), nameddimsindices(a))

const NamedDimsIndices = Union{
AbstractNamedUnitRange{<:Integer},AbstractNamedArray{<:Integer}
}
const NamedDimsAxis = AbstractNamedUnitRange{
<:Integer,<:AbstractUnitRange,<:NamedDimsIndices
}

# Generic constructor.
function nameddims(a::AbstractArray, nameddimsindices)
# TODO: Check the shape of `nameddimsindices` matches the shape of `a`.
return nameddimstype(eltype(nameddimsindices))(
a, to_nameddimsindices(a, nameddimsindices)
)
arrtype = mapreduce(nameddimsarraytype, combine_nameddimsarraytype, nameddimsindices)
return arrtype(a, to_nameddimsindices(a, nameddimsindices))
end

# Can overload this to get custom named dims array wrapper
# depending on the dimension name types, for example
# output an `ITensor` if the dimension names are `IndexName`s.
nameddimstype(dimnametype::Type) = NamedDimsArray
nameddimsarraytype(nameddim) = nameddimsarraytype(typeof(nameddim))
nameddimsarraytype(nameddimtype::Type) = NamedDimsArray
function nameddimsarraytype(nameddimtype::Type{<:NamedDimsIndices})
return nameddimsarraytype(nametype(nameddimtype))
end
function combine_nameddimsarraytype(

Check warning on line 165 in src/abstractnameddimsarray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractnameddimsarray.jl#L165

Added line #L165 was not covered by tests
::Type{<:AbstractNamedDimsArray}, ::Type{<:AbstractNamedDimsArray}
)
return NamedDimsArray

Check warning on line 168 in src/abstractnameddimsarray.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractnameddimsarray.jl#L168

Added line #L168 was not covered by tests
end
combine_nameddimsarraytype(::Type{T}, ::Type{T}) where {T<:AbstractNamedDimsArray} = T

Base.axes(a::AbstractNamedDimsArray) = map(named, axes(dename(a)), nameddimsindices(a))
Base.size(a::AbstractNamedDimsArray) = map(named, size(dename(a)), nameddimsindices(a))
Expand All @@ -175,13 +191,6 @@
Base.axes(a::AbstractNamedDimsArray, dimname::Name) = axes(a, dim(a, dimname))
Base.size(a::AbstractNamedDimsArray, dimname::Name) = size(a, dim(a, dimname))

const NamedDimsIndices = Union{
AbstractNamedUnitRange{<:Integer},AbstractNamedArray{<:Integer}
}
const NamedDimsAxis = AbstractNamedUnitRange{
<:Integer,<:AbstractUnitRange,<:NamedDimsIndices
}

to_nameddimsaxes(dims) = map(to_nameddimsaxis, dims)
to_nameddimsaxis(ax::NamedDimsAxis) = ax
to_nameddimsaxis(I::NamedDimsIndices) = named(dename(only(axes(I))), I)
Expand Down
Loading