Skip to content

Commit

Permalink
Add setfield frule
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Dec 29, 2023
1 parent ae37562 commit 3b00cd5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"

[compat]
Adapt = "3.4.0"
ChainRulesCore = "1.15.3"
ChainRulesCore = "1.17"
ChainRulesTestUtils = "1.5"
Compat = "3.46, 4.2"
Distributed = "1"
Expand Down
8 changes: 8 additions & 0 deletions src/rulesets/Base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
@scalar_rule zero(x) ZeroTangent()
@scalar_rule transpose(x) true


function ChainRulesCore.frule((_, ȯbj, _, ẋ), ::typeof(setfield!), obj, field, x)
ȯbj::MutableTangent
y = setfield!(obj, field, x)
= setproperty!(ȯbj, field, ẋ)
return y, ẏ
end

# `adjoint`

frule((_, Δz), ::typeof(adjoint), z::Number) = (z', Δz')
Expand Down
9 changes: 9 additions & 0 deletions test/rulesets/Base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
end
end
end

@testset "setfield!" begin
mutable struct MDemo
x::Float64
end

test_frule(setfield!, MDemo(3.5)MutableTangent{MDemo}(x=2.0), :x, 5.0)
test_frule(setfield!, MDemo(3.5)MutableTangent{MDemo}(x=2.0), 1, 5.0)
end

@testset "Trig" begin
@testset "Basics" for x = (Float64(π)-0.01, Complex(π, π/2))
Expand Down

0 comments on commit 3b00cd5

Please sign in to comment.