summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile20
-rw-r--r--inc/types.h2
-rw-r--r--lib/lizfcm.abin14496 -> 14128 bytes
-rw-r--r--src/main.c7
-rw-r--r--src/matrix.c12
-rw-r--r--src/vector.c20
-rw-r--r--test/main.c117
7 files changed, 96 insertions, 82 deletions
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 <stddef.h>
+#include <stdint.h>
#ifndef TYPES_H
#define TYPES_H
diff --git a/lib/lizfcm.a b/lib/lizfcm.a
index 969c894..47bbc82 100644
--- a/lib/lizfcm.a
+++ b/lib/lizfcm.a
Binary files differ
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 <stdio.h>
-
-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 <assert.h>
+#include <string.h>
#include <stdio.h>
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 <assert.h>
#include <float.h>
#include <math.h>
+#include <string.h>
#include <stdio.h>
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");
-
- for (size_t i = 0; i < v->size; ++i)
- sprintf(s, "%s %f,", s, v->data[i]);
+ if (v->size == 0) {
+ strcat(s, "empty");
+ return;
+ }
+
+ 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;
}