From e71eaaeeb15134fff8abf99c817190d778ba116d Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Mon, 30 Oct 2023 17:54:18 -0400 Subject: [PATCH 01/12] specify method for vcat to avoid ambiguity --- src/array_interface.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/array_interface.jl b/src/array_interface.jl index 745dde5a..e82c6e29 100644 --- a/src/array_interface.jl +++ b/src/array_interface.jl @@ -33,8 +33,8 @@ second_axis(::ComponentVector) = FlatAxis() # Are all these methods necessary? # TODO: See what we can reduce down to without getting ambiguity errors -Base.vcat(x::ComponentVector, y::AbstractVector) = vcat(getdata(x), y) -Base.vcat(x::AbstractVector, y::ComponentVector) = vcat(x, getdata(y)) +Base.vcat(x::ComponentVector{<:Number}, y::AbstractVector{<:Number}) = vcat(getdata(x), y) +Base.vcat(x::AbstractVector{<:Number}, y::ComponentVector{<:Number}) = vcat(x, getdata(y)) function Base.vcat(x::ComponentVector, y::ComponentVector) if reduce((accum, key) -> accum || (key in keys(x)), keys(y); init=false) return vcat(getdata(x), getdata(y)) @@ -147,4 +147,4 @@ end Base.stride(x::ComponentArray, k) = stride(getdata(x), k) Base.stride(x::ComponentArray, k::Int64) = stride(getdata(x), k) -ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A \ No newline at end of file +ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A From 88ed61449f9cd06f8cee95a2915a696e964a2775 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Tue, 31 Oct 2023 08:53:36 -0400 Subject: [PATCH 02/12] Revert "specify method for vcat to avoid ambiguity" This reverts commit e71eaaeeb15134fff8abf99c817190d778ba116d. --- src/array_interface.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/array_interface.jl b/src/array_interface.jl index e82c6e29..745dde5a 100644 --- a/src/array_interface.jl +++ b/src/array_interface.jl @@ -33,8 +33,8 @@ second_axis(::ComponentVector) = FlatAxis() # Are all these methods necessary? # TODO: See what we can reduce down to without getting ambiguity errors -Base.vcat(x::ComponentVector{<:Number}, y::AbstractVector{<:Number}) = vcat(getdata(x), y) -Base.vcat(x::AbstractVector{<:Number}, y::ComponentVector{<:Number}) = vcat(x, getdata(y)) +Base.vcat(x::ComponentVector, y::AbstractVector) = vcat(getdata(x), y) +Base.vcat(x::AbstractVector, y::ComponentVector) = vcat(x, getdata(y)) function Base.vcat(x::ComponentVector, y::ComponentVector) if reduce((accum, key) -> accum || (key in keys(x)), keys(y); init=false) return vcat(getdata(x), getdata(y)) @@ -147,4 +147,4 @@ end Base.stride(x::ComponentArray, k) = stride(getdata(x), k) Base.stride(x::ComponentArray, k::Int64) = stride(getdata(x), k) -ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A +ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A \ No newline at end of file From cd4b136310d3754b006edfce4ecb6b7c5b4cde57 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Tue, 31 Oct 2023 09:12:28 -0400 Subject: [PATCH 03/12] test on jl v1.10 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 28af4bc4..d72da20d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: - '1.6' - '1.8' - '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia. + - '1.10' os: - ubuntu-latest arch: From 8f5e9e62ceeabc8da6cebf00fa8e8e3d34c2ff68 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Tue, 31 Oct 2023 09:14:45 -0400 Subject: [PATCH 04/12] version num --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d72da20d..49265a7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - '1.6' - '1.8' - '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia. - - '1.10' + - '1.10.0-beta3' os: - ubuntu-latest arch: From 3f4e29f5a22f780ac568d954a34c059a242be35a Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Tue, 31 Oct 2023 09:28:08 -0400 Subject: [PATCH 05/12] resolve https://github.com/jonniedie/ComponentArrays.jl/actions/runs/6707047919/job/18224714243?pr=231#step:6:390 --- src/array_interface.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/array_interface.jl b/src/array_interface.jl index 745dde5a..0c8645da 100644 --- a/src/array_interface.jl +++ b/src/array_interface.jl @@ -84,7 +84,7 @@ Base.IndexStyle(::Type{<:ComponentArray{T,N,<:A,<:Axes}}) where {T,N,A,Axes} = I # Since we aren't really using the standard approach to indexing, this will forward things to # the correct methods -Base.to_indices(x::ComponentArray, i::Tuple) = i +Base.to_indices(x::ComponentArray, i::Tuple{Any}) = i Base.to_indices(x::ComponentArray, i::NTuple{N,Union{Integer, CartesianIndex}}) where N = i Base.to_indices(x::ComponentArray, i::NTuple{N,Int64}) where N = i Base.to_index(x::ComponentArray, i) = i @@ -147,4 +147,4 @@ end Base.stride(x::ComponentArray, k) = stride(getdata(x), k) Base.stride(x::ComponentArray, k::Int64) = stride(getdata(x), k) -ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A \ No newline at end of file +ArrayInterface.parent_type(::Type{ComponentArray{T,N,A,Axes}}) where {T,N,A,Axes} = A From 1f5546132864f83be6591cb7e4171d817612fd19 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 12:23:58 -0400 Subject: [PATCH 06/12] parameterized CA Types --- src/componentarray.jl | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/componentarray.jl b/src/componentarray.jl index 00ed6064..45192017 100644 --- a/src/componentarray.jl +++ b/src/componentarray.jl @@ -115,21 +115,21 @@ ComponentMatrix{T}(x::ComponentMatrix) where {T} = T.(x) ComponentMatrix() = ComponentMatrix(Array{Any}(undef, 0, 0), (FlatAxis(), FlatAxis())) ComponentMatrix{T}() where {T} = ComponentMatrix(Array{T}(undef, 0, 0), (FlatAxis(), FlatAxis())) -const CArray = ComponentArray -const CVector = ComponentVector -const CMatrix = ComponentMatrix +const CArray{T} = ComponentArray{T} where{T} +const CVector{T} = ComponentVector{T} where{T} +const CMatrix{T} = ComponentMatrix{T} where{T} const AdjOrTrans{T, A} = Union{Adjoint{T, A}, Transpose{T, A}} const AdjOrTransComponentArray{T, A} = Union{Adjoint{T, A}, Transpose{T, A}} where A<:ComponentArray const AdjOrTransComponentVector{T} = Union{Adjoint{T, A}, Transpose{T, A}} where A<:ComponentVector const AdjOrTransComponentMatrix{T} = Union{Adjoint{T, A}, Transpose{T, A}} where A<:ComponentMatrix -const ComponentVecOrMat = Union{ComponentVector, ComponentMatrix} -const AdjOrTransComponentVecOrMat = AdjOrTrans{T, <:ComponentVecOrMat} where T -const AbstractComponentArray = Union{ComponentArray, AdjOrTransComponentArray} -const AbstractComponentVecOrMat = Union{ComponentVecOrMat, AdjOrTransComponentVecOrMat} -const AbstractComponentVector = Union{ComponentVector, AdjOrTransComponentVector} -const AbstractComponentMatrix = Union{ComponentMatrix, AdjOrTransComponentMatrix} +const ComponentVecOrMat{T} = Union{ComponentVector{T}, ComponentMatrix{T}} where{T} +const AdjOrTransComponentVecOrMat{T} = AdjOrTrans{T, <:ComponentVecOrMat} where {T} +const AbstractComponentArray{T} = Union{ComponentArray{T}, AdjOrTransComponentArray{T}} where{T} +const AbstractComponentVecOrMat{T} = Union{ComponentVecOrMat{T}, AdjOrTransComponentVecOrMat{T}} where{T} +const AbstractComponentVector{T} = Union{ComponentVector{T}, AdjOrTransComponentVector{T}} where{T} +const AbstractComponentMatrix{T} = Union{ComponentMatrix{T}, AdjOrTransComponentMatrix{T}} where{T} ## Constructor helpers From 9f3b7684503607488885f0dca46e9b9ea079528a Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 12:25:00 -0400 Subject: [PATCH 07/12] restricted vcat operations to T<:Number to resolve method ambiguities with SparseArrays in julia v1.10 --- src/array_interface.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/array_interface.jl b/src/array_interface.jl index 0c8645da..c9060eb4 100644 --- a/src/array_interface.jl +++ b/src/array_interface.jl @@ -33,9 +33,9 @@ second_axis(::ComponentVector) = FlatAxis() # Are all these methods necessary? # TODO: See what we can reduce down to without getting ambiguity errors -Base.vcat(x::ComponentVector, y::AbstractVector) = vcat(getdata(x), y) -Base.vcat(x::AbstractVector, y::ComponentVector) = vcat(x, getdata(y)) -function Base.vcat(x::ComponentVector, y::ComponentVector) +Base.vcat(x::ComponentVector{<:Number}, y::AbstractVector{<:Number}) = vcat(getdata(x), y) +Base.vcat(x::AbstractVector{<:Number}, y::ComponentVector{<:Number}) = vcat(x, getdata(y)) +function Base.vcat(x::ComponentVector{<:Number}, y::ComponentVector{<:Number}) if reduce((accum, key) -> accum || (key in keys(x)), keys(y); init=false) return vcat(getdata(x), getdata(y)) else @@ -46,7 +46,7 @@ function Base.vcat(x::ComponentVector, y::ComponentVector) return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...))) end end -function Base.vcat(x::AbstractComponentVecOrMat, y::AbstractComponentVecOrMat) +function Base.vcat(x::AbstractComponentVecOrMat{<:Number}, y::AbstractComponentVecOrMat{<:Number}) ax_x, ax_y = getindex.(getaxes.((x, y)), 1) if reduce((accum, key) -> accum || (key in keys(ax_x)), keys(ax_y); init=false) || getaxes(x)[2:end] != getaxes(y)[2:end] return vcat(getdata(x), getdata(y)) @@ -57,10 +57,10 @@ function Base.vcat(x::AbstractComponentVecOrMat, y::AbstractComponentVecOrMat) return ComponentArray(vcat(data_x, data_y), Axis((;idxmap_x..., idxmap_y...)), getaxes(x)[2:end]...) end end -Base.vcat(x::CV...) where {CV<:AdjOrTransComponentArray} = ComponentArray(reduce(vcat, map(y->getdata(y.parent)', x)), getaxes(x[1])) -Base.vcat(x::ComponentVector, args...) = vcat(getdata(x), getdata.(args)...) -Base.vcat(x::ComponentVector, args::Vararg{Union{Number, UniformScaling, AbstractVecOrMat}}) = vcat(getdata(x), getdata.(args)...) -Base.vcat(x::ComponentVector, args::Vararg{AbstractVector{T}, N}) where {T,N} = vcat(getdata(x), getdata.(args)...) +Base.vcat(x::CV...) where {CV<:AdjOrTransComponentArray{<:Number}} = ComponentArray(reduce(vcat, map(y->getdata(y.parent)', x)), getaxes(x[1])) +Base.vcat(x::ComponentVector{<:Number}, args...) = vcat(getdata(x), getdata.(args)...) +Base.vcat(x::ComponentVector{<:Number}, args::Vararg{Union{Number, UniformScaling, AbstractVecOrMat{<:Number}}}) = vcat(getdata(x), getdata.(args)...) +Base.vcat(x::ComponentVector{<:Number}, args::Vararg{AbstractVector{T}, N}) where {T<:Number,N} = vcat(getdata(x), getdata.(args)...) function Base.hvcat(row_lengths::NTuple{N,Int}, xs::Vararg{AbstractComponentVecOrMat}) where {N} i = 1 From 98c70c662b23ede68df7f40d200c82eff9e8e22d Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 12:36:18 -0400 Subject: [PATCH 08/12] bump version --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index af94a3c9..9120e240 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ComponentArrays" uuid = "b0b7db55-cfe3-40fc-9ded-d10e2dbeff66" authors = ["Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com>"] -version = "0.15.4" +version = "0.15.5" [deps] ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9" From 16506f4f12086634dd57584120e3b3dbaf3d9566 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 12:57:06 -0400 Subject: [PATCH 09/12] Update .github/workflows/ci.yml Co-authored-by: Jonnie Diegelman <47193959+jonniedie@users.noreply.github.com> --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 49265a7b..9b5d5dff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - '1.6' - '1.8' - '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia. - - '1.10.0-beta3' + - 'beta' os: - ubuntu-latest arch: From 332be8c55ef24b594338337ebb14f9fc0b07fffa Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 12:59:29 -0400 Subject: [PATCH 10/12] remove additional type parameterization from CArray, CVector, CMatrix as type parameters are already curried --- src/componentarray.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/componentarray.jl b/src/componentarray.jl index 45192017..736b0c44 100644 --- a/src/componentarray.jl +++ b/src/componentarray.jl @@ -115,9 +115,9 @@ ComponentMatrix{T}(x::ComponentMatrix) where {T} = T.(x) ComponentMatrix() = ComponentMatrix(Array{Any}(undef, 0, 0), (FlatAxis(), FlatAxis())) ComponentMatrix{T}() where {T} = ComponentMatrix(Array{T}(undef, 0, 0), (FlatAxis(), FlatAxis())) -const CArray{T} = ComponentArray{T} where{T} -const CVector{T} = ComponentVector{T} where{T} -const CMatrix{T} = ComponentMatrix{T} where{T} +const CArray = ComponentArray +const CVector = ComponentVector +const CMatrix = ComponentMatrix const AdjOrTrans{T, A} = Union{Adjoint{T, A}, Transpose{T, A}} const AdjOrTransComponentArray{T, A} = Union{Adjoint{T, A}, Transpose{T, A}} where A<:ComponentArray From 3f0e02683bcb5714f7f9eda1a9caa5b0ec45b0b0 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 13:04:34 -0400 Subject: [PATCH 11/12] Revert "Update .github/workflows/ci.yml" This reverts commit 16506f4f12086634dd57584120e3b3dbaf3d9566. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9b5d5dff..49265a7b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - '1.6' - '1.8' - '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia. - - 'beta' + - '1.10.0-beta3' os: - ubuntu-latest arch: From 315910be67ff3112a5a4c24ecf768bbba96e1e34 Mon Sep 17 00:00:00 2001 From: Vedant Puri Date: Wed, 1 Nov 2023 14:57:57 -0400 Subject: [PATCH 12/12] lmul --- src/linear_algebra.jl | 3 +++ test/runtests.jl | 14 +++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/linear_algebra.jl b/src/linear_algebra.jl index 6c2efedb..22b87e14 100644 --- a/src/linear_algebra.jl +++ b/src/linear_algebra.jl @@ -57,3 +57,6 @@ function LinearAlgebra.axpby!(α::Number, x::ComponentArray, β::Number, y::Comp axpby!(α, getdata(x), β, getdata(y)) return ComponentArray(y, getaxes(y)) end + +lmul!(a::Number, B::ComponentArray) = ComponentArray(lmul!(a, getdata(B)), getaxes(B)) + diff --git a/test/runtests.jl b/test/runtests.jl index 4f4d4167..8d9279af 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -36,7 +36,6 @@ caa = ComponentArray(a = ca, b = sq_mat) _a, _b, _c = Val.((:a, :b, :c)) - ## Tests @testset "Allocations and Inference" begin @test @ballocated($ca.c.a.a) == 0 @@ -690,6 +689,19 @@ end @test_throws ArgumentError axpby!(2, x, 3, y) end +@testset "lmul!" begin + a = rand() + x = ComponentArray(a = rand(4), b = rand(4)) + + xdata = copy(getdata(x)) + xaxes = getaxes(x) + + y = lmul!(a, x) + @test y == x + @test getdata(x) ≈ a * xdata + @test getaxes(x) == xaxes +end + @testset "Autodiff" begin include("autodiff_tests.jl") end