-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
928 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
name = "NamedDimsArrays" | ||
uuid = "60cbd0c0-df58-4cb7-918c-6f5607b73fde" | ||
authors = ["ITensor developers <[email protected]> and contributors"] | ||
version = "0.2.0" | ||
version = "0.3.0" | ||
|
||
[deps] | ||
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" | ||
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a" | ||
BroadcastMapConversion = "4a4adec5-520f-4750-bb37-d5e66b4ddeb2" | ||
Derive = "a07dfc7f-7d04-4eb5-84cc-a97f051f655a" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
|
@@ -13,8 +14,16 @@ SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" | |
TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a" | ||
TypeParameterAccessors = "7e5a90cf-f82e-492e-a09b-e3e26432c138" | ||
|
||
[weakdeps] | ||
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e" | ||
|
||
[extensions] | ||
NamedDimsArraysBlockArraysExt = "BlockArrays" | ||
|
||
[compat] | ||
Adapt = "4.1.1" | ||
ArrayLayouts = "1.11.0" | ||
BlockArrays = "1.3.0" | ||
BroadcastMapConversion = "0.1.2" | ||
Derive = "0.3.6" | ||
LinearAlgebra = "1.10" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
ext/NamedDimsArraysBlockArraysExt/NamedDimsArraysBlockArraysExt.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module NamedDimsArraysBlockArraysExt | ||
using ArrayLayouts: ArrayLayouts | ||
using BlockArrays: Block, BlockRange | ||
using NamedDimsArrays: | ||
AbstractNamedDimsArray, | ||
AbstractNamedUnitRange, | ||
named_getindex, | ||
nameddims_getindex, | ||
nameddims_view | ||
|
||
function Base.getindex(r::AbstractNamedUnitRange{<:Integer}, I::Block{1}) | ||
# TODO: Use `Derive.@interface NamedArrayInterface() r[I]` instead. | ||
return named_getindex(r, I) | ||
end | ||
|
||
function Base.getindex(r::AbstractNamedUnitRange{<:Integer}, I::BlockRange{1}) | ||
# TODO: Use `Derive.@interface NamedArrayInterface() r[I]` instead. | ||
return named_getindex(r, I) | ||
end | ||
|
||
const BlockIndex{N} = Union{Block{N},BlockRange{N},AbstractVector{<:Block{N}}} | ||
|
||
function Base.view(a::AbstractNamedDimsArray, I1::Block{1}, Irest::BlockIndex{1}...) | ||
# TODO: Use `Derive.@interface NamedDimsArrayInterface() r[I]` instead. | ||
return nameddims_view(a, I1, Irest...) | ||
end | ||
|
||
function Base.view(a::AbstractNamedDimsArray, I::Block) | ||
# TODO: Use `Derive.@interface NamedDimsArrayInterface() r[I]` instead. | ||
return nameddims_view(a, Tuple(I)...) | ||
end | ||
|
||
function Base.view(a::AbstractNamedDimsArray, I1::BlockIndex{1}, Irest::BlockIndex{1}...) | ||
# TODO: Use `Derive.@interface NamedDimsArrayInterface() r[I]` instead. | ||
return nameddims_view(a, I1, Irest...) | ||
end | ||
|
||
# Fix ambiguity error. | ||
function Base.getindex( | ||
a::AbstractNamedDimsArray, I1::BlockRange{1}, Irest::BlockRange{1}... | ||
) | ||
return ArrayLayouts.layout_getindex(a, I1, Irest...) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using TypeParameterAccessors: unspecify_type_parameters | ||
|
||
abstract type AbstractNamedArray{T,N,Value<:AbstractArray,Name} <: AbstractArray{T,N} end | ||
|
||
const AbstractNamedVector{T,Value<:AbstractVector,Name} = AbstractNamedArray{T,1,Value,Name} | ||
const AbstractNamedMatrix{T,Value<:AbstractVector,Name} = AbstractNamedArray{T,2,Value,Name} | ||
|
||
# Minimal interface. | ||
dename(a::AbstractNamedArray) = throw(MethodError(dename, Tuple{typeof(a)})) | ||
name(a::AbstractNamedArray) = throw(MethodError(name, Tuple{typeof(a)})) | ||
|
||
# This can be customized to output different named integer types, | ||
# such as `namedarray(a::AbstractArray, name::IndexName) = Index(a, name)`. | ||
namedarray(a::AbstractArray, name) = NamedArray(a, name) | ||
|
||
# Shorthand. | ||
named(a::AbstractArray, name) = namedarray(a, name) | ||
|
||
# Derived interface. | ||
# TODO: Use `Accessors.@set`? | ||
setname(a::AbstractNamedArray, name) = namedarray(dename(a), name) | ||
|
||
# TODO: Use `TypeParameterAccessors`. | ||
denametype(::Type{<:AbstractNamedArray{<:Any,<:Any,Value}}) where {Value} = Value | ||
nametype(::Type{<:AbstractNamedArray{<:Any,<:Any,<:Any,Name}}) where {Name} = Name | ||
|
||
# Traits. | ||
isnamed(::Type{<:AbstractNamedArray}) = true | ||
|
||
# TODO: Should they also have the same base type? | ||
function Base.:(==)(a1::AbstractNamedArray, a2::AbstractNamedArray) | ||
return name(a1) == name(a2) && dename(a1) == dename(a2) | ||
end | ||
function Base.hash(a::AbstractNamedArray, h::UInt) | ||
h = hash(Symbol(unspecify_type_parameters(typeof(a))), h) | ||
# TODO: Double check how this is handling blocking/sector information. | ||
h = hash(dename(a), h) | ||
return hash(name(a), h) | ||
end | ||
|
||
named_getindex(a::AbstractArray, I...) = named(getindex(dename(a), I...), name(a)) | ||
|
||
# Array funcionality. | ||
Base.size(a::AbstractNamedArray) = map(s -> named(s, name(a)), size(dename(a))) | ||
Base.axes(a::AbstractNamedArray) = map(s -> named(s, name(a)), axes(dename(a))) | ||
Base.eachindex(a::AbstractNamedArray) = eachindex(dename(a)) | ||
function Base.getindex(a::AbstractNamedArray{<:Any,N}, I::Vararg{Int,N}) where {N} | ||
return named_getindex(a, I...) | ||
end | ||
function Base.getindex(a::AbstractNamedArray, I::Int) | ||
return named_getindex(a, I) | ||
end | ||
Base.isempty(a::AbstractNamedArray) = isempty(dename(a)) | ||
|
||
## function Base.AbstractArray{Int}(a::AbstractNamedArray) | ||
## return AbstractArray{Int}(dename(a)) | ||
## end | ||
## | ||
## Base.iterate(a::AbstractNamedArray) = isempty(a) ? nothing : (first(a), first(a)) | ||
## function Base.iterate(a::AbstractNamedArray, i) | ||
## i == last(a) && return nothing | ||
## next = named(dename(i) + dename(step(a)), name(a)) | ||
## return (next, next) | ||
## end | ||
|
||
function randname(ang::AbstractRNG, a::AbstractNamedArray) | ||
return named(dename(a), randname(name(a))) | ||
end | ||
|
||
function Base.show(io::IO, a::AbstractNamedArray) | ||
print(io, "named(", dename(a), ", ", repr(name(a)), ")") | ||
return nothing | ||
end | ||
function Base.show(io::IO, mime::MIME"text/plain", a::AbstractNamedArray) | ||
print(io, "named(\n") | ||
show(io, mime, dename(a)) | ||
print(io, ",\n ", repr(name(a)), ")") | ||
return nothing | ||
end |
Oops, something went wrong.