Skip to content

Format code with JuliaFormatter v2.1.5 #472

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 1 commit into from
Jul 31, 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ pB = ArrayPartition(b)
pA .* pB # Now all standard array stuff works!

# or do:
x0 = rand(3,3)
v0 = rand(3,3)
a0 = rand(3,3)
x0 = rand(3, 3)
v0 = rand(3, 3)
a0 = rand(3, 3)
u0 = ArrayPartition(x0, v0, a0)
u0.x[1] == x0 # true

Expand Down
1 change: 0 additions & 1 deletion ext/RecursiveArrayToolsKernelAbstractionsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module RecursiveArrayToolsKernelAbstractionsExt
import RecursiveArrayTools: VectorOfArray
import KernelAbstractions


function KernelAbstractions.get_backend(x::VectorOfArray)
u = parent(x)
if length(u) == 0
Expand Down
22 changes: 15 additions & 7 deletions ext/RecursiveArrayToolsZygoteExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ using RecursiveArrayTools
using Zygote
using Zygote: FillArrays, ChainRulesCore, literal_getproperty, @adjoint


# Define a new species of projection operator for this type:
# ChainRulesCore.ProjectTo(x::VectorOfArray) = ChainRulesCore.ProjectTo{VectorOfArray}()

Expand Down Expand Up @@ -216,10 +215,12 @@ z̄ -> (nothing, conj.(z̄))
@adjoint Broadcast.broadcasted(::typeof(real), x::AbstractVectorOfArray) = real.(x),
z̄ -> (nothing, real.(z̄))

@adjoint Broadcast.broadcasted(::typeof(imag), x::AbstractVectorOfArray) = imag.(x),
@adjoint Broadcast.broadcasted(
::typeof(imag), x::AbstractVectorOfArray) = imag.(x),
z̄ -> (nothing, im .* real.(z̄))

@adjoint Broadcast.broadcasted(::typeof(abs2), x::AbstractVectorOfArray) = abs2.(x),
@adjoint Broadcast.broadcasted(::typeof(abs2),
x::AbstractVectorOfArray) = abs2.(x),
z̄ -> (nothing, 2 .* real.(z̄) .* x)

