GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
la.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <grass/config.h>
#include <grass/gis.h>
#include <grass/glocale.h>
#include <grass/la.h>
Include dependency graph for la.c:

Go to the source code of this file.

Functions

mat_structG_matrix_init (int rows, int cols, int ldim)
 Initialize a matrix structure. More...
 
int G_matrix_zero (mat_struct *A)
 Clears (or resets) the matrix values to 0. More...
 
int G_matrix_set (mat_struct *A, int rows, int cols, int ldim)
 Set parameters for an initialized matrix. More...
 
mat_structG_matrix_copy (const mat_struct *A)
 Copy a matrix. More...
 
mat_structG_matrix_add (mat_struct *mt1, mat_struct *mt2)
 Adds two matricies. More...
 
mat_structG_matrix_subtract (mat_struct *mt1, mat_struct *mt2)
 Subtract two matricies. More...
 
mat_structG_matrix_scalar_mul (double scalar, mat_struct *matrix, mat_struct *out)
 Calculates the scalar-matrix multiplication. More...
 
mat_structG_matrix_scale (mat_struct *mt1, const double c)
 Scale a matrix by a scalar value. More...
 
mat_structG__matrix_add (mat_struct *mt1, mat_struct *mt2, const double c1, const double c2)
 General add/subtract/scalar multiply routine. More...
 
mat_structG_matrix_product (mat_struct *mt1, mat_struct *mt2)
 Returns product of two matricies. More...
 
mat_structG_matrix_transpose (mat_struct *mt)
 Transpose a matrix. More...
 
int G_matrix_LU_solve (const mat_struct *mt1, mat_struct **xmat0, const mat_struct *bmat, mat_type mtype)
 Solve a general system A.X = B. More...
 
mat_structG_matrix_inverse (mat_struct *mt)
 Returns the matrix inverse. More...
 
void G_matrix_free (mat_struct *mt)
 Free up allocated matrix. More...
 
void G_matrix_print (mat_struct *mt)
 Print out a matrix. More...
 
int G_matrix_set_element (mat_struct *mt, int rowval, int colval, double val)
 Set the value of the (i, j)th element. More...
 
double G_matrix_get_element (mat_struct *mt, int rowval, int colval)
 Retrieve value of the (i,j)th element. More...
 
vec_structG_matvect_get_column (mat_struct *mt, int col)
 Retrieve a column of the matrix to a vector structure. More...
 
vec_structG_matvect_get_row (mat_struct *mt, int row)
 Retrieve a row of the matrix to a vector structure. More...
 
int G_matvect_extract_vector (mat_struct *mt, vtype vt, int indx)
 Convert matrix to vector. More...
 
int G_matvect_retrieve_matrix (vec_struct *vc)
 Revert a vector to matrix. More...
 
vec_structG_matvect_product (mat_struct *A, vec_struct *b, vec_struct *out)
 Calculates the matrix-vector product. More...
 
vec_structG_vector_init (int cells, int ldim, vtype vt)
 Initialize a vector structure. More...
 
void G_vector_free (vec_struct *v)
 Free an allocated vector structure. More...
 
vec_structG_vector_sub (vec_struct *v1, vec_struct *v2, vec_struct *out)
 Subtract two vectors. More...
 
int G_vector_set (vec_struct *A, int cells, int ldim, vtype vt, int vindx)
 Set parameters for vector structure. More...
 
double G_vector_norm_euclid (vec_struct *vc)
 Calculates euclidean norm. More...
 
double G_vector_norm_maxval (vec_struct *vc, int vflag)
 Calculates maximum value. More...
 
double G_vector_norm1 (vec_struct *vc)
 Calculates the 1-norm of a vector. More...
 
vec_structG_vector_product (vec_struct *v1, vec_struct *v2, vec_struct *out)
 Calculates the vector product. More...
 
vec_structG_vector_copy (const vec_struct *vc1, int comp_flag)
 Returns a vector copied from vc1. Underlying structure is preserved unless DO_COMPACT flag. More...
 
int G_matrix_read (FILE *fp, mat_struct *out)
 Read a matrix from a file stream. More...
 
mat_structG_matrix_resize (mat_struct *in, int rows, int cols)
 Resizes a matrix. More...
 
int G_matrix_stdin (mat_struct *out)
 
