GRASS 8 Programmer's Manual 8.6.0dev(2026)-b43c9b8403
Loading...
Searching...
No Matches
ccmath_grass_wrapper.c
Go to the documentation of this file.
1/*****************************************************************************
2 *
3 * MODULE: Grass numerical math interface
4 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5 * soerengebbert <at> googlemail <dot> com
6 *
7 * PURPOSE: ccmath library function wrapper
8 * part of the gmath library
9 *
10 * SPDX-FileCopyrightText: 2010 GRASS Development Team
11 * SPDX-License-Identifier: GPL-2.0-or-later
12 *
13 *****************************************************************************/
14
15#if defined(HAVE_CCMATH)
16#include <ccmath.h>
17#else
18#include <grass/ccmath_grass.h>
19#endif
20
21/**
22 * Documentation and ccmath library version 2.2.1 by Daniel A. Atkinson
23 *
24 Chapter 1
25
26 LINEAR ALGEBRA
27
28 Summary
29
30 The matrix algebra library contains functions that
31 perform the standard computations of linear algebra.
32 General areas covered are:
33
34 o Solution of Linear Systems
35 o Matrix Inversion
36 o Eigensystem Analysis
37 o Matrix Utility Operations
38 o Singular Value Decomposition
39
40 The operations covered here are fundamental to many
41 areas of mathematics and statistics. Thus, functions
42 in this library segment are called by other library
43 functions. Both real and complex valued matrices
44 are covered by functions in the first four of these
45 categories.
46
47
48 Notes on Contents
49
50 Functions in this library segment provide the basic operations of
51 numerical linear algebra and some useful utility functions for operations on
52 vectors and matrices. The following list describes the functions available for
53 operations with real-valued matrices.
54
55
56 o Solving and Inverting Linear Systems:
57
58 solv --------- solve a general system of real linear equations.
59 solvps ------- solve a real symmetric linear system.
60 solvru ------- solve a real right upper triangular linear system.
61 solvtd ------- solve a tridiagonal real linear system.
62
63 minv --------- invert a general real square matrix.
64 psinv -------- invert a real symmetric matrix.
65 ruinv -------- invert a right upper triangular matrix.
66
67
68 The solution of a general linear system and efficient algorithms for
69 solving special systems with symmetric and tridiagonal matrices are provided
70 by these functions. The general solution function employs a LU factorization
71 with partial pivoting and it is very robust. It will work efficiently on any
72 problem that is not ill-conditioned. The symmetric matrix solution is based
73 on a modified Cholesky factorization. It is best used on positive definite
74 matrices that do not require pivoting for numeric stability. Tridiagonal
75 solvers require order-N operations (N = dimension). Thus, they are highly
76 recommended for this important class of sparse systems. Two matrix inversion
77 routines are provided. The general inversion function is again LU based. It
78 is suitable for use on any stable (ie. well-conditioned) problem. The
79 Cholesky based symmetric matrix inversion is efficient and safe for use on
80 matrices known to be positive definite, such as the variance matrices
81 encountered in statistical computations. Both the solver and the inverse
82 functions are designed to enhance data locality. They are very effective
83 on modern microprocessors.
84
85
86 o Eigensystem Analysis:
87
88 eigen ------ extract all eigen values and vectors of a real
89 symmetric matrix.
90 eigval ----- extract the eigen values of a real symmetric matrix.
91 evmax ------ compute the eigen value of maximum absolute magnitude
92 and its corresponding vector for a symmetric matrix.
93
94
95 Eigensystem functions operate on real symmetric matrices. Two forms of
96 the general eigen routine are provided because the computation of eigen values
97 only is much faster when vectors are not required. The basic algorithms use
98 a Householder reduction to tridiagonal form followed by QR iterations with
99 shifts to enhance convergence. This has become the accepted standard for
100 symmetric eigensystem computation. The evmax function uses an efficient
101 iterative power method algorithm to extract the eigen value of maximum
102 absolute size and the corresponding eigenvector.
103
104
105 o Singular Value Decomposition:
106
107 svdval ----- compute the singular values of a m by n real matrix.
108 sv2val ----- compute the singular values of a real matrix
109 efficiently for m >> n.
110 svduv ------ compute the singular values and the transformation
111 matrices u and v for a real m by n matrix.
112 sv2uv ------ compute the singular values and transformation
113 matrices efficiently for m >> n.
114 svdu1v ----- compute the singular values and transformation
115 matrices u1 and v, where u1 overloads the input
116 with the first n column vectors of u.
117 sv2u1v ----- compute the singular values and the transformation
118 matrices u1 and v efficiently for m >> n.
119
120
121 Singular value decomposition is extremely useful when dealing with linear
122 systems that may be singular. Singular values with values near zero are flags
123 of a potential rank deficiency in the system matrix. They can be used to
124 identify the presence of an ill-conditioned problem and, in some cases, to
125 deal with the potential instability. They are applied to the linear least
126 squares problem in this library. Singular values also define some important
127 matrix norm parameters such as the 2-norm and the condition value. A complete
128 decomposition provides both singular values and an orthogonal decomposition of
129 vector spaces related to the matrix identifying the range and null-space.
130 Fortunately, a highly stable algorithm based on Householder reduction to
131 bidiagonal form and QR rotations can be used to implement the decomposition.
132 The library provides two forms with one more efficient when the dimensions
133 satisfy m > (3/2)n.
134
135 General Technical Comments
136
137 Efficient computation with matrices on modern processors must be
138 adapted to the storage scheme employed for matrix elements. The functions
139 of this library segment do not employ the multidimensional array intrinsic
140 of the C language. Access to elements employs the simple row-major scheme
141 described here.
142
143 Matrices are modeled by the library functions as arrays with elements
144 stored in row order. Thus, the element in the jth row and kth column of
145 the n by n matrix M, stored in the array mat[], is addressed by
146
147 M[j,k] = mat[n*j+k] , with 0 =< j,k <= n-1 .
148
149 (Remember that C employs zero as the starting index.) The storage order has
150 important implications for data locality.
151
152 The algorithms employed here all have excellent numerical stability, and
153 the default double precision arithmetic of C enhances this. Thus, any
154 problems encountered in using the matrix algebra functions will almost
155 certainly be due to an ill-conditioned matrix. (The Hilbert matrices,
156
157 H[i,j] = 1/(1+i+j) for i,j < n
158
159 form a good example of such ill-conditioned systems.) We remind the reader
160 that the appropriate response to such ill-conditioning is to seek an
161 alternative approach to the problem. The option of increasing precision has
162 already been exploited. Modification of the linear algebra algorithm code is
163 not normally effective in an ill-conditioned problem.
164
165------------------------------------------------------------------------------
166 FUNCTION SYNOPSES
167------------------------------------------------------------------------------
168
169 Linear System Solutions:
170-----------------------------------------------------------------------------
171*/
172
173/**
174 \brief Solve a general linear system A*x = b.
175
176 \param a = array containing system matrix A in row order (altered to L-U
177 factored form by computation) \param b = array containing system vector b at
178 entry and solution vector x at exit \param n = dimension of system \return 0
179 -> normal exit; -1 -> singular input
180 */
181int G_math_solv(double **a, double *b, int n)
182{
183 return solv(a[0], b, n);
184}
185
186/**
187 \brief Solve a symmetric positive definite linear system S*x = b.
188
189 \param a = array containing system matrix S (altered to Cholesky upper
190 right factor by computation) \param b = array containing system vector b as
191 input and solution vector x as output \param n = dimension of system
192 \return: 0 -> normal exit; -1 -> input matrix not positive definite
193 */
194int G_math_solvps(double **a, double *b, int n)
195{
196 return solvps(a[0], b, n);
197}
198
199/**
200 \brief Solve a tridiagonal linear system M*x = y.
201
202 \param a = array containing m+1 diagonal elements of M
203 \param b = array of m elements below the main diagonal of M
204 \param c = array of m elements above the main diagonal
205 \param x = array containing the system vector y initially, and the
206 solution vector at exit (m+1 elements) \param m = dimension parameter ( M is
207 (m+1)x(m+1) )
208
209*/
210void G_math_solvtd(double *a, double *b, double *c, double *x, int m)
211{
212 solvtd(a, b, c, x, m);
213 return;
214}
215
216/*
217 \brief Solve an upper right triangular linear system T*x = b.
218
219 \param a = pointer to array of upper right triangular matrix T
220 \param b = pointer to array of system vector The computation overloads this
221 with the solution vector x. \param n = dimension (dim(a)=n*n,dim(b)=n)
222 \return value: f = status flag, with 0 -> normal exit, -1 -> system singular
223 */
224int G_math_solvru(double **a, double *b, int n)
225{
226 return solvru(a[0], b, n);
227}
228
229/**
230 \brief Invert (in place) a general real matrix A -> Inv(A).
231
232 \param a = array containing the input matrix A. This is converted to the
233 inverse matrix. \param n = dimension of the system (i.e. A is n x n )
234 \return: 0 -> normal exit, 1 -> singular input matrix
235*/
236int G_math_minv(double **a, int n)
237{
238 return minv(a[0], n);
239}
240
241/**
242 \brief Invert (in place) a symmetric real matrix, V -> Inv(V).
243
244 The input matrix V is symmetric (V[i,j] = V[j,i]).
245 \param a = array containing a symmetric input matrix. This is converted to
246 the inverse matrix. \param n = dimension of the system (dim(v)=n*n) \return:
247 0 -> normal exit 1 -> input matrix not positive definite
248*/
249int G_math_psinv(double **a, int n)
250{
251 return psinv(a[0], n);
252}
253
254/**
255 \brief Invert an upper right triangular matrix T -> Inv(T).
256
257 \param a = pointer to array of upper right triangular matrix, This is
258 replaced by the inverse matrix. \param n = dimension (dim(a)=n*n) \return
259 value: status flag, with 0 -> matrix inverted -1 -> matrix singular
260*/
261int G_math_ruinv(double **a, int n)
262{
263 return ruinv(a[0], n);
264}
265
266/*
267 -----------------------------------------------------------------------------
268
269 Symmetric Eigensystem Analysis:
270 -----------------------------------------------------------------------------
271 */
272
273/**
274
275 \brief Compute the eigenvalues of a real symmetric matrix A.
276
277 \param a = pointer to array of symmetric n by n input matrix A. The
278 computation alters these values. \param ev = pointer to array of the output
279 eigenvalues \param n = dimension parameter (dim(a)= n*n, dim(ev)= n)
280*/
281void G_math_eigval(double **a, double *ev, int n)
282{
283 eigval(a[0], ev, n);
284 return;
285}
286
287/**
288 \brief Compute the eigenvalues and eigenvectors of a real symmetric matrix
289 A.
290
291 The input and output matrices are related by
292
293 A = E*D*E~ where D is the diagonal matrix of eigenvalues
294 D[i,j] = ev[i] if i=j and 0 otherwise.
295
296 The columns of E are the eigenvectors.
297
298 \param a = pointer to store for symmetric n by n input matrix A. The
299 computation overloads this with an orthogonal matrix of eigenvectors E.
300 \param ev = pointer to the array of the output eigenvalues
301 \param n = dimension parameter (dim(a)= n*n, dim(ev)= n)
302*/
303void G_math_eigen(double **a, double *ev, int n)
304{
305 eigen(a[0], ev, n);
306 return;
307}
308
309/*
310 \brief Compute the maximum (absolute) eigenvalue and corresponding
311 eigenvector of a real symmetric matrix A.
312
313
314 \param a = array containing symmetric input matrix A
315 \param u = array containing the n components of the eigenvector at exit
316 (vector normalized to 1) \param n = dimension of system \return: ev =
317 eigenvalue of A with maximum absolute value HUGE -> convergence failure
318 */
319double G_math_evmax(double **a, double *u, int n)
320{
321 return evmax(a[0], u, n);
322}
323
324/*
325 ------------------------------------------------------------------------------
326
327 Singular Value Decomposition:
328 ------------------------------------------------------------------------------
329
330 A number of versions of the Singular Value Decomposition (SVD)
331 are implemented in the library. They support the efficient
332 computation of this important factorization for a real m by n
333 matrix A. The general form of the SVD is
334
335 A = U*S*V~ with S = | D |
336 | 0 |
337
338 where U is an m by m orthogonal matrix, V is an n by n orthogonal matrix,
339 D is the n by n diagonal matrix of singular value, and S is the singular
340 m by n matrix produced by the transformation.
341
342 The singular values computed by these functions provide important
343 information on the rank of the matrix A, and on several matrix
344 norms of A. The number of non-zero singular values d[i] in D
345 equal to the rank of A. The two norm of A is
346
347 ||A|| = max(d[i]) , and the condition number is
348
349 k(A) = max(d[i])/min(d[i]) .
350
351 The Frobenius norm of the matrix A is
352
353 Fn(A) = Sum(i=0 to n-1) d[i]^2 .
354
355 Singular values consistent with zero are easily recognized, since
356 the decomposition algorithms have excellent numerical stability.
357 The value of a 'zero' d[i] is no larger than a few times the
358 computational rounding error e.
359
360 The matrix U1 is formed from the first n orthonormal column vectors
361 of U. U1[i,j] = U[i,j] for i = 1 to m and j = 1 to n. A singular
362 value decomposition of A can also be expressed in terms of the m by\
363 n matrix U1, with
364
365 A = U1*D*V~ .
366
367 SVD functions with three forms of output are provided. The first
368 form computes only the singular values, while the second computes
369 the singular values and the U and V orthogonal transformation
370 matrices. The third form of output computes singular values, the
371 V matrix, and saves space by overloading the input array with
372 the U1 matrix.
373
374 Two forms of decomposition algorithm are available for each of the
375 three output types. One is computationally efficient when m ~ n.
376 The second, distinguished by the prefix 'sv2' in the function name,
377 employs a two stage Householder reduction to accelerate computation
378 when m substantially exceeds n. Use of functions of the second form
379 is recommended for m > 2n.
380
381 Singular value output from each of the six SVD functions satisfies
382
383 d[i] >= 0 for i = 0 to n-1.
384 -------------------------------------------------------------------------------
385 */
386
387/**
388 \brief Compute the singular values of a real m by n matrix A.
389
390
391 \param d = pointer to double array of dimension n (output = singular
392 values of A) \param a = pointer to store of the m by n input matrix A (A is
393 altered by the computation) \param m = number of rows in A \param n =
394 number of columns in A (m>=n required) \return value: status flag with: 0 ->
395 success -1 -> input error m < n
396
397*/
398int G_math_svdval(double *d, double **a, int m, int n)
399{
400 return svdval(d, a[0], m, n);
401}
402
403/**
404
405 \brief Compute singular values when m >> n.
406
407 \param d = pointer to double array of dimension n (output = singular
408 values of A) \param a = pointer to store of the m by n input matrix A (A is
409 altered by the computation) \param m = number of rows in A \param n =
410 number of columns in A (m>=n required) \return value: status flag with: 0 ->
411 success -1 -> input error m < n
412*/
413int G_math_sv2val(double *d, double **a, int m, int n)
414{
415 return sv2val(d, a[0], m, n);
416}
417
418/*
419 \brief Compute the singular value transformation S = U~*A*V.
420
421 \param d = pointer to double array of dimension n (output = singular values
422 of A) \param a = pointer to store of the m by n input matrix A (A is altered
423 by the computation) \param u = pointer to store for m by m orthogonal matrix
424 U \param v = pointer to store for n by n orthogonal matrix V \param m =
425 number of rows in A \param n = number of columns in A (m>=n required)
426 \return value: status flag with: 0 -> success -1 -> input error m < n
427 */
428int G_math_svduv(double *d, double **a, double **u, int m, double **v, int n)
429{
430 return svduv(d, a[0], u[0], m, v[0], n);
431}
432
433/**
434 \brief Compute the singular value transformation when m >> n.
435
436 \param d = pointer to double array of dimension n (output = singular
437 values of A) \param a = pointer to store of the m by n input matrix A (A is
438 altered by the computation) \param u = pointer to store for m by m
439 orthogonal matrix U \param v = pointer to store for n by n orthogonal matrix
440 V \param m = number of rows in A \param n = number of columns in A (m>=n
441 required) \return value: status flag with: 0 -> success -1 -> input error m <
442 n
443*/
444int G_math_sv2uv(double *d, double **a, double **u, int m, double **v, int n)
445{
446 return sv2uv(d, a[0], u[0], m, v[0], n);
447}
448
449/**
450
451 \brief Compute the singular value transformation with A overloaded by the
452 partial U-matrix.
453
454 \param d = pointer to double array of dimension n
455 (output = singular values of A)
456 \param a = pointer to store of the m by n input matrix A (At output a is
457 overloaded by the matrix U1 whose n columns are orthogonal vectors equal to
458 the first n columns of U.) \param v = pointer to store for n by n
459 orthogonal matrix V \param m = number of rows in A \param n = number of
460 columns in A (m>=n required) \return value: status flag with: 0 -> success -1
461 -> input error m < n
462
463*/
464int G_math_svdu1v(double *d, double **a, int m, double **v, int n)
465{
466 return svdu1v(d, a[0], m, v[0], n);
467}
void solvtd(double *a, double *b, double *c, double *x, int m)
Definition solvtd.c:8
int svdu1v(double *d, double *a, int m, double *v, int n)
Definition svdu1v.c:10
void eigval(double *a, double *eval, int n)
Definition eigval.c:10
int solv(double *a, double *b, int n)
Definition solv.c:10
int sv2uv(double *d, double *a, double *u, int m, double *v, int n)
Definition sv2uv.c:10
int svduv(double *d, double *a, double *u, int m, double *v, int n)
Definition svduv.c:10
int psinv(double *v, int n)
Definition psinv.c:9
int solvps(double *s, double *x, int n)
Definition solvps.c:9
int ruinv(double *a, int n)
Definition ruinv.c:8
double evmax(double *a, double *u, int n)
Definition evmax.c:10
void eigen(double *a, double *eval, int n)
Definition eigen.c:10
int svdval(double *d, double *a, int m, int n)
Definition svdval.c:10
int solvru(double *a, double *b, int n)
Definition solvru.c:8
int minv(double *a, int n)
Definition minv.c:10
int sv2val(double *d, double *a, int m, int n)
Definition sv2val.c:10
int G_math_solvru(double **a, double *b, int n)
int G_math_psinv(double **a, int n)
Invert (in place) a symmetric real matrix, V -> Inv(V).
int G_math_solv(double **a, double *b, int n)
Solve a general linear system A*x = b.
int G_math_svdu1v(double *d, double **a, int m, double **v, int n)
Compute the singular value transformation with A overloaded by the partial U-matrix.
int G_math_minv(double **a, int n)
Invert (in place) a general real matrix A -> Inv(A).
void G_math_eigen(double **a, double *ev, int n)
Compute the eigenvalues and eigenvectors of a real symmetric matrix A.
int G_math_svduv(double *d, double **a, double **u, int m, double **v, int n)
void G_math_eigval(double **a, double *ev, int n)
Compute the eigenvalues of a real symmetric matrix A.
int G_math_solvps(double **a, double *b, int n)
Solve a symmetric positive definite linear system S*x = b.
double G_math_evmax(double **a, double *u, int n)
void G_math_solvtd(double *a, double *b, double *c, double *x, int m)
Solve a tridiagonal linear system M*x = y.
int G_math_ruinv(double **a, int n)
Invert an upper right triangular matrix T -> Inv(T).
int G_math_sv2val(double *d, double **a, int m, int n)
Compute singular values when m >> n.
int G_math_svdval(double *d, double **a, int m, int n)
Compute the singular values of a real m by n matrix A.
int G_math_sv2uv(double *d, double **a, double **u, int m, double **v, int n)
Compute the singular value transformation when m >> n.
double b
Definition r_raster.c:37
#define x