structr.tex 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. \chapter{Display and Structuring of Expressions}\index{Display}
  2. \index{Structuring}
  3. In this section, we consider a variety of commands and operators that
  4. permit the user to obtain various parts of algebraic expressions and also
  5. display their structure in a variety of forms. Also presented are some
  6. additional concepts in the {\REDUCE} design that help the user gain a better
  7. understanding of the structure of the system.
  8. \section{Kernels}\index{Kernel}
  9. {\REDUCE} is designed so that each operator in the system has an
  10. evaluation (or simplification)\index{Simplification} function associated
  11. with it that transforms the expression into an internal canonical form.
  12. \index{Canonical form} This form, which bears little resemblance to the
  13. original expression, is described in detail in Hearn, A. C., ``{\REDUCE} 2:
  14. A System and Language for Algebraic Manipulation,'' Proc. of the Second
  15. Symposium on Symbolic and Algebraic Manipulation, ACM, New York (1971)
  16. 128-133.
  17. The evaluation function may transform its arguments in one of two
  18. alternative ways. First, it may convert the expression into other
  19. operators in the system, leaving no functions of the original operator for
  20. further manipulation. This is in a sense true of the evaluation functions
  21. associated with the operators {\tt +}, {\tt *} and {\tt /} , for example,
  22. because the canonical form\index{Canonical form} does not include these
  23. operators explicitly. It is also true of an operator such as the
  24. determinant operator {\tt DET}\ttindex{DET} because the relevant
  25. evaluation function calculates the appropriate determinant, and the
  26. operator {\tt DET} no longer appears. On the other hand, the evaluation
  27. process may leave some residual functions of the relevant operator. For
  28. example, with the operator {\tt COS}, a residual expression like {\tt
  29. COS(X)} may remain after evaluation unless a rule for the reduction of
  30. cosines into exponentials, for example, were introduced. These residual
  31. functions of an operator are termed {\em kernels\/}\index{Kernel} and are
  32. stored uniquely like variables. Subsequently, the kernel is carried
  33. through the calculation as a variable unless transformations are
  34. introduced for the operator at a later stage.
  35. In those cases where the evaluation process leaves an operator expression
  36. with non-trivial arguments, the form of the argument can vary depending on
  37. the state of the system at the point of evaluation. Such arguments are
  38. normally produced in expanded form with no terms factored or grouped in
  39. any way. For example, the expression {\tt cos(2*x+2*y)} will normally be
  40. returned in the same form. If the argument {\tt 2*x+2*y} were evaluated
  41. at the top level, however, it would be printed as {\tt 2*(X+Y)}. If it is
  42. desirable to have the arguments themselves in a similar form, the switch
  43. {\tt INTSTR}\ttindex{INTSTR} (for ``internal structure''), if on, will
  44. cause this to happen.
  45. In cases where the arguments of the kernel operators may be reordered, the
  46. system puts them in a canonical order, based on an internal intrinsic
  47. ordering of the variables. However, some commands allow arguments in the
  48. form of kernels, and the user has no way of telling what internal order the
  49. system will assign to these arguments. To resolve this difficulty, we
  50. introduce the notion of a {\em kernel form\/}\index{kernel form} as an
  51. expression that transforms to a kernel on evaluation.
  52. Examples of kernel forms are:
  53. \begin{verbatim}
  54. a
  55. cos(x*y)
  56. log(sin(x))
  57. \end{verbatim}
  58. whereas
  59. \begin{verbatim}
  60. a*b
  61. (a+b)^4
  62. \end{verbatim}
  63. are not.
  64. We see that kernel forms can usually be used as generalized variables, and
  65. most algebraic properties associated with variables may also be associated
  66. with kernels.
  67. \section{The Expression Workspace}\index{Workspace}
  68. Several mechanisms are available for saving and retrieving previously
  69. evaluated expressions. The simplest of these refers to the last algebraic
  70. expression simplified. When an assignment of an algebraic expression is
  71. made, or an expression is evaluated at the top level, (i.e., not inside a
  72. compound statement or procedure) the results of the evaluation are
  73. automatically saved in a variable {\tt WS} that we shall refer to as the
  74. workspace. (More precisely, the expression is assigned to the variable
  75. {\tt WS} that is then available for further manipulation.)
  76. {\it Example:}
  77. If we evaluate the expression {\tt (x+y)\verb|^|2} at the top level and next
  78. wish to differentiate it with respect to {\tt Y}, we can simply say
  79. \begin{verbatim}
  80. df(ws,y);
  81. \end{verbatim}
  82. to get the desired answer.
  83. If the user wishes to assign the workspace to a variable or expression for
  84. later use, the {\tt SAVEAS}\ttindex{SAVEAS} statement can be used. It
  85. has the syntax
  86. \begin{verbatim}
  87. SAVEAS <expression>
  88. \end{verbatim}
  89. For example, after the differentiation in the last example, the workspace
  90. holds the expression {\tt 2*x+2*y}. If we wish to assign this to the
  91. variable {\tt Z} we can now say
  92. \begin{verbatim}
  93. saveas z;
  94. \end{verbatim}
  95. If the user wishes to save the expression in a form that allows him to use
  96. some of its variables as arbitrary parameters, the {\tt FOR ALL}
  97. command can be used.
  98. {\it Example:}
  99. \begin{verbatim}
  100. for all x saveas h(x);
  101. \end{verbatim}
  102. with the above expression would mean that {\tt h(z)} evaluates to {\tt
  103. 2*Y+2*Z}.
  104. A further method for referencing more than the last expression is described
  105. in the section on interactive use of {\REDUCE}.
  106. \section{Output of Expressions}
  107. A considerable degree of flexibility is available in {\REDUCE} in the
  108. printing of expressions generated during calculations. No explicit format
  109. statements are supplied, as these are in most cases of little use in
  110. algebraic calculations, where the size of output or its composition is not
  111. generally known in advance. Instead, {\REDUCE} provides a series of mode
  112. options to the user that should enable him to produce his output in a
  113. comprehensible and possibly pleasing form.
  114. The most extreme option offered is to suppress the output entirely from
  115. any top level evaluation. This is accomplished by turning off the switch
  116. {\tt OUTPUT}\ttindex{OUTPUT} which is normally on. It is useful for
  117. limiting output when loading large files or producing ``clean'' output from
  118. the prettyprint programs.
  119. In most circumstances, however, we wish to view the output, so we need to
  120. know how to format it appropriately. As we mentioned earlier, an
  121. algebraic expression is normally printed in an expanded form, filling the
  122. whole output line with terms. Certain output declarations,\index{Output
  123. declaration} however, can be used to affect this format. To begin with,
  124. we look at an operator for changing the length of the output line.
  125. \subsection{LINELENGTH Operator}\ttindex{LINELENGTH}
  126. This operator is used with the syntax
  127. \begin{verbatim}
  128. LINELENGTH(NUM:integer):integer
  129. \end{verbatim}
  130. and sets the output line length to the integer {\tt NUM}. It returns the
  131. previous output line length (so that it can be stored for later resetting
  132. of the output line if needed).
  133. \subsection{Output Declarations}
  134. We now describe a number of switches and declarations that are available
  135. for controlling output formats. It should be noted, however, that the
  136. transformation of large expressions to produce these varied output formats
  137. can take a lot of computing time and space. If a user wishes to speed up
  138. the printing of the output in such cases, he can turn off the switch {\tt
  139. PRI}.\ttindex{PRI} If this is done, then output is produced in one fixed
  140. format, which basically reflects the internal form of the expression, and
  141. none of the options below apply. {\tt PRI} is normally on.
  142. With {\tt PRI} on, the output declarations\index{Output declaration}
  143. and switches available are as follows:
  144. \subsubsection{ORDER Declaration}
  145. The declaration {\tt ORDER}\ttindex{ORDER} may be used to order variables
  146. on output. The syntax is:
  147. \begin{verbatim}
  148. order v1,...vn;
  149. \end{verbatim}
  150. where the {\tt vi} are kernels. Thus,
  151. \begin{verbatim}
  152. order x,y,z;
  153. \end{verbatim}
  154. orders {\tt X} ahead of {\tt Y}, {\tt Y} ahead of {\tt Z} and all three
  155. ahead of other variables not given an order. {\tt order nil;} resets the
  156. output order to the system default. The order of variables may be changed
  157. by further calls of {\tt ORDER}, but then the reordered variables would
  158. have an order lower than those in earlier {\tt ORDER}\ttindex{ORDER} calls.
  159. Thus,
  160. \begin{verbatim}
  161. order x,y,z;
  162. order y,x;
  163. \end{verbatim}
  164. would order {\tt Z} ahead of {\tt Y} and {\tt X}. The default ordering is
  165. usually alphabetic.
  166. \subsubsection{FACTOR Declaration}
  167. This declaration takes a list of identifiers or kernels\index{Kernel}
  168. as argument. {\tt FACTOR}\ttindex{FACTOR} is not a factoring command
  169. (use {\tt FACTORIZE} or the {\tt FACTOR} switch for this purpose); rather it
  170. is a separation command. All terms involving fixed powers of the declared
  171. expressions are printed as a product of the fixed powers and a sum of the
  172. rest of the terms.
  173. All expressions involving a given prefix operator may also be factored by
  174. putting the operator name in the list of factored identifiers. For example:
  175. \begin{verbatim}
  176. factor x,cos,sin(x);
  177. \end{verbatim}
  178. causes all powers of {\tt X} and {\tt SIN(X)} and all functions of
  179. {\tt COS} to be factored.
  180. Note that {\tt FACTOR} does not affect the order of its arguments. You
  181. should also use {\tt ORDER} if this is important.
  182. The declaration {\tt remfac v1,...,vn;}\ttindex{REMFAC} removes the
  183. factoring flag from the expressions {\tt v1} through {\tt vn}.
  184. \subsection{Output Control Switches}
  185. \label{sec-output}
  186. In addition to these declarations, the form of the output can be modified
  187. by switching various output control switches using the declarations
  188. {\tt ON} and {\tt OFF}. We shall illustrate the use of these switches by an
  189. example, namely the printing of the expression
  190. \begin{verbatim}
  191. x^2*(y^2+2*y)+x*(y^2+z)/(2*a) .
  192. \end{verbatim}
  193. The relevant switches are as follows:
  194. \subsubsection{ALLFAC Switch}
  195. This switch will cause the system to search the whole expression, or any
  196. sub-expression enclosed in parentheses, for simple multiplicative factors
  197. and print them outside the parentheses. Thus our expression with {\tt ALLFAC}
  198. \ttindex{ALLFAC}
  199. off will print as
  200. \begin{verbatim}
  201. 2 2 2 2
  202. (2*X *Y *A + 4*X *Y*A + X*Y + X*Z)/(2*A)
  203. \end{verbatim}
  204. and with {\tt ALLFAC} on as
  205. \begin{verbatim}
  206. 2 2
  207. X*(2*X*Y *A + 4*X*Y*A + Y + Z)/(2*A) .
  208. \end{verbatim}
  209. {\tt ALLFAC} is normally on, and is on in the following examples, except
  210. where otherwise stated.
  211. \subsubsection{DIV Switch}\ttindex{DIV}
  212. This switch makes the system search the denominator of an expression for
  213. simple factors that it divides into the numerator, so that rational
  214. fractions and negative powers appear in the output. With {\tt DIV} on, our
  215. expression would print as
  216. \begin{verbatim}
  217. 2 2 (-1) (-1)
  218. X*(X*Y + 2*X*Y + 1/2*Y *A + 1/2*A *Z) .
  219. \end{verbatim}
  220. {\tt DIV} is normally off.
  221. \subsubsection{LIST Switch}\ttindex{LIST}
  222. This switch causes the system to print each term in any sum on a separate
  223. line. With {\tt LIST} on, our expression prints as
  224. \begin{verbatim}
  225. 2
  226. X*(2*X*Y *A
  227. + 4*X*Y*A
  228. 2
  229. + Y
  230. + Z)/(2*A) .
  231. \end{verbatim}
  232. {\tt LIST} is normally off.
  233. \subsubsection{NOSPLIT Switch}\ttindex{NOSPLIT}
  234. Under normal circumstances, the printing routines try to break an expression
  235. across lines at a natural point. This is a fairly expensive process. If
  236. you are not overly concerned about where the end-of-line breaks come, you
  237. can speed up the printing of expressions by turning off the switch
  238. {\tt NOSPLIT}. This switch is normally on.
  239. \subsubsection{RAT Switch}\ttindex{RAT}
  240. This switch is only useful with expressions in which variables are
  241. factored with {\tt FACTOR}. With this mode, the overall denominator of the
  242. expression is printed with each factored sub-expression. We assume a prior
  243. declaration {\tt factor x;} in the following output. We first print the
  244. expression with {\tt RAT off}:
  245. \begin{verbatim}
  246. 2 2
  247. (2*X *Y*A*(Y + 2) + X*(Y + Z))/(2*A) .
  248. \end{verbatim}
  249. With {\tt RAT} on the output becomes:
  250. \extendedmanual{\newpage}
  251. \begin{verbatim}
  252. 2 2
  253. X *Y*(Y + 2) + X*(Y + Z)/(2*A) .
  254. \end{verbatim}
  255. {\tt RAT} is normally off.
  256. Next, if we leave {\tt X} factored, and turn on both {\tt DIV} and
  257. {\tt RAT}, the result becomes
  258. \begin{verbatim}
  259. 2 (-1) 2
  260. X *Y*(Y + 2) + 1/2*X*A *(Y + Z) .
  261. \end{verbatim}
  262. Finally, with {\tt X} factored, {\tt RAT} on and {\tt ALLFAC}\ttindex{ALLFAC}
  263. off we retrieve the original structure
  264. \begin{verbatim}
  265. 2 2 2
  266. X *(Y + 2*Y) + X*(Y + Z)/(2*A) .
  267. \end{verbatim}
  268. \subsubsection{RATPRI Switch}\ttindex{RATPRI}
  269. If the numerator and denominator of an expression can each be printed in
  270. one line, the output routines will print them in a two dimensional
  271. notation, with numerator and denominator on separate lines and a line of
  272. dashes in between. For example, {\tt (a+b)/2} will print as
  273. \begin{verbatim}
  274. A + B
  275. -----
  276. 2
  277. \end{verbatim}
  278. Turning this switch off causes such expressions to be output in a linear
  279. form.
  280. \subsubsection{REVPRI Switch}\ttindex{REVPRI}
  281. The normal ordering of terms in output is from highest to lowest power.
  282. In some situations (e.g., when a power series is output), the opposite
  283. ordering is more convenient. The switch {\tt REVPRI} if on causes such a
  284. reverse ordering of terms. For example, the expression
  285. {\tt y*(x+1)\verb|^|2+(y+3)\verb|^|2} will normally print as
  286. \begin{verbatim}
  287. 2 2
  288. X *Y + 2*X*Y + Y + 7*Y + 9
  289. \end{verbatim}
  290. whereas with {\tt REVPRI} on, it will print as
  291. \begin{verbatim}
  292. 2 2
  293. 9 + 7*Y + Y + 2*X*Y + X *Y.
  294. \end{verbatim}
  295. \subsection{WRITE Command}\ttindex{WRITE}
  296. In simple cases no explicit output\index{Output} command is necessary in
  297. {\REDUCE}, since the value of any expression is automatically printed if a
  298. semicolon is used as a delimiter. There are, however, several situations
  299. in which such a command is useful.
  300. In a {\tt FOR}, {\tt WHILE}, or {\tt REPEAT} statement it may be desired
  301. to output something each time the statement within the loop construct is
  302. repeated.
  303. It may be desired for a procedure to output intermediate results or other
  304. information while it is running. It may be desired to have results labeled
  305. in special ways, especially if the output is directed to a file or device
  306. other than the terminal.
  307. The {\tt WRITE} command consists of the word {\tt WRITE} followed by one
  308. or more items separated by commas, and followed by a terminator. There
  309. are three kinds of items that can be used:
  310. \begin{enumerate}
  311. \item Expressions (including variables and constants). The expression is
  312. evaluated, and the result is printed out.
  313. \item Assignments. The expression on the right side of the {\tt :=}
  314. operator is evaluated, and is assigned to the variable on the left; then
  315. the symbol on the left is printed, followed by a ``{\tt :=}'', followed by
  316. the value of the expression on the right -- almost exactly the way an
  317. assignment followed by a semicolon prints out normally. (The difference is
  318. that if the {\tt WRITE} is in a {\tt FOR} statement and the left-hand side
  319. of the assignment is an array position or something similar containing the
  320. variable of the {\tt FOR} iteration, then the value of that variable is
  321. inserted in the printout.)
  322. \item Arbitrary strings of characters, preceded and followed by double-quote
  323. marks (e.g., {\tt "string"}).
  324. \end{enumerate}
  325. The items specified by a single {\tt WRITE} statement print side by side
  326. on one line. (The line is broken automatically if it is too long.) Strings
  327. print exactly as quoted. The {\tt WRITE} command itself however does not
  328. return a value.
  329. The print line is closed at the end of a {\tt WRITE} command evaluation.
  330. Therefore the command {\tt WRITE "";} (specifying nothing to be printed
  331. except the empty string) causes a line to be skipped.
  332. {\it Examples:}
  333. \begin{enumerate}
  334. \item If {\tt A} is {\tt X+5}, {\tt B} is itself, {\tt C} is 123, {\tt M} is
  335. an array, and {\tt Q}=3, then
  336. \begin{verbatim}
  337. write m(q):=a," ",b/c," THANK YOU";
  338. \end{verbatim}
  339. will set {\tt M(3)} to {\tt x+5} and print
  340. \begin{verbatim}
  341. M(Q) := X + 5 B/123 THANK YOU
  342. \end{verbatim}
  343. The blanks between the {\tt 5} and {\tt B}, and the
  344. {\tt 3} and {\tt T}, come from the blanks in the quoted strings.
  345. \item To print a table of the squares of the integers from 1 to 20:
  346. \begin{verbatim}
  347. for i:=1:20 do write i," ",i^2;
  348. \end{verbatim}
  349. \item To print a table of the squares of the integers from 1 to 20, and at
  350. the same time store them in positions 1 to 20 of an array {\tt A:}
  351. \begin{verbatim}
  352. for i:=1:20 do <<a(i):=i^2; write i," ",a(i)>>;
  353. \end{verbatim}
  354. This will give us two columns of numbers. If we had used
  355. \begin{verbatim}
  356. for i:=1:20 do write i," ",a(i):=i^2;
  357. \end{verbatim}
  358. we would also get {\tt A(}i{\tt ) := } repeated on each line.
  359. \item The following more complete example calculates the famous f and g
  360. series, first reported in Sconzo, P., LeSchack, A. R., and Tobey, R.,
  361. ``Symbolic Computation of f and g Series by Computer'', Astronomical Journal
  362. 70 (May 1965).
  363. \begin{verbatim}
  364. x1:= -sig*(mu+2*eps)$
  365. x2:= eps - 2*sig^2$
  366. x3:= -3*mu*sig$
  367. f:= 1$
  368. g:= 0$
  369. for i:= 1 step 1 until 10 do begin
  370. f1:= -mu*g+x1*df(f,eps)+x2*df(f,sig)+x3*df(f,mu);
  371. write "f(",i,") := ",f1;
  372. g1:= f+x1*df(g,eps)+x2*df(g,sig)+x3*df(g,mu);
  373. write "g(",i,") := ",g1;
  374. f:=f1$
  375. g:=g1$
  376. end;
  377. \end{verbatim}
  378. A portion of the output, to illustrate the printout from the {\tt WRITE}
  379. command, is as follows:
  380. \begin{verbatim}
  381. ... <prior output> ...
  382. 2
  383. F(4) := MU*(3*EPS - 15*SIG + MU)
  384. G(4) := 6*SIG*MU
  385. 2
  386. F(5) := 15*SIG*MU*( - 3*EPS + 7*SIG - MU)
  387. 2
  388. G(5) := MU*(9*EPS - 45*SIG + MU)
  389. ... <more output> ...
  390. \end{verbatim}
  391. \end{enumerate}
  392. \subsection{Suppression of Zeros}
  393. It is sometimes annoying to have zero assignments (i.e. assignments of the
  394. form {\tt <expression> := 0}) printed, especially in printing large arrays
  395. with many zero elements. The output from such assignments can be
  396. suppressed by turning on the switch {\tt NERO}.\ttindex{NERO}
  397. \subsection{{FORTRAN} Style Output Of Expressions}
  398. It is naturally possible to evaluate expressions numerically in {\REDUCE} by
  399. giving all variables and sub-expressions numerical values. However, as we
  400. pointed out elsewhere the user must declare real arithmetical operation by
  401. turning on the switch {\tt ROUNDED}\ttindex{ROUNDED}. However, it should be
  402. remembered that arithmetic in {\REDUCE} is not particularly fast, since
  403. results are interpreted rather than evaluated in a compiled form. The user
  404. with a large amount of numerical computation after all necessary algebraic
  405. manipulations have been performed is therefore well advised to perform
  406. these calculations in a FORTRAN\index{FORTRAN} or similar system. For
  407. this purpose, {\REDUCE} offers facilities for users to produce FORTRAN
  408. compatible files for numerical processing.
  409. First, when the switch {\tt FORT}\ttindex{FORT} is on, the system will
  410. print expressions in a FORTRAN notation. Expressions begin in column
  411. seven. If an expression extends over one line, a continuation mark (.)
  412. followed by a blank appears on subsequent cards. After a certain number
  413. of lines have been produced (according to the value of the variable {\tt
  414. CARD\_NO}),\ttindex{CARD\_NO} a new expression is started. If the
  415. expression printed arises from an assignment to a variable, the variable
  416. is printed as the name of the expression. Otherwise the expression is
  417. given the default name {\tt ANS}. An error occurs if identifiers or
  418. numbers are outside the bounds permitted by FORTRAN.
  419. A second option is to use the {\tt WRITE} command to produce other programs.
  420. {\it Example:}
  421. The following {\REDUCE} statements
  422. \begin{verbatim}
  423. on fort;
  424. out "forfil";
  425. write "C this is a fortran program";
  426. write " 1 format(e13.5)";
  427. write " u=1.23";
  428. write " v=2.17";
  429. write " w=5.2";
  430. x:=(u+v+w)^11;
  431. write "C it was foolish to expand this expression";
  432. write " print 1,x";
  433. write " end";
  434. shut "forfil";
  435. off fort;
  436. \end{verbatim}
  437. will generate a file {\tt forfil} that contains:
  438. {\small
  439. \begin{verbatim}
  440. c this is a fortran program
  441. 1 format(e13.5)
  442. u=1.23
  443. v=2.17
  444. w=5.2
  445. ans1=1320.*u**3*v*w**7+165.*u**3*w**8+55.*u**2*v**9+495.*u
  446. . **2*v**8*w+1980.*u**2*v**7*w**2+4620.*u**2*v**6*w**3+
  447. . 6930.*u**2*v**5*w**4+6930.*u**2*v**4*w**5+4620.*u**2*v**3*
  448. . w**6+1980.*u**2*v**2*w**7+495.*u**2*v*w**8+55.*u**2*w**9+
  449. . 11.*u*v**10+110.*u*v**9*w+495.*u*v**8*w**2+1320.*u*v**7*w
  450. . **3+2310.*u*v**6*w**4+2772.*u*v**5*w**5+2310.*u*v**4*w**6
  451. . +1320.*u*v**3*w**7+495.*u*v**2*w**8+110.*u*v*w**9+11.*u*w
  452. . **10+v**11+11.*v**10*w+55.*v**9*w**2+165.*v**8*w**3+330.*
  453. . v**7*w**4+462.*v**6*w**5+462.*v**5*w**6+330.*v**4*w**7+
  454. . 165.*v**3*w**8+55.*v**2*w**9+11.*v*w**10+w**11
  455. x=u**11+11.*u**10*v+11.*u**10*w+55.*u**9*v**2+110.*u**9*v*
  456. . w+55.*u**9*w**2+165.*u**8*v**3+495.*u**8*v**2*w+495.*u**8
  457. . *v*w**2+165.*u**8*w**3+330.*u**7*v**4+1320.*u**7*v**3*w+
  458. . 1980.*u**7*v**2*w**2+1320.*u**7*v*w**3+330.*u**7*w**4+462.
  459. . *u**6*v**5+2310.*u**6*v**4*w+4620.*u**6*v**3*w**2+4620.*u
  460. . **6*v**2*w**3+2310.*u**6*v*w**4+462.*u**6*w**5+462.*u**5*
  461. . v**6+2772.*u**5*v**5*w+6930.*u**5*v**4*w**2+9240.*u**5*v
  462. . **3*w**3+6930.*u**5*v**2*w**4+2772.*u**5*v*w**5+462.*u**5
  463. . *w**6+330.*u**4*v**7+2310.*u**4*v**6*w+6930.*u**4*v**5*w
  464. . **2+11550.*u**4*v**4*w**3+11550.*u**4*v**3*w**4+6930.*u**
  465. . 4*v**2*w**5+2310.*u**4*v*w**6+330.*u**4*w**7+165.*u**3*v
  466. . **8+1320.*u**3*v**7*w+4620.*u**3*v**6*w**2+9240.*u**3*v**
  467. . 5*w**3+11550.*u**3*v**4*w**4+9240.*u**3*v**3*w**5+4620.*u
  468. . **3*v**2*w**6+ans1
  469. c it was foolish to expand this expression
  470. print 1,x
  471. end
  472. \end{verbatim}
  473. }
  474. If the arguments of a {\tt WRITE} statement include an expression that
  475. requires continuation records, the output will need editing, since the
  476. output routine prints the arguments of {\tt WRITE} sequentially, and the
  477. continuation mechanism therefore generates its auxiliary variables after
  478. the preceding expression has been printed.
  479. Finally, since there is no direct analog of {\em list\/} in FORTRAN,
  480. a comment line of the form
  481. \begin{verbatim}
  482. c ***** invalid fortran construct (list) not printed
  483. \end{verbatim}
  484. will be printed if you try to print a list with {\tt FORT} on.
  485. \subsubsection{{FORTRAN} Output Options}\index{Output}\index{FORTRAN}
  486. There are a number of methods available to change the default format of the
  487. FORTRAN output.
  488. The breakup of the expression into subparts is such that the number of
  489. continuation lines produced is less than a given number. This number can
  490. be modified by the assignment
  491. \begin{verbatim}
  492. card_no := <number>;
  493. \end{verbatim}
  494. where {\tt <number>} is the {\em total\/} number of cards allowed in a
  495. statement. The default value of {\tt CARD\_NO} is 20.
  496. The width of the output expression is also adjustable by the assignment
  497. \begin{verbatim}
  498. fort_width := <integer>;
  499. \end{verbatim}
  500. \ttindex{FORT\_WIDTH} which sets the total width of a given line to
  501. {\tt <integer>}. The initial FORTRAN output width is 70.
  502. {\REDUCE} automatically inserts a decimal point after each isolated integer
  503. coefficient in a FORTRAN expression (so that, for example, 4 becomes
  504. {\tt 4.} ). To prevent this, set the {\tt PERIOD}\ttindex{PERIOD}
  505. mode switch to {\tt OFF}.
  506. FORTRAN output is normally produced in lower case. If upper case is desired,
  507. the switch {\tt FORTUPPER}\ttindex{FORTUPPER} should be turned on.
  508. Finally, the default name {\tt ANS} assigned to an unnamed expression and
  509. its subparts can be changed by the operator {\tt VARNAME}.
  510. \ttindex{VARNAME} This takes a single identifier as argument, which then
  511. replaces {\tt ANS} as the expression name. The value of {\tt VARNAME} is
  512. its argument.
  513. Further facilities for the production of FORTRAN and other language output
  514. are provided by the SCOPE and GENTRAN
  515. packages\extendedmanual{described in chapters~\ref{GENTRAN} and \ref{SCOPE}}.
  516. \subsection{Saving Expressions for Later Use as Input}
  517. \index{Saving an expression}
  518. It is often useful to save an expression on an external file for use later
  519. as input in further calculations. The commands for opening and closing
  520. output files are explained elsewhere. However, we see in the examples on
  521. output of expressions that the standard ``natural'' method of printing
  522. expressions is not compatible with the input syntax. So to print the
  523. expression in an input compatible form we must inhibit this natural style
  524. by turning off the switch {\tt NAT}.\ttindex{NAT} If this is done, a
  525. dollar sign will also be printed at the end of the expression.
  526. {\it Example:}
  527. The following sequence of commands
  528. \begin{verbatim}
  529. off nat; out "out"; x := (y+z)^2; write "end";
  530. shut "out"; on nat;
  531. \end{verbatim}
  532. will generate a file {\tt out} that contains
  533. \begin{verbatim}
  534. X := Y**2 + 2*Y*Z + Z**2$
  535. END$
  536. \end{verbatim}
  537. \subsection{Displaying Expression Structure}\index{Displaying structure}
  538. In those cases where the final result has a complicated form, it is often
  539. convenient to display the skeletal structure of the answer. The operator
  540. {\tt STRUCTR},\ttindex{STRUCTR} that takes a single expression as argument,
  541. will do this for you. Its syntax is:
  542. \begin{verbatim}
  543. STRUCTR(EXPRN:algebraic[,ID1:identifier[,ID2:identifier]]);
  544. \end{verbatim}
  545. The structure is printed effectively as a tree, in which the subparts are
  546. laid out with auxiliary names. If the optional {\tt ID1} is absent, the
  547. auxiliary names are prefixed by the root {\tt ANS}. This root may be
  548. changed by the operator {\tt VARNAME}\ttindex{VARNAME}. If the
  549. optional {\tt ID1} is present, and is an array name, the subparts are
  550. named as elements of that array, otherwise {\tt ID1} is used as the root
  551. prefix. (The second optional argument {\tt ID2} is explained later.)
  552. The {\tt EXPRN} can be either a scalar or a matrix expression. Use of any
  553. other will result in an error.
  554. {\it Example:}
  555. Let us suppose that the workspace contains
  556. {\tt ((A+B)\verb|^|2+C)\verb|^|3+D}.
  557. Then the input {\tt STRUCTR WS;} will (with {\tt EXP} off) result in the
  558. output:\newpage
  559. \begin{verbatim}
  560. ANS3
  561. where
  562. 3
  563. ANS3 := ANS2 + D
  564. 2
  565. ANS2 := ANS1 + C
  566. ANS1 := A + B
  567. \end{verbatim}
  568. The workspace remains unchanged after this operation, since {\tt STRUCTR}
  569. \ttindex{STRUCTR} in the default situation returns
  570. no value (if {\tt STRUCTR} is used as a sub-expression, its value is taken
  571. to be 0). In addition, the sub-expressions are normally only displayed
  572. and not retained. If you wish to access the sub-expressions with their
  573. displayed names, the switch {\tt SAVESTRUCTR}\ttindex{SAVESTRUCTR} should be
  574. turned on. In this case, {\tt STRUCTR} returns a list whose first element
  575. is a representation for the expression, and subsequent elements are the
  576. sub-expression relations. Thus, with {\tt SAVESTRUCTR} on, {\tt STRUCTR WS}
  577. in the above example would return
  578. \vspace{-11pt}
  579. \begin{verbatim}
  580. 3 2
  581. {ANS3,ANS3=ANS2 + D,ANS2=ANS1 + C,ANS1=A + B}
  582. \end{verbatim}
  583. The {\tt PART}\ttindex{PART} operator can
  584. be used to retrieve the required parts of the expression. For example, to
  585. get the value of {\tt ANS2} in the above, one could say:
  586. \begin{verbatim}
  587. part(ws,3,2);
  588. \end{verbatim}
  589. If {\tt FORT} is on, then the results are printed in the reverse order; the
  590. algorithm in fact guaranteeing that no sub-expression will be referenced
  591. before it is defined. The second optional argument {\tt ID2} may also be
  592. used in this case to name the actual expression (or expressions in the
  593. case of a matrix argument).
  594. {\it Example:}
  595. Let us suppose that {\tt M}, a 2 by 1 matrix, contains the elements {\tt
  596. ((a+b)\verb|^|2 + c)\verb|^|3 + d} and {\tt (a + b)*(c + d)} respectively,
  597. and that {\tt V} has been declared to be an array. With {\tt EXP} off and
  598. {\tt FORT} on, the statement {\tt structr(2*m,v,k);} will result in the output
  599. \begin{verbatim}
  600. V(1)=A+B
  601. V(2)=V(1)**2+C
  602. V(3)=V(2)**3+D
  603. V(4)=C+D
  604. K(1,1)=2.*V(3)
  605. K(2,1)=2.*V(1)*V(4)
  606. \end{verbatim}
  607. \section{Changing the Internal Order of Variables}
  608. The internal ordering of variables (more specifically kernels) can have
  609. a significant effect on the space and time associated with a calculation.
  610. In its default state, {\REDUCE} uses a specific order for this which may
  611. vary between sessions. However, it is possible for the user to change
  612. this internal order by means of the declaration
  613. {\tt KORDER}\ttindex{KORDER}. The syntax for this is:
  614. \begin{verbatim}
  615. korder v1,...,vn;
  616. \end{verbatim}
  617. where the {\tt Vi} are kernels\index{Kernel}. With this declaration, the
  618. {\tt Vi} are ordered internally ahead of any other kernels in the system.
  619. {\tt V1} has the highest order, {\tt V2} the next highest, and so on. A
  620. further call of {\tt KORDER} replaces a previous one. {\tt KORDER NIL;}
  621. resets the internal order to the system default.
  622. Unlike the {\tt ORDER}\ttindex{ORDER} declaration, that has a purely
  623. cosmetic effect on the way results are printed, the use of {\tt KORDER}
  624. can have a significant effect on computation time. In critical cases
  625. then, the user can experiment with the ordering of the variables used to
  626. determine the optimum set for a given problem.
  627. \section{Obtaining Parts of Algebraic Expressions}
  628. There are many occasions where it is desirable to obtain a specific part
  629. of an expression, or even change such a part to another expression. A
  630. number of operators are available in {\REDUCE} for this purpose, and will be
  631. described in this section. In addition, operators for obtaining specific
  632. parts of polynomials and rational functions (such as a denominator) are
  633. described in another section.
  634. \subsection{COEFF Operator}\ttindex{COEFF}
  635. Syntax:
  636. \begin{verbatim}
  637. COEFF(EXPRN:polynomial,VAR:kernel)
  638. \end{verbatim}
  639. {\tt COEFF} is an operator that partitions {\tt EXPRN} into its various
  640. coefficients with respect to {\tt VAR} and returns them as a list, with
  641. the coefficient independent of {\tt VAR} first.
  642. Under normal circumstances, an error results if {\tt EXPRN} is not a
  643. polynomial in {\tt VAR}, although the coefficients themselves can be
  644. rational as long as they do not depend on {\tt VAR}. However, if the
  645. switch {\tt RATARG}\ttindex{RATARG} is on, denominators are not checked for
  646. dependence on {\tt VAR}, and are taken to be part of the coefficients.
  647. {\it Example:}
  648. \begin{verbatim}
  649. coeff((y^2+z)^3/z,y);
  650. \end{verbatim}
  651. returns the result
  652. \begin{verbatim}
  653. 2
  654. {Z ,0,3*Z,0,3,0,1/Z}.
  655. \end{verbatim}
  656. whereas
  657. \begin{verbatim}
  658. coeff((y^2+z)^3/y,y);
  659. \end{verbatim}
  660. gives an error if {\tt RATARG} is off, and the result
  661. \begin{verbatim}
  662. 3 2
  663. {Z /Y,0,3*Z /Y,0,3*Z/Y,0,1/Y}
  664. \end{verbatim}
  665. if {\tt RATARG} is on.
  666. The length of the result of {\tt COEFF} is the highest power of {\tt VAR}
  667. encountered plus 1. In the above examples it is 7. In addition, the
  668. variable {\tt HIGH\_POW}\ttindex{HIGH\_POW} is set to the highest non-zero
  669. power found in {\tt EXPRN} during the evaluation, and {\tt LOW\_POW}
  670. \ttindex{LOW\_POW} to the lowest non-zero power, or zero if there is a
  671. constant term. If {\tt EXPRN} is a constant, then {\tt HIGH\_POW} and
  672. {\tt LOW\_POW} are both set to zero.
  673. \subsection{COEFFN Operator}\ttindex{COEFFN}
  674. The {\tt COEFFN} operator is designed to give the user a particular
  675. coefficient of a variable in a polynomial, as opposed to {\tt COEFF} that
  676. returns all coefficients. {\tt COEFFN} is used with the syntax
  677. \begin{verbatim}
  678. COEFFN(EXPRN:polynomial,VAR:kernel,N:integer)
  679. \end{verbatim}
  680. It returns the $n^{th}$ coefficient of {\tt VAR} in the polynomial
  681. {\tt EXPRN}.
  682. \subsection{PART Operator}\ttindex{PART}
  683. Syntax:
  684. \begin{verbatim}
  685. PART(EXPRN:algebraic[,INTEXP:integer])
  686. \end{verbatim}
  687. This operator works on the form of the expression as printed {\em or as it
  688. would have been printed at that point in the calculation\/} bearing in mind
  689. all the relevant switch settings at that point. The reader therefore
  690. needs some familiarity with the way that expressions are represented in
  691. prefix form in {\REDUCE} to use these operators effectively. Furthermore,
  692. it is assumed that {\tt PRI} is {\tt ON} at that point in the calculation.
  693. The reason for this is that with {\tt PRI} off, an expression is printed
  694. by walking the tree representing the expression internally. To save
  695. space, it is never actually transformed into the equivalent prefix
  696. expression as occurs when {\tt PRI} is on. However, the operations on
  697. polynomials described elsewhere can be equally well used in this case to
  698. obtain the relevant parts.
  699. The evaluation proceeds recursively down the integer expression list. In
  700. other words,
  701. \begin{verbatim}
  702. PART(<expression>,<integer1>,<integer2>)
  703. -> PART(PART(<expression>,<integer1>),<integer2>)
  704. \end{verbatim}
  705. and so on, and
  706. \begin{verbatim}
  707. PART(<expression>) -> <expression>.
  708. \end{verbatim}
  709. {\tt INTEXP} can be any expression that evaluates to an integer. If the
  710. integer is positive, then that term of the expression is found. If the
  711. integer is 0, the operator is returned. Finally, if the integer is
  712. negative, the counting is from the tail of the expression rather than the
  713. head.
  714. For example, if the expression {\tt a+b} is printed as {\tt A+B} (i.e.,
  715. the ordering of the variables is alphabetical), then
  716. \begin{verbatim}
  717. part(a+b,2) -> B
  718. part(a+b,-1) -> B
  719. and
  720. part(a+b,0) -> PLUS
  721. \end{verbatim}
  722. An operator {\tt ARGLENGTH}\ttindex{ARGLENGTH} is available to determine
  723. the number of arguments of the top level operator in an expression. If
  724. the expression does not contain a top level operator, then $-1$ is returned.
  725. For example,
  726. \begin{verbatim}
  727. arglength(a+b+c) -> 3
  728. arglength(f()) -> 0
  729. arglength(a) -> -1
  730. \end{verbatim}
  731. \subsection{Substituting for Parts of Expressions}
  732. {\tt PART} may also be used to substitute for a given part of an
  733. expression. In this case, the {\tt PART} construct appears on the
  734. left-hand side of an assignment statement, and the expression to replace
  735. the given part on the right-hand side.
  736. For example, with the normal settings of the {\REDUCE} switches:
  737. \begin{verbatim}
  738. xx := a+b;
  739. part(xx,2) := c; -> A+C
  740. part(c+d,0) := -; -> C-D
  741. \end{verbatim}
  742. Note that {\tt xx} in the above is not changed by this substitution. In
  743. addition, unlike expressions such as array and matrix elements that have
  744. an {\em instant evaluation\/}\index{Instant evaluation} property, the values
  745. of {\tt part(xx,2)} and {\tt part(c+d,0)} are also not changed.