From 39715d7f92843c3cc2950b89f917408ee4b7f8b4 Mon Sep 17 00:00:00 2001 From: Jasper Date: Fri, 12 Sep 2025 12:23:29 +0200 Subject: Vector doesn't keep track of being transposed, underlying matrix does --- matrix.c | 6 +++--- vector.c | 9 +++------ vector.h | 3 ++- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/matrix.c b/matrix.c index 222ad6d..8f16006 100644 --- a/matrix.c +++ b/matrix.c @@ -311,7 +311,7 @@ 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(b->is_colvec && "Column vector b expected"); + assert(vector_is_colvec(b) && "Column vector b expected"); Matrix *L = matrix_id(A->n); Matrix *A_prev = matrix_copy(A); @@ -351,7 +351,7 @@ Matrix **matrix_LR(const Matrix *A, const Vector *b) Vector *matrix_forwardel(const Matrix *L, const Vector *b) { assert(L->n == b->m && "Dimension mismatch"); - assert(b->is_colvec && "Column vector b expected"); + assert(vector_is_colvec(b) && "Column vector b expected"); double sum; Vector *y = vector_alloc(L->n); @@ -370,7 +370,7 @@ Vector *matrix_forwardel(const Matrix *L, const Vector *b) Vector *matrix_backsubst(const Matrix *R, const Vector *y) { assert(R->n == y->m && "Dimension mismatch"); - assert(y->is_colvec && "Column vector y expected"); + assert(vector_is_colvec(y) && "Column vector y expected"); double sum; Vector *x = vector_alloc(R->n); diff --git a/vector.c b/vector.c index 2f180c7..e0d7dea 100644 --- a/vector.c +++ b/vector.c @@ -16,7 +16,6 @@ Vector *vector_alloc(size_t m) v->mat = matrix_alloc(m, 1); v->xs = v->mat->xs; v->m = v->mat->m; - v->is_colvec = true; return v; } @@ -27,7 +26,6 @@ Vector *vector_alloc1(Matrix *mat) v->mat = mat; v->m = mat->m; v->xs = mat->xs; - v->is_colvec = true; return v; } @@ -51,8 +49,7 @@ void vector_free_many(size_t argcount, ...) void vector_print(const Vector *v) { - if (v->is_colvec) matrix_print(v->mat); - else matrix_print(matrix_transpose(v->mat)); + matrix_print(v->mat); } Vector *vector_from_str(char *str) @@ -115,14 +112,14 @@ void vector_copy1(Vector *u, const Vector *v) Vector *vector_transpose(const Vector *v) { Vector *w = vector_copy(v); - w->is_colvec = false; + vector_transpose1(w); return w; } void vector_transpose1(Vector *v) { - v->is_colvec = false; + matrix_transpose1(v->mat); } Vector *vector_add(const Vector *u, const Vector *v) diff --git a/vector.h b/vector.h index 099ed31..52fefd5 100644 --- a/vector.h +++ b/vector.h @@ -14,13 +14,14 @@ #define VECTOR_DIM_MATCH(v1, v2) ((v1)->m == (v2)->m) +#define vector_is_colvec(v) matrix_is_colvec((v)->mat) + typedef struct Matrix Matrix; typedef struct Vector { double *xs; size_t m; Matrix *mat; - bool is_colvec; } Vector; Vector *vector_alloc(size_t m); -- cgit v1.2.3