Two-dimensional-arrays.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <html lang="en">
  2. <head>
  3. <title>Two dimensional arrays - avram - a virtual machine code interpreter</title>
  4. <meta http-equiv="Content-Type" content="text/html">
  5. <meta name="description" content="avram - a virtual machine code interpreter">
  6. <meta name="generator" content="makeinfo 4.13">
  7. <link title="Top" rel="start" href="index.html#Top">
  8. <link rel="up" href="Type-Conversions.html#Type-Conversions" title="Type Conversions">
  9. <link rel="prev" href="One-dimensional-arrays.html#One-dimensional-arrays" title="One dimensional arrays">
  10. <link rel="next" href="Related-utility-functions.html#Related-utility-functions" title="Related utility functions">
  11. <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <style type="text/css"><!--
  14. pre.display { font-family:inherit }
  15. pre.format { font-family:inherit }
  16. pre.smalldisplay { font-family:inherit; font-size:smaller }
  17. pre.smallformat { font-family:inherit; font-size:smaller }
  18. pre.smallexample { font-size:smaller }
  19. pre.smalllisp { font-size:smaller }
  20. span.sc { font-variant:small-caps }
  21. span.roman { font-family:serif; font-weight:normal; }
  22. span.sansserif { font-family:sans-serif; font-weight:normal; }
  23. --></style>
  24. </head>
  25. <body>
  26. <div class="node">
  27. <a name="Two-dimensional-arrays"></a>
  28. <p>
  29. Next:&nbsp;<a rel="next" accesskey="n" href="Related-utility-functions.html#Related-utility-functions">Related utility functions</a>,
  30. Previous:&nbsp;<a rel="previous" accesskey="p" href="One-dimensional-arrays.html#One-dimensional-arrays">One dimensional arrays</a>,
  31. Up:&nbsp;<a rel="up" accesskey="u" href="Type-Conversions.html#Type-Conversions">Type Conversions</a>
  32. <hr>
  33. </div>
  34. <h5 class="subsubsection">3.1.4.3 Two dimensional arrays</h5>
  35. <p>Several other functions in <samp><span class="file">matcon.h</span></samp> are meant to support
  36. conversions between matrices represented as lists of lists and arrays
  37. in a variety of representations. Dense matrices either square or
  38. <a name="index-matrices-450"></a>rectangular are accommodated, and symmetric square matrices can be
  39. stored with redundant entries omitted in either upper trangular or
  40. lower triangular format.
  41. <p>Similarly to the vector operations (<a href="One-dimensional-arrays.html#One-dimensional-arrays">One dimensional arrays</a>)
  42. these functions are intended to allow a developer to present an
  43. interface to external libraries based on lists rather than arrays.
  44. <p>The preferred convention for virtual code applications is to represent
  45. a matrix as a list of lists of entities (typically numbers), with
  46. one list for each row of the matrix. For example, a 3 by 3 matrix
  47. containing a value of <code>aij</code> in the <code>i</code>-th row and the
  48. <code>j</code>-th column would be represented by this list of three lists.
  49. <pre class="example"> &lt;
  50. &lt;a11,a12,a13&gt;,
  51. &lt;a21,a22,a23&gt;,
  52. &lt;a31,a32,a33&gt;&gt;
  53. </pre>
  54. <p class="noindent">Such a representation is convenient for manipulation by
  55. virtual machine combinators, for example <code>transpose</code>
  56. (<a href="Transpose.html#Transpose">Transpose</a>), and is readily identified with the matrix
  57. it represents.
  58. <p>If a matrix is symmetric (that is, with <code>aij</code> equal to
  59. <code>aji</code> for all values of <code>i</code> and <code>j</code>), only the lower
  60. triangular portion needs to be stored because the other entries are
  61. <a name="index-triangular-matrix-451"></a>redundant. The list representatation would be something like this.
  62. <pre class="example"> &lt;
  63. &lt;a11&gt;,
  64. &lt;a21,a22&gt;,
  65. &lt;a31,a32,a33&gt;&gt;
  66. </pre>
  67. <p>Another alternative for representing a symmetric matrix is to store only the
  68. upper triangular portion. In this case, a list such as the following would be used.
  69. <pre class="example"> &lt;
  70. &lt;a11,a12,a13&gt;,
  71. &lt;a22,a23&gt;,
  72. &lt;a33&gt;&gt;
  73. </pre>
  74. <p class="noindent">The upper and lower triangular representations are distinguishable by
  75. whether or not the row lengths form an increasing sequence.
  76. <p>In addition to representing symmetric matrices, these upper and lower
  77. <a name="index-symmetric-matrix-452"></a>triangular forms are also appropriate for representing matrices whose
  78. remaining entries are zero, such as the factors in an LU
  79. decomposition.
  80. <a name="index-LU-decomposition-453"></a>
  81. <div class="defun">
  82. &mdash; Function: void <b>*avm_matrix_of_list</b> (<var>int square, int upper_triangular, int lower_triangular, int column_major, list operand, size_t item_size, list *message, int *fault</var>)<var><a name="index-g_t_002aavm_005fmatrix_005fof_005flist-454"></a></var><br>
  83. <blockquote>
  84. <p>This function converts a matrix in one of the list representations
  85. above to a contiguous array according to the given specifications.
  86. The array can contain elements of any fixed sized type of size
  87. <var>item_size</var>. The memory for it is allocated by this function and
  88. it should be freed by the caller when no longer needed.
  89. <p>The input matrix is given by the list parameter, <var>operand</var>, and
  90. its format is described by the integer parameters <var>square</var>,
  91. <var>upper_triangular</var>, and <var>lower_triangular</var>. The number of
  92. bytes occupied by each entry is given by <var>item_size</var>.
  93. <p>To the extent these specifications are redundant, they are used for
  94. validation. If any of the following conditions is not met, the integer
  95. referenced by <var>fault</var> is assigned a non-zero value and a copy
  96. of the message <code>&lt;'bad matrix specification'&gt;</code> represented as
  97. a list is assigned to the list referenced by <var>message</var>. Errors
  98. are also possible due to insufficient memory.
  99. <ul>
  100. <li>The <var>operand</var> must be a list of lists of lists such that
  101. each item of each item is has a length of <var>item_size</var>,
  102. and its items consist of character representations as
  103. required by <code>avm_value_of_list</code> (<a href="Primitive-types.html#Primitive-types">Primitive types</a>).
  104. <li>If the lengths of the top level lists in the <var>operand</var> form an
  105. increasing sequence, the lower triangular representation is assumed
  106. and the <var>lower_triangular</var> parameter must have a non-zero value.
  107. <li>If the lengths of the top level lists in the <var>operand</var> form a
  108. decreasing sequence, the upper triangular representation is assumed
  109. and the <var>upper_triangular</var> parameter must have a non-zero value.
  110. <li>At least one of <var>upper_triangular</var> or <var>lower_triangular</var> must
  111. be zero.
  112. <li>If <var>square</var> has a non-zero value, then either all items of the
  113. <var>operand</var> must have the same length as the operand, or if it's
  114. triangular, then the longest one must have the same length as the
  115. operand.
  116. <li>If the <var>operand</var> is neither square nor a triangular form, all
  117. items of it are required to have the same length.
  118. </ul>
  119. <p>The parameters <var>upper_triangular</var> or <var>lower_triangular</var> may be
  120. set to non-zero values even if the <var>operand</var> is not in one of the
  121. upper or lower triangular forms discussed above. In this case, the
  122. <var>operand</var> must be square or rectangular (i.e., with all items the
  123. same length), and the following interpretations apply.
  124. <ul>
  125. <li>If <var>upper_triangular</var> is non-zero, the diagonal elements and the
  126. upper triangular portion of the input matrix are copied to the output.
  127. The lower triangle of the input is ignored and the lower triangle of
  128. the output is left uninitialized.
  129. <li>If <var>lower_triangular</var> is non-zero, the diagonal elements and the
  130. lower triangular portion of the input matrix are copied to the output.
  131. The upper triangle of the input is ignored and the upper triangle of
  132. the output is left uninitialized.
  133. </ul>
  134. <p>The <var>column_major</var> parameter affects the form of the output array.
  135. If it is zero, then each row of the input matrix is stored in a
  136. contiguous block of memory in the output array, and if it is non-zero,
  137. each column is stored contiguously.
  138. <a name="index-Fortran-455"></a>The latter representation is also
  139. known as Fortran order and may be required by library functions
  140. written in Fortran.
  141. <p>In all cases when a triangular form is specified, part of the output
  142. matrix is left uninitialized. The redundant entries may be assigned if
  143. required by the <code>avm_reflect_matrix</code> function (<a href="Related-utility-functions.html#Related-utility-functions">Related utility functions</a>).
  144. </p></blockquote></div>
  145. <div class="defun">
  146. &mdash; Function: list <b>avm_list_of_matrix</b> (<var>void *matrix, int rows, int cols, size_t item_size, int *fault</var>)<var><a name="index-avm_005flist_005fof_005fmatrix-456"></a></var><br>
  147. <blockquote><p>This function performs an inverse operation to
  148. <code>avm_matrix_of_list</code> by taking the address of a matrix stored as
  149. a contiguous array in the parameter <var>matrix</var> and constructing the
  150. list representation as discussed above. Only square and rectangular
  151. matrices in row major order are supported, but see
  152. <code>avm_matrix_transposition</code> for a way to convert between row major
  153. <a name="index-column-major-order-457"></a>and column major order (<a href="Related-utility-functions.html#Related-utility-functions">Related utility functions</a>).
  154. <p>The parameters <var>rows</var>, <var>cols</var>, and <var>item_size</var> describe
  155. the form of the matrix. The list returned as a result will have a
  156. length of <var>rows</var>, and each item will be a list of length
  157. <var>cols</var>. Each item of the result corresponds to a row of the
  158. matrix, and each item of the items represents the an entry of the
  159. matrix as a list of length <var>item_size</var>. These items could be
  160. passed to <code>avm_value_of_list</code>, for example, to obtain their
  161. values (<a href="Primitive-types.html#Primitive-types">Primitive types</a>).
  162. <p>Memory is allocated by this function to create the list, which can be
  163. reclaimed by <code>avm_dispose</code> (<a href="Simple-Operations.html#Simple-Operations">Simple Operations</a>). If there is
  164. insufficient memory, the integer referenced by <var>fault</var> is assigned
  165. a non-zero value and the result returned is a list representation of
  166. the message <code>&lt;'memory overflow'&gt;</code>. The error message be reclaimed
  167. by the caller as well using <code>avm_dispose</code>.
  168. </p></blockquote></div>
  169. <p>A packed storage representation for symmetric square matrices and
  170. <a name="index-packed-arrays-458"></a>triangular matrices is of interest because it is used by some library
  171. functions, notably those in <code>LAPACK</code>, to save memory and thereby
  172. accommodate larger problems. In this representation, column major
  173. <a name="index-column-major-order-459"></a>order is assumed, and either the lower or the upper triangle of the
  174. matrix is not explicitly stored. For example, a lower triangular
  175. <a name="index-triangular-matrix-460"></a>matrix whose list representation corresponds to
  176. <pre class="example"> &lt;
  177. &lt;a11&gt;,
  178. &lt;a21,a22&gt;,
  179. &lt;a31,a32,a33&gt;,
  180. &lt;a41,a42,a43,a44&gt;&gt;
  181. </pre>
  182. <p class="noindent">would be stored according to the memory map
  183. <a name="index-matrix-memory-map-461"></a>
  184. <pre class="example"> [a11 a21 a31 a41 a22 a32 a42 a33 a43 a44]
  185. </pre>
  186. <p class="noindent">with <code>a11</code> at the beginning address. An upper triangular matrix
  187. <pre class="example"> &lt;
  188. &lt;a11,a12,a13,a14&gt;,
  189. &lt;a22,a23,a24&gt;,
  190. &lt;a33,a34&gt;,
  191. &lt;a44&gt;&gt;
  192. </pre>
  193. <p class="noindent">would be stored according to the memory map
  194. <pre class="example"> [a11 a12 a22 a13 a23 a33 a14 a24 a34 a44].
  195. </pre>
  196. <p>A couple of functions converting between list representations and
  197. packed array format are provided as described below.
  198. <div class="defun">
  199. &mdash; Function: void <b>*avm_packed_matrix_of_list</b> (<var>int upper_triangular, list operand, int n, size_t item_size, list *message, int *fault</var>)<var><a name="index-g_t_002aavm_005fpacked_005fmatrix_005fof_005flist-462"></a></var><br>
  200. <blockquote>
  201. <p>If the <var>operand</var> is a list in one of the triangular forms
  202. explained above, then the <var>upper_triangular</var> parameter must be
  203. consisitent with it, being non-zero if the <var>operand</var> is upper
  204. triangular and zero otherwise.
  205. <p>If the <var>operand</var> is not in a triangular form, then each item of
  206. the operand must be a list of length <var>n</var>. In this case, the
  207. <var>upper_triangular</var> parameter indicates which triangle of the
  208. operand should be copied to the result, and the other triangle is
  209. ignored.
  210. <p>In either case, the operand must have a length of <var>n</var>, and the
  211. items of its items must be lists of length <var>item_size</var> containing
  212. character representations as required by <code>avm_value_of_list</code>
  213. (<a href="Primitive-types.html#Primitive-types">Primitive types</a>).
  214. <p>If the input parameters are inconsistent or if there is insufficient
  215. memory to allocate the result, the integer referenced by <var>fault</var>
  216. is assigned a non-zero value, and the list referenced by <var>message</var>
  217. is assigned a copy of the list representation of <code>&lt;'bad matrix
  218. specification'&gt;</code> or <code>&lt;'memory overflow'&gt;</code>, respectively. A
  219. non-empty message must be reclaimed by the caller using
  220. <code>avm_dispose</code> (<a href="Simple-Operations.html#Simple-Operations">Simple Operations</a>).
  221. <p>If there are no errors, the result is a pointer to a packed array
  222. representation of the <var>operand</var> as explained above. The memory for
  223. this result is allocated by this function and should be freed by the
  224. caller when no longer required. The number of bytes allocated will be
  225. <var>item_size</var> * (<var>n</var> * (<var>n</var> + 1))/2.
  226. </p></blockquote></div>
  227. <div class="defun">
  228. &mdash; Function: list <b>avm_list_of_packed_matrix</b> (<var>int upper_trianguler,void *operand, int n, size_t item_size, int *fault</var>)<var><a name="index-avm_005flist_005fof_005fpacked_005fmatrix-463"></a></var><br>
  229. <blockquote>
  230. <p>This function performs an inverse operation to that of
  231. <code>avm_packed_matrix_of_list</code> given the address of a packed matrix
  232. stored according to one of the memory maps discussed above. The
  233. <var>operand</var> parameter holds the address, the parameter <var>n</var> gives
  234. the number of rows, and the <var>upper_triangular</var> parameter specifies
  235. which of the two possible memory maps to assume.
  236. <p>If there is sufficient memory, the result returned is a list in one of
  237. the triangular forms described above, being upper triangular if the
  238. <var>upper_triangular</var> parameter is non-zero, with values of length
  239. <var>item_size</var> taken from the array.
  240. <p>In the event of a memory overflow, the integer referenced by
  241. <var>fault</var> is assigned a non-zero value and the result is a copy of
  242. the message <code>&lt;'memory overflow'&gt;</code> represented as a list. A
  243. <a name="index-segmentation-fault-464"></a>segmentation fault is possible if this function is passed an invalid
  244. pointer or dimension.
  245. </p></blockquote></div>
  246. </body></html>