Skip to content

Commit

Permalink
fix: vecvecapply
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSabharwal committed Dec 29, 2023
1 parent 5830231 commit b3cdb27
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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 @@ vecvecapply(f::Base.Callable, v)
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

0 comments on commit b3cdb27

Please sign in to comment.