gmathlib.dox 29 KB

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