Two-dimensional-arrays.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
  2. <html>
  3. <!-- Created on November 8, 2012 by texi2html 1.82
  4. texi2html was written by:
  5. Lionel Cons <[email protected]> (original author)
  6. Karl Berry <[email protected]>
  7. Olaf Bachmann <[email protected]>
  8. and many others.
  9. Maintained by: Many creative people.
  10. Send bugs and suggestions to <[email protected]>
  11. -->
  12. <head>
  13. <title>avram - a virtual machine code interpreter: 3.1.4.3 Two dimensional arrays</title>
  14. <meta name="description" content="avram - a virtual machine code interpreter: 3.1.4.3 Two dimensional arrays">
  15. <meta name="keywords" content="avram - a virtual machine code interpreter: 3.1.4.3 Two dimensional arrays">
  16. <meta name="resource-type" content="document">
  17. <meta name="distribution" content="global">
  18. <meta name="Generator" content="texi2html 1.82">
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <style type="text/css">
  21. <!--
  22. a.summary-letter {text-decoration: none}
  23. blockquote.smallquotation {font-size: smaller}
  24. pre.display {font-family: serif}
  25. pre.format {font-family: serif}
  26. pre.menu-comment {font-family: serif}
  27. pre.menu-preformatted {font-family: serif}
  28. pre.smalldisplay {font-family: serif; font-size: smaller}
  29. pre.smallexample {font-size: smaller}
  30. pre.smallformat {font-family: serif; font-size: smaller}
  31. pre.smalllisp {font-size: smaller}
  32. span.roman {font-family:serif; font-weight:normal;}
  33. span.sansserif {font-family:sans-serif; font-weight:normal;}
  34. ul.toc {list-style: none}
  35. -->
  36. </style>
  37. </head>
  38. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  39. <a name="Two-dimensional-arrays"></a>
  40. <table cellpadding="1" cellspacing="1" border="0">
  41. <tr><td valign="middle" align="left">[<a href="One-dimensional-arrays.html#One-dimensional-arrays" title="Previous section in reading order"> &lt; </a>]</td>
  42. <td valign="middle" align="left">[<a href="Related-utility-functions.html#Related-utility-functions" title="Next section in reading order"> &gt; </a>]</td>
  43. <td valign="middle" align="left"> &nbsp; </td>
  44. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  45. <td valign="middle" align="left">[<a href="Type-Conversions.html#Type-Conversions" title="Up section"> Up </a>]</td>
  46. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next chapter"> &gt;&gt; </a>]</td>
  47. <td valign="middle" align="left"> &nbsp; </td>
  48. <td valign="middle" align="left"> &nbsp; </td>
  49. <td valign="middle" align="left"> &nbsp; </td>
  50. <td valign="middle" align="left"> &nbsp; </td>
  51. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  52. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  53. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  54. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  55. </tr></table>
  56. <hr size="1">
  57. <a name="Two-dimensional-arrays-1"></a>
  58. <h4 class="subsubsection">3.1.4.3 Two dimensional arrays</h4>
  59. <p>Several other functions in &lsquo;<tt>matcon.h</tt>&rsquo; are meant to support
  60. conversions between matrices represented as lists of lists and arrays
  61. in a variety of representations. Dense matrices either square or
  62. <a name="index-matrices"></a>
  63. rectangular are accommodated, and symmetric square matrices can be
  64. stored with redundant entries omitted in either upper trangular or
  65. lower triangular format.
  66. </p>
  67. <p>Similarly to the vector operations (<a href="One-dimensional-arrays.html#One-dimensional-arrays">One dimensional arrays</a>)
  68. these functions are intended to allow a developer to present an
  69. interface to external libraries based on lists rather than arrays.
  70. </p>
  71. <p>The preferred convention for virtual code applications is to represent
  72. a matrix as a list of lists of entities (typically numbers), with
  73. one list for each row of the matrix. For example, a 3 by 3 matrix
  74. containing a value of <code>aij</code> in the <code>i</code>-th row and the
  75. <code>j</code>-th column would be represented by this list of three lists.
  76. </p>
  77. <table><tr><td>&nbsp;</td><td><pre class="example">&lt;
  78. &lt;a11,a12,a13&gt;,
  79. &lt;a21,a22,a23&gt;,
  80. &lt;a31,a32,a33&gt;&gt;
  81. </pre></td></tr></table>
  82. <p>Such a representation is convenient for manipulation by
  83. virtual machine combinators, for example <code>transpose</code>
  84. (<a href="Transpose.html#Transpose">Transpose</a>), and is readily identified with the matrix
  85. it represents.
  86. </p>
  87. <p>If a matrix is symmetric (that is, with <code>aij</code> equal to
  88. <code>aji</code> for all values of <code>i</code> and <code>j</code>), only the lower
  89. triangular portion needs to be stored because the other entries are
  90. <a name="index-triangular-matrix"></a>
  91. redundant. The list representatation would be something like this.
  92. </p>
  93. <table><tr><td>&nbsp;</td><td><pre class="example">&lt;
  94. &lt;a11&gt;,
  95. &lt;a21,a22&gt;,
  96. &lt;a31,a32,a33&gt;&gt;
  97. </pre></td></tr></table>
  98. <p>Another alternative for representing a symmetric matrix is to store only the
  99. upper triangular portion. In this case, a list such as the following would be used.
  100. </p>
  101. <table><tr><td>&nbsp;</td><td><pre class="example">&lt;
  102. &lt;a11,a12,a13&gt;,
  103. &lt;a22,a23&gt;,
  104. &lt;a33&gt;&gt;
  105. </pre></td></tr></table>
  106. <p>The upper and lower triangular representations are distinguishable by
  107. whether or not the row lengths form an increasing sequence.
  108. </p>
  109. <p>In addition to representing symmetric matrices, these upper and lower
  110. <a name="index-symmetric-matrix"></a>
  111. triangular forms are also appropriate for representing matrices whose
  112. remaining entries are zero, such as the factors in an LU
  113. decomposition.
  114. <a name="index-LU-decomposition"></a>
  115. </p>
  116. <dl>
  117. <dt><a name="index-_002aavm_005fmatrix_005fof_005flist"></a><u>Function:</u> void <b>*avm_matrix_of_list</b><i> (int <var>square</var>, int <var>upper_triangular</var>, int <var>lower_triangular</var>, int <var>column_major</var>, list <var>operand</var>, size_t <var>item_size</var>, list *<var>message</var>, int *<var>fault</var>)</i></dt>
  118. <dd>
  119. <p>This function converts a matrix in one of the list representations
  120. above to a contiguous array according to the given specifications.
  121. The array can contain elements of any fixed sized type of size
  122. <var>item_size</var>. The memory for it is allocated by this function and
  123. it should be freed by the caller when no longer needed.
  124. </p>
  125. <p>The input matrix is given by the list parameter, <var>operand</var>, and
  126. its format is described by the integer parameters <var>square</var>,
  127. <var>upper_triangular</var>, and <var>lower_triangular</var>. The number of
  128. bytes occupied by each entry is given by <var>item_size</var>.
  129. </p>
  130. <p>To the extent these specifications are redundant, they are used for
  131. validation. If any of the following conditions is not met, the integer
  132. referenced by <var>fault</var> is assigned a non-zero value and a copy
  133. of the message <code>&lt;'bad matrix specification'&gt;</code> represented as
  134. a list is assigned to the list referenced by <var>message</var>. Errors
  135. are also possible due to insufficient memory.
  136. </p>
  137. <ul>
  138. <li>
  139. The <var>operand</var> must be a list of lists of lists such that
  140. each item of each item is has a length of <var>item_size</var>,
  141. and its items consist of character representations as
  142. required by <code>avm_value_of_list</code> (<a href="Primitive-types.html#Primitive-types">Primitive types</a>).
  143. </li><li>
  144. If the lengths of the top level lists in the <var>operand</var> form an
  145. increasing sequence, the lower triangular representation is assumed
  146. and the <var>lower_triangular</var> parameter must have a non-zero value.
  147. </li><li>
  148. If the lengths of the top level lists in the <var>operand</var> form a
  149. decreasing sequence, the upper triangular representation is assumed
  150. and the <var>upper_triangular</var> parameter must have a non-zero value.
  151. </li><li>
  152. At least one of <var>upper_triangular</var> or <var>lower_triangular</var> must
  153. be zero.
  154. </li><li>
  155. If <var>square</var> has a non-zero value, then either all items of the
  156. <var>operand</var> must have the same length as the operand, or if it&rsquo;s
  157. triangular, then the longest one must have the same length as the
  158. operand.
  159. </li><li>
  160. If the <var>operand</var> is neither square nor a triangular form, all
  161. items of it are required to have the same length.
  162. </li></ul>
  163. <p>The parameters <var>upper_triangular</var> or <var>lower_triangular</var> may be
  164. set to non-zero values even if the <var>operand</var> is not in one of the
  165. upper or lower triangular forms discussed above. In this case, the
  166. <var>operand</var> must be square or rectangular (i.e., with all items the
  167. same length), and the following interpretations apply.
  168. </p>
  169. <ul>
  170. <li>
  171. If <var>upper_triangular</var> is non-zero, the diagonal elements and the
  172. upper triangular portion of the input matrix are copied to the output.
  173. The lower triangle of the input is ignored and the lower triangle of
  174. the output is left uninitialized.
  175. </li><li>
  176. If <var>lower_triangular</var> is non-zero, the diagonal elements and the
  177. lower triangular portion of the input matrix are copied to the output.
  178. The upper triangle of the input is ignored and the upper triangle of
  179. the output is left uninitialized.
  180. </li></ul>
  181. <p>The <var>column_major</var> parameter affects the form of the output array.
  182. If it is zero, then each row of the input matrix is stored in a
  183. contiguous block of memory in the output array, and if it is non-zero,
  184. each column is stored contiguously.
  185. <a name="index-Fortran"></a>
  186. The latter representation is also
  187. known as Fortran order and may be required by library functions
  188. written in Fortran.
  189. </p>
  190. <p>In all cases when a triangular form is specified, part of the output
  191. matrix is left uninitialized. The redundant entries may be assigned if
  192. required by the <code>avm_reflect_matrix</code> function (<a href="Related-utility-functions.html#Related-utility-functions">Related utility functions</a>).
  193. </p></dd></dl>
  194. <dl>
  195. <dt><a name="index-avm_005flist_005fof_005fmatrix"></a><u>Function:</u> list <b>avm_list_of_matrix</b><i> (void *<var>matrix</var>, int <var>rows</var>, int <var>cols</var>, size_t <var>item_size</var>, int *<var>fault</var>)</i></dt>
  196. <dd><p>This function performs an inverse operation to
  197. <code>avm_matrix_of_list</code> by taking the address of a matrix stored as
  198. a contiguous array in the parameter <var>matrix</var> and constructing the
  199. list representation as discussed above. Only square and rectangular
  200. matrices in row major order are supported, but see
  201. <code>avm_matrix_transposition</code> for a way to convert between row major
  202. <a name="index-column-major-order"></a>
  203. and column major order (<a href="Related-utility-functions.html#Related-utility-functions">Related utility functions</a>).
  204. </p>
  205. <p>The parameters <var>rows</var>, <var>cols</var>, and <var>item_size</var> describe
  206. the form of the matrix. The list returned as a result will have a
  207. length of <var>rows</var>, and each item will be a list of length
  208. <var>cols</var>. Each item of the result corresponds to a row of the
  209. matrix, and each item of the items represents the an entry of the
  210. matrix as a list of length <var>item_size</var>. These items could be
  211. passed to <code>avm_value_of_list</code>, for example, to obtain their
  212. values (<a href="Primitive-types.html#Primitive-types">Primitive types</a>).
  213. </p>
  214. <p>Memory is allocated by this function to create the list, which can be
  215. reclaimed by <code>avm_dispose</code> (<a href="Simple-Operations.html#Simple-Operations">Simple Operations</a>). If there is
  216. insufficient memory, the integer referenced by <var>fault</var> is assigned
  217. a non-zero value and the result returned is a list representation of
  218. the message <code>&lt;'memory overflow'&gt;</code>. The error message be reclaimed
  219. by the caller as well using <code>avm_dispose</code>.
  220. </p></dd></dl>
  221. <p>A packed storage representation for symmetric square matrices and
  222. <a name="index-packed-arrays"></a>
  223. triangular matrices is of interest because it is used by some library
  224. functions, notably those in <code>LAPACK</code>, to save memory and thereby
  225. accommodate larger problems. In this representation, column major
  226. <a name="index-column-major-order-1"></a>
  227. order is assumed, and either the lower or the upper triangle of the
  228. matrix is not explicitly stored. For example, a lower triangular
  229. <a name="index-triangular-matrix-1"></a>
  230. matrix whose list representation corresponds to
  231. </p>
  232. <table><tr><td>&nbsp;</td><td><pre class="example">&lt;
  233. &lt;a11&gt;,
  234. &lt;a21,a22&gt;,
  235. &lt;a31,a32,a33&gt;,
  236. &lt;a41,a42,a43,a44&gt;&gt;
  237. </pre></td></tr></table>
  238. <p>would be stored according to the memory map
  239. <a name="index-matrix-memory-map"></a>
  240. </p>
  241. <table><tr><td>&nbsp;</td><td><pre class="example">[a11 a21 a31 a41 a22 a32 a42 a33 a43 a44]
  242. </pre></td></tr></table>
  243. <p>with <code>a11</code> at the beginning address. An upper triangular matrix
  244. </p>
  245. <table><tr><td>&nbsp;</td><td><pre class="example">&lt;
  246. &lt;a11,a12,a13,a14&gt;,
  247. &lt;a22,a23,a24&gt;,
  248. &lt;a33,a34&gt;,
  249. &lt;a44&gt;&gt;
  250. </pre></td></tr></table>
  251. <p>would be stored according to the memory map
  252. </p>
  253. <table><tr><td>&nbsp;</td><td><pre class="example">[a11 a12 a22 a13 a23 a33 a14 a24 a34 a44].
  254. </pre></td></tr></table>
  255. <p>A couple of functions converting between list representations and
  256. packed array format are provided as described below.
  257. </p>
  258. <dl>
  259. <dt><a name="index-_002aavm_005fpacked_005fmatrix_005fof_005flist"></a><u>Function:</u> void <b>*avm_packed_matrix_of_list</b><i> (int <var>upper_triangular</var>, list <var>operand</var>, int <var>n</var>, size_t <var>item_size</var>, list *<var>message</var>, int *<var>fault</var>)</i></dt>
  260. <dd>
  261. <p>If the <var>operand</var> is a list in one of the triangular forms
  262. explained above, then the <var>upper_triangular</var> parameter must be
  263. consisitent with it, being non-zero if the <var>operand</var> is upper
  264. triangular and zero otherwise.
  265. </p>
  266. <p>If the <var>operand</var> is not in a triangular form, then each item of
  267. the operand must be a list of length <var>n</var>. In this case, the
  268. <var>upper_triangular</var> parameter indicates which triangle of the
  269. operand should be copied to the result, and the other triangle is
  270. ignored.
  271. </p>
  272. <p>In either case, the operand must have a length of <var>n</var>, and the
  273. items of its items must be lists of length <var>item_size</var> containing
  274. character representations as required by <code>avm_value_of_list</code>
  275. (<a href="Primitive-types.html#Primitive-types">Primitive types</a>).
  276. </p>
  277. <p>If the input parameters are inconsistent or if there is insufficient
  278. memory to allocate the result, the integer referenced by <var>fault</var>
  279. is assigned a non-zero value, and the list referenced by <var>message</var>
  280. is assigned a copy of the list representation of <code>&lt;'bad matrix
  281. specification'&gt;</code> or <code>&lt;'memory overflow'&gt;</code>, respectively. A
  282. non-empty message must be reclaimed by the caller using
  283. <code>avm_dispose</code> (<a href="Simple-Operations.html#Simple-Operations">Simple Operations</a>).
  284. </p>
  285. <p>If there are no errors, the result is a pointer to a packed array
  286. representation of the <var>operand</var> as explained above. The memory for
  287. this result is allocated by this function and should be freed by the
  288. caller when no longer required. The number of bytes allocated will be
  289. <var>item_size</var> * (<var>n</var> * (<var>n</var> + 1))/2.
  290. </p></dd></dl>
  291. <dl>
  292. <dt><a name="index-avm_005flist_005fof_005fpacked_005fmatrix"></a><u>Function:</u> list <b>avm_list_of_packed_matrix</b><i> (int <var>upper_trianguler</var>,void *<var>operand</var>, int <var>n</var>, size_t <var>item_size</var>, int *<var>fault</var>)</i></dt>
  293. <dd>
  294. <p>This function performs an inverse operation to that of
  295. <code>avm_packed_matrix_of_list</code> given the address of a packed matrix
  296. stored according to one of the memory maps discussed above. The
  297. <var>operand</var> parameter holds the address, the parameter <var>n</var> gives
  298. the number of rows, and the <var>upper_triangular</var> parameter specifies
  299. which of the two possible memory maps to assume.
  300. </p>
  301. <p>If there is sufficient memory, the result returned is a list in one of
  302. the triangular forms described above, being upper triangular if the
  303. <var>upper_triangular</var> parameter is non-zero, with values of length
  304. <var>item_size</var> taken from the array.
  305. </p>
  306. <p>In the event of a memory overflow, the integer referenced by
  307. <var>fault</var> is assigned a non-zero value and the result is a copy of
  308. the message <code>&lt;'memory overflow'&gt;</code> represented as a list. A
  309. <a name="index-segmentation-fault-4"></a>
  310. segmentation fault is possible if this function is passed an invalid
  311. pointer or dimension.
  312. </p></dd></dl>
  313. <hr size="1">
  314. <table cellpadding="1" cellspacing="1" border="0">
  315. <tr><td valign="middle" align="left">[<a href="One-dimensional-arrays.html#One-dimensional-arrays" title="Previous section in reading order"> &lt; </a>]</td>
  316. <td valign="middle" align="left">[<a href="Related-utility-functions.html#Related-utility-functions" title="Next section in reading order"> &gt; </a>]</td>
  317. <td valign="middle" align="left"> &nbsp; </td>
  318. <td valign="middle" align="left">[<a href="Library-Reference.html#Library-Reference" title="Beginning of this chapter or previous chapter"> &lt;&lt; </a>]</td>
  319. <td valign="middle" align="left">[<a href="Type-Conversions.html#Type-Conversions" title="Up section"> Up </a>]</td>
  320. <td valign="middle" align="left">[<a href="Character-Table.html#Character-Table" title="Next chapter"> &gt;&gt; </a>]</td>
  321. <td valign="middle" align="left"> &nbsp; </td>
  322. <td valign="middle" align="left"> &nbsp; </td>
  323. <td valign="middle" align="left"> &nbsp; </td>
  324. <td valign="middle" align="left"> &nbsp; </td>
  325. <td valign="middle" align="left">[<a href="avram.html#Top" title="Cover (top) of document">Top</a>]</td>
  326. <td valign="middle" align="left">[<a href="avram_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
  327. <td valign="middle" align="left">[<a href="Function-Index.html#Function-Index" title="Index">Index</a>]</td>
  328. <td valign="middle" align="left">[<a href="avram_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
  329. </tr></table>
  330. <p>
  331. <font size="-1">
  332. This document was generated on <i>November 8, 2012</i> using <a href="http://www.nongnu.org/texi2html/"><i>texi2html 1.82</i></a>.
  333. </font>
  334. <br>
  335. </p>
  336. </body>
  337. </html>