Skip to content

Commit dfe4d67

Browse files
YingboMashashi
andauthored
muladd with Zero() (#98)
* `muladd` with `Zero()` Co-authored-by: "Shashi Gowda" <gowda@mit.edu> Co-authored-by: "Yingbo Ma" <mayingbo5@gmail.com> * Bump version Co-authored-by: Shashi Gowda <shashigowda91@gmail.com>
1 parent 4028afc commit dfe4d67

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesCore"
22
uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3-
version = "0.5.4"
3+
version = "0.5.4-DEV"
44

55
[deps]
66
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"

src/differential_arithmetic.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ Base.:+(::Zero, ::DoesNotExist) = DoesNotExist()
3131
Base.:*(::DoesNotExist, ::Zero) = Zero()
3232
Base.:*(::Zero, ::DoesNotExist) = Zero()
3333

34+
Base.muladd(::Zero, x, y) = y
35+
Base.muladd(x, ::Zero, y) = y
36+
Base.muladd(x, y, ::Zero) = x*y
37+
38+
Base.muladd(::Zero, ::Zero, y) = y
39+
Base.muladd(x, ::Zero, ::Zero) = Zero()
40+
Base.muladd(::Zero, x, ::Zero) = Zero()
41+
42+
Base.muladd(::Zero, ::Zero, ::Zero) = Zero()
3443

3544
Base.:+(::Zero, ::Zero) = Zero()
3645
Base.:*(::Zero, ::Zero) = Zero()

test/differentials/zero.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,14 @@
1616
@test zero(One()) === z
1717
@test zero(DoesNotExist()) === z
1818
@test zero(Composite{Tuple{Int,Int}}((1, 2))) === z
19+
20+
# use mutable objects to test the strong `===` condition
21+
x = ones(2)
22+
@test muladd(Zero(), 2, x) === x
23+
@test muladd(2, Zero(), x) === x
24+
@test muladd(Zero(), Zero(), x) === x
25+
@test muladd(2, 2, Zero()) === 4
26+
@test muladd(x, Zero(), Zero()) === Zero()
27+
@test muladd(Zero(), x, Zero()) === Zero()
28+
@test muladd(Zero(), Zero(), Zero()) === Zero()
1929
end

0 commit comments

Comments
 (0)