commit 505d04b123cfb85d445042d88900ed96df6d1d4e
parent bbd789ecc0cafb7a97bfd3f0fd07dc068e1745f6
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date: Thu, 22 Apr 2021 12:09:06 +0200
sync arrays.[ch] from granular
Diffstat:
2 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/arrays.c b/arrays.c
@@ -8,11 +8,9 @@
void
check_magnitude(const char *func_name, int limit, int value)
{
- if (value < limit) {
- fprintf(stderr, "error: %s: input size %d is less than %d\n",
- func_name, value, limit);
- exit(1);
- }
+ if (value < limit)
+ errx(1, "%s: input size %d is less than %d\n",
+ func_name, value, limit);
}
/* Translate a i,j,k index in grid with dimensions nx, ny, nz into a
@@ -290,8 +288,7 @@ normalize(const double *in, const int n)
double *out;
check_magnitude(__func__, 1, n);
- if (!(out = calloc(n, sizeof(double))))
- err(1, "%s: out calloc", __func__);
+ out = empty(n);
copy_values(in, out, n);
max_val = max(out, n);
@@ -327,3 +324,27 @@ euclidean_distance(const double *a, const double *b, const int n)
return sqrt(out);
}
+
+double
+dot(const double *a, const double *b, const int n)
+{
+ int i;
+ double out = 0.0;
+
+ for (i = 0; i < n; i++)
+ out += a[i] * b[i];
+
+ return out;
+}
+
+double *
+cross(const double a[3], const double b[3])
+{
+ double *out = empty(3);
+
+ out[0] = a[1]*b[2] - a[2]*b[1];
+ out[1] = a[2]*b[0] - a[0]*b[2];
+ out[2] = a[0]*b[1] - a[1]*b[0];
+
+ return out;
+}
diff --git a/arrays.h b/arrays.h
@@ -1,8 +1,8 @@
-#include <stdio.h>
-
#ifndef ARRAYS_
#define ARRAYS_
+#include <stdio.h>
+
unsigned int idx3(
const unsigned int i, const unsigned int j, const unsigned int k,
const unsigned int nx, const unsigned int ny);
@@ -53,5 +53,7 @@ double * normalize(const double *in, const int n);
double euclidean_norm(const double *a, const int n);
double euclidean_distance(const double *a, const double *b, const int n);
+double dot(const double *a, const double *b, const int n);
+double * cross(const double a[3], const double b[3]);
#endif