Skip to content

Commit

Permalink
Implemented method 'create_array' for Base.ReshapedArrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
benedict-96 committed Dec 11, 2024
1 parent 59e3aa6 commit 4c16785
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,10 @@ end
create_array(P, S, nd, d, elems...)
end

@inline function create_array(::Type{<:Base.ReshapedArray{T, N, P}}, S, nd::Val, d, elems...) where {T, N, P}
create_array(P, S, nd, d, elems...)
end

## SArray
@inline function create_array(::Type{<:SArray}, ::Nothing, nd::Val, ::Val{dims}, elems...) where dims
SArray{Tuple{dims...}}(elems...)
Expand Down
6 changes: 6 additions & 0 deletions test/code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ nanmath_st.rewrites[:nanmath] = true
@test eval(toexpr(Let([a 1, b 2, arr [1,2]],
MakeArray([a b;a+b a/b], arr)))) == [1 2; 3 1/2]

@test eval(toexpr(Let([a 1, b 2, arr [1,2]],
MakeArray(reshape(view([a,b,a+b,a/b], :), 1, 4), arr)))) == [1 2 3 1/2]

@test eval(toexpr(Let([a 1, b 2, arr @SVector([1,2])],
MakeArray([a,b,a+b,a/b], arr)))) === @SVector [1, 2, 3, 1/2]

Expand All @@ -159,6 +162,9 @@ nanmath_st.rewrites[:nanmath] = true
@test eval(toexpr(Let([a 1, b 2, arr @SLVector((:a, :b))(@SVector[1,2])],
MakeArray([a+b,a/b], arr)))) === @SLVector((:a, :b))(@SVector [3, 1/2])

@test eval(toexpr(Let([a 1, b 2, arr reshape(view([1,2], :), 1, 2)],
MakeArray([a,b,a+b,a/b], arr)))) == [1, 2, 3, 1/2]

trackedarr = eval(toexpr(Let([a ReverseDiff.track(1.0), b 2, arr ReverseDiff.track(ones(2))],
MakeArray([a+b,a/b], arr))))
@test trackedarr isa ReverseDiff.TrackedArray
Expand Down

0 comments on commit 4c16785

Please sign in to comment.