int G_matrix_eigen_sort (vec_struct *d, mat_struct *m)
 Sort eigenvectors according to eigenvalues. More...
 

Function Documentation

◆ G__matrix_add()

mat_struct* G__matrix_add ( mat_struct mt1,
mat_struct mt2,
const double  c1,
const double  c2 
)

General add/subtract/scalar multiply routine.

General add/subtract/scalar multiply routine. c2 may be zero, but c1 must be non-zero.

Parameters
mt1
mt2
c1
c2
Returns
mat_struct

Definition at line 278 of file la.c.

References _, matrix_::cols, G_matrix_init(), G_warning(), matrix_::is_init, matrix_::ldim, NULL, matrix_::rows, and matrix_::vals.

Referenced by G_matrix_add(), and G_matrix_subtract().

◆ G_matrix_add()

mat_struct* G_matrix_add ( mat_struct mt1,
mat_struct mt2 
)

Adds two matricies.

Adds two matricies mt1 and mt2 and returns a resulting matrix. The return structure is automatically initialized.

Parameters
mt1
mt2
Returns
mat_struct

Definition at line 178 of file la.c.

References G__matrix_add().

◆ G_matrix_copy()

mat_struct* G_matrix_copy ( const mat_struct A)

Copy a matrix.

Copy matrix A by exactly duplicating its contents.

Parameters
A
Returns
mat_struct

Definition at line 145 of file la.c.

Referenced by G_matrix_LU_solve().

◆ G_matrix_eigen_sort()

int G_matrix_eigen_sort ( vec_struct d,
mat_struct m 
)

Sort eigenvectors according to eigenvalues.

Sort eigenvectors according to eigenvalues. Returns 0.

Parameters
d
m
Returns
int

Definition at line 1648 of file la.c.

◆ G_matrix_free()

void G_matrix_free ( mat_struct mt)

Free up allocated matrix.

Free up allocated matrix.

Parameters
mt
Returns
void

Definition at line 667 of file la.c.

References G_free(), matrix_::is_init, and matrix_::vals.

Referenced by G_matrix_inverse(), and G_matrix_LU_solve().

◆ G_matrix_get_element()

double G_matrix_get_element ( mat_struct mt,
int  rowval,
int  colval 
)

Retrieve value of the (i,j)th element.

Retrieve the value of the (i, j)th element to a double value. Index values are C-like ie. zero-based. Note: Does currently not set an error flag for bounds checking.

Parameters
mt
rowval
colval
Returns
double

Definition at line 759 of file la.c.

References matrix_::ldim, and matrix_::vals.

Referenced by G_matrix_print(), G_matrix_resize(), G_matrix_scalar_mul(), G_matvect_get_column(), G_matvect_get_row(), G_vector_norm1(), G_vector_product(), and G_vector_sub().

◆ G_matrix_init()

mat_struct* G_matrix_init ( int  rows,
int  cols,
int  ldim 
)

Initialize a matrix structure.

Initialize a matrix structure

Parameters
rows
cols
ldim
Returns
mat_struct

Definition at line 56 of file la.c.

References _, matrix_::cols, G_calloc, G_malloc, G_warning(), matrix_::is_init, matrix_::ldim, MATRIX_, NULL, matrix_::rows, matrix_::type, matrix_::v_indx, and matrix_::vals.

Referenced by G__matrix_add(), G_matrix_inverse(), G_matrix_product(), G_matrix_resize(), G_matrix_scalar_mul(), and G_matrix_transpose().

◆ G_matrix_inverse()

mat_struct* G_matrix_inverse ( mat_struct mt)

Returns the matrix inverse.

Calls G_matrix_LU_solve() to obtain matrix inverse using LU decomposition. Returns NULL on failure.

Parameters
mt
Returns
mat_struct

Definition at line 608 of file la.c.

References _, matrix_::cols, G_matrix_free(), G_matrix_init(), G_matrix_LU_solve(), G_warning(), if(), matrix_::ldim, NONSYM, NULL, matrix_::rows, and matrix_::vals.

◆ G_matrix_LU_solve()

int G_matrix_LU_solve ( const mat_struct mt1,
mat_struct **  xmat0,
const mat_struct bmat,
mat_type  mtype 
)

Solve a general system A.X = B.

