bug fixes and fix compilation on gcc

This commit is contained in:
Elizabeth Hunt 2023-10-11 17:00:38 -06:00
parent 78bd8c4a95
commit add2520e49
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
7 changed files with 95 additions and 81 deletions

View File

@ -5,30 +5,26 @@ BIN_DIR := dist
LIB_DIR := lib LIB_DIR := lib
TEST_EXE := $(BIN_DIR)/lizfcm.test TEST_EXE := $(BIN_DIR)/lizfcm.test
EXE := $(BIN_DIR)/lizfcm EXE := $(BIN_DIR)/lizfcm
LIBRARY := $(LIB_DIR)/lizfcm.a LIBRARY := $(LIB_DIR)/lizfcm.a
SRC := $(wildcard $(SRC_DIR)/*.c) SRC := $(wildcard $(SRC_DIR)/*.c)
OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
CPPFLAGS := -Iinc -MMD -MP CPPFLAGS := -Iinc -MMD -MP
CFLAGS := -Wall CFLAGS := -Wall
LDFLAGS := LDFLAGS := -lm
LDLIBS := -lm
.PHONY: all clean .PHONY: all clean
all: $(TEST_EXE) all: $(TEST_EXE)
$(TEST_EXE): $(LIBRARY) $(TEST_EXE): $(BIN_DIR) | $(LIBRARY)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LIBRARY) $(TEST_SRC) -o $@ $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(TEST_SRC) $(LIBRARY) -o $@
$(LIBRARY): $(EXE) $(LIBRARY): $(OBJ)
ar rcs $(LIBRARY) $(OBJ_DIR)/*.o ar rcs $(LIBRARY) $(OBJ_DIR)/*.o
ranlib $(LIBRARY) ranlib $(LIBRARY)
$(EXE): $(OBJ) | $(BIN_DIR)
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

View File

@ -1,5 +1,5 @@
#include "macros.h" #include "macros.h"
#include <stddef.h> #include <stdint.h>
#ifndef TYPES_H #ifndef TYPES_H
#define TYPES_H #define TYPES_H

Binary file not shown.

View File

@ -1,7 +0,0 @@
#include "lizfcm.h"
#include <stdio.h>
int main() {
printf("hello, from lizfcm!\n");
return 0;
}

View File

@ -1,5 +1,6 @@
#include "lizfcm.h" #include "lizfcm.h"
#include <assert.h> #include <assert.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
void put_identity_diagonal(Matrix_double *m) { 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) { void format_matrix_into(Matrix_double *m, char *s) {
sprintf(s, "");
if (m->rows == 0) if (m->rows == 0)
sprintf(s, "empty"); strcpy(s, "empty");
for (size_t y = 0; y < m->rows; ++y) { 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); format_vector_into(m->data[y], row_s);
sprintf(s, "%s %s \n", s, row_s); strcat(s, row_s);
free(row_s);
} }
strcat(s, "\n");
} }

View File

@ -2,6 +2,7 @@
#include <assert.h> #include <assert.h>
#include <float.h> #include <float.h>
#include <math.h> #include <math.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
double l2_norm(Array_double *v) { 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) { void format_vector_into(Array_double *v, char *s) {
sprintf(s, ""); if (v->size == 0) {
if (v->size == 0) strcat(s, "empty");
sprintf(s, "empty"); return;
}
for (size_t i = 0; i < v->size; ++i) for (size_t i = 0; i < v->size; ++i) {
sprintf(s, "%s %f,", s, v->data[i]); char num[64];
strcpy(num, "");
sprintf(num, "%f,", v->data[i]);
strcat(s, num);
}
strcat(s, "\n");
} }

View File

@ -5,72 +5,87 @@
double f(double x) { return (x - 1) / (x + 1); } double f(double x) { return (x - 1) / (x + 1); }
int main() { int main() {
// printf("smaceps(): %.10e\n", smaceps()); printf("Basic Routines\n");
// printf("dmaceps(): %.10e\n", dmaceps()); printf("smaceps(): %.10e\n", smaceps());
// printf("dmaceps(): %.10e\n", dmaceps());
// Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3}); printf("========\n");
// Array_double *w = InitArray(double, {-2, 7, 1, -8, -2, 8, 5});
// Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3});
// char v_s[256]; char v_s[256];
// char w_s[256]; strcpy(v_s, "");
// format_vector_into(v, v_s); 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);
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); Matrix_double *m = InitMatrixWithSize(double, 8, 8, 0.0);
put_identity_diagonal(m);
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
m->data[i]->data[j] = (i + 1.0) + j * 3 + (rand() % 12); m->data[i]->data[j] = (i + 1.0) + j * 3 + (rand() % 12);
} }
} }
format_matrix_into(m, m_s); 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_l = put_lu_decomp(m);
Matrix_double *u = u_l[0]; Matrix_double *u = u_l[0];
Matrix_double *l = u_l[1]; Matrix_double *l = u_l[1];
strcpy(m_s, "");
format_matrix_into(u, 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); format_matrix_into(l, m_s);
printf("%s\n", m_s); printf("l = %s", m_s);
printf("========\n");
printf("Back Substitution\n");
return 0; return 0;
} }