Zephyr Scientific Library (zscilib)
vectors.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 Kevin Townsend (KTOWN)
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
22 #ifndef ZEPHYR_INCLUDE_ZSL_VECTORS_H_
23 #define ZEPHYR_INCLUDE_ZSL_VECTORS_H_
24 
25 #include <zsl/zsl.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
40 struct zsl_vec {
42  size_t sz;
45 };
46 
52 #define ZSL_VECTOR_DEF(name, n) \
53  zsl_real_t name ## _vec[n]; \
54  struct zsl_vec name = { \
55  .sz = n, \
56  .data = name ## _vec \
57  }
58  /* End of VEC_STRUCTS group */
60 
76 int zsl_vec_init(struct zsl_vec *v);
77 
93 int zsl_vec_from_arr(struct zsl_vec *v, zsl_real_t *a);
94 
103 int zsl_vec_copy(struct zsl_vec *vdest, struct zsl_vec *vsrc);
104  /* End of VEC_INIT group */
106 
127 int zsl_vec_get_subset(struct zsl_vec *v, size_t offset, size_t len,
128  struct zsl_vec *vsub);
129  /* End of VEC_SELECTION group */
131 
149 int zsl_vec_add(struct zsl_vec *v, struct zsl_vec *w, struct zsl_vec *x);
150 
160 int zsl_vec_sub(struct zsl_vec *v, struct zsl_vec *w, struct zsl_vec *x);
161 
169 int zsl_vec_neg(struct zsl_vec *v);
170 
181 int zsl_vec_sum(struct zsl_vec **v, size_t n, struct zsl_vec *w);
182 
191 int zsl_vec_scalar_add(struct zsl_vec *v, zsl_real_t s);
192 
201 int zsl_vec_scalar_mult(struct zsl_vec *v, zsl_real_t s);
202 
211 int zsl_vec_scalar_div(struct zsl_vec *v, zsl_real_t s);
212 
223 zsl_real_t zsl_vec_dist(struct zsl_vec *v, struct zsl_vec *w);
224 
237 int zsl_vec_dot(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *d);
238 
247 zsl_real_t zsl_vec_norm(struct zsl_vec *v);
248 
260 int zsl_vec_project(struct zsl_vec *u, struct zsl_vec *v, struct zsl_vec *w);
261 
274 int zsl_vec_to_unit(struct zsl_vec *v);
275 
299 int zsl_vec_cross(struct zsl_vec *v, struct zsl_vec *w, struct zsl_vec *c);
300 
309 
321 int zsl_vec_mean(struct zsl_vec **v, size_t n, struct zsl_vec *m);
322 
331 int zsl_vec_ar_mean(struct zsl_vec *v, zsl_real_t *m);
332 
340 int zsl_vec_rev(struct zsl_vec *v);
341 
352 int zsl_vec_zte(struct zsl_vec *v);
353  /* End of VEC_BASICMATH group */
355 
374 bool zsl_vec_is_equal(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t eps);
375 
383 bool zsl_vec_is_nonneg(struct zsl_vec *v);
384 
395 int zsl_vec_contains(struct zsl_vec *v, zsl_real_t val, zsl_real_t eps);
396 
406 int zsl_vec_sort(struct zsl_vec *v, struct zsl_vec *w);
407  /* End of VEC_COMPARE group */
409 
426 int zsl_vec_print(struct zsl_vec *v);
427 
428 // int zsl_vec_fprint(FILE *stream, zsl_vec *v);
429  /* End of VEC_DISPLAY group */
431 
432 #ifdef __cplusplus
433 }
434 #endif
435 
436 #endif /* ZEPHYR_INCLUDE_ZSL_VECTORS_H_ */
437  /* End of vectors group */
zsl_vec_get_subset
int zsl_vec_get_subset(struct zsl_vec *v, size_t offset, size_t len, struct zsl_vec *vsub)
Returns a subset of source vector 'v' in 'vsub'..
Definition: vectors.c:42
zsl_vec_mean
int zsl_vec_mean(struct zsl_vec **v, size_t n, struct zsl_vec *m)
Computes the component-wise mean of a set of identically-sized vectors.
Definition: vectors.c:312
zsl_vec_ar_mean
int zsl_vec_ar_mean(struct zsl_vec *v, zsl_real_t *m)
Computes the arithmetic mean of a vector.
Definition: vectors.c:334
zsl_vec_init
int zsl_vec_init(struct zsl_vec *v)
Definition: vectors.c:19
zsl_vec::data
zsl_real_t * data
Definition: vectors.h:44
zsl_vec::sz
size_t sz
Definition: vectors.h:42
zsl_vec_sum_of_sqrs
zsl_real_t zsl_vec_sum_of_sqrs(struct zsl_vec *v)
Computes the vector's sum of squares.
Definition: vectors.c:303
zsl_vec_norm
zsl_real_t zsl_vec_norm(struct zsl_vec *v)
Calculates the norm or absolute value of vector 'v' (the square root of the vector's dot product).
Definition: vectors.c:219
zsl_vec_dot
int zsl_vec_dot(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *d)
Computes the dot (aka scalar) product of two equal-length vectors (the sum of their component-wise pr...
Definition: vectors.c:199
zsl_vec_project
int zsl_vec_project(struct zsl_vec *u, struct zsl_vec *v, struct zsl_vec *w)
Calculates the projection of vector 'u' over vector 'v', placing the results in vector 'w'.
Definition: vectors.c:230
zsl_vec_print
int zsl_vec_print(struct zsl_vec *v)
Print the supplied vector using printf in a human-readable manner.
Definition: vectors.c:515
zsl_vec_sub
int zsl_vec_sub(struct zsl_vec *v, struct zsl_vec *w, struct zsl_vec *x)
Subtracts corresponding vector elements in 'v' and 'w', saving to 'x'.
Definition: vectors.c:88
zsl_vec_contains
int zsl_vec_contains(struct zsl_vec *v, zsl_real_t val, zsl_real_t eps)
Checks if vector v contains val, returning the number of occurences.
Definition: vectors.c:416
zsl_vec_sort
int zsl_vec_sort(struct zsl_vec *v, struct zsl_vec *w)
Sorts the values in vector v from smallest to largest using quicksort, and assigns the sorted output ...
Definition: vectors.c:475
zsl_vec_from_arr
int zsl_vec_from_arr(struct zsl_vec *v, zsl_real_t *a)
Converts an array of values into a vector. The number of elements in array 'a' must match the number ...
Definition: vectors.c:26
zsl_vec_rev
int zsl_vec_rev(struct zsl_vec *v)
Reverses the order of the entries in vector 'v'.
Definition: vectors.c:351
zsl_vec_sum
int zsl_vec_sum(struct zsl_vec **v, size_t n, struct zsl_vec *w)
Component-wise sum of a set of equal-length vectors.
Definition: vectors.c:113
zsl_vec_neg
int zsl_vec_neg(struct zsl_vec *v)
Negates the elements in vector 'v'.
Definition: vectors.c:104
zsl_vec_copy
int zsl_vec_copy(struct zsl_vec *vdest, struct zsl_vec *vsrc)
Copies the contents of vector 'vsrc' into vector 'vdest'.
Definition: vectors.c:33
zsl_vec_cross
int zsl_vec_cross(struct zsl_vec *v, struct zsl_vec *w, struct zsl_vec *c)
Computes the cross (or vector) product of two 3-vectors.
Definition: vectors.c:273
zsl_vec_dist
zsl_real_t zsl_vec_dist(struct zsl_vec *v, struct zsl_vec *w)
Calculates the distance between two vectors, which is equal to the norm of vector v - vector w.
Definition: vectors.c:181
zsl_vec_to_unit
int zsl_vec_to_unit(struct zsl_vec *v)
Converts (normalises) vector 'v' to a unit vector (a vector of magnitude or length 1)....
Definition: vectors.c:250
zsl.h
API header file for zscilib.
zsl_vec_zte
int zsl_vec_zte(struct zsl_vec *v)
Rearranges the input vector to place any zero-values at the end.
Definition: vectors.c:368
zsl_vec
Represents a vector.
Definition: vectors.h:40
zsl_real_t
double zsl_real_t
Definition: zsl.h:51
zsl_vec_add
int zsl_vec_add(struct zsl_vec *v, struct zsl_vec *w, struct zsl_vec *x)
Adds corresponding vector elements in 'v' and 'w', saving to 'x'.
Definition: vectors.c:71
zsl_vec_scalar_div
int zsl_vec_scalar_div(struct zsl_vec *v, zsl_real_t s)
Divide a vector by a scalar.
Definition: vectors.c:166
zsl_vec_is_equal
bool zsl_vec_is_equal(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t eps)
Checks if two vectors are identical in size and content.
Definition: vectors.c:387
zsl_vec_scalar_mult
int zsl_vec_scalar_mult(struct zsl_vec *v, zsl_real_t s)
Multiply a vector by a scalar.
Definition: vectors.c:155
zsl_vec_scalar_add
int zsl_vec_scalar_add(struct zsl_vec *v, zsl_real_t s)
Adds a scalar to each element in a vector.
Definition: vectors.c:144
zsl_vec_is_nonneg
bool zsl_vec_is_nonneg(struct zsl_vec *v)
Checks if all elements in vector v are >= zero.
Definition: vectors.c:405