From 43f06890e2689af2ef54c4480fe5790692a24f65 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 11 Oct 2023 10:04:04 -0600 Subject: deprecate common lisp solutions and write c; it's too much effort to keep up with the requirements for an archive. --- .gitignore | 2 + Makefile | 41 +++++++++++++++ compile_archive.sh | 3 -- deprecated-cl/.main.lisp.swp | Bin 0 -> 12288 bytes deprecated-cl/approx,derivative.lisp | 25 +++++++++ deprecated-cl/approx,maceps.lisp | 12 +++++ deprecated-cl/approx,package.lisp | 7 +++ deprecated-cl/lizfcm.asd | 33 ++++++++++++ deprecated-cl/main.lisp | 60 +++++++++++++++++++++ deprecated-cl/tests,approx.lisp | 48 +++++++++++++++++ deprecated-cl/tests,maceps.lisp | 27 ++++++++++ deprecated-cl/tests,suite.lisp | 10 ++++ deprecated-cl/tests,table.lisp | 31 +++++++++++ deprecated-cl/tests,vector.lisp | 42 +++++++++++++++ deprecated-cl/utils,package.lisp | 5 ++ deprecated-cl/utils,table.lisp | 11 ++++ deprecated-cl/utils,within-range.lisp | 5 ++ deprecated-cl/vector,distance.lisp | 6 +++ deprecated-cl/vector,least-squares.lisp | 14 +++++ deprecated-cl/vector,norm.lisp | 14 +++++ deprecated-cl/vector,package.lisp | 8 +++ inc/lizfcm.h | 30 +++++++++++ inc/macros.h | 41 +++++++++++++++ inc/types.h | 18 +++++++ lib/lizfcm.a | Bin 0 -> 9672 bytes lizfcm.a | Bin 12720 -> 0 bytes notes/Oct-11.org | 15 ++++++ src/approx,derivative.lisp | 25 --------- src/approx,maceps.lisp | 12 ----- src/approx,package.lisp | 7 --- src/approx_derivative.c | 38 ++++++++++++++ src/lin.c | 19 +++++++ src/lizfcm.asd | 33 ------------ src/maceps.c | 28 ++++++++++ src/main.c | 7 +++ src/main.lisp | 60 --------------------- src/tests,approx.lisp | 48 ----------------- src/tests,maceps.lisp | 27 ---------- src/tests,suite.lisp | 10 ---- src/tests,table.lisp | 31 ----------- src/tests,vector.lisp | 42 --------------- src/utils,package.lisp | 5 -- src/utils,table.lisp | 11 ---- src/utils,within-range.lisp | 5 -- src/vector,distance.lisp | 6 --- src/vector,least-squares-reg.lisp | 2 - src/vector,least-squares.lisp | 14 ----- src/vector,norm.lisp | 14 ----- src/vector,package.lisp | 8 --- src/vector.c | 90 ++++++++++++++++++++++++++++++++ test/main.c | 54 +++++++++++++++++++ 51 files changed, 741 insertions(+), 363 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile delete mode 100755 compile_archive.sh create mode 100644 deprecated-cl/.main.lisp.swp create mode 100644 deprecated-cl/approx,derivative.lisp create mode 100644 deprecated-cl/approx,maceps.lisp create mode 100644 deprecated-cl/approx,package.lisp create mode 100644 deprecated-cl/lizfcm.asd create mode 100644 deprecated-cl/main.lisp create mode 100644 deprecated-cl/tests,approx.lisp create mode 100644 deprecated-cl/tests,maceps.lisp create mode 100644 deprecated-cl/tests,suite.lisp create mode 100644 deprecated-cl/tests,table.lisp create mode 100644 deprecated-cl/tests,vector.lisp create mode 100644 deprecated-cl/utils,package.lisp create mode 100644 deprecated-cl/utils,table.lisp create mode 100644 deprecated-cl/utils,within-range.lisp create mode 100644 deprecated-cl/vector,distance.lisp create mode 100644 deprecated-cl/vector,least-squares.lisp create mode 100644 deprecated-cl/vector,norm.lisp create mode 100644 deprecated-cl/vector,package.lisp create mode 100644 inc/lizfcm.h create mode 100644 inc/macros.h create mode 100644 inc/types.h create mode 100644 lib/lizfcm.a delete mode 100644 lizfcm.a create mode 100644 notes/Oct-11.org delete mode 100644 src/approx,derivative.lisp delete mode 100644 src/approx,maceps.lisp delete mode 100644 src/approx,package.lisp create mode 100644 src/approx_derivative.c create mode 100644 src/lin.c delete mode 100644 src/lizfcm.asd create mode 100644 src/maceps.c create mode 100644 src/main.c delete mode 100644 src/main.lisp delete mode 100644 src/tests,approx.lisp delete mode 100644 src/tests,maceps.lisp delete mode 100644 src/tests,suite.lisp delete mode 100644 src/tests,table.lisp delete mode 100644 src/tests,vector.lisp delete mode 100644 src/utils,package.lisp delete mode 100644 src/utils,table.lisp delete mode 100644 src/utils,within-range.lisp delete mode 100644 src/vector,distance.lisp delete mode 100644 src/vector,least-squares-reg.lisp delete mode 100644 src/vector,least-squares.lisp delete mode 100644 src/vector,norm.lisp delete mode 100644 src/vector,package.lisp create mode 100644 src/vector.c create mode 100644 test/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d0b71a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +dist diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a76a59f --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +TEST_SRC := test/main.c +SRC_DIR := src +OBJ_DIR := build +BIN_DIR := dist +LIB_DIR := lib + +TEST_EXE := $(BIN_DIR)/lizfcm.test +EXE := $(BIN_DIR)/lizfcm +LIBRARY := $(LIB_DIR)/lizfcm.a +SRC := $(wildcard $(SRC_DIR)/*.c) +OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + +CPPFLAGS := -Iinc -MMD -MP +CFLAGS := -Wall +LDFLAGS := +LDLIBS := -lm + +.PHONY: all clean + +all: $(TEST_EXE) + +$(TEST_EXE): $(LIBRARY) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LIBRARY) $(TEST_SRC) -o $@ + +$(LIBRARY): $(EXE) + ar rcs $(LIBRARY) $(OBJ_DIR)/*.o + ranlib $(LIBRARY) + +$(EXE): $(OBJ) | $(BIN_DIR) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +$(BIN_DIR) $(OBJ_DIR) $(LIB_DIR): + mkdir -p $@ + +clean: + @$(RM) -r $(BIN_DIR) $(OBJ_DIR) + +-include $(OBJ:.o=.d) diff --git a/compile_archive.sh b/compile_archive.sh deleted file mode 100755 index f174fbe..0000000 --- a/compile_archive.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -ar rcs lizfcm.a src/* diff --git a/deprecated-cl/.main.lisp.swp b/deprecated-cl/.main.lisp.swp new file mode 100644 index 0000000..e0a098e Binary files /dev/null and b/deprecated-cl/.main.lisp.swp 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)) diff --git a/inc/lizfcm.h b/inc/lizfcm.h new file mode 100644 index 0000000..b390394 --- /dev/null +++ b/inc/lizfcm.h @@ -0,0 +1,30 @@ +#include "macros.h" +#include "types.h" + +#ifndef LIZFCM_H +#define LIZFCM_H + +extern float smaceps(); +extern double dmaceps(); + +extern double central_derivative_at(double (*f)(double), double a, double h); +extern double forward_derivative_at(double (*f)(double), double a, double h); +extern double backward_derivative_at(double (*f)(double), double a, double h); + +extern double sum_v(Array_double *v); +extern Array_double *add_v(Array_double *v1, Array_double *v2); +extern Array_double *minus_v(Array_double *v1, Array_double *v2); +extern double dot_v(Array_double *v1, Array_double *v2); +extern double l2_norm(Array_double *v); +extern double l1_norm(Array_double *v); +extern double linf_norm(Array_double *v); + +extern double l2_distance(Array_double *v1, Array_double *v2); +extern double l1_distance(Array_double *v1, Array_double *v2); +extern double linf_distance(Array_double *v1, Array_double *v2); + +extern void format_vector_into(Array_double *v, char *s); + +extern Line *least_squares_lin_reg(Array_double *x, Array_double *y); + +#endif // LIZFCM_H diff --git a/inc/macros.h b/inc/macros.h new file mode 100644 index 0000000..841084d --- /dev/null +++ b/inc/macros.h @@ -0,0 +1,41 @@ +#include +#include +#include + +#ifndef MACROS_H +#define MACROS_H + +#define DEFINE_ARRAY(TYPE) \ + typedef struct { \ + TYPE *data; \ + size_t size; \ + } Array_##TYPE + +#define InitArray(TYPE, ...) \ + ({ \ + TYPE temp[] = __VA_ARGS__; \ + Array_##TYPE *arr = malloc(sizeof(Array_##TYPE)); \ + arr->size = sizeof(temp) / sizeof(temp[0]); \ + arr->data = malloc(arr->size * sizeof(TYPE)); \ + if (arr->data) { \ + memcpy(arr->data, temp, arr->size * sizeof(TYPE)); \ + } \ + arr; \ + }) + +#define InitArrayWithSize(TYPE, SIZE, INIT_VALUE) \ + ({ \ + Array_##TYPE *arr = malloc(sizeof(Array_##TYPE)); \ + arr->size = SIZE; \ + arr->data = malloc(arr->size * sizeof(TYPE)); \ + if (arr->data) { \ + for (size_t i = 0; i < arr->size; i++) \ + arr->data[i] = INIT_VALUE; \ + } \ + arr; \ + }) + +#define c_max(x, y) (((x) >= (y)) ? (x) : (y)) +#define c_min(x, y) (((x) <= (y)) ? (x) : (y)) + +#endif // MACROS_H diff --git a/inc/types.h b/inc/types.h new file mode 100644 index 0000000..4af17db --- /dev/null +++ b/inc/types.h @@ -0,0 +1,18 @@ +#include "macros.h" +#include + +#ifndef TYPES_H +#define TYPES_H + +DEFINE_ARRAY(int); +DEFINE_ARRAY(uint32_t); +DEFINE_ARRAY(int32_t); +DEFINE_ARRAY(float); +DEFINE_ARRAY(double); + +typedef struct Line { + double m; + double a; +} Line; + +#endif // TYPES_H diff --git a/lib/lizfcm.a b/lib/lizfcm.a new file mode 100644 index 0000000..fe9a6ca Binary files /dev/null and b/lib/lizfcm.a differ diff --git a/lizfcm.a b/lizfcm.a deleted file mode 100644 index cb34117..0000000 Binary files a/lizfcm.a and /dev/null differ diff --git a/notes/Oct-11.org b/notes/Oct-11.org new file mode 100644 index 0000000..575ea74 --- /dev/null +++ b/notes/Oct-11.org @@ -0,0 +1,15 @@ +* Diagonal Dominance +Suppose that A \in R^{n \times n} is diagonally dominant then Gaussian eliminiation of A produces no zero pivot +elements. + +Def. A \in R^{n \times n} is diagonally dominant if for each i=1,2,...n |a_{i,i}| \geq \Sigma_{j=1}^n |a_i,j| + + +* To test solution code: + [[1] + [1] +Set y = [\cdots] \in R^n + [1]] + +Compute b=Ay +Solve Ax=b diff --git a/src/approx,derivative.lisp b/src/approx,derivative.lisp deleted file mode 100644 index 631a5c0..0000000 --- a/src/approx,derivative.lisp +++ /dev/null @@ -1,25 +0,0 @@ -(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/src/approx,maceps.lisp b/src/approx,maceps.lisp deleted file mode 100644 index e2738e4..0000000 --- a/src/approx,maceps.lisp +++ /dev/null @@ -1,12 +0,0 @@ -(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/src/approx,package.lisp b/src/approx,package.lisp deleted file mode 100644 index a0eac80..0000000 --- a/src/approx,package.lisp +++ /dev/null @@ -1,7 +0,0 @@ -(in-package :cl-user) -(defpackage lizfcm.approx - (:use :cl) - (:export :central-derivative-at - :forward-derivative-at - :backward-derivative-at - :compute-maceps)) diff --git a/src/approx_derivative.c b/src/approx_derivative.c new file mode 100644 index 0000000..b33a208 --- /dev/null +++ b/src/approx_derivative.c @@ -0,0 +1,38 @@ +#include "lizfcm.h" +#include + +double central_derivative_at(double (*f)(double), double a, double h) { + assert(h > 0); + + double x2 = a + h; + double x1 = a - h; + + double y2 = (*f)(x2); + double y1 = (*f)(x1); + + return (y2 - y1) / (x2 - x1); +} + +double forward_derivative_at(double (*f)(double), double a, double h) { + assert(h > 0); + + double x2 = a + h; + double x1 = a; + + double y2 = (*f)(x2); + double y1 = (*f)(x1); + + return (y2 - y1) / (x2 - x1); +} + +double backward_derivative_at(double (*f)(double), double a, double h) { + assert(h > 0); + + double x2 = a; + double x1 = a - h; + + double y2 = (*f)(x2); + double y1 = (*f)(x1); + + return (y2 - y1) / (x2 - x1); +} diff --git a/src/lin.c b/src/lin.c new file mode 100644 index 0000000..2df6f28 --- /dev/null +++ b/src/lin.c @@ -0,0 +1,19 @@ +#include "lizfcm.h" +#include + +Line *least_squares_lin_reg(Array_double *x, Array_double *y) { + assert(x->size == y->size); + + uint64_t n = x->size; + double sum_x = sum_v(x); + double sum_y = sum_v(y); + double sum_xy = dot_v(x, y); + double sum_xx = dot_v(x, x); + double denom = ((n * sum_xx) - (sum_x * sum_x)); + + Line *line = malloc(sizeof(Line)); + line->m = ((sum_xy * n) - (sum_x * sum_y)) / denom; + line->a = ((sum_y * sum_xx) - (sum_x * sum_xy)) / denom; + + return line; +} diff --git a/src/lizfcm.asd b/src/lizfcm.asd deleted file mode 100644 index 0096257..0000000 --- a/src/lizfcm.asd +++ /dev/null @@ -1,33 +0,0 @@ -(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)))) diff --git a/src/maceps.c b/src/maceps.c new file mode 100644 index 0000000..23bc9db --- /dev/null +++ b/src/maceps.c @@ -0,0 +1,28 @@ +#include "lizfcm.h" +#include + +float smaceps() { + float one = 1.0; + float machine_epsilon = 1.0; + float one_approx = one + machine_epsilon; + + while (fabsf(one_approx - one) > 0) { + machine_epsilon /= 2; + one_approx = one + machine_epsilon; + } + + return machine_epsilon; +} + +double dmaceps() { + double one = 1.0; + double machine_epsilon = 1.0; + double one_approx = one + machine_epsilon; + + while (fabs(one_approx - one) > 0) { + machine_epsilon /= 2; + one_approx = one + machine_epsilon; + } + + return machine_epsilon; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..6bb704a --- /dev/null +++ b/src/main.c @@ -0,0 +1,7 @@ +#include "lizfcm.h" +#include + +int main() { + printf("hello, from lizfcm!\n"); + return 0; +} diff --git a/src/main.lisp b/src/main.lisp deleted file mode 100644 index 7a8b455..0000000 --- a/src/main.lisp +++ /dev/null @@ -1,60 +0,0 @@ -(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/src/tests,approx.lisp b/src/tests,approx.lisp deleted file mode 100644 index 678ff8c..0000000 --- a/src/tests,approx.lisp +++ /dev/null @@ -1,48 +0,0 @@ -(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/src/tests,maceps.lisp b/src/tests,maceps.lisp deleted file mode 100644 index cd5ced9..0000000 --- a/src/tests,maceps.lisp +++ /dev/null @@ -1,27 +0,0 @@ -(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/src/tests,suite.lisp b/src/tests,suite.lisp deleted file mode 100644 index e23cfaf..0000000 --- a/src/tests,suite.lisp +++ /dev/null @@ -1,10 +0,0 @@ -(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/src/tests,table.lisp b/src/tests,table.lisp deleted file mode 100644 index 33d4e86..0000000 --- a/src/tests,table.lisp +++ /dev/null @@ -1,31 +0,0 @@ -(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/src/tests,vector.lisp b/src/tests,vector.lisp deleted file mode 100644 index 6edb1ac..0000000 --- a/src/tests,vector.lisp +++ /dev/null @@ -1,42 +0,0 @@ -(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/src/utils,package.lisp b/src/utils,package.lisp deleted file mode 100644 index bdd5589..0000000 --- a/src/utils,package.lisp +++ /dev/null @@ -1,5 +0,0 @@ -(in-package :cl-user) -(defpackage lizfcm.utils - (:use :cl) - (:export :within-range-p - :table)) diff --git a/src/utils,table.lisp b/src/utils,table.lisp deleted file mode 100644 index e96f37b..0000000 --- a/src/utils,table.lisp +++ /dev/null @@ -1,11 +0,0 @@ -(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/src/utils,within-range.lisp b/src/utils,within-range.lisp deleted file mode 100644 index 9a0b762..0000000 --- a/src/utils,within-range.lisp +++ /dev/null @@ -1,5 +0,0 @@ -(in-package :lizfcm.utils) - -(defun within-range-p (x true-value delta) - (and (< x (+ true-value delta)) - (> x (- true-value delta)))) diff --git a/src/vector,distance.lisp b/src/vector,distance.lisp deleted file mode 100644 index 74631ce..0000000 --- a/src/vector,distance.lisp +++ /dev/null @@ -1,6 +0,0 @@ -(in-package :lizfcm.vector) - -(defun distance (v1 v2 norm) - (let* ((d (mapcar #'- v1 v2)) - (length (funcall norm d))) - length)) diff --git a/src/vector,least-squares-reg.lisp b/src/vector,least-squares-reg.lisp deleted file mode 100644 index 1c7272c..0000000 --- a/src/vector,least-squares-reg.lisp +++ /dev/null @@ -1,2 +0,0 @@ -(in-package :lizfcm.vector) - diff --git a/src/vector,least-squares.lisp b/src/vector,least-squares.lisp deleted file mode 100644 index 687af32..0000000 --- a/src/vector,least-squares.lisp +++ /dev/null @@ -1,14 +0,0 @@ -(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/src/vector,norm.lisp b/src/vector,norm.lisp deleted file mode 100644 index aa51bce..0000000 --- a/src/vector,norm.lisp +++ /dev/null @@ -1,14 +0,0 @@ -(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/src/vector,package.lisp b/src/vector,package.lisp deleted file mode 100644 index f491908..0000000 --- a/src/vector,package.lisp +++ /dev/null @@ -1,8 +0,0 @@ -(in-package :cl-user) -(defpackage lizfcm.vector - (:use :cl) - (:export - :p-norm - :max-norm - :distance - :least-squares-reg)) diff --git a/src/vector.c b/src/vector.c new file mode 100644 index 0000000..61692e1 --- /dev/null +++ b/src/vector.c @@ -0,0 +1,90 @@ +#include "lizfcm.h" +#include +#include +#include +#include + +double l2_norm(Array_double *v) { + double norm = 0; + for (size_t i = 0; i < v->size; ++i) + norm += v->data[i] * v->data[i]; + return sqrt(norm); +} + +double l1_norm(Array_double *v) { + double sum = 0; + for (size_t i = 0; i < v->size; ++i) + sum += fabs(v->data[i]); + return sum; +} + +double linf_norm(Array_double *v) { + double max = -DBL_MAX; + for (size_t i = 0; i < v->size; ++i) + max = c_max(v->data[i], max); + return max; +} + +Array_double *minus_v(Array_double *v1, Array_double *v2) { + assert(v1->size == v2->size); + + Array_double *sub = InitArrayWithSize(double, v1->size, 0); + for (size_t i = 0; i < v1->size; i++) + sub->data[i] = v1->data[i] - v2->data[i]; + return sub; +} + +double sum_v(Array_double *v) { + double sum = 0; + for (size_t i = 0; i < v->size; i++) + sum += v->data[i]; + return sum; +} + +Array_double *add_v(Array_double *v1, Array_double *v2) { + assert(v1->size == v2->size); + + Array_double *sum = InitArrayWithSize(double, v1->size, 0); + for (size_t i = 0; i < v1->size; i++) + sum->data[i] = v1->data[i] + v2->data[i]; + return sum; +} + +double dot_v(Array_double *v1, Array_double *v2) { + assert(v1->size == v2->size); + + double dot = 0; + for (size_t i = 0; i < v1->size; i++) + dot += v1->data[i] * v2->data[i]; + return dot; +} + +double l2_distance(Array_double *v1, Array_double *v2) { + Array_double *minus = minus_v(v1, v2); + double dist = l2_norm(minus); + free(minus); + return dist; +} + +double l1_distance(Array_double *v1, Array_double *v2) { + Array_double *minus = minus_v(v1, v2); + double dist = l1_norm(minus); + free(minus); + return dist; +} + +double linf_distance(Array_double *v1, Array_double *v2) { + Array_double *minus = minus_v(v1, v2); + double dist = linf_norm(minus); + free(minus); + return dist; +} + +void format_vector_into(Array_double *v, char *s) { + sprintf(s, ""); + if (v->size == 0) + sprintf(s, "empty"); + + for (size_t i = 0; i < v->size; ++i) + sprintf(s, "%s %f,", s, v->data[i]); +} diff --git a/test/main.c b/test/main.c new file mode 100644 index 0000000..b0faf97 --- /dev/null +++ b/test/main.c @@ -0,0 +1,54 @@ +#include "lizfcm.h" +#include +#include + +double f(double x) { return (x - 1) / (x + 1); } + +int main() { + printf("smaceps(): %.10e\n", smaceps()); + printf("dmaceps(): %.10e\n", dmaceps()); + + Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3}); + Array_double *w = InitArray(double, {-2, 7, 1, -8, -2, 8, 5}); + + char v_s[256]; + char w_s[256]; + format_vector_into(v, v_s); + format_vector_into(w, w_s); + + printf("v: %s\n", v_s); + printf("w: %s\n", w_s); + printf("l1_norm(v): %f\n", l1_norm(v)); + printf("l2_norm(v): %f\n", l2_norm(v)); + printf("linf_norm(v): %f\n", linf_norm(v)); + + printf("l1_dist(v, w): %f\n", l1_distance(v, w)); + printf("l2_dist(v, w): %f\n", l2_distance(v, w)); + printf("linf_dist(v, w): %f\n", linf_distance(v, w)); + + double h = 0.001; + printf("approx f'(1) w/ c.d.: %f\n", central_derivative_at(&f, 1, h)); + printf("approx f'(1) w/ fw.d.: %f\n", forward_derivative_at(&f, 1, h)); + printf("approx f'(1) w/ bw.d.: %f\n", backward_derivative_at(&f, 1, h)); + + v = InitArray(double, {1, 2, 3, 4, 5}); + w = InitArray(double, {2, 3, 4, 5, 6}); + format_vector_into(v, v_s); + format_vector_into(w, w_s); + printf("v: %s\n", v_s); + printf("w: %s\n", w_s); + + Line *line = least_squares_lin_reg(v, w); + printf("least_squares_lin_reg(v, w): (%f)x + %f\n", line->m, line->a); + + v = InitArray(double, {1, 2, 3, 4, 5, 6, 7}); + w = InitArray(double, {0.5, 3, 2, 3.5, 5, 6, 7.5}); + format_vector_into(v, v_s); + format_vector_into(w, w_s); + printf("v: %s\n", v_s); + printf("w: %s\n", w_s); + line = least_squares_lin_reg(v, w); + printf("least_squares_lin_reg(v, w): (%f)x + %f\n", line->m, line->a); + + return 0; +} -- cgit v1.2.3-70-g09d2