problem one hw 4
This commit is contained in:
parent
e46e5eee74
commit
834bb45be7
0
compile.sh → compile_archive.sh
Normal file → Executable file
0
compile.sh → compile_archive.sh
Normal file → Executable file
@ -1,3 +0,0 @@
|
||||
img {
|
||||
max-width: 100%;
|
||||
}
|
@ -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")))
|
||||
|
60
src/main.lisp
Normal file
60
src/main.lisp
Normal file
@ -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)))
|
||||
|
@ -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
|
||||
|
2
src/vector,least-squares-reg.lisp
Normal file
2
src/vector,least-squares-reg.lisp
Normal file
@ -0,0 +1,2 @@
|
||||
(in-package :lizfcm.vector)
|
||||
|
14
src/vector,least-squares.lisp
Normal file
14
src/vector,least-squares.lisp
Normal file
@ -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)))
|
||||
|
@ -4,4 +4,5 @@
|
||||
(:export
|
||||
:p-norm
|
||||
:max-norm
|
||||
:distance))
|
||||
:distance
|
||||
:least-squares-reg))
|
||||
|
Loading…
Reference in New Issue
Block a user