@adjoint function Broadcast.broadcasted(
Expand Down Expand Up @@ -260,7 +261,9 @@ end
end
end

@adjoint Broadcast.broadcasted(::Type{T}, x::AbstractVectorOfArray) where {T <: Number} = T.(x),
@adjoint Broadcast.broadcasted(::Type{T},
x::AbstractVectorOfArray) where {T <:
Number} = T.(x),
ȳ -> (nothing, Zygote._project(x, ȳ))

function Zygote.unbroadcast(x::AbstractVectorOfArray, x̄)
Expand All @@ -273,11 +276,16 @@ function Zygote.unbroadcast(x::AbstractVectorOfArray, x̄)
end
end

@adjoint Broadcast.broadcasted(::Broadcast.AbstractArrayStyle, f::F, a::AbstractVectorOfArray, b) where {F} = _broadcast_generic(
@adjoint Broadcast.broadcasted(
::Broadcast.AbstractArrayStyle, f::F, a::AbstractVectorOfArray,
b) where {F} = _broadcast_generic(
__context__, f, a, b)
@adjoint Broadcast.broadcasted(::Broadcast.AbstractArrayStyle, f::F, a, b::AbstractVectorOfArray) where {F} = _broadcast_generic(
@adjoint Broadcast.broadcasted(::Broadcast.AbstractArrayStyle, f::F, a,
b::AbstractVectorOfArray) where {F} = _broadcast_generic(
__context__, f, a, b)
@adjoint Broadcast.broadcasted(::Broadcast.AbstractArrayStyle, f::F, a::AbstractVectorOfArray, b::AbstractVectorOfArray) where {F} = _broadcast_generic(
@adjoint Broadcast.broadcasted(
::Broadcast.AbstractArrayStyle, f::F, a::AbstractVectorOfArray,
b::AbstractVectorOfArray) where {F} = _broadcast_generic(
__context__, f, a, b)

@inline function _broadcast_generic(__context__, f::F, args...) where {F}
Expand Down
13 changes: 3 additions & 10 deletions src/RecursiveArrayTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,15 @@ An AbstractVectorOfArray subtype should match the following behaviors.

!!! note

In 2023 the linear indexing `A[i]`` was deprecated. It previously had the behavior that
`A[i] = A.u[i]`. However, this is incompatible with standard `AbstractArray` interfaces,
Since if `A = VectorOfArray([[1,2],[3,4]])` and `A` is supposed to act like `[1 3; 2 4]`,
then there is a difference `A[1] = [1,2]` for the VectorOfArray while `A[1] = 1` for the
matrix. This causes many issues if `AbstractVectorOfArray <: AbstractArray`. Thus we
plan in 2026 to complete the deprecation and thus have a breaking update where `A[i]`
matches the linear indexing of an `AbstractArray`, and then making
`AbstractVectorOfArray <: AbstractArray`. Until then, `AbstractVectorOfArray` due to
In 2023 the linear indexing `A[i]`` was deprecated. It previously had the behavior that `A[i] = A.u[i]`. However, this is incompatible with standard `AbstractArray`interfaces, Since if`A = VectorOfArray([[1,2],[3,4]])`and`A`is supposed to act like`[1 3; 2 4]`, then there is a difference `A[1] = [1,2]`for the VectorOfArray while`A[1] = 1`for the matrix. This causes many issues if`AbstractVectorOfArray <: AbstractArray`. Thus we plan in 2026 to complete the deprecation and thus have a breaking update where `A[i]`matches the linear indexing of an`AbstractArray`, and then making `AbstractVectorOfArray <: AbstractArray`. Until then, `AbstractVectorOfArray` due to
this interface break but manually implements an AbstractArray-like interface for
future compatibility.

## Fields

An AbstractVectorOfArray has the following fields:

* `u` which holds the Vector of values at each timestep
- `u` which holds the Vector of values at each timestep

## Array Interface

Expand Down Expand Up @@ -125,7 +118,7 @@ additional properties:

An AbstractDiffEqArray adds the following fields:

* `t` which holds the times of each timestep.
- `t` which holds the times of each timestep.
"""
abstract type AbstractDiffEqArray{T, N, A} <: AbstractVectorOfArray{T, N, A} end

Expand Down
9 changes: 7 additions & 2 deletions src/named_array_partition.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,20 @@ end

# hook into ArrayPartition broadcasting routines
@inline RecursiveArrayTools.npartitions(x::NamedArrayPartition) = npartitions(ArrayPartition(x))
@inline RecursiveArrayTools.unpack(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{NamedArrayPartition}}, i) = Broadcast.Broadcasted(
@inline RecursiveArrayTools.unpack(
bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{NamedArrayPartition}},
i) = Broadcast.Broadcasted(
bc.f, RecursiveArrayTools.unpack_args(i, bc.args))
@inline RecursiveArrayTools.unpack(x::NamedArrayPartition, i) = unpack(ArrayPartition(x), i)

function Base.copy(A::NamedArrayPartition{T, S, NT}) where {T, S, NT}
NamedArrayPartition{T, S, NT}(copy(ArrayPartition(A)), getfield(A, :names_to_indices))
end

@inline NamedArrayPartition(f::F, N, names_to_indices) where {F <: Function} = NamedArrayPartition(
@inline NamedArrayPartition(f::F,
N,
names_to_indices) where {F <:
Function} = NamedArrayPartition(
ArrayPartition(ntuple(f, Val(N))), names_to_indices)

@inline function Base.copy(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{NamedArrayPartition}})
Expand Down
2 changes: 2 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ function recursivefill!(bs::AbstractVectorOfArray{T, N},
a::T2) where {T <: StaticArraysCore.StaticArray,
T2 <: StaticArraysCore.StaticArray, N}
@inbounds for b in bs, i in eachindex(b)

b[i] = copy(a)
end
end
Expand All @@ -126,6 +127,7 @@ function recursivefill!(bs::AbstractVectorOfArray{T, N},
a::T2) where {T <: StaticArraysCore.SArray,
T2 <: Union{Number, Bool}, N}
@inbounds for b in bs, i in eachindex(b)

b[i] = fill(a, typeof(b[i]))
end
end
Expand Down
2 changes: 2 additions & 0 deletions test/adjoints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function loss3(x)
y = VectorOfArray([x .* i for i in 1:5])
tmp = 0.0
for i in 1:5, j in 1:5

tmp += y[i, j]
end
tmp
Expand All @@ -22,6 +23,7 @@ function loss4(x)
y = DiffEqArray([x .* i for i in 1:5], 1:5)
tmp = 0.0
for i in 1:5, j in 1:5

tmp += y[i, j]
end
tmp
Expand Down
6 changes: 3 additions & 3 deletions test/gpu/vectorofarray_gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ va_cu = convert(AbstractArray, va)
@test size(va_cu) == size(x)

a = VectorOfArray([ones(2) for i in 1:3])
_a = Adapt.adapt(CuArray,a)
_a = Adapt.adapt(CuArray, a)
@test _a isa VectorOfArray
@test _a.u isa Vector{<:CuArray}

b = DiffEqArray([ones(2) for i in 1:3],ones(2))
_b = Adapt.adapt(CuArray,b)
b = DiffEqArray([ones(2) for i in 1:3], ones(2))
_b = Adapt.adapt(CuArray, b)
@test _b isa DiffEqArray
@test _b.u isa Vector{<:CuArray}
@test _b.t isa CuArray
3 changes: 2 additions & 1 deletion test/partitions_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ end
@testset "Copy and zero with type changing array" begin
# Motivating use case for this is ArrayPartitions of Arrow arrays which are mmap:ed and change type when copied
struct TypeChangingArray{T, N} <: AbstractArray{T, N} end
Base.copy(::TypeChangingArray{T, N}) where {T, N} = Array{T, N}(undef,
Base.copy(::TypeChangingArray{
T, N}) where {T, N} = Array{T, N}(undef,
ntuple(_ -> 0, N))
Base.zero(::TypeChangingArray{T, N}) where {T, N} = zeros(T, ntuple(_ -> 0, N))

Expand Down
Loading