From add2520e49da79357b7759d65f7c0863d88669b3 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Wed, 11 Oct 2023 17:00:38 -0600 Subject: [PATCH] bug fixes and fix compilation on gcc --- Makefile | 20 ++++----- inc/types.h | 2 +- lib/lizfcm.a | Bin 14496 -> 14128 bytes src/main.c | 7 --- src/matrix.c | 12 +++--- src/vector.c | 18 +++++--- test/main.c | 117 +++++++++++++++++++++++++++++---------------------- 7 files changed, 95 insertions(+), 81 deletions(-) delete mode 100644 src/main.c diff --git a/Makefile b/Makefile index a76a59f..176e8cd 100644 --- a/Makefile +++ b/Makefile @@ -5,30 +5,26 @@ 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) +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 +LDFLAGS := -lm .PHONY: all clean all: $(TEST_EXE) -$(TEST_EXE): $(LIBRARY) - $(CC) $(CPPFLAGS) $(CFLAGS) $(LIBRARY) $(TEST_SRC) -o $@ +$(TEST_EXE): $(BIN_DIR) | $(LIBRARY) + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(TEST_SRC) $(LIBRARY) -o $@ -$(LIBRARY): $(EXE) +$(LIBRARY): $(OBJ) 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 $@ diff --git a/inc/types.h b/inc/types.h index 6df04c2..95ab96d 100644 --- a/inc/types.h +++ b/inc/types.h @@ -1,5 +1,5 @@ #include "macros.h" -#include +#include #ifndef TYPES_H #define TYPES_H diff --git a/lib/lizfcm.a b/lib/lizfcm.a index 969c89475c1ecb7220497fad381d5da52f74143a..47bbc82c6ce73ecb42787a52b330983cc2e889a2 100644 GIT binary patch delta 2870 zcmZ`)ZA?>F7(TbPQb4497NL|2I7gY4QfMj60ugkMbbf3)2>~xnEkdaBX)=dNyYOc& z%hvAjgKavUOO}ziM8|%ZB}cG&H;vh1)`%d%{@;YY5v*_^dPbJ-#<7YAnF2@hryBfv{ z2nT>yvKhM$6!9)#dnRMQ5!TKDz&{B@Owu_m64$n-eLcU66iaw4Ev8V<;XFw&p$ZA zlo9oTH)#&?52Q~`jFnec*ugSky}Ew-8_oME>*7xh<=b`Y(~h)GtyySm zuWYT|VdoPS2eMtMs1I!p<%#A><=>Sz<~>ePsgSW+%#hAlJY4UFmd+3cDOmcf`TqF9SCw^o)&8~G3EH9S=5k>+`pE@wJg*{hM} zr(d-i)&*O1x0VKSrk2NYrRqq=$foi1z~Xf!#oy2v4Vq?h4XnAZqoPRCGUDug()w zv1@icYhXnf;35Q^R_mNj#cPCDfJW$NfF;0o;3nWcpqaaCjj4j2S|?ant?KeDZ zMDN7Wt|q=7Z!Fq7A|LTg;$CQimk}SPxQ)1vcnk3k;{7UbWBW-sO$ob+e@MKEc$oMY z@kD_E!LbOFyxv(SUFAd0s_IK93i8vo86=tGYCOZA@Xwul?dDbL&Q+s`bC^6zzQoJw zZWYF-apieNnB;@I;endSj8^#t6EYqvYRDine4%G6%x${LG$lzketC;AxP<*S z?*`&rcsf;dR{+I$ zG7uk+NyVW15qwfi98e2|!^9mW)4_2( zB|GG(;K)i`r_-xAUMX%!iM!NAT&7F(w#dh<7UCX?HxkFP#pA6i?_mAZp^XwciSHx+ z8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Q zmJWBNL6-1)!mYw1!t;fj3rJ1Fkm6>$`Ezt6Q z`3nuJE~>YEtKtK#NK|d=XWu$CSdtS>+n(enZH13c+g^MQ)vc^e3v|(;nW%O3{KD1Q zU*pQSuoY)XGYFt6BTt*2QNvGkDv9O1Xh1eMBIYJZ1S4vV-JdpvgnNf&jIew4Wc6~vA#K(W_PFS36bPX?KLoPCN1|A}7s$eF0p3sEj zVQv{O!VZR9?tBf>VZKRF7rccq%TvyxXUaH5xWIVI?KmuMT`$O5VSW{dpQk=3YU^); zC072Q!^847;IF~kHr_& Vemo#Dde*>(!^Y8!w diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 6bb704a..0000000 --- a/src/main.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "lizfcm.h" -#include - -int main() { - printf("hello, from lizfcm!\n"); - return 0; -} diff --git a/src/matrix.c b/src/matrix.c index a23aad4..953d50f 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,5 +1,6 @@ #include "lizfcm.h" #include +#include #include void put_identity_diagonal(Matrix_double *m) { @@ -69,14 +70,15 @@ void free_matrix(Matrix_double *m) { } void format_matrix_into(Matrix_double *m, char *s) { - sprintf(s, ""); if (m->rows == 0) - sprintf(s, "empty"); + strcpy(s, "empty"); for (size_t y = 0; y < m->rows; ++y) { - char *row_s = malloc(sizeof(char) * 256); + char row_s[256]; + strcpy(row_s, ""); + format_vector_into(m->data[y], row_s); - sprintf(s, "%s %s \n", s, row_s); - free(row_s); + strcat(s, row_s); } + strcat(s, "\n"); } diff --git a/src/vector.c b/src/vector.c index e6e2544..a7ff783 100644 --- a/src/vector.c +++ b/src/vector.c @@ -2,6 +2,7 @@ #include #include #include +#include #include double l2_norm(Array_double *v) { @@ -100,10 +101,17 @@ void free_vector(Array_double *v) { } void format_vector_into(Array_double *v, char *s) { - sprintf(s, ""); - if (v->size == 0) - sprintf(s, "empty"); + if (v->size == 0) { + strcat(s, "empty"); + return; + } - for (size_t i = 0; i < v->size; ++i) - sprintf(s, "%s %f,", s, v->data[i]); + for (size_t i = 0; i < v->size; ++i) { + char num[64]; + strcpy(num, ""); + + sprintf(num, "%f,", v->data[i]); + strcat(s, num); + } + strcat(s, "\n"); } diff --git a/test/main.c b/test/main.c index 3551af2..ac7fba3 100644 --- a/test/main.c +++ b/test/main.c @@ -5,72 +5,87 @@ 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); + printf("Basic Routines\n"); + printf("smaceps(): %.10e\n", smaceps()); + printf("dmaceps(): %.10e\n", dmaceps()); + printf("========\n"); + + Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3}); + char v_s[256]; + strcpy(v_s, ""); + format_vector_into(v, v_s); - char m_s[256]; + Array_double *w = InitArray(double, {-2, 7, 1, -8, -2, 8, 5}); + char w_s[256]; + strcpy(w_s, ""); + format_vector_into(w, w_s); + + printf("Norm, Distance\n"); + printf("v: %s", v_s); + printf("w: %s", 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)); + printf("========\n"); + + double h = 0.001; + printf("Derivative Approxs\n"); + printf("f(x) = (x-1)/(x+1)\n"); + 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)); + printf("========\n"); + + v = InitArray(double, {1, 2, 3, 4, 5}); + strcpy(v_s, ""); + format_vector_into(v, v_s); + w = InitArray(double, {2, 3, 4, 5, 6}); + strcpy(w_s, ""); + format_vector_into(w, w_s); + Line *line = least_squares_lin_reg(v, w); + printf("Least Squares\n"); + printf("v: %s", v_s); + printf("w: %s", w_s); + 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}); + strcpy(v_s, ""); + format_vector_into(v, v_s); + w = InitArray(double, {0.5, 3, 2, 3.5, 5, 6, 7.5}); + strcpy(w_s, ""); + format_vector_into(w, w_s); + printf("v: %s", v_s); + printf("w: %s", w_s); + line = least_squares_lin_reg(v, w); + printf("least_squares_lin_reg(v, w): (%f)x + %f\n", line->m, line->a); + printf("========\n"); + + + printf("LU Decomp\n"); + char m_s[2048]; Matrix_double *m = InitMatrixWithSize(double, 8, 8, 0.0); - put_identity_diagonal(m); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { m->data[i]->data[j] = (i + 1.0) + j * 3 + (rand() % 12); } } - format_matrix_into(m, m_s); - printf("%s\n", m_s); + printf("m = %s", m_s); Matrix_double **u_l = put_lu_decomp(m); Matrix_double *u = u_l[0]; Matrix_double *l = u_l[1]; + strcpy(m_s, ""); format_matrix_into(u, m_s); - printf("%s\n", m_s); + printf("u = %s", m_s); + strcpy(m_s, ""); format_matrix_into(l, m_s); - printf("%s\n", m_s); + printf("l = %s", m_s); + printf("========\n"); + printf("Back Substitution\n"); return 0; }