home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / maths / rlab / docs / RLAB-R~3 < prev    next >
Encoding:
Text File  |  1996-06-28  |  31.9 KB  |  1,005 lines

  1. <HTML>
  2. <HEAD>
  3. <TITLE> Rlab2 Reference Manual: Objects and Data</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <A HREF="rlab-ref-2.html"><IMG SRC="prev.gif" ALT="Previous"></A>
  7. <A HREF="rlab-ref-4.html"><IMG SRC="next.gif" ALT="Next"></A>
  8. <A HREF="rlab-ref.html#toc3"><IMG SRC="toc.gif" ALT="Contents"></A>
  9. <HR>
  10. <H2><A NAME="s3">3. Objects and Data</A></H2>
  11.  
  12.  
  13. <P>Rlab is, in a sense, an object oriented language. The term ``object
  14. oriented'' is used with some trepidation, since the term has itself
  15. been overloaded to the point of becoming unintelligible. Although
  16. Rlab does not support all of the concepts of the more classical
  17. object-oriented languages like Smalltalk it does offer some features
  18. of an object-oriented language. Operator overloading is one concept
  19. Rlab supports. For example, the behavior of the mathematical
  20. operators is dictated by the arguments, or objects.</P>
  21. <P>There are several predefined classes, they are:</P>
  22. <P>
  23. <DL>
  24. <DT><B>Numeric</B><DD><P>This class is the most widely used class in
  25. Rlab. The numeric class, abbreviated for use as <CODE>num</CODE>
  26. consists of two dimensional arrays or matrices. Subsets of this
  27. class include real and complex matrices, in both dense and
  28. sparse storage formats.</P>
  29.  
  30. <DT><B>String</B><DD><P>The string class consists of two dimensional
  31. arrays of variable length strings. Much of the syntax used for
  32. working with string arrays is directly derived from the numeric
  33. array class.</P>
  34.  
  35. <DT><B>List</B><DD><P>The list class provides a convenient, and user
  36. definable method for grouping related sets of data and
  37. functions. The list class is implemented as an N-dimensional
  38. associative array.</P>
  39.  
  40. <DT><B>Function</B><DD><P>The function class consists of both builtin and
  41. user-defined functions. If your computing platform supports
  42. runtime dynamic linking, then you can add builtin function via
  43. the <CODE>dlopen</CODE> interface.</P>
  44. </DL>
  45. </P>
  46. <P>We will stick with conventional object oriented terminology, and
  47. call an instantiation of a class an object. All objects have
  48. members. The members themselves are other objects. The syntax for
  49. accessing an object's members is the same syntax used for list
  50. members (see Section 
  51. <A HREF="#lists">lists</A>). To see what an objects
  52. members are named use the members function. Members returns a string
  53. matrix containing the names of an objects members, like so:</P>
  54. <P>
  55. <BLOCKQUOTE><CODE>
  56. <PRE>
  57. > a = rand(3,4);
  58. > members(a)
  59. nr       nc       n        class    type     storage  
  60. </PRE>
  61. </CODE></BLOCKQUOTE>
  62. </P>
  63. <P>The objects members can be referenced with either the formal string
  64. syntax like:</P>
  65. <P>
  66. <BLOCKQUOTE><CODE>
  67. <PRE>
  68. > a.["nr"]
  69.         3  
  70. </PRE>
  71. </CODE></BLOCKQUOTE>
  72. </P>
  73. <P>Or, the shorthand notation can be used:</P>
  74. <P>
  75. <BLOCKQUOTE><CODE>
  76. <PRE>
  77. > a.nr
  78.         3  
  79. </PRE>
  80. </CODE></BLOCKQUOTE>
  81. </P>
  82. <P>The formal notation is useful when you need variable evaluation:</P>
  83. <P>
  84. <BLOCKQUOTE><CODE>
  85. <PRE>
  86. > for (i in members(a)) { printf("%10s\t%s\n", i, a.[i]); }
  87.         nr      3
  88.         nc      4
  89.          n      12
  90.      class      num
  91.       type      real
  92.    storage      dense
  93. </PRE>
  94. </CODE></BLOCKQUOTE>
  95. </P>
  96. <P>An object's predefined members can be considered "read-only". That
  97. is, the user cannot change these member's values without changing
  98. the object itself. For example: the <CODE>nr</CODE> member of a matrix
  99. denotes the number of rows in the matrix. This member cannot be
  100. directly modified by the user. The only way to change the number of
  101. rows member is to actually add, or delete rows from the object.</P>
  102. <P>Additional members can be added to any object. Additional object
  103. members are arbitrary, and can be modified after creation. For
  104. example:</P>
  105. <P>
  106. <BLOCKQUOTE><CODE>
  107. <PRE>
  108. > a = rand(3,4);
  109. > a.rid = [1;2;3];
  110. > a.cid = 1:3;
  111. > a
  112.      0.91      0.265     0.0918      0.915  
  113.     0.112        0.7      0.902      0.441  
  114.     0.299       0.95       0.96     0.0735  
  115. > a.rid
  116.         1  
  117.         2  
  118.         3  
  119. > a.cid
  120.         1          2          3  
  121. > a.rid = ["row1", "row2", "row3"]
  122.      0.91      0.265     0.0918      0.915  
  123.     0.112        0.7      0.902      0.441  
  124.     0.299       0.95       0.96     0.0735  
  125. > a.rid
  126. row1  row2  row3  
  127. </PRE>
  128. </CODE></BLOCKQUOTE>
  129. </P>
  130. <P><CODE>show</CODE> and <CODE>whos</CODE> are useful functions for displaying
  131. object information. <CODE>show</CODE> displays an objects members, and
  132. their values, iff the values are scalar. Otherwise <CODE>show</CODE>
  133. displays the member's own attributes. For example:</P>
  134. <P>
  135. <BLOCKQUOTE><CODE>
  136. <PRE>
  137. > a = rand(3,4);
  138. > a.row_id = (1:3)';
  139. > a.col_id = 1:4;
  140. > show(a);
  141.         nr                  :   3
  142.         nc                  :   4
  143.         n                   :   12
  144.         class               :   num
  145.         type                :   real
  146.         storage             :   dense
  147.         col_id              :   num, real, dense, 1x4
  148.         row_id              :   num, real, dense, 3x1
  149. </PRE>
  150. </CODE></BLOCKQUOTE>
  151. </P>
  152. <P><CODE>whos</CODE> will display the object information for each member
  153. (with the exception of members that are functions) in addition to
  154. more detailed information about the size in bytes of each object.</P>
  155. <P>
  156. <BLOCKQUOTE><CODE>
  157. <PRE>
  158. > whos(a)
  159.         Name            Class   Type    Size            NBytes
  160.         nr              num     real    1       1       8
  161.         nc              num     real    1       1       8
  162.         n               num     real    1       1       8
  163.         class           string  string  1       1       7
  164.         type            string  string  1       1       8
  165.         storage         string  string  1       1       9
  166.         col_id          num     real    1       4       32
  167.         row_id          num     real    3       1       24
  168. Total MBytes = 0.000104
  169. </PRE>
  170. </CODE></BLOCKQUOTE>
  171. </P>
  172. <P>Both <CODE>show</CODE> and <CODE>whos</CODE> were originally designed to operate
  173. on the global workspace. The fact that they work equally well on
  174. individual objects provides a clue to the design of Rlab. The global
  175. symbol table, or global workspace can be considered an object of the
  176. list class. There is a special symbol for referring to the global
  177. workspace, <CODE>$$</CODE>. Accessing members of the global
  178. workspace can be performed with the same notation as previously
  179. described for an object's members. For example:</P>
  180. <P>
  181. <BLOCKQUOTE><CODE>
  182. <PRE>
  183. > $$.a
  184.         1      0.333      0.665      0.167  
  185.     0.975     0.0369     0.0847      0.655  
  186.     0.647      0.162      0.204      0.129  
  187. > $$.cos($$.a)
  188.      0.54      0.945      0.787      0.986  
  189.     0.562      0.999      0.996      0.793  
  190.     0.798      0.987      0.979      0.992  
  191. </PRE>
  192. </CODE></BLOCKQUOTE>
  193. </P>
  194. <P>In this example, the object <CODE>a</CODE> is referenced through the
  195. global workspace object <CODE>$$</CODE> rather than using
  196. the more conventional shorthand <CODE>a</CODE>. Then, the cosine function
  197. is invoked, again through the global workspace symbol. There are
  198. special provisions in place to ensure that users don't delete
  199. <CODE>$$</CODE>. The benefits of these capabilities may not
  200. be apparent until there is a need to construct and work with
  201. variables in an automated fashion.</P>
  202.  
  203.  
  204. <H2><A NAME="ss3.1">3.1 Numeric</A></H2>
  205.  
  206.  
  207. <P>The simplest numeric objects are scalars, or matrices with row and
  208. column dimensions of 1. Real values can be specified in integer or
  209. floating point format. For example:</P>
  210. <P>
  211. <BLOCKQUOTE><CODE>
  212. <PRE>
  213. > 3
  214.         3
  215. > 3.14
  216.      3.14
  217. > 3.14e2
  218.       314
  219. > 3.14e-2
  220.    0.0314
  221. > 3.14E-02
  222.    0.0314
  223. </PRE>
  224. </CODE></BLOCKQUOTE>
  225. </P>
  226. <P>Are all valid ways to express real numeric values.  Complex values
  227. are specified with the help of a complex constant. The complex
  228. constant is any real value <EM>immediately</EM> followed by <CODE>i</CODE>
  229. or <CODE>j</CODE>. Complex numbers, with real and imaginary parts can be
  230. constructed with the arithmetic operators, for example:</P>
  231. <P>
  232. <BLOCKQUOTE><CODE>
  233. <PRE>
  234. > z = 3.2 + 2j
  235.                 3.2 + 2i
  236. </PRE>
  237. </CODE></BLOCKQUOTE>
  238. </P>
  239.  
  240.  
  241. <H3>Numeric Object Elements</H3>
  242.  
  243.  
  244. <P>The numeric class supports two types of data, and two types of
  245. storage format.</P>
  246. <P>Each object has, as a minimum the following members:</P>
  247. <P>
  248. <DL>
  249. <DT><B><CODE>nr</CODE></B><DD><P>The matrix number of rows.</P>
  250. <DT><B><CODE>nc</CODE></B><DD><P>The matrix number of columns.</P>
  251. <DT><B><CODE>n</CODE></B><DD><P>The matrix number of elements.</P>
  252. <DT><B><CODE>class</CODE></B><DD><P>A string, with value <CODE>"num"</CODE>, for
  253. numeric. </P>
  254. <DT><B><CODE>type</CODE></B><DD><P>A string, with value <CODE>"real"</CODE> or
  255. <CODE>"complex"</CODE>.</P>
  256. <DT><B><CODE>storage</CODE></B><DD><P>A string, with value <CODE>"dense"</CODE> or
  257. <CODE>"sparse"</CODE>.</P>
  258. </DL>
  259. </P>
  260.  
  261.  
  262.  
  263. <H3>Numeric Object Operations</H3>
  264.  
  265.  
  266. <P>This section will cover the basic numeric operations. Starting with
  267. the operations necessary for creating and manipulating numeric
  268. matrices. The aritmetic operations are covered in Section 
  269. <A HREF="#arithmetic-ops">Arithmetic Operations</A></P>
  270.  
  271. <H3>Matrix Creation</H3>
  272.  
  273.  
  274. <P>The syntax for operating with numeric arrays/matrices is fairly
  275. simple. Square braces, <CODE>[]</CODE> are used for both creating
  276. matrices, assignment to matrix elements, and partitioning. The
  277. operation of creating a matrix consists of either appending elements
  278. to form rows, or stacking elements to form columns. Both operations
  279. must be performed within brackets. The append operation is performed
  280. with commas:</P>
  281. <P>
  282. <BLOCKQUOTE><CODE>
  283. <PRE>
  284. > a = [ 1 , 2 ];
  285. > a = [ a , a ]
  286.         1          2          1          2  
  287. </PRE>
  288. </CODE></BLOCKQUOTE>
  289. </P>
  290. <P>As you can see, either scalar elements, or matrices can be used with
  291. the append operator, as long as the row dimensions are the same. The
  292. stack operation is similar to the append operation, except
  293. semicolons are used as delimiters, and the column dimensions must
  294. match. </P>
  295. <P>
  296. <BLOCKQUOTE><CODE>
  297. <PRE>
  298. > b = [ 1 ; 2 ];
  299. > b = [ b ; b]
  300.         1  
  301.         2  
  302.         1  
  303.         2  
  304. </PRE>
  305. </CODE></BLOCKQUOTE>
  306. </P>
  307. <P>Any combination of append and stack operations can be performed
  308. together as long as the dimensions of the operands match.</P>
  309. <P>
  310. <BLOCKQUOTE><CODE>
  311. <PRE>
  312. > a = [1, 2];
  313. > b = [3; 4];
  314. > c = [ [a; a], [b, b] ]
  315.         1          2          3          3  
  316.         1          2          4          4  
  317. </PRE>
  318. </CODE></BLOCKQUOTE>
  319. </P>
  320.  
  321. <H3>Assignment</H3>
  322.  
  323.  
  324. <P>Assignment to matrix elements is also simple. Square brackets,
  325. <CODE>[]</CODE> are used to identify the matrix elements to be
  326. re-assigned. Row and column identifiers are separated with a
  327. semicolon. Multiple row or column specifiers can separated with
  328. commas. To assign to a single element:</P>
  329. <P>
  330. <BLOCKQUOTE><CODE>
  331. <PRE>
  332. > a = [1, 2, 3; 4, 5, 6; 7, 8, 9];
  333. > a[1;1] = 10
  334.        10          2          3  
  335.         4          5          6  
  336.         7          8          9  
  337. </PRE>
  338. </CODE></BLOCKQUOTE>
  339. </P>
  340. <P>To assign to multiple elements, specifically multiple rows:</P>
  341. <P>
  342. <BLOCKQUOTE><CODE>
  343. <PRE>
  344. > a[1,3;2] = [11;12]
  345.        10         11          3  
  346.         4          5          6  
  347.         7         12          9  
  348. </PRE>
  349. </CODE></BLOCKQUOTE>
  350. </P>
  351. <P>The dimensions of both the right hand side (RHS) and left hand side
  352. (LHS) must match. Assignment can be made to blocks of elements, in
  353. any specified order:</P>
  354. <P>
  355. <BLOCKQUOTE><CODE>
  356. <PRE>
  357. > a[3,1;3,1] = [30,30;30,30]
  358.        30         11         30  
  359.         4          5          6  
  360.        30         12         30  
  361. </PRE>
  362. </CODE></BLOCKQUOTE>
  363. </P>
  364. <P>To eliminate the tedium of specifying all the rows or all the
  365. columns, simply leave out the appropriate row or column specifier: </P>
  366. <P>
  367. <BLOCKQUOTE><CODE>
  368. <PRE>
  369. > a[;2] = [100;200;300]
  370.        30        100         30  
  371.         4        200          6  
  372.        30        300         30  
  373. </PRE>
  374. </CODE></BLOCKQUOTE>
  375. </P>
  376. <P>Entire rows and columns can be eliminated via the assignment of the
  377. null matrix to those row and columns to be removed. The allowed
  378. syntax for this operation is:</P>
  379. <P>
  380. <BLOCKQUOTE>
  381. <EM>VAR</EM> [ <EM>ROW-ID</EM> ; ] = []
  382. </BLOCKQUOTE>
  383. </P>
  384. <P>
  385. <BLOCKQUOTE>
  386. <EM>VAR</EM> [ ; <EM>COL-ID</EM> ] = []
  387. </BLOCKQUOTE>
  388. </P>
  389. <P>A simple example:</P>
  390. <P>
  391. <BLOCKQUOTE><CODE>
  392. <PRE>
  393. > a = magic(5)
  394.        17         24          1          8         15  
  395.        23          5          7         14         16  
  396.         4          6         13         20         22  
  397.        10         12         19         21          3  
  398.        11         18         25          2          9  
  399. > a[3,2;] = []
  400.        17         24          1          8         15  
  401.        10         12         19         21          3  
  402.        11         18         25          2          9  
  403. > a[;2,4] = []
  404.        17          1         15  
  405.        10         19          3  
  406.        11         25          9  
  407. </PRE>
  408. </CODE></BLOCKQUOTE>
  409. </P>
  410.  
  411.  
  412.  
  413. <H3>Matrix Partitioning</H3>
  414.  
  415.  
  416. <P>Matrix partitioning, the operation of extracting a sub-matrix from
  417. an existing matrix, uses the same syntax, and concepts of matrix
  418. element assignment. To partition a 2-by-2 sub-matrix from the
  419. original <CODE>a</CODE>:</P>
  420. <P>
  421. <BLOCKQUOTE><CODE>
  422. <PRE>
  423. > a = [1, 2, 3; 4, 5, 6; 7, 8, 9];
  424. > a[2,3;2,3]
  425.         5          6  
  426.         8          9  
  427. </PRE>
  428. </CODE></BLOCKQUOTE>
  429. </P>
  430.  
  431.  
  432.  
  433. <H3><A NAME="arithmetic-ops"></A> Arithmetic Operations </H3>
  434.  
  435.  
  436. <P>For clarification: each operand (either <CODE>A</CODE> or <CODE>B</CODE>) is a
  437. matrix, with row dimension M, and column dimension N.</P>
  438. <P>
  439. <DL>
  440.  
  441. <DT><B><CODE>A + B</CODE></B><DD><P>Does element-by-element addition of two
  442. matrices.  The row and column dimensions of both <CODE>A</CODE> and
  443. <CODE>B</CODE> must be the same. An exception to the aforementioned
  444. rule occurs when either <CODE>A</CODE> or <CODE>B</CODE> is a 1-by-1 matrix;
  445. in this case a scalar-matrix addition operation is performed.</P>
  446.  
  447. <DT><B><CODE>A - B</CODE></B><DD><P>Does element-by-element subtraction of two
  448. matrices.  The row and column dimensions of both <CODE>A</CODE> and
  449. <CODE>B</CODE> must be the same. An exception to the aforementioned
  450. rule occurs when either <CODE>A</CODE> or <CODE>B</CODE> is a 1-by-1 matrix;
  451. in this case a scalar-matrix addition operation is performed.</P>
  452.  
  453. <DT><B><CODE>A * B</CODE></B><DD><P>Performs matrix multiplication on the two
  454. operands.  The column dimension of <CODE>A</CODE> must match the row
  455. dimension of <CODE>B</CODE>. An exception to the aforementioned rule
  456. occurs when either <CODE>A</CODE> or <CODE>B</CODE> is a 1-by-1 matrix; in
  457. this case a scalar-matrix multiplication is performed.</P>
  458.  
  459. <DT><B><CODE>A .* B</CODE></B><DD><P>Performs element-by-element matrix
  460. multiplication on the two operands. Both row and column
  461. dimensions must agree, unless:</P>
  462. <P>
  463. <UL>
  464. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a 1x1. In this case the operation
  465. is performed element-by-element over the entire matrix. The
  466. result is a MxN matrix.
  467. </LI>
  468. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a 1xN. and the other is MxN. In
  469. this instance the operation is performed element-by-element
  470. fashion for each row in the matrix. The result is a MxN matrix.
  471. </LI>
  472. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a Nx1. and the other is NxM. In
  473. this instance the operation is performed element-by-element
  474. fashion for each column in the matrix. The result is a NxM
  475. matrix.</LI>
  476. </UL>
  477. </P>
  478.  
  479. <DT><B><CODE>A / B</CODE></B><DD><P>Performs matrix right-division on its
  480. operands.  The matrix right-division <CODE>B/A</CODE> can be thought of
  481. as <CODE>B*inv (A)</CODE>. The column dimensions of <CODE>A</CODE> and
  482. <CODE>B</CODE> must be the same. Internally right division is the same
  483. as left-division with the arguments transposed.</P>
  484. <P>
  485. <BLOCKQUOTE><CODE>
  486. <PRE>
  487. B / A = ( A' \ B')'
  488. </PRE>
  489. </CODE></BLOCKQUOTE>
  490. </P>
  491. <P>The exception to the aforementioned dimension rule occurs when
  492. <CODE>A</CODE> is a 1-by-1 matrix; in this case a matrix-scalar divide
  493. occurs.</P>
  494.  
  495. <DT><B><CODE>A ./ B</CODE></B><DD><P>Performs element-by-element right-division
  496. on its operands. The dimensions of <CODE>A</CODE> and <CODE>B</CODE> must
  497. agree, unless:</P>
  498. <P>
  499. <UL>
  500. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a 1x1. In this case the operation
  501. is performed element-by-element over the entire matrix. The
  502. result is a MxN matrix.
  503. </LI>
  504. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a 1xN. and the other is MxN. In
  505. this instance the operation is performed element-by-element
  506. fashion for each row in the matrix. The result is a MxN matrix.
  507. </LI>
  508. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a Nx1. and the other is NxM. In
  509. this instance the operation is performed element-by-element
  510. fashion for each column in the matrix. The result is a NxM
  511. matrix.</LI>
  512. </UL>
  513. </P>
  514.  
  515. <DT><B><CODE>A \ B</CODE></B><DD><P>Performs matrix left-division. Given
  516. operands <CODE>A\B</CODE> matrix left division is the solution to
  517. the set of equations <CODE>Ax = B</CODE>. If <CODE>B</CODE> has several
  518. columns, then each column of <CODE>x</CODE> is a solution to
  519. <CODE>A*x[;i] = B[;i]</CODE>. The row dimensions of <CODE>A</CODE> and
  520. <CODE>B</CODE> must agree.</P>
  521.  
  522. <DT><B><CODE>A .\ B</CODE></B><DD><P>Performs element-by-element
  523. left-division.  Element-by-element left-division is provided for
  524. symmetry, and is equivalent to <CODE>B .\ A</CODE>. The row and
  525. column dimensions of <CODE>A</CODE> and <CODE>B</CODE> must agree, unless:</P>
  526. <P>
  527. <UL>
  528. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a 1x1. In this case the operation
  529. is performed element-by-element over the entire matrix. The
  530. result is a MxN matrix.
  531. </LI>
  532. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a 1xN. and the other is MxN. In
  533. this instance the operation is performed element-by-element
  534. fashion for each row in the matrix. The result is a MxN matrix.
  535. </LI>
  536. <LI> <CODE>A</CODE> or <CODE>B</CODE> is a Nx1. and the other is NxM. In
  537. this instance the operation is performed element-by-element
  538. fashion for each column in the matrix. The result is a NxM
  539. matrix.</LI>
  540. </UL>
  541. </P>
  542. </DL>
  543. </P>
  544.  
  545.  
  546.  
  547. <H3><A NAME="relational-ops"></A> Relational Operations </H3>
  548.  
  549.  
  550.  
  551.  
  552.  
  553. <H3><A NAME="logical-ops"></A> Logical Operations </H3>
  554.  
  555.  
  556.  
  557.  
  558.  
  559. <H3><A NAME="vectors"></A> Vectors </H3>
  560.  
  561.  
  562. <P>Although there is no separate vector class, the concept of row and
  563. column vectors is often used. Row and column vectors are matrices
  564. with a column or row dimension equal to one, respectively. Rlab
  565. offers convenient notation for creating, assignment to, and
  566. partitioning matrices as if they were vectors.</P>
  567. <P>There is a special notation for creating ordered row vectors.</P>
  568. <P>
  569. <BLOCKQUOTE>
  570. start-value : end-value : increment-value
  571. </BLOCKQUOTE>
  572. </P>
  573. <P>The start, end and increment values can be any floating point or
  574. integer value. If the start-value is less than the end-value, then a
  575. null-vector will be returned, unless the increment-value is
  576. negative. This vector notation is most often used within for-loops. </P>
  577. <P>
  578. <BLOCKQUOTE><CODE>
  579. <PRE>
  580. > n = 4;
  581. > 1:n
  582.         1          2          3          4  
  583. > n:1:-1
  584.         4          3          2          1  
  585. > 1:n/2:0.5
  586.         1        1.5          2  
  587. </PRE>
  588. </CODE></BLOCKQUOTE>
  589. </P>
  590. <P>Unexpected results can occur when a non-integer increment is
  591. used. Since not all real numbers can be expressed precisely in
  592. floating point format, incrementing the start-value by the
  593. increment-value may not produce the expected result. An increment
  594. value of 0.1 provides a nice example:</P>
  595. <P>
  596. <BLOCKQUOTE><CODE>
  597. <PRE>
  598. > 1:2:.1
  599.  matrix columns 1 thru 6
  600.         1        1.1        1.2        1.3        1.4        1.5  
  601.  
  602.  matrix columns 7 thru 10
  603.       1.6        1.7        1.8        1.9  
  604. </PRE>
  605. </CODE></BLOCKQUOTE>
  606. </P>
  607. <P>Most would expect the final value to be 2. But, since 0.1 cannot be
  608. expressed exactly in floating point format, the final value of 2 is
  609. not reached. The reason is more obvious if we reset the print
  610. format:</P>
  611. <P>
  612. <BLOCKQUOTE><CODE>
  613. <PRE>
  614. > format(18);
  615. > 1:2:.1
  616.  matrix columns 1 thru 3
  617.                     1    1.10000000000000009    1.19999999999999996  
  618.  
  619.  matrix columns 4 thru 6
  620.   1.30000000000000004    1.39999999999999991                    1.5  
  621.  
  622.  matrix columns 7 thru 9
  623.   1.60000000000000009    1.69999999999999996    1.80000000000000004  
  624.  
  625.  matrix columns 10 thru 10
  626.   1.90000000000000013  
  627. </PRE>
  628. </CODE></BLOCKQUOTE>
  629. </P>
  630. <P>When it is important to have the precise start and end values, the
  631. user-function <CODE>linspace</CODE> should be used.</P>
  632.  
  633. <P>Fairly frequently it is desirable to force a matrix into a column
  634. vector. This is fairly natural since matrices are stored in
  635. column-major order, and it makes operating on the data notationally
  636. simpler. The syntax for this operation is:</P>
  637. <P>
  638. <BLOCKQUOTE>
  639. <EM>matrix</EM> [ : ]
  640. </BLOCKQUOTE>
  641. </P>
  642. <P>For example:</P>
  643. <P>
  644. <BLOCKQUOTE><CODE>
  645. <PRE>
  646. > a = [1,2,3,4];
  647. > a = a[:]
  648.         1  
  649.         2  
  650.         3  
  651.         4  
  652. </PRE>
  653. </CODE></BLOCKQUOTE>
  654. </P>
  655.  
  656.  
  657.  
  658. <H3><A NAME="sparse"></A> Sparse Storage </H3>
  659.  
  660.  
  661. <P>Sparse matrices are matrices in which the zero elements are not
  662. explicitly stored. Quite a few applications, such as finite element
  663. modeling, lead to matrices which are sparsely populated with
  664. non-zero elements. A sparse storage scheme offers reduced memory
  665. usage, and more efficient matrix operations (in most cases) for
  666. operations on matrices with mostly zero elements. There are many
  667. different sparse storage schemes, each offers particular
  668. advantages. Rlab uses the compressed row-wise (CRW) sparse storage
  669. format. The CRW format is very general, and offers good performance
  670. for a wide variety of problems.</P>
  671. <P>Sparse matrices, and operations are not common for the majority of
  672. users. Therefore, some extra work is required for users who wish to
  673. use this storage scheme. The functions <CODE>sparse</CODE> and
  674. <CODE>spconvert</CODE> are useful for converting from dense/full storage
  675. to sparse storage, and vice-versa. Although the syntax for sparse
  676. storage matrices is the same as that used for dense matrices, sparse
  677. matrices are visibly different when printed to the display:</P>
  678. <P>
  679. <BLOCKQUOTE><CODE>
  680. <PRE>
  681. > a = speye(3,3)
  682.  (1, 1)                 1
  683.  (2, 2)                 1
  684.  (3, 3)                 1
  685. > full(a)
  686.         1          0          0  
  687.         0          1          0  
  688.         0          0          1  
  689. </PRE>
  690. </CODE></BLOCKQUOTE>
  691. </P>
  692. <P>Since only the non-zero elements are stored, only the non-zero
  693. elements, along with their row and column indices are printed.</P>
  694. <P>All matrix partitioning, assignment and arithmetic operations
  695. perform the same function for sparse matrices as for dense matrices
  696. (eventually). The only difference is the storage format of the
  697. result. In some instances an operation on a sparse matrix will
  698. produce a dense (or full) matrix because there is no benefit to
  699. retaining sparse storage. For instance, using the <CODE>cos</CODE>
  700. function on a sparse matrix will return a full matrix, since the
  701. cosine of zero is one, there is no point in retaining the sparse
  702. storage format for the result. On the other hand Rlab will never
  703. change the storage format of a matrix, once it has been
  704. created. Even if you deliberately add zeros to a sparse matrix, or
  705. increase the number of non-zeros to the point where the matrix is
  706. full, the storage format will remain sparse. </P>
  707. <P>While sparse storage formats facilitate the solution of problems
  708. that dense storage cannot manage, there are some things that sparse
  709. storage cannot do efficiently. Sparse storage is inefficient for
  710. matrix manipulations. Assigning to the elements of a sparse matrix
  711. can be very inefficient, especially if you are replacing zeros with
  712. non-zero values. Likewise matrix stacking and concatenation are not
  713. very efficient.</P>
  714.  
  715.  
  716.  
  717. <H3>Special Numeric Values (Inf and NaN)</H3>
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724. <H2><A NAME="ss3.2">3.2 String</A></H2>
  725.  
  726.  
  727. <P>Strings are arbitrary length concatenations of printable
  728. characters. </P>
  729.  
  730. <H3>String Object Elements</H3>
  731.  
  732.  
  733. <P>Each object has, as a minimum the following members:</P>
  734. <P>
  735. <DL>
  736. <DT><B><CODE>nr</CODE></B><DD><P>The matrix number of rows.</P>
  737. <DT><B><CODE>nc</CODE></B><DD><P>The matrix number of columns.</P>
  738. <DT><B><CODE>n</CODE></B><DD><P>The matrix number of elements.</P>
  739. <DT><B><CODE>class</CODE></B><DD><P>A string, with value <CODE>"string"</CODE>, for
  740. numeric. </P>
  741. <DT><B><CODE>type</CODE></B><DD><P>A string, with value <CODE>"string"</CODE>.</P>
  742. <DT><B><CODE>storage</CODE></B><DD><P>A string, with value <CODE>"dense"</CODE>.</P>
  743. </DL>
  744. </P>
  745.  
  746.  
  747. <H3>String Object Operations</H3>
  748.  
  749.  
  750. <P>The syntax for creating a string is similar to the C-language
  751. syntax:</P>
  752. <P>
  753. <BLOCKQUOTE>
  754. " arbitrary_printable_characters "
  755. </BLOCKQUOTE>
  756. </P>
  757. <P>So to create a string, and assign it to a variable you might do:</P>
  758. <P>
  759. <BLOCKQUOTE><CODE>
  760. <PRE>
  761. > str = "this is a sample string"
  762. this is a sample string  
  763. </PRE>
  764. </CODE></BLOCKQUOTE>
  765. </P>
  766. <P>String matrix operations are performed exactly the same way as
  767. numeric matrix operations. String matrix creation, element
  768. assignment, and partitioning are all performed as described for
  769. numeric matrices. For example:</P>
  770. <P>
  771. <BLOCKQUOTE><CODE>
  772. <PRE>
  773. > strm = [ "this", "is a"; "sample", "string matrix"]
  774. this           is a           
  775. sample         string matrix  
  776. > for (i in [1,3,2,4]) { strm[i] }
  777. this  
  778. is a  
  779. sample  
  780. string matrix  
  781. </PRE>
  782. </CODE></BLOCKQUOTE>
  783. </P>
  784. <P>There is no provision for individual character operations on
  785. strings, unless the string consists of a single character. However,
  786. the function <CODE>strsplt</CODE> will break a string into an array
  787. (row-matrix) of single character strings.</P>
  788. <P>
  789. <BLOCKQUOTE><CODE>
  790. <PRE>
  791. > strsplt (str)
  792. t  h  i  s     i  s     a     s  a  m  p  l  e     s  t  r  i  n  g  
  793. </PRE>
  794. </CODE></BLOCKQUOTE>
  795. </P>
  796. <P><CODE>strsplt</CODE> can also split strings into sub-strings of a
  797. specified length, using the second (optional) argument.:</P>
  798. <P>
  799. <BLOCKQUOTE><CODE>
  800. <PRE>
  801. > strsplt (str, 4)
  802. this   is   a sa  mple   str  
  803. > length(strsplt (str, 4))
  804.         4          4          4          4          4  
  805. </PRE>
  806. </CODE></BLOCKQUOTE>
  807. </P>
  808. <P>Furthermore, <CODE>strsplt</CODE> can split strings using a field
  809. separator defined in the second (optional) argument:</P>
  810. <P>
  811. <BLOCKQUOTE><CODE>
  812. <PRE>
  813. > strsplt (str, "i")
  814. th              s               s a sample str  ng              
  815. </PRE>
  816. </CODE></BLOCKQUOTE>
  817. </P>
  818. <P>Strings can be concatenated with the <CODE>+</CODE> operator:</P>
  819. <P>
  820. <BLOCKQUOTE><CODE>
  821. <PRE>
  822. > strm[1;1] + " " + strm[1;2] + " " + strm[2;1] + " " + strm[2;2]
  823. this is a sample string matrix  
  824. </PRE>
  825. </CODE></BLOCKQUOTE>
  826. </P>
  827. <P>The relational operators work for strings, comparing them using the
  828. characters ASCII decimal representation. Thus
  829. <CODE>"A"</CODE>, (ASCII 65) is less than
  830. <CODE>"a"</CODE> (ASCII 97). String comparisons are useful for
  831. testing the properties of objects. For instance, the function
  832. <CODE>class</CODE> returns a string identifying the class an object
  833. belongs to.</P>
  834. <P>
  835. <BLOCKQUOTE><CODE>
  836. <PRE>
  837. > class(l)
  838. list  
  839. > class(l) == "list"
  840.         1  
  841. </PRE>
  842. </CODE></BLOCKQUOTE>
  843. </P>
  844.  
  845.  
  846.  
  847.  
  848. <H2><A NAME="lists"></A> <A NAME="ss3.3">3.3 List </A></H2>
  849.  
  850.  
  851. <P>A list is a heterogeneous associative array. Simply, a list is an
  852. array whose elements can be from different classes. Thus a list can
  853. contain numeric, string, function, and other list objects. Lists are
  854. also a convenient vehicle for functions that must return multiple
  855. data objects. Additionally, lists offer programmer the ability to
  856. create arbitrary data structures to suit particular programming
  857. tasks.</P>
  858.  
  859. <H3>List Object Elements</H3>
  860.  
  861.  
  862. <P>Lists have no predefined elements, the quantity and class of a
  863. list's elements is entirely up to the user. A list's elements are
  864. displayed when an expression evaluates to a list. Entering the name
  865. of a list variable, without a trailing semi-colon, will print out
  866. the list's element names. The standard user-functions: <CODE>show</CODE>,
  867. <CODE>who</CODE>, and <CODE>whos</CODE> will also display information about a
  868. list's elements. The following example will create a list, then
  869. display information about the list's elements using the
  870. aforementioned methods.</P>
  871. <P>
  872. <BLOCKQUOTE><CODE>
  873. <PRE>
  874. > rfile magic
  875. > l = << m2 = magic(2); m3 = magic(3); m6 = magic(6) >>
  876.    m2           m3           m6           
  877. > who(l)
  878. m2  m3  m6          
  879. > l
  880.    m2           m3           m6           
  881. > who(l)
  882. m2  m3  m6          
  883. > show(l);
  884.         m2                  :num        real
  885.         m3                  :num        real
  886.         m6                  :num        real
  887. > whos(l);
  888.         Name            Class   Type    Size            NBytes
  889.         m2              num     real    2       2       32
  890.         m3              num     real    3       3       72
  891.         m6              num     real    6       6       288
  892. Total MBytes = 0.000392
  893. </PRE>
  894. </CODE></BLOCKQUOTE>
  895. </P>
  896.  
  897.  
  898. <H3>List Object Operations</H3>
  899.  
  900.  
  901. <P>To create a list-object use the <CODE><<</CODE> and <CODE>>></CODE>
  902. operators. The list will be created, and the objects inside the
  903. <CODE><< >></CODE> will be installed in the new list. If the
  904. objects are not renamed during the list-creation, they will be given
  905. numerical index values. An expression that evaluates to a list will
  906. print out the names of that list's elements. For example:</P>
  907. <P>
  908. <BLOCKQUOTE><CODE>
  909. <PRE>
  910. > a = rand(3,4); b = sqrt (a); c = 2*a + b;
  911. > ll = << a ; b ; c >>
  912.    1            2            3            
  913. > ll2 = << A = a; b = b ; x = c >>
  914.    A            b            x            
  915. > ll2.A == ll.[1]
  916.         1          1          1          1  
  917.         1          1          1          1  
  918.         1          1          1          1  
  919. </PRE>
  920. </CODE></BLOCKQUOTE>
  921. </P>
  922. <P>Lists are not indexed with numeric values. Lists are indexed with
  923. string values (in a fashion similar to AWK's associative
  924. arrays.}. There are two methods for referencing the elements of a
  925. list. The first, a shorthand notation looks like:</P>
  926. <P>
  927. <BLOCKQUOTE>
  928. <EM>list_name</EM> . <EM>element_name</EM>
  929. </BLOCKQUOTE>
  930. </P>
  931. <P>In this case, the <EM>list_name</EM> and <EM>element_name</EM> must
  932. follow the same rules as ordinary variable names. The second method
  933. for indexing a list is:</P>
  934. <P>
  935. <BLOCKQUOTE>
  936. <EM>list_name</EM> . [ <EM>numeric_or_string_expression</EM> ]
  937. </BLOCKQUOTE>
  938. </P>
  939. <P>The second method allows string and numeric variables to be
  940. evaluated before doing the conversion to string type.</P>
  941. <P>The dimensionality of a list is also arbitrary. To increase the
  942. dimension of a list make a member of the parent list a list. For
  943. example:</P>
  944. <P>
  945. <BLOCKQUOTE><CODE>
  946. <PRE>
  947. > person = << type="Human"; name=<<first="John"; last="Doe">>; age=37 >>
  948.    age          name         type         
  949. > person.name
  950.    first        last         
  951. > person.name.first
  952. John  
  953. > person.name.last
  954. Doe  
  955. </PRE>
  956. </CODE></BLOCKQUOTE>
  957. </P>
  958. <P>The <CODE>person</CODE> list contains the elements <CODE>type</CODE>,
  959. <CODE>name</CODE>, and <CODE>age</CODE>. However, the <CODE>name</CODE> element is
  960. another list that contains the elements <CODE>first</CODE> and
  961. <CODE>last</CODE>.</P>
  962.  
  963.  
  964.  
  965.  
  966. <H2><A NAME="ss3.4">3.4 Function</A></H2>
  967.  
  968.  
  969. <P>Functions, both builtin and user written are stored in ordinary
  970. variables, and in almost all instances are treated as such. An
  971. expression that evaluates to a function prints the string:
  972. <CODE><user-function></CODE> if it is a user-written function, and
  973. the string: <CODE><bltin-function></CODE> if it is a builtin
  974. function.</P>
  975.  
  976. <H3>Function Object Elements</H3>
  977.  
  978.  
  979. <P>Each object has, as a minimum the following members:</P>
  980. <P>
  981. <DL>
  982. <DT><B><CODE>class</CODE></B><DD><P>A string, with value <CODE>"function"</CODE>.</P>
  983. <DT><B><CODE>type</CODE></B><DD><P>A string, with value <CODE>"user"</CODE> or 
  984. <CODE>"builtin"</CODE>.</P>
  985. </DL>
  986. </P>
  987. <P>The function class has an optional member which exists only when the
  988. function is of type <CODE>user</CODE>. The additional member is named
  989. <CODE>file</CODE>, and its value is the full pathname of the file that
  990. contains the source code for the user function.</P>
  991. <P>Functions, both user and builtin are treated in great detail in
  992. subsequent sections of this manual.</P>
  993.  
  994.  
  995. <H3>Function Object Operations</H3>
  996.  
  997.  
  998.  
  999. <HR>
  1000. <A HREF="rlab-ref-2.html"><IMG SRC="prev.gif" ALT="Previous"></A>
  1001. <A HREF="rlab-ref-4.html"><IMG SRC="next.gif" ALT="Next"></A>
  1002. <A HREF="rlab-ref.html#toc3"><IMG SRC="toc.gif" ALT="Contents"></A>
  1003. </BODY>
  1004. </HTML>
  1005.