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

Preserve metadata on getindex operation when scalarizing #1010

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,12 @@ function scalarize(arr)
end
elseif istree(arr) && operation(arr) == getindex
args = arguments(arr)
scalarize(args[1], (args[2:end]...,))
scalarized = scalarize(args[1], (args[2:end]...,))
if isnothing(metadata(arr))
scalarized
else
metadata(scalarized, metadata(arr))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shashi @YingboMa should the metadata be the same, or should it know that it's derived from indexing this object somehow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be the same... And I think this should not be added... I would say this is a usecase where tagged-data like API becomes useful. You should be able to define something like:

@metadata getindex(::MetadataType, idx) = compute_output_metadata_here

And it should be handled for every key which registers in this way.

I'd be interested in adding this general feature, but I'm looking for more substantial examples where this is super useful.

Until then, I think you should just try to propagate the metadata outside of the package's behavior (by walking the tree once constructed). The method in this PR seems not correct to me.

end
elseif arr isa Num
wrap(scalarize(unwrap(arr)))
elseif istree(arr) && symtype(arr) <: Number
Expand Down
5 changes: 5 additions & 0 deletions test/arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ getdef(v) = getmetadata(v, Symbolics.VariableDefaultValue)
@variables t F(t)[1:1]

@test isequal(collect(F ./ t), [F[1] / t])

@variables a[1:2]
x = setmetadata(a[1], TestMetaT, 5)
@test getmetadata(x, TestMetaT) == 5
@test getmetadata(scalarize(x), TestMetaT) == 5
end

n = 2
Expand Down
Loading