new table macro, notes for 9/11

This commit is contained in:
Elizabeth Hunt 2023-09-13 09:54:12 -06:00
parent 2cb14ebf79
commit 2e284b7150
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
7 changed files with 144 additions and 4 deletions

View File

@ -7,6 +7,7 @@
((:module "utils" ((:module "utils"
:components :components
((:file "within-range" :depends-on ("package")) ((:file "within-range" :depends-on ("package"))
(:file "table" :depends-on ("package"))
(:file "package"))) (:file "package")))
(:module "approx" (:module "approx"
:components :components
@ -21,7 +22,8 @@
:lizfcm) :lizfcm)
:components ((:module "tests" :components ((:module "tests"
:components :components
((:file "approx" :depends-on ("suite")) ((:file "table" :depends-on ("suite"))
(:file "approx" :depends-on ("suite"))
(:file "suite")))) (:file "suite"))))
:perform (asdf:test-op (o c) (uiop:symbol-call :perform (asdf:test-op (o c) (uiop:symbol-call
:fiveam :run! :fiveam :run!

View File

@ -5,4 +5,5 @@
(x1 (- x delta)) (x1 (- x delta))
(y2 (apply f (list x2))) (y2 (apply f (list x2)))
(y1 (apply f (list x1)))) (y1 (apply f (list x1))))
(/ (- y2 y1) (- x2 x1)))) (/ (- y2 y1)
(- x2 x1))))

View File

@ -1,4 +1,5 @@
(in-package :cl-user) (in-package :cl-user)
(defpackage lizfcm.utils (defpackage lizfcm.utils
(:use :cl) (:use :cl)
(:export :within-range-p)) (:export :within-range-p
:table))

11
cl/src/utils/table.lisp Normal file
View File

@ -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)))

View File

@ -15,9 +15,10 @@
:description "derivative at is within bounds" :description "derivative at is within bounds"
(let ((f (lambda (x) (* x x))) (let ((f (lambda (x) (* x x)))
(x 2) (x 2)
(accepted-delta 0.02)
(f-prime-at-x 4) (f-prime-at-x 4)
(delta 0.01)) (delta 0.01))
(is (within-range-p (is (within-range-p
(derivative-at f x delta) (derivative-at f x delta)
f-prime-at-x f-prime-at-x
0.1)))) accepted-delta))))

31
cl/tests/table.lisp Normal file
View File

@ -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))))

93
notes/Sep-11.org Normal file
View File

@ -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.