lizfcm/test/main.c

125 lines
3.4 KiB
C
Raw Normal View History

#include "lizfcm.h"
2023-10-13 19:54:47 -04:00
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
double f(double x) { return (x - 1) / (x + 1); }
int main() {
2023-10-13 19:54:47 -04:00
char s[2048];
2023-10-13 11:58:25 -04:00
printf("Basic Routines\n");
printf("smaceps(): %.10e\n", smaceps());
printf("dmaceps(): %.10e\n", dmaceps());
printf("========\n");
2023-10-13 19:20:17 -04:00
printf("Norm, Distance\n");
2023-10-13 23:00:07 -04:00
Array_double *v = InitArray(double, {3, 1, -4});
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_vector_into(v, s);
printf("v: %s", s);
2023-10-13 11:58:25 -04:00
2023-10-13 23:00:07 -04:00
Array_double *w = InitArray(double, {-2, 7, 1});
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_vector_into(w, s);
printf("w: %s", s);
2023-10-13 11:58:25 -04:00
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");
2023-10-13 19:20:17 -04:00
printf("Least Squares\n");
2023-10-13 11:58:25 -04:00
v = InitArray(double, {1, 2, 3, 4, 5});
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_vector_into(v, s);
printf("v: %s", s);
2023-10-13 11:58:25 -04:00
w = InitArray(double, {2, 3, 4, 5, 6});
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_vector_into(w, s);
printf("w: %s", s);
2023-10-13 11:58:25 -04:00
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});
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_vector_into(v, s);
printf("v: %s", s);
2023-10-13 11:58:25 -04:00
w = InitArray(double, {0.5, 3, 2, 3.5, 5, 6, 7.5});
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_vector_into(w, s);
printf("w: %s", s);
2023-10-13 11:58:25 -04:00
line = least_squares_lin_reg(v, w);
printf("least_squares_lin_reg(v, w): (%f)x + %f\n", line->m, line->a);
2023-10-13 23:00:07 -04:00
free(line);
2023-10-13 11:58:25 -04:00
printf("========\n");
2023-10-11 19:00:38 -04:00
printf("LU Decomp\n");
2023-10-13 19:54:47 -04:00
uint32_t n = 10;
Matrix_double *a = InitMatrixWithSize(double, n, n, 0.0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
a->data[i]->data[j] = (100 - rand() % 200);
2023-10-11 17:56:20 -04:00
}
2023-10-13 19:20:17 -04:00
strcpy(s, "");
2023-10-13 19:54:47 -04:00
format_matrix_into(a, s);
printf("a = %s", s);
2023-10-11 17:56:20 -04:00
2023-10-13 23:00:07 -04:00
uint32_t solution = 1;
Array_double *y = InitArrayWithSize(double, n, (double)solution);
Array_double *b = m_dot_v(a, y); // q8 steps
2023-10-13 19:54:47 -04:00
Matrix_double **u_l = lu_decomp(a);
2023-10-11 17:56:20 -04:00
Matrix_double *u = u_l[0];
Matrix_double *l = u_l[1];
2023-10-13 19:20:17 -04:00
strcpy(s, "");
format_matrix_into(u, s);
printf("u = %s", s);
strcpy(s, "");
format_matrix_into(l, s);
printf("l = %s", s);
strcpy(s, "");
format_vector_into(b, s);
2023-10-13 23:00:07 -04:00
printf("(after following q8) b = %s", s);
2023-10-11 19:00:38 -04:00
printf("========\n");
2023-10-13 19:54:47 -04:00
printf("Forward / Backward Substitution Solution to ax=b\n");
2023-10-13 19:20:17 -04:00
Array_double *b_fsub = fsubst(l, b);
strcpy(s, "");
format_vector_into(b_fsub, s);
2023-10-13 19:54:47 -04:00
printf("b_fsub: %s", s);
2023-10-13 19:20:17 -04:00
Array_double *x_bsub = bsubst(u, b_fsub);
strcpy(s, "");
format_vector_into(x_bsub, s);
2023-10-13 19:54:47 -04:00
printf("x_bsub: %s", s);
2023-10-13 19:20:17 -04:00
2023-10-13 23:00:07 -04:00
Array_double *x_solve_matrix = solve_matrix(a, b);
free_vector(b);
strcpy(s, "");
format_vector_into(x_solve_matrix, s);
printf("\\--> == x_solve_matrix: %s", s);
2023-10-13 19:20:17 -04:00
free_vector(b_fsub);
2023-10-13 23:00:07 -04:00
free_vector(x_solve_matrix);
2023-10-13 19:20:17 -04:00
2023-10-13 19:54:47 -04:00
printf("Verifications\n");
2023-10-13 23:00:07 -04:00
for (size_t i = 0; i < x_bsub->size; i++)
printf("in row %zu, solution = %f, true value err=%.10e\n", i,
x_bsub->data[i], fabs(x_bsub->data[i] - solution));
2023-10-11 16:09:59 -04:00
return 0;
}