From a8609ccd901b1942e862c14205026d841e640add Mon Sep 17 00:00:00 2001 From: EliteTK Date: Thu, 10 Jul 2014 22:35:35 +0100 Subject: More stuff. --- vectors-testing.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 vectors-testing.c (limited to 'vectors-testing.c') diff --git a/vectors-testing.c b/vectors-testing.c new file mode 100644 index 0000000..dba96b2 --- /dev/null +++ b/vectors-testing.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define V_NEW(l) v_new(l, (double[]) + +typedef struct { + int length; + double *raw; +} Vec; + +double v_dot(Vec *, Vec *); +Vec *v_new(int, double *); + +int main(int argc, char **argv) +{ + Vec *v1 = v_new(3, (double[]){1, 2, 2}); + Vec *v2 = V_NEW(3){1, 2, 3}); + printf("Dot: %f\n", v_dot(v1, v2)); + printf("%d: %f, %f, %f.\n", v1->length, v1->raw[0], v1->raw[1], v1->raw[2]); + printf("%d: %f, %f, %f.\n", v2->length, v2->raw[0], v2->raw[1], v2->raw[2]); + + free(v1); + free(v2); +} + +Vec *v_new(int length, double *raw) +{ + Vec *vec = malloc(sizeof(Vec)); + vec->raw = malloc(length * sizeof(double)); + vec->length = length; + memcpy(vec->raw, raw, length * sizeof(double)); + return vec; +} + +double v_dot(Vec *v1, Vec *v2) +{ + int shortest = MIN(v1->length, v2->length); + + double dot = 0; + + for (int i = 0; i < shortest; i++) + dot += v1->raw[i] * v2->raw[i]; + + return dot; +} -- cgit v1.2.3-54-g00ecf