about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2023-08-23 07:52:19 -0400
committerelioat <elioat@tilde.institute>2023-08-23 07:52:19 -0400
commit562a9a52d599d9a05f871404050968a5fd282640 (patch)
tree7d3305c1252c043bfe246ccc7deff0056aa6b5ab /js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm
parent5d012c6c011a9dedf7d0a098e456206244eb5a0f (diff)
downloadtour-562a9a52d599d9a05f871404050968a5fd282640.tar.gz
*
Diffstat (limited to 'js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm')
-rw-r--r--js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm47
1 files changed, 47 insertions, 0 deletions
diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm
new file mode 100644
index 0000000..079a8b5
--- /dev/null
+++ b/js/games/nluqo.github.io/~bh/61a-pages/Lectures/1.3/roots.scm
@@ -0,0 +1,47 @@
+;;; Note: all versions work only for quadratics with real roots
+
+;;; Straightforward but slow way:
+
+(define (roots a b c)
+  (se (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a))
+      (/ (- (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a)) ))
+
+
+;;; Using a subprocedure to eliminate the repeated computation:
+
+(define (roots a b c)
+  (define (roots1 d)
+    (se (/ (+ (- b) d) (* 2 a))
+	(/ (- (- b) d) (* 2 a)) ))
+  (roots1 (sqrt (- (* b b) (* 4 a c)))) )
+
+
+;;; Using lambda to avoid naming the subprocedure:
+
+(define (roots a b c)
+  ((lambda (d)
+     (se (/ (+ (- b) d) (* 2 a))
+	 (/ (- (- b) d) (* 2 a)) ))
+   (sqrt (- (* b b) (* 4 a c))) ))
+
+
+;;; Using let to rearrange the above:
+
+(define (roots a b c)
+  (let ((d (sqrt (- (* b b) (* 4 a c)))))
+    (se (/ (+ (- b) d) (* 2 a))
+	(/ (- (- b) d) (* 2 a)) )))
+
+
+
+;;; More optimization:
+
+(define (roots a b c)
+  (let ((d (sqrt (- (* b b) (* 4 a c))))
+	(-b (- b))
+	(2a (* 2 a)))
+    (se (/ (+ -b d) 2a)
+	(/ (- -b d) 2a) )))
+
+
+