diff options
author | Jasper | 2025-09-08 16:07:05 +0200 |
---|---|---|
committer | Jasper | 2025-09-08 16:07:05 +0200 |
commit | 6997b40597e8f8bfc0226bdf0c83bb4108f02e6d (patch) | |
tree | 4254eee9e125533a75ffe73882ac9c34ca833001 /matrix.c | |
parent | e99c921808989b0b242ccfa1aa6edd74871bc213 (diff) |
'Matrix' -> 'Vector' where needed
Diffstat (limited to 'matrix.c')
-rw-r--r-- | matrix.c | 29 |
1 files changed, 15 insertions, 14 deletions
@@ -1,5 +1,6 @@ #include "matrix.h" +#include "vector.h" #include "utils.h" #include <stdio.h> @@ -287,11 +288,11 @@ double matrix_norm_frob(const Matrix *mat) return sqrt(matrix_trace(matrix_mult(matrix_transpose(mat), mat))); } -Matrix **matrix_LR(const Matrix *A, const Matrix *b) +Matrix **matrix_LR(const Matrix *A, const Vector *b) { assert(matrix_is_square(A) && "LR decomposition only works for square matrices"); assert(A->n == b->m && "Dimension mismatch"); - assert(matrix_is_colvec(b) && "Column vector b expected"); + assert(b->is_colvec && "Column vector b expected"); Matrix *L = matrix_id(A->n); Matrix *A_prev = matrix_copy(A); @@ -329,39 +330,39 @@ Matrix **matrix_LR(const Matrix *A, const Matrix *b) return result; } -Matrix *matrix_forwardel(const Matrix *L, const Matrix *b) +Vector *matrix_forwardel(const Matrix *L, const Vector *b) { assert(L->n == b->m && "Dimension mismatch"); - assert(matrix_is_colvec(b) && "Column vector b expected"); + assert(b->is_colvec && "Column vector b expected"); double sum; - Matrix *y = matrix_alloc(L->n, 1); + Vector *y = vector_alloc(L->n); for (size_t i = 0; i < y->m; ++i) { - sum = matrix_at(b, i, 0); + sum = vector_at(b, i); for (size_t j = 0; j < i; ++j) - sum -= matrix_at(L, i, j) * matrix_at(y, j, 0); - matrix_at(y, i, 0) = sum / matrix_at(L, i, i); + sum -= matrix_at(L, i, j) * vector_at(y, j); + vector_at(y, i) = sum / matrix_at(L, i, i); } return y; } -Matrix *matrix_backsubst(const Matrix *R, const Matrix *y) +Vector *matrix_backsubst(const Matrix *R, const Vector *y) { assert(R->n == y->m && "Dimension mismatch"); - assert(matrix_is_colvec(y) && "Column vector y expected"); - Matrix *x = matrix_alloc(R->n, 1); + assert(y->is_colvec && "Column vector y expected"); + Vector *x = vector_alloc(R->n); double sum; for (int i = x->m; i >= 0; --i) { - sum = matrix_at(y, i, 0); + sum = vector_at(y, i); for (size_t j = i + 1; j < x->m; ++j) - sum -= matrix_at(R, i, j) * matrix_at(x, j, 0); - matrix_at(x, i, 0) = sum / matrix_at(R, i, i); + sum -= matrix_at(R, i, j) * vector_at(x, j); + vector_at(x, i) = sum / matrix_at(R, i, i); } return x; |