map.tex 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. \section{MAP Operator}
  2. The operator MAP applies a uniform evaluation pattern
  3. to all members of a composite structure: a matrix, a list or the arguments
  4. of an operator expression. The evaluation pattern can be a
  5. unary procedure, an operator, or an algebraic expression with
  6. one free variable. MAP is used with the syntax:
  7. \begin{verbatim}
  8. MAP(EXPRN1:algebraic,EXPRN2:expression)
  9. \end{verbatim}
  10. {\tt EXPRN2} is a list, a matrix or an operator expression.
  11. {\tt EXPRN1} is
  12. \begin{itemize}
  13. \item the name of an operator for a single argument: the operator
  14. is evaluated once with each element of {\tt EXPRN1} as its single argument,
  15. \item an algebraic expression with exactly one free variable, that is
  16. a variable preceded by the tilde symbol: the expression
  17. is evaluated for each element of {\tt EXPRN1} where the element is
  18. substituted for the free variable,
  19. \item a replacement rule of the form
  20. \begin{verbatim}
  21. VAR => EXPRN3
  22. \end{verbatim}
  23. where {\tt VAR} is a variable and {\tt EXPRN3} is an expression
  24. which contains {\tt VAR}.
  25. Here {\tt EXPRN3} is evaluated for each element of {\tt EXPRN1} where
  26. the element is substituted for {\tt VAR}. {\tt VAR} may be
  27. optionally preceded by a tilde.
  28. \end{itemize}
  29. The rule form for {\tt EXPRN2} is needed when more than
  30. one free variable occurs.
  31. {\it Examples:}
  32. \begin{verbatim}
  33. % collect absolute values
  34. map(abs,{1,-2,a,-a});
  35. ->
  36. {1,2,abs(a),abs(a)}
  37. % integrate a matrix
  38. map(int(~w,x), mat((x^2,x^5),(x^4,x^5)));
  39. ->
  40. [ 3 6 ]
  41. [ x x ]
  42. [---- ----]
  43. [ 3 6 ]
  44. [ ]
  45. [ 5 6 ]
  46. [ x x ]
  47. [---- ----]
  48. [ 5 6 ]
  49. % multiply an equation
  50. map(~w*6 , x^2/3 = y^3/2 -1);
  51. ->
  52. 2 3
  53. 2*x =3*(y - 2)
  54. \end{verbatim}
  55. {\tt MAP} can be applied in nested mode:
  56. \begin{verbatim}
  57. map(sub(x=y,~q)-sub(x=0,~q),map(mat(int(~w,x),(x^2-y,x^3))));
  58. ->
  59. [ 2 4 ]
  60. [ y *(y - 3) y ]
  61. [------------ ----]
  62. [ 3 4 ]
  63. % The following example needs the rule form because there are two
  64. % free variables in the rightmost expression.
  65. map(~w=>map(int(~r,x),w),{mat((x^2,x^4)),mat((x^3,x^5))});
  66. ->
  67. {
  68. [ 3 5 ]
  69. [ x x ]
  70. [---- ----]
  71. [ 3 5 ]
  72. ,
  73. [ 4 6 ]
  74. [ x x ]
  75. [---- ----]
  76. [ 4 6 ]
  77. }
  78. \end{verbatim}
  79. \section{SELECT Operator}
  80. The operator SELECT extracts from a list
  81. or from the arguments of an n--ary operator elements corresponding
  82. to a boolean predicate. The predicate pattern can be a
  83. unary procedure, an operator or an algebraic expression with
  84. one free variable. SELECT is used with the syntax:
  85. \begin{verbatim}
  86. SELECT(EXPRN1:expression,EXPRN2:list)
  87. \end{verbatim}
  88. {\tt EXPRN1} is
  89. \begin{itemize}
  90. \item the name of an operator for a single argument: the operator
  91. is evaluated once with each element of {\tt EXPRN2} as its single argument,
  92. \item an algebraic expression with exactly one free variable, that is
  93. a variable preceded by the tilde symbol at least once: the expression
  94. is evaluated for each element of {\tt EXPRN2} where the element is
  95. substituted for the free variable,
  96. \item a replacement rule of the form
  97. \begin{verbatim}
  98. VAR => EXPRN3
  99. \end{verbatim}
  100. where {\tt VAR} is a variable and {\tt EXPRN3} is an expression
  101. which contains {\tt VAR}.
  102. Here {\tt EXPRN3} is evaluated for each element of {\tt EXPRN2} where
  103. the element is substituted for {\tt VAR}. {\tt VAR} may be
  104. optionally preceded by a tilde.
  105. \end{itemize}
  106. The rule form for {\tt EXPRN1} is needed when more than
  107. one free variable occurs. The result of the operation is built
  108. from those elements of {\tt EXPRN2} which let {\tt EXPRN1} evaluate
  109. to a value different from {\tt 0} or {\tt nil}, using the
  110. leading operator of {\tt EXPRN2}.
  111. Examples:
  112. \begin{verbatim}
  113. select( ~w>0, {1,-1,2,-3,3})
  114. ->
  115. {1,2,3}
  116. % select the tersm with even powers of y
  117. select(evenp deg(~w,y), part((x+y)^5,0):=list);
  118. ->
  119. 5 3 2 4
  120. {x ,10*x *y ,5*x*y }
  121. % select elements of a sum directly
  122. select(evenp deg(~w,x), 2x^2+3x^3+4x^4);
  123. ->
  124. 2 4
  125. 2x +4x
  126. \end{verbatim}