gmathlib.dox 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 Numerical math interface with optional support of LAPACK/BLAS
  6. <P>
  7. Author: David D. Gray<br>
  8. Additions: Brad Douglas
  9. <P>
  10. (under development)
  11. <BR>
  12. <P>
  13. This chapter provides an explanation of how numerical algebra routines from
  14. LAPACK/BLAS can be accessed through the GRASS GIS library "gmath". Most of
  15. the functions are wrapper modules for linear algebra problems, a few are locally
  16. implemented.
  17. <BR>
  18. <P>
  19. Getting BLAS/LAPACK (one package) if not already provided by the system:
  20. <BR><TT><A HREF="http://www.netlib.org/lapack/">http://www.netlib.org/lapack/</A></TT>
  21. <BR><TT><A HREF="http://netlib.bell-labs.com/netlib/master/readme.html">http://netlib.bell-labs.com/netlib/master/readme.html</A></TT>
  22. <BR>
  23. <P>
  24. Pre-compiled binaries of LAPACK/BLAS are provided on many Linux
  25. distributions.
  26. <P>
  27. \section Implementation Implementation
  28. <P>
  29. The function name convention is as follows:
  30. <P>
  31. <OL>
  32. <LI>G_matrix_*() : corresponding to Level3 BLAS (matrix -matrix) ,
  33. </LI>
  34. <LI>G_matvect_*() : corresponding to Level2 BLAS (vector-matrix) and
  35. </LI>
  36. <LI>G_vector_*() : corresponding to Level1 BLAS (vector-vector)
  37. </LI>
  38. </OL>
  39. <P>
  40. \section matrix_matrix_functions matrix -matrix functions
  41. <P>
  42. mat_struct *G_matrix_init (int rows, int cols, int ldim)<br>
  43. Initialise a matrix structure Initialise a matrix structure. Set
  44. the number of rows with
  45. the first parameter and columns with the second. The third parameter, lead
  46. dimension (>= no. of rows) needs attention by the programmer as it is
  47. related to the Fortran workspace:
  48. <P>
  49. A 3x3 matrix would be stored as
  50. <P>
  51. &nbsp;&nbsp;[ x x x _ ][ x x x _ ][ x x x _ ]
  52. <BR>
  53. <P>
  54. This work space corresponds to the sequence:
  55. <P>
  56. (1,1) (2,1) (3,1) and unused are (1,2) (2,2) ... and so on, ie. it is column major.
  57. So although the programmer uses the normal parameter sequence of (row, col)
  58. the entries traverse the data column by column instead of row by row. Each
  59. block in the workspace must be large enough (here 4) to hold a whole column
  60. (3) , and trailing spaces are unused. The number of rows (ie. size of a
  61. column) is therefore not necessarily the same size as the block size
  62. allocated to hold them (called the "lead dimension") . Some operations may
  63. produce matrices a different size from the inputs, but still write to the
  64. same workspace. This may seem awkward, but it does make for efficient code.
  65. Unfortunately, this creates a responsibility on the programmer to specify the
  66. lead dimension (>= no. of rows). In most cases the programmer can just use
  67. the rows. So for 3 rows/2 cols it would be called:
  68. <P>
  69. G_matrix_init (3, 2, 3);
  70. <P>
  71. mat_struct *G_matrix_set (mat_struct *A, int rows, int cols, int ldim)<br>
  72. Set parameters for a matrix structureSet parameters for a matrix
  73. structure that is allocated but not yet initialised fully.
  74. <P>
  75. <B>Note:</B>
  76. <BR>
  77. <P>
  78. G_matrix_set() is an alternative to G_matrix_init() . G_matrix_init()
  79. initialises and returns a pointer to a dynamically allocated matrix
  80. structure (ie. in the process heap) . G_matrix_set() sets the parameters for
  81. an already created matrix structure. In both cases the data workspace still
  82. has to be allocated dynamically.
  83. <P>
  84. Example 1:
  85. <P>
  86. \verbatim
  87. mat_struct *A;
  88. G_matrix_set (A, 4, 3, 4);
  89. \endverbatim
  90. <BR>
  91. <P>
  92. Example 2:
  93. <P>
  94. \verbatim
  95. mat_struct A; /* Allocated on the local stack */
  96. G_matrix_set (&A, 4, 3, 4) ;
  97. \endverbatim
  98. <BR>
  99. <P>
  100. mat_struct *G_matrix_add (mat_struct *mt1, mat_struct *mt2)<br>
  101. Add two matrices. Add two matrices and return the result. The receiving structure
  102. should not be initialised, as the matrix is created by the routine.
  103. <P>
  104. mat_struct *G_matrix_product (mat_struct *mt1, mat_struct *mt2)<br>
  105. Multiply two matricesMultiply two matrices and return the result.
  106. The receiving structure should not be initialised, as the matrix is created
  107. by the routine.
  108. <P>
  109. mat_struct *G_matrix_scale (mat_struct *mt1, const double c)<br>
  110. Scale matrix Scale the matrix by a given scalar value and return the
  111. result. The receiving structure should not be initialised, as the matrix is
  112. created by the routine.
  113. <P>
  114. mat_struct *G_matrix_subtract (mat_struct *mt1, mat_struct *mt2)<br>
  115. Subtract two matrices. Subtract two matrices and return the result.
  116. The receiving structure should not be initialised, as the matrix is created
  117. by the routine.
  118. <P>
  119. mat_struct *G_matrix_copy (const mat_struct *A)<br>
  120. Copy a matrix. Copy a matrix by exactly duplicating its structure.
  121. <P>
  122. mat_struct *G_matrix_transpose (mat_struct *mt)<br>
  123. Transpose a matrix. Transpose a matrix by creating a new one and
  124. populating with transposed element s.
  125. <P>
  126. void G_matrix_print (mat_struct *mt)<br>
  127. Print out a matrix. Print out a representation of the matrix to
  128. standard output.
  129. <P>
  130. int G_matrix_LU_solve (const mat_struct *mt1, mat_struct **xmat0, const
  131. mat_struct *bmat, mat_type mtype)<br>
  132. Solve a general system A.X=B. Solve a general
  133. system A.X=B, where A is a NxN matrix, X and B are NxC matrices, and we are to
  134. solve for C arrays in X given B. Uses LU decomposition.
  135. <BR>
  136. <P>
  137. Links to LAPACK function dgesv_() and similar to perform the core routine.
  138. (By default solves for a general non-symmetric matrix .)
  139. <P>
  140. mtype is a flag to indicate what kind of matrix (real/complex, Hermitian,
  141. symmetric, general etc.) is used (NONSYM, SYM, HERMITIAN) .
  142. <P>
  143. <B>Warning:</B> NOT YET COMPLETE: only some solutions' options
  144. available. Now, only general real matrix is supported.
  145. <P>
  146. mat_struct *G_matrix_inverse (mat_struct *mt)<br>
  147. Matrix inversion using
  148. LU decomposition Calls G_matrix_LU_solve() to obtain matrix inverse using
  149. LU decomposition. Returns NULL on failure.
  150. <P>
  151. void G_matrix_free (mat_struct *mt)<br> Free up allocated matrix Free up
  152. allocated matrix.
  153. <P>
  154. int G_matrix_set_element (mat_struct *mt, int rowval, int colval, double val)<br>
  155. Set the value of the (i,j) th element Set the value of the
  156. (i,j) th element to a double value. Index values are C-like ie. zero-based.
  157. The row number is given first as is conventional. Returns -1 if the
  158. accessed cell is outside the bounds.
  159. <P>
  160. double G_matrix_get_element (mat_struct *mt, int rowval, int colval)<br>
  161. Retrieve value of the (i,j) th element Retrieve the value of the
  162. (i,j) th element to a double value. Index values are C-like ie. zero-based.
  163. <P>
  164. <B>Note:</B> Does currently not set an error flag for bounds checking.
  165. <P>
  166. \section matrix_Vector_functions Matrix-Vector functions
  167. <P>
  168. vec_struct *G_matvect_get_column (mat_struct *mt, int
  169. col) Retrieve a column of matrix Retrieve a column of the matrix to a vector
  170. structure. The receiving structure should not be initialised, as the
  171. vector is created by the routine. Col 0 is the first column.
  172. <P>
  173. vec_struct *G_matvect_get_row (mat_struct *mt, int
  174. row) Retrieve a row of matrix Retrieve a row of the matrix to a vector
  175. structure. The receiving structure should not be initialised, as the
  176. vector is created by the routine. Row 0 is the first number.
  177. <P>
  178. int G_matvect_extract_vector (mat_struct *mt, vtype vt, int
  179. indx) Convert matrix to vectorConvert the current matrix structure to
  180. a vector structure. The vtype is RVEC or CVEC which specifies a row vector or
  181. column vector. The indx indicates the row/column number (zero based) .
  182. <P>
  183. int G_matvect_retrieve_matrix (vec_struct *vc) Revert a
  184. vector to matrix Revert a vector structure to a matrix .
  185. <P>
  186. \section Vector_Vector_functions Vector-Vector functions
  187. vec_struct *G_vector_init (int cells, int ldim, vtype vt) Initialise
  188. a vector structure Initialise a vector structure. The vtype is RVEC or
  189. CVEC which specifies a row vector or column vector.
  190. <P>
  191. int G_vector_set (vec_struct *A, int cells, int ldim, vtype vt, int vindx) Set
  192. parameters for vector structureSet parameters for a vector structure that is
  193. allocated but not yet initialised fully. The vtype is RVEC or
  194. CVEC which specifies a row vector or column vector.
  195. <P>
  196. vec_struct *G_vector_copy (const vec_struct *vc1, int
  197. comp_flag) Copy a vectorCopy a vector to a new vector structure. This preserves
  198. the underlying structure unless you specify DO_COMPACT comp_flag:
  199. <P>
  200. 0 Eliminate unnecessary rows (cols) in matrix
  201. <BR>
  202. 1 ... or not
  203. <P>
  204. double G_vector_norm_euclid (vec_struct *vc) Calculates euclidean
  205. norm Calculates the euclidean norm of a row or column vector, using BLAS
  206. routine dnrm2_()
  207. <P>
  208. double G_vector_norm_maxval (vec_struct *vc, int vflag) Calculates
  209. maximum value Calculates the maximum value of a row or column vector.
  210. The vflag setting defines which value to be calculated:
  211. <P>
  212. vflag:
  213. <P>
  214. 1 Indicates maximum value
  215. <BR> -1 Indicates minimum value
  216. <BR>
  217. 0 Indicates absolute value [???]
  218. <P>
  219. <B>Note:</B> More functions and wrappers will be implemented soon (11/2000) .
  220. <P>
  221. \section Notes Notes
  222. The numbers of rows and columns is stored in the matrix structure:
  223. <P>
  224. \verbatim
  225. G_message (" M1 rows: %d, M1 cols: %d", m1->rows, m1->cols);
  226. \endverbatim
  227. <P>
  228. Draft Notes:
  229. <P>
  230. * G_vector_free() can be done by G_matrix_free() .
  231. <P>
  232. \verbatim
  233. #define G_vector_free(x) G_matrix_free( (x) )
  234. \endverbatim
  235. <P>
  236. * Ax calculations can be done with G_matrix_multiply()
  237. <P>
  238. * Vector print can be done by G_matrix_print() .
  239. <P>
  240. \verbatim
  241. #define G_vector_print(x) G_matrix_print( (x) )
  242. \endverbatim
  243. <P>
  244. \section Example Example
  245. The Makefile needs a reference to $(GMATHLIB) in LIBES line.
  246. <P>
  247. Example module:
  248. <P>
  249. \verbatim
  250. #include <grass/config.h>
  251. #include <grass/gis.h>
  252. #include <grass/gmath.h>
  253. int
  254. main (int argc, char *argv[])
  255. {
  256. mat_struct *matrix;
  257. double val;
  258. /* init a 3x2 matrix */
  259. matrix = G_matrix_init (3, 2, 3);
  260. /* set cell element 0,0 in matrix to 2.2: */
  261. G_matrix_set_element (matrix, 0, 0, 2.2);
  262. /* retrieve this value */
  263. val = G_matrix_get_element (matrix, 0, 0);
  264. /* print it */
  265. G_message ( "Value: %g", val);
  266. /* Free the memory */
  267. G_matrix_free (matrix);
  268. return 0;
  269. }
  270. \endverbatim
  271. <P>
  272. See \ref Compiling_and_Installing_GRASS_Modules for a complete
  273. discussion of Makefiles.
  274. */