home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / utility / sm22a.zip / SYMBMATH.H11 < prev    next >
Text File  |  1993-04-21  |  4KB  |  169 lines

  1.         7.9 Lists, Arrays, Vectors and Matrices
  2.  
  3.     SymbMath can construct lists of arbitrary length, and the 
  4. entries in the lists can be of any type of value whatsoever: 
  5. constants, expressions with undefined variables, or even other lists.  
  6. Lists are another kind of value in SymbMath, and they can be assigned 
  7. to variables just like simple values.  (Since variables in SymbMath 
  8. language are untyped, you can assign any value to any variable.).
  9.  
  10.  
  11.           7.9.1     Entering Lists
  12.  
  13.     To define a list, put its elements between square brackets:
  14.  
  15.          a = [1,2,3]
  16.          b = [f(2), g(1), h(1)]      # assumes f,g,h defined
  17.          c = [[1,2],3,[4,5]]
  18.  
  19.     A function can have a list for its value:
  20.          f(x) = [1,x,x^2]
  21.     You can define lists another way, with the list command:
  22.  
  23.          list(f(x), x from xmin to xmax step dx)
  24.  
  25.     This is similar to the sum command,  but the result is a list:
  26.            [f(xmin), f(xmin+dx), ..., f(xmin+x*dx), ...]
  27. which continues until the last value of xmin + x*dx  <= xmax.  
  28.     Try
  29.  
  30.          a = list(j^2, j from 0 to 10 step 1)
  31.          f(x) = list(x^j, j from 0 to 6 step 1)
  32.          b = f(-2)
  33.  
  34.       The third way to construct a list is to transform the sum to
  35. the list.
  36.  
  37.     Input:
  38. y1=[a,b,c]
  39. sum(y1)+d
  40. list(last)
  41. end
  42.     Output:
  43. y1 = [a, b, c]
  44. a + b + c + d
  45. [a, b, c, d]
  46.  
  47.       This is how you extend an existing list to include a new 
  48. element.
  49.  
  50.  
  51.           7.9.2     Accessing Lists
  52.  
  53.     To find the value of the j-th element in a list x, use the
  54. formula  x[j]. The first element of x is always  x[1].  If the x[j] 
  55. is itself a list, then its k-th element is accessed by repeating the 
  56. similar step. Try:
  57.  
  58.     Input:
  59. x = [[1,2],3,[4,5]]
  60. x[1]
  61. x[2]
  62. end
  63.     Output:
  64. x = [[1, 2], 3, [4, 5]]
  65. [1, 2]  
  66. 3
  67.  
  68.     An entire sub-list of a list x  can be accessed with the 
  69. command x[j], which is the list:
  70.           [x[j], x[j+1], ... ]
  71.  
  72.  
  73.           7.9.3     Modifying Lists
  74.  
  75.     The function subs() substitutes the value of the element in
  76. the list, as in the variables. e.g.
  77.  
  78.     Input:
  79. l=[a,b,c]
  80. subs(l, a=a0)
  81. end
  82.     Output:
  83. l = [a, b, c]
  84. [a0, b, c]
  85.  
  86.     But you can't use this form unless the element of the list is
  87. already defined.
  88.  
  89.  
  90.           7.9.4     List Operations
  91.  
  92.     Lists can be added, subtracted, multiplied, and divided by 
  93. other lists or by constants.  When two lists are combined, they are
  94. combined term-by-term, and the combination stops when the shortest 
  95. list is exhausted.  When a scalar is combined with a list, it is 
  96. combined with each element of the list.  Try:
  97.  
  98.      a = [1,2,3]
  99.      b = [4,5,6]
  100.      a + b
  101.      a / b
  102.      3 * a
  103.      b - 4
  104.  
  105.     Example 7.9.1. Two lists are added.
  106.     Input:
  107. list1=[a1,a2,a3]
  108. list2=[b1,b2,b3]
  109. list1+list2
  110. last[1]
  111. end
  112.     Output:
  113. list1 = [a1,a2,a3]
  114. list2 = [b1,b2,b3]
  115. [a1 + b1, a2 + b2, a3 + b3]
  116. a1 + b1
  117.  
  118.     If L is a list, then  f(L) results in a list of the values,
  119. even though f() is the differentiation or integration function d() or 
  120. inte(). Try list with:
  121.     Input:
  122. sqrt([a, b, c])
  123. d([x, x^2, x^3], x)
  124. end
  125.  
  126.     You can find the number of elements in a list with:
  127.         length(a)
  128.     If you use a list as the value of a variable in a user-
  129. defined function, SymbMath will try to use the list in the 
  130. calculation.  
  131.     You can sum all the elements in a list x with the user-
  132. defined function:
  133.  
  134.                listsum(x)
  135. in the statistics package 'stat.sm'.
  136.     Example:
  137.  
  138.             x=[1,2,3]
  139.             listsum(x^2)
  140.  
  141.     This functions takes the sum of the squares of the elements of
  142. the list x.
  143.     See the statistics package 'stat.sm' for other statistics 
  144. operations (e.g. average, max, min).
  145.     See the list plot package 'listplot.sm' for plotting a list.
  146.     
  147.           7.9.5     Vector Operations
  148.  
  149.     SymbMath uses lists to represent vectors, and lists of lists to
  150. represent matrices.
  151.     Vectors and matrices can be operated by "+" and "-" with vectors
  152. and matrixes, by "*" and "/" with a scalar, and by subs(), diff() and 
  153. inte(). These operations are on each element, as in lists and arrays.
  154.     You can use lists as vectors, adding them and multiplying them 
  155. by scalars. For example, the dot product of two vectors of a and b is:
  156.  
  157.                dot = listsum(a*b)
  158.  
  159.     You can even make this into a function:
  160.  
  161.                Dot(x_, y_) = listsum(x*y)
  162.                a = [2,3,5]
  163.                b = [4,3,2]
  164.                Dot(a,b)
  165.  
  166.     How about the cross product:
  167.  
  168. Cross(a,b) = [a[2]*b[3]-b[2]*a[3],a[3]*b[1]-b[3]*a[1],a[1]*b[2]-b[1]*a[2]]
  169.