Zephyr Scientific Library (zscilib)
|
Go to the documentation of this file.
54 for (
size_t i = 0; i < m->
sz_rows; i++) {
55 for (
size_t j = 0; j < m->
sz_cols; j++) {
57 if (entry_fn == NULL) {
60 rc = entry_fn(m, i, j);
83 #if CONFIG_ZSL_BOUNDS_CHECKS
101 #if CONFIG_ZSL_BOUNDS_CHECKS
115 #if CONFIG_ZSL_BOUNDS_CHECKS
132 for (
size_t j = 0; j < m->
sz_cols; j++) {
148 for (
size_t j = 0; j < m->
sz_cols; j++) {
164 for (
size_t i = 0; i < m->
sz_rows; i++) {
180 for (
size_t i = 0; i < m->
sz_rows; i++) {
270 for (
size_t i = 0; i < m->
sz_rows; i++) {
271 for (
size_t j = 0; j < m->
sz_cols; j++) {
289 #if CONFIG_ZSL_BOUNDS_CHECKS
309 if (mb->
data[i] == 0.0) {
352 #if CONFIG_ZSL_BOUNDS_CHECKS
359 for (
size_t i = 0; i < ma->
sz_rows; i++) {
360 for (
size_t j = 0; j < ma->
sz_cols; j++) {
363 rc = fn(ma, mb, mc, i, j);
389 #if CONFIG_ZSL_BOUNDS_CHECKS
396 for (
size_t x = 0; x < m->
sz_cols; x++) {
406 #if CONFIG_ZSL_BOUNDS_CHECKS
413 for (
size_t x = 0; x < m->
sz_cols; x++) {
436 #if CONFIG_ZSL_BOUNDS_CHECKS
453 for (
size_t i = 0; i < ma_copy.sz_rows; i++) {
454 for (
size_t j = 0; j < mb_copy.sz_cols; j++) {
455 mc->
data[j + i * mb_copy.sz_cols] = 0;
456 for (
size_t k = 0; k < ma_copy.sz_cols; k++) {
457 mc->
data[j + i * mb_copy.sz_cols] +=
458 ma_copy.data[k + i * ma_copy.sz_cols] *
459 mb_copy.data[j + k * mb_copy.sz_cols];
470 #if CONFIG_ZSL_BOUNDS_CHECKS
500 #if CONFIG_ZSL_BOUNDS_CHECKS
506 for (
size_t k = 0; k < m->
sz_cols; k++) {
516 #if CONFIG_ZSL_BOUNDS_CHECKS
525 for (
size_t i = 0; i < ma->
sz_rows; i++) {
541 #if CONFIG_ZSL_BOUNDS_CHECKS
579 #if CONFIG_ZSL_BOUNDS_CHECKS
590 for (
size_t i = 0; i < m->
sz_cols; i++) {
591 for (
size_t j = 0; j < m->
sz_cols; j++) {
593 if ((i + j) % 2 != 0) {
606 #ifndef CONFIG_ZSL_SINGLE_PRECISION
609 #if CONFIG_ZSL_BOUNDS_CHECKS
626 for (
size_t i = 0; i < m->
sz_rows; i++) {
651 #if CONFIG_ZSL_BOUNDS_CHECKS
664 for (
size_t k = 0; k < m->
sz_rows; k++) {
665 for (
size_t g = 0; g < m->
sz_rows; g++) {
666 if (k != i && g != j) {
703 for (
size_t i = 0; i < m->
sz_rows; i++) {
704 for (
size_t j = 0; j < m->
sz_rows; j++) {
721 #if CONFIG_ZSL_BOUNDS_CHECKS
763 #if CONFIG_ZSL_BOUNDS_CHECKS
786 for (
size_t g = 0; g < m->
sz_cols; g++) {
802 *d += dtmp * cur * sign;
829 if ((y >= 0 && y < epsilon) || (y <= 0 && y > -epsilon)) {
834 for (
size_t p = 0; p < mg->
sz_rows; p++) {
844 if ((x >= 1E-6) || (x <= -1E-6)) {
879 for (
size_t k = 0; k < m->
sz_rows; k++) {
887 if ((x >= 0 && x < epsilon) || (x <= 0 && x > -epsilon)) {
889 for (
size_t q = 0; q < m->
sz_rows; q++) {
891 if ((v[q] >= epsilon) || (v[q] <= -epsilon)) {
898 if (q < k && ((y >= epsilon)
899 || (y <= -epsilon))) {
928 for (
size_t t = 0; t < m->
sz_cols; t++) {
931 for (
size_t g = 0; g < t; g++) {
955 for (
size_t g = 0; g < m->
sz_cols; g++) {
985 if ((x >= 0 && x < epsilon) || (x <= 0 && x > -epsilon)) {
1020 #if CONFIG_ZSL_BOUNDS_CHECKS
1079 #if CONFIG_ZSL_BOUNDS_CHECKS
1118 #if CONFIG_ZSL_BOUNDS_CHECKS
1126 for (
size_t i = 0; i < m->
sz_rows; i++) {
1127 for (
size_t j = 0; j < m->
sz_rows; j++) {
1147 for (
size_t j = 0; j < m->
sz_cols; j++) {
1149 for (
size_t k = 0; k < j; k++) {
1156 for (
size_t i = j + 1; i < m->
sz_cols; i++) {
1158 for (
size_t k = 0; k < j; k++) {
1186 #if CONFIG_ZSL_BOUNDS_CHECKS
1204 for (
size_t i = 0; i < m->
sz_rows; i++) {
1206 for (
size_t j = 0; j < m->
sz_cols; j++) {
1214 if (col != 0.0 && row != 0.0) {
1219 while (col < row2) {
1226 while (col > row2) {
1231 if ((col + row) / col2 < 0.95 * sum) {
1235 for (
int k = 0; k < m->
sz_rows; k++) {
1258 if (hessenberg ==
true) {
1278 if (hessenberg ==
true) {
1288 if (v.data[0] < 0) {
1331 for (
size_t g = 0; g < (m->
sz_rows - 1); g++) {
1339 if (hessenberg ==
true) {
1353 if (hessenberg ==
true) {
1367 #ifndef CONFIG_ZSL_SINGLE_PRECISION
1383 for (
size_t g = 1; g <= iter; g++) {
1396 #ifndef CONFIG_ZSL_SINGLE_PRECISION
1431 for (
size_t g = 0; g < m->
sz_rows; g++) {
1458 for (
size_t g = 0; g < (m->
sz_rows - 1); g++) {
1461 if ((sdiag >= epsilon) || (sdiag <= -epsilon)) {
1469 v->
data[real] = diag;
1478 if ((sdiag >= epsilon) || (sdiag <= -epsilon)) {
1484 v->
data[real] = diag;
1502 #ifndef CONFIG_ZSL_SINGLE_PRECISION
1542 for (
size_t q = 0; q < m->
sz_rows; q++) {
1543 if ((k.data[q] >= epsilon) || (k.data[q] <= -epsilon)) {
1545 o.data[e_vals] = k.data[q];
1559 for (
size_t g = 0; g < e_vals; g++) {
1569 if (orthonormal ==
true) {
1572 for (
size_t h = 0; h < m->
sz_rows; h++) {
1574 if ((x >= 0.0 && x < epsilon) ||
1575 (x <= 0.0 && x > -epsilon)) {
1581 evec.sz_cols = count;
1582 evec2.sz_cols = count;
1586 for (
size_t h = 0; h < m->
sz_rows; h++) {
1588 if ((x >= 0.0 && x < epsilon) ||
1589 (x <= 0.0 && x > -epsilon)) {
1605 for (
size_t gi = 0; gi < count; gi++) {
1615 for (
size_t h = 0; h < m->
sz_rows; h++) {
1617 if ((x >= 0.0 && x < epsilon) ||
1618 (x <= 0.0 && x > -epsilon)) {
1633 for (
size_t s = 0; s < b; s++) {
1650 #ifndef CONFIG_ZSL_SINGLE_PRECISION
1653 struct zsl_mtx *v,
size_t iter)
1685 if (min < m->sz_cols) {
1694 for (
size_t g = 0; g < min; g++) {
1701 for (
size_t i = 0; i < min; i++) {
1710 if ((d >= 0.0 && d < epsilon) || (d <= 0.0 && d > -epsilon)) {
1723 for (
size_t f = min - pu; f < m->
sz_rows; f++) {
1732 #ifndef CONFIG_ZSL_SINGLE_PRECISION
1759 for (
size_t g = 0; g < min; g++) {
1764 if ((x < epsilon) || (x > -epsilon)) {
1788 if (m->
data[i] < min) {
1804 if (m->
data[i] > max) {
1822 for (
size_t _i = 0; _i < m->
sz_rows; _i++) {
1823 for (
size_t _j = 0; _j < m->
sz_cols; _j++) {
1843 for (
size_t _i = 0; _i < m->
sz_rows; _i++) {
1844 for (
size_t _j = 0; _j < m->
sz_cols; _j++) {
1869 return res == 0 ? true :
false;
1876 if (m->
data[i] < 0.0) {
1892 for (
size_t i = 0; i < m->
sz_rows; i++) {
1893 for (
size_t j = 0; j < m->
sz_cols; j++) {
1897 if (diff >= epsilon || diff <= -epsilon) {
1912 for (
size_t i = 0; i < m->
sz_rows; i++) {
1913 for (
size_t j = 0; j < m->
sz_cols; j++) {
1916 printf(
"Error reading (%zu,%zu)!\n", i, j);
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_augm_diag(struct zsl_mtx *m, struct zsl_mtx *maug)
Augments the input square matrix with additional rows and columns, based on the size 'diff' between m...
int zsl_mtx_mult_d(struct zsl_mtx *ma, struct zsl_mtx *mb)
Multiplies matrix 'ma' by 'mb', assigning the output to 'ma'. Matrices 'ma' and 'mb' must be compatib...
int zsl_mtx_svd(struct zsl_mtx *m, struct zsl_mtx *u, struct zsl_mtx *e, struct zsl_mtx *v, size_t iter)
Performs singular value decomposition, converting input matrix 'm' into matrices 'u',...
int zsl_mtx_sum_rows_scaled_d(struct zsl_mtx *m, size_t i, size_t j, zsl_real_t s)
This function takes the coefficients of row 'j' and multiplies them by scalar 's',...
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_set(struct zsl_mtx *m, size_t i, size_t j, zsl_real_t x)
Sets a single value at the specified row (i) and column (j).
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_mtx_norm_elem_d(struct zsl_mtx *m, struct zsl_mtx *mi, size_t i, size_t j)
Normalises elements in matrix m such that the element at position (i, j) is equal to 1....
int zsl_mtx_cholesky(struct zsl_mtx *m, struct zsl_mtx *l)
Calculates the Cholesky decomposition of a symmetric square matrix using the Cholesky–Crout algorithm...
int zsl_vec_init(struct zsl_vec *v)
int zsl_mtx_get(struct zsl_mtx *m, size_t i, size_t j, zsl_real_t *x)
Gets a single value from the specified row (i) and column (j).
int(* zsl_mtx_unary_fn_t)(struct zsl_mtx *m, size_t i, size_t j)
Function prototype called when applying a set of component-wise unary operations to a matrix via zsl_...
Represents a m x n matrix, with data stored in row-major order.
int zsl_mtx_qrd(struct zsl_mtx *m, struct zsl_mtx *q, struct zsl_mtx *r, bool hessenberg)
If 'hessenberg' is set to false, this function performs the QR decomposition, which is a factorisatio...
int zsl_mtx_max(struct zsl_mtx *m, zsl_real_t *x)
Traverses the matrix elements to find the maximum element value.
int(* zsl_mtx_init_entry_fn_t)(struct zsl_mtx *m, size_t i, size_t j)
Function prototype called when populating a matrix via zsl_mtx_init.
int zsl_mtx_reduce_iter(struct zsl_mtx *m, struct zsl_mtx *mred)
Reduces the number of rows/columns in the input square matrix 'm' to match the shape of 'mred',...
@ ZSL_MTX_BINARY_OP_GREAT
int zsl_mtx_sub_d(struct zsl_mtx *ma, struct zsl_mtx *mb)
Subtracts matrix 'mb' from 'ma', assigning the output to 'ma'. Matrices 'ma', and 'mb' must be identi...
int zsl_mtx_min(struct zsl_mtx *m, zsl_real_t *x)
Traverses the matrix elements to find the minimum element value.
int zsl_mtx_eigenvectors(struct zsl_mtx *m, struct zsl_mtx *mev, size_t iter, bool orthonormal)
Calcualtes the set of eigenvectors for input matrix 'm', using the specified number of iterations to ...
int zsl_mtx_unary_func(struct zsl_mtx *m, zsl_mtx_unary_fn_t fn)
Applies a unary function on every coefficient in matrix 'm', using the specified 'zsl_mtx_apply_unary...
enum zsl_mtx_binary_op zsl_mtx_binary_op_t
Component-wise binary operations.
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...
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).
int zsl_mtx_binary_func(struct zsl_mtx *ma, struct zsl_mtx *mb, struct zsl_mtx *mc, zsl_mtx_binary_fn_t fn)
Applies a component-wise binary operztion on every coefficient in symmetrical matrices 'ma' and 'mb',...
@ ZSL_MTX_BINARY_OP_EQUAL
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'.
int zsl_mtx_deter_3x3(struct zsl_mtx *m, zsl_real_t *d)
Calculates the determinant of the input 3x3 matrix 'm'.
int zsl_mtx_min_idx(struct zsl_mtx *m, size_t *i, size_t *j)
Traverses the matrix elements to find the (i,j) index of the minimum element value....
int zsl_mtx_gauss_reduc(struct zsl_mtx *m, struct zsl_mtx *mi, struct zsl_mtx *mg)
Given matrix 'm', puts the matrix into echelon form using Gauss-Jordan reduction.
int zsl_mtx_qrd_iter(struct zsl_mtx *m, struct zsl_mtx *mout, size_t iter)
Computes recursively the QR decompisition method to put the input square matrix into upper triangular...
enum zsl_mtx_unary_op zsl_mtx_unary_op_t
Component-wise unary operations.
int zsl_mtx_balance(struct zsl_mtx *m, struct zsl_mtx *mout)
Balances the square matrix 'm', a process in which the eigenvalues of the output matrix are the same ...
int zsl_mtx_deter(struct zsl_mtx *m, zsl_real_t *d)
Calculates the determinant of the input square matrix 'm'.
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'.
int zsl_mtx_add_d(struct zsl_mtx *ma, struct zsl_mtx *mb)
Adds matrices 'ma' and 'mb', assigning the output to 'ma'. Matrices 'ma', and 'mb' must be identicall...
int zsl_mtx_eigenvalues(struct zsl_mtx *m, struct zsl_vec *v, size_t iter)
Calculates the eigenvalues for input matrix 'm' using QR decomposition recursively....
int zsl_mtx_gauss_elim(struct zsl_mtx *m, struct zsl_mtx *mg, struct zsl_mtx *mi, size_t i, size_t j)
Given the element (i,j) in matrix 'm', this function performs gaussian elimination by adding row 'i' ...
int zsl_mtx_entry_fn_empty(struct zsl_mtx *m, size_t i, size_t j)
Assigns a zero-value to all entries in the matrix.
int zsl_mtx_vec_wedge(struct zsl_mtx *m, struct zsl_vec *v)
Calculates the wedge product of n-1 vectors of size n, which are the rows of the matrix 'm'....
int zsl_mtx_sum_rows_d(struct zsl_mtx *m, size_t i, size_t j)
Adds the values of row 'j' to row 'i' in matrix 'm'. This operation is destructive for row 'i'.
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'.
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_mtx_pinv(struct zsl_mtx *m, struct zsl_mtx *pinv, size_t iter)
Performs the pseudo-inverse (aka pinv or Moore-Penrose inverse) on input matrix 'm'.
int zsl_mtx_add(struct zsl_mtx *ma, struct zsl_mtx *mb, struct zsl_mtx *mc)
Adds matrices 'ma' and 'mb', assigning the output to 'mc'. Matrices 'ma', 'mb' and 'mc' must all be i...
int zsl_mtx_max_idx(struct zsl_mtx *m, size_t *i, size_t *j)
Traverses the matrix elements to find the (i,j) index of the maximum element value....
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.
@ ZSL_MTX_BINARY_OP_EXPON
int zsl_mtx_cols_norm(struct zsl_mtx *m, struct zsl_mtx *mnorm)
Updates the values of every column vector in input matrix 'm' to have unitary values.
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_mtx_entry_fn_random(struct zsl_mtx *m, size_t i, size_t j)
Sets the value to a random number between -1.0 and 1.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_mtx_binary_fn_t)(struct zsl_mtx *ma, struct zsl_mtx *mb, struct zsl_mtx *mc, size_t i, size_t j)
Function prototype called when applying a set of component-wise binary operations using a pair of sym...
int zsl_mtx_gauss_elim_d(struct zsl_mtx *m, struct zsl_mtx *mi, size_t i, size_t j)
Given the element (i,j) in matrix 'm', this function performs gaussian elimination by adding row 'i' ...
int zsl_mtx_copy(struct zsl_mtx *mdest, struct zsl_mtx *msrc)
Copies the contents of matrix 'msrc' into matrix 'mdest'.
int zsl_mtx_norm_elem(struct zsl_mtx *m, struct zsl_mtx *mn, struct zsl_mtx *mi, size_t i, size_t j)
Normalises elements in matrix m such that the element at position (i, j) is equal to 1....
bool zsl_mtx_is_sym(struct zsl_mtx *m)
Checks if the square input matrix is symmetric.
int zsl_vec_neg(struct zsl_vec *v)
Negates the elements in vector 'v'.
int zsl_vec_copy(struct zsl_vec *vdest, struct zsl_vec *vsrc)
Copies the contents of vector 'vsrc' into vector 'vdest'.
int zsl_mtx_binary_op(struct zsl_mtx *ma, struct zsl_mtx *mb, struct zsl_mtx *mc, zsl_mtx_binary_op_t op)
Applies a component-wise binary operation on every coefficient in symmetrical matrices 'ma' and 'mb',...
int zsl_mtx_gram_schmidt(struct zsl_mtx *m, struct zsl_mtx *mort)
Performs the Gram-Schmidt algorithm on the set of column vectors in matrix 'm'. This algorithm calcul...
int zsl_vec_to_unit(struct zsl_vec *v)
Converts (normalises) vector 'v' to a unit vector (a vector of magnitude or length 1)....
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_mtx_inv_3x3(struct zsl_mtx *m, struct zsl_mtx *mi)
Calculates the inverse of 3x3 matrix 'm'. If the determinant of 'm' is zero, an identity matrix will ...
int zsl_mtx_print(struct zsl_mtx *m)
Printf the supplied matrix using printf in a human-readable manner.
API header file for matrices in zscilib.
bool zsl_mtx_is_equal(struct zsl_mtx *ma, struct zsl_mtx *mb)
Checks if two matrices are identical in shape and content.
int zsl_mtx_unary_op(struct zsl_mtx *m, zsl_mtx_unary_op_t op)
Applies a unary operand on every coefficient in matrix 'm'.
#define ZSL_MATRIX_DEF(name, m, n)
bool zsl_mtx_is_notneg(struct zsl_mtx *m)
Checks if all elements in matrix m are >= zero.
#define ZSL_VECTOR_DEF(name, n)
API header file for zscilib.
int zsl_mtx_scalar_mult_d(struct zsl_mtx *m, zsl_real_t s)
Multiplies all elements in matrix 'm' by scalar value 's'.
int zsl_mtx_householder(struct zsl_mtx *m, struct zsl_mtx *h, bool hessenberg)
Calculates the householder reflection of 'm'. Used as part of QR decomposition. When 'hessenberg' is ...
int zsl_vec_zte(struct zsl_vec *v)
Rearranges the input vector to place any zero-values at the end.
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'.
@ ZSL_MTX_UNARY_OP_NEGATIVE
int zsl_mtx_adjoint(struct zsl_mtx *m, struct zsl_mtx *ma)
Calculates the ajoint matrix, based on the input square matrix 'm'.
int zsl_mtx_inv(struct zsl_mtx *m, struct zsl_mtx *mi)
Calculates the inverse of square matrix 'm'.
@ ZSL_MTX_BINARY_OP_NEQUAL
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'.
@ ZSL_MTX_UNARY_OP_INCREMENT
int zsl_vec_scalar_div(struct zsl_vec *v, zsl_real_t s)
Divide a vector by a scalar.
int zsl_mtx_adjoint_3x3(struct zsl_mtx *m, struct zsl_mtx *ma)
Calculates the ajoint matrix, based on the input 3x3 matrix 'm'.
int zsl_vec_scalar_mult(struct zsl_vec *v, zsl_real_t s)
Multiply a vector by a scalar.
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_MTX_UNARY_OP_DECREMENT
int zsl_mtx_reduce(struct zsl_mtx *m, struct zsl_mtx *mr, size_t i, size_t j)
Removes row 'i' and column 'j' from square matrix 'm', assigning the remaining elements in the matrix...