Solve a general system A.X = B, where A is a NxN matrix, X and B are NxC matrices, and we are to solve for C arrays in X given B. Uses LU decomposition.
Links to LAPACK function dgesv_() and similar to perform the core routine. (By default solves for a general non-symmetric matrix.)
mtype is a flag to indicate what kind of matrix (real/complex, Hermitian, symmetric, general etc.) is used (NONSYM, SYM, HERMITIAN).
Warning: NOT YET COMPLETE: only some solutions' options available. Now, only general real matrix is supported.

Parameters
mt1
xmat0
bmat
mtype
Returns
int

Definition at line 480 of file la.c.

References _, matrix_::cols, f77_dgesv, G_free(), G_malloc, G_matrix_copy(), G_matrix_free(), G_warning(), matrix_::is_init, matrix_::ldim, NONSYM, NULL, matrix_::rows, and matrix_::vals.

Referenced by G_matrix_inverse().

◆ G_matrix_print()

void G_matrix_print ( mat_struct mt)

Print out a matrix.

Print out a representation of the matrix to standard output.

Parameters
mt
Returns
void

Definition at line 687 of file la.c.

References matrix_::cols, G_matrix_get_element(), G_message(), and matrix_::rows.

◆ G_matrix_product()

mat_struct* G_matrix_product ( mat_struct mt1,
mat_struct mt2 
)

Returns product of two matricies.

Returns a matrix with the product of matrix mt1 and mt2. The return matrix is automatically initialized.

Parameters
mt1
mt2
Returns
mat_struct

Definition at line 356 of file la.c.

References _, matrix_::cols, f77_dgemm, G_matrix_init(), G_warning(), matrix_::is_init, matrix_::ldim, NULL, matrix_::rows, and matrix_::vals.

◆ G_matrix_read()

int G_matrix_read ( FILE *  fp,
mat_struct out 
)

Read a matrix from a file stream.

Populates matrix structure out with matrix read from file stream fp. Matrix out is automatically initialized. Returns -1 on error and 0 on success.

Parameters
fp
out
Returns
int

Definition at line 1542 of file la.c.

References _, G_getl(), G_matrix_set(), G_matrix_set_element(), and G_warning().

Referenced by G_matrix_stdin().

◆ G_matrix_resize()

mat_struct* G_matrix_resize ( mat_struct in,
int  rows,
int  cols 
)

Resizes a matrix.

Resizes a matrix

Parameters
A
rows
cols
Returns
mat_struct

Definition at line 1596 of file la.c.

References matrix_::cols, G_matrix_get_element(), G_matrix_init(), G_matrix_set_element(), and matrix_::rows.

Referenced by G_matrix_scalar_mul().

◆ G_matrix_scalar_mul()

mat_struct* G_matrix_scalar_mul ( double  scalar,
mat_struct matrix,
mat_struct out 
)

Calculates the scalar-matrix multiplication.

Calculates the scalar-matrix multiplication

Parameters
scalar
A
Returns
mat_struct

Definition at line 214 of file la.c.

References _, matrix_::cols, G_matrix_get_element(), G_matrix_init(), G_matrix_resize(), G_matrix_set_element(), G_warning(), NULL, matrix_::rows, and Key_Value::value.

◆ G_matrix_scale()

mat_struct* G_matrix_scale ( mat_struct mt1,
const double  c 
)

Scale a matrix by a scalar value.

Scales matrix mt1 by scalar value c. The resulting matrix is automatically initialized.

Parameters
mt1
c
Returns
mat_struct

Definition at line 257 of file la.c.

◆ G_matrix_set()

int G_matrix_set ( mat_struct A,
int  rows,
int  cols,
int  ldim 
)

Set parameters for an initialized matrix.

Set parameters for matrix A that is allocated, but not yet fully initialized. Is an alternative to G_matrix_init().

Parameters
A
rows
cols
ldim
Returns
int

Definition at line 114 of file la.c.

Referenced by G_matrix_read().

◆ G_matrix_set_element()

int G_matrix_set_element ( mat_struct mt,
int  rowval,
int  colval,
double  val 
)

Set the value of the (i, j)th element.

Set the value of the (i, j)th element to a double value. Index values are C-like ie. zero-based. The row number is given first as is conventional. Returns -1 if the accessed cell is outside the bounds.

Parameters
mt
rowval
colval
val
Returns
int

