Zephyr Scientific Library (zscilib)
Functions
Basic Math

Basic mathematical operations (add, sum, norm, etc.). More...

Functions

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'. More...
 
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'. More...
 
int zsl_vec_neg (struct zsl_vec *v)
 Negates the elements in vector 'v'. More...
 
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. More...
 
int zsl_vec_scalar_add (struct zsl_vec *v, zsl_real_t s)
 Adds a scalar to each element in a vector. More...
 
int zsl_vec_scalar_mult (struct zsl_vec *v, zsl_real_t s)
 Multiply a vector by a scalar. More...
 
int zsl_vec_scalar_div (struct zsl_vec *v, zsl_real_t s)
 Divide a vector by a scalar. More...
 
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. More...
 
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 products). The dot product is an indicator of the "agreement" between two vectors, or can be used to determine how far vector w deviates from vector v. More...
 
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). More...
 
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'. More...
 
int zsl_vec_to_unit (struct zsl_vec *v)
 Converts (normalises) vector 'v' to a unit vector (a vector of magnitude or length 1). This is accomploished by dividing each element by the it's norm. More...
 
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. More...
 
zsl_real_t zsl_vec_sum_of_sqrs (struct zsl_vec *v)
 Computes the vector's sum of squares. More...
 
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. More...
 
int zsl_vec_ar_mean (struct zsl_vec *v, zsl_real_t *m)
 Computes the arithmetic mean of a vector. More...
 
int zsl_vec_rev (struct zsl_vec *v)
 Reverses the order of the entries in vector 'v'. More...
 
int zsl_vec_zte (struct zsl_vec *v)
 Rearranges the input vector to place any zero-values at the end. More...
 

Detailed Description

Basic mathematical operations (add, sum, norm, etc.).

Function Documentation

◆ 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'.

Parameters
vThe first vector.
wThe second vector.
xThe output vector.
Returns
0 on success, -EINVAL if v and w are not equal length.

Definition at line 71 of file vectors.c.

Referenced by zsl_mtx_gram_schmidt(), and zsl_mtx_householder().

◆ zsl_vec_ar_mean()

int zsl_vec_ar_mean ( struct zsl_vec v,
zsl_real_t m 
)

Computes the arithmetic mean of a vector.

Parameters
vThe vector to use.
mThe arithmetic mean of vector v.
Returns
0 on success, otherwise an appropriate error code.

Definition at line 334 of file vectors.c.

Referenced by zsl_sta_mean().

◆ 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.

Parameters
vThe first 3-vector.
wThe second 3-vector.
cThe cross product 3-vector output.
Returns
0 on success, or -EINVAL if a non 3-vector is provided.

Given two linearly independent 3-vectors (v and w), the cross product, (v X w), is a vector that is perpendicular to both v and w and thus 'normal' to the plane containing them.

If two vectors have the same direction (or have the exact opposite direction from one another, i.e. are not linearly independent) or if either one has zero length, then their cross product is zero.

The norm of the cross product equals the area of a parallelogram with vectors v and w for sides.

For a discusson of geometric and algebraic applications, see: https://en.wikipedia.org/wiki/Cross_product

Definition at line 273 of file vectors.c.

◆ 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.

Parameters
vThe first vector.
wThe second vector.
Returns
The norm of vector v - vector w, or NAN is there was a size mismatch between vectors v and w.

Definition at line 181 of file vectors.c.

◆ 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 products). The dot product is an indicator of the "agreement" between two vectors, or can be used to determine how far vector w deviates from vector v.

Parameters
vThe first vector.
wThe second vector.
dThe dot product.
Returns
0 on success, or -EINVAL if vectors v and w aren't equal-length.

Definition at line 199 of file vectors.c.

Referenced by zsl_vec_project(), and zsl_vec_sum_of_sqrs().

◆ 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.

Parameters
vPointer to the array of vectors.
nThe number of vectors in 'v'.
mPointer to the output vector whose i'th element is the mean of the i'th elements of the input vectors.
Returns
0 on success, and -EINVAL if all vectors aren't identically sized.

