Robot Control Library
rc_test_vector.c
/**
* @example rc_test_vector.c
*
* @brief Tests the functions in rc_vector.h
*
*
* @author James Strawson
* @date 1/29/2018
*/
#include <stdio.h>
#include <rc/math.h>
#define LEN 4 // length of vector to test
int main()
{
int i;
printf("Testing vector functions\n\n");
// create ones vector
rc_vector_ones(&a, LEN);
printf("\nVector of ones:\n");
// create zero vector
rc_vector_zeros(&a, LEN);
printf("\nVector of Zeros:\n");
// create random vector
rc_vector_random(&a, LEN);
printf("\nRandom Vector:\n");
// duplicate vector
printf("\nDuplicate vector:\n");
// times scalar
printf("\nVector Times 2\n");
// sum
printf("\nsum a+2a\n");
// vector from array
double fib[] = {1,1,2,3,5,8};
printf("\nFibonacci vector from array:\n");
// fibonnaci vector
printf("\nFibonacci vector built-in function:\n");
// vector norm
printf("\n%7f Vector 1-norm\n", rc_vector_norm(a,1.0f));
printf("%7f Vector 2-norm\n", rc_vector_norm(a,2.0f));
printf("%7f Vector 3-norm\n", rc_vector_norm(a,3.0f));
// vector max min
i = rc_vector_max(a);
printf("%7f Vector Max at position %d\n", a.d[i],i);
i = rc_vector_min(a);
printf("%7f Vector Min at position %d\n", a.d[i],i);
// standard deviation
printf("%7f standard deviation\n", rc_vector_std_dev(a));
// mean
printf("%7f mean\n", rc_vector_mean(a));
// cleanup
printf("\nDONE\n");
return 0;
}
double rc_vector_mean(rc_vector_t v)
Returns the mean (average) of all values in vector v or -1.0f on error.
int rc_vector_max(rc_vector_t v)
Returns the index of the maximum value in v.
int rc_vector_sum_inplace(rc_vector_t *v1, rc_vector_t v2)
Adds vector v2 to v1 and leaves the result in v1.
int rc_vector_duplicate(rc_vector_t a, rc_vector_t *b)
Duplicates the contents of vector a and into a new vector b.
int rc_vector_random(rc_vector_t *v, int length)
Resizes vector v and fills with random numbers between -1.0 and 1.0.
int rc_vector_from_array(rc_vector_t *v, double *ptr, int length)
Resizes vector v and populates with values from specified array ptr.
int rc_vector_free(rc_vector_t *v)
Frees the memory allocated for vector v.
int rc_vector_ones(rc_vector_t *v, int length)
Resizes vector v and fills with ones.
double rc_vector_norm(rc_vector_t v, double p)
Returns the vector norm defined by sum(abs(v)^p)^(1/p), where p is any positive real value.
int rc_vector_times_scalar(rc_vector_t *v, double s)
Multiplies every entry in vector v by scalar s.
int rc_vector_zeros(rc_vector_t *v, int length)
Resizes vector v and fills with zeros.
int rc_vector_fibonnaci(rc_vector_t *v, int length)
Resizes vector v and fills with Fibonnaci sequence.
#define RC_VECTOR_INITIALIZER
Definition: vector.h:48
int rc_vector_min(rc_vector_t v)
Returns the index of the minimum value in v.
double rc_vector_std_dev(rc_vector_t v)
Returns the standard deviation of the values in a vector.
int rc_vector_print(rc_vector_t v)
Prints to stdout the contents of vector v in one line.
Struct containing the state of a vector and a pointer to dynamically allocated memory to hold its con...
Definition: vector.h:41
double * d
pointer to dynamically allocated data
Definition: vector.h:43