Skip to content

Update for TypeParameterAccessors.jl v0.4 #138

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

Merged
merged 6 commits into from
Jun 12, 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
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <support@itensor.org> and contributors"]
version = "0.7.5"
version = "0.7.6"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down Expand Up @@ -31,7 +31,7 @@ Adapt = "4.1.1"
Aqua = "0.8.9"
ArrayLayouts = "1.10.4"
BlockArrays = "1.2.0"
DerivableInterfaces = "0.5"
DerivableInterfaces = "0.5.2"
DiagonalArrays = "0.3"
Dictionaries = "0.4.3"
FillArrays = "1.13.0"
Expand All @@ -44,7 +44,7 @@ SparseArraysBase = "0.5"
SplitApplyCombine = "1.2.3"
TensorAlgebra = "0.3.2"
Test = "1.10"
TypeParameterAccessors = "0.2.0, 0.3"
TypeParameterAccessors = "0.4"
julia = "1.10"

[extras]
Expand Down
6 changes: 6 additions & 0 deletions src/BlockArraysExtensions/blockedunitrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ axis(a::AbstractVector) = axes(a, 1)
function eachblockaxis(a::AbstractVector)
return map(axis, blocks(a))
end
function blockaxistype(a::AbstractVector)
return eltype(eachblockaxis(a))
end

# Take a collection of axes and mortar them
# into a single blocked axis.
function mortar_axis(axs)
return blockrange(axs)
end
function mortar_axis(axs::Vector{<:Base.OneTo{<:Integer}})
return blockedrange(length.(axs))
end

Expand Down
10 changes: 5 additions & 5 deletions src/abstractblocksparsearray/abstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.getindex(a::AbstractBlockSparseArray{<:Any,N}, I::Vararg{Int,N}) where {N}
return @interface BlockSparseArrayInterface() getindex(a, I...)
return @interface interface(a) getindex(a, I...)
end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.getindex(a::AbstractBlockSparseArray{<:Any,0})
return @interface BlockSparseArrayInterface() getindex(a)
return @interface interface(a) getindex(a)
end

## # Fix ambiguity error with `BlockArrays`.
Expand All @@ -39,21 +39,21 @@ end
##
## # Fix ambiguity error with `BlockArrays`.
## function Base.getindex(a::AbstractBlockSparseArray, I::Vararg{AbstractVector})
## ## return @interface BlockSparseArrayInterface() getindex(a, I...)
## ## return @interface interface(a) getindex(a, I...)
## return ArrayLayouts.layout_getindex(a, I...)
## end

# Specialized in order to fix ambiguity error with `BlockArrays`.
function Base.setindex!(
a::AbstractBlockSparseArray{<:Any,N}, value, I::Vararg{Int,N}
) where {N}
@interface BlockSparseArrayInterface() setindex!(a, value, I...)
@interface interface(a) setindex!(a, value, I...)
return a
end

# Fix ambiguity error.
function Base.setindex!(a::AbstractBlockSparseArray{<:Any,0}, value)
@interface BlockSparseArrayInterface() setindex!(a, value)
@interface interface(a) setindex!(a, value)
return a
end

Expand Down
5 changes: 2 additions & 3 deletions src/abstractblocksparsearray/arraylayouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ function Base.similar(
elt::Type,
axes,
) where {A,B}
# TODO: Check that this equals `similartype(blocktype(B), elt, axes)`,
# or maybe promote them?
output_blocktype = similartype(blocktype(A), elt, axes)
# TODO: Use something like `Base.promote_op(*, A, B)` to determine the output block type.
output_blocktype = similartype(blocktype(A), Type{elt}, Tuple{blockaxistype.(axes)...})
return similar(BlockSparseArray{elt,length(axes),output_blocktype}, axes)
end

Expand Down
4 changes: 0 additions & 4 deletions src/abstractblocksparsearray/unblockedsubarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ function Broadcast.BroadcastStyle(arraytype::Type{<:UnblockedSubArray})
return BroadcastStyle(blocktype(parenttype(arraytype)))
end

function TypeParameterAccessors.similartype(arraytype::Type{<:UnblockedSubArray}, elt::Type)
return similartype(blocktype(parenttype(arraytype)), elt)
end

function Base.similar(
a::UnblockedSubArray, elt::Type, axes::Tuple{Base.OneTo,Vararg{Base.OneTo}}
)
Expand Down
10 changes: 5 additions & 5 deletions src/abstractblocksparsearray/wrappedabstractblocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ const AnyAbstractBlockSparseVecOrMat{T,N} = Union{
AnyAbstractBlockSparseVector{T},AnyAbstractBlockSparseMatrix{T}
}

