From 2e284b71500a1f8dc6cc46ecf21eb1e9389ea780 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 13 Sep 2023 09:54:12 -0600 Subject: [PATCH] new table macro, notes for 9/11 --- cl/lizfcm.asd | 4 +- cl/src/approx/derivative.lisp | 3 +- cl/src/utils/package.lisp | 3 +- cl/src/utils/table.lisp | 11 +++++ cl/tests/approx.lisp | 3 +- cl/tests/table.lisp | 31 ++++++++++++ notes/Sep-11.org | 93 +++++++++++++++++++++++++++++++++++ 7 files changed, 144 insertions(+), 4 deletions(-) create mode 100644 cl/src/utils/table.lisp create mode 100644 cl/tests/table.lisp create mode 100644 notes/Sep-11.org diff --git a/cl/lizfcm.asd b/cl/lizfcm.asd index 9e91eb0..ad69fa1 100644 --- a/cl/lizfcm.asd +++ b/cl/lizfcm.asd @@ -7,6 +7,7 @@ ((:module "utils" :components ((:file "within-range" :depends-on ("package")) + (:file "table" :depends-on ("package")) (:file "package"))) (:module "approx" :components @@ -21,7 +22,8 @@ :lizfcm) :components ((:module "tests" :components - ((:file "approx" :depends-on ("suite")) + ((:file "table" :depends-on ("suite")) + (:file "approx" :depends-on ("suite")) (:file "suite")))) :perform (asdf:test-op (o c) (uiop:symbol-call :fiveam :run! diff --git a/cl/src/approx/derivative.lisp b/cl/src/approx/derivative.lisp index 280463f..02fcb4c 100644 --- a/cl/src/approx/derivative.lisp +++ b/cl/src/approx/derivative.lisp @@ -5,4 +5,5 @@ (x1 (- x delta)) (y2 (apply f (list x2))) (y1 (apply f (list x1)))) - (/ (- y2 y1) (- x2 x1)))) + (/ (- y2 y1) + (- x2 x1)))) diff --git a/cl/src/utils/package.lisp b/cl/src/utils/package.lisp index 0436a94..bdd5589 100644 --- a/cl/src/utils/package.lisp +++ b/cl/src/utils/package.lisp @@ -1,4 +1,5 @@ (in-package :cl-user) (defpackage lizfcm.utils (:use :cl) - (:export :within-range-p)) + (:export :within-range-p + :table)) diff --git a/cl/src/utils/table.lisp b/cl/src/utils/table.lisp new file mode 100644 index 0000000..e96f37b --- /dev/null +++ b/cl/src/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/cl/tests/approx.lisp b/cl/tests/approx.lisp index a458e66..2fd8124 100644 --- a/cl/tests/approx.lisp +++ b/cl/tests/approx.lisp @@ -15,9 +15,10 @@ :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 (derivative-at f x delta) f-prime-at-x - 0.1)))) + accepted-delta)))) diff --git a/cl/tests/table.lisp b/cl/tests/table.lisp new file mode 100644 index 0000000..33d4e86 --- /dev/null +++ b/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/notes/Sep-11.org b/notes/Sep-11.org new file mode 100644 index 0000000..1568618 --- /dev/null +++ b/notes/Sep-11.org @@ -0,0 +1,93 @@ +#+TITLE: Errors +#+AUTHOR: Elizabeth Hunt +#+STARTUP: entitiespretty fold inlineimages +#+LATEX_HEADER: \notindent \notag \usepackage{amsmath} \usepackage[a4paper,margin=1in,landscape]{geometry} +#+LATEX: \setlength\parindent{0pt} +#+OPTIONS: toc:nil + +* Errors +$x,y \in \mathds{R}$, using y as a way to approximate x. Then the +absolute error of in approximating x w/ y is $e_{abs}(x, y) = |x-y|$. + +and the relative error is $e_{rel}(x, y) = \frac{|x-y|}{|x|}$ + +Table of Errors + +#+BEGIN_SRC lisp :results table + (load "../cl/lizfcm.asd") + (ql:quickload 'lizfcm) + + (defun eabs (x y) (abs (- x y))) + (defun erel (x y) (/ (abs (- x y)) (abs x))) + + (defparameter *u-v* '( + (1 0.99) + (1 1.01) + (-1.5 -1.2) + (100 99.9) + (100 99) + )) + + (lizfcm.utils:table (:headers '("u" "v" "e_{abs}" "e_{rel}") + :domain-order (u v) + :domain-values *u-v*) + (fround (eabs u v) 4) + (fround (erel u v) 4)) +#+END_SRC + +#+RESULTS: +| u | v | e_{abs} | e_{rel} | +| 1 | 0.99 | 0.0 | 0.0 | +| 1 | 1.01 | 0.0 | 0.0 | +| -1.5 | -1.2 | 0.0 | 0.0 | +| 100 | 99.9 | 0.0 | 0.0 | +| 100 | 99 | 0.0 | 0.0 | + +Look at $u \approx 0$ then $v \approx 0$, $e_{abs}$ is better error since $e_{rel}$ is high. + +* Vector spaces & measures +Suppose we want solutions fo a linear system of the form $Ax = b$, and we want to approximate $x$, +we need to find a form of "distance" between vectors in $\mathds{R}^n$ + +** Vector Distances +A norm on a vector space $|| v ||$ is a function from $\mathds{R}^n$ such that: + +1. $||v|| \geq 0$ for all $v \in \mathds{R}^n$ and $||v|| = \Leftrightarrow v = 0$ +2. $||cv|| = |c| ||v||$ for all $c \in \mathds{R}, v \in \mathds{R}^n$ +3. $||x + y|| \leq ||x|| + ||y|| \forall x,y \in \mathds{R}^n$ + +*** Example norms: +$||v||_2 = || [v_1, v_2, \dots v_n] || = (v_1^2 + v_2^2 + \dots + v_n^2)^{}^{\frac{1}{2}}$ + +$||v||_1 = |v_1| + |v_2| + \dots + |v_n|$ + +$||v||_{\infty} = \text{max}(|v_i|)$ (most restriction) + +p-norm: +$||v||_p = \sum_{i=1}^{h} (|v_i|^p)^{\frac{1}{p}}$ + +** Length +The length of a vector in a given norm is $||v|| \forall v \in \mathds{R}^n$ + +All norms on finite dimensional vectors are equivalent. Then exist constants +$\alpha, \beta > 0 \ni \alpha ||v||_p \leq ||v||_q \leq \beta||v||_p$ + +** Distance +Let $u,v$ be vectors in $\mathds{R}^n$ then the distance is $||u - v||$ by some norm: +$e_{abs} = d(v, u) = ||u - v||$ + +The relative errors is: + +$e_{rel} = \frac{||u - v||}{||v||}$ + + +** Approxmiating Solutions to $Ax = b$ +We define the residual vector $r(x) = b - Ax$ + +If $x$ is the exact solution, then $r(x) = 0$. + +Then we can measure the "correctness" of the approximated solution on the norm of the +residual. We want to minimize the norm. + +But, $r(y) = b - Ay \approx 0 \nRightarrow y \equiv x$, if $A$ is not invertible. +