2023-10-11 12:04:04 -04:00
|
|
|
#include "lizfcm.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
double f(double x) { return (x - 1) / (x + 1); }
|
|
|
|
|
|
|
|
int main() {
|
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 11:58:25 -04:00
|
|
|
Array_double *v = InitArray(double, {3, 1, -4, 1, 5, -9, 3});
|
2023-10-13 19:20:17 -04:00
|
|
|
char s[2048];
|
|
|
|
strcpy(s, "");
|
|
|
|
format_vector_into(v, s);
|
|
|
|
printf("v: %s", s);
|
2023-10-13 11:58:25 -04:00
|
|
|
|
|
|
|
Array_double *w = InitArray(double, {-2, 7, 1, -8, -2, 8, 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
|
|
|
|
|
|
|
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-11 12:04:04 -04:00
|
|
|
|
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);
|
2023-10-13 19:20:17 -04:00
|
|
|
|
2023-10-13 11:58:25 -04:00
|
|
|
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);
|
|
|
|
printf("========\n");
|
2023-10-11 19:00:38 -04:00
|
|
|
|
|
|
|
printf("LU Decomp\n");
|
2023-10-13 19:20:17 -04:00
|
|
|
Matrix_double *m = InitMatrixWithSize(double, 10, 10, 0.0);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
for (int j = 0; j < 10; j++)
|
|
|
|
m->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, "");
|
|
|
|
format_matrix_into(m, s);
|
|
|
|
printf("m = %s", s);
|
2023-10-11 17:56:20 -04:00
|
|
|
|
2023-10-13 19:20:17 -04:00
|
|
|
Array_double *b = InitArrayWithSize(double, 10, 100.0);
|
|
|
|
Matrix_double **u_l = lu_decomp(m);
|
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);
|
|
|
|
printf("b = %s", s);
|
2023-10-11 19:00:38 -04:00
|
|
|
printf("========\n");
|
2023-10-13 19:20:17 -04:00
|
|
|
printf("Backward -> Forward Substitution\n");
|
|
|
|
|
|
|
|
Array_double *b_fsub = fsubst(l, b);
|
|
|
|
strcpy(s, "");
|
|
|
|
format_vector_into(b_fsub, s);
|
|
|
|
printf("x: %s\n", s);
|
|
|
|
|
|
|
|
Array_double *x_bsub = bsubst(u, b_fsub);
|
|
|
|
strcpy(s, "");
|
|
|
|
format_vector_into(x_bsub, s);
|
|
|
|
printf("x: %s", s);
|
|
|
|
|
|
|
|
free_vector(b_fsub);
|
|
|
|
|
|
|
|
printf("Verifications (each should be approximately 100)\n");
|
|
|
|
for (size_t row = 0; row < m->rows; row++) {
|
|
|
|
double curr = 0;
|
|
|
|
for (size_t col = 0; col < m->cols; col++)
|
|
|
|
curr += m->data[row]->data[col] * x_bsub->data[col];
|
|
|
|
printf("Substitution for row %zu = %f\n", row, curr);
|
|
|
|
}
|
2023-10-11 16:09:59 -04:00
|
|
|
|
2023-10-11 12:04:04 -04:00
|
|
|
return 0;
|
|
|
|
}
|