diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-10-11 10:04:04 -0600 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-10-11 10:04:04 -0600 |
| commit | 43f06890e2689af2ef54c4480fe5790692a24f65 (patch) | |
| tree | b933f3e05aad81d780c0c94646676efa1bbad22d /src/lin.c | |
| parent | a74a732b27fb610133190e89a91b2d42d0cf78b3 (diff) | |
| download | math-4610-43f06890e2689af2ef54c4480fe5790692a24f65.tar.gz math-4610-43f06890e2689af2ef54c4480fe5790692a24f65.zip | |
deprecate common lisp solutions and write c; it's too much effort to keep up with the requirements for an archive.
Diffstat (limited to 'src/lin.c')
| -rw-r--r-- | src/lin.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/lin.c b/src/lin.c new file mode 100644 index 0000000..2df6f28 --- /dev/null +++ b/src/lin.c @@ -0,0 +1,19 @@ +#include "lizfcm.h" +#include <assert.h> + +Line *least_squares_lin_reg(Array_double *x, Array_double *y) { + assert(x->size == y->size); + + uint64_t n = x->size; + double sum_x = sum_v(x); + double sum_y = sum_v(y); + double sum_xy = dot_v(x, y); + double sum_xx = dot_v(x, x); + double denom = ((n * sum_xx) - (sum_x * sum_x)); + + Line *line = malloc(sizeof(Line)); + line->m = ((sum_xy * n) - (sum_x * sum_y)) / denom; + line->a = ((sum_y * sum_xx) - (sum_x * sum_xy)) / denom; + + return line; +} |