function DerivableInterfaces.interface(::Type{<:AnyAbstractBlockSparseArray})
return BlockSparseArrayInterface()
function DerivableInterfaces.interface(arrayt::Type{<:AnyAbstractBlockSparseArray})
return BlockSparseArrayInterface(interface(blocktype(arrayt)))
end

# a[1:2, 1:2]
Expand Down Expand Up @@ -231,9 +231,9 @@ function Base.similar(
end

function blocksparse_similar(a, elt::Type, axes::Tuple)
return BlockSparseArray{elt,length(axes),similartype(blocktype(a), elt, axes)}(
undef, axes
)
ndims = length(axes)
blockt = similartype(blocktype(a), Type{elt}, Tuple{blockaxistype.(axes)...})
return BlockSparseArray{elt,ndims,blockt}(undef, axes)
end
@interface ::AbstractBlockSparseArrayInterface function Base.similar(
a::AbstractArray, elt::Type, axes::Tuple{Vararg{Int}}
Expand Down
45 changes: 40 additions & 5 deletions src/blocksparsearrayinterface/blocksparsearrayinterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ using BlockArrays:
blocklength,
blocks,
findblockindex
using DerivableInterfaces: DerivableInterfaces, @interface, DefaultArrayInterface, zero!
using DerivableInterfaces:
DerivableInterfaces,
@interface,
AbstractArrayInterface,
DefaultArrayInterface,
interface,
zero!
using LinearAlgebra: Adjoint, Transpose
using SparseArraysBase:
AbstractSparseArrayInterface,
Expand Down Expand Up @@ -101,18 +107,47 @@ blockstype(a::BlockArray) = blockstype(typeof(a))
blocktype(arraytype::Type{<:BlockArray}) = eltype(blockstype(arraytype))
blocktype(a::BlockArray) = eltype(blocks(a))

abstract type AbstractBlockSparseArrayInterface{N} <: AbstractSparseArrayInterface{N} end
abstract type AbstractBlockSparseArrayInterface{N,B<:AbstractArrayInterface{N}} <:
AbstractSparseArrayInterface{N} end

function blockinterface(interface::AbstractBlockSparseArrayInterface{<:Any,B}) where {B}
return B()
end

# TODO: Also support specifying the `blocktype` along with the `eltype`.
function Base.similar(::AbstractBlockSparseArrayInterface, T::Type, ax::Tuple)
return similar(BlockSparseArray{T}, ax)
function Base.similar(interface::AbstractBlockSparseArrayInterface, T::Type, ax::Tuple)
# TODO: Generalize by storing the block interface in the block sparse array interface.
N = length(ax)
B = similartype(typeof(blockinterface(interface)), Type{T}, Tuple{blockaxistype.(ax)...})
return similar(BlockSparseArray{T,N,B}, ax)
end

struct BlockSparseArrayInterface{N} <: AbstractBlockSparseArrayInterface{N} end
struct BlockSparseArrayInterface{N,B<:AbstractArrayInterface{N}} <:
AbstractBlockSparseArrayInterface{N,B}
blockinterface::B
end
function BlockSparseArrayInterface{N}(blockinterface::AbstractArrayInterface{N}) where {N}
return BlockSparseArrayInterface{N,typeof(blockinterface)}(blockinterface)
end
function BlockSparseArrayInterface{M,B}(::Val{N}) where {M,B<:AbstractArrayInterface{M},N}
B′ = B(Val(N))
return BlockSparseArrayInterface(B′)
end
function BlockSparseArrayInterface{N}() where {N}
return BlockSparseArrayInterface{N}(DefaultArrayInterface{N}())
end
BlockSparseArrayInterface(::Val{N}) where {N} = BlockSparseArrayInterface{N}()
BlockSparseArrayInterface{M}(::Val{N}) where {M,N} = BlockSparseArrayInterface{N}()
BlockSparseArrayInterface() = BlockSparseArrayInterface{Any}()

function DerivableInterfaces.combine_interface_rule(
interface1::AbstractBlockSparseArrayInterface,
interface2::AbstractBlockSparseArrayInterface,
)
B = interface(blockinterface(interface1), blockinterface(interface2))
return BlockSparseArrayInterface(B)
end

@interface ::AbstractBlockSparseArrayInterface function BlockArrays.blocks(a::AbstractArray)
return error("Not implemented")
end
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ Suppressor = "0.2"
TensorAlgebra = "0.3.2"
Test = "1"
TestExtras = "0.3"
TypeParameterAccessors = "0.3"
TypeParameterAccessors = "0.4"
Loading