double MATHNS::angle(std::vector<double> u, std::vector<double> v) { double um = vector_mag(u); double vm = vector_mag(v); double dot = vector_dot(u,v); return acos(dot/(um*vm)); }
void test_vector_magnitudes() { printf("Testing vector magnitudes...\n"); TestStats stats = test_stats(); Vector test_vector; double computed; test_vector = zeroes(0); computed = vector_mag(&test_vector); assert_double(&stats, 0, computed, "Zero element vectors should have zero magnitude"); int i; for (i = -1; i < 3; i++) { test_vector = linear(1, i, 0); computed = vector_mag(&test_vector); assert_double(&stats, abs(i), computed, "One element vectors must have magnitude equal to their element."); } double quad_a[4] = {-.4, 0, .6, 1.2}; Vector quad = { 4, &quad_a[0] }; computed = vector_mag(&quad); assert_double(&stats, 1.4, computed, "Incorrectly computed vector magnitude"); print_state(&stats); }
static void compute_normal(point_t* points, int num_points, vector_t* n) { vector_t A, B; point_displacement(&points[0], &points[1], &A); point_displacement(&points[0], &points[2], &B); vector_cross(&A, &B, n); ASSERT(vector_mag(n) > 0.0); vector_normalize(n); }
Vector gravitational_acel (Body a, Body b) { Vector res; double force, tmp; force = G * b->bbody.mass; res = vector_zeros (a->bbody.position->size); vector_copy (res, a->bbody.position); vector_sub (res, b->bbody.position); tmp = vector_mag (res); if (tmp == 0) { fprintf (stderr, "gravitational_force: "); fprintf (stderr, "Division by zero.\n"); /* exit(EXIT_FAILURE); */ tmp = 0.00001; } force /= tmp*tmp*tmp; vector_scale (res, force); return res; }
static vector_t hat_grad(void* context, vector_t* y) { real_t h = *((real_t*)context); // Spatial extent of W. real_t D = vector_mag(y); static vector_t zero = {.x = 0.0, .y = 0.0, .z = 0.0}; vector_t g = {.x = -y->x/(D*h), .y = -y->y/(D*h), .z = -y->z/(D*h)}; return (D < h) ? g : zero; } static void hat_dtor(void* context) { polymec_free(context); } static point_weight_function_t* hat_function_new(real_t h) { real_t* Wh = polymec_malloc(sizeof(real_t)); *Wh = h; point_weight_function_vtable vtable = {.value = hat_W, .gradient = hat_grad, .dtor = hat_dtor}; return point_weight_function_new("Hat function", Wh, vtable); }
static real_t hat_W(void* context, vector_t* y) { real_t h = *((real_t*)context); // Spatial extent of W. real_t D = vector_mag(y); return (D < h) ? 1.0 - D/h : 0.0; }
Vector vector_unit(Vector v) { return vector_scale(v, 1. / vector_mag(v)); }