diff options
author | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2018-07-06 14:51:29 -0400 |
---|---|---|
committer | Jaron Kent-Dobias <jaron@kent-dobias.com> | 2018-07-06 14:51:29 -0400 |
commit | 31f4244352b5e68eed770090419541d469f7f999 (patch) | |
tree | c8fbe8259618833cab736593e263f0984bdb01ab /lib/vector.h | |
parent | 2d8fcebf2f56efd1c3913ba49eaff6520ffdb33d (diff) | |
download | c++-31f4244352b5e68eed770090419541d469f7f999.tar.gz c++-31f4244352b5e68eed770090419541d469f7f999.tar.bz2 c++-31f4244352b5e68eed770090419541d469f7f999.zip |
split up some files
Diffstat (limited to 'lib/vector.h')
-rw-r--r-- | lib/vector.h | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/vector.h b/lib/vector.h new file mode 100644 index 0000000..c7f459c --- /dev/null +++ b/lib/vector.h @@ -0,0 +1,72 @@ + +#pragma once + +#include <stdlib.h> +#include <cmath> + +#include "types.h" + +template <q_t q, class T> +struct vector_t { T *x; }; + +template <q_t q, class T> +void init(vector_t <q, T> *ptr) { + ptr->x = (T *)calloc(q, sizeof(T)); + + ptr->x[0] = (T)1; +} + +template <q_t q, class T> +vector_t <q, T> copy (vector_t <q, T> v) { + vector_t <q, T> v_copy; + + v_copy.x = (T *)calloc(q, sizeof(T)); + + for (q_t i = 0; i < q; i++) { + v_copy.x[i] = v.x[i]; + } + + return v_copy; +} + +template <q_t q, class T> +void add (vector_t <q, T> v1, vector_t <q, T> v2) { + for (q_t i = 0; i < q; i++) { + v1.x[i] += v2.x[i]; + } +} + +template <q_t q, class T> +void subtract (vector_t <q, T> v1, vector_t <q, T> v2) { + for (q_t i = 0; i < q; i++) { + v1.x[i] -= v2.x[i]; + } +} + +template <q_t q, class T> +vector_t <q, T> scalar_multiple(v_t a, vector_t <q, T> v) { + vector_t <q, T> multiple; + multiple.x = (T *)malloc(q * sizeof(T)); + for (q_t i = 0; i < q; i++) { + multiple.x[i] = a * v.x[i]; + } + + return multiple; +} + +template <q_t q, class T> +T dot(vector_t <q, T> v1, vector_t <q, T> v2) { + T prod = 0; + + for (q_t i = 0; i < q; i++) { + prod += v1.x[i] * v2.x[i]; + } + + return prod; +} + +template <q_t q, class T> +void free_spin (vector_t <q, T> v) { + free(v.x); +} + |