aboutsummaryrefslogtreecommitdiffstats
path: root/vectors-testing.c
diff options
context:
space:
mode:
authorEliteTK <tomasz.kramkowski@gmail.com>2014-07-10 22:35:35 +0100
committerEliteTK <tomasz.kramkowski@gmail.com>2014-07-10 22:35:35 +0100
commita8609ccd901b1942e862c14205026d841e640add (patch)
tree5eaf1d04d454bc45acfdf4c698b2860b91f48a00 /vectors-testing.c
parent922fe2f68c39a765896d274356c7c9dc4fb9cd73 (diff)
downloadc-stuff-a8609ccd901b1942e862c14205026d841e640add.tar.gz
c-stuff-a8609ccd901b1942e862c14205026d841e640add.tar.xz
c-stuff-a8609ccd901b1942e862c14205026d841e640add.zip
More stuff.
Diffstat (limited to 'vectors-testing.c')
-rw-r--r--vectors-testing.c47
1 files changed, 47 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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;
+}