home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume24 / gnucalc / part49 < prev    next >
Encoding:
Text File  |  1991-11-01  |  55.5 KB  |  1,207 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i097:  gnucalc - GNU Emacs Calculator, v2.00, Part49/56
  4. Message-ID: <1991Nov1.183950.21360@sparky.imd.sterling.com>
  5. X-Md4-Signature: a52d97effca603a5a26f3c220a524f23
  6. Date: Fri, 1 Nov 1991 18:39:50 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 97
  11. Archive-name: gnucalc/part49
  12. Environment: Emacs
  13. Supersedes: gmcalc: Volume 13, Issue 27-45
  14.  
  15. ---- Cut Here and unpack ----
  16. #!/bin/sh
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file calc.texinfo continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 49; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test ! -f _shar_wnt_.tmp; then
  33.     echo 'x - still skipping calc.texinfo'
  34. else
  35. echo 'x - continuing file calc.texinfo'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
  37. include references to @code{select} to tell where in the pattern
  38. the selected sub-formula should appear.
  39. X
  40. If there is still exactly one @samp{select( )} function call in
  41. the formula after rewriting is done, it indicates which part of
  42. the formula should be selected afterwards.  Otherwise, the
  43. formula will be unselected.
  44. X
  45. You can make @kbd{j r} act much like @kbd{a r} by enclosing both parts
  46. of the rewrite rule with @samp{select()}.  However, @kbd{j r}
  47. allows you to use the current selection in more flexible ways.
  48. Suppose you wished to make a rule which removed the exponent from
  49. the selected term; the rule @samp{select(a)^x := select(a)} would
  50. work.  In the above example, it would rewrite @samp{2 select(a + b)^2}
  51. to @samp{2 select(a + b)}.  This would then be returned to the
  52. stack as @samp{2 (a + b)} with the @samp{a + b} selected.
  53. X
  54. The @kbd{j r} command uses one iteration by default, unlike
  55. @kbd{a r} which defaults to 100 iterations.  A numeric prefix
  56. argument affects @kbd{j r} in the same way as @kbd{a r}.
  57. @xref{Nested Formulas with Rewrite Rules}.
  58. X
  59. As with other selection commands, @kbd{j r} operates on the stack
  60. entry that contains the cursor.  (If the cursor is on the top-of-stack
  61. @samp{.} marker, it works as if the cursor were on the formula
  62. at stack level 1.)
  63. X
  64. If you don't specify a set of rules, the rules are taken from the
  65. top of the stack, just as with @kbd{a r}.  In this case, the
  66. cursor must indicate stack entry 2 or above as the formula to be
  67. rewritten (otherwise the same formula would be used as both the
  68. target and the rewrite rules).
  69. X
  70. If the indicated formula has no selection, the cursor position within
  71. the formula temporarily selects a sub-formula for the purposes of this
  72. command.  If the cursor is not on any sub-formula (e.g., it is in
  73. the line-number area to the left of the formula), the @samp{select( )}
  74. markers are ignored by the rewrite mechanism and the rules are allowed
  75. to apply anywhere in the formula.
  76. X
  77. As a special feature, the normal @kbd{a r} command also ignores
  78. @samp{select( )} calls in rewrite rules.  For example, if you used the
  79. above rule @samp{select(a)^x := select(a)} with @kbd{a r}, it would apply
  80. the rule as if it were @samp{a^x := a}.  Thus, you can write general
  81. purpose rules with @samp{select( )} hints inside them so that they
  82. will ``do the right thing'' in both @kbd{a r} and @kbd{j r},
  83. both with and without selections.
  84. X
  85. @node Matching Commands, Automatic Rewrites, Selections with Rewrite Rules, Rewrite Rules
  86. @subsection Matching Commands
  87. X
  88. @noindent
  89. @kindex a m
  90. @pindex calc-match
  91. @tindex match
  92. The @kbd{a m} (@code{calc-match}) [@code{match}] function takes a
  93. vector of formulas and a rewrite-rule-style pattern, and produces
  94. a vector of all formulas which match the pattern.  The command
  95. prompts you to enter the pattern; as for @kbd{a r}, you can enter
  96. a single pattern (i.e., a formula with meta-variables), or a
  97. vector of patterns, or a variable which contains patterns, or
  98. you can give a blank response in which case the patterns are taken
  99. from the top of the stack.  The pattern set will be compiled once
  100. and saved if it is stored in a variable.  If there are several
  101. patterns in the set, vector elements are kept if they match any
  102. of the patterns.
  103. X
  104. For example, @samp{match(a+b, [x, x+y, x-y, 7, x+y+z])}
  105. will return @samp{[x+y, x-y, x+y+z]}.
  106. X
  107. The @code{import} mechanism is not available for pattern sets.
  108. X
  109. The @kbd{a m} command does not provide you with any information about
  110. which meta-variables in the pattern matched which parts of the vector
  111. elements.  To get this information you should map @code{rewrite} across
  112. the vector instead.  For example, @samp{map(rewrite,
  113. [x, x+y, x-y, 7, x+y+z], a+b := b)} might produce the result
  114. @samp{[x, y, -y, 7, y+z]}.  (This has the unfortunate effect of
  115. recompiling the rewrite rule five times; putting @samp{a+b := b}
  116. in a variable would, as usual, avoid this problem.)
  117. @xref{Reducing and Mapping}.
  118. X
  119. The @kbd{a m} command can also be used to extract all vector elements
  120. which satisfy any condition:  The pattern @samp{x :: x>0} will select
  121. all the positive vector elements.
  122. X
  123. @kindex I a m
  124. @tindex matchnot
  125. With the Inverse flag [@code{matchnot}], this command extracts all
  126. vector elements which do @emph{not} match the given pattern.
  127. X
  128. @tindex matches
  129. There is also a function @samp{matches(@var{x}, @var{p})} which
  130. evaluates to 1 if expression @var{x} matches pattern @var{p}, or
  131. to 0 otherwise.  This is sometimes useful for including into the
  132. conditional clauses of other rewrite rules.
  133. X
  134. @node Automatic Rewrites, Debugging Rewrites, Matching Commands, Rewrite Rules
  135. @subsection Automatic Rewrites
  136. X
  137. @noindent
  138. @cindex @code{EvalRules} variable
  139. @vindex EvalRules
  140. It is possible to get Calc to apply a set of rewrite rules on all
  141. results, effectively adding to the built-in set of default
  142. simplifications.  To do this, simply store your rule set in the
  143. variable @code{EvalRules}.  There is a convenient @kbd{s E} command
  144. for editing @code{EvalRules}; @pxref{Operations on Variables}.
  145. X
  146. For example, suppose you want @samp{sin(a + b)} to be expanded out
  147. to @samp{sin(b) cos(a) + cos(b) sin(a)} wherever it appears, and
  148. similarly for @samp{cos(a + b)}.  The corresponding rewrite rule
  149. set would be,
  150. X
  151. @group
  152. @smallexample
  153. [ sin(a + b)  :=  cos(a) sin(b) + sin(a) cos(b),
  154. X  cos(a + b)  :=  cos(a) cos(b) - sin(a) sin(b) ]
  155. @end smallexample
  156. @end group
  157. X
  158. To apply these manually, you could put them in a variable called
  159. @code{trigexp} and then use @kbd{a r trigexp} every time you wanted
  160. to expand trig functions.  But if instead you store them in the
  161. variable @code{EvalRules}, they will automatically be applied to all
  162. sines and cosines of sums.  Then, with @samp{2 x} and @samp{45} on
  163. the stack, typing @kbd{+ S} will (assuming degrees mode) result in
  164. @samp{0.7071 sin(2 x) + 0.7071 cos(2 x)} automatically.
  165. X
  166. As each level of a formula is evaluated, the rules from
  167. @code{EvalRules} are applied before the default simplifications.
  168. Rewriting continues until no further @code{EvalRules} apply.
  169. Note that this is different from the usual order of application of
  170. rewrite rules:  @code{EvalRules} works from the bottom up, simplifying
  171. the arguments to a function before the function itself, while @kbd{a r}
  172. applies rules from the top down.
  173. X
  174. Because the @code{EvalRules} are tried first, you can use them to
  175. override the normal behavior of any built-in Calc function.
  176. X
  177. It is important not to write a rule that will get into an infinite
  178. loop.  For example, the rule set @samp{[f(0) := 1, f(n) := n f(n-1)]}
  179. appears to be a good definition of a factorial function, but it is
  180. unsafe.  Imagine what happens if @samp{f(2.5)} is simplified.  Calc
  181. will continue to subtract 1 from this argument forever without reaching
  182. zero.  A safer second rule would be @samp{f(n) := n f(n-1) :: n>0}.
  183. Another dangerous rule is @samp{g(x, y) := g(y, x)}.  Rewriting
  184. @samp{g(2, 4)}, this would bounce back and forth between that and
  185. @samp{g(4, 2)} forever.  If an infinite loop in @code{EvalRules}
  186. occurs, Emacs will eventually stop with a ``Computation got stuck
  187. or ran too long'' message.
  188. X
  189. Another subtle difference between @code{EvalRules} and regular rewrites
  190. concerns rules that rewrite a formula into an identical formula.  For
  191. example, @samp{f(n) := f(floor(n))} ``fails to match'' when @cite{n} is
  192. already an integer.  But in @code{EvalRules} this case is detected only
  193. if the righthand side literally becomes the original formula before any
  194. further simplification.  For example, @samp{f(n) := f(floor(n))} will
  195. get into an infinite loop if it occurs in @code{EvalRules}.  Calc will
  196. replace @samp{f(6)} with @samp{f(floor(6))}, which is different from
  197. @samp{f(6)}, so it will consider the rule to have matched and will
  198. continue simplifying that formula; first the argument is simplified
  199. to get @samp{f(6)}, then the rule matches again to get @samp{f(floor(6))}
  200. again, ad infinitum.  A much safer rule would check its argument first,
  201. say, with @samp{f(n) := f(floor(n)) :: !integer(n)}.
  202. X
  203. (What really happens is that the rewrite mechanism substitutes the
  204. meta-variables in the righthand side of a rule, compares to see if the
  205. result is the same as the original formula and fails if so, then uses
  206. the default simplifications to simplify the result and compares again
  207. (and again fails if the formula has simplified back to its original
  208. form).  The only special wrinkle for the @code{EvalRules} is that they
  209. will of course come back into play when the default simplifications
  210. are used.  What Calc wants to do is build @samp{f(floor(6))}, see that
  211. this is different from the original formula, simplify to @samp{f(6)},
  212. see that this is the same as the original formula, and thus halt the
  213. rewriting.  But while simplifying, @samp{f(6)} will again trigger
  214. the same @code{EvalRules} rule and Calc will get into a loop inside
  215. the rewrite mechanism itself.)
  216. X
  217. The @code{phase}, @code{schedule}, and @code{iterations} markers do
  218. not work in @code{EvalRules}.  If the rule set is divided into phases,
  219. only the phase 1 rules are applied, and the schedule is ignored.
  220. The rules are always repeated as many times as possible.
  221. X
  222. The @code{EvalRules} are applied to all function calls in a formula,
  223. but not to numbers (and other number-like objects like error forms),
  224. nor to vectors or individual variable names.  (Though they will apply
  225. to @emph{components} of vectors and error forms when appropriate.)  You
  226. might try to make a variable @code{phihat} which automatically expands
  227. to its definition without the need to press @kbd{=} by writing the
  228. rule @samp{quote(phihat) := (1-sqrt(5))/2}, but unfortunately this rule
  229. will not work as part of @code{EvalRules}.
  230. X
  231. Finally, another limitation is that Calc sometimes calls its built-in
  232. functions directly rather than going through the default simplifications.
  233. When it does this, @code{EvalRules} will not be able to override those
  234. functions.  For example, when you take the absolute value of the complex
  235. number @cite{(2, 3)}, Calc computes @samp{sqrt(2*2 + 3*3)} by calling
  236. the multiplication, addition, and square root functions directly rather
  237. than applying the default simplifications to this formula.  So an
  238. @code{EvalRules} rule that (perversely) rewrites @samp{sqrt(13) := 6}
  239. would not apply.  (However, if you put Calc into symbolic mode so that
  240. @samp{sqrt(13)} will be left in symbolic form by the built-in square
  241. root function, your rule will be able to apply.  But if the complex
  242. number were @cite{(3,4)}, so that @samp{sqrt(25)} must be calculated,
  243. then symbolic mode will not help because @samp{sqrt(25)} can be
  244. evaluated exactly to 5.)
  245. X
  246. One subtle restriction that normally only manifests itself with
  247. @code{EvalRules} is that while a given rewrite rule is in the process
  248. of being checked, that same rule cannot be recursively applied.  Calc
  249. effectively removes the rule from its rule set while checking the rule,
  250. then puts it back once the match succeeds or fails.  (The technical
  251. reason for this is that compiled pattern programs are not reentrant.)
  252. For example, consider the rule @samp{foo(x) := x :: foo(x/2) > 0}
  253. attempting to match @samp{foo(8)}.  This rule will be inactive while
  254. the condition @samp{foo(4) > 0} is checked, even though it might be
  255. an integral part of evaluating that condition.  Note that this is not
  256. a problem for the more usual recursive type of rule, such as
  257. @samp{foo(x) := foo(x/2)}, because there the rule has succeeded and
  258. been reactivated by the time the righthand side is evaluated.
  259. X
  260. If @code{EvalRules} has no stored value (its default state), or if
  261. anything but a non-empty vector is stored in it, then it is ignored.
  262. X
  263. Even though Calc's rewrite mechanism is designed to compare rewrite
  264. rules to formulas as quickly as possible, storing rules in
  265. @code{EvalRules} may make Calc run substantially slower.  This is
  266. particularly true of rules where the top-level function call is common,
  267. or is not fixed.  The rule @samp{f(n) := n f(n-1) :: n>0} will only
  268. activate the rewrite mechanism for calls to the function @code{f}, but
  269. @samp{lg(n) + lg(m) := lg(n m)} will check every @samp{+} operator.
  270. And @samp{apply(f, [a*b]) := apply(f, [a]) + apply(f, [b]) ::
  271. in(f, [ln, log10])} may seem more ``efficient'' than two separate
  272. rules for @code{ln} and @code{log10}, but actually it is vastly less
  273. efficient because rules with @code{apply} as the top-level pattern
  274. must be tested against @emph{every} function call that is simplified.
  275. X
  276. @cindex @code{AlgSimpRules} variable
  277. @vindex AlgSimpRules
  278. Suppose you want @samp{sin(a + b)} to be expanded out not all the time,
  279. but only when @kbd{a s} is used to simplify the formula.  The variable
  280. @code{AlgSimpRules} holds rules for this purpose.  The @kbd{a s} command
  281. will apply @code{EvalRules} and @code{AlgSimpRules} to the formula, as
  282. well as all of its built-in simplifications.
  283. X
  284. Most of the special limitations for @code{EvalRules} don't apply to
  285. @code{AlgSimpRules}.  Calc simply does an @kbd{a r AlgSimpRules}
  286. command with an infinite repeat count as the first step of @kbd{a s}.
  287. It then applies its own built-in simplifications throughout the
  288. formula, and then repeats these two steps (along with applying the
  289. default simplifications) until no further changes are possible.
  290. X
  291. @cindex @code{ExtSimpRules} variable
  292. @cindex @code{UnitSimpRules} variable
  293. @vindex ExtSimpRules
  294. @vindex UnitSimpRules
  295. There are also @code{ExtSimpRules} and @code{UnitSimpRules} variables
  296. that are used by @kbd{a e} and @kbd{u s}, respectively; these commands
  297. also apply @code{EvalRules} and @code{AlgSimpRules}.  The variable
  298. @code{IntegSimpRules} contains simplification rules that are used
  299. only during integration by @kbd{a i}; there is no special command
  300. for editing this variable.
  301. X
  302. @node Debugging Rewrites, Examples of Rewrite Rules, Automatic Rewrites, Rewrite Rules
  303. @subsection Debugging Rewrites
  304. X
  305. @noindent
  306. If a buffer named @samp{*Trace*} exists, the rewrite mechanism will
  307. record some useful information there as it operates.  The original
  308. formula is written there, as is the result of each successful rewrite,
  309. and the final result of the rewriting.  All phase changes are also
  310. noted.
  311. X
  312. Calc always appends to @samp{*Trace*}.  You must empty this buffer
  313. yourself periodically if it is in danger of growing unwieldy.
  314. X
  315. Note that the rewriting mechanism is substantially slower when the
  316. @samp{*Trace*} buffer exists.  Once you are done, you will probably
  317. want to kill this buffer (with @kbd{C-x k *Trace* @key{RET}}).
  318. If you leave it in existence and forget about it, all your future
  319. rewrite commands will be needlessly slow.
  320. X
  321. @node Examples of Rewrite Rules, , Debugging Rewrites, Rewrite Rules
  322. @subsection Examples of Rewrite Rules
  323. X
  324. @noindent
  325. Returning to the example of substituting the pattern
  326. @samp{sin(x)^2 + cos(x)^2} with 1, we saw that the rule
  327. @samp{opt(a) sin(x)^2 + opt(a) cos(x)^2 := a} does a good job of
  328. finding suitable cases.  Another solution would be to use the rule
  329. @samp{cos(x)^2 := 1 - sin(x)^2}, followed by algebraic simplification
  330. if necessary.  This rule will be the most effective way to do the job,
  331. but at the expense of making some changes that you might not desire.@refill
  332. X
  333. Another algebraic rewrite rule is @samp{exp(x+y) := exp(x) exp(y)}.
  334. To make this work with the @kbd{j r} command so that it can be
  335. easily targeted to a particular exponential in a large formula,
  336. you might wish to write the rule as @samp{select(exp(x+y)) :=
  337. select(exp(x) exp(y))}.  The @samp{select} markers will be
  338. ignored by the regular @kbd{a r} command
  339. (@pxref{Selections with Rewrite Rules}).@refill
  340. X
  341. A surprisingly useful rewrite rule is @samp{a/(b-c) := a*(b+c)/(b^2-c^2)}.
  342. This will simplify the formula whenever @cite{b} and/or @cite{c} can
  343. be made simpler by squaring.  For example, applying this rule to
  344. @samp{2 / (sqrt(2) + 3)} yields @samp{6:7 - 2:7 sqrt(2)} (assuming
  345. Symbolic Mode has been enabled to keep the square root from being
  346. evaulated to a floating-point approximation).  This rule is also
  347. useful when working with symbolic complex numbers, e.g.,
  348. @samp{(a + b i) / (c + d i)}.
  349. X
  350. As another example, we could define our own factorial function with the
  351. rules @samp{[f(0) := 1, f(n) := n*f(n-1) :: n>0]}.  Enter this vector
  352. and store it in a variable:  @kbd{s t factrules}.  Now, given a factorial
  353. formula like @samp{f(5)} on the stack, type @samp{a r factrules} to apply
  354. these rules repeatedly.  After six applications, @kbd{a r} will stop with
  355. 120 on the stack.  Once these rules are debugged, it would probably be
  356. most useful to add them to @code{EvalRules} so that Calc will evaluate
  357. the new @code{f} function automatically.  We could then use @kbd{Z K} on
  358. the keyboard macro @kbd{' f($) RET} to make a command analogous to @kbd{!}
  359. that applies @code{f} to the value on the top of the stack.
  360. @xref{Programming}.
  361. X
  362. @cindex Quaternions
  363. The following rule set, contributed by @c{Fran\c cois}
  364. @asis{Francois} Pinard, implements
  365. @dfn{Quaternions}, a generalization of the concept of complex numbers.
  366. Quaternions have four components, and are here represented by function
  367. calls @samp{quat(@var{w}, [@var{x}, @var{y}, @var{x}])} with ``real
  368. part'' @var{w} and the three ``imaginary'' parts collected into a
  369. vector.  Various arithmetical operations on quaternions are supported.
  370. To use these rules, either add them to @code{EvalRules}, or create a
  371. command based on @kbd{a r} for simplifying quaternion formulas.
  372. A convenient way to enter quaternions would be a command defined by
  373. a keyboard macro containing: @kbd{' quat($$$$, [$$$, $$, $]) @key{RET}}.
  374. X
  375. @smallexample
  376. [ quat(w, x, y, z) := quat(w, [x, y, z]),
  377. X  quat(w, [0, 0, 0]) := w,
  378. X  abs(quat(w, v)) := hypot(w, v),
  379. X  -quat(w, v) := quat(-w, -v),
  380. X  r + quat(w, v) := quat(r + w, v) :: real(r),
  381. X  r - quat(w, v) := quat(r - w, -v) :: real(r),
  382. X  quat(w1, v1) + quat(w2, v2) := quat(w1 + w2, v1 + v2),
  383. X  r * quat(w, v) := quat(r * w, r * v) :: real(r),
  384. X  plain(quat(w1, v1) * quat(w2, v2))
  385. X     := quat(w1 * w2 - v1 * v2, w1 * v2 + w2 * v1 + cross(v1, v2)),
  386. X  quat(w1, v1) / r := quat(w1 / r, v1 / r) :: real(r),
  387. X  z / quat(w, v) := z * quatinv(quat(w, v)),
  388. X  quatinv(quat(w, v)) := quat(w, -v) / (w^2 + v^2),
  389. X  quatsqr(quat(w, v)) := quat(w^2 - v^2, 2 * w * v),
  390. X  quat(w, v)^k := quatsqr(quat(w, v)^(k / 2))
  391. X               :: integer(k) :: k > 0 :: k % 2 = 0,
  392. X  quat(w, v)^k := quatsqr(quat(w, v)^((k - 1) / 2)) * quat(w, v)
  393. X               :: integer(k) :: k > 2,
  394. X  quat(w, v)^-k := quatinv(quat(w, v)^k) :: integer(k) :: k > 0 ]
  395. @end smallexample
  396. X
  397. Quaternions, like matrices, have non-commutative multiplication.
  398. In other words, @cite{q1 * q2 = q2 * q1} is not necessarily true if
  399. @cite{q1} and @cite{q2} are @code{quat} forms.  The @samp{quat*quat}
  400. rule above uses @code{plain} to prevent Calc from rearranging the
  401. product.  It may also be wise to add the line @samp{[quat(), matrix]}
  402. to the @code{Decls} matrix, to ensure that Calc's other algebraic
  403. operations will not rearrange a quaternion product.  @xref{Declarations}.
  404. X
  405. These rules also accept a four-argument @code{quat} form, converting
  406. it to the preferred form in the first rule.  If you would rather see
  407. results in the four-argument form, just append the two items
  408. @samp{phase(2), quat(w, [x, y, z]) := quat(w, x, y, z)} to the end
  409. of the rule set.  (But remember that multi-phase rule sets don't work
  410. in @code{EvalRules}.)
  411. X
  412. @node Units, Store and Recall, Algebra, Top
  413. @chapter Operating on Units
  414. X
  415. @noindent
  416. One special interpretation of formulas is as numbers with units.
  417. For example, the formula @samp{5 m / s^2} can be read ``five meters
  418. per second squared.''  The commands in this chapter help you
  419. manipulate units expressions in this form.  Units-related commands
  420. begin with the @kbd{u} prefix key.
  421. X
  422. @menu
  423. * Basic Operations on Units::
  424. * The Units Table::
  425. * Predefined Units::
  426. * User-Defined Units::
  427. @end menu
  428. X
  429. @node Basic Operations on Units, The Units Table, Units, Units
  430. @section Basic Operations on Units
  431. X
  432. @noindent
  433. A @dfn{units expression} is a formula which is basically a number
  434. multiplied and/or divided by one or more @dfn{unit names}, which may
  435. optionally be raised to powers.  Actually, the value part need not be
  436. a number; any product or quotient involving unit names is a units
  437. expression.  Many of the units commands will also accept any formula,
  438. where the command applies to all units expressions which appear in the
  439. formula.
  440. X
  441. A unit name is a variable whose name appears in the @dfn{unit table},
  442. or a variable whose name is a prefix character like @samp{k} (for ``kilo'')
  443. or @samp{u} (for ``micro'') followed by a name from the unit table.
  444. A substantial table of built-in units is provided with Calc;
  445. @pxref{Predefined Units}.  You can also define your own unit names;
  446. @pxref{User-Defined Units}.@refill
  447. X
  448. Note that if the value part of a units expression is exactly @samp{1},
  449. it will be removed by the Calculator's automatic algebra routines:  The
  450. formula @samp{1 mm} is ``simplified'' to @samp{mm}.  This is only a
  451. display anomaly, however; @samp{mm} will work just fine as a
  452. representation of one millimeter.@refill
  453. X
  454. You may find that Algebraic Mode (@pxref{Algebraic Entry}) makes working
  455. with units expressions easier.  Otherwise, you will have to remember
  456. to hit the apostrophe key every time you wish to enter units.
  457. X
  458. @kindex u s
  459. @pindex calc-simplify-units
  460. @tindex usimplify
  461. The @kbd{u s} (@code{calc-simplify-units}) [@code{usimplify}] command
  462. simplifies a units
  463. expression.  It uses @kbd{a s} (@code{calc-simplify}) to simplify the
  464. expression first as a regular algebraic formula; it then looks for
  465. features that can be further simplified by converting one object's units
  466. to be compatible with another's.  For example, @samp{5 m + 23 mm} will
  467. simplify to @samp{5.023 m}.  When different but compatible units are
  468. added, the righthand term's units are converted to match those of the
  469. lefthand term.  @xref{Simplification Modes}, for a way to have this done
  470. automatically at all times.@refill
  471. X
  472. Units simplification also handles quotients of two units with the same
  473. dimensionality, as in @samp{2 in s/L cm} to @samp{5.08 s/L}; fractional
  474. powers of unit expressions, as in @samp{sqrt(9 mm^2)} to @samp{3 mm} and
  475. @samp{sqrt(9 acre)} to a quantity in meters; and @code{floor},
  476. @code{ceil}, @code{round}, @code{trunc}, @code{float}, @code{frac},
  477. @code{abs}, and @code{clean} applied to units expressions, in which case
  478. the operation in question is applied only to the numeric part of the
  479. expression.  Finally, trigonometric functions of quantities with units
  480. of angle are evaluated, regardless of the current angular mode.@refill
  481. X
  482. @kindex u c
  483. @pindex calc-convert-units
  484. The @kbd{u c} (@code{calc-convert-units}) command converts a units
  485. expression to new, compatible units.  For example, given the units
  486. expression @samp{55 mph}, typing @kbd{u c m/s @key{RET}} produces
  487. @samp{24.5872 m/s}.  If the units you request are inconsistent with
  488. the original units, the number will be converted into your units
  489. times whatever ``remainder'' units are left over.  For example,
  490. converting @samp{55 mph} into acres produces @samp{6.08e-3 acre / m s}.
  491. (Recall that multiplication binds more strongly than division in Calc
  492. formulas, so the units here are acres per meter-second.)  Remainder
  493. units are expressed in terms of ``fundamental'' units like @samp{m} and
  494. @samp{s}, regardless of the input units.
  495. X
  496. One special exception is that if you specify a single unit name, and
  497. a compatible unit appears somewhere in the units expression, then
  498. that compatible unit will be converted to the new unit and the
  499. remaining units in the expression will be left alone.  For example,
  500. given the input @samp{980 cm/s^2}, the command @kbd{u c ms} will
  501. change the @samp{s} to @samp{ms} to get @samp{9.8e-4 cm/ms^2}.
  502. The ``remainder unit'' @samp{cm} is left alone rather than being
  503. changed to the base unit @samp{m}.
  504. X
  505. You can use explicit unit conversion instead of the @kbd{u s} command
  506. to gain more control over the units of the result of an expression.
  507. For example, given @samp{5 m + 23 mm}, you can type @kbd{u c m} or
  508. @kbd{u c mm} to express the result in either meters or millimeters.
  509. (For that matter, you could type @kbd{u c fath} to express the result
  510. in fathoms, if you preferred!)
  511. X
  512. In place of a specific set of units, you can also enter one of the
  513. units system names @code{si}, @code{mks} (equivalent), or @code{cgs}.
  514. For example, @kbd{u c si @key{RET}} converts the expression into
  515. International System of Units (SI) base units.  Also, @code{base}
  516. converts to Calc's base units, which are the same as @code{si} units
  517. except that @code{base} uses @samp{g} as the fundamental unit of mass
  518. whereas @code{si} uses @samp{kg}.
  519. X
  520. @cindex Composite units
  521. The @kbd{u c} command also accepts @dfn{composite units}, which
  522. are expressed as the sum of several compatible unit names.  For
  523. example, converting @samp{30.5 in} to units @samp{mi+ft+in} (miles,
  524. feet, and inches) produces @samp{2 ft + 6.5 in}.  Calc first
  525. sorts the unit names into order of decreasing relative size.
  526. It then accounts for as much of the input quantity as it can
  527. using an integer number times the largest unit, then moves on
  528. to the next smaller unit, and so on.  Only the smallest unit
  529. may have a non-integer amount attached in the result.  A few
  530. standard unit names exist for common combinations, such as
  531. @code{mfi} for @samp{mi+ft+in}, and @code{tpo} for @samp{ton+lb+oz}.
  532. Composite units are expanded as if by @kbd{a x}, so that
  533. @samp{(ft+in)/hr} is first converted to @samp{ft/hr+in/hr}.
  534. X
  535. If the value on the stack does not contain any units, @kbd{u c} will
  536. prompt first for the old units which this value should be considered
  537. to have, then for the new units.  Assuming the old and new units you
  538. give are consistent with each other, the result also will not contain
  539. any units.  For example, @kbd{u c cm RET in RET} converts the number
  540. 2 on the stack to 5.08.
  541. X
  542. @kindex u b
  543. @pindex calc-base-units
  544. The @kbd{u b} (@code{calc-base-units}) command is shorthand for
  545. @kbd{u c base}; it converts the units expression on the top of the
  546. stack into @code{base} units.  If @kbd{u s} does not simplify a
  547. units expression as far as you would like, try @kbd{u b}.
  548. X
  549. The @kbd{u c} and @kbd{u b} commands treat temperature units (like
  550. @samp{degC} and @samp{K}) as relative temperatures.  For example,
  551. @kbd{u c} converts @samp{10 degC} to @samp{18 degF}: A change of 10
  552. degrees Celsius corresponds to a change of 18 degrees Fahrenheit.
  553. X
  554. @kindex u t
  555. @pindex calc-convert-temperature
  556. @cindex Temperature conversion
  557. The @kbd{u t} (@code{calc-convert-temperature}) command converts
  558. absolute temperatures.  The value on the stack must be a simple units
  559. expression with units of temperature only.  This command would convert
  560. @samp{10 degC} to @samp{50 degF}, the equivalent temperature on the
  561. Fahrenheit scale.@refill
  562. X
  563. @kindex u r
  564. @pindex calc-remove-units
  565. @kindex u x
  566. @pindex calc-extract-units
  567. The @kbd{u r} (@code{calc-remove-units}) command removes units from the
  568. formula at the top of the stack.  The @kbd{u x}
  569. (@code{calc-extract-units}) command extracts only the units portion of a
  570. formula.  These commands essentially replace every term of the formula
  571. that does or doesn't (respectively) look like a unit name by the
  572. constant 1, then resimplify the formula.@refill
  573. X
  574. @kindex u a
  575. @pindex calc-autorange-units
  576. The @kbd{u a} (@code{calc-autorange-units}) command turns on and off a
  577. mode in which unit prefixes like @code{k} (``kilo'') are automatically
  578. applied to keep the numeric part of a units expression in a reasonable
  579. range.  This mode affects @kbd{u s} and all units conversion commands
  580. except @kbd{u b}.  For example, with autoranging on, @samp{12345 Hz}
  581. will be simplified to @samp{12.345 kHz}.  Autoranging is useful for
  582. some kinds of units (like @code{Hz} and @code{m}), but is probably
  583. undesirable for non-metric units like @code{ft} and @code{tbsp}.
  584. (Composite units are more appropriate for those; see above.)
  585. X
  586. Autoranging always applies the prefix to the leftmost unit name.
  587. Calc chooses the largest prefix that causes the number to be greater
  588. than or equal to 1.0.  Thus an increasing sequence of adjusted times
  589. would be @samp{1 ms, 10 ms, 100 ms, 1 s, 10 s, 100 s, 1 ks}.
  590. Generally the rule of thumb is that the number will be adjusted
  591. to be in the interval @samp{[1 .. 1000)}, although there are several
  592. exceptions to this rule.  First, if the unit has a power then this
  593. is not possible; @samp{0.1 s^2} simplifies to @samp{100000 ms^2}.
  594. Second, the ``centi-'' prefix is allowed to form @code{cm} (centimeters),
  595. but will not apply to other units.  The ``deci-,'' ``deka-,'' and
  596. ``hecto-'' prefixes are never used.  Thus the allowable interval is
  597. @samp{[1 .. 10)} for millimeters and @samp{[1 .. 100)} for centimeters.
  598. Finally, a prefix will not be added to a unit if the resulting name
  599. is also the actual name of another unit; @samp{1e-15 t} would normally
  600. be considered a ``femto-ton,'' but it is written as @samp{1000 at}
  601. (1000 atto-tons) instead because @code{ft} would be confused with feet.
  602. X
  603. @node The Units Table, Predefined Units, Basic Operations on Units, Units
  604. @section The Units Table
  605. X
  606. @noindent
  607. @kindex u v
  608. @pindex calc-enter-units-table
  609. The @kbd{u v} (@code{calc-enter-units-table}) command displays the units table
  610. in another buffer called @code{*Units Table*}.  Each entry in this table
  611. gives the unit name as it would appear in an expression, the definition
  612. of the unit in terms of simpler units, and a full name or description of
  613. the unit.  Fundamental units are defined as themselves; these are the
  614. units produced by the @kbd{u b} command.  The fundamental units are
  615. meters, seconds, grams, kelvins, amperes, candelas, moles, radians,
  616. and steradians.
  617. X
  618. The Units Table buffer also displays the Unit Prefix Table.  Note that
  619. two prefixes, ``kilo'' and ``hecto,'' accept either upper- or lower-case
  620. prefix letters.  @samp{Meg} is also accepted as a synonym for the @samp{M}
  621. prefix.  Whenever a unit name can be interpreted as either a built-in name
  622. or a prefix followed by another built-in name, the former interpretation
  623. wins.  For example, @samp{2 pt} means two pints, not two pico-tons.
  624. X
  625. The Units Table buffer, once created, is not rebuilt unless you define
  626. new units.  To force the buffer to be rebuilt, give any numeric prefix
  627. argument to @kbd{u v}.
  628. X
  629. @kindex u V
  630. @pindex calc-view-units-table
  631. The @kbd{u V} (@code{calc-view-units-table}) command is like @kbd{u v} except
  632. that the cursor is not moved into the Units Table buffer.  You can
  633. type @kbd{u V} again to remove the Units Table from the display.  To
  634. return from the Units Table buffer after a @kbd{u v}, type @kbd{M-x calc}
  635. again or use the regular Emacs @kbd{C-x o} (@code{other-window})
  636. command.  You can also kill the buffer with @kbd{C-x k} if you wish;
  637. the actual units table is safely stored inside the Calculator.
  638. X
  639. @kindex u g
  640. @pindex calc-get-unit-definition
  641. The @kbd{u g} (@code{calc-get-unit-definition}) command retrieves a unit's
  642. defining expression and pushes it onto the Calculator stack.  For example,
  643. @kbd{u g in} will produce the expression @samp{2.54 cm}.  This is the
  644. same definition for the unit that would appear in the Units Table buffer.
  645. Note that this command works only for actual unit names; @kbd{u g km}
  646. will report that no such unit exists, for example, because @code{km} is
  647. really the unit @code{m} with a @code{k} (``kilo'') prefix.  To see a
  648. definition of a unit in terms of base units, it is easier to push the
  649. unit name on the stack and then reduce it to base units with @kbd{u b}.
  650. X
  651. @kindex u e
  652. @pindex calc-explain-units
  653. The @kbd{u e} (@code{calc-explain-units}) command displays an English
  654. description of the units of the expression on the stack.  For example,
  655. for the expression @samp{62 km^2 g / s^2 mol K}, the description is
  656. ``Square-Kilometer Gram per (Second-squared Mole Degree-Kelvin).''  This
  657. command uses the English descriptions that appear in the righthand
  658. column of the Units Table.
  659. X
  660. @node Predefined Units, User-Defined Units, The Units Table, Units
  661. @section Predefined Units
  662. X
  663. @noindent
  664. Since the exact definitions of many kinds of units have evolved over the
  665. years, and since certain countries sometimes have local differences in
  666. their definitions, it is a good idea to examine Calc's definition of a
  667. unit before depending on its exact value.  For example, there are three
  668. different units for gallons, corresponding to the US (@code{gal}),
  669. Canadian (@code{galC}), and British (@code{galUK}) definitions.  Also,
  670. note that @code{oz} is a standard ounce of mass, @code{ozt} is a Troy
  671. ounce, and @code{ozfl} is a fluid ounce.
  672. X
  673. The temperature units corresponding to degrees Kelvin and Centigrade
  674. (Celsius) are the same in this table, since most units commands treat
  675. temperatures as being relative.  The @code{calc-convert-temperature}
  676. command has special rules for handling the different absolute magnitudes
  677. of the various temperature scales.
  678. X
  679. The unit of volume ``liters'' can be referred to by either the lower-case
  680. @code{l} or the upper-case @code{L}.
  681. X
  682. The unit @code{A} stands for Amperes; the name @code{Ang} is used
  683. @iftex
  684. for \AA ngstroms.
  685. @end iftex
  686. @ifinfo
  687. for Angstroms.
  688. @end ifinfo
  689. X
  690. The unit @code{pt} stands for pints; the name @code{point} stands for
  691. a typographical point, defined by @samp{72 point = 1 in}.  There is
  692. also @code{tpt}, which stands for a printer's point as defined by the
  693. @TeX{} typesetting system:  @samp{72.27 tpt = 1 in}.
  694. X
  695. The unit @code{e} stands for the elementary (electron) unit of charge;
  696. because algebra command could mistake this for the special constant
  697. @cite{e}, Calc provides the alternate unit name @code{ech} which is
  698. preferable to @code{e}.
  699. X
  700. The name @code{g} stands for one gram of mass; there is also @code{gf},
  701. one gram of force.  (Likewise for @kbd{lb}, pounds, and @kbd{lbf}).
  702. Meanwhile, one ``@cite{g}'' of acceleration is denoted @code{ga}.
  703. X
  704. The unit @code{ton} is a U.S. ton of @samp{2000 lb}, and @code{t} is
  705. a metric ton of @samp{1000 kg}.
  706. X
  707. The names @code{s} (or @code{sec}) and @code{min} refer to units of
  708. time; @code{arcsec} and @code{arcmin} are units of angle.
  709. X
  710. Some ``units'' are really physical constants; for example, @code{c}
  711. represents the speed of light, and @code{h} represents Planck's
  712. constant.  You can use these just like other units: converting
  713. @samp{.5 c} to @samp{m/s} expresses one-half the speed of light in
  714. meters per second.  You can also use this merely as a handy reference;
  715. the @kbd{u g} command gets the definition of one of these constants
  716. in its normal terms, and @kbd{u b} expresses the definition in base
  717. units.
  718. X
  719. Two units, @code{pi} and @code{fsc} (the fine structure constant) are
  720. dimensionless.  The units simplification commands simply treat these
  721. names as equivalent to their corresponding values.  However you can, for
  722. example, use @kbd{u c} to convert a pure number into multiples of the
  723. fine structure constant, or @kbd{u b} to convert this back into a pure
  724. number.  (When @kbd{u c} prompts for the ``old units,'' just enter a
  725. blank line to signify that the value really is unitless.)
  726. X
  727. @c Describe angular units, luminosity vs. steradians problem.
  728. X
  729. @node User-Defined Units, , Predefined Units, Units
  730. @section User-Defined Units
  731. X
  732. @noindent
  733. @kindex u d
  734. @pindex calc-define-unit
  735. @cindex User-defined units
  736. The @kbd{u d} (@code{calc-define-unit}) command records the units
  737. expression on the top of the stack as the definition for a new,
  738. user-defined unit.  For example, putting @samp{16.5 ft} on the stack and
  739. typing @kbd{u d rod} defines the new unit @samp{rod} to be equivalent to
  740. 16.5 feet.  The unit conversion and simplification commands will now
  741. treat @code{rod} just like any other unit of length.  You will also be
  742. prompted for an optional English description of the unit, which will
  743. appear in the Units Table.
  744. X
  745. @kindex u u
  746. @pindex calc-undefine-unit
  747. The @kbd{u u} (@code{calc-undefine-unit}) command removes a user-defined
  748. unit.  It is not possible to remove one of the predefined units,
  749. however.
  750. X
  751. If you define a unit with an existing unit name, your new definition
  752. will replace the original definition of that unit.  If the unit was a
  753. predefined unit, the old definition will not be replaced, only
  754. ``shadowed.''  The built-in definition will reappear if you later use
  755. @kbd{u u} to remove the shadowing definition.
  756. X
  757. To create a new fundamental unit, use either 1 or the unit name itself
  758. as the defining expression.  Otherwise the expression can involve any
  759. other units that you like (except for composite units like @samp{mfi}).
  760. You can create a new composite unit with a sum of other units as the
  761. defining expression.  The next unit operation like @kbd{u c} or @kbd{u v}
  762. will rebuild the internal unit table incorporating your modifications.
  763. Note that erroneous definitions (such as two units defined in terms of
  764. each other) will not be detected until the unit table is next rebuilt;
  765. @kbd{u v} is a convenient way to cause this to happen.
  766. X
  767. Temperature units are treated specially inside the Calculator; it is not
  768. possible to create user-defined temperature units.
  769. X
  770. @kindex u p
  771. @pindex calc-permanent-units
  772. @cindex @file{.emacs} file, user-defined units
  773. The @kbd{u p} (@code{calc-permanent-units}) command stores the user-defined
  774. units in your @file{.emacs} file, so that the units will still be
  775. available in subsequent Emacs sessions.  If there was already a set of
  776. user-defined units in your @file{.emacs} file, it is replaced by the
  777. new set.  (@xref{General Mode Commands}, for a way to tell Calc to use
  778. a different file instead of @file{.emacs}.)
  779. X
  780. @node Store and Recall, Graphics, Units, Top
  781. @chapter Storing and Recalling
  782. X
  783. @noindent
  784. Calculator variables are really just Lisp variables that contain numbers
  785. or formulas in a form that Calc can understand.  The commands in this
  786. section allow you to manipulate variables conveniently.  Commands related
  787. to variables use the @kbd{s} prefix key.
  788. X
  789. @menu
  790. * Storing Variables::
  791. * Recalling Variables::
  792. * Operations on Variables::
  793. * Let Command::
  794. * Evaluates-To Operator::
  795. @end menu
  796. X
  797. @node Storing Variables, Recalling Variables, Store and Recall, Store and Recall
  798. @section Storing Variables
  799. X
  800. @noindent
  801. @kindex s s
  802. @pindex calc-store
  803. @cindex Storing variables
  804. @cindex Quick variables
  805. @vindex q0
  806. @vindex q9
  807. The @kbd{s s} (@code{calc-store}) command stores the value at the top of
  808. the stack into a specified variable.  It prompts you to enter the
  809. name of the variable.  If you press a single digit, the value is stored
  810. immediately in one of the ``quick'' variables @code{var-q0} through
  811. @code{var-q9}.  Or you can enter any variable name.  The prefix @samp{var-}
  812. is supplied for you; when a name appears in a formula (as in @samp{a+q2})
  813. the prefix @samp{var-} is also supplied there, so normally you can simply
  814. forget about @samp{var-} everywhere.  Its only purpose is to enable you to
  815. use Calc variables without fear of accidentally clobbering some variable in
  816. another Emacs package.  If you really want to store in an arbitrary Lisp
  817. variable, just backspace over the @samp{var-}.
  818. X
  819. @kindex s t
  820. @pindex calc-store-into
  821. The @kbd{s s} command leaves the stored value on the stack.  There is
  822. also an @kbd{s t} (@code{calc-store-into}) command, which removes a
  823. value from the stack and stores it in a variable.
  824. X
  825. If the top of stack value is an equation @samp{a = 7} or assignment
  826. @samp{a := 7} with a variable on the lefthand side, then Calc will
  827. assign that variable with that value by default, i.e., if you type
  828. @kbd{s s @key{RET}} or @kbd{s t @key{RET}}.  In this example, the
  829. value 7 would be stored in the variable @samp{a}.  (If you do type
  830. a variable name at the prompt, the top-of-stack value is stored in
  831. its entirety, even if it is an equation:  @samp{s s b @key{RET}}
  832. with @samp{a := 7} on the stack stores @samp{a := 7} in @code{b}.)
  833. X
  834. In fact, the top of stack value can be a vector of equations or
  835. assignments with different variables on their lefthand sides; the
  836. default will be to store all the variables with their corresponding
  837. righthand sides simultaneously.
  838. X
  839. It is also possible to type an equation or assignment directly at
  840. the prompt for the @kbd{s s} or @kbd{s t} command:  @kbd{s s foo = 7}.
  841. In this case the expression to the right of the @kbd{=} or @kbd{:=}
  842. symbol is evaluated as if by the @kbd{=} command, and that value is
  843. stored in the variable.  No value is taken from the stack; @kbd{s s}
  844. and @kbd{s t} are equivalent when used in this way.
  845. X
  846. @kindex s 0-9
  847. @kindex t 0-9
  848. The prefix keys @kbd{s} and @kbd{t} may be followed immediately by a
  849. digit; @kbd{s 9} is equivalent to @kbd{s s 9}, and @kbd{t 9} is
  850. equivalent to @kbd{s t 9}.  (The @kbd{t} prefix is otherwise used
  851. for trail and time/date commands.)
  852. X
  853. @kindex s +
  854. @kindex s -
  855. @kindex s *
  856. @kindex s /
  857. @kindex s ^
  858. @kindex s |
  859. @kindex s n
  860. @kindex s &
  861. @kindex s [
  862. @kindex s ]
  863. @pindex calc-store-plus
  864. @pindex calc-store-minus
  865. @pindex calc-store-times
  866. @pindex calc-store-div
  867. @pindex calc-store-power
  868. @pindex calc-store-concat
  869. @pindex calc-store-neg
  870. @pindex calc-store-inv
  871. @pindex calc-store-decr
  872. @pindex calc-store-incr
  873. There are also several ``arithmetic store'' commands.  For example,
  874. @kbd{s +} removes a value from the stack and adds it to the specified
  875. variable.  The other arithmetic stores are @kbd{s -}, @kbd{s *}, @kbd{s /},
  876. @kbd{s ^}, and @kbd{s |} (vector concatenation), plus @kbd{s n} and
  877. @kbd{s &} which negate or invert the value in a variable, and @kbd{s [}
  878. and @kbd{s ]} which decrease or increase a variable by one.
  879. X
  880. All the arithmetic stores accept the Inverse prefix to reverse the
  881. order of the operands.  If @cite{v} represents the contents of the
  882. variable, and @cite{a} is the value drawn from the stack, then regular
  883. @kbd{s -} assigns @cite{v := v - a}, but @kbd{I s -} assigns
  884. @cite{v := a - v}.  While @kbd{I s *} might seem pointless, it is
  885. useful if matrix multiplication is involved.  Actually, all the
  886. arithmetic stores use formulas designed to behave usefully both
  887. forwards and backwards:
  888. X
  889. @group
  890. @example
  891. s +        v := v + a          v := a + v
  892. s -        v := v - a          v := a - v
  893. s *        v := v * a          v := a * v
  894. s /        v := v / a          v := a / v
  895. s ^        v := v ^ a          v := a ^ v
  896. s |        v := v | a          v := a | v
  897. s n        v := v / (-1)       v := (-1) / v
  898. s &        v := v ^ (-1)       v := (-1) ^ v
  899. s [        v := v - 1          v := 1 - v
  900. s ]        v := v - (-1)       v := (-1) - v
  901. @end example
  902. @end group
  903. X
  904. In the last four cases, a numeric prefix argument will be used in
  905. place of the number one.  (For example, @kbd{M-2 s ]} increases
  906. a variable by 2, and @kbd{M-2 I s ]} replaces a variable by
  907. minus-two minus the variable.
  908. X
  909. The first six arithmetic stores can also be typed @kbd{s t +}, @kbd{s t -},
  910. etc.  The commands @kbd{s s +}, @kbd{s s -}, and so on are analogous
  911. arithmetic stores that don't remove the value @cite{a} from the stack.
  912. X
  913. All arithmetic stores report the new value of the variable in the
  914. Trail for your information.  They signal an error if the variable
  915. previously had no stored value.  If default simplifications have been
  916. turned off, the arithmetic stores temporarily turn them on for numeric
  917. arguments only (i.e., they temporarily do an @kbd{m N} command).
  918. @xref{Simplification Modes}.
  919. X
  920. @kindex s m
  921. @pindex calc-store-map
  922. The @kbd{s m} command is a general way to adjust a variable's value
  923. using any Calc function.  It is a ``mapping'' command analogous to
  924. @kbd{V M}, @kbd{V R}, etc.  @xref{Reducing and Mapping}, to see
  925. how to specify a function for a mapping command.  Basically,
  926. all you do is type the Calc command key that would invoke that
  927. function normally.  For example, @kbd{s m n} applies the @kbd{n}
  928. key to negate the contents of the variable, so @kbd{s m n} is
  929. equivalent to @kbd{s n}.  Also, @kbd{s m Q} takes the square root
  930. of the value stored in a variable, @kbd{s m v v} uses @kbd{v v} to
  931. reverse the vector stored in the variable, and @kbd{s m H I S}
  932. takes the hyperbolic arcsine of the variable contents.
  933. X
  934. If the mapping function takes two or more arguments, the additional
  935. arguments are taken from the stack; the old value of the variable
  936. is provided as the first argument.  Thus @kbd{s m -} with @cite{a}
  937. on the stack computes @cite{v - a}, just like @kbd{s -}.  With the
  938. Inverse prefix, the variable's original value becomes the @emph{last}
  939. argument instead of the first.  Thus @kbd{I s m -} is also
  940. equivalent to @kbd{I s -}.
  941. X
  942. @kindex s x
  943. @pindex calc-store-exchange
  944. The @kbd{s x} (@code{calc-store-exchange}) command exchanges the value
  945. of a variable with the value on the top of the stack.  Naturally, the
  946. variable must already have a stored value for this to work.
  947. X
  948. You can type an equation or assignment at the @kbd{s x} prompt.  The
  949. command @kbd{s x a=6} takes no values from the stack; instead, it
  950. pushes the old value of @samp{a} on the stack and stores @samp{a = 6}.
  951. X
  952. @kindex s u
  953. @pindex calc-unstore
  954. @cindex Void variables
  955. @cindex Un-storing variables
  956. Until you store something in them, variables are ``void,'' that is, they
  957. contain no value at all.  If they appear in an algebraic formula they
  958. will be left alone even if you press @kbd{=} (@code{calc-evaluate}).
  959. The @kbd{s u} (@code{calc-unstore}) command returns a variable to the
  960. void state.@refill
  961. X
  962. The only variables with predefined values are the ``special constants''
  963. @code{var-pi}, @code{var-e}, and @code{var-i}.  You are free to unstore
  964. these variables or to store new values into them if you like, although
  965. some of the algebraic-manipulation functions may assume these variables
  966. represent their standard values.  Calc displays a warning if you change
  967. the value of one of these variables, or of one of the other special
  968. variables @code{var-inf}, @code{var-uinf}, and @code{var-nan} (which
  969. are normally void).
  970. X
  971. Note that @code{var-pi} doesn't actually have 3.14159265359 stored
  972. in it, but rather a special magic value that evaluates to @c{$\pi$}
  973. @cite{pi}
  974. at the current precision.  Likewise @code{var-e}, @code{var-i}, and
  975. @code{var-phi} evaluate according to the current precision or polar mode.
  976. If you recall a value from @code{pi} and store it back, this magic
  977. property will be lost.
  978. X
  979. @kindex s c
  980. @pindex calc-copy-variable
  981. The @kbd{s c} (@code{calc-copy-variable}) command copies the stored
  982. value of one variable to another.  It differs from a simple @kbd{s r}
  983. followed by an @kbd{s t} in two important ways.  First, the value
  984. never goes on the stack and thus is never evaluated or simplified
  985. in any way; it is not even rounded down to the current precision.
  986. Second, the ``magic'' contents of a variable like @code{var-e} can
  987. be copied into another variable with this command, perhaps because
  988. you need to unstore @code{var-e} right now but you wish to put it
  989. back when you're done.  The @kbd{s c} command is the only way to
  990. manipulate these magic values intact.
  991. X
  992. @node Recalling Variables, Operations on Variables, Storing Variables, Store and Recall
  993. @section Recalling Variables
  994. X
  995. @noindent
  996. @kindex s r
  997. @pindex calc-recall
  998. @cindex Recalling variables
  999. The most straightforward way to extract the stored value from a variable
  1000. is to use the @kbd{s r} (@code{calc-recall}) command.  This command prompts
  1001. for a variable name (similarly to @code{calc-store}), looks up the value
  1002. of the specified variable, and pushes that value onto the stack.  It is
  1003. an error to try to recall a void variable.
  1004. X
  1005. It is also possible to recall the value from a variable by evaluating a
  1006. formula containing that variable.  For example, @kbd{' a @key{RET} =} is
  1007. the same as @kbd{s r a @key{RET}} except that if the variable is void, the
  1008. former will simply leave the formula @samp{a} on the stack whereas the
  1009. latter will produce an error message.
  1010. X
  1011. @kindex r 0-9
  1012. The @kbd{r} prefix may be followed by a digit, so that @kbd{r 9} is
  1013. equivalent to @kbd{s r 9}.  (The @kbd{r} prefix is otherwise unused
  1014. in the current version of Calc.)
  1015. X
  1016. @node Operations on Variables, Let Command, Recalling Variables, Store and Recall
  1017. @section Other Operations on Variables
  1018. X
  1019. @noindent
  1020. @kindex s e
  1021. @pindex calc-edit-variable
  1022. The @kbd{s e} (@code{calc-edit-variable}) command edits the stored
  1023. value of a variable without ever putting that value on the stack
  1024. or simplifying or evaluating the value.  It prompts for the name of
  1025. the variable to edit.  If the variable has no stored value, the
  1026. editing buffer will start out empty.  If the editing buffer is
  1027. empty when you press @key{M-# M-#} to finish, the variable will
  1028. be made void.  @xref{Editing Stack Entries}, for a general
  1029. description of editing.
  1030. X
  1031. The @kbd{s e} command is especially useful for creating and editing
  1032. rewrite rules which are stored in variables.  Sometimes these rules
  1033. contain formulas which must not be evaluated until the rules are
  1034. actually used.  (For example, they may refer to @samp{variable(x)},
  1035. which tests if @code{x} is a variable; if you let Calc evaluate the
  1036. rule while you are defining it, Calc will replace @samp{variable(x)}
  1037. with 1 (true) because @code{x} itself is a variable, but what you
  1038. really wanted was to test the thing that will replace @code{x} when
  1039. the rule is invoked.)  By contrast, recalling the variable, editing
  1040. with @kbd{`}, and storing will evaluate the variable's value as a
  1041. side effect of putting the value on the stack.
  1042. X
  1043. There are several special-purpose variable-editing commands that
  1044. use the @kbd{s} prefix followed by a shifted letter:
  1045. X
  1046. @kindex s A
  1047. @kindex s D
  1048. @kindex s E
  1049. @kindex s F
  1050. @kindex s G
  1051. @kindex s I
  1052. @kindex s P
  1053. @kindex s T
  1054. @kindex s U
  1055. @kindex s X
  1056. @pindex calc-store-AlgSimpRules
  1057. @pindex calc-store-Decls
  1058. @pindex calc-store-EvalRules
  1059. @pindex calc-store-FitRules
  1060. @pindex calc-store-GenCount
  1061. @pindex calc-store-IntegLimit
  1062. @pindex calc-store-PlotRejects
  1063. @pindex calc-store-TimeZone
  1064. @pindex calc-store-UnitSimpRules
  1065. @pindex calc-store-ExtSimpRules
  1066. @table @kbd
  1067. @item s A
  1068. Edit @code{AlgSimpRules}.  @xref{Algebraic Simplifications}.
  1069. @item s D
  1070. Edit @code{Decls}.  @xref{Declarations}.
  1071. @item s E
  1072. Edit @code{EvalRules}.  @xref{Default Simplifications}.
  1073. @item s F
  1074. Edit @code{FitRules}.  @xref{Curve Fitting}.
  1075. @item s G
  1076. Edit @code{GenCount}.  @xref{Solving Equations}.
  1077. @item s I
  1078. Edit @code{IntegLimit}.  @xref{Calculus}.
  1079. @item s P
  1080. Edit @code{PlotRejects}.  @xref{Graphics}.
  1081. @item s T
  1082. Edit @code{TimeZone}.  @xref{Time Zones}.
  1083. @item s U
  1084. Edit @code{UnitSimpRules}.  @xref{Simplification of Units}.
  1085. @item s X
  1086. Edit @code{ExtSimpRules}.  @xref{Unsafe Simplifications}.
  1087. @end table
  1088. X
  1089. These commands are just versions of @kbd{s e} that use fixed variable
  1090. names rather than prompting for the variable name.
  1091. X
  1092. @kindex s p
  1093. @pindex calc-permanent-variable
  1094. @cindex Storing variables
  1095. @cindex Permanent variables
  1096. @cindex @file{.emacs} file, veriables
  1097. The @kbd{s p} (@code{calc-permanent-variable}) command saves a
  1098. variable's value permanently in your @file{.emacs} file, so that its
  1099. value will still be available in future Emacs sessions.  You can
  1100. re-execute @kbd{s p} later on to update the saved value, but the
  1101. only way to remove a saved variable is to edit your @file{.emacs} file
  1102. by hand.  (@xref{General Mode Commands}, for a way to tell Calc to
  1103. use a different file instead of @file{.emacs}.)
  1104. X
  1105. If you do not specify the name of a variable to save (i.e.,
  1106. @kbd{s p @key{RET}}), all @samp{var-} variables with defined values
  1107. are saved except for the special constants @code{pi}, @code{e},
  1108. @code{i}, @code{phi}, and @code{gamma}; the variables @code{TimeZone}
  1109. and @code{PlotRejects};
  1110. @code{FitRules}, @code{DistribRules}, and other built-in rewrite
  1111. rules; and @code{PlotData@var{n}} variables generated
  1112. by the graphics commands.  (You can still save these variables by
  1113. explicitly naming them in an @kbd{s p} command.)@refill
  1114. X
  1115. @kindex s i
  1116. @pindex calc-insert-variables
  1117. The @kbd{s i} (@code{calc-insert-variables}) command writes
  1118. the values of all @samp{var-} variables into a specified buffer.
  1119. The variables are written in the form of Lisp @code{setq} commands
  1120. which store the values in string form.  You can place these commands
  1121. in your @file{.emacs} buffer if you wish, though in this case it
  1122. would be easier to use @kbd{s p @key{RET}}.  (Note that @kbd{s i}
  1123. omits the same set of variables as @kbd{s p @key{RET}}; the difference
  1124. is that @kbd{s i} will store the variables in any buffer, and it also
  1125. stores in a more human-readable format.)
  1126. X
  1127. @node Let Command, Evaluates-To Operator, Operations on Variables, Store and Recall
  1128. @section The Let Command
  1129. X
  1130. @noindent
  1131. @kindex s l
  1132. @pindex calc-let
  1133. @cindex Variables, temporary assignment
  1134. @cindex Temporary assignment to variables
  1135. If you have an expression like @samp{a+b^2} on the stack and you wish to
  1136. compute its value where @cite{b=3}, you can simply store 3 in @cite{b} and
  1137. then press @kbd{=} to reevaluate the formula.  This has the side-effect
  1138. of leaving the stored value of 3 in @cite{b} for future operations.
  1139. X
  1140. The @kbd{s l} (@code{calc-let}) command evaluates a formula under a
  1141. @emph{temporary} assignment of a variable.  It stores the value on the
  1142. top of the stack into the specified variable, then evaluates the
  1143. second-to-top stack entry, then restores the original value (or lack of
  1144. one) in the variable.  Thus after @kbd{' a+b^2 @key{RET} 3 s l b @key{RET}},
  1145. the stack will contain the formula @samp{a + 9}.  The subsequent command
  1146. @kbd{5 s l a @key{RET}} will replace this formula with the number 14.  The
  1147. variables @samp{a} and @samp{b} are not permanently affected in any way
  1148. by these commands.
  1149. X
  1150. The value on the top of the stack may be an equation or assignment, or
  1151. vector of equations or assignments, in which case the default will be
  1152. analogous to the case of @kbd{s t @key{RET}}.  @xref{Storing Variables}.
  1153. X
  1154. Also, you can answer the variable-name prompt with an equation or
  1155. assignment:  @kbd{s l b=3 RET} is the same as storing 3 on the stack
  1156. and typing @kbd{s l b RET}.
  1157. X
  1158. The @kbd{a b} (@code{calc-substitute}) is another way to substitute
  1159. a variable with a value in a formula.  It does an actual substitution
  1160. rather than temporarily assigning the variable and evaluating.  For
  1161. example, letting @cite{n=2} in @samp{f(n pi)} with @kbd{a b} will
  1162. produce @samp{f(2 pi)}, whereas @kbd{s l} would give @samp{f(6.28)}
  1163. since the evaluation step will also evaluate @code{pi}.
  1164. X
  1165. @node Evaluates-To Operator, , Let Command, Store and Recall
  1166. @section The Evaluates-To Operator
  1167. X
  1168. @noindent
  1169. @tindex evalto
  1170. @tindex =>
  1171. @cindex Evaluates-to operator
  1172. @cindex @samp{=>} operator
  1173. The special algebraic symbol @samp{=>} is known as the @dfn{evaluates-to
  1174. operator}.  (It will show up as an @code{evalto} function call in
  1175. other language modes like Pascal and @TeX{}.)  This is a binary
  1176. operator, that is, it has a lefthand and a righthand argument,
  1177. although it can be entered with the righthand argument omitted.
  1178. X
  1179. A formula like @samp{@var{a} => @var{b}} is evaluated by Calc as
  1180. follows:  First, @var{a} is not simplified or modified in any
  1181. way.  The previous value of argument @var{b} is thrown away; the
  1182. formula @var{a} is then copied and evaluated as if by the @kbd{=}
  1183. command according to all current modes and stored variable values,
  1184. and the result is installed as the new value of @var{b}.
  1185. X
  1186. For example, suppose you enter the algebraic formula @samp{2 + 3 => 17}.
  1187. The number 17 is ignored, and the lefthand argument is left in its
  1188. unevaluated form; the result is the formula @samp{2 + 3 => 5}.
  1189. X
  1190. @kindex s =
  1191. @pindex calc-evalto
  1192. You can enter an @samp{=>} formula either directly using algebraic
  1193. entry (in which case the righthand side may be omitted since it is
  1194. SHAR_EOF
  1195. true || echo 'restore of calc.texinfo failed'
  1196. fi
  1197. echo 'End of  part 49'
  1198. echo 'File calc.texinfo is continued in part 50'
  1199. echo 50 > _shar_seq_.tmp
  1200. exit 0
  1201. exit 0 # Just in case...
  1202. -- 
  1203. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1204. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1205. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1206. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1207.