diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-09-25 10:36:23 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-09-25 10:36:23 -0600 |
commit | 58c73fd511b77cb94124b71a4bb75c7ab6a6d8bc (patch) | |
tree | 25ae52afe365de29973efbb10fdecf2712deb430 /cl | |
parent | 2e284b71500a1f8dc6cc46ecf21eb1e9389ea780 (diff) | |
download | cmath-58c73fd511b77cb94124b71a4bb75c7ab6a6d8bc.tar.gz cmath-58c73fd511b77cb94124b71a4bb75c7ab6a6d8bc.zip |
add september notes & hw2 code / pdf
Diffstat (limited to 'cl')
-rw-r--r-- | cl/lizfcm.asd | 10 | ||||
-rw-r--r-- | cl/src/approx/maceps.lisp | 13 | ||||
-rw-r--r-- | cl/src/approx/package.lisp | 3 | ||||
-rw-r--r-- | cl/src/package.lisp | 7 | ||||
-rw-r--r-- | cl/src/vector/distance.lisp | 6 | ||||
-rw-r--r-- | cl/src/vector/norm.lisp | 17 | ||||
-rw-r--r-- | cl/src/vector/package.lisp | 7 | ||||
-rw-r--r-- | cl/tests/maceps.lisp | 25 | ||||
-rw-r--r-- | cl/tests/vector.lisp | 31 |
9 files changed, 117 insertions, 2 deletions
diff --git a/cl/lizfcm.asd b/cl/lizfcm.asd index ad69fa1..3e8c289 100644 --- a/cl/lizfcm.asd +++ b/cl/lizfcm.asd @@ -11,7 +11,13 @@ (:file "package"))) (:module "approx" :components - ((:file "derivative" :depends-on ("package")) + ((:file "maceps" :depends-on ("package")) + (:file "derivative" :depends-on ("package")) + (:file "package"))) + (:module "vector" + :components + ((:file "distance" :depends-on ("norm" "package")) + (:file "norm" :depends-on ("package")) (:file "package"))))))) @@ -23,7 +29,9 @@ :components ((:module "tests" :components ((:file "table" :depends-on ("suite")) + (:file "maceps" :depends-on ("suite")) (:file "approx" :depends-on ("suite")) + (:file "vector" :depends-on ("suite")) (:file "suite")))) :perform (asdf:test-op (o c) (uiop:symbol-call :fiveam :run! diff --git a/cl/src/approx/maceps.lisp b/cl/src/approx/maceps.lisp new file mode 100644 index 0000000..cd24482 --- /dev/null +++ b/cl/src/approx/maceps.lisp @@ -0,0 +1,13 @@ +(in-package :lizfcm.approx) + +(defun compute-maceps (val f) + (let* ((h val) + (a val) + (err val)) + (loop while (> err 0) + do + (progn + (setf h (/ h 2) + err (abs (- (funcall f (+ a h)) + (funcall f a))))) + collect (list a h err)))) diff --git a/cl/src/approx/package.lisp b/cl/src/approx/package.lisp index 60dd0bb..c11eb60 100644 --- a/cl/src/approx/package.lisp +++ b/cl/src/approx/package.lisp @@ -1,4 +1,5 @@ (in-package :cl-user) (defpackage lizfcm.approx (:use :cl) - (:export :derivative-at)) + (:export :derivative-at + :compute-maceps)) diff --git a/cl/src/package.lisp b/cl/src/package.lisp new file mode 100644 index 0000000..88b10eb --- /dev/null +++ b/cl/src/package.lisp @@ -0,0 +1,7 @@ +(in-package :cl-user) +(defpackage lizfcm.vector + (:use :cl) + (:export + :n-norm + :max-norm + :distance)) diff --git a/cl/src/vector/distance.lisp b/cl/src/vector/distance.lisp new file mode 100644 index 0000000..74631ce --- /dev/null +++ b/cl/src/vector/distance.lisp @@ -0,0 +1,6 @@ +(in-package :lizfcm.vector) + +(defun distance (v1 v2 norm) + (let* ((d (mapcar #'- v1 v2)) + (length (funcall norm d))) + length)) diff --git a/cl/src/vector/norm.lisp b/cl/src/vector/norm.lisp new file mode 100644 index 0000000..2158296 --- /dev/null +++ b/cl/src/vector/norm.lisp @@ -0,0 +1,17 @@ +(in-package :lizfcm.vector) + +(defun p-norm (p) + (lambda (v) + (expt + (reduce (lambda (acc x) + (+ acc x)) + (mapcar (lambda (x) + (abs + (expt x p))) + v)) + (/ 1 p)))) + +(defun max-norm (v) + (reduce (lambda (acc x) + (max acc x)) + v)) diff --git a/cl/src/vector/package.lisp b/cl/src/vector/package.lisp new file mode 100644 index 0000000..009b190 --- /dev/null +++ b/cl/src/vector/package.lisp @@ -0,0 +1,7 @@ +(in-package :cl-user) +(defpackage lizfcm.vector + (:use :cl) + (:export + :p-norm + :max-norm + :distance)) diff --git a/cl/tests/maceps.lisp b/cl/tests/maceps.lisp new file mode 100644 index 0000000..9d61772 --- /dev/null +++ b/cl/tests/maceps.lisp @@ -0,0 +1,25 @@ +(defpackage lizfcm/tests.maceps + (:use :cl + :fiveam + :lizfcm.approx + :lizfcm.utils + :lizfcm/tests) + (:export :approx-suite)) +(in-package :lizfcm/tests.maceps) + +(def-suite maceps-suite + :in lizfcm-test-suite) +(in-suite maceps-suite) + +(test maceps + :description "double precision provides precision about (mac eps of single precision) squared" + (let* ((maceps-computation-double (compute-maceps 1.0d0 + (lambda (x) x))) + (maceps-computation-single (compute-maceps 1.0 + (lambda (x) x))) + (last-double-h (cadar (last maceps-computation-double))) + (last-single-h (cadar (last maceps-computation-single)))) + (is (within-range-p + (- last-double-h (* last-single-h last-single-h)) + 0 + last-single-h)))) diff --git a/cl/tests/vector.lisp b/cl/tests/vector.lisp new file mode 100644 index 0000000..3ffe5a8 --- /dev/null +++ b/cl/tests/vector.lisp @@ -0,0 +1,31 @@ +(defpackage lizfcm/tests.vector + (:use :cl + :fiveam + :lizfcm.vector + :lizfcm.utils + :lizfcm/tests) + (:export :vector-suite)) +(in-package :lizfcm/tests.vector) + +(def-suite vector-suite + :in lizfcm-test-suite) +(in-suite vector-suite) + +(test p-norm + :description "computes p-norm" + (let ((v '(1 1)) + (length (sqrt 2)) + (2-norm (p-norm 2))) + (is (within-range-p (funcall 2-norm v) + length + 0.00001)))) + +(test vector-distance + :description "computes distance via norm" + (let ((v1 '(0 0)) + (v2 '(1 1)) + (dist (sqrt 2))) + (is (within-range-p (distance v1 v2 (p-norm 2)) + dist + 0.00001)))) + |