summaryrefslogtreecommitdiff
path: root/src/matrix.c
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-13 21:00:07 -0600
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-10-13 21:00:07 -0600
commitd21a7de801b4a326001e45c0d26826e9ab53589b (patch)
tree314c35f77473b651f4a2f79395769595ddb9dd6c /src/matrix.c
parentcae58e90e0a2f19aa9a01177ef1707904f22c64c (diff)
downloadcmath-d21a7de801b4a326001e45c0d26826e9ab53589b.tar.gz
cmath-d21a7de801b4a326001e45c0d26826e9ab53589b.zip
hw 4
Diffstat (limited to 'src/matrix.c')
-rw-r--r--src/matrix.c37
1 files changed, 34 insertions, 3 deletions
diff --git a/src/matrix.c b/src/matrix.c
index 438a468..51de22c 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -3,11 +3,23 @@
#include <stdio.h>
#include <string.h>
-void put_identity_diagonal(Matrix_double *m) {
- assert(m->rows == m->cols);
+Array_double *m_dot_v(Matrix_double *m, Array_double *v) {
+ assert(v->size == m->cols);
+
+ Array_double *product = copy_vector(v);
+
+ for (size_t row = 0; row < v->size; ++row)
+ product->data[row] = v_dot_v(m->data[row], v);
+
+ return product;
+}
+Matrix_double *put_identity_diagonal(Matrix_double *m) {
+ assert(m->rows == m->cols);
+ Matrix_double *copy = copy_matrix(m);
for (size_t y = 0; y < m->rows; ++y)
- m->data[y]->data[y] = 1.0;
+ copy->data[y]->data[y] = 1.0;
+ return copy;
}
Matrix_double *copy_matrix(Matrix_double *m) {
@@ -89,6 +101,25 @@ Array_double *fsubst(Matrix_double *l, Array_double *b) {
return x;
}
+Array_double *solve_matrix(Matrix_double *m, Array_double *b) {
+ assert(b->size == m->rows);
+ assert(m->rows == m->cols);
+
+ Array_double *x = copy_vector(b);
+ Matrix_double **u_l = lu_decomp(m);
+ Matrix_double *u = u_l[0];
+ Matrix_double *l = u_l[1];
+
+ Array_double *b_fsub = fsubst(l, b);
+ x = bsubst(u, b_fsub);
+ free_vector(b_fsub);
+
+ free_matrix(u);
+ free_matrix(l);
+
+ return x;
+}
+
void free_matrix(Matrix_double *m) {
for (size_t y = 0; y < m->rows; ++y)
free_vector(m->data[y]);