|
|
|
@ -27,6 +27,16 @@ struct vec* do_on_vec_ip(struct vec * v, double (*func)(double)) {
@@ -27,6 +27,16 @@ struct vec* do_on_vec_ip(struct vec * v, double (*func)(double)) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct vec* |
|
|
|
|
new_vec_of(int num_dimensions, double value) { |
|
|
|
|
struct vec* new_vector = new_vec(num_dimensions); |
|
|
|
|
for (int i = 0; i < num_dimensions; i++) { |
|
|
|
|
new_vector->elements[i] = value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return new_vector; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Frees all memory related to the given vec. |
|
|
|
|
*/ |
|
|
|
@ -132,6 +142,32 @@ add_vec_ip(struct vec* a, struct vec* b)
@@ -132,6 +142,32 @@ add_vec_ip(struct vec* a, struct vec* b)
|
|
|
|
|
return a; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add vec a to vec b * scaleFactor and store in vec a. Returns a pointer to vec a. |
|
|
|
|
*/ |
|
|
|
|
struct vec* |
|
|
|
|
add_scaled_vec_ip(struct vec* a, struct vec* b, double scaleFactor) { |
|
|
|
|
int smallest_dimension = a->dimension < b->dimension ? a->dimension : b->dimension; |
|
|
|
|
int largest_dimension = a->dimension > b->dimension? a->dimension : b->dimension; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < smallest_dimension; i++) { |
|
|
|
|
a->elements[i] = a->elements[i] + b->elements[i] * scaleFactor; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Assume the smaller array is all 0s if the dimensions aren't equal
|
|
|
|
|
for (int i = smallest_dimension; i < largest_dimension; i++) { |
|
|
|
|
if (largest_dimension == a->dimension) { |
|
|
|
|
a->elements[i] = a->elements[i]; |
|
|
|
|
} else if (largest_dimension == b->dimension) { |
|
|
|
|
a->elements[i] = b->elements[i] * scaleFactor; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return a; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subtracts vec b from vec a and returns a reference to the difference vec. |
|
|
|
|
*/ |
|
|
|
@ -159,6 +195,33 @@ subtract_vec(struct vec* a, struct vec* b)
@@ -159,6 +195,33 @@ subtract_vec(struct vec* a, struct vec* b)
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Subtracts vec b from vec a and stores the result in vec a, returning a
|
|
|
|
|
* pointer to it as well. |
|
|
|
|
*/ |
|
|
|
|
struct vec*
|
|
|
|
|
subtract_vec_ip(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
int smallest_dimension = a->dimension < b->dimension ? a->dimension : b->dimension; |
|
|
|
|
int largest_dimension = a->dimension > b->dimension? a->dimension : b->dimension; |
|
|
|
|
|
|
|
|
|
// Perform subtraction up to where the dimensions of both vectors are equal.
|
|
|
|
|
for (int i = 0; i < smallest_dimension; i++) { |
|
|
|
|
a->elements[i] = a->elements[i] - b->elements[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Assume the smaller array is all 0s if the dimensions aren't equal
|
|
|
|
|
for (int i = smallest_dimension; i < largest_dimension; i++) { |
|
|
|
|
if (largest_dimension == a->dimension) { |
|
|
|
|
break; // the elements of a don't need to be changed
|
|
|
|
|
} else if (largest_dimension == b->dimension) { |
|
|
|
|
a->elements[i] = - b->elements[i]; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return a; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculates the magnitude of vec a. |
|
|
|
|
*/ |
|
|
|
@ -203,15 +266,14 @@ normalise_vec_ip(struct vec* a)
@@ -203,15 +266,14 @@ normalise_vec_ip(struct vec* a)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the dot product of vec a and vec b and return a reference to |
|
|
|
|
* the result. |
|
|
|
|
* Calculate the dot product of vec a and vec b. |
|
|
|
|
*/ |
|
|
|
|
struct vec*
|
|
|
|
|
double |
|
|
|
|
dot_product_vec(struct vec* a, struct vec* b)
|
|
|
|
|
{ |
|
|
|
|
struct vec* result = new_vec(a->dimension); |
|
|
|
|
double result = 0; |
|
|
|
|
for (int i = 0; i < a->dimension; i++) { |
|
|
|
|
result->elements[i] = a->elements[i] * b->elements[i]; |
|
|
|
|
result += a->elements[i] * b->elements[i]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
@ -231,6 +293,19 @@ scalar_multiply_vec(struct vec* a, double scalarFactor)
@@ -231,6 +293,19 @@ scalar_multiply_vec(struct vec* a, double scalarFactor)
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Multiply vec a by a scalarFactor and return vec a with the result. |
|
|
|
|
*/ |
|
|
|
|
struct vec*
|
|
|
|
|
scalar_multiply_vec_ip(struct vec* a, double scalarFactor)
|
|
|
|
|
{ |
|
|
|
|
for (int i = 0; i < a->dimension; i++) { |
|
|
|
|
a->elements[i] *= scalarFactor; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return a; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the distance between vec a and vec b by summing the square of |
|
|
|
|
* the differences of each component. |
|
|
|
@ -246,7 +321,9 @@ distance_vec(struct vec* a, struct vec* b)
@@ -246,7 +321,9 @@ distance_vec(struct vec* a, struct vec* b)
|
|
|
|
|
return sqrt(sum_of_differences); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
double vec_max(const struct vect *v) { |
|
|
|
|
double
|
|
|
|
|
vec_max(const struct vec *v)
|
|
|
|
|
{ |
|
|
|
|
double max = -DBL_MAX; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < v->dimension; i++) { |
|
|
|
@ -257,7 +334,9 @@ double vec_max(const struct vect *v) {
@@ -257,7 +334,9 @@ double vec_max(const struct vect *v) {
|
|
|
|
|
return max; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
double vec_min(const struct vec *v) { |
|
|
|
|
double
|
|
|
|
|
vec_min(const struct vec *v)
|
|
|
|
|
{ |
|
|
|
|
double min = DBL_MAX; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < v->dimension; i++) { |
|
|
|
|