less7 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. COMMENT
  2. REDUCE INTERACTIVE LESSON NUMBER 7
  3. David R. Stoutemyer
  4. University of Hawaii
  5. COMMENT This is lesson 7 of 7 REDUCE lessons. It was suggested that
  6. you bring a REDUCE source listing, together with a cross-reference
  7. (CREF) thereof, but this lesson is beneficial even without them.
  8. Sometimes it is desired to have a certain facility available to
  9. algebraic mode, no such facility is described in the REDUCE User's
  10. manual, and there is no easy way to implement the facility directly
  11. in algebraic mode. The possibilities are:
  12. 1. The facility exists for algebraic mode, but is undocumented.
  13. 2. The facility exists, but is available only in symbolic mode.
  14. 3. The facility is not built-in for either mode.
  15. Perusal of the source listing and CREF, together with experimentation
  16. can reveal which of these alternatives is true. (Even in case 3, an
  17. inquiry to A.C. Hearn at the Rand Corporation may reveal that someone
  18. else has already implemented the supplementary facility and can send a
  19. copy.)
  20. ;PAUSE;COMMENT
  21. A type of statement is available to both modes if its leading keyword
  22. appears in either of the equivalent statements
  23. PUT (..., 'STAT, ...)
  24. or
  25. DEFLIST('(...), 'STAT) .
  26. A symbolic-mode global variable is available to algebraic mode and
  27. vice-versa if the name of the variable appears in either of the
  28. equivalent statements
  29. SHARE ...,
  30. or
  31. FLAG('(...), 'SHARE) .
  32. A function defined in symbolic mode is directly available to
  33. algebraic mode if the function name appears in one of the statements
  34. SYMBOLIC OPERATOR ...,
  35. PUT(..., 'SIMPFN, ...),
  36. DEFLIST('(...), 'SIMPFN),
  37. FLAG('(...), 'OPFN),
  38. In addition, if you want a function to be used as a predicate in
  39. in IF or WHILE statements, it should be flagged BOOLEAN, as in
  40. FLAG('(...),'BOOLEAN);
  41. ;PAUSE;COMMENT
  42. Other functions which are used but not defined in RLISP are the built-in
  43. LISP functions. See a description of the underlying LISP system for
  44. documentation on these functions.
  45. Particularly notable built-in features available only to symbolic
  46. mode include
  47. 1. A function named FIX, which returns the truncated integer
  48. portion of its floating-point argument.
  49. 2. A function named SPACES, which prints the number of blanks
  50. indicated by its integer argument.
  51. 3. A function named REDERR, which provokes an error interrupt
  52. after printing its arguments.
  53. 4. A predicate named KERNP, which returns NIL if its argument
  54. is not an indeterminate or a functional form.
  55. 5. A function named MATHPRINT, which prints its argument in
  56. natural mathematical notation, beginning on a new line.
  57. 6. A function named MAPRIN, which is like MATHPRINT, but does not
  58. automatically start or end a new line.
  59. 7. A function named TERPRI!*, which ends the current print-line.
  60. Thus, for example, all that we have to do to make the predicate
  61. KERNP and the function FIX available to algebraic mode is to type
  62. FLAG('(KERNP), 'BOOLEAN),
  63. SYMBOLIC OPERATOR FIX .
  64. When such simple remedies are unavailable, we can introduce our own
  65. statements or write our own SYMBOLIC-mode variables and procedures, then
  66. use these techniques to make them available to algebraic mode. In order
  67. to do so, it is usually necessary to understand how REDUCE represents
  68. and simplifies algebraic expressions.
  69. ;PAUSE;COMMENT
  70. One of the REDUCE representations is called Cambridge Prefix. An
  71. expression is either an atom or a list consisting of a literal atom,
  72. denoting a function or operator name, followed by arguments which are
  73. Cambridge Prefix expressions. The most common unary operator names are
  74. MINUS, LOG, SIN, and COS. The most common binary operator names are
  75. DIFFERENCE, QUOTIENT, and EXPT. The most common nary operator names are
  76. PLUS and TIMES. Thus, for example, the expression
  77. 3*x**2*y + x**(1/2) + e**(-x)
  78. could be represented as
  79. '(PLUS (TIMES 3 (EXPT X 2) Y) (EXPT X (QUOTIENT 1 2)) (EXPT E (MINUS X))
  80. The parser produces an unsimplified Cambridge Prefix version of
  81. algebraic-mode expressions typed by the user, then the simplifier
  82. returns a simplified prefix version. When a symbolic procedure that has
  83. been declared a symbolic operator is invoked from algebraic mode, the
  84. procedure is given simplified Cambridge Prefix versions of the
  85. arguments. To illustrate these ideas, here is an infix function named
  86. ISFREEOF, which determines whether its left argument is free of the
  87. indeterminate, function name, or literal subexpression which is the
  88. right argument. Isfreeof is similar to the REDUCE FREEOF function but
  89. less general;
  90. PAUSE;
  91. FLAG('(ISFREEOF), 'BOOLEAN);
  92. INFIX ISFREEOF;
  93. SYMBOLIC PROCEDURE CAMPRE1 ISFREEOF CAMPRE2;
  94. IF CAMPRE1=CAMPRE2 THEN NIL
  95. ELSE IF ATOM CAMPRE1 THEN T
  96. ELSE (CAR CAMPRE1 ISFREEOF CAMPRE2)
  97. AND (CDR CAMPRE1 ISFREEOF CAMPRE2);
  98. ALGEBRAIC IF LOG(5+X+COS(Y)) ISFREEOF SIN(Z-7)
  99. THEN WRITE "WORKS ONE WAY";
  100. ALGEBRAIC IF NOT(LOG(5+X+COS(Y)) ISFREEOF COS(Y))
  101. THEN WRITE "WORKS OTHER WAY TOO";
  102. COMMENT Conceivably we might wish to distinguish when CAMPRE2 is a
  103. literal atom occurring as a function name from the case when CAMPRE2 is
  104. a literal atom and occurs as an indeterminate. Accordingly, see if you
  105. can write two such more specialized infix predicates named ISFREEOFINDET
  106. and ISFREEOFFUNCTION;
  107. PAUSE;
  108. COMMENT When writing a symbolic-mode function, it is often desired
  109. to invoke the algebraic simplifier from within the function. This
  110. can be done by using the function named REVAL, which returns a
  111. simplified Cambridge Prefix version of its prefix argument.
  112. Usually, REDUCE uses and produces a different representation,
  113. which I call REDUCE prefix. The symbolic function AEVAL returns a
  114. simplified REDUCE-prefix version of its prefix argument. Both REVAL
  115. and AEVAL can take either type of prefix argument.
  116. A REDUCE-prefix expression is an integer, a floating-point number, an
  117. indeterminate, or an expression of the form
  118. ('!*SQ standardquotient . !*SQVAR!*).
  119. !*SQVAR!* is a global variable which is set to T when the REDUCE-
  120. prefix expression is originally formed. The values of !*SQVAR!* is
  121. reset to NIL if subsequent LET, MATCH, or computational ON
  122. statements could change the environment is such a way that the
  123. expression might require resimplification next time it is used.
  124. ;PAUSE;COMMENT
  125. Standard quotients are neither Cambridge nor REDUCE prefix, so the
  126. purpose of the atom '!*SQ is to make the value of all algebraic-mode
  127. variables always be some type of prefix form at the top level.
  128. A standard quotient is a unit-normal dotted pair of 2 standard forms,
  129. and a standard form is the REDUCE representation for a polynomial.
  130. Unit-normal means that the leading coefficient of the denominator is
  131. positive.
  132. REDUCE has a built-in symbolic function SIMP!*, which returns the
  133. simplified standard quotient representation of its argument, which can
  134. be either Cambridge or REDUCE prefix. REDUCE also has symbolic
  135. functions named NEGSQ, INVSQ, ADDSQ, MULTSQ, DIVSQ, DIFFSQ, and CANONSQ
  136. which respectively negate, reciprocate, add, multiply, divide,
  137. differentiate, and unit-normalize standard quotients. There is also a
  138. function named ABSQ, which negates a standard quotient if the leading
  139. coefficient of its numerator is negative, and there is a function named
  140. EXPTSQ which raises a standard quotient to an integer power. Finally,
  141. there is a function named MK!*SQ, which returns a REDUCE prefix version
  142. of its standard-quotient argument, and there is also a function named
  143. PREPSQ which returns a Cambridge prefix version of its standard-quotient
  144. argument.
  145. If there is a sequence of operations, rather than converting from
  146. prefix to standard quotient and back at each step, it is usually more
  147. efficient to do the operations on standard quotients, then use MK!*SQ
  148. to make the final result be REDUCE prefix. Also it is often more
  149. efficient to work with polynomials rather than rational functions
  150. during the intermediate steps.
  151. ;PAUSE;COMMENT
  152. The coefficient domain of polynomials is floating-point numbers,
  153. integers, integers modulo an arbitrary integer modulus, or rational
  154. numbers. However, zero is represented as NIL.
  155. The polynomial variables are called kernels, which can be
  156. indeterminates or uniquely-stored fully simplified Cambridge-prefix
  157. functional forms. The latter alternative permits the representation
  158. of expressions which could not otherwise be represented as the ratio
  159. of two expanded polynomials, such as
  160. 1. subexpressions of the form LOG(...) or SIN(...).
  161. 2. subexpressions of the form indeterminate**noninteger.
  162. 3. unexpanded polynomials, each polynomial factor being
  163. represented as a functional form.
  164. 4. rational expressions not placed over a common denominator,
  165. each quotient subexrpession being represented as a functional
  166. form.
  167. A polynomial is represented as a list of its nonzero terms in
  168. decreasing order of the degree of the leading "variable". Each term
  169. is represented as a standard power dotted with its coefficient, which
  170. is a standard form in the remaining variables. A standard power is
  171. represented as a variable dotted with a positive integer degree.
  172. ;PAUSE;COMMENT
  173. Letting ::= denote "is defined as" and letting | denote "or",
  174. we can summarize the REDUCE data representations as follows:
  175. reduceprefix ::= ('!*SQ standardquotient . !*SQVAR!*)
  176. standardquotient ::= NUMR(standardquotient) ./
  177. DENR(standardquotient)
  178. NUMR(standardquotient) ::= standardform
  179. DENR(standardquotient) ::= unitnormalstandardform
  180. domainelement ::= NIL | nonzerointeger | nonzerofloat |
  181. nonzerointeger . positiveinteger
  182. standardform ::= domainelement |
  183. LT(standardform) .+ RED(standardform)
  184. RED(standardform) ::= standardform
  185. LT(standardform) := LPOW(standardform) .* LC(standardform)
  186. LPOW(standardform) := MVAR(standardform) .** LDEG(standardform)
  187. LC(standardform) ::= standardform
  188. MVAR(standardform) ::= kernel
  189. kernel ::= indeterminate | functionalform
  190. functionalform ::= (functionname Cambridgeprefix1 Cambridgeprefix2
  191. ...)
  192. Cambridgeprefix ::= integer | float | indeterminate |
  193. functionalform
  194. LC(unitnormalstandardform) ::= positivedomainelement |
  195. unitnormalstandardform
  196. I have taken this opportunity to also introduce the major REDUCE
  197. selector macros named NUMR, DENR, LT, RED, LPOW, LC, MVAR, and LDEG,
  198. together with the major constructor macros named ./, .+, .*, and .** .
  199. The latter are just mnemonic aliases for "." A comparison of my verbal
  200. and more formal definitions also reveals that the selectors are
  201. respectively just aliases for CAR, CDR, CAR, CDR, CAAR, CDAR, CAAAR, and
  202. CDAAR. Since these selectors and constructors are macros rather than
  203. functions, they afford a more readable and modifiable programming style
  204. at no cost in ultimate efficiency. Thus you are encouraged to use them
  205. and to invent your own when convenient. As an example of how this can
  206. be done, here is the macro definition for extracting the main variable
  207. of a standard term;
  208. SYMBOLIC SMACRO PROCEDURE TVAR TRM; CAAR TRM;
  209. PAUSE;
  210. COMMENT It turns out that there are already built-in selectors named TC,
  211. TPOW, and TDEG, which respectively extract the coefficient, leading
  212. power, and leading degree of a standard term. There are also built-in
  213. constructors named !*P2F, !*K2F, !*K2Q, and !*T2Q, which respectively
  214. make a power into a standard form, a kernel into a standard form, a
  215. kernel into a standard quotient, and a term into a standard quotient.
  216. See the User's Manual for a complete list.
  217. The unary functions NEGF and ABSF respectively negate, and unit-
  218. normalize their standard-form arguments. The binary functions ADDF,
  219. MULTF, QUOTF, SUBF, EXPTF, and GCDF respectively add, multiply, divide,
  220. substitute into, raise to a positive integer power, and determine the
  221. greatest common divisor of standard forms. See if you can use them to
  222. define a macro which subtracts standard forms;
  223. PAUSE;
  224. COMMENT The best way to become adept at working with standard forms and
  225. standard quotients is to study the corresponding portions of the REDUCE
  226. source listing. The listing of ADDF and its subordinates is
  227. particularly instructive. As an exercise, see if you can write a
  228. function named ISFREEOFKERN which determines whether or not its left
  229. argument is free of the kernel which is the right argument, using REDUCE
  230. prefix rather than Cambridge prefix for the left argument;
  231. PAUSE;
  232. COMMENT As a final example of the interaction between modes, here
  233. is a function which produces simple print plots;
  234. SHARE NCOLS;
  235. NCOLS := 66;
  236. SYMBOLIC OPERATOR PLOT;
  237. SYMBOLIC PROCEDURE PLOT(EX, XINIT, DX, NDX, YINIT, DY);
  238. BEGIN COMMENT This procedure produces a print-plot of univariate
  239. expression EX where,
  240. XINIT is the initial value of the indeterminate,
  241. DX is the increment per line of the indeterminate,
  242. NDX is the number of lines plotted,
  243. YINIT is the value represented at the left edge,
  244. DY is incremental value per column.
  245. The shared variable NCOLS, initially 66, is the number of columns
  246. used. Points are plotted using "*", except "<" and ">" are used
  247. at the left and right edges to indicate out of bounds points.
  248. Without supplementary rules, many REDUCE implementations will be
  249. unable to numerically evaluate expressions involving operations
  250. other than +, -, *, /, and integer powers;
  251. SCALAR X, F, ROUNDSAV, Y; INTEGER COL, NCOLSMINUS1;
  252. ROUNDSAV := !*ROUNDED; % initial float mode;
  253. ON ROUNDED;
  254. NCOLSMINUS1 := NCOLS - 1;
  255. WRITE "Starting the plot of ",EX;
  256. X := LISTOFVARS EX; % find indeterminates;
  257. IF LENGTH X > 1 THEN REDERR
  258. "ERROR: PLOT expression can have at most 1 indeterminate";
  259. IF NULL X THEN <<
  260. WRITE "ERROR: no indeterminates in ", EX;
  261. REDERR "" >>
  262. ELSE X := CAR X;
  263. WRITE " in variable ",x;terpri();
  264. COMMENT Convert args from algebraic to symbolic values;
  265. XINIT := REVAL XINIT;
  266. DX := REVAL DX;
  267. YINIT := REVAL YINIT;
  268. DY := REVAL DY;
  269. FOR J:= 0:NDX DO <<
  270. % generate expression with current value substituted for x
  271. F := SUBST(XINIT + J*DX, X, EX);
  272. Y := EVAL(F); % eval expression
  273. COL := RND((Y - YINIT)/DY); % scale and round for cols
  274. IF COL<0 THEN WRITE "<"
  275. ELSE IF COL > NCOLSMINUS1 THEN << SPACES(NCOLSMINUS1);
  276. PRIN2 ">";
  277. TERPRI() >>
  278. ELSE << SPACES(COL);
  279. PRIN2 "*";
  280. TERPRI() >>
  281. >> ;
  282. IF NULL ROUNDSAV THEN OFF ROUNDED;
  283. IF NULL Y THEN REDERR
  284. "ERROR: UNABLE TO PERFORM FLOATING-POINT EVALUATION OF 1ST ARG"
  285. END;
  286. PAUSE;
  287. SYMBOLIC PROCEDURE LISTOFVARS CAMPRE;
  288. IF NULL CAMPRE OR NUMBERP CAMPRE THEN NIL
  289. ELSE IF ATOM CAMPRE THEN LIST CAMPRE
  290. ELSE VARSINARGS CDR CAMPRE;
  291. SYMBOLIC PROCEDURE VARSINARGS LISTOFCAMPRE;
  292. BEGIN SCALAR X;
  293. RETURN IF NULL LISTOFCAMPRE THEN NIL
  294. ELSE UNION(LISTOFVARS CAR LISTOFCAMPRE, VARSINARGS CDR LISTOFCAMPRE);
  295. END;
  296. SYMBOLIC PROCEDURE RND X;
  297. BEGIN SCALAR ANS, ROUNDSAV;
  298. ROUNDSAV := !*ROUNDED;
  299. ON ROUNDED;
  300. ANS := REVAL X;
  301. IF NOT NUMBERP X THEN REDDERR "RND GIVEN NON-NUMERIC ARGUMENT";
  302. IF ANS >=0 THEN ANS := FIX(ANS+00.5)
  303. ELSE ANS:= FIX(ANS-0.5);
  304. IF NULL ROUNDSAV THEN OFF ROUNDED;
  305. RETURN ANS
  306. END;
  307. PAUSE;
  308. PLOT(Y**2, 0, 0.25, 10, 0, 0.25);
  309. PAUSE;
  310. PLOT((A+1)**2, 0, 0.25, 10, 0, 0.25);
  311. PAUSE;
  312. B := A*2;
  313. PLOT(A*B, 0, 0.25, 10, 0, 0.25);
  314. PAUSE;
  315. COMMENT We leave it as an exercise to write a more elaborate plot
  316. procedure which offers amenities such as automatic scaling, numbered
  317. ordinates, etc.
  318. Good luck with these exercises, with REDUCE, with computer algebra and
  319. with all of your endeavors.
  320. ;END;