Skip to content

Commit

Permalink
Add test files for ex-2-73 and ex-2-75 and improve derivation functions
Browse files Browse the repository at this point in the history
Created new test files for exercises 2-73 and 2-75. The test cases for these exercises are now fully covered. Also, made some improvements in the derivation functions in exercise 2-73 by replacing `put` with `put-op`
and `get` with `get-op` methods to manage operations in a more optimized way.
  • Loading branch information
Denis Smet committed Feb 11, 2024
1 parent 668f42e commit a0721ad
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/sicp/chapter_2/part_4/ex_2_73.clj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
; ((get (operator exp) 'deriv)
; (operands exp) var)

(def derivations (atom {}))

(defn put-op
[op method deriv-fn]
(swap! derivations assoc-in [op method] deriv-fn))

(defn get-op
[op method]
(get-in @derivations [op method]))

(defn deriv
[exp var]
(cond
Expand Down Expand Up @@ -87,7 +97,7 @@
(cond
(number? exp) 0
(b23/variable? exp) (if (b23/same-variable? exp var) 1 0)
:else ((get :deriv (operator exp))
:else ((get-op :deriv (operator exp))
(operands exp)
var)))

Expand All @@ -96,11 +106,6 @@
; Sorry, I'm lazy and took examples of code here and rewrite it to Clojure
; https://github.com/ivanjovanovic/sicp/blob/master/2.4/e-2.73.scm

(defn put
[param1 param2 deriver]
; Just for linter
(println param1 param2 deriver))

(defn make-sum
([a b] (list '+ a b))
([a b c] (list '+ a b c)))
Expand All @@ -118,7 +123,7 @@
(make-sum (deriv (addend operands) var)
(deriv (augend operands) var)))]
; and methods for putting the thing in the table
(put '+ 'deriv derive-sum)))
(put-op '+ :deriv derive-sum)))

(defn install-product-derivation
[]
Expand All @@ -133,7 +138,7 @@
(multiplicand operands))
var))]
; put that into table
(put '* 'deriv derive-product)))
(put-op '* :deriv derive-product)))

(defn install-exponent-derivation
[]
Expand All @@ -146,4 +151,4 @@
(make-product (power operands)
(make-exponent (base operands) (dec (power operands))))
(deriv (base operands) var)))]
(put '** 'deriv derive-exponent)))
(put-op '** :deriv derive-exponent)))
25 changes: 25 additions & 0 deletions test/sicp/chapter_2/part_4/ex_2_73_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(ns sicp.chapter-2.part-4.ex-2-73-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-2.part-4.ex-2-73 :as ex-2-73]
[sicp.misc :as m]))

(ex-2-73/install-sum-derivation)
(ex-2-73/install-product-derivation)
(ex-2-73/install-exponent-derivation)

(deftest deriv-test
(is (= 0 (ex-2-73/deriv 10 5)))
(is (= 0 (ex-2-73/deriv 'x 5)))
(is (= 0 (ex-2-73/deriv 'x 'y)))
(is (= 0 (ex-2-73/deriv '+ 'y)))
(is (= 0 (ex-2-73/deriv '(+ 2 2) 'x)))
(is (= 0 (ex-2-73/deriv '(* 2 2) 'x)))
(is (= true (m/is-exception? (ex-2-73/deriv '(** 2 2) 'x) "unknown expression type: DERIV (** 2 2)"))))

(deftest deriv-v2-test
(is (= 0 (ex-2-73/deriv-v2 10 5)))
(is (= 0 (ex-2-73/deriv-v2 'x 5)))
(is (= 0 (ex-2-73/deriv-v2 'x 'y)))
(is (= 0 (ex-2-73/deriv-v2 '+ 'y)))
(is (= true (m/is-exception? (ex-2-73/deriv-v2 '(* 2 2) 'x) "unknown expression type: DERIV (* 2 2)"))))
18 changes: 18 additions & 0 deletions test/sicp/chapter_2/part_4/ex_2_75_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns sicp.chapter-2.part-4.ex-2-75-test
(:require
[clojure.test :refer :all]
[sicp.chapter-2.part-4.ex-2-75 :refer [make-from-mag-ang]]
[sicp.misc :as m]))

(def angle-60 (/ Math/PI 3))
(def radius 2.0)
(def x 1.0000000000000002)
(def y 1.7320508075688772)

(deftest make-from-mag-ang-test
(is (= x ((make-from-mag-ang radius angle-60) :real-part)))
(is (= y ((make-from-mag-ang radius angle-60) :imag-part)))
(is (= radius ((make-from-mag-ang radius angle-60) :magnitude)))
(is (= angle-60 ((make-from-mag-ang radius angle-60) :angle)))
(is (= true (m/is-exception? ((make-from-mag-ang radius angle-60) :undefined)
"Unknown op: MAKE-FROM-REAL-IMAG :undefined"))))

0 comments on commit a0721ad

Please sign in to comment.