gmathlib.dox 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*! \page gmathlib GRASS Numerical math interface
  2. <!-- doxygenized from "GRASS 5 Programmer's Manual"
  3. by M. Neteler 8/2005
  4. -->
  5. \section gmathintro The GRASS numerical math interface
  6. <P>
  7. Authors: probably CERL, David D. Gray, Brad Douglas, Soeren Gebbert and many others
  8. <P>
  9. The gmath library provides many common mathematical functions to be used in GRASS modules:
  10. <ul>
  11. <li>Linear algebra functions of type blas level 1, 2 and 3 </li>
  12. <li>Iterative and direct linear algebra solver for sparse, band and dense matrices </li>
  13. <li>Eigenvalue and single value decomposition methods </li>
  14. <li>Memory allocation methods for vectors and matrices </li>
  15. <li>Fast fourier transformation and many more </li>
  16. </ul>
  17. The GRASS numerical math interface also makes use of 3d party libraries to
  18. implement linear algebra and fast Fourier transformation functions,
  19. like fftw-library, BLAS/LAPACK, ccmath and ATLAS. Parts of the ccmath library are
  20. integrated in GRASS in lib/external.
  21. There are several tests and benchmarks available for blas level 1, 2 and 3 as well
  22. as for ccmath and linear equation solver in the lib/gmath/test directory. These
  23. tests and benchmarks are not compiled by default. In case GRASS has been compiled,
  24. just change in the test directory and type make. A binary named test.gmath.lib
  25. will be available after starting GRASS.
  26. To run all tests just type:
  27. \verbatim
  28. test.gmath.lib -a
  29. \endverbatim
  30. To run a benchmark to solve a matrix size of 2000x2000 using the Krylov-Subspace iterative solver type:
  31. \verbatim
  32. test.gmath.lib solverbench=krylov rows=2000
  33. \endverbatim
  34. Several GRASS modules implement there own numerical functions.
  35. The goal is to collect all these functions in the GRASS numerical math interface,
  36. to standardize them and to modify all modules to make use of the gmath interface.
  37. \subsection memalloc Memory allocation functions
  38. GMATH provides several functions for vector and matrix memory allocation of different
  39. types. These functions should be used to allocate vectors and matrices which are
  40. used by gmath linear algebra functions.
  41. <P>
  42. Matrix and vector allocation for real types
  43. <P>
  44. double *G_alloc_vector(size_t)<br>
  45. double **G_alloc_matrix(int, int)<br>
  46. float *G_alloc_fvector(size_t)<br>
  47. float **G_alloc_fmatrix(int, int)<br>
  48. void G_free_vector(double *)<br>
  49. void G_free_matrix(double **)<br>
  50. void G_free_fvector(float *)<br>
  51. void G_free_fmatrix(float **)<br>
  52. <P>
  53. Matrix and vector allocation for integer types
  54. <P>
  55. int *G_alloc_ivector(size_t)<br>
  56. int **G_alloc_imatrix(int, int)<br>
  57. void G_free_ivector(int *)<br>
  58. void G_free_imatrix(int **)<br>
  59. \subsection commonmath Common mathematical functions
  60. <P>
  61. Fast Fourier Transformation
  62. <P>
  63. int fft(int, double *[2], int, int, int)<br>
  64. int fft2(int, double (*)[2], int, int, int)<br>
  65. <P>
  66. Several mathematical functions mostly used by Imagery modules
  67. <P>
  68. double G_math_rand_gauss(int, double)<br>
  69. long G_math_max_pow2 (long n)<br>
  70. long G_math_min_pow2 (long n)<br>
  71. float G_math_rand(int)<br>
  72. int del2g(double *[2], int, double)<br>
  73. int getg(double, double *[2], int)<br>
  74. int G_math_egvorder(double *, double **, long)<br>
  75. int G_math_complex_mult (double *v1[2], int size1, double *v2[2], int size2, double *v3[2], int size3)<br>
  76. int G_math_findzc(double conv[], int size, double zc[], double thresh, int num_orients);
  77. <P>
  78. Obsolete LU decomposition lend from numerical recipes
  79. <P>
  80. int G_ludcmp(double **, int, int *, double *);
  81. void G_lubksb(double **a, int n, int *indx, double b[]);
  82. \subsection gmathblas Blas like level 1,2 and 3 functions
  83. <P>
  84. Author: Soeren Gebbert
  85. <P>
  86. The gmath library provides its own implementation of blas level 1, 2 and 3
  87. functions for vector - vector, vector - matrix and matrix - matrix operations.
  88. OpenMP is internally used to parallelize the vector and matrix operations. Most of the
  89. function can be called from inside a OpenMP parallel region.
  90. Additionally a wrapper for the ATLAS blas level 1 functions is available. The wrapping
  91. functions can also be used, when the ATLAS library is not available. In this case
  92. the GRASS blas level 1 implementation is called automatically instead.
  93. \subsubsection blas_level_1 Level 1 vector - vector GRASS implementation with OpenMP thread support
  94. <P>
  95. Double precision blas level 1 functions
  96. <P>
  97. void G_math_d_x_dot_y(double *, double *, double *, int )<br>
  98. void G_math_d_asum_norm(double *, double *, int )<br>
  99. void G_math_d_euclid_norm(double *, double *, int )<br>
  100. void G_math_d_max_norm(double *, double *, int )<br>
  101. void G_math_d_ax_by(double *, double *, double *, double , double , int )<br>
  102. void G_math_d_copy(double *, double *, int )<br><br>
  103. <P>
  104. Single precision blas level 1 functions
  105. <P>
  106. void G_math_f_x_dot_y(float *, float *, float *, int )<br>
  107. void G_math_f_asum_norm(float *, float *, int )<br>
  108. void G_math_f_euclid_norm(float *, float *, int )<br>
  109. void G_math_f_max_norm(float *, float *, int )<br>
  110. void G_math_f_ax_by(float *, float *, float *, float , float , int )<br>
  111. void G_math_f_copy(float *, float *, int )<br><br>
  112. <P>
  113. Integer blas level 1 functions
  114. <P>
  115. void G_math_i_x_dot_y(int *, int *, double *, int )<br>
  116. void G_math_i_asum_norm(int *, double *, int )<br>
  117. void G_math_i_euclid_norm(int *, double *,int )<br>
  118. void G_math_i_max_norm(int *, int *, int )<br>
  119. void G_math_i_ax_by(int *, int *, int *, int , int , int )<br>
  120. void G_math_i_copy(int *, int *, int )<br><br>
  121. <P>
  122. ATLAS blas level 1 wrapper
  123. <P>
  124. double G_math_ddot(double *, double *, int )<br>
  125. float G_math_sdot(float *, float *, int )<br>
  126. float G_math_sdsdot(float *, float *, float , int )<br>
  127. double G_math_dnrm2(double *, int )<br>
  128. double G_math_dasum(double *, int )<br>
  129. double G_math_idamax(double *, int )<br>
  130. float G_math_snrm2(float *, int )<br>
  131. float G_math_sasum(float *, int )<br>
  132. float G_math_isamax(float *, int )<br>
  133. void G_math_dscal(double *, double , int )<br>
  134. void G_math_sscal(float *, float , int )<br>
  135. void G_math_dcopy(double *, double *, int )<br>
  136. void G_math_scopy(float *, float *, int )<br>
  137. void G_math_daxpy(double *, double *, double , int )<br>
  138. void G_math_saxpy(float *, float *, float , int )<br>
  139. \subsubsection blas_level_2 Level 2 matrix - vector GRASS implementation with OpenMP thread support
  140. void G_math_Ax_sparse(G_math_spvector **, double *, double *, int )<br>
  141. void G_math_d_Ax(double **, double *, double *, int , int )<br>
  142. void G_math_f_Ax(float **, float *, float *, int , int )<br>
  143. void G_math_d_x_dyad_y(double *, double *, double **, int, int )<br>
  144. void G_math_f_x_dyad_y(float *, float *, float **, int, int )<br>
  145. void G_math_d_aAx_by(double **, double *, double *, double , double , double *, int , int )<br>
  146. void G_math_f_aAx_by(float **, float *, float *, float , float , float *, int , int )<br>
  147. int G_math_d_A_T(double **A, int rows)<br>
  148. int G_math_f_A_T(float **A, int rows)<br>
  149. \subsubsection blas_level_3 Blas level 3 matrix - matrix GRASS implementation with OpenMP thread support
  150. void G_math_d_aA_B(double **, double **, double , double **, int , int )<br>
  151. void G_math_f_aA_B(float **, float **, float , float **, int , int )<br>
  152. void G_math_d_AB(double **, double **, double **, int , int , int )<br>
  153. void G_math_f_AB(float **, float **, float **, int , int , int )<br>
  154. \subsection gmathccmath Ccmath library function wrapper
  155. <P>
  156. Author: Daniel A. Atkinson: ccmath library and documentation<br>
  157. Wrapper Soeren Gebbert
  158. <P>
  159. GRASS make use of several functions from the ccmath library. The ccmath library is
  160. located in the lib/external directory. The library was designed and developed by
  161. Daniel A. Atkinson and is licensed under the terms of the LGPL. Ccmath provides
  162. many common mathematical functions. Currently GRASS uses only the linear algebra
  163. part of the library. These functions are available via wrapping functions.<br><br>
  164. The wrapper integrates the ccmath library functions into the gmath
  165. structure. Hence only gmath memory allocation function should be used to create
  166. vector and matrix structures used by ccmath.
  167. This is the documentation of the linear algebra part of the the ccmath library used by GRASS.
  168. It was written by Daniel A. Atkinson and provides a detailed description of the available
  169. linear algebra functions.
  170. \verbatim
  171. LINEAR ALGEBRA
  172. Summary
  173. The matrix algebra library contains functions that
  174. perform the standard computations of linear algebra.
  175. General areas covered are:
  176. o Solution of Linear Systems
  177. o Matrix Inversion
  178. o Eigensystem Analysis
  179. o Matrix Utility Operations
  180. o Singular Value Decomposition
  181. The operations covered here are fundamental to many
  182. areas of mathematics and statistics. Thus, functions
  183. in this library segment are called by other library
  184. functions. Both real and complex valued matrices
  185. are covered by functions in the first four of these
  186. categories.
  187. Notes on Contents
  188. Functions in this library segment provide the basic operations of
  189. numerical linear algebra and some useful utility functions for operations on
  190. vectors and matrices. The following list describes the functions available for
  191. operations with real-valued matrices.
  192. o Solving and Inverting Linear Systems:
  193. solv --------- solve a general system of real linear equations.
  194. solvps ------- solve a real symmetric linear system.
  195. solvru ------- solve a real right upper triangular linear system.
  196. solvtd ------- solve a tridiagonal real linear system.
  197. minv --------- invert a general real square matrix.
  198. psinv -------- invert a real symmetric matrix.
  199. ruinv -------- invert a right upper triangular matrix.
  200. The solution of a general linear system and efficient algorithms for
  201. solving special systems with symmetric and tridiagonal matrices are provided
  202. by these functions. The general solution function employs a LU factorization
  203. with partial pivoting and it is very robust. It will work efficiently on any
  204. problem that is not ill-conditioned. The symmetric matrix solution is based
  205. on a modified Cholesky factorization. It is best used on positive definite
  206. matrices that do not require pivoting for numeric stability. Tridiagonal
  207. solvers require order-N operations (N = dimension). Thus, they are highly
  208. recommended for this important class of sparse systems. Two matrix inversion
  209. routines are provided. The general inversion function is again LU based. It
  210. is suitable for use on any stable (ie. well-conditioned) problem. The
  211. Cholesky based symmetric matrix inversion is efficient and safe for use on
  212. matrices known to be positive definite, such as the variance matrices
  213. encountered in statistical computations. Both the solver and the inverse
  214. functions are designed to enhance data locality. They are very effective
  215. on modern microprocessors.
  216. o Eigensystem Analysis:
  217. eigen ------ extract all eigen values and vectors of a real
  218. symmetric matrix.
  219. eigval ----- extract the eigen values of a real symmetric matrix.
  220. evmax ------ compute the eigen value of maximum absolute magnitude
  221. and its corresponding vector for a symmetric matrix.
  222. Eigensystem functions operate on real symmetric matrices. Two forms of
  223. the general eigen routine are provided because the computation of eigen values
  224. only is much faster when vectors are not required. The basic algorithms use
  225. a Householder reduction to tridiagonal form followed by QR iterations with
  226. shifts to enhance convergence. This has become the accepted standard for
  227. symmetric eigensystem computation. The evmax function uses an efficient
  228. iterative power method algorithm to extract the eigen value of maximum
  229. absolute size and the corresponding eigenvector.
  230. o Singular Value Decomposition:
  231. svdval ----- compute the singular values of a m by n real matrix.
  232. sv2val ----- compute the singular values of a real matrix
  233. efficiently for m >> n.
  234. svduv ------ compute the singular values and the transformation
  235. matrices u and v for a real m by n matrix.
  236. sv2uv ------ compute the singular values and transformation
  237. matrices efficiently for m >> n.
  238. svdu1v ----- compute the singular values and transformation
  239. matrices u1 and v, where u1 overloads the input
  240. with the first n column vectors of u.
  241. sv2u1v ----- compute the singular values and the transformation
  242. matrices u1 and v efficiently for m >> n.
  243. Singular value decomposition is extremely useful when dealing with linear
  244. systems that may be singular. Singular values with values near zero are flags
  245. of a potential rank deficiency in the system matrix. They can be used to
  246. identify the presence of an ill-conditioned problem and, in some cases, to
  247. deal with the potential instability. They are applied to the linear least
  248. squares problem in this library. Singular values also define some important
  249. matrix norm parameters such as the 2-norm and the condition value. A complete
  250. decomposition provides both singular values and an orthogonal decomposition of
  251. vector spaces related to the matrix identifying the range and null-space.
  252. Fortunately, a highly stable algorithm based on Householder reduction to
  253. bidiagonal form and QR rotations can be used to implement the decomposition.
  254. The library provides two forms with one more efficient when the dimensions
  255. satisfy m > (3/2)n.
  256. General Technical Comments
  257. Efficient computation with matrices on modern processors must be
  258. adapted to the storage scheme employed for matrix elements. The functions
  259. of this library segment do not employ the multidimensional array intrinsic
  260. of the C language. Access to elements employs the simple row-major scheme
  261. described here.
  262. Matrices are modeled by the library functions as arrays with elements
  263. stored in row order. Thus, the element in the jth row and kth column of
  264. the n by n matrix M, stored in the array mat[], is addressed by
  265. M[j,k] = mat[n*j+k] , with 0 =< j,k <= n-1 .
  266. (Remember that C employs zero as the starting index.) The storage order has
  267. important implications for data locality.
  268. The algorithms employed here all have excellent numerical stability, and
  269. the default double precision arithmetic of C enhances this. Thus, any
  270. problems encountered in using the matrix algebra functions will almost
  271. certainly be due to an ill-conditioned matrix. (The Hilbert matrices,
  272. H[i,j] = 1/(1+i+j) for i,j < n
  273. form a good example of such ill-conditioned systems.) We remind the reader
  274. that the appropriate response to such ill-conditioning is to seek an
  275. alternative approach to the problem. The option of increasing precision has
  276. already been exploited. Modification of the linear algebra algorithm code is
  277. not normally effective in an ill-conditioned problem.
  278. \endverbatim
  279. <P>
  280. Ccmath functions available via wrapping:
  281. <P>
  282. int G_math_solv(double **,double *,int)<br>
  283. int G_math_solvps(double **,double *,int)<br>
  284. void G_math_solvtd(double *,double *,double *,double *,int)<br>
  285. int G_math_solvru(double **,double *,int)<br>
  286. int G_math_minv(double **,int)<br>
  287. int G_math_psinv(double **,int)<br>
  288. int G_math_ruinv(double **,int)<br>
  289. void G_math_eigval(double **,double *,int)<br>
  290. void G_math_eigen(double **,double *,int)<br>
  291. double G_math_evmax(double **,double *,int)<br>
  292. int G_math_svdval(double *,double **,int,int)<br>
  293. int G_math_sv2val(double *,double **,int,int)<br>
  294. int G_math_svduv(double *,double **,double **, int,double **,int)<br>
  295. int G_math_sv2uv(double *,double **,double **,int,double **,int)<br>
  296. int G_math_svdu1v(double *,double **,int,double **,int)<br>
  297. \subsection gmathsolver Linear equation solver
  298. <P>
  299. Author: Soeren Gebbert and other
  300. <P>
  301. Besides the ccmath linear equation solver GRASS provides a set of additional
  302. direct and iterative linear equation solver for sparse, band and dense matrices. A special sparse
  303. matrix structure was implemented to allow the solution of huge sparse linear
  304. equation systems.
  305. As iterative linear equation solver are classic (Gauss-Seidel/SOR, Jacobi) and
  306. Krylov-Subspace (CG and BiCGStab) solver available. All iterative solver support
  307. sparse and dense matrices. The Krylov-Subspace solver are parallelized with OpenMP.
  308. As direct solver are LU-decomposition and Gauss-elimination without pivoting and a bandwidth
  309. optimized Cholesky decomposition available. Direct solver only support dense and
  310. band(Cholesky only) matrices.
  311. <P>
  312. The row vector of the sparse matrix
  313. <P>
  314. \verbatim
  315. typedef struct
  316. {
  317. double *values; //The non null values of the row
  318. unsigned int cols; //Number of entries
  319. unsigned int *index;//the index number
  320. } G_math_spvector;
  321. \endverbatim
  322. <P>
  323. Sparse matrix and sparse vector functions
  324. <P>
  325. G_math_spvector *G_math_alloc_spvector(int )<br>
  326. G_math_spvector **G_math_alloc_spmatrix(int )<br>
  327. void G_math_free_spmatrix(G_math_spvector ** , int )<br>
  328. void G_math_free_spvector(G_math_spvector * )<br>
  329. int G_math_add_spvector(G_math_spvector **, G_math_spvector * , int )<br>
  330. <P>
  331. Direct linear equation solver
  332. <P>
  333. int G_math_solver_gauss(double **, double *, double *, int )<br>
  334. int G_math_solver_lu(double **, double *, double *, int )<br>
  335. int G_math_solver_cholesky(double **, double *, double *, int , int )<br>
  336. <P>
  337. Classic iterative linear equation solver for dense and sparse matrices
  338. <P>
  339. int G_math_solver_jacobi(double **, double *, double *, int , int , double , double )<br>
  340. int G_math_solver_gs(double **, double *, double *, int , int , double , double )<br>
  341. int G_math_solver_sparse_jacobi(G_math_spvector **, double *, double *, int , int , double , double )<br>
  342. int G_math_solver_sparse_gs(G_math_spvector **, double *, double *, int , int , double , double )<br>
  343. <P>
  344. Krylov-Subspace iterative linear equation solver for dense and sparse matrices
  345. <P>
  346. int G_math_solver_pcg(double **, double *, double *, int , int , double , int )<br>
  347. int G_math_solver_cg(double **, double *, double *, int , int , double )<br>
  348. int G_math_solver_bicgstab(double **, double *, double *, int , int , double )<br>
  349. int G_math_solver_sparse_pcg(G_math_spvector **, double *, double *, int , int , double , int )<br>
  350. int G_math_solver_sparse_cg(G_math_spvector **, double *, double *, int , int , double )<br>
  351. int G_math_solver_sparse_bicgstab(G_math_spvector **, double *, double *, int , int , double )<br>
  352. <P>
  353. LU, Gauss and Cholesky decomposition and substitution functions
  354. <P>
  355. void G_math_gauss_elimination(double **, double *, int )<br>
  356. void G_math_lu_decomposition(double **, double *, int )<br>
  357. int G_math_cholesky_decomposition(double **, int , int )<br>
  358. void G_math_backward_substitution(double **, double *, double *, int )<br>
  359. void G_math_forward_substitution(double **, double *, double *, int )<br>
  360. \subsection gmathlapack Optional support of LAPACK/BLAS
  361. <P>
  362. Author: David D. Gray<br>
  363. Additions: Brad Douglas
  364. <P>
  365. (under development)
  366. <BR>
  367. <P>
  368. This chapter provides an explanation of how numerical algebra routines from
  369. LAPACK/BLAS can be accessed through the GRASS GIS library "gmath". Most of
  370. the functions are wrapper modules for linear algebra problems, a few are locally
  371. implemented.
  372. <BR>
  373. <P>
  374. Getting BLAS/LAPACK (one package) if not already provided by the system:
  375. <BR><TT><A HREF="http://www.netlib.org/lapack/">http://www.netlib.org/lapack/</A></TT>
  376. <BR><TT><A HREF="http://netlib.bell-labs.com/netlib/master/readme.html">http://netlib.bell-labs.com/netlib/master/readme.html</A></TT>
  377. <BR>
  378. <P>
  379. Pre-compiled binaries of LAPACK/BLAS are provided on many Linux
  380. distributions.
  381. <P>
  382. \subsubsection Implementation Implementation
  383. <P>
  384. The function name convention is as follows:
  385. <P>
  386. <OL>
  387. <LI>G_matrix_*() : corresponding to Level3 BLAS (matrix -matrix) ,
  388. </LI>
  389. <LI>G_matvect_*() : corresponding to Level2 BLAS (vector-matrix) and
  390. </LI>
  391. <LI>G_vector_*() : corresponding to Level1 BLAS (vector-vector)
  392. </LI>
  393. </OL>
  394. <P>
  395. \subsubsection matrix_matrix_functions matrix -matrix functions
  396. <P>
  397. mat_struct *G_matrix_init (int rows, int cols, int ldim)<br>
  398. Initialise a matrix structure Initialise a matrix structure. Set
  399. the number of rows with
  400. the first parameter and columns with the second. The third parameter, lead
  401. dimension (>= no. of rows) needs attention by the programmer as it is
  402. related to the Fortran workspace:
  403. <P>
  404. A 3x3 matrix would be stored as
  405. <P>
  406. &nbsp;&nbsp;[ x x x _ ][ x x x _ ][ x x x _ ]
  407. <BR>
  408. <P>
  409. This work space corresponds to the sequence:
  410. <P>
  411. (1,1) (2,1) (3,1) and unused are (1,2) (2,2) ... and so on, ie. it is column major.
  412. So although the programmer uses the normal parameter sequence of (row, col)
  413. the entries traverse the data column by column instead of row by row. Each
  414. block in the workspace must be large enough (here 4) to hold a whole column
  415. (3) , and trailing spaces are unused. The number of rows (ie. size of a
  416. column) is therefore not necessarily the same size as the block size
  417. allocated to hold them (called the "lead dimension") . Some operations may
  418. produce matrices a different size from the inputs, but still write to the
  419. same workspace. This may seem awkward, but it does make for efficient code.
  420. Unfortunately, this creates a responsibility on the programmer to specify the
  421. lead dimension (>= no. of rows). In most cases the programmer can just use
  422. the rows. So for 3 rows/2 cols it would be called:
  423. <P>
  424. G_matrix_init (3, 2, 3);
  425. <P>
  426. mat_struct *G_matrix_set (mat_struct *A, int rows, int cols, int ldim)<br>
  427. Set parameters for a matrix structureSet parameters for a matrix
  428. structure that is allocated but not yet initialised fully.
  429. <P>
  430. <B>Note:</B>
  431. <BR>
  432. <P>
  433. G_matrix_set() is an alternative to G_matrix_init() . G_matrix_init()
  434. initialises and returns a pointer to a dynamically allocated matrix
  435. structure (ie. in the process heap) . G_matrix_set() sets the parameters for
  436. an already created matrix structure. In both cases the data workspace still
  437. has to be allocated dynamically.
  438. <P>
  439. Example 1:
  440. <P>
  441. \verbatim
  442. mat_struct *A;
  443. G_matrix_set (A, 4, 3, 4);
  444. \endverbatim
  445. <BR>
  446. <P>
  447. Example 2:
  448. <P>
  449. \verbatim
  450. mat_struct A; /* Allocated on the local stack */
  451. G_matrix_set (&A, 4, 3, 4) ;
  452. \endverbatim
  453. <BR>
  454. <P>
  455. mat_struct *G_matrix_add (mat_struct *mt1, mat_struct *mt2)<br>
  456. Add two matrices. Add two matrices and return the result. The receiving structure
  457. should not be initialised, as the matrix is created by the routine.
  458. <P>
  459. mat_struct *G_matrix_product (mat_struct *mt1, mat_struct *mt2)<br>
  460. Multiply two matricesMultiply two matrices and return the result.
  461. The receiving structure should not be initialised, as the matrix is created
  462. by the routine.
  463. <P>
  464. mat_struct *G_matrix_scale (mat_struct *mt1, const double c)<br>
  465. Scale matrix Scale the matrix by a given scalar value and return the
  466. result. The receiving structure should not be initialised, as the matrix is
  467. created by the routine.
  468. <P>
  469. mat_struct *G_matrix_subtract (mat_struct *mt1, mat_struct *mt2)<br>
  470. Subtract two matrices. Subtract two matrices and return the result.
  471. The receiving structure should not be initialised, as the matrix is created
  472. by the routine.
  473. <P>
  474. mat_struct *G_matrix_copy (const mat_struct *A)<br>
  475. Copy a matrix. Copy a matrix by exactly duplicating its structure.
  476. <P>
  477. mat_struct *G_matrix_transpose (mat_struct *mt)<br>
  478. Transpose a matrix. Transpose a matrix by creating a new one and
  479. populating with transposed element s.
  480. <P>
  481. void G_matrix_print (mat_struct *mt)<br>
  482. Print out a matrix. Print out a representation of the matrix to
  483. standard output.
  484. <P>
  485. int G_matrix_LU_solve (const mat_struct *mt1, mat_struct **xmat0, const
  486. mat_struct *bmat, mat_type mtype)<br>
  487. Solve a general system A.X=B. Solve a general
  488. system A.X=B, where A is a NxN matrix, X and B are NxC matrices, and we are to
  489. solve for C arrays in X given B. Uses LU decomposition.
  490. <BR>
  491. <P>
  492. Links to LAPACK function dgesv_() and similar to perform the core routine.
  493. (By default solves for a general non-symmetric matrix .)
  494. <P>
  495. mtype is a flag to indicate what kind of matrix (real/complex, Hermitian,
  496. symmetric, general etc.) is used (NONSYM, SYM, HERMITIAN) .
  497. <P>
  498. <B>Warning:</B> NOT YET COMPLETE: only some solutions' options
  499. available. Now, only general real matrix is supported.
  500. <P>
  501. mat_struct *G_matrix_inverse (mat_struct *mt)<br>
  502. Matrix inversion using
  503. LU decomposition Calls G_matrix_LU_solve() to obtain matrix inverse using
  504. LU decomposition. Returns NULL on failure.
  505. <P>
  506. void G_matrix_free (mat_struct *mt)<br> Free up allocated matrix Free up
  507. allocated matrix.
  508. <P>
  509. int G_matrix_set_element (mat_struct *mt, int rowval, int colval, double val)<br>
  510. Set the value of the (i,j) th element Set the value of the
  511. (i,j) th element to a double value. Index values are C-like ie. zero-based.
  512. The row number is given first as is conventional. Returns -1 if the
  513. accessed cell is outside the bounds.
  514. <P>
  515. double G_matrix_get_element (mat_struct *mt, int rowval, int colval)<br>
  516. Retrieve value of the (i,j) th element Retrieve the value of the
  517. (i,j) th element to a double value. Index values are C-like ie. zero-based.
  518. <P>
  519. <B>Note:</B> Does currently not set an error flag for bounds checking.
  520. <P>
  521. \section matrix_Vector_functions Matrix-Vector functions
  522. <P>
  523. vec_struct *G_matvect_get_column (mat_struct *mt, int
  524. col) Retrieve a column of matrix Retrieve a column of the matrix to a vector
  525. structure. The receiving structure should not be initialised, as the
  526. vector is created by the routine. Col 0 is the first column.
  527. <P>
  528. vec_struct *G_matvect_get_row (mat_struct *mt, int
  529. row) Retrieve a row of matrix Retrieve a row of the matrix to a vector
  530. structure. The receiving structure should not be initialised, as the
  531. vector is created by the routine. Row 0 is the first number.
  532. <P>
  533. int G_matvect_extract_vector (mat_struct *mt, vtype vt, int
  534. indx) Convert matrix to vectorConvert the current matrix structure to
  535. a vector structure. The vtype is RVEC or CVEC which specifies a row vector or
  536. column vector. The indx indicates the row/column number (zero based) .
  537. <P>
  538. int G_matvect_retrieve_matrix (vec_struct *vc) Revert a
  539. vector to matrix Revert a vector structure to a matrix .
  540. <P>
  541. \section Vector_Vector_functions Vector-Vector functions
  542. vec_struct *G_vector_init (int cells, int ldim, vtype vt) Initialise
  543. a vector structure Initialise a vector structure. The vtype is RVEC or
  544. CVEC which specifies a row vector or column vector.
  545. <P>
  546. int G_vector_set (vec_struct *A, int cells, int ldim, vtype vt, int vindx) Set
  547. parameters for vector structureSet parameters for a vector structure that is
  548. allocated but not yet initialised fully. The vtype is RVEC or
  549. CVEC which specifies a row vector or column vector.
  550. <P>
  551. vec_struct *G_vector_copy (const vec_struct *vc1, int
  552. comp_flag) Copy a vectorCopy a vector to a new vector structure. This preserves
  553. the underlying structure unless you specify DO_COMPACT comp_flag:
  554. <P>
  555. 0 Eliminate unnecessary rows (cols) in matrix
  556. <BR>
  557. 1 ... or not
  558. <P>
  559. double G_vector_norm_euclid (vec_struct *vc) Calculates euclidean
  560. norm Calculates the euclidean norm of a row or column vector, using BLAS
  561. routine dnrm2_()
  562. <P>
  563. double G_vector_norm_maxval (vec_struct *vc, int vflag) Calculates
  564. maximum value Calculates the maximum value of a row or column vector.
  565. The vflag setting defines which value to be calculated:
  566. <P>
  567. vflag:
  568. <P>
  569. 1 Indicates maximum value
  570. <BR> -1 Indicates minimum value
  571. <BR>
  572. 0 Indicates absolute value [???]
  573. <P>
  574. <B>Note:</B> More functions and wrappers will be implemented soon (11/2000) .
  575. <P>
  576. \section Notes Notes
  577. The numbers of rows and columns is stored in the matrix structure:
  578. <P>
  579. \verbatim
  580. G_message (" M1 rows: %d, M1 cols: %d", m1->rows, m1->cols);
  581. \endverbatim
  582. <P>
  583. Draft Notes:
  584. <P>
  585. * G_vector_free() can be done by G_matrix_free() .
  586. <P>
  587. \verbatim
  588. #define G_vector_free(x) G_matrix_free( (x) )
  589. \endverbatim
  590. <P>
  591. * Ax calculations can be done with G_matrix_multiply()
  592. <P>
  593. * Vector print can be done by G_matrix_print() .
  594. <P>
  595. \verbatim
  596. #define G_vector_print(x) G_matrix_print( (x) )
  597. \endverbatim
  598. <P>
  599. \section Example Example
  600. The Makefile needs a reference to $(GMATHLIB) in LIBES line.
  601. <P>
  602. Example module:
  603. <P>
  604. \verbatim
  605. #include <grass/config.h>
  606. #include <grass/gis.h>
  607. #include <grass/gmath.h>
  608. int
  609. main (int argc, char *argv[])
  610. {
  611. mat_struct *matrix;
  612. double val;
  613. /* init a 3x2 matrix */
  614. matrix = G_matrix_init (3, 2, 3);
  615. /* set cell element 0,0 in matrix to 2.2: */
  616. G_matrix_set_element (matrix, 0, 0, 2.2);
  617. /* retrieve this value */
  618. val = G_matrix_get_element (matrix, 0, 0);
  619. /* print it */
  620. G_message ( "Value: %g", val);
  621. /* Free the memory */
  622. G_matrix_free (matrix);
  623. return 0;
  624. }
  625. \endverbatim
  626. <P>
  627. See \ref Compiling_and_Installing_GRASS_Modules for a complete
  628. discussion of Makefiles.
  629. */