Skip to content

Commit

Permalink
add nil impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Apr 11, 2024
1 parent 53d4fd3 commit c95133e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/emmy/differential.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@
;; The default implementations are straightforward, and match the docstrings:

(extend-protocol IPerturbed
nil
(perturbed? [_] false)
(replace-tag [_ _ _] nil)
(extract-tangent [_ _] 0)

#?(:clj Object :cljs default)
(perturbed? [_] false)
(replace-tag [this _ _] this)
Expand Down
6 changes: 5 additions & 1 deletion test/emmy/differential_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@
(g/* x g/cube))]
(g x)))
Df (derivative f)]

(is (= ((derivative (g/* identity g/square)) 10)
(Df 10))
"providing 10 takes the x*g/square branch")
Expand All @@ -232,6 +231,11 @@
"d/with-active-tag calls its fn with the supplied args.")))

(deftest dual-arithmetic-tests
(let [Df (derivative (fn [_x]))]
(checking "derivative of nil-valued function is always zero" 100
[x sg/real]
(is (zero? (Df x)))))

(checking "(a/b)*b == a, (a*b)/b == a" 100
[x integral-dual-gen
y (nonzero integral-dual-gen)]
Expand Down

0 comments on commit c95133e

Please sign in to comment.