From 2ed2e423aaa067aa8b7d244db093219d45dcb1ec Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Mon, 17 Oct 2022 07:53:47 +0200 Subject: [PATCH 1/2] Handle multisize undefs --- Project.toml | 2 +- src/LabelledArrays.jl | 3 ++- src/larray.jl | 2 +- test/larrays.jl | 4 ++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 2aa3605..95ef146 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LabelledArrays" uuid = "2ee39098-c373-598a-b85f-a56591580800" authors = ["Chris Rackauckas "] -version = "1.12.1" +version = "1.12.2" [deps] ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2" diff --git a/src/LabelledArrays.jl b/src/LabelledArrays.jl index e1cc525..6a49591 100644 --- a/src/LabelledArrays.jl +++ b/src/LabelledArrays.jl @@ -65,7 +65,8 @@ end ArrayInterfaceCore.can_setindex(::Type{<:SLArray}) = false function ArrayInterfaceCore.undefmatrix(x::LArray{T, N, D, Syms}) where {T, N, D, Syms} - similar(x.__x, length(Syms), length(Syms)) + n = sum(length, Syms) + similar(x.__x, n, n) end function PreallocationTools.get_tmp(dc::PreallocationTools.DiffCache, diff --git a/src/larray.jl b/src/larray.jl index cc64d8a..407b495 100644 --- a/src/larray.jl +++ b/src/larray.jl @@ -5,7 +5,7 @@ struct LArray{T, N, D <: AbstractArray{T, N}, Syms} <: DenseArray{T, N} end function LArray{T, N, D, Syms}(::UndefInitializer, n::Int64) where {T, N, D, Syms} - @assert length(Syms) == n + @assert sum(length, Syms) == n LArray{T, N, D, Syms}(similar(D, n)) end diff --git a/test/larrays.jl b/test/larrays.jl index a83ef15..01dd67b 100644 --- a/test/larrays.jl +++ b/test/larrays.jl @@ -16,6 +16,10 @@ using LabelledArrays, Test, InteractiveUtils syms = (:a, :b, :c) @test typeof(typeof(x)(undef, 3)) == typeof(x) + x = LabelledArrays.LArray{Float64, 1, Vector{Float64}, (x = 1:4, y = 5:8)}(undef, 8) + @test length(x) == 8 + @test size(LabelledArrays.ArrayInterfaceCore.undefmatrix(x)) == (8, 8) + for (i, s) in enumerate(syms) @show i, s @test x[i] == x[s] From fed81dca1ace4d6c018197307171ba43ce86c9c5 Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Mon, 17 Oct 2022 08:14:56 +0200 Subject: [PATCH 2/2] fix symbol handling --- src/LabelledArrays.jl | 4 +++- src/larray.jl | 2 +- test/larrays.jl | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/LabelledArrays.jl b/src/LabelledArrays.jl index 6a49591..0780230 100644 --- a/src/LabelledArrays.jl +++ b/src/LabelledArrays.jl @@ -64,8 +64,10 @@ function ArrayInterfaceCore.ismutable(::Type{<:LArray{T, N, D, Syms}}) where {T, end ArrayInterfaceCore.can_setindex(::Type{<:SLArray}) = false +lenfun(x) = length(x) +lenfun(::Symbol) = 1 function ArrayInterfaceCore.undefmatrix(x::LArray{T, N, D, Syms}) where {T, N, D, Syms} - n = sum(length, Syms) + n = sum(lenfun, Syms) similar(x.__x, n, n) end diff --git a/src/larray.jl b/src/larray.jl index 407b495..ec811db 100644 --- a/src/larray.jl +++ b/src/larray.jl @@ -5,7 +5,7 @@ struct LArray{T, N, D <: AbstractArray{T, N}, Syms} <: DenseArray{T, N} end function LArray{T, N, D, Syms}(::UndefInitializer, n::Int64) where {T, N, D, Syms} - @assert sum(length, Syms) == n + @assert sum(lenfun, Syms) == n LArray{T, N, D, Syms}(similar(D, n)) end diff --git a/test/larrays.jl b/test/larrays.jl index 01dd67b..918d47a 100644 --- a/test/larrays.jl +++ b/test/larrays.jl @@ -16,9 +16,9 @@ using LabelledArrays, Test, InteractiveUtils syms = (:a, :b, :c) @test typeof(typeof(x)(undef, 3)) == typeof(x) - x = LabelledArrays.LArray{Float64, 1, Vector{Float64}, (x = 1:4, y = 5:8)}(undef, 8) - @test length(x) == 8 - @test size(LabelledArrays.ArrayInterfaceCore.undefmatrix(x)) == (8, 8) + z = LabelledArrays.LArray{Float64, 1, Vector{Float64}, (x = 1:4, y = 5:8)}(undef, 8) + @test length(z) == 8 + @test size(LabelledArrays.ArrayInterfaceCore.undefmatrix(z)) == (8, 8) for (i, s) in enumerate(syms) @show i, s