summaryrefslogtreecommitdiff
path: root/deprecated-cl
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-11 10:04:04 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-11 10:04:04 -0600
commit43f06890e2689af2ef54c4480fe5790692a24f65 (patch)
treeb933f3e05aad81d780c0c94646676efa1bbad22d /deprecated-cl
parenta74a732b27fb610133190e89a91b2d42d0cf78b3 (diff)
downloadcmath-43f06890e2689af2ef54c4480fe5790692a24f65.tar.gz
cmath-43f06890e2689af2ef54c4480fe5790692a24f65.zip
deprecate common lisp solutions and write c; it's too much effort to keep up with the requirements for an archive.
Diffstat (limited to 'deprecated-cl')
-rw-r--r--deprecated-cl/.main.lisp.swpbin0 -> 12288 bytes
-rw-r--r--deprecated-cl/approx,derivative.lisp25
-rw-r--r--deprecated-cl/approx,maceps.lisp12
-rw-r--r--deprecated-cl/approx,package.lisp7
-rw-r--r--deprecated-cl/lizfcm.asd33
-rw-r--r--deprecated-cl/main.lisp60
-rw-r--r--deprecated-cl/tests,approx.lisp48
-rw-r--r--deprecated-cl/tests,maceps.lisp27
-rw-r--r--deprecated-cl/tests,suite.lisp10
-rw-r--r--deprecated-cl/tests,table.lisp31
-rw-r--r--deprecated-cl/tests,vector.lisp42
-rw-r--r--deprecated-cl/utils,package.lisp5
-rw-r--r--deprecated-cl/utils,table.lisp11
-rw-r--r--deprecated-cl/utils,within-range.lisp5
-rw-r--r--deprecated-cl/vector,distance.lisp6
-rw-r--r--deprecated-cl/vector,least-squares.lisp14
-rw-r--r--deprecated-cl/vector,norm.lisp14
-rw-r--r--deprecated-cl/vector,package.lisp8
18 files changed, 358 insertions, 0 deletions
diff --git a/deprecated-cl/.main.lisp.swp b/deprecated-cl/.main.lisp.swp
new file mode 100644
index 0000000..e0a098e
--- /dev/null
+++ b/deprecated-cl/.main.lisp.swp
Binary files differ
diff --git a/deprecated-cl/approx,derivative.lisp b/deprecated-cl/approx,derivative.lisp
new file mode 100644
index 0000000..631a5c0
--- /dev/null
+++ b/deprecated-cl/approx,derivative.lisp
@@ -0,0 +1,25 @@
+(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))))
diff --git a/deprecated-cl/approx,maceps.lisp b/deprecated-cl/approx,maceps.lisp
new file mode 100644
index 0000000..e2738e4
--- /dev/null
+++ b/deprecated-cl/approx,maceps.lisp
@@ -0,0 +1,12 @@
+(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))))
+
diff --git a/deprecated-cl/approx,package.lisp b/deprecated-cl/approx,package.lisp
new file mode 100644
index 0000000..a0eac80
--- /dev/null
+++ b/deprecated-cl/approx,package.lisp
@@ -0,0 +1,7 @@
+(in-package :cl-user)
+(defpackage lizfcm.approx
+ (:use :cl)
+ (:export :central-derivative-at
+ :forward-derivative-at
+ :backward-derivative-at
+ :compute-maceps))
diff --git a/deprecated-cl/lizfcm.asd b/deprecated-cl/lizfcm.asd
new file mode 100644
index 0000000..dea3ddd
--- /dev/null
+++ b/deprecated-cl/lizfcm.asd
@@ -0,0 +1,33 @@
+(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" :depends-on ("vector,package"))
+ (: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))))
diff --git a/deprecated-cl/main.lisp b/deprecated-cl/main.lisp
new file mode 100644
index 0000000..7a8b455
--- /dev/null
+++ b/deprecated-cl/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/deprecated-cl/tests,approx.lisp b/deprecated-cl/tests,approx.lisp
new file mode 100644
index 0000000..678ff8c
--- /dev/null
+++ b/deprecated-cl/tests,approx.lisp
@@ -0,0 +1,48 @@
+(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))))
diff --git a/deprecated-cl/tests,maceps.lisp b/deprecated-cl/tests,maceps.lisp
new file mode 100644
index 0000000..cd5ced9
--- /dev/null
+++ b/deprecated-cl/tests,maceps.lisp
@@ -0,0 +1,27 @@
+(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))))
diff --git a/deprecated-cl/tests,suite.lisp b/deprecated-cl/tests,suite.lisp
new file mode 100644
index 0000000..e23cfaf
--- /dev/null
+++ b/deprecated-cl/tests,suite.lisp
@@ -0,0 +1,10 @@
+(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")
diff --git a/deprecated-cl/tests,table.lisp b/deprecated-cl/tests,table.lisp
new file mode 100644
index 0000000..33d4e86
--- /dev/null
+++ b/deprecated-cl/tests,table.lisp
@@ -0,0 +1,31 @@
+(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))))
+
diff --git a/deprecated-cl/tests,vector.lisp b/deprecated-cl/tests,vector.lisp
new file mode 100644
index 0000000..6edb1ac
--- /dev/null
+++ b/deprecated-cl/tests,vector.lisp
@@ -0,0 +1,42 @@
+(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
diff --git a/deprecated-cl/utils,package.lisp b/deprecated-cl/utils,package.lisp
new file mode 100644
index 0000000..bdd5589
--- /dev/null
+++ b/deprecated-cl/utils,package.lisp
@@ -0,0 +1,5 @@
+(in-package :cl-user)
+(defpackage lizfcm.utils
+ (:use :cl)
+ (:export :within-range-p
+ :table))
diff --git a/deprecated-cl/utils,table.lisp b/deprecated-cl/utils,table.lisp
new file mode 100644
index 0000000..e96f37b
--- /dev/null
+++ b/deprecated-cl/utils,table.lisp
@@ -0,0 +1,11 @@
+(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)))
diff --git a/deprecated-cl/utils,within-range.lisp b/deprecated-cl/utils,within-range.lisp
new file mode 100644
index 0000000..9a0b762
--- /dev/null
+++ b/deprecated-cl/utils,within-range.lisp
@@ -0,0 +1,5 @@
+(in-package :lizfcm.utils)
+
+(defun within-range-p (x true-value delta)
+ (and (< x (+ true-value delta))
+ (> x (- true-value delta))))
diff --git a/deprecated-cl/vector,distance.lisp b/deprecated-cl/vector,distance.lisp
new file mode 100644
index 0000000..74631ce
--- /dev/null
+++ b/deprecated-cl/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/deprecated-cl/vector,least-squares.lisp b/deprecated-cl/vector,least-squares.lisp
new file mode 100644
index 0000000..687af32
--- /dev/null
+++ b/deprecated-cl/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/deprecated-cl/vector,norm.lisp b/deprecated-cl/vector,norm.lisp
new file mode 100644
index 0000000..aa51bce
--- /dev/null
+++ b/deprecated-cl/vector,norm.lisp
@@ -0,0 +1,14 @@
+(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))
diff --git a/deprecated-cl/vector,package.lisp b/deprecated-cl/vector,package.lisp
new file mode 100644
index 0000000..f491908
--- /dev/null
+++ b/deprecated-cl/vector,package.lisp
@@ -0,0 +1,8 @@
+(in-package :cl-user)
+(defpackage lizfcm.vector
+ (:use :cl)
+ (:export
+ :p-norm
+ :max-norm
+ :distance
+ :least-squares-reg))