Skip to content

Commit

Permalink
make partial output container iterable (zero cost) (#47)
Browse files Browse the repository at this point in the history
* make partial output container iterable (zero cost)

* fix test
  • Loading branch information
tpapp committed Nov 9, 2023
1 parent eb8519a commit 9ec9c6a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SpectralKit"
uuid = "5c252ae7-b5b6-46ab-a016-b0e3d78320b7"
authors = ["Tamas K. Papp <tkpapp@gmail.com>"]
version = "0.14.0"
version = "0.14.1"

[deps]
ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197"
Expand Down
6 changes: 4 additions & 2 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ end

"""
Container for output of evaluating partial derivatives. Each corresponds to an
specification in a [`∂Specification`](@ref). Can be indexed with integers, or converted
to a `Tuple`.
specification in a [`∂Specification`](@ref). Can be indexed with integers, iterated, or
converted to a `Tuple`.
"""
struct ∂Output{N,T}
values::NTuple{N,T}
Expand All @@ -303,6 +303,8 @@ end

@inline Base.getindex(∂output::∂Output, i) = ∂output.values[i]

@inline Base.iterate(∂output::∂Output, i...) = Base.iterate(∂output.values, i...)

function _product(∂specification::∂Specification, sources, indices)
(; lookups) = ∂specification
p = map(lookups) do lookup
Expand Down
14 changes: 14 additions & 0 deletions test/test_derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,17 @@ end
end
end
end

@testset "iteration for ∂Output" begin
o_src = (1.0, 2.0, 3.0)
∂o = SpectralKit.∂Output(o_src)
for (o1, o2) in zip(o_src, ∂o)
@test o1 o2
end
function f(o)
o1, o2, o3 = o
o1 + o2 + o3
end
@test @inferred(f(∂o)) == sum(o_src)
@test @ballocated($f($∂o)) == 0
end

2 comments on commit 9ec9c6a

@tpapp
Copy link
Owner Author

@tpapp tpapp commented on 9ec9c6a Nov 9, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/95052

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.14.1 -m "<description of version>" 9ec9c6acc8ca8fc6e2d9c341611f20925fe766b6
git push origin v0.14.1

Please sign in to comment.