summaryrefslogtreecommitdiff
path: root/test/main.c
blob: d31c92b8570ee63c2fbe366f8c122168d49b4271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "lizfcm.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

double f(double x) { return (x - 1) / (x + 1); }

int main() {
  char s[2048];

  printf("Basic Routines\n");
  printf("smaceps(): %.10e\n", smaceps());
  printf("dmaceps(): %.10e\n", dmaceps());
  printf("========\n");

  printf("Norm, Distance\n");
  Array_double *v = InitArray(double, {3, 1, -4});
  strcpy(s, "");
  format_vector_into(v, s);
  printf("v: %s", s);

  Array_double *w = InitArray(double, {-2, 7, 1});
  strcpy(s, "");
  format_vector_into(w, s);
  printf("w: %s", 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");
  printf("Least Squares\n");

  v = InitArray(double, {1, 2, 3, 4, 5});
  strcpy(s, "");
  format_vector_into(v, s);
  printf("v: %s", s);
  w = InitArray(double, {2, 3, 4, 5, 6});
  strcpy(s, "");
  format_vector_into(w, s);
  printf("w: %s", 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});
  strcpy(s, "");
  format_vector_into(v, s);
  printf("v: %s", s);
  w = InitArray(double, {0.5, 3, 2, 3.5, 5, 6, 7.5});
  strcpy(s, "");
  format_vector_into(w, s);
  printf("w: %s", s);

  line = least_squares_lin_reg(v, w);
  printf("least_squares_lin_reg(v, w): (%f)x + %f\n", line->m, line->a);
  free(line);
  printf("========\n");

  printf("LU Decomp\n");
  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);
  }
  strcpy(s, "");
  format_matrix_into(a, s);
  printf("a = %s", s);

  uint32_t solution = 1;
  Array_double *y = InitArrayWithSize(double, n, (double)solution);
  Array_double *b = m_dot_v(a, y); // q8 steps
  Matrix_double **u_l = lu_decomp(a);
  Matrix_double *u = u_l[0];
  Matrix_double *l = u_l[1];

  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("(after following q8) b = %s", s);
  printf("========\n");
  printf("Forward / Backward Substitution Solution to ax=b\n");

  Array_double *b_fsub = fsubst(l, b);
  strcpy(s, "");
  format_vector_into(b_fsub, s);
  printf("b_fsub: %s", s);

  Array_double *x_bsub = bsubst(u, b_fsub);
  strcpy(s, "");
  format_vector_into(x_bsub, s);
  printf("x_bsub: %s", s);

  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);

  free_vector(b_fsub);
  free_vector(x_solve_matrix);

  printf("Verifications\n");
  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));

  return 0;
}