Definition at line 312 of file vectors.c.

◆ zsl_vec_neg()

int zsl_vec_neg ( struct zsl_vec v)

Negates the elements in vector 'v'.

Parameters
vThe vector to negate.
Returns
0 on success, and non-zero error code on failure

Definition at line 104 of file vectors.c.

Referenced by zsl_mtx_eigenvectors().

◆ 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).

Parameters
vThe vector to calculate the norm of.
Returns
The norm of vector 'v'.

Definition at line 219 of file vectors.c.

Referenced by zsl_mtx_householder(), zsl_vec_dist(), and zsl_vec_to_unit().

◆ 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'.

Parameters
uPointer to the first input vector.
vPointer to the second input vector.
wPointer to the output vector where the proj. results are stored.
Returns
0 if everything executed correctly, otherwise an appropriate error code.

Definition at line 230 of file vectors.c.

Referenced by zsl_mtx_gram_schmidt().

◆ zsl_vec_rev()

int zsl_vec_rev ( struct zsl_vec v)

Reverses the order of the entries in vector 'v'.

Parameters
vThe vector to use.
Returns
0 on success, otherwise an appropriate error code.

Definition at line 351 of file vectors.c.

◆ 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.

Parameters
vThe vector to scale.
sThe scalar to add to each element.
Returns
0 on success, and non-zero error code on failure

Definition at line 144 of file vectors.c.

Referenced by zsl_sta_demean().

◆ zsl_vec_scalar_div()

int zsl_vec_scalar_div ( struct zsl_vec v,
zsl_real_t  s 
)

Divide a vector by a scalar.

Parameters
vThe vector to scale.
sThe scalar to divide all elements by.
Returns
0 on success, and non-zero error code on failure

Definition at line 166 of file vectors.c.

Referenced by zsl_mtx_householder().

◆ zsl_vec_scalar_mult()

int zsl_vec_scalar_mult ( struct zsl_vec v,
zsl_real_t  s 
)

Multiply a vector by a scalar.

Parameters
vThe vector to scale.
sThe scalar to multiply by.
Returns
0 on success, and non-zero error code on failure

Definition at line 155 of file vectors.c.

Referenced by zsl_mtx_householder(), zsl_vec_mean(), zsl_vec_project(), and zsl_vec_to_unit().

◆ 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'.

Parameters
vThe first vector.
wThe second vector.
xThe output vector.
Returns
0 on success, -EINVAL if v and w are not equal length.

Definition at line 88 of file vectors.c.

Referenced by zsl_mtx_gram_schmidt(), and zsl_vec_dist().

◆ 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.

Parameters
vPointer to the array of vectors.
nThe number of vectors in 'v'.
wPointer to the output vector containing the component-wise sum.
Returns
0 on success, -EINVAL if vectors in 'v' are no equal length, or -EINVAL if 'n' = 0.

Definition at line 113 of file vectors.c.

Referenced by zsl_vec_mean().

◆ zsl_vec_sum_of_sqrs()

zsl_real_t zsl_vec_sum_of_sqrs ( struct zsl_vec v)

Computes the vector's sum of squares.

Parameters
vThe vector to use.
Returns
The sum of the squares of vector 'v'.

Definition at line 303 of file vectors.c.

Referenced by zsl_vec_norm().

◆ 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). This is accomploished by dividing each element by the it's norm.

Note
Unit vectors are important since they have the ability to provide 'directional' information, without requiring a specific magnitude.
Parameters
vThe vector to convert to a unit vector.
Returns
0 on success, and non-zero error code on failure

Definition at line 250 of file vectors.c.

Referenced by zsl_mtx_cols_norm().

◆ zsl_vec_zte()

int zsl_vec_zte ( struct zsl_vec v)

Rearranges the input vector to place any zero-values at the end.

Parameters
vThe input vector to rearrange.
Warning
This function is destructive to 'v'!
Returns
0 if everything executed correctly, otherwise an appropriate error code.

Definition at line 368 of file vectors.c.

Referenced by zsl_mtx_eigenvalues().