home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume15 / calc-1.05 / part17 < prev    next >
Internet Message Format  |  1991-09-10  |  58KB

  1. From bacchus.pa.dec.com!decwrl!elroy.jpl.nasa.gov!usc!jarthur!nntp-server.caltech.edu!nntp-server.caltech.edu!daveg Tue Oct 16 08:45:23 PDT 1990
  2. Article: 2712 of comp.sources.bugs
  3. Xref: bacchus.pa.dec.com comp.sources.bugs:2712 gnu.emacs.sources:4
  4. Path: bacchus.pa.dec.com!decwrl!elroy.jpl.nasa.gov!usc!jarthur!nntp-server.caltech.edu!nntp-server.caltech.edu!daveg
  5. From: daveg@near.cs.caltech.edu (Dave Gillespie)
  6. Newsgroups: comp.sources.bugs,gnu.emacs.sources
  7. Subject: Repost of Calc 1.05 patch, part 17/20
  8. Message-ID: <DAVEG.90Oct15155720@near.cs.caltech.edu>
  9. Date: 15 Oct 90 22:57:20 GMT
  10. Sender: news@nntp-server.caltech.edu
  11. Organization: California Institute of Technology
  12. Lines: 1167
  13. Nntp-Posting-Host: near.cs.caltech.edu
  14.  
  15. Several sites have reported that part 17 of the Calc 1.04 -> 1.05 patch
  16. arrived truncated in comp.sources.misc.  (In fact it was truncated when
  17. it arrived here.)  Following is a complete re-post of part 17 of the patch.
  18. Starting at the "#!/bin/sh" line it should be 1156 lines long.
  19.  
  20.                                                                 -- Dave
  21.  
  22. #!/bin/sh
  23. # this is part 17 of a multipart archive
  24. # do not concatenate these parts, unpack them in order with /bin/sh
  25. # file calc.patch continued
  26. #
  27. CurArch=17
  28. if test ! -r s2_seq_.tmp
  29. then echo "Please unpack part 1 first!"
  30.      exit 1; fi
  31. ( read Scheck
  32.   if test "$Scheck" != $CurArch
  33.   then echo "Please unpack part $Scheck next!"
  34.        exit 1;
  35.   else exit 0; fi
  36. ) < s2_seq_.tmp || exit 1
  37. sed 's/^X//' << 'SHAR_EOF' >> calc.patch
  38. X! 
  39. X! @kindex g v
  40. X! @kindex g V
  41. X! @pindex calc-graph-view-commands
  42. X! @pindex calc-graph-view-trail
  43. X! The @kbd{g v} (@code{calc-graph-view-commands}) and @kbd{g V}
  44. X! (@code{calc-graph-view-trail}) commands display the @samp{*Gnuplot Commands*}
  45. X! and @samp{*Gnuplot Trail*} buffers, respectively, in another window.
  46. X! This happens automatically when Calc thinks there is something you
  47. X! will want to see in either of these buffers.  If you type @kbd{g v}
  48. X! or @kbd{g V} when the relevant buffer is already displayed, the
  49. X! buffer is hidden again.
  50. X! 
  51. X! @kindex g K
  52. X! @pindex calc-graph-kill
  53. X! If something goes wrong, you can use @kbd{g K} (@code{calc-graph-kill})
  54. X! to kill the GNUPLOT process that is running.  The next graphing
  55. X! command you give will start a fresh GNUPLOT process.  The word
  56. X! @samp{Graph} appears in the Calc window's mode line whenever a GNUPLOT
  57. X! process is currently running.
  58. X! 
  59. X! @node Algebra, Units, Graphics, Top
  60. X  @chapter Algebra
  61. X  
  62. X! This section covers the Calc features that help you work with
  63. X! algebraic formulas.  First, the general sub-formula selection
  64. X! mechanism is described; this works in conjunction with any Calc
  65. X! commands.  Then, commands for specific algebraic operations are
  66. X! described.  Finally, the flexiblr @dfn{rewrite rule} mechanism
  67. X! is discussed.
  68. X! 
  69. X! The algebraic commands use the @kbd{a} key prefix; selection
  70. X! commands use the @kbd{j} (for ``just a letter that wasn't used
  71. X! for anything else'') prefix.
  72. X  
  73. X! @xref{Editing Stack Entries}, to see how to manipulate formulas
  74. X! using regular Emacs editing commands.@refill
  75. X  
  76. X  When doing algebraic work, you may find several of the Calculator's
  77. X! modes to be helpful, including algebraic-simplification mode (@kbd{m A})
  78. X! or no-simplification mode (@kbd{m O}),
  79. X  algebraic-entry mode (@kbd{m a}), fraction mode (@kbd{m f}), and
  80. X  symbolic mode (@kbd{m s}).  @xref{Mode Settings}, for discussions
  81. X  of these modes.  You may also wish to select ``big'' display mode (@kbd{d B}).
  82. X! @xref{Normal Language Modes}.@refill
  83. X  
  84. X  @menu
  85. X+ * Selecting Subformulas::
  86. X  * Algebraic Manipulation::
  87. X  * Calculus::
  88. X+ * Numerical Solutions::
  89. X+ * Logical Operations::
  90. X+ * Rewrite Rules::
  91. X  @end menu
  92. X  
  93. X! @node Selecting Subformulas, Algebraic Manipulation, Algebra, Algebra
  94. X! @section Selecting Sub-Formulas
  95. X! 
  96. X! @cindex Selections
  97. X! @cindex Sub-formulas
  98. X! @cindex Parts of formulas
  99. X! When working with an algebraic formula it is often necessary to
  100. X! manipulate a portion of the formula rather than the formula as a
  101. X! whole.  Calc allows you to ``select'' a portion of any formula on
  102. X! the stack.  Commands which would normally operate on that stack
  103. X! entry will now operate only on the sub-formula, leaving the
  104. X! surrounding part of the stack entry alone.
  105. X! 
  106. X! One common non-algebraic use for selection involves vectors.  To work
  107. X! on one element of a vector in-place, simply select that element as a
  108. X! ``sub-formula'' of the vector.
  109. X! 
  110. X! @menu
  111. X! * Making Selections::
  112. X! * Changing Selections::
  113. X! * Displaying Selections::
  114. X! * Operating on Selections::
  115. X! * Rearranging with Selections::
  116. X! @end menu
  117. X! 
  118. X! @node Making Selections, Changing Selections, Selecting Subformulas, Selecting Subformulas
  119. X! @subsection Making Selections
  120. X! 
  121. X! @kindex j s
  122. X! @pindex calc-select-here
  123. X! To select a sub-formula, move the Emacs cursor to any character in that
  124. X! sub-formula, and press @kbd{j s} (@code{calc-select-here}).  Calc will
  125. X! highlight the smallest portion of the formula that contains that
  126. X! character.  By default the sub-formula is highlighted by blanking out
  127. X! all of the rest of the formula with dots.  Selection works in any
  128. X! display mode but is perhaps easiest in ``big'' (@kbd{d B}) mode.
  129. X! Suppose you enter the following formula:
  130. X! 
  131. X! @example
  132. X!            3
  133. X!     (a + b)  + sqrt(c)
  134. X! 1:  ------------------
  135. X!          2 x + 1
  136. X! @end example
  137. X! 
  138. X! @noindent
  139. X! (by typing @kbd{' ((a+b)^3 + sqrt(c)) / (2x+1)}).  If you move the
  140. X! cursor to the letter @samp{b} and press @kbd{j s}, the display changes
  141. X! to
  142. X! 
  143. X! @example
  144. X!            .
  145. X!     .. . b.  . .......
  146. X! 1*  ..................
  147. X!          . . . .
  148. X! @end example
  149. X! 
  150. X! Every character not part of the sub-formula @samp{b} has been changed
  151. X! to a dot.  The @samp{*} next to the line number is to remind you that
  152. X! the formula has a portion of it selected.  (In this case, it's very
  153. X! obvious, but it might not always be.)
  154. X! 
  155. X! If you had instead placed the cursor on the parenthesis immediately to
  156. X! the right of the @samp{b}, the selection would have been:
  157. X! 
  158. X! @example
  159. X!            .
  160. X!     (a + b)  . .......
  161. X! 1*  ..................
  162. X!          . . . .
  163. X! @end example
  164. X! 
  165. X! The portion selected is always large enough to be considered a complete
  166. X! formula all by itself, so selecting the parenthesis selects the whole
  167. X! formula that it encloses.  Putting the cursor on the the @samp{+} sign
  168. X! would have had the same effect.
  169. X! 
  170. X! (Strictly speaking, the Emacs cursor is really the manifestation of
  171. X! the Emacs ``point,'' which is a position @emph{between} two characters
  172. X! in the buffer.  So purists would say that Calc selects the smallest
  173. X! sub-formula which contains the character to the right of ``point.'')
  174. X! 
  175. X! If you supply a numeric prefix argument @var{n}, the selection is
  176. X! expanded to the @var{n}th enclosing sub-formula.  Thus, positioning
  177. X! the cursor on the @samp{b} and typing @kbd{C-u 1 g s} will select
  178. X! @samp{a + b}; typing @kbd{C-u 2 g s} will select @samp{(a + b)^3},
  179. X! and so on.
  180. X! 
  181. X! If the cursor is not on any part of the formula, or if you give a
  182. X! numeric prefix that is too large, the entire formula is selected.
  183. X! 
  184. X! If the cursor is on the @samp{.} line that marks the top of the stack
  185. X! (i.e., its normal ``rest position''), this command selects the entire
  186. X! formula at stack level 1.  Most selection commands similarly operate
  187. X! on the formula at the top of the stack if you haven't positioned the
  188. X! cursor on any stack entry.
  189. X! 
  190. X! @kindex j a
  191. X! @pindex calc-select-additional
  192. X! The @kbd{j a} (@code{calc-select-additional}) command enlarges the
  193. X! current selection to encompass the cursor.  To select the smallest
  194. X! sub-formula defined by two different points, move to the first and
  195. X! press @kbd{j s}, then move to the other and press @kbd{j a}.  This
  196. X! is roughly analogous to using @code{set-mark-command} to select the
  197. X! two ends of a region of text during normal Emacs editing.
  198. X! 
  199. X! @kindex j o
  200. X! @pindex calc-select-once
  201. X! The @kbd{j o} (@code{calc-select-once}) command selects a formula in
  202. X! exactly the same way as @kbd{j s}, except that the selection will
  203. X! last only as long as the next command that uses it.  For example,
  204. X! @kbd{j o 1 +} is a handy way to add one to the sub-formula indicated
  205. X! by the cursor.
  206. X! 
  207. X! (A somewhat more precise definition: The @kbd{j o} command sets a flag
  208. X! such that the next command involving selected stack entries will clear
  209. X! the selections on those stack entries afterwards.  All other selection
  210. X! commands except @kbd{j a} clear this flag.)
  211. X! 
  212. X! @kindex j S
  213. X! @kindex j O
  214. X! @pindex calc-select-here-maybe
  215. X! @pindex calc-select-once-maybe
  216. X! The @kbd{j S} (@code{calc-select-here-maybe}) and @kbd{j O}
  217. X! (@code{calc-select-once-maybe}) commands are equivalent to @kbd{j s}
  218. X! and @kbd{j o}, respectively, except that if the formula already
  219. X! has a selection they have no effect.  This is analogous to the
  220. X! behavior of some commands such as @kbd{j r} (@code{calc-rewrite-selection};
  221. X! @pxref{Rewrite Rules}) and is mainly intended to be used in keyboard
  222. X! macros that implement your own selection-oriented commands.@refill
  223. X! 
  224. X! Selection of sub-formulas normally treats associative terms like
  225. X! @samp{a + b - c + d} and @samp{x * y * z} as single levels of the formula.
  226. X! If you place the cursor anywhere inside @samp{a + b - c + d} except
  227. X! on one of the variable names and use @kbd{j s}, you will select the
  228. X! entire four-term sum.
  229. X! 
  230. X! @kindex j b
  231. X! @pindex calc-break-selections
  232. X! The @kbd{j b} (@code{calc-break-selections}) command controls a mode
  233. X! in which the ``deep structure'' of these associative formulas shows
  234. X! through.  Calc actually stores the above formulas as @samp{((a + b) - c) + d}
  235. X! and @samp{x * (y * z)} (note that for certain obscure reasons, Calc
  236. X! treats multiplication as right-associative).  Once you have enabled
  237. X! @kbd{j b} mode, selecting with the cursor on the @samp{-} sign would
  238. X! only select the @samp{a + b - c} portion, which makes sense when the
  239. X! deep structure of the sum is considered.  There is no way to select
  240. X! the @samp{b - c + d} portion; although this might initially look
  241. X! like just as legitimate a sub-formula as @samp{a + b - c}, the deep
  242. X! structure shows that it isn't.  The @kbd{d U} command can be used
  243. X! to view the deep structure of any formula (@pxref{Normal Language Modes}).
  244. X! 
  245. X! When @kbd{j b} mode has not been enabled, the deep structure is
  246. X! generally hidden by the selection commands---what you see is what
  247. X! you get.
  248. X! 
  249. X! @kindex j u
  250. X! @pindex calc-unselect
  251. X! The @kbd{j u} (@code{calc-unselect}) command unselects the formula
  252. X! that the cursor is on.  If there was no selection in the formula,
  253. X! this command has no effect.  With a numeric prefix argument, it
  254. X! unselects that stack element rather than using the cursor position.
  255. X! 
  256. X! @kindex j c
  257. X! @pindex calc-clear-selections
  258. X! The @kbd{j c} (@code{calc-clear-selections}) command unselects all
  259. X! stack elements.
  260. X! 
  261. X! @node Changing Selections, Displaying Selections, Making Selections, Selecting Subformulas
  262. X! @subsection Changing Selections
  263. X! 
  264. X! @kindex j m
  265. X! @pindex calc-select-more
  266. X! Once you have selected a sub-formula, you can expand it using the
  267. X! @kbd{j m} (@code{calc-select-more}) command.  If @samp{a + b} is
  268. X! selected, pressing @kbd{j m} repeatedly works as follows:
  269. X! 
  270. X! @example
  271. X!            3                        3                        3
  272. X!     (a + b)  . .......       (a + b)  + sqrt(c)       (a + b)  + sqrt(c)
  273. X! 1*  ..................   1*  ..................   1*  ------------------
  274. X!          . . . .                  . . . .                   2 x + 1
  275. X! @end example
  276. X! 
  277. X! In the last example, the entire formula is selected.  This is roughly
  278. X! the same as having no selection at all, but because there are subtle
  279. X! differences the @samp{*} character is still there on the line number.
  280. X! 
  281. X! With a numeric prefix argument @var{n}, @kbd{j m} expands @var{n}
  282. X! times (or until the entire formula is selected).  Note that @kbd{j s}
  283. X! with argument @var{n} is equivalent to plain @kbd{j s} followed by
  284. X! @kbd{j m} with argument @var{n}.  If @kbd{j m} is used when there
  285. X! is no current selection, it is equivalent to @kbd{j s}.
  286. X! 
  287. X! Even though @kbd{j m} does not explicitly use the location of the
  288. X! cursor within the formula, it nevertheless uses the cursor to determine
  289. X! which stack element to operate on.  As usual, @kbd{j m} when the cursor
  290. X! is not on any stack element operates on the top stack element.
  291. X! 
  292. X! @kindex j l
  293. X! @pindex calc-select-less
  294. X! The @kbd{j l} (@code{calc-select-less}) command reduces the current
  295. X! selection around the cursor position.  That is, it selects the
  296. X! immediate sub-formula of the current selection which contains the
  297. X! cursor, the opposite of @kbd{j m}.  If the cursor is not inside the
  298. X! current selection, the command de-selects the formula.
  299. X! 
  300. X! @kindex j 1-9
  301. X! @pindex calc-select-part
  302. X! The @kbd{j 1} through @kbd{j 9} (@code{calc-select-part}) commands
  303. X! select the @var{n}th sub-formula of the current selection.  They are
  304. X! like @kbd{j l} (@code{calc-select-less}) except they use counting
  305. X! rather than the cursor position to decide which sub-formula to select.
  306. X! For example, if the current selection is @kbd{a + b + c} or
  307. X! @kbd{f(a, b, c)} or @kbd{[a, b, c]}, then @kbd{j 1} selects @samp{a},
  308. X! @kbd{j 2} selects @samp{b}, and @kbd{j 3} selects @samp{c}; in each of
  309. X! these cases, @kbd{j 4} through @kbd{j 9} would be errors.
  310. X! 
  311. X! If there is no current selection, @kbd{j 1} through @kbd{j 9} select
  312. X! the @var{n}th top-level sub-formula.  (In other words, they act as if
  313. X! the entire stack entry were selected first.)  To select the @var{n}th
  314. X! sub-formula where @var{n} is greater than nine, you must instead invoke
  315. X! @kbd{j 1} with @var{n} as a numeric prefix argument.@refill
  316. X! 
  317. X! @kindex j n
  318. X! @kindex j p
  319. X! @pindex calc-select-next
  320. X! @pindex calc-select-previous
  321. X! The @kbd{j n} (@code{calc-select-next}) and @kbd{j p}
  322. X! (@code{calc-select-previous}) commands change the current selection
  323. X! to the next or previous sub-formula at the same level.  For example,
  324. X! if @samp{b} is selected in @samp{2 + a*b*c + x}, then @kbd{j n}
  325. X! selects @samp{c}.  Further @kbd{j n} commands would be in error because,
  326. X! even though there is something to the right of @samp{c} (namely, @samp{x}),
  327. X! it is not at the same level; in this case, it is not a term of the
  328. X! same product as @samp{b} and @samp{c}.  However, @kbd{j m} (to select
  329. X! the whole product @samp{a*b*c} as a term of the sum) followed by
  330. X! @kbd{j n} would successfully select the @samp{x}.
  331. X! 
  332. X! Similarly, @kbd{j p} moves the selection from the @samp{b} in this
  333. X! sample formula to the @samp{a}.  Both commands accept numeric prefix
  334. X! arguments to move several steps at a time.
  335. X! 
  336. X! It is interesting to compare Calc's selection commands with the
  337. X! Emacs Info system's commands for navigating through hierarchically
  338. X! organized documentation.  Calc's @kbd{j n} command is completely
  339. X! analogous to Info's @kbd{n} command.  Likewise, @kbd{j p} maps to
  340. X! @kbd{p}, @kbd{j 2} maps to @kbd{2}, and Info's @kbd{u} is like @kbd{j m}.
  341. X! (Note that @kbd{j u} stands for @code{calc-unselect}, not ``up''.)
  342. X! The Info @kbd{m} command is somewhat similar to Calc's @kbd{j s} and
  343. X! @kbd{j l}; in each case, you can jump directly to a sub-component
  344. X! of the hierarchy simply by pointing to it with the cursor.
  345. X! 
  346. X! @node Displaying Selections, Operating on Selections, Changing Selections, Selecting Subformulas
  347. X! @subsection Displaying Selections
  348. X! 
  349. X! @kindex j d
  350. X! @pindex calc-show-selections
  351. X! The @kbd{j d} (@code{calc-show-selections}) command controls how
  352. X! selected sub-formulas are displayed.  One of the alternatives is
  353. X! illustrated in the above examples; if we press @kbd{j d} we switch
  354. X! to the other style in which the selected portion itself is obscured
  355. X! by @samp{#} signs:
  356. X! 
  357. X! @example
  358. X!            3                          #               
  359. X!     (a + b)  . .......         ## # ##  + sqrt(c)     
  360. X! 1:  ..................     1:  ------------------     
  361. X!          . . . .                    2 x + 1           
  362. X! @end example
  363. X! 
  364. X! @node Operating on Selections, Rearranging with Selections, Displaying Selections, Selecting Subformulas
  365. X! @subsection Operating on Selections
  366. X! 
  367. X! Once a selection is made, all Calc commands that manipulate items
  368. X! on the stack will operate on the selected portions of the items
  369. X! instead.  (Note that several stack elements may have selections
  370. X! at once, though there can be only one selection at a time in any
  371. X! given stack element.)
  372. X! 
  373. X! @kindex j e
  374. X! @pindex calc-enable-selections
  375. X! The @kbd{j e} (@code{calc-enable-selections}) command disables the
  376. X! effect that selections have on Calc commands.  The current selections
  377. X! still exist, but Calc commands operate on whole stack elements anyway.
  378. X! This mode can be identified by the fact that the @samp{*} markers on
  379. X! the line numbers are gone, even though selections are visible.  To
  380. X! reactivate the selections, press @kbd{j e} again.
  381. X! 
  382. X! To extract a sub-formula as a new formula, simply press @key{RET}.
  383. X! This normally duplicates the top stack element; here it duplicates
  384. X! only the selected portion of that element.
  385. X! 
  386. X! To replace a sub-formula with something different, you can enter the
  387. X! new value onto the stack and press @key{TAB}.  This normally exchanges
  388. X! the top two stack elements; here it swaps the value you entered into
  389. X! the selected portion of the formula, returning the old selected
  390. X! portion to the top of the stack.
  391. X! 
  392. X! @example
  393. X!            3
  394. X!     (a + b)  . .......        17 x y . .......        17 x y + sqrt(c)
  395. X! 2*  ..................    2*  ................    2:  ----------------
  396. X!          . . . .                    . . . .               2 x + 1
  397. X! 
  398. X!                                      3                       3
  399. X! 1:  17 x y                1:  (a + b)             1:  (a + b)
  400. X! @end example
  401. X! 
  402. X! In this example we select a sub-formula of our original example,
  403. X! enter a new formula, @key{TAB} it into place, then deselect to see
  404. X! the complete, edited formula.
  405. X! 
  406. X! If you want to swap whole formulas around even though they contain
  407. X! selections, just use @kbd{j e} before and after.
  408. X! 
  409. X! @kindex j '
  410. X! @pindex calc-enter-selection
  411. X! The @kbd{j '} (@code{calc-enter-selection}) command is another way
  412. X! to replace a selected sub-formula.  This command does an algebraic
  413. X! entry just like the regular @kbd{'} key.  When you press @key{RET},
  414. X! the formula you type replaces the original selection.  You can use
  415. X! the @samp{$} symbol in the formula to refer to the original
  416. X! selection.  If there is no selection in the formula under the cursor,
  417. X! the cursor is used to make a temporary selection for the purposes of
  418. X! the command.  Thus, to change a term of a formula, all you have to
  419. X! do is move the Emacs cursor to that term and press @kbd{j '}.
  420. X! 
  421. X! @kindex j `
  422. X! @pindex calc-edit-selection
  423. X! The @kbd{j `} (@code{calc-edit-selection}) command is a similar
  424. X! analogue of the @kbd{`} (@code{calc-edit}) command.  It edits the
  425. X! selected sub-formula in a separate buffer.  If there is no
  426. X! selection, it edits the sub-formula indicated by the cursor.
  427. X! 
  428. X! To delete a sub-formula, press @key{DEL}.  This generally replaces
  429. X! the sub-formula with the constant zero, but in a few suitable contexts
  430. X! it uses the constant one instead.  The @key{DEL} key automatically
  431. X! deselects and re-simplifies the entire formula afterwards.  Thus:
  432. X! 
  433. X! @example
  434. X!     17 x y - #######        17 x y         17 # y          17 y
  435. X! 1*  ----------------    1:  -------    1*  -------    1:  -------
  436. X!         2 x + 1             2 x + 1        2 x + 1        2 x + 1
  437. X! @end example
  438. X! 
  439. X! In this example, we first delete the @samp{sqrt(c)} term; Calc
  440. X! accomplishes this by replacing @samp{sqrt(c)} with zero and
  441. X! resimplifying.  We then delete the @kbd{x} in the numerator;
  442. X! since this is part of a product, Calc replaces it with @samp{1}
  443. X! and resimplifies.
  444. X! 
  445. X! If you select an element of a vector and press @key{DEL}, that
  446. X! element is deleted from the vector.
  447. X! 
  448. X! @kindex j DEL
  449. X! @pindex calc-del-selection
  450. X! The @kbd{j @key{DEL}} (@code{calc-del-selection}) command is like
  451. X! @key{DEL} but with the auto-selecting behavior of @kbd{j '} and
  452. X! @kbd{j `}.  It deletes the selected portion of the formula
  453. X! indicated by the cursor, or, in the absence of a selection, it
  454. X! deletes the sub-formula indicated by the cursor position.
  455. X! 
  456. X! @kindex j RET
  457. X! @pindex calc-grab-selection
  458. X! (There is also an auto-selecting @kbd{j @key{RET}} (@code{calc-copy-selection})
  459. X! command.)
  460. X! 
  461. X! Normal arithmetic operations also apply to sub-formulas.  Here we
  462. X! select the denominator, press @kbd{5 -} to subtract five from the
  463. X! denominator, press @kbd{n} to negate the denominator, then
  464. X! press @kbd{Q} to take the square root.
  465. X! 
  466. X! @example
  467. X!      .. .           .. .           .. .              .. .     
  468. X! 1*  .......    1*  .......    1*  .......    1*  .............
  469. X!     2 x + 1        2 x - 4        4 - 2 x        sqrt(4 - 2 x)
  470. X! @end example
  471. X! 
  472. X! Certain types of operations on selections are not allowed.  For
  473. X! example, for an arithmetic function like @kbd{-} no more than one of
  474. X! the arguments may be a selected sub-formula.  (As the above example
  475. X! shows, the result of the subtraction is spliced back into the argument
  476. X! which had the selection; if there were more than one selection involved,
  477. X! this would not be well-defined.)  If you try to subtract two selections,
  478. X! the command will abort with an error message.
  479. X! 
  480. X! Operations on sub-formulas sometimes leave the formula as a whole
  481. X! in an ``un-natural'' state.  Consider negating the @samp{2 x} term
  482. X! of our sample formula by selecting it and pressing @kbd{n}
  483. X! (@code{calc-change-sign}).@refill
  484. X! 
  485. X! @example
  486. X!         .. .                   .. .
  487. X! 1*  .............     1*  ..............
  488. X!     ...... . 2 x.         ...... . -2 x.
  489. X! @end example
  490. X! 
  491. X! Unselecting the sub-formula reveals that the minus sign, which would
  492. X! normally have cancelled out with the subtraction automatically, has
  493. X! not been able to do so because the subtraction was not part of the
  494. X! selected portion.  Pressing @kbd{=} (@code{calc-evaluate}) or doing
  495. X! any other mathematical operation on the whole formula will cause it
  496. X! to be simplified.
  497. X! 
  498. X! @example
  499. X!          17 y                  17 y
  500. X! 1:  --------------     1:  -------------
  501. X!     sqrt(4 - -2 x)         sqrt(4 + 2 x)
  502. X! @end example
  503. X! 
  504. X! @node Rearranging with Selections, , Operating on Selections, Selecting Subformulas
  505. X! @subsection Rearranging Formulas using Selections
  506. X! 
  507. X! @kindex j R
  508. X! @pindex calc-commute-right
  509. X! The @kbd{j R} (@code{calc-commute-right}) command moves the selected
  510. X! sub-formula to the right in its surrounding formula.  Generally the
  511. X! selection is one term of a sum or product; the sum or product is
  512. X! rearranged according to the commutative laws of algebra.
  513. X! 
  514. X! As with @kbd{j '} and @kbd{j DEL}, the term under the cursor is used
  515. X! if there is no selection in the current formula.  All commands described
  516. X! in this section share this property.  In this example, we place the
  517. X! cursor on the @samp{a} and type @kbd{j R}, then repeat.
  518. X! 
  519. X! @example
  520. X! 1:  a + b - c          1:  b + a - c          1:  b - c + a
  521. X! @end example
  522. X! 
  523. X! Note that in the final step above, the @samp{a} is switched with
  524. X! the @samp{c} but the signs are adjusted accordingly.  When moving
  525. X! terms of sums and products, @kbd{j R} will never change the
  526. X! mathematical meaning of the formula.
  527. X! 
  528. X! The selected term may also be an element of a vector or an argument
  529. X! of a function.  The term is exchanged with the one to its right.
  530. X! In this case, the ``meaning'' of the vector or function may of
  531. X! course be drastically changed.
  532. X! 
  533. X! @example
  534. X! 1:  [a, b, c]          1:  [b, a, c]          1:  [b, c, a]
  535. X! 
  536. X! 1:  f(a, b, c)         1:  f(b, a, c)         1:  f(b, c, a)
  537. X! @end example
  538. X! 
  539. X! @kindex j L
  540. X! @pindex calc-commute-left
  541. X! The @kbd{j L} (@code{calc-commute-left}) command is like @kbd{j R}
  542. X! except that it swaps the selected term with the one to its left.
  543. X! 
  544. X! With numeric prefix arguments, these commands move the selected
  545. X! term several steps at a time.  It is an error to try to move a
  546. X! term left or right past the end of its enclosing formula.
  547. X! 
  548. X! With numeric prefix arguments of zero, these commands move the
  549. X! selected term as far as possible in the given direction, with
  550. X! no possibility of signalling an error.
  551. X! 
  552. X! @kindex j D
  553. X! @pindex calc-sel-distribute
  554. X! The @kbd{j D} (@code{calc-sel-distribute}) command mixes the selected
  555. X! sum or product into the surrounding formula using the distributive
  556. X! law.  For example, in @samp{a * (b - c)} with the @samp{b - c}
  557. X! selected, the result is @samp{a b - a c}.  This also distributes
  558. X! products or quotients into surrounding powers, and can also do
  559. X! transformations like @samp{exp(a + b)} to @samp{exp(a) exp(b)},
  560. X! where @samp{a + b} is the selected term, and @samp{ln(a ^ b)}
  561. X! to @samp{ln(a) b}, where @samp{a ^ b} is the selected term.
  562. X! 
  563. X! For multiple-term sums or products, @kbd{j D} takes off one term
  564. X! at a time:  @samp{a * (b + c - d)} goes to @samp{a * (c - d) + a b}
  565. X! with the @samp{c - d} selected so that you can type @kbd{j D}
  566. X! repeatedly to expand completely.  The @kbd{j D} command allows a
  567. X! numeric prefix argument which specifies the maximum number of
  568. X! times to expand at once; the default is one time only.
  569. X! 
  570. X! The @kbd{j D} command is implemented using rewrite rules.
  571. X! @xref{Selections with Rewrite Rules}.  The rules are stored in
  572. X! the Calc variable @code{DistribRules}.  A convenient way to view
  573. X! these rules is to put the name @code{DistribRules} on the stack,
  574. X! then type @kbd{`} (@code{calc-edit}).  Press @key{RET} to return
  575. X! from editing mode; be careful not to make any actual changes or
  576. X! else you will affect the behavior of future @kbd{j D} commands!
  577. X! 
  578. X! To extend @kbd{j D} to handle new cases, just edit @code{DistribRules}
  579. X! as described above.  You can then use the @kbd{Z V} command to save
  580. X! this variable's value permanently for future Calc sessions.
  581. X! 
  582. X! @kindex j M
  583. X! @pindex calc-sel-merge
  584. X! The @kbd{j M} (@code{calc-sel-merge}) command is the complement
  585. X! of @kbd{j D}; given @samp{a b - a c} with either @samp{a b} or
  586. X! @samp{a c} selected, the result is @samp{a * (b - c)}.  Once
  587. X! again, @kbd{j M} can also merge calls to functions like @code{exp}
  588. X! and @code{ln}; examine the variable @code{MergeRules} to see all
  589. X! the relevant rules.
  590. X! 
  591. X! @kindex j C
  592. X! @pindex calc-sel-commute
  593. X! The @kbd{j C} (@code{calc-sel-commute}) command swaps the arguments
  594. X! of the selected sum, product, or equation.  It always behaves as
  595. X! if @kbd{j b} mode were in effect, i.e., the sum @samp{a + b + c} is
  596. X! treated as the nested sums @samp{(a + b) + c} by this command.
  597. X! If you put the cursor on the first @samp{+}, the result is
  598. X! @samp{(b + a) + c}; if you put the cursor on the second @samp{+}, the
  599. X! result is @samp{c + (a + b)} (which the default simplifications
  600. X! will rearrange to @samp{(c + a) + b}).  The relevant rules are stored
  601. X! in the variable @code{CommuteRules}.
  602. X! 
  603. X! You may need to turn default simplifications off (with the @kbd{m O}
  604. X! command) in order to get the full benefit of @kbd{j C}.  For example,
  605. X! commuting @samp{a - b} produces @samp{-b + a}, but the default
  606. X! simplifications will ``simplify'' this right back to @samp{a - b} if
  607. X! you don't turn them off.  The same is true of some of the other
  608. X! manipulations described in this section.
  609. X! 
  610. X! @kindex j N
  611. X! @pindex calc-sel-negate
  612. X! The @kbd{j N} (@code{calc-sel-negate}) command replaces the selected
  613. X! term with the negative of that term, then adjusts the surrounding
  614. X! formula in order to preserve the meaning.  For example, given
  615. X! @samp{exp(a - b)} where @samp{a - b} is selected, the result is
  616. X! @samp{1 / exp(b - a)}.  By contrast, selecting a term and using the
  617. X! regular @kbd{n} (@code{calc-change-sign}) command negates the
  618. X! term without adjusting the surroundings, thus changing the meaning
  619. X! of the formula as a whole.  The rules variable is @code{NegateRules}.
  620. X! 
  621. X! @kindex j &
  622. X! @pindex calc-sel-invert
  623. X! The @kbd{j &} (@code{calc-sel-invert}) command is similar to @kbd{j N}
  624. X! except it takes the reciprocal of the selected term.  For example,
  625. X! given @samp{a - ln(b)} with @samp{b} selected, the result is
  626. X! @samp{a + ln(1/b)}.  The rules variable is @code{InvertRules}.
  627. X! 
  628. X! @kindex j E
  629. X! @pindex calc-sel-jump-equals
  630. X! The @kbd{j E} (@code{calc-sel-jump-equals}) command moves the
  631. X! selected term from one side of an equation to the other.  Given
  632. X! @samp{a + b = c + d} with @samp{c} selected, the result is
  633. X! @samp{a + b - c = d}.  This command also works if the selected
  634. X! term is part of a @samp{*}, @samp{/}, or @samp{^} formula.  If the
  635. X! term you wish to move is neither the first nor the last term
  636. X! of its sum or product, you may need to use @kbd{j L} or @kbd{j R}
  637. X! first to move it to one end.  The relevant rules variable is
  638. X! @code{JumpRules}.
  639. X! 
  640. X! @kindex j I
  641. X! @pindex calc-sel-isolate
  642. X! The @kbd{j I} (@code{calc-sel-isolate}) command isolates the
  643. X! selected term on its side of an equation.  It uses the @kbd{a S}
  644. X! (@code{calc-solve-for}) command to solve the equation, and the
  645. X! Hyperbolic flag affects it in the same way.  @xref{Calculus}.
  646. X! When it applies, @kbd{j I} is often easier to use than @kbd{j E}.
  647. X! It understands more rules of algebra, and works for inequalities
  648. X! as well as equations.
  649. X! 
  650. X! @kindex j *
  651. X! @kindex j /
  652. X! @pindex calc-sel-mult-both-sides
  653. X! @pindex calc-sel-div-both-sides
  654. X! The @kbd{j *} (@code{calc-sel-mult-both-sides}) command prompts for a
  655. X! formula using algebraic entry, then multiplies both sides of the
  656. X! selected quotient or equation by that formula.  It simplifies each
  657. X! side with @kbd{a s} (@code{calc-simplify}) before re-forming the
  658. X! quotient or equation.  You can suppress this simplification by
  659. X! providing any numeric prefix argument.  There is also a @kbd{j /}
  660. X! (@code{calc-sel-div-both-sides}) which is similar to @kbd{j *} but
  661. X! dividing instead of multiplying by the factor you enter.
  662. X! 
  663. X! If the selection is an inequality, @kbd{j *} and @kbd{j /} work
  664. X! only if the formula you enter evaluates to a nonzero real number.
  665. X! (The sense of the inequality is reversed if the number is negative.)
  666. X! For selections that are not quotients, equations, or inequalities,
  667. X! these commands pull out a multiplicative factor:  They divide (or
  668. X! multiply) by the entered formula, simplify, then multiply (or divide)
  669. X! back by the formula.
  670. X! 
  671. X! @kindex j +
  672. X! @kindex j -
  673. X! @pindex calc-sel-add-both-sides
  674. X! @pindex calc-sel-sub-both-sides
  675. X! The @kbd{j +} (@code{calc-sel-add-both-sides}) and @kbd{j -}
  676. X! (@code{calc-sel-sub-both-sides}) commands analogously add to or
  677. X! subtract from both sides of an equation or inequality.  For other
  678. X! types of selections, they extract an additive factor.  A numeric
  679. X! prefix argument suppresses simplification of the intermediate
  680. X! results.
  681. X! 
  682. X! @kindex j U
  683. X! @pindex calc-sel-unpack
  684. X! The @kbd{j U} (@code{calc-sel-unpack}) command replaces the
  685. X! selected function call with its argument.  For example, given
  686. X! @samp{a + sin(x^2)} with @samp{sin(x^2)} selected, the result
  687. X! is @samp{a + x^2}.  (The @samp{x^2} will remain selected; if you
  688. X! wanted to change the @code{sin} to @code{cos}, just press @kbd{C}
  689. X! now to take the cosine of the selected part.)
  690. X! 
  691. X! @kindex j v
  692. X! @pindex calc-sel-evaluate
  693. X! The @kbd{j v} (@code{calc-sel-evaluate}) command performs the
  694. X! normal default simplifications on the selected sub-formula.
  695. X! These are the simplifications that are normally done automatically
  696. X! on all results, but which may have been partially inhibited by
  697. X! previous selection-related operations, or turned off altogether
  698. X! by the @kbd{m O} command.
  699. X! 
  700. X! With a numeric prefix argument of 2, @kbd{C-u 2 g v} applies
  701. X! the @kbd{a s} (@code{calc-simplify}) command to the selected
  702. X! sub-formula.  With a prefix argument of 3 or more, e.g., @kbd{C-u g v}
  703. X! applies the @kbd{a e} (@code{calc-simplify-extended}) command.
  704. X! @xref{Algebraic Manipulation}.
  705. X! 
  706. X! You can use the @kbd{j r} (@code{calc-rewrite-selection}) command
  707. X! to define other algebraic operations on sub-formulas.  @xref{Rewrite Rules}.
  708. X! 
  709. X! @node Algebraic Manipulation, Calculus, Selecting Subformulas, Algebra
  710. X  @section Algebraic Manipulation
  711. X  
  712. X+ The commands in this section perform general-purpose algebraic
  713. X+ manipulations.  They work on the whole formula at the top of the
  714. X+ stack (unless, of course, you have made a selection in that
  715. X+ formula).
  716. X+ 
  717. X+ Many of these commands prompt for a variable name or formula.  If you
  718. X+ answer the prompt with a blank line, the variable or formula is taken
  719. X+ from top-of-stack, and the normal argument for the command is taken
  720. X+ from the second-to-top stack level.
  721. X+ 
  722. X+ @kindex a v
  723. X+ @pindex calc-alg-evaluate
  724. X+ The @kbd{a v} (@code{calc-alg-evaluate}) command performs the normal
  725. X+ default simplifications on a formula; for example, @samp{a - -b} is
  726. X+ changed to @samp{a + b}.  These simplifications are normally done
  727. X+ automatically on all Calc results, so this command is useful only if
  728. X+ you have turned default simplifications off with an @kbd{m O}
  729. X+ command.  @xref{Simplification Modes}.
  730. X+ 
  731. X  @kindex a s
  732. X  @pindex calc-simplify
  733. X! The @kbd{a s} (@code{calc-simplify}) [@code{simplify}] command applies
  734. X! various algebraic rules to simplify a formula.  This includes rules which
  735. X! are not part of the default simplifications because they may be too slow
  736. X! to apply all the time, or may not be desirable all of the time.  For
  737. X! example, non-adjacent terms of sums are combined, as in @samp{a + b + 2 a}
  738. X! to @samp{b + 3 a}, and some formulas like @samp{sin(arcsin(x))} are
  739. X! simplified to @samp{x}.
  740. X  
  741. X  @kindex a e
  742. X  @pindex calc-simplify-extended
  743. X! The @kbd{a e} (@code{calc-simplify-extended}) [@code{esimplify}] command
  744. X! is like @kbd{a s}
  745. X  except that it applies some additional simplifications which are not
  746. X  ``safe'' in all cases.  For example, powers are simplified according
  747. X  to the rule @samp{(a^b)^c = a^(b c)}, even though this is not always
  748. X! true (consider @samp{sqrt(a^2)} for negative @samp{a}).  As another
  749. X! example, equations of the form @samp{a x = b x} are simplified to
  750. X! @samp{a = b} even though this is not valid if @samp{x} may be zero.
  751. X! The symbolic
  752. X  integrator uses @code{calc-simplify-extended}; one effect of this is
  753. X  that the integrator's results must be used with caution.  Where an
  754. X  integral table will often attach conditions like ``for positive @samp{a}
  755. X! only,'' Calc (like most other symbolic integration programs) will simply
  756. X! produce an unqualified result.@refill
  757. X  
  758. X  @kindex a x
  759. X  @pindex calc-expand
  760. X***************
  761. X*** 7195,7201 ****
  762. X  The @kbd{a x} (@code{calc-expand}) command expands an expression by
  763. X  applying the distributive law.  It applies to products, quotients, and
  764. X  powers involving sums.  By default, it fully distributes all parts of
  765. X! the expression.  With the a numeric prefix argument, the distributive
  766. X  law is applied only the specified number of times, then the partially
  767. X  expanded expression is left on the stack.
  768. X  
  769. X--- 11490,11496 ----
  770. X  The @kbd{a x} (@code{calc-expand}) command expands an expression by
  771. X  applying the distributive law.  It applies to products, quotients, and
  772. X  powers involving sums.  By default, it fully distributes all parts of
  773. X! the expression.  With a numeric prefix argument, the distributive
  774. X  law is applied only the specified number of times, then the partially
  775. X  expanded expression is left on the stack.
  776. X  
  777. X***************
  778. X*** 7223,7229 ****
  779. X  @pindex calc-substitute
  780. X  The @kbd{a b} (@code{calc-substitute}) command substitutes occurrences
  781. X  of some variable or sub-expression of an expression with a new
  782. X! sub-expression.  For example, substituting @samp{sin(x)} to @samp{cos(y)}
  783. X  in @samp{2 sin(x)^2 + x sin(x) + sin(2 x)} produces
  784. X  @samp{2 cos(y)^2 + x cos(y) + sin(2 x)}.
  785. X  Note that this is a purely structural substitution; the lone @code{x} and the
  786. X--- 11518,11524 ----
  787. X  @pindex calc-substitute
  788. X  The @kbd{a b} (@code{calc-substitute}) command substitutes occurrences
  789. X  of some variable or sub-expression of an expression with a new
  790. X! sub-expression.  For example, substituting @samp{sin(x)} with @samp{cos(y)}
  791. X  in @samp{2 sin(x)^2 + x sin(x) + sin(2 x)} produces
  792. X  @samp{2 cos(y)^2 + x cos(y) + sin(2 x)}.
  793. X  Note that this is a purely structural substitution; the lone @code{x} and the
  794. X***************
  795. X*** 7245,7391 ****
  796. X  because the @samp{+} operator is left-associative, so the ``deep
  797. X  structure'' of that formula is @samp{(x+y) + z}.  Use @kbd{d U}
  798. X  (@code{calc-unformatted-language}) mode to see the true structure of
  799. X! a formula.
  800. X  
  801. X! @node Rewrite Rules, Logical Operations, Algebraic Manipulation, Algebra
  802. X! @section Rewrite Rules
  803. X  
  804. X! @cindex Rewrite rules
  805. X! @cindex Transformations
  806. X! @cindex Pattern matching
  807. X! @kindex a r
  808. X! @pindex calc-rewrite
  809. X! The @kbd{a r} (@code{calc-rewrite}) command makes substitutions in a
  810. X! formula according to a specified pattern or patterns.  It is like a
  811. X! generalized version of @code{calc-substitute}.  You provide Calc with
  812. X! one or more @dfn{rewrite rules}, which take the form of vectors of
  813. X! two or three elements:  @samp{[old, new]} or @samp{[old, new, cond]}.
  814. X! To specify a set of rules, make a vector of rule vectors.  (Calc will
  815. X! interpret this structure as a matrix as long as the rules either all
  816. X! include or all omit the @samp{cond} part.  Whether or not this is true
  817. X! makes no difference to @code{calc-rewrite} itself.)
  818. X  
  819. X! When you are prompted to enter the rewrite rules, you can either type
  820. X! the vector directly (as an algebraic entry), or type the name of a
  821. X! Calc variable into which you have stored a rules vector, or enter
  822. X! a blank line in which case the rules vector will be taken from the
  823. X! top of the stack.  It is most convenient to store rules you use often
  824. X! in a variable and invoke them by naming the variable.  You may also
  825. X! wish to use @kbd{Z V} (@code{calc-permanent-variable}) to save the
  826. X! rules permanently; @pxref{Storing Variables}.
  827. X  
  828. X! To match a particular formula @samp{x} with a particular rule vector
  829. X! @samp{[old, new]}, Calc compares the structure of @samp{x} with the
  830. X! structure of @samp{old}.  Variables that appear in @samp{old} are
  831. X! treated as ``meta-variables''; the corresponding positions in @samp{x}
  832. X! may contain any sub-formulas.  For example, the pattern @samp{f(x,y)}
  833. X! would match the expression @samp{f(12, a+1)} with the meta-variable
  834. X! @samp{x} corresponding to 12 and with @samp{y} corresponding to
  835. X! @samp{a+1}.  However, this pattern would not match @samp{f(12)} or
  836. X! @samp{g(12, a+1)}, since there is no assignment of the meta-variables
  837. X! that will allow the pattern to match these expressions.  Notice that
  838. X! if the pattern is a single variable, it will match any expression.
  839. X  
  840. X! If a given meta-variable appears more than once in @var{old}, the
  841. X! corresponding sub-formulas of @samp{x} must be identical.  Thus
  842. X! the pattern @samp{f(x,x)} would match @samp{f(12, 12)} and
  843. X! @samp{f(a+1, a+1)} but not @samp{f(12, a+1)} or @samp{f(12, 12.0)}.
  844. X  
  845. X! Things other than variables must match exactly between the pattern
  846. X! and the target formula.  To match a particular variable, use the
  847. X! pseudo-function @samp{quote(v)} in the pattern.  For example, the
  848. X! pattern @samp{x+quote(y)} matches @samp{x+y}, @samp{2+y}, or
  849. X! @samp{sin(x)+y}.
  850. X  
  851. X! Pattern-matching is completely structural, not taking the algebraic
  852. X! properties of formulas into account.  Thus @samp{f(x+1)} matches
  853. X! the formula @samp{f(a+1)} but not @samp{f(a)}, @samp{f(a+2)}, or
  854. X! @samp{f(1+a)}.  For other examples @pxref{Algebraic Manipulation},
  855. X! under the discussion of @code{calc-substitute}.@refill
  856. X  
  857. X! If the @samp{old} pattern is found to match a given formula, that
  858. X! formula is replaced by @samp{new}, where any occurrences in @samp{new}
  859. X! of meta-variables from the pattern are replaced with the sub-formulas
  860. X! that they matched.  Thus, applying the rule @samp{[f(x,y), g(y+x,x)]}
  861. X! to @samp{f(12, a+1)} would produce @samp{g(a+13, 12)}.
  862. X  
  863. X! If a @samp{cond} part is also present in the rule, this is an
  864. X! additional condition that must be satisfied before the rule is
  865. X! accepted.  Once @samp{old} has been successfully matched to the
  866. X! target expression, @samp{cond} is evaluated (with the meta-variables
  867. X! substituted for the values they matched) and simplified with
  868. X! @code{calc-simplify}.  If the result is a nonzero number, the rule
  869. X! is accepted.  If the result is zero or if it is still a symbolic
  870. X! formula, the rule is rejected.  @xref{Logical Operations}, for a
  871. X! number of functions that return 1 or 0 according to the result of
  872. X! some test.  For example, the formula @samp{n > 0} simplifies to
  873. X! 1 or 0 if @samp{n} is a replaced by a positive or nonpositive
  874. X! number, respectively.  Thus, the rule @samp{[f(x,y), g(y+x,x), x+y > 0]}
  875. X! would be applied to @samp{f(0, 4)} but not to @samp{f(-3, 2)} or
  876. X! @samp{f(12, a+1)}.  (In the former case, the condition can be shown
  877. X! not to be satisfied; in the latter case, the condition merely cannot
  878. X! be shown to be satisfied, but that is enough to reject the rule.)
  879. X  
  880. X! When @code{calc-rewrite} is used, it takes an expression from the
  881. X! top of the stack and attempts to match any of the specified rules
  882. X! to any part of the expression, starting with the whole expression
  883. X! and then, if that fails, trying deeper and deeper sub-expressions.
  884. X! For each part of the expression, the rules are tried in the
  885. X! specified order.  The first rule to match the first sub-expression
  886. X! wins; it replaces the matched sub-expression according to the
  887. X! @samp{new} part of the rule.  By default, only one substitution
  888. X! is made per @code{calc-rewrite} command.
  889. X  
  890. X! Given a positive numeric prefix argument, @code{calc-rewrite} will
  891. X! perform up to that many substitutions before stopping.  First, the
  892. X! top-level formula is matched and substituted repeatedly until it
  893. X! no longer matches the pattern; then, sub-formulas are tried, and
  894. X! so on.  If the limit still has not been reached, the top-level
  895. X! formula is check again (in case a substitution of one of its
  896. X! arguments causes it again to match).  This continues until no
  897. X! further matches can be made, or until the limit is reached.
  898. X  
  899. X! Given a negative numeric prefix argument, @code{calc-rewrite} will
  900. X! match and substitute the top-level expression up to that many times,
  901. X! but will not attempt to match any sub-expressions.
  902. X  
  903. X! A prefix argument of zero is equivalent to a prefix of 25.  This is
  904. X! meant to be a convenient limit that is usually enough to finish the
  905. X! job, but not enough to be too slow in case the rule transformations
  906. X! get into an infinite loop.
  907. X  
  908. X! As a larger example, suppose we wish to substitute the pattern
  909. X! @samp{sin(x)^2 + cos(x)^2} with 1 in a given expression.  The rule
  910. X! @samp{[sin(x)^2 + cos(x)^2, 1]} would work, but only for the most
  911. X! literal matches.  This would miss the formula @samp{2 sin(a)^2 + 2
  912. X! cos(a)^2}, for example, because our pattern does not account for both
  913. X! terms being multiplied by an equal factor.  A more flexible solution
  914. X! would be to use the rule @samp{[cos(x)^2, 1 - sin(x)^2]}, followed by
  915. X! algebraic simplification if necessary.@refill
  916. X! 
  917. X! Similarly, the pattern @samp{[f(x, x+1), g(x)]} is not a very reliable
  918. X! rule since it fails to match in cases like @samp{f(2, 3)}.  A better
  919. X! rule would be @samp{[f(x,y), g(x), y=x+1]}, since the algebraic
  920. X! simplifier is applied to conditions but not to patterns.
  921. X  
  922. X! As another example, we could define our own factorial function with the
  923. X! rules @samp{[[f(0), 1], [f(n), n*f(n-1), n>0]]}.  Enter this rules
  924. X! vector and store it in a variable: @kbd{s factrules}.  Now, given a
  925. X! factorial formula like @samp{f(5)} on the stack, type @samp{C-u 0 a r
  926. X! factrules} to apply these rules repeatedly.  After six applications,
  927. X! @code{calc-rewrite} will stop with 120 on the stack.  If we expect to
  928. X! use these rules often, we could define the above sequence of keystrokes
  929. X! as a keyboard macro.  We could then assign this macro to a single
  930. X! key sequence which effectively means, ``simplify all calls to the function
  931. X! @samp{f} on top-of-stack.''  @xref{Keyboard Macros}.@refill
  932. X  
  933. X! @node Logical Operations, Calculus, Rewrite Rules, Algebra
  934. X  @section Logical Operations
  935. X  
  936. X  The following commands and algebraic functions return true/false values,
  937. X  where 1 represents ``true'' and 0 represents ``false.''  In cases where
  938. X  a truth value is required (such as for the condition part of a rewrite
  939. X! rule), any nonzero number is accepted to mean ``true.''
  940. X  
  941. X  @kindex a =
  942. X  @pindex calc-equal-to
  943. X--- 11540,11840 ----
  944. X  because the @samp{+} operator is left-associative, so the ``deep
  945. X  structure'' of that formula is @samp{(x+y) + z}.  Use @kbd{d U}
  946. X  (@code{calc-unformatted-language}) mode to see the true structure of
  947. X! a formula.  The rewrite rule mechanism, discussed next, does not have
  948. X! these limitations.
  949. X  
  950. X! @node Calculus, Numerical Solutions, Algebraic Manipulation, Algebra
  951. X! @section Calculus
  952. X  
  953. X! The following calculus commands do not automatically simplify their
  954. X! inputs or outputs using @code{calc-simplify}.  You may find it helps
  955. X! to do this by hand by typing @kbd{a s} or @kbd{a e}.  It may also help
  956. X! to use @kbd{a x} and/or @kbd{a c} to arrange a result in the most
  957. X! readable way.
  958. X  
  959. X! @kindex a d
  960. X! @kindex H a d
  961. X! @pindex calc-derivative
  962. X! @tindex deriv
  963. X! @tindex tderiv
  964. X! The @kbd{a d} (@code{calc-derivative}) [@code{deriv}] command computes
  965. X! the derivative of the expression on the top of the stack with respect to
  966. X! some variable, which it will prompt you to enter.  Normally, variables
  967. X! in the formula other than the specified differentiation variable are
  968. X! considered constant, i.e., @samp{deriv(y,x)} is reduced to zero.  With
  969. X! the Hyperbolic flag, the @code{tderiv} (total derivative) operation is used
  970. X! instead, in which derivatives of variables are not reduced to zero.
  971. X  
  972. X! With a numeric prefix argument @var{n}, this computes the @var{n}th
  973. X! derivative.
  974. X  
  975. X! If you use the @code{deriv} function directly in an algebraic formula,
  976. X! you can also write @samp{deriv(f,x,x0)} which represents the derivative
  977. X! of @code{f} with respect to @code{x}, evaluated at the point @samp{x=x0}.
  978. X  
  979. X! If the formula being differentiated contains functions which Calc does
  980. X! not know, the derivatives of those functions are produced by adding
  981. X! primes (apostrophe characters).  For example, @samp{deriv(f(2x), x)}
  982. X! produces @samp{2 f'(2 x)}, where the function @code{f'} represents the
  983. X! derivative of @code{f}.  If you enter a definition for @code{f'}, for
  984. X! example using the @kbd{Z F} (@code{calc-user-define-formula}) command,
  985. X! the Calculator will use it.  @xref{Algebraic Definitions}.
  986. X  
  987. X! For multi-argument functions @samp{f(x,y,z)}, the derivative with respect
  988. X! to the first argument is written @samp{f'(x,y,z)}; derivatives with
  989. X! respect to the other arguments are @samp{f'2(x,y,z)} and @samp{f'3(x,y,z)}.
  990. X! Various higher-order derivatives can be formed in the obvious way, e.g.,
  991. X! @samp{f'@var{}'(x)} (the second derivative of @code{f}) or
  992. X! @samp{f'@var{}'2'3(x,y,z)} (@code{f} differentiated with respect to each
  993. X! argument once).@refill
  994. X  
  995. X! @kindex a i
  996. X! @pindex calc-integral
  997. X! @tindex integ
  998. X! The @kbd{a i} (@code{calc-integral}) [@code{integ}] command computes the
  999. X! indefinite integral of the expression on the top of the stack with
  1000. X! respect to a variable.  The integrator is not guaranteed to work for
  1001. X! all integrable functions, but it will work for any polynomial and for
  1002. X! most simple functions.
  1003. X  
  1004. X! If you use the @code{integ} function directly in an algebraic formula,
  1005. X! you can also write @samp{integ(f,x,v)} which expresses the resulting
  1006. X! indefinite integral in terms of variable @code{v} instead of @code{x},
  1007. X! or @samp{integ(f,x,a,b)}, which represents a definite integral from
  1008. X! @code{a} to @code{b}.
  1009. X  
  1010. X! The Calculator remembers all the integrals it has done.  If conditions
  1011. X! change in a way that would invalidate the old integrals, they will be
  1012. X! thrown out.  If you suspect this is not happening when it should,
  1013. X! use the @code{calc-flush-caches} command; @pxref{Caches}.
  1014. X  
  1015. X! @kindex a I
  1016. X! @pindex calc-integral-limit
  1017. X! The @kbd{a I} (@code{calc-integral-limit}) command displays or, with a
  1018. X! numeric prefix argument, sets the current ``integration nesting limit.''
  1019. X! This is the depth to which Calc will pursue integration by substitution
  1020. X! or integration by parts before abandoning an approach as fruitless.
  1021. X! The default value is 3.  If the integrator is taking too long, you
  1022. X! might consider lowering this limit.  However, the lower this limit is,
  1023. X! the greater the chance that Calc will be unable to integrate a function
  1024. X! it could otherwise handle.  Raising this limit allows the Calculator to
  1025. X! solve more integrals, though the time it takes may grow exponentially.
  1026. X! You can monitor the integrator's actions by creating an Emacs buffer called
  1027. X! @code{*Trace*}.  If such a buffer exists, the @kbd{a i} command will write
  1028. X! a log of its actions there.
  1029. X  
  1030. X! @kindex a t
  1031. X! @pindex calc-taylor
  1032. X! @tindex taylor
  1033. X! The @kbd{a t} (@code{calc-taylor}) [@code{taylor}] command computes a
  1034. X! power series expansion or Taylor series of a function.  You specify the
  1035. X! variable and the desired number of terms.  You may give an expression of
  1036. X! the form @samp{@var{var} = @var{a}} or @samp{@var{var} - @var{a}} to
  1037. X! produce a Taylor expansion about the point @var{a}.  You may specify the
  1038. X! number of terms with a numeric prefix argument; otherwise the command
  1039. X! will prompt you for the number of terms.  Note that many series
  1040. X! expansions have coefficients of zero for some terms, so you may appear
  1041. X! to get fewer terms than you asked for.@refill
  1042. X  
  1043. X! If the @kbd{a i} command is unable to find a symbolic integral for a
  1044. X! function, you can get an approximation by integrating the function's
  1045. X! Taylor series.
  1046. X  
  1047. X! @kindex a S
  1048. X! @pindex calc-solve-for
  1049. X! @tindex solve
  1050. X! The @kbd{a S} (@code{calc-solve-for}) [@code{solve}] command rearranges
  1051. X! an equation to solve for a specific variable.  An equation is an
  1052. X! expression of the form @samp{L = R}.  For example, the command @kbd{a S x}
  1053. X! will rearrange @samp{y = 3x + 6} to the form, @samp{x = y/3 - 2}.  If the
  1054. X! input is not an equation, it is treated like an equation of the
  1055. X! form @samp{X = 0}.
  1056. X  
  1057. X! This command also works for inequalities, as in @samp{y < 3x + 6}.
  1058. X! Some inequalities cannot be solved where the analogous equation could
  1059. X! be; for example, solving @samp{a < b c} for @samp{b} is impossible
  1060. X! without knowing the sign of @samp{c}.  In this case, @kbd{a S} will
  1061. X! produce the result @samp{b != a/c} (using the not-equal-to operator)
  1062. X! to signify that the direction of the inequality was unknown.  The
  1063. X! inequality @samp{a <= b c} is not even partially solved.
  1064. X! 
  1065. X! @kindex H a S
  1066. X! @tindex fsolve
  1067. X! Some equations have more than one solution.  The Hyperbolic flag
  1068. X! (@code{H a S}) [@code{fsolve}] tells the solver to report the fully
  1069. X! general family of solutions.  It will invent variables @code{n1},
  1070. X! @code{n2}, @dots{}, which represent independent arbitrary integers, and
  1071. X! @code{s1}, @code{s2}, @dots{}, which represent independent arbitrary
  1072. X! signs (either @samp{+1} or @samp{-1}).  If you don't use the Hyperbolic
  1073. X! flag, Calc will use zero in place of all arbitrary integers, and plus
  1074. X! one in place of all arbitrary signs.  Note that variables like @code{n1}
  1075. X! and @code{s1} are not given any special interpretation in Calc except by
  1076. X! @code{calc-solve-for} itself.  As usual, you can use the @code{calc-let}
  1077. X! command to obtain solutions for various actual values of these
  1078. X! variables.
  1079. X! 
  1080. X! @kindex I a S
  1081. X! @kindex H I a S
  1082. X! @tindex finv
  1083. X! @tindex ffinv
  1084. X! With the Inverse flag, @kbd{I a S} [@code{finv}] treats the expression
  1085. X! on top of the stack as a function of the specified variable and solves
  1086. X! to find the inverse function, written in terms of the same variable.
  1087. X! For example, @kbd{I a S x} inverts @samp{2x + 6} to @samp{x/2 - 3}.
  1088. X! You can use both Inverse and Hyperbolic [@code{ffinv}] to obtain a
  1089. X! fully general inverse, as described above.
  1090. X! 
  1091. X! @node Numerical Solutions, Logical Operations, Calculus, Algebra
  1092. X! @section Numerical Solutions
  1093. X  
  1094. X! Not all equations can be solved symbolically.  The commands in this
  1095. X! section use numerical algorithms that can find a solution to a specific
  1096. X! instance of an equation to any desired accuracy.  Note that these
  1097. X! commands are slower than their algebraic cousins; it is a good idea
  1098. X! to try @kbd{a S} before resorting to @kbd{a R}.
  1099. X! 
  1100. X! @kindex a R
  1101. X! @pindex calc-find-root
  1102. X! @tindex root
  1103. X! @cindex Newton's method
  1104. X! @cindex Roots of equations
  1105. X! @cindex Numerical root-finding
  1106. X! The @kbd{a R} (@code{calc-find-root}) [@code{root}] command finds a
  1107. X! numerical solution (or @dfn{root}) of an equation.  (This command treats
  1108. X! inequalities the same as equations.  If the input is any other kind
  1109. X! of formula, it is interpreted as an equation of the form @samp{X = 0}.)
  1110. X! 
  1111. X! The @kbd{a R} command requires an initial guess on the top of the
  1112. X! stack, and a formula in the second-to-top position.  It prompts for a
  1113. X! solution variable, which must appear in the formula.  All other variables
  1114. X! that appear in the formula must have assigned values, i.e., when
  1115. X! a value is assigned to the solution variable and the formula is
  1116. X! evaluated with @kbd{=}, it should evaluate to a number.  Any assigned
  1117. X! value for the solution variable itself is ignored and unaffected by
  1118. X! this command.
  1119. X! 
  1120. X! When the command completes, the initial guess is replaced on the stack
  1121. X! by a vector of two numbers:  The value of the solution variable that
  1122. X! solves the equation, and the difference between the lefthand and
  1123. X! righthand sides of the equation at that value.  Ordinarily, the second
  1124. X! number will be zero or very nearly zero.  (Note that Calc uses a
  1125. X! slightly higher precision while finding the root, and thus the second
  1126. X! number may be slightly different from the value you would compute from
  1127. X! the equation yourself.)
  1128. X! 
  1129. X! The initial guess can be a real number, in which case Calc searches
  1130. X! for a real solution near that number, or a complex number, in which
  1131. X! case Calc searches the whole complex plane near that number for a
  1132. X! solution, or it can be an interval form which restricts the search
  1133. X! to values inside that interval of real numbers.
  1134. X! 
  1135. X! Calc tries to use @kbd{a d} to take the derivative of the equation.
  1136. X! If this succeeds, it uses Newton's method.  If the equation is not
  1137. X! differentiable Calc uses a bisection method.  (If Newton's method
  1138. X! appears to be going astray, Calc switches over to bisection if it
  1139. X! can, or otherwise gives up.  In this case it may help to try again
  1140. X! with a slightly different initial guess.)  If the initial guess is a
  1141. X! complex number, the function must be differentiable.
  1142. X! 
  1143. X! If the formula (or the difference between the sides of an equation)
  1144. X! is negative at one end of the interval you specify and positive at
  1145. X! the other end, the root finder is guaranteed to find a root if there
  1146. X! is one.  Otherwise, Calc subdivides the interval into small parts
  1147. X! looking for positive and negative values to bracket the root.  If your
  1148. X! guess is an interval, Calc will not look outside that interval for a root.
  1149. X! 
  1150. X! @kindex H a R
  1151. X! @tindex wroot
  1152. X! The @kbd{H a R} [@code{wroot}] command is similar to @kbd{a R}, except
  1153. X! that if the initial guess is an interval for which the function has
  1154. X! the same sign at both ends, then rather than subdividing the interval
  1155. X! Calc attempts to widen it to enclose a root.  Use this mode if
  1156. X! you are not sure if the function has a root in your interval.
  1157. X! 
  1158. X! If the function is not differentiable, and you give a simple number
  1159. X! instead of an interval as your initial guess, Calc uses this widening
  1160. X! process even if you did not type the Hyperbolic flag.  (If the function
  1161. X! @emph{is} differentiable, Calc uses Newton's method which does not
  1162. X! require a bounding interval in order to work.)
  1163. X! 
  1164. X! @cindex Systems of equations, solving numerically
  1165. X! The @kbd{a R} command can also solve systems of equations.  In this
  1166. X! case, the equation should instead be a vector of equations, the
  1167. X! guess should instead be a vector of numbers (intervals are not
  1168. X! supported), and the variable should be a vector of variables.  You
  1169. X! can omit the brackets while entering the list of variables.  Each
  1170. X! equation must be differentiable by each variable for this mode to
  1171. X! work.  The result will be a vector of two vectors:  The variable
  1172. X! values that solved the system of equations, and the differences
  1173. X! between the sides of the equations with those variable values.
  1174. SHAR_EOF
  1175. echo "End of part 17, continue with part 18"
  1176. echo "18" > s2_seq_.tmp
  1177. exit 0
  1178. --
  1179. Dave Gillespie
  1180.   256-80 Caltech Pasadena CA USA 91125
  1181.   daveg@csvax.cs.caltech.edu, ...!cit-vax!daveg
  1182.  
  1183.  
  1184.