Definition at line 726 of file la.c.

References _, matrix_::cols, G_warning(), matrix_::is_init, matrix_::ldim, matrix_::rows, and matrix_::vals.

Referenced by G_matrix_read(), G_matrix_resize(), G_matrix_scalar_mul(), G_matvect_get_column(), G_matvect_get_row(), G_vector_product(), and G_vector_sub().

◆ G_matrix_stdin()

int G_matrix_stdin ( mat_struct out)

Definition at line 1630 of file la.c.

References G_matrix_read().

◆ G_matrix_subtract()

mat_struct* G_matrix_subtract ( mat_struct mt1,
mat_struct mt2 
)

Subtract two matricies.

Subtracts two matricies mt1 and mt2 and returns a resulting matrix. The return matrix is automatically initialized.

Parameters
mt1
mt2
Returns
mat_struct

Definition at line 197 of file la.c.

References G__matrix_add().

◆ G_matrix_transpose()

mat_struct* G_matrix_transpose ( mat_struct mt)

Transpose a matrix.

Transpose matrix m1 by creating a new one and populating with transposed elements. The return matrix is automatically initialized.

Parameters
mt
Returns
mat_struct

Definition at line 410 of file la.c.

References matrix_::cols, G_matrix_init(), matrix_::ldim, matrix_::rows, and matrix_::vals.

◆ G_matrix_zero()

int G_matrix_zero ( mat_struct A)

Clears (or resets) the matrix values to 0.

Parameters
A
Returns
0 on error; 1 on success

Definition at line 88 of file la.c.

◆ G_matvect_extract_vector()

int G_matvect_extract_vector ( mat_struct mt,
vtype  vt,
int  indx 
)

Convert matrix to vector.

Convert the matrix mt to a vector structure. The vtype, vt, is RVEC or CVEC which specifies a row vector or column vector. The index, indx, indicates the row/column number (zero based).

Parameters
mt
vt
indx
Returns
int

Definition at line 866 of file la.c.

References _, matrix_::cols, COLVEC_, CVEC, G_warning(), matrix_::rows, ROWVEC_, RVEC, matrix_::type, and matrix_::v_indx.

◆ G_matvect_get_column()

vec_struct* G_matvect_get_column ( mat_struct mt,
int  col 
)

Retrieve a column of the matrix to a vector structure.

Retrieve a column of matrix mt to a returning vector structure

Parameters
mt
col
Returns
vec_struct

Definition at line 782 of file la.c.

References _, matrix_::cols, CVEC, G_matrix_get_element(), G_matrix_set_element(), G_vector_init(), G_warning(), matrix_::is_init, matrix_::ldim, NULL, and matrix_::rows.

◆ G_matvect_get_row()

vec_struct* G_matvect_get_row ( mat_struct mt,
int  row 
)

Retrieve a row of the matrix to a vector structure.

Retrieves a row from matrix mt and returns it in a vector structure.

Parameters
mt
row
Returns
vec_struct

Definition at line 823 of file la.c.

References _, matrix_::cols, G_matrix_get_element(), G_matrix_set_element(), G_vector_init(), G_warning(), matrix_::is_init, matrix_::ldim, NULL, and RVEC.

◆ G_matvect_product()

vec_struct* G_matvect_product ( mat_struct A,
vec_struct b,
vec_struct out 
)

Calculates the matrix-vector product.

Calculates the product of a matrix and a vector

Parameters
A
b
Returns
vec_struct

Definition at line 941 of file la.c.

◆ G_matvect_retrieve_matrix()

int G_matvect_retrieve_matrix ( vec_struct vc)

Revert a vector to matrix.

Revert vector vc to a matrix.

Parameters
vc
Returns
int

Definition at line 916 of file la.c.

References MATRIX_, matrix_::type, and matrix_::v_indx.

◆ G_vector_copy()

vec_struct* G_vector_copy ( const vec_struct vc1,
int  comp_flag 
)

Returns a vector copied from vc1. Underlying structure is preserved unless DO_COMPACT flag.

Parameters
vc1
comp_flag
Returns
vec_struct

Definition at line 1429 of file la.c.

References _, matrix_::cols, COLVEC_, DO_COMPACT, G_calloc, G_malloc, G_warning(), matrix_::is_init, matrix_::ldim, NO_COMPACT, NULL, matrix_::rows, ROWVEC_, matrix_::type, matrix_::v_indx, and matrix_::vals.

