From 072502de745d26e10be1cebc4daf879f01af683d Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Jun 2025 17:24:17 -0400 Subject: [PATCH 1/3] More generalizations for generic block types --- Project.toml | 2 +- src/blocksparsearray/blocksparsearray.jl | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index ead2a2e5..c3f798c8 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "BlockSparseArrays" uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4" authors = ["ITensor developers and contributors"] -version = "0.7.9" +version = "0.7.10" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/blocksparsearray/blocksparsearray.jl b/src/blocksparsearray/blocksparsearray.jl index be3f0a6d..6517574c 100644 --- a/src/blocksparsearray/blocksparsearray.jl +++ b/src/blocksparsearray/blocksparsearray.jl @@ -6,7 +6,7 @@ using BlockArrays: blockedrange, blocklength, undef_blocks -using DerivableInterfaces: @interface +using DerivableInterfaces: @interface, similartype using Dictionaries: Dictionary using SparseArraysBase: SparseArrayDOK @@ -173,7 +173,9 @@ end function BlockSparseArray{T,N}( ::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}} ) where {T,N} - return BlockSparseArray{T,N,Array{T,N}}(undef, axes) + axt = Tuple{blockaxistype.(axes)...} + A = similartype(Array{T}, axt) + return BlockSparseArray{T,N,A}(undef, axes) end function BlockSparseArray{T,N}( From 3079e691e9e9cc0814c55027bd6628e9c52a8716 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Jun 2025 17:31:47 -0400 Subject: [PATCH 2/3] More generalizations --- src/blocksparsearray/blocksparsearray.jl | 3 ++- src/blocksparsearrayinterface/arraylayouts.jl | 11 +++++++---- src/factorizations/svd.jl | 7 +++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/blocksparsearray/blocksparsearray.jl b/src/blocksparsearray/blocksparsearray.jl index 6517574c..abc3a992 100644 --- a/src/blocksparsearray/blocksparsearray.jl +++ b/src/blocksparsearray/blocksparsearray.jl @@ -6,9 +6,10 @@ using BlockArrays: blockedrange, blocklength, undef_blocks -using DerivableInterfaces: @interface, similartype +using DerivableInterfaces: @interface using Dictionaries: Dictionary using SparseArraysBase: SparseArrayDOK +using TypeParameterAccessors: similartype """ SparseArrayDOK{T}(undef_blocks, axes) diff --git a/src/blocksparsearrayinterface/arraylayouts.jl b/src/blocksparsearrayinterface/arraylayouts.jl index f1e70c91..14d3d563 100644 --- a/src/blocksparsearrayinterface/arraylayouts.jl +++ b/src/blocksparsearrayinterface/arraylayouts.jl @@ -11,6 +11,10 @@ using LinearAlgebra: LinearAlgebra, dot, mul! return a_dest end +function DerivableInterfaces.interface(m::MulAdd) + return interface(m.A, m.B, m.C) +end + function ArrayLayouts.materialize!( m::MatMulMatAdd{ <:BlockLayout{<:SparseLayout}, @@ -18,8 +22,7 @@ function ArrayLayouts.materialize!( <:BlockLayout{<:SparseLayout}, }, ) - α, a1, a2, β, a_dest = m.α, m.A, m.B, m.β, m.C - @interface BlockSparseArrayInterface() muladd!(m.α, m.A, m.B, m.β, m.C) + @interface interface(m) muladd!(m.α, m.A, m.B, m.β, m.C) return m.C end function ArrayLayouts.materialize!( @@ -29,7 +32,7 @@ function ArrayLayouts.materialize!( <:BlockLayout{<:SparseLayout}, }, ) - @interface BlockSparseArrayInterface() matmul!(m) + @interface interface(m) matmul!(m) return m.C end @@ -42,5 +45,5 @@ end end function Base.copy(d::Dot{<:BlockLayout{<:SparseLayout},<:BlockLayout{<:SparseLayout}}) - return @interface BlockSparseArrayInterface() dot(d.A, d.B) + return @interface interface(d.A, d.B) dot(d.A, d.B) end diff --git a/src/factorizations/svd.jl b/src/factorizations/svd.jl index 1f8f4a42..be7ca75c 100644 --- a/src/factorizations/svd.jl +++ b/src/factorizations/svd.jl @@ -1,5 +1,7 @@ +using DiagonalArrays: diagonaltype using MatrixAlgebraKit: MatrixAlgebraKit, check_input, default_svd_algorithm, svd_compact!, svd_full! +using TypeParameterAccessors: realtype """ BlockPermutedDiagonalAlgorithm(A::MatrixAlgebraKit.AbstractAlgorithm) @@ -24,10 +26,7 @@ function similar_output( ::typeof(svd_compact!), A, S_axes, alg::MatrixAlgebraKit.AbstractAlgorithm ) U = similar(A, axes(A, 1), S_axes[1]) - T = real(eltype(A)) - # TODO: this should be replaced with a more general similar function that can handle setting - # the blocktype and element type - something like S = similar(A, BlockType(...)) - S = BlockSparseMatrix{T,Diagonal{T,Vector{T}}}(undef, S_axes) + S = similar(A, BlockType(diagonaltype(realtype(blocktype(A)))), S_axes) Vt = similar(A, S_axes[2], axes(A, 2)) return U, S, Vt end From 6c1b5e0367d3465d2d570820dce4d83ca267525c Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Jun 2025 17:33:14 -0400 Subject: [PATCH 3/3] More specific TypeParameterAccessors version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c3f798c8..f76da0df 100644 --- a/Project.toml +++ b/Project.toml @@ -44,7 +44,7 @@ SparseArraysBase = "0.5" SplitApplyCombine = "1.2.3" TensorAlgebra = "0.3.2" Test = "1.10" -TypeParameterAccessors = "0.4" +TypeParameterAccessors = "0.4.1" julia = "1.10" [extras]