!<arch>
#1/20           1696970693  501   20    100644  28        `
__.SYMDEF SORTED            #1/28           1696908991  501   20    100644  692       `
approx,derivative.lisp      (in-package :lizfcm.approx)

(defun central-derivative-at (f x &optional (delta 0.01))
  (let* ((x2 (+ x delta))
         (x1 (- x delta))
         (y2 (apply f (list x2)))
         (y1 (apply f (list x1))))
    (/ (- y2 y1)
       (- x2 x1))))

(defun forward-derivative-at (f x &optional (delta 0.01))
  (let* ((x2 (+ x delta))
         (x1 x)
         (y2 (apply f (list x2)))
         (y1 (apply f (list x1))))
    (/ (- y2 y1)
       (- x2 x1))))

(defun backward-derivative-at (f x &optional (delta 0.01))
  (let* ((x2 x)
         (x1 (- x delta))
         (y2 (apply f (list x2)))
         (y1 (apply f (list x1))))
    (/ (- y2 y1)
       (- x2 x1))))




#1/28           1696908123  501   20    100644  324       `
approx,maceps.lisp          (in-package :lizfcm.approx)

(defun compute-maceps (f a init)
  (let ((h init)
        (err init))
    (loop collect (list a h err)
          do
          (setf h (/ h 2)
                err (abs (- (funcall f (+ a h))
                            (funcall f a))))
          while (> err 0))))



#1/28           1696908999  501   20    100644  228       `
approx,package.lisp         (in-package :cl-user)
(defpackage lizfcm.approx
  (:use :cl)
  (:export :central-derivative-at
           :forward-derivative-at
           :backward-derivative-at
           :compute-maceps))







#1/20           1696969272  501   20    100644  1556      `
lizfcm.asd          (asdf:defsystem "lizfcm"
                :version "0.1.0"
                :author "Elizabeth Hunt"
                :license "MIT"
                :components
                ((:file "utils,within-range" :depends-on ("utils,package"))
                 (:file "utils,table" :depends-on ("utils,package"))
                 (:file "utils,package")
                 (: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")))


(asdf:defsystem "lizfcm/tests"
                :author "Elizabeth Hunt"
                :license "MIT"
                :depends-on
                (:fiveam
                  :lizfcm)
                :components
                ((:file "tests,table" :depends-on ("tests,suite"))
                 (:file "tests,maceps" :depends-on ("tests,suite"))
                 (:file "tests,approx" :depends-on ("tests,suite"))
                 (:file "tests,vector" :depends-on ("tests,suite"))
                 (:file "tests,suite"))
                :perform
                (asdf:test-op (o c) (uiop:symbol-call
                                      :fiveam :run!
                                      (uiop:find-symbol* :lizfcm-test-suite :lizfcm/tests))))



#1/28           1696908986  501   20    100644  1396      `
tests,approx.lisp           (defpackage lizfcm/tests.approx
  (:use :cl
        :fiveam
        :lizfcm.approx
        :lizfcm.utils
        :lizfcm/tests)
  (:export :approx-suite))
(in-package :lizfcm/tests.approx)

(def-suite approx-suite
           :in lizfcm-test-suite)
(in-suite approx-suite)

(test central-derivative-at
      :description "derivative at is within bounds"
      (let ((f (lambda (x) (* x x)))
            (x 2)
            (accepted-delta 0.02)
            (f-prime-at-x 4)
            (delta 0.01))
        (is (within-range-p
              (central-derivative-at f x delta)
              f-prime-at-x
              accepted-delta))))

(test fwd-derivative-at
      :description "forward derivative at is within bounds"
      (let ((f (lambda (x) (* x x)))
            (x 2)
            (accepted-delta 0.02)
            (f-prime-at-x 4)
            (delta 0.01))
        (is (within-range-p
              (forward-derivative-at f x delta)
              f-prime-at-x
              accepted-delta))))

(test bwd-derivative-at
      :description "backward derivative at is within bounds"
      (let ((f (lambda (x) (* x x)))
            (x 2)
            (accepted-delta 0.02)
            (f-prime-at-x 4)
            (delta 0.01))
        (is (within-range-p
              (backward-derivative-at f x delta)
              f-prime-at-x
              accepted-delta))))



#1/28           1696908124  501   20    100644  1092      `
tests,maceps.lisp           (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 (lambda (x) x)
                                                        1.0d0
                                                        1.0d0))
             (maceps-computation-single (compute-maceps (lambda (x) x)
                                                        1.0
                                                        1.0))
             (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))))






