summaryrefslogtreecommitdiff
path: root/vector.h
diff options
context:
space:
mode:
authorJasper2025-09-08 10:35:14 +0200
committerJasper2025-09-08 10:35:14 +0200
commit1386f6dc5024937c8c9c741efed9274d4861b76f (patch)
tree50c27e867e3551a820e90ac2bd0d43ca087625ef /vector.h
parentbd39c6ab8b87a90944afe271ce847ed0ce090c92 (diff)
Added vector struct
It's completely based on the existing matrix struct and its functions.
Diffstat (limited to 'vector.h')
-rw-r--r--vector.h67
1 files changed, 67 insertions, 0 deletions
diff --git a/vector.h b/vector.h
new file mode 100644
index 0000000..c10a765
--- /dev/null
+++ b/vector.h
@@ -0,0 +1,67 @@
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include <stddef.h>
+
+#include "matrix.h"
+
+#define vector_at(v, i) ((v)->xs[(i)])
+
+#define vector_loop(v, i) matrix_loop((v)->mat, i)
+
+#define vector_foreach(v, it) matrix_foreach((v)->mat, it)
+
+#define vector_foreach_idx(v, it, i) matrix_foreach_idx((v)->mat, it, i, _)
+
+#define VECTOR_DIM_MATCH(v1, v2) ((v1)->m == (v2)->m)
+
+typedef struct {
+ double *xs;
+ size_t m;
+ Matrix *mat;
+ bool is_colvec;
+} Vector;
+
+Vector *vector_alloc(size_t m);
+Vector *vector_alloc1(Matrix *mat);
+
+void vector_free(Vector *v);
+
+void vector_print(const Vector *v);
+
+Vector *vector_from_str(char *str);
+Vector *vector_from_arr(double arr[], size_t m);
+
+Vector *vector_unit(size_t m, size_t i);
+
+Vector *vector_const(size_t m, double x);
+void vector_const1(Vector *v, double x);
+
+Vector *vector_slice(const Vector *v, size_t i, size_t j);
+
+Vector *vector_copy(const Vector *v);
+void vector_copy1(Vector *u, const Vector *v);
+
+Vector *vector_transpose(const Vector *v);
+void vector_transpose1(Vector *v);
+
+Vector *vector_add(const Vector *u, const Vector *v);
+void vector_add1(Vector *u, const Vector *v);
+
+Vector *vector_scale(const Vector *v, double x);
+void vector_scale1(Vector *v, double x);
+
+double vector_norm_p(const Vector *x, size_t p);
+
+Vector *vector_sub(const Vector *u, const Vector *v);
+void vector_sub1(Vector *u, const Vector *v);
+
+Vector *vector_rand(size_t m, int bound_l, int bound_u);
+void vector_rand1(Vector *v, int bound_l, int bound_u);
+
+bool vector_eq(const Vector *u, const Vector *v, double tol);
+
+double vector_norm_p(const Vector *v, size_t p);
+double vector_norm_max(const Vector *v);
+
+#endif