diff --git a/Project.toml b/Project.toml index 340eab72..a5116f01 100644 --- a/Project.toml +++ b/Project.toml @@ -1,14 +1,16 @@ name = "ValueShapes" uuid = "136a8f8c-c49b-4edb-8b98-f3d64d48be8f" -version = "0.11.5" +version = "0.11.6" [deps] +Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" ArgCheck = "dce04be8-c92d-5529-be00-80e4d2c0e197" ArraysOfArrays = "65a8f2f4-9b39-5baf-92e2-a9cc46fdf018" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" ElasticArrays = "fdbdab4c-e67f-52f5-8c3f-e7b388dad3d4" FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953" +InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" @@ -16,18 +18,14 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9" [weakdeps] -Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" ChangesOfVariables = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" -InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" Mooncake = "da2b9cff-9c12-43a0-ae48-6db2b0edb7d6" ZygoteRules = "700de1a5-db45-46bc-99cf-38207098b444" [extensions] -ValueShapesAdaptExt = "Adapt" ValueShapesChainRulesCoreExt = "ChainRulesCore" ValueShapesChangesOfVariablesExt = "ChangesOfVariables" -ValueShapesInverseFunctionsExt = "InverseFunctions" ValueShapesMooncakeExt = "Mooncake" ValueShapesZygoteRulesExt = "ZygoteRules" diff --git a/ext/ValueShapesAdaptExt.jl b/ext/ValueShapesAdaptExt.jl deleted file mode 100644 index 24fb0690..00000000 --- a/ext/ValueShapesAdaptExt.jl +++ /dev/null @@ -1,18 +0,0 @@ -# This file is a part of ValueShapes.jl, licensed under the MIT License (MIT). - -module ValueShapesAdaptExt - -using ValueShapes -using ValueShapes: _data, _valshape, _elshape - -import Adapt - -function Adapt.adapt_structure(to, x::ShapedAsNT) - ShapedAsNT(Adapt.adapt(to, _data(x)), _valshape(x)) -end - -function Adapt.adapt_structure(to, x::ShapedAsNTArray) - ShapedAsNTArray(Adapt.adapt(to, _data(x)), _elshape(x)) -end - -end # module ValueShapesAdaptExt diff --git a/ext/ValueShapesInverseFunctionsExt.jl b/ext/ValueShapesInverseFunctionsExt.jl deleted file mode 100644 index a7c143cb..00000000 --- a/ext/ValueShapesInverseFunctionsExt.jl +++ /dev/null @@ -1,14 +0,0 @@ -# This file is a part of ValueShapes.jl, licensed under the MIT License (MIT). - -module ValueShapesInverseFunctionsExt - -using ValueShapes -using ValueShapes: _InvValueShape - -import InverseFunctions - - -InverseFunctions.inverse(vs::AbstractValueShape) = Base.Fix2(unshaped, vs) -InverseFunctions.inverse(inv_vs::_InvValueShape) = inv_vs.x - -end # module ValueShapesInverseFunctionsExt diff --git a/src/ValueShapes.jl b/src/ValueShapes.jl index 3255929f..73a5adc5 100644 --- a/src/ValueShapes.jl +++ b/src/ValueShapes.jl @@ -18,10 +18,12 @@ using ArraysOfArrays using Distributions using ElasticArrays using FillArrays +using InverseFunctions using Random using Statistics using StatsBase +import Adapt import IntervalSets import Tables import TypedTables diff --git a/src/named_tuple_shape.jl b/src/named_tuple_shape.jl index 96b69809..f09e5178 100644 --- a/src/named_tuple_shape.jl +++ b/src/named_tuple_shape.jl @@ -389,6 +389,11 @@ Base.isapprox(A::ShapedAsNT, B::ShapedAsNT; kwargs...) = isapprox(_data(A), _dat Base.copy(A::ShapedAsNT) = ShapedAsNT(copy(_data(A)),_valshape(A)) +function Adapt.adapt_structure(to, x::ShapedAsNT) + ShapedAsNT(Adapt.adapt(to, _data(x)), _valshape(x)) +end + + # Required for accumulation during automatic differentiation: function Base.:(+)(A::ShapedAsNT{names}, B::ShapedAsNT{names}) where names @argcheck _valshape(A) == _valshape(B) @@ -638,6 +643,11 @@ end @inline Tables.rows(A::ShapedAsNTArray) = A +function Adapt.adapt_structure(to, x::ShapedAsNTArray) + ShapedAsNTArray(Adapt.adapt(to, _data(x)), _elshape(x)) +end + + const _AnySNTArray{names} = ShapedAsNTArray{<:Union{NamedTuple{names},ShapedAsNT{names}}} diff --git a/src/value_shape.jl b/src/value_shape.jl index 39b1a1b9..f51f5584 100644 --- a/src/value_shape.jl +++ b/src/value_shape.jl @@ -270,6 +270,10 @@ const _InvValueShape = Base.Fix2{typeof(unshaped),<:AbstractValueShape} Base.Broadcast.broadcasted(unshaped, xs, Ref(inv_vs.x)) end + +InverseFunctions.inverse(vs::AbstractValueShape) = Base.Fix2(unshaped, vs) +InverseFunctions.inverse(inv_vs::_InvValueShape) = inv_vs.x + const _BroadcastValueShape = Base.Fix1{typeof(broadcast),<:AbstractValueShape} const _BroadcastInvValueShape = Base.Fix1{typeof(broadcast),<:_InvValueShape} const _BroadcastUnshaped = Base.Fix1{typeof(broadcast),typeof(unshaped)}