From 834bb45be7e74553f51da00bad95595666586a7c Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Tue, 10 Oct 2023 14:44:33 -0600 Subject: [PATCH] problem one hw 4 --- compile.sh => compile_archive.sh | 0 lizfcm.a | Bin 9632 -> 9632 bytes md2pdf.css | 3 -- src/lizfcm.asd | 1 + src/main.lisp | 60 ++++++++++++++++++++++++++++++ src/tests,vector.lisp | 11 ++++++ src/vector,least-squares-reg.lisp | 2 + src/vector,least-squares.lisp | 14 +++++++ src/vector,package.lisp | 3 +- 9 files changed, 90 insertions(+), 4 deletions(-) rename compile.sh => compile_archive.sh (100%) mode change 100644 => 100755 delete mode 100644 md2pdf.css create mode 100644 src/main.lisp create mode 100644 src/vector,least-squares-reg.lisp create mode 100644 src/vector,least-squares.lisp diff --git a/compile.sh b/compile_archive.sh old mode 100644 new mode 100755 similarity index 100% rename from compile.sh rename to compile_archive.sh diff --git a/lizfcm.a b/lizfcm.a index 1d27463724a6783a9702623d29341286a2165f06..ec49adbcf02005c6d536e541a82dd6c47b97bdc6 100644 GIT binary patch delta 12 TcmZ4By})~d0;9!7#d=i$9&iLU delta 12 TcmZ4By})~d0;9=B#d=i$9$W-6 diff --git a/md2pdf.css b/md2pdf.css deleted file mode 100644 index e2718ba..0000000 --- a/md2pdf.css +++ /dev/null @@ -1,3 +0,0 @@ -img { - max-width: 100%; -} diff --git a/src/lizfcm.asd b/src/lizfcm.asd index ee8beb0..0096257 100644 --- a/src/lizfcm.asd +++ b/src/lizfcm.asd @@ -9,6 +9,7 @@ (:file "approx,maceps" :depends-on ("approx,package")) (:file "approx,derivative" :depends-on ("approx,package")) (:file "approx,package") + (:file "vector,least-squares") (:file "vector,distance" :depends-on ("vector,norm" "vector,package")) (:file "vector,norm" :depends-on ("vector,package")) (:file "vector,package"))) diff --git a/src/main.lisp b/src/main.lisp new file mode 100644 index 0000000..7a8b455 --- /dev/null +++ b/src/main.lisp @@ -0,0 +1,60 @@ +(load "lizfcm.asd") +(ql:quickload :lizfcm) + +;; this is a collection showcasing the library developed for math4610, required +;; from the Shared Library definition + +(defun smaceps () + (cadar (last (lizfcm.approx:compute-maceps + (lambda (x) x) 1.0 1.0)))) + +(defun dmaceps () + (cadar (last (lizfcm.approx:compute-maceps + (lambda (x) x) 1.0d0 1.0d0)))) + +(defun l2-norm (v) + (let ((2-norm (lizfcm.vector:p-norm 2))) + (funcall 2-norm v))) + +(defun l1-norm (v) + (let ((1-norm (lizfcm.vector:p-norm 1))) + (funcall 1-norm v))) + +(defun linf-norm (v) + (lizfcm.vector:max-norm v)) + +(defun l2-distance (v1 v2) + (let ((2-norm (lizfcm.vector:p-norm 2))) + (lizfcm.vector:distance v1 v2 2-norm))) + +(defun l1-distance (v1 v2) + (let ((1-norm (lizfcm.vector:p-norm 1))) + (lizfcm.vector:distance v1 v2 1-norm))) + +(defun linf-distance (v1 v2) + (lizfcm.vector:distance v1 v2 'lizfcm.vector:max-norm)) + +(defun f (x) + (/ (- x 1) (+ x 1))) + +(defun fprime (x) + (/ 2 (expt (+ x 1) 2))) + +(defmacro showcase (s-expr) + `(format t "~a = ~a~%" ,(format nil "~a" s-expr) ,s-expr)) + +(defun main () + (showcase (smaceps)) + (showcase (dmaceps)) + (showcase (l2-norm '(1 2))) + (showcase (l1-norm '(1 2))) + (showcase (linf-norm '(1 2))) + (showcase (l1-distance '(1 2) '(-3 4))) + (showcase (l2-distance '(1 2) '(-3 4))) + (showcase (linf-distance '(1 2) '(-3 4))) + (showcase (lizfcm.vector:least-squares-reg '(1 2 3 4 5 6 7) + '(0.5 3 2 3.5 5 6 7.5))) + (showcase (lizfcm.approx:forward-derivative-at 'f 1 0.00001)) + (showcase (lizfcm.approx:central-derivative-at 'f 1 0.00001)) + (showcase (lizfcm.approx:backward-derivative-at 'f 1 0.00001))) + diff --git a/src/tests,vector.lisp b/src/tests,vector.lisp index 3ffe5a8..6edb1ac 100644 --- a/src/tests,vector.lisp +++ b/src/tests,vector.lisp @@ -29,3 +29,14 @@ dist 0.00001)))) +(test least-squares + :description "least squares is correct enough" + (let ((x '(0 1 2 3 4)) + (y '(1 2 3 4 5))) + (destructuring-bind (m b) (lizfcm.vector:least-squares-reg x y) + (is (within-range-p m 1 0.00001)) + (is (within-range-p b 1 0.00001)))) + (let ((x '(1 2 3 4 5 6 7)) + (y '(0.5 3 2 3.5 5 6 7.5))) + (destructuring-bind (m b) (lizfcm.vector:least-squares-reg x y) + (is (within-range-p m 1 0.3))))) ;; just a guestimate for best fit diff --git a/src/vector,least-squares-reg.lisp b/src/vector,least-squares-reg.lisp new file mode 100644 index 0000000..1c7272c --- /dev/null +++ b/src/vector,least-squares-reg.lisp @@ -0,0 +1,2 @@ +(in-package :lizfcm.vector) + diff --git a/src/vector,least-squares.lisp b/src/vector,least-squares.lisp new file mode 100644 index 0000000..687af32 --- /dev/null +++ b/src/vector,least-squares.lisp @@ -0,0 +1,14 @@ +(in-package :lizfcm.vector) + +(defun least-squares-reg (x y) + (let* ((n (length x)) + (sum-y (reduce #'+ y)) + (sum-x (reduce #'+ x)) + (sum-xy (reduce #'+ (mapcar #'* x y))) + (sum-xsquared (reduce #'+ (mapcar #'* x x))) + (b (/ (- (* sum-y sum-xsquared) (* sum-x sum-xy)) + (- (* n sum-xsquared) (* sum-x sum-x)))) + (a (/ (- (* n sum-xy) (* sum-x sum-y)) + (- (* n sum-xsquared) (* sum-x sum-x))))) + (list a b))) + diff --git a/src/vector,package.lisp b/src/vector,package.lisp index 009b190..f491908 100644 --- a/src/vector,package.lisp +++ b/src/vector,package.lisp @@ -4,4 +4,5 @@ (:export :p-norm :max-norm - :distance)) + :distance + :least-squares-reg))