◆ G_vector_free()

void G_vector_free ( vec_struct v)

Free an allocated vector structure.

Free an allocated vector structure.

Parameters
v
Returns
void

Definition at line 1042 of file la.c.

References G_free(), matrix_::is_init, and matrix_::vals.

◆ G_vector_init()

vec_struct* G_vector_init ( int  cells,
int  ldim,
vtype  vt 
)

Initialize a vector structure.

Returns an initialized vector structure with cell cells, of dimension ldim, and of type vt.

Parameters
cells
ldim
vt
Returns
vec_struct

Definition at line 995 of file la.c.

References matrix_::cols, COLVEC_, CVEC, G_calloc, G_malloc, G_warning(), matrix_::is_init, matrix_::ldim, NULL, matrix_::rows, ROWVEC_, RVEC, matrix_::type, matrix_::v_indx, and matrix_::vals.

Referenced by G_matvect_get_column(), and G_matvect_get_row().

◆ G_vector_norm1()

double G_vector_norm1 ( vec_struct vc)

Calculates the 1-norm of a vector.

Calculates the 1-norm of a vector

Parameters
vc
Returns
double

Definition at line 1318 of file la.c.

References _, matrix_::cols, G_matrix_get_element(), G_warning(), if(), matrix_::is_init, matrix_::rows, ROWVEC_, matrix_::type, and matrix_::v_indx.

◆ G_vector_norm_euclid()

double G_vector_norm_euclid ( vec_struct vc)

Calculates euclidean norm.

Calculates the euclidean norm of a row or column vector, using BLAS routine dnrm2_().

Parameters
vc
Returns
double

Definition at line 1192 of file la.c.

References _, matrix_::cols, f77_dnrm2, G_fatal_error(), if(), matrix_::is_init, matrix_::ldim, matrix_::rows, ROWVEC_, matrix_::type, matrix_::v_indx, and matrix_::vals.

◆ G_vector_norm_maxval()

double G_vector_norm_maxval ( vec_struct vc,
int  vflag 
)

Calculates maximum value.

Calculates the maximum value of a row or column vector. The vflag setting defines which value to be calculated: vflag: 1 Indicates maximum value
-1 Indicates minimum value
0 Indicates absolute value [???]

Parameters
vc
vflag
Returns
double

Definition at line 1243 of file la.c.

References _, matrix_::cols, G_fatal_error(), if(), matrix_::is_init, matrix_::ldim, MAX_ABS, MAX_NEG, MAX_POS, matrix_::rows, ROWVEC_, matrix_::type, matrix_::v_indx, and matrix_::vals.

◆ G_vector_product()

vec_struct* G_vector_product ( vec_struct v1,
vec_struct v2,
vec_struct out 
)

Calculates the vector product.

Calculates the vector product of two vectors

Parameters
v1
v2
Returns
vec_struct

Definition at line 1355 of file la.c.

References _, matrix_::cols, COLVEC_, G_matrix_get_element(), G_matrix_set_element(), G_warning(), if(), matrix_::is_init, MATRIX_, NULL, matrix_::rows, ROWVEC_, matrix_::type, matrix_::v_indx, and matrix_::vals.

◆ G_vector_set()

int G_vector_set ( vec_struct A,
int  cells,
int  ldim,
vtype  vt,
int  vindx 
)

Set parameters for vector structure.

Set parameters for a vector structure that is allocated but not yet initialised fully. The vtype is RVEC or CVEC which specifies a row vector or column vector.

Parameters
A
cells
ldim
vt
vindx
Returns
int

Definition at line 1139 of file la.c.

◆ G_vector_sub()

vec_struct* G_vector_sub ( vec_struct v1,
vec_struct v2,
vec_struct out 
)

Subtract two vectors.

Subtracts two vectors, v1 and v2, and returns and populates vector out.

Parameters
v1
v2
out
Returns
vec_struct

Definition at line 1065 of file la.c.

References _, matrix_::cols, COLVEC_, G_matrix_get_element(), G_matrix_set_element(), G_warning(), if(), matrix_::is_init, MATRIX_, NULL, matrix_::rows, ROWVEC_, matrix_::type, and matrix_::v_indx.