From 43f06890e2689af2ef54c4480fe5790692a24f65 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 11 Oct 2023 10:04:04 -0600 Subject: [PATCH] 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 {src => deprecated-cl}/approx,derivative.lisp | 0 {src => deprecated-cl}/approx,maceps.lisp | 0 {src => deprecated-cl}/approx,package.lisp | 0 {src => deprecated-cl}/lizfcm.asd | 2 +- {src => deprecated-cl}/main.lisp | 0 {src => deprecated-cl}/tests,approx.lisp | 0 {src => deprecated-cl}/tests,maceps.lisp | 0 {src => deprecated-cl}/tests,suite.lisp | 0 {src => deprecated-cl}/tests,table.lisp | 0 {src => deprecated-cl}/tests,vector.lisp | 0 {src => deprecated-cl}/utils,package.lisp | 0 {src => deprecated-cl}/utils,table.lisp | 0 .../utils,within-range.lisp | 0 {src => deprecated-cl}/vector,distance.lisp | 0 .../vector,least-squares.lisp | 0 {src => deprecated-cl}/vector,norm.lisp | 0 {src => deprecated-cl}/vector,package.lisp | 0 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.c | 38 ++++++++ src/lin.c | 19 ++++ src/maceps.c | 28 ++++++ src/main.c | 7 ++ src/vector,least-squares-reg.lisp | 2 - src/vector.c | 90 ++++++++++++++++++ test/main.c | 54 +++++++++++ 34 files changed, 384 insertions(+), 6 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile delete mode 100755 compile_archive.sh create mode 100644 deprecated-cl/.main.lisp.swp rename {src => deprecated-cl}/approx,derivative.lisp (100%) rename {src => deprecated-cl}/approx,maceps.lisp (100%) rename {src => deprecated-cl}/approx,package.lisp (100%) rename {src => deprecated-cl}/lizfcm.asd (94%) rename {src => deprecated-cl}/main.lisp (100%) rename {src => deprecated-cl}/tests,approx.lisp (100%) rename {src => deprecated-cl}/tests,maceps.lisp (100%) rename {src => deprecated-cl}/tests,suite.lisp (100%) rename {src => deprecated-cl}/tests,table.lisp (100%) rename {src => deprecated-cl}/tests,vector.lisp (100%) rename {src => deprecated-cl}/utils,package.lisp (100%) rename {src => deprecated-cl}/utils,table.lisp (100%) rename {src => deprecated-cl}/utils,within-range.lisp (100%) rename {src => deprecated-cl}/vector,distance.lisp (100%) rename {src => deprecated-cl}/vector,least-squares.lisp (100%) rename {src => deprecated-cl}/vector,norm.lisp (100%) rename {src => deprecated-cl}/vector,package.lisp (100%) 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 create mode 100644 src/approx_derivative.c create mode 100644 src/lin.c create mode 100644 src/maceps.c create mode 100644 src/main.c delete mode 100644 src/vector,least-squares-reg.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 0000000000000000000000000000000000000000..e0a098ed29a3dbd28e99b3c1c464e804c23df265 GIT binary patch literal 12288 zcmeI2zmFS56vyYN638WpNGT}Li&1pD$*s@p%O%1GqM!*1Gzq^70-4(#$E%zDakG1S zPKrZBv?xkL5eX{(0ze89)bt>rrJ;j@@E7E}yJN@pB~F|!(p&lCwRhgUx9|JrjbwSZ zIv>3KF1^<72plg8aqI3Wb^NE>;!Ro-I=XS=dUgFVhct1Nv~?OtJ=b!-cDQySjn!Ui zw%1}=49=eOJ8PlJj0$9-LNCy`TzQM~Y_2mNWCd7(1r=Bp>s`NY^;cUb>G(_UFF2pg zSpimn6<`He0akz&U~3GoN`9ee>k0aw69@Cx|$+l&hd?ETk$SF!dGq_JdF%~f{sPB}(`j$eI(2Cs=M6eX=i3{P%A!<3k(!=X zGB3P*XIC1Pdq!QO700hU(Ambr0S%JA7e;v@lR!1LE>0fo4@T-fv8DgSmJ#A7sflgu z12yvJj&|pbhOrD(me0D($CsT@^>-7ho{J^N6{yZt2W7VY3!DLFeW;vKs9u{+IZ_7e z1y5sZvKhm=Zm7j&c!Xb@*0kS6a-H@&wc9va##^DR)(_$3w>x2{c}Es?8i5R@LHV(V zj2D63V1qQyb_?Y-%`;lIw#97sOhv1f(QLdb+Ed4}eOt%c+@!uqV=4xUE)THRh4fCe zWu&=IXu~LpiYQG4tcpRD6JC;nRBP;!xISvoD7jWU(Bqk<(rE@?*aWaEG!N0Ql2JRm h5klYx@{aD|bFi&b8B(ul6ZCNXwPm|T=3&bf_W`0rIVS)B literal 0 HcmV?d00001 diff --git a/src/approx,derivative.lisp b/deprecated-cl/approx,derivative.lisp similarity index 100% rename from src/approx,derivative.lisp rename to deprecated-cl/approx,derivative.lisp diff --git a/src/approx,maceps.lisp b/deprecated-cl/approx,maceps.lisp similarity index 100% rename from src/approx,maceps.lisp rename to deprecated-cl/approx,maceps.lisp diff --git a/src/approx,package.lisp b/deprecated-cl/approx,package.lisp similarity index 100% rename from src/approx,package.lisp rename to deprecated-cl/approx,package.lisp diff --git a/src/lizfcm.asd b/deprecated-cl/lizfcm.asd similarity index 94% rename from src/lizfcm.asd rename to deprecated-cl/lizfcm.asd index 0096257..dea3ddd 100644 --- a/src/lizfcm.asd +++ b/deprecated-cl/lizfcm.asd @@ -9,7 +9,7 @@ (:file "approx,maceps" :depends-on ("approx,package")) (:file "approx,derivative" :depends-on ("approx,package")) (:file "approx,package") - (:file "vector,least-squares") + (: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"))) diff --git a/src/main.lisp b/deprecated-cl/main.lisp similarity index 100% rename from src/main.lisp rename to deprecated-cl/main.lisp diff --git a/src/tests,approx.lisp b/deprecated-cl/tests,approx.lisp similarity index 100% rename from src/tests,approx.lisp rename to deprecated-cl/tests,approx.lisp diff --git a/src/tests,maceps.lisp b/deprecated-cl/tests,maceps.lisp similarity index 100% rename from src/tests,maceps.lisp rename to deprecated-cl/tests,maceps.lisp diff --git a/src/tests,suite.lisp b/deprecated-cl/tests,suite.lisp similarity index 100% rename from src/tests,suite.lisp rename to deprecated-cl/tests,suite.lisp diff --git a/src/tests,table.lisp b/deprecated-cl/tests,table.lisp similarity index 100% rename from src/tests,table.lisp rename to deprecated-cl/tests,table.lisp diff --git a/src/tests,vector.lisp b/deprecated-cl/tests,vector.lisp similarity index 100% rename from src/tests,vector.lisp rename to deprecated-cl/tests,vector.lisp diff --git a/src/utils,package.lisp b/deprecated-cl/utils,package.lisp similarity index 100% rename from src/utils,package.lisp rename to deprecated-cl/utils,package.lisp diff --git a/src/utils,table.lisp b/deprecated-cl/utils,table.lisp similarity index 100% rename from src/utils,table.lisp rename to deprecated-cl/utils,table.lisp diff --git a/src/utils,within-range.lisp b/deprecated-cl/utils,within-range.lisp similarity index 100% rename from src/utils,within-range.lisp rename to deprecated-cl/utils,within-range.lisp diff --git a/src/vector,distance.lisp b/deprecated-cl/vector,distance.lisp similarity index 100% rename from src/vector,distance.lisp rename to deprecated-cl/vector,distance.lisp diff --git a/src/vector,least-squares.lisp b/deprecated-cl/vector,least-squares.lisp similarity index 100% rename from src/vector,least-squares.lisp rename to deprecated-cl/vector,least-squares.lisp diff --git a/src/vector,norm.lisp b/deprecated-cl/vector,norm.lisp similarity index 100% rename from src/vector,norm.lisp rename to deprecated-cl/vector,norm.lisp diff --git a/src/vector,package.lisp b/deprecated-cl/vector,package.lisp similarity index 100% rename from src/vector,package.lisp rename to deprecated-cl/vector,package.lisp 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 0000000000000000000000000000000000000000..fe9a6ca0f0f94baa4855185533df4a85a3fcd19e GIT binary patch literal 9672 zcmd^FZ)}^z89&!;01U;keb%XLL0Qc%+MAw$r}~)L#J(hZ5(GYI8LzL z($ayfr5`$JlhUc0(1g?!KCIa$O`7%rG`d9tZIy-uYz%c0A6!71%LbH*4+)F$e!sit z+}HN&I)x!2?UeKHp8NCMy?6Kgxw~xMnarog@96&0;I^Ttb2$udzkNq^I65?VyW`vz z9YldC9*jn}4-Y%e@ZgYCPIP*knGa_UJ@1m7XdnuV51s$xKdp$DNFtR^6({pvCRdzPUUtwM%@m5sTq>=J9O@u3z4 z!2-J|9GoCz)5${7D?EHKnNJtIY$oUB)8EDCDz59uxwlDTGzpm9otRyM0av( zDnEJHHzQjo$x?@xK78jROf^>3{Hz2%L&`x!_yXdHF4bh~PuOIgw{?W>LN=q2%JYip z!^O+ZJ#XKh{rfJ{HnnqwNs%9u=P{tqGi#+P5p3|hRH2y9Y{*4VRhfqd2G1GD_Dj_1U z67!!xJIuSFat zW~UDQDBS*Qsiq9s%8>s%NIXh4Q*sRfWF#T6wL|JwkV>YI$v}dZ7z6>R&*; zH&C{D-h)+Ws}@PAapw*v zs)BWE8wK68gI_RolXNO2`Rchgxw;srLy$0O~knL6)bSab=bbJY80J>CVY)2VpySPDjH<0aO2Hl-NG>wlK zbeRu&wIc}7^xuHjp}+>}jO)P2>a|}8eIN1|UPNU1n9v*t>8#K{5c>N<)3*Bg&6CE! zXQljM&^_qCBx4|uw&8Z6?K=G$w67QnId~UkJ@TP7CF)nvUlsjTwM~VMl>Vyduc}_Xfc_HA{Z&J~32ifj+Dm`c+O+A<&dC%Ly>6NAH}oGBBRbW+%5eWBJJmC!2Y6EH$y2 z+q5Cbytb$j2emNPfg6DscbxVZ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I#wiEfP9UfZcpdT)1J z7Tez2S(@9O5u&5Ra_=8TeEcA`Dgg#Kl2f^q2sU)OFR*;JNrPTH5TVK2A3EI^jLWzj zX%quLvYf|{6g9BybYE}|?RX!+_w5qUe_sIoOC7BFat=}DszYs>{?sbm+nIOiPkbH? zc%Qa@7|(n3K{3davthh_!-rzJya8|4$UkjqzG0I_)a7`?<~zAMpM0G6?@xsfEIo~Q zS0vS^jwkiKgtBK|`n7n&=6m>N)Mwo{H^i#8u0G%6UAscxHw3>IWtQigXct%y{#+UL zt4H_X?$Y>b!T%USiAkx$mp!#3;;7ZvMfO|EQt`bI{=&d45GsFwm!`xL5dOsg{WRjY zfmrK-{XlGuIPDP7jRUc<;`bQT@0{3g`kfP4zZX4%a=+dC3TZU7a?m66H)ONYe`EPR zp&x9jKS~+{u>1aI(A4ub@LttN>a?Hc_xNdia|3Plyr^z_>)ND_1OH3Mr{9BD%d@)H O=v4FEVKi&~-Q&O7J?iiP literal 0 HcmV?d00001 diff --git a/lizfcm.a b/lizfcm.a deleted file mode 100644 index cb3411787bc26666d48c980786295cea622231f0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12720 zcmds7-E-Tx5%079iali-!;a~Qk}XHN>rEdnopz=-ooVvYr!z%T78{CGNJ^3VkiWe@ zU;z@KNLe{2*UoXqA0!rw-Nk+b-~A92;p)f13;*?aM9t$*XVcmJXgZq^-Hm(<@UTA` zO>b}U_Qt?I41W3L=H0)3{^{*s=-og5`Tp%sf3O~jw+6)&g?IF3HUr8oywllOBYqeJ zn@y4LucNp~c0rZw;+r%nH;h~)nYRhT55Z$h^ECO-GF;y<{N-REqIkK@D2%hJ2vV;> z-~|e)fEY`ACq_mNHwfRG+kovlkhr64wwTX z(gQ7lCn+o1k1w6M2h43jU|9_!UQ5J4a_}W3>X9prY0p50v1ML-3X13(Ft)E3h9rI~ zb{5#jXYu1u{Xg8E!9dwjhravc9y^+hVNB|&^?eh+bZ@LfmHN> zk}Ro=T0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&$Y!_J!Ct3Jii3t`43L~z`?sTfc{(0X@v*BPVcU(CbEl(rybKBzv zmq5^>5FgX|$88dRP-8l+hnx{GanC{}If1!bByWP0tNc?K$d(euW1NO9il8{aW$fZK z-=Ij9K@|;=)$P0;)an`{KNZnJsHk`v(tJQ^+6 zj@S5Hlu>8$>XKZeV>iw?95VeAOP(o)lS?uVXGtlUnlKJ9%%>xv8iGl`a91(D!IC&m zc2!TG@0RXSaq_iRwO{ppQguk%K%zEM&R1Q4#SB$wDqv2}wBh3RUFiW(T@8E?uvQo= zRW$q@(4XjY@cEBJ*~sC~S(3_uA?Yb}t^aAXL;4wQrq&8 zT9b@g_l(xc*)wiw1w){2@l-vgcB&!HR@o;Yoo#;#R^4*ewsPd_kBX>c59YYD)(gt+ zz6#5hpncQ2`=%mYEbU?sRP@g8KFB|Re$Q#CivaqC2l>=QUp!HatN4dFe}CT{=Vkoo z=V74Z{E@`%nCQfN$0|>)KbySi$usV(d%<1n+iS~^d~$u`N$p#As>(;Mt_mhuh|5Dm zn4j)I;rd6TKcd{Oy#qBV)GJJDk0Whw(n5KCq&0ud N)rx2~Eab|A{{o$x;)MVJ 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.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/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/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.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; +}