Zephyr Scientific Library (zscilib)
|
Go to the documentation of this file.
23 #if CONFIG_ZSL_BOUNDS_CHECKS
25 if (p > 50.0 || p < 0.0) {
32 size_t first_val = 0, count = 0;
41 for (
size_t i = 0; i < v->
sz; i++) {
42 if (w.data[i] >= per_l && w.data[i] <= per_h) {
61 for (
size_t i = 0; i < v->
sz; i++) {
65 #if CONFIG_ZSL_BOUNDS_CHECKS
72 for (
size_t i = 0; i < v->
sz; i++) {
73 if (w->
data[i] < 0.0) {
84 for (
size_t i = 0; i < v->
sz; i++) {
96 #if CONFIG_ZSL_BOUNDS_CHECKS
102 for (
size_t i = 0; i < v->
sz; i++) {
103 if (v->
data[i] < 0.0) {
108 for (
size_t i = 0; i < t->
sz; i++) {
122 for (
size_t i = 0; i < t->
sz; i++) {
123 for (
size_t g = 0; g < t->
sz; g++) {
125 vs.data[i] = v->
data[g];
130 for (
size_t j = 1; j < t->
sz; j++) {
131 sum += (ts.data[j] - ts.data[j - 1]) *
132 (vs.data[j] + vs.data[j - 1]) / 2.0;
135 *m = sum / (ts.data[(t->
sz - 1)] - ts.data[0]);
142 #if CONFIG_ZSL_BOUNDS_CHECKS
144 if (v->
sz != w->
sz) {
160 #if CONFIG_ZSL_BOUNDS_CHECKS
162 if (p > 100.0 || p < 0.0) {
176 *val = (w.data[(int)per] + w.data[(
int)per - 1]) / 2.;
178 *val = w.data[(int)per];
193 #if CONFIG_ZSL_BOUNDS_CHECKS
195 if (v->
sz != w->
sz) {
201 for (
size_t i = 0; i < v->
sz; i++) {
203 if (w->
data[i] < 0.0) {
209 if (
ZSL_ABS(sum - 1.0) > 1E-6) {
217 while (lsum <= 0.5) {
222 lsum -= w->
data[i - 1];
226 if (
ZSL_ABS(lsum - 0.5) < 1E-6) {
227 *m = (vsort.data[i - 1] + vsort.data[i - 2]) / 2.0;
229 *m = vsort.data[i - 1];
259 #if CONFIG_ZSL_BOUNDS_CHECKS
261 if (v->
sz != w->
sz) {
266 size_t i, j = 0, count, maxcount = 0;
273 for (i = 0; i < v->
sz; i++) {
276 if (count > maxcount) {
281 for (i = 0; i < v->
sz; i++) {
282 if (u.data[i] == maxcount) {
283 w_temp.data[j] = v->
data[i];
291 for (i = 0; i < j; i++) {
292 if (w_temp.data[i] >= 1E-5 || w_temp.data[i] <= 1E-5) {
294 w->
data[count] = w_temp.data[i];
315 *r = w.data[v->
sz - 1] - w.data[0];
322 #if CONFIG_ZSL_BOUNDS_CHECKS
332 for (
size_t i = 0; i < v->
sz; i++) {
347 for (
size_t i = 0; i < v->
sz; i++) {
363 for (
size_t i = 0; i < v->
sz; i++) {
364 *var += w.data[i] * w.data[i];
385 #if CONFIG_ZSL_BOUNDS_CHECKS
387 if (v->
sz != w->
sz) {
399 for (
size_t i = 0; i < v->
sz; i++) {
400 *c += v_dm.data[i] * w_dm.data[i];
410 #if CONFIG_ZSL_BOUNDS_CHECKS
422 for (
size_t i = 0; i < m->
sz_cols; i++) {
423 for (
size_t j = 0; j < m->
sz_cols; j++) {
437 #if CONFIG_ZSL_BOUNDS_CHECKS
439 if (x->
sz != y->
sz) {
446 sumx = sumy = sumxy = sumxx = sumyy = 0.0;
448 for (
size_t i = 0; i < x->
sz; i++) {
456 c->
slope = (x->
sz * sumxy - sumx * sumy) / (x->
sz * sumxx - sumx * sumx);
460 (x->
sz * sumyy - sumy * sumy));
465 #ifndef CONFIG_ZSL_SINGLE_PRECISION
470 #if CONFIG_ZSL_BOUNDS_CHECKS
489 for (
size_t i = 0; i < x->
sz_rows; i++) {
494 for (
size_t j = 0; j < x->
sz_cols; j++) {
523 for (
size_t g = 0; g < x->
sz_rows; g++) {
524 e_norm += emtx.data[g] * emtx.data[g];
525 ysum += (y->
data[g] - ymean) * (y->
data[g] - ymean);
528 *r = 1. - e_norm / ysum;
534 #ifndef CONFIG_ZSL_SINGLE_PRECISION
538 #if CONFIG_ZSL_BOUNDS_CHECKS
544 for (
size_t k = 0; k < w->
sz; k++) {
564 for (
size_t k = 0; k < w->
sz; k++) {
571 for (
size_t i = 0; i < x->
sz_rows; i++) {
576 for (
size_t j = 0; j < x->
sz_cols; j++) {
606 for (
size_t g = 0; g < x->
sz_rows; g++) {
607 e_norm += emtx.data[g] * emtx.data[g];
608 ysum += (y->
data[g] - ymean) * (y->
data[g] - ymean);
611 *r = 1. - e_norm / ysum;
617 #ifndef CONFIG_ZSL_SINGLE_PRECISION
620 #if CONFIG_ZSL_BOUNDS_CHECKS
632 for (
size_t i = 0; i < m->
sz_rows; i++) {
634 xv.data[0] = mv.data[0] * mv.data[0];
635 xv.data[1] = mv.data[1] * mv.data[1];
636 xv.data[2] = mv.data[2] * mv.data[2];
637 xv.data[3] = 2.0 * mv.data[0] * mv.data[1];
638 xv.data[4] = 2.0 * mv.data[0] * mv.data[2];
639 xv.data[5] = 2.0 * mv.data[1] * mv.data[2];
640 xv.data[6] = 2.0 * mv.data[0];
641 xv.data[7] = 2.0 * mv.data[1];
642 xv.data[8] = 2.0 * mv.data[2];
667 *err =
ZSL_ABS(*val - *exp_val);
674 *err =
ZSL_ABS(100. * *val - 100. * *exp_val) / *exp_val;
681 #if CONFIG_ZSL_BOUNDS_CHECKS
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'..
int zsl_mtx_scalar_mult_row_d(struct zsl_mtx *m, size_t i, zsl_real_t s)
Multiplies the elements of row 'i' in matrix 'm' by scalar 's'.
int zsl_mtx_get_row(struct zsl_mtx *m, size_t i, zsl_real_t *v)
Gets the contents of row 'i' from matrix 'm', assigning the array of data to 'v'.
int zsl_sta_weighted_mult_linear_reg(struct zsl_mtx *x, struct zsl_vec *y, struct zsl_vec *w, struct zsl_vec *b, zsl_real_t *r)
Calculates the coefficients (vector 'b') of the weighted multiple linear regression of the x_i values...
int zsl_sta_quart_range(struct zsl_vec *v, zsl_real_t *r)
Calculates the numeric difference between the third and the first quartiles of a vector v.
int zsl_sta_abs_err(zsl_real_t *val, zsl_real_t *exp_val, zsl_real_t *err)
Calculates the absolute error given a value and its expected value.
int zsl_vec_ar_mean(struct zsl_vec *v, zsl_real_t *m)
Computes the arithmetic mean of a vector.
int zsl_vec_init(struct zsl_vec *v)
int zsl_sta_mode(struct zsl_vec *v, struct zsl_vec *w)
Computes the mode or modes of a vector v.
Represents a m x n matrix, with data stored in row-major order.
int zsl_sta_median(struct zsl_vec *v, zsl_real_t *m)
Computes the median of a vector (the value separating the higher half from the lower half of a data s...
int zsl_sta_mean(struct zsl_vec *v, zsl_real_t *m)
Computes the arithmetic mean (average) of a vector.
int zsl_sta_rel_err(zsl_real_t *val, zsl_real_t *exp_val, zsl_real_t *err)
Calculates the relative error given a value and its expected value.
API header file for statistics in zscilib.
zsl_real_t correlation
The correlation coefficient, where closer to 1.0 is better.
int zsl_mtx_mult(struct zsl_mtx *ma, struct zsl_mtx *mb, struct zsl_mtx *mc)
Multiplies matrix 'ma' by 'mb', assigning the output to 'mc'. Matrices 'ma' and 'mb' must be compatib...
int zsl_sta_time_weighted_mean(struct zsl_vec *v, struct zsl_vec *t, zsl_real_t *m)
Computes the time-weighted arithmetic mean (average) of a positive data vector (v) and its time vecto...
int zsl_sta_median_abs_dev(struct zsl_vec *v, zsl_real_t *m)
Computes the median absolute deviation of a data vector v.
int zsl_mtx_get_col(struct zsl_mtx *m, size_t j, zsl_real_t *v)
Gets the contents of column 'j' from matrix 'm', assigning the array of data to 'v'.
zsl_real_t slope
The estimated slope.
int zsl_mtx_deter(struct zsl_mtx *m, zsl_real_t *d)
Calculates the determinant of the input square matrix 'm'.
int zsl_sta_quart(struct zsl_vec *v, zsl_real_t *q1, zsl_real_t *q2, zsl_real_t *q3)
Calculates the first, second and third quartiles of a vector v.
int zsl_mtx_init(struct zsl_mtx *m, zsl_mtx_init_entry_fn_t entry_fn)
Initialises matrix 'm' using the specified entry function to assign values.
int zsl_sta_data_range(struct zsl_vec *v, zsl_real_t *r)
Computes the difference between the greatest value and the lowest in a vector v.
int zsl_sta_weighted_mean(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *m)
Computes the weighted arithmetic mean (average) of a data vector (v) and a weight vector (w).
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.
int zsl_sta_trim_mean(struct zsl_vec *v, zsl_real_t p, zsl_real_t *m)
Computes the trimmed arithmetic mean (average) of a vector.
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 ...
int zsl_mtx_trans(struct zsl_mtx *ma, struct zsl_mtx *mb)
Transposes the matrix 'ma' into matrix 'mb'. Note that output matrix 'mb' must have 'ma->sz_rows' col...
int zsl_mtx_entry_fn_identity(struct zsl_mtx *m, size_t i, size_t j)
Sets the value to '1.0' if the entry is on the diagonal (row=col), otherwise '0.0'.
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 ...
int zsl_sta_linear_reg(struct zsl_vec *x, struct zsl_vec *y, struct zsl_sta_linreg *c)
Calculates the slope, intercept and correlation coefficient of the linear regression of two vectors,...
int zsl_vec_copy(struct zsl_vec *vdest, struct zsl_vec *vsrc)
Copies the contents of vector 'vsrc' into vector 'vdest'.
int zsl_sta_covar(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *c)
Computes the variance of two sets of data: v and w.
int zsl_sta_mult_linear_reg(struct zsl_mtx *x, struct zsl_vec *y, struct zsl_vec *b, zsl_real_t *r)
Calculates the coefficients (vector 'b') of the multiple linear regression of the x_i values (columns...
Simple linear regression coefficients.
int zsl_sta_mean_abs_dev(struct zsl_vec *v, zsl_real_t *m)
Computes the mean absolute deviation of a data vector v.
int zsl_mtx_set_row(struct zsl_mtx *m, size_t i, zsl_real_t *v)
Sets the contents of row 'i' in matrix 'm', assigning the values found in array 'v'.
int zsl_sta_demean(struct zsl_vec *v, struct zsl_vec *w)
Subtracts the mean of vector v from every component of the vector. The output vector w then has a zer...
#define ZSL_MATRIX_DEF(name, m, n)
#define ZSL_VECTOR_DEF(name, n)
API header file for zscilib.
int zsl_sta_weighted_median(struct zsl_vec *v, struct zsl_vec *w, zsl_real_t *m)
Computes the weighted median of a data vector (v) and a weight vector (w).
int zsl_sta_percentile(struct zsl_vec *v, zsl_real_t p, zsl_real_t *val)
Computes the given percentile of a vector.
int zsl_mtx_from_arr(struct zsl_mtx *m, zsl_real_t *a)
Converts an array of values into a matrix.
int zsl_mtx_set_col(struct zsl_mtx *m, size_t j, zsl_real_t *v)
Sets the contents of column 'j' in matrix 'm', assigning the values found in array 'v'.
int zsl_sta_std_dev(struct zsl_vec *v, zsl_real_t *s)
Computes the standard deviation of vector v.
int zsl_mtx_inv(struct zsl_mtx *m, struct zsl_mtx *mi)
Calculates the inverse of square matrix 'm'.
int zsl_sta_sta_err(struct zsl_vec *v, zsl_real_t *err)
Calculates the standard error of the mean of a sample (vector v).
int zsl_vec_scalar_add(struct zsl_vec *v, zsl_real_t s)
Adds a scalar to each element in a vector.
int zsl_mtx_sub(struct zsl_mtx *ma, struct zsl_mtx *mb, struct zsl_mtx *mc)
Subtracts matrices 'mb' from 'ma', assigning the output to 'mc'. Matrices 'ma', 'mb' and 'mc' must al...
zsl_real_t intercept
The estimated intercept.
int zsl_sta_covar_mtx(struct zsl_mtx *m, struct zsl_mtx *mc)
Calculates the nxn covariance matrix of a set of n vectors of the same length.
int zsl_sta_var(struct zsl_vec *v, zsl_real_t *var)
Computes the variance of a vector v (the average of the squared differences from the mean).
int zsl_sta_quad_fit(struct zsl_mtx *m, struct zsl_vec *b)
This function uses the least squares fitting method to compute the coefficients of a quadric surface ...