Skip to content
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

fix: vecvecapply #320

Merged
merged 1 commit into from
Dec 29, 2023
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
8 changes: 5 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,18 @@

Calls `f` on each element of a vecvec `v`.
"""
function vecvecapply(f, v)
function vecvecapply(f, v::AbstractArray{<:AbstractArray})

Check warning on line 177 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L177

Added line #L177 was not covered by tests
sol = Vector{eltype(eltype(v))}()
for i in eachindex(v)
for j in eachindex(v[:, i])
push!(sol, v[:, i][j])
for j in eachindex(v[i])
push!(sol, v[i][j])

Check warning on line 181 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L180-L181

Added lines #L180 - L181 were not covered by tests
end
end
f(sol)
end

vecvecapply(f, v::AbstractVectorOfArray) = vecvecapply(f, v.u)

Check warning on line 187 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L187

Added line #L187 was not covered by tests

function vecvecapply(f, v::Array{T}) where {T <: Number}
f(v)
end
Expand Down
7 changes: 7 additions & 0 deletions test/utils_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ A = [[1 2; 3 4], [1 3; 4 6], [5 6; 7 8]]
A = zeros(5, 5)
@test recursive_unitless_eltype(A) == Float64

@test vecvecapply(x -> abs.(x), -1) == 1
@test vecvecapply(x -> abs.(x), [-1, -2, 3, -4]) == [1, 2, 3, 4]
v = [[-1 2; 3 -4], [5 -6; -7 -8]]
vv = [1, 3, 2, 4, 5, 7, 6, 8]
@test vecvecapply(x -> abs.(x), v) == vv
@test vecvecapply(x -> abs.(x), VectorOfArray(v)) == vv

using Unitful
A = zeros(5, 5) * 1u"kg"
@test recursive_unitless_eltype(A) == Float64
Expand Down
Loading