#1/20           1696908124  501   20    100644  260       `
tests,suite.lisp    (in-package :cl-user)
(defpackage lizfcm/tests
  (:use :cl
        :fiveam)
  (:export :run!
           :lizfcm-test-suite))
(in-package :lizfcm/tests)

(def-suite lizfcm-test-suite
    :description "The ultimate parent test suite")







#1/20           1696908124  501   20    100644  996       `
tests,table.lisp    (defpackage lizfcm/tests.table
  (:use :cl
        :fiveam
        :lizfcm.utils
        :lizfcm/tests)
  (:export :approx-suite))
(in-package :lizfcm/tests.table)

(def-suite table-suite
           :in lizfcm-test-suite)
(in-suite table-suite)

(defun fib (n)
  (cond ((< n 2) n)
        (t (+ (fib (- n 1)) (fib (- n 2))))))

(test table-of-fib-vals
      :description "table generates correctly"
      (let* ((headers '("n" "fib(n)"))
             (n-values '((1) (2) (3) (4)))
             (expected `(("n" "fib(n)")
                         (1 ,(fib 1))
                         (2 ,(fib 2))
                         (3 ,(fib 3))
                         (4 ,(fib 4))))
             (tabled (lizfcm.utils:table (:headers headers
                                                   :domain-order (n)
                                                   :domain-values n-values)
                                         (fib n))))
        (is (equal expected tabled))))






#1/28           1696970521  501   20    100644  1356      `
tests,vector.lisp           (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))))

(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



#1/28           1696908123  501   20    100644  140       `
utils,package.lisp          (in-package :cl-user)
(defpackage lizfcm.utils
  (:use :cl)
  (:export :within-range-p
           :table))





#1/20           1696908123  501   20    100644  364       `
utils,table.lisp    (in-package :lizfcm.utils)

(defmacro table ((&key headers domain-order domain-values) &body body)
  `(cons
     ,headers
     (mapcar (lambda (tuple)
               (destructuring-bind ,domain-order tuple
                 (append tuple
                         (list
                           ,@body))))
             ,domain-values)))







#1/28           1696908124  501   20    100644  172       `
utils,within-range.lisp     (in-package :lizfcm.utils)

(defun within-range-p (x true-value delta)
  (and (< x (+ true-value delta))
       (> x (- true-value delta))))



#1/28           1696908124  501   20    100644  172       `
vector,distance.lisp        (in-package :lizfcm.vector)

(defun distance (v1 v2 norm)
  (let* ((d (mapcar #'- v1 v2))
         (length (funcall norm d)))
    length))





#1/20           1696908124  501   20    100644  300       `
vector,norm.lisp    (in-package :lizfcm.vector)

(defun p-norm (p)
  (lambda (v)
    (expt
      (reduce #'+
              (mapcar (lambda (x)
                        (abs
                          (expt x p)))
                      v))
      (/ 1 p))))

(defun max-norm (v)
  (reduce #'max v))





#1/28           1696962268  501   20    100644  172       `
vector,package.lisp         (in-package :cl-user)
(defpackage lizfcm.vector
  (:use :cl)
  (:export
    :p-norm
    :max-norm
    :distance
    :least-squares-reg))







#1/20           1696970639  501   20    100644  1732      `
main.lisp           (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)))




#1/36           1696962277  501   20    100644  68        `
vector,least-squares-reg.lisp       (in-package :lizfcm.vector)




#1/36           1696969388  501   20    100644  524       `
vector,least-squares.lisp           (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)))

