GRASS GIS 8 Programmer's Manual 8.3.2(2024)-exported
Loading...
Searching...
No Matches
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_struct * G_matrix_init (int rows, int cols, int ldim)
 Initialize a matrix structure.
 
int G_matrix_zero (mat_struct *A)
 Clears (or resets) the matrix values to 0.
 
int G_matrix_set (mat_struct *A, int rows, int cols, int ldim)
 Set parameters for an initialized matrix.
 
mat_struct * G_matrix_copy (const mat_struct *A)
 Copy a matrix.
 
mat_struct * G_matrix_add (mat_struct *mt1, mat_struct *mt2)
 Adds two matricies.
 
mat_struct * G_matrix_subtract (mat_struct *mt1, mat_struct *mt2)
 Subtract two matricies.
 
mat_struct * G_matrix_scalar_mul (double scalar, mat_struct *matrix, mat_struct *out)
 Calculates the scalar-matrix multiplication.
 
mat_struct * G_matrix_scale (mat_struct *mt1, const double c)
 Scale a matrix by a scalar value.
 
mat_struct * G__matrix_add (mat_struct *mt1, mat_struct *mt2, const double c1, const double c2)
 
mat_struct * G_matrix_product (mat_struct *mt1, mat_struct *mt2)
 Returns product of two matricies.
 
mat_struct * G_matrix_transpose (mat_struct *mt)
 Transpose a matrix.
 
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.
 
mat_struct * G_matrix_inverse (mat_struct *mt)
 Returns the matrix inverse.
 
void G_matrix_free (mat_struct *mt)
 Free up allocated matrix.
 
void G_matrix_print (mat_struct *mt)
 Print out a matrix.
 
int G_matrix_set_element (mat_struct *mt, int rowval, int colval, double val)
 
double G_matrix_get_element (mat_struct *mt, int rowval, int colval)
 Retrieve value of the (i,j)th element.
 
vec_struct * G_matvect_get_column (mat_struct *mt, int col)
 Retrieve a column of the matrix to a vector structure.
 
vec_struct * G_matvect_get_row (mat_struct *mt, int row)
 Retrieve a row of the matrix to a vector structure.
 
int G_matvect_extract_vector (mat_struct *mt, vtype vt, int indx)
 Convert matrix to vector.
 
int G_matvect_retrieve_matrix (vec_struct *vc)
 Revert a vector to matrix.
 
vec_struct * G_matvect_product (mat_struct *A, vec_struct *b, vec_struct *out)
 Calculates the matrix-vector product.
 
vec_struct * G_vector_init (int cells, int ldim, vtype vt)
 Initialize a vector structure.
 
void G_vector_free (vec_struct *v)
 Free an allocated vector structure.
 
vec_struct * G_vector_sub (vec_struct *v1, vec_struct *v2, vec_struct *out)
 Subtract two vectors.
 
int G_vector_set (vec_struct *A, int cells, int ldim, vtype vt, int vindx)
 
double G_vector_norm_euclid (vec_struct *vc)
 Calculates euclidean norm.
 
double G_vector_norm_maxval (vec_struct *vc, int vflag)
 Calculates maximum value.
 
double G_vector_norm1 (vec_struct *vc)
 Calculates the 1-norm of a vector.
 
vec_struct * G_vector_product (vec_struct *v1, vec_struct *v2, vec_struct *out)
 Calculates the vector product.
 
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.
 
int G_matrix_read (FILE *fp, mat_struct *out)
 Read a matrix from a file stream.
 
mat_struct * G_matrix_resize (mat_struct *in, int rows, int cols)
 Resizes a matrix.
 
int G_matrix_stdin (mat_struct *out)
 
int G_matrix_eigen_sort (vec_struct *d, mat_struct *m)
 Sort eigenvectors according to eigenvalues.
 

Function Documentation

◆ G__matrix_add()

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

Definition at line 271 of file la.c.

References G_matrix_init(), G_warning(), and NULL.

Referenced by G_matrix_add(), G_matrix_scale(), 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 171 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 139 of file la.c.

References G_matrix_init(), G_warning(), and NULL.

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 1615 of file la.c.

References G_free(), G_matrix_get_element(), G_matrix_set(), and G_matrix_set_element().

◆ 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 653 of file la.c.

References G_free().

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 743 of file la.c.

Referenced by G_matrix_eigen_sort(), G_matrix_print(), G_matrix_resize(), G_matrix_scalar_mul(), G_matvect_get_column(), G_matvect_get_row(), G_matvect_product(), 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 53 of file la.c.

References G_warning(), and NULL.

Referenced by G__matrix_add(), G_matrix_copy(), 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 595 of file la.c.

References G_matrix_free(), G_matrix_init(), G_matrix_LU_solve(), G_warning(), and NULL.

◆ 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 468 of file la.c.

References G_free(), G_matrix_copy(), G_matrix_free(), G_warning(), and NULL.

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 672 of file la.c.

References G_matrix_get_element(), and G_message().

◆ 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 346 of file la.c.

References G_matrix_init(), G_warning(), and NULL.

◆ 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 1510 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 1563 of file la.c.

References G_matrix_get_element(), G_matrix_init(), and G_matrix_set_element().

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 207 of file la.c.

References G_matrix_get_element(), G_matrix_init(), G_matrix_resize(), G_matrix_set_element(), G_warning(), and NULL.

◆ 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 250 of file la.c.

References G__matrix_add(), and NULL.

◆ 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 109 of file la.c.

References G_warning().

Referenced by G_matrix_eigen_sort(), and G_matrix_read().

◆ G_matrix_set_element()

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

◆ G_matrix_stdin()

int G_matrix_stdin ( mat_struct * out)

Definition at line 1598 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 189 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 400 of file la.c.

References G_matrix_init().

◆ 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 84 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 847 of file la.c.

References G_warning().

◆ 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 765 of file la.c.

References G_matrix_get_element(), G_matrix_set_element(), G_vector_init(), G_warning(), and NULL.

◆ 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 805 of file la.c.

References G_matrix_get_element(), G_matrix_set_element(), G_vector_init(), G_warning(), and NULL.

◆ 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 918 of file la.c.

References b, G_matrix_get_element(), G_warning(), and NULL.

◆ 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 893 of file la.c.

◆ 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 1398 of file la.c.

References G_warning(), and NULL.

◆ 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 1018 of file la.c.

References G_free().

◆ 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 972 of file la.c.

References G_warning(), and NULL.

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 1288 of file la.c.

References G_matrix_get_element(), and G_warning().

◆ 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 1167 of file la.c.

References G_fatal_error().

◆ 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 1217 of file la.c.

References G_fatal_error().

◆ 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 1326 of file la.c.

References G_matrix_get_element(), G_matrix_set_element(), G_warning(), and NULL.

◆ G_vector_set()

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

Definition at line 1116 of file la.c.

References G_warning().

◆ 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 1041 of file la.c.

References G_matrix_get_element(), G_matrix_set_element(), G_warning(), and NULL.