home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume13 / gmcalc / part17 < prev    next >
Encoding:
Text File  |  1990-06-05  |  57.8 KB  |  1,227 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@csvax.caltech.edu (David Gillespie)
  3. Subject: v13i043: Emacs Calculator 1.01, part 17/19
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 13, Issue 43
  7. Submitted-by: daveg@csvax.caltech.edu (David Gillespie)
  8. Archive-name: gmcalc/part17
  9.  
  10. ---- Cut Here and unpack ----
  11. #!/bin/sh
  12. # this is part 17 of a multipart archive
  13. # do not concatenate these parts, unpack them in order with /bin/sh
  14. # file calc.texinfo continued
  15. #
  16. CurArch=17
  17. if test ! -r s2_seq_.tmp
  18. then echo "Please unpack part 1 first!"
  19.      exit 1; fi
  20. ( read Scheck
  21.   if test "$Scheck" != $CurArch
  22.   then echo "Please unpack part $Scheck next!"
  23.        exit 1;
  24.   else exit 0; fi
  25. ) < s2_seq_.tmp || exit 1
  26. echo "x - Continuing file calc.texinfo"
  27. sed 's/^X//' << 'SHAR_EOF' >> calc.texinfo
  28. Xuse these rules often, we could define the above sequence of keystrokes
  29. Xas a keyboard macro.  We could then assign this macro to a single
  30. Xkey sequence which effectively means, ``simplify all calls to the function
  31. X@samp{f} on top-of-stack.''  @xref{Keyboard Macros}.@refill
  32. X
  33. X@node Logical Operations, Calculus, Rewrite Rules, Algebra
  34. X@section Logical Operations
  35. X
  36. XThe following commands and algebraic functions return true/false values,
  37. Xwhere 1 represents ``true'' and 0 represents ``false.''  In cases where
  38. Xa truth value is required (such as for the condition part of a rewrite
  39. Xrule), any nonzero number is accepted to mean ``true.''
  40. X
  41. X@kindex a =
  42. X@pindex calc-equal-to
  43. X@tindex eq
  44. XThe @kbd{a =} (@code{calc-equal-to}) command, or @samp{eq(a,b)} function
  45. X(which can also be written @samp{a = b} or @samp{a == b} in an algebraic
  46. Xformula) is true if @samp{a} and @samp{b} are equal, either because they
  47. Xare identical expressions, or because they are numbers which are
  48. Xnumerically equal.  (Thus the integer 1 is considered equal to the float
  49. X1.0.)  If the equality of @samp{a} and @samp{b} cannot be determined,
  50. Xthe comparison is left in symbolic form.  Note that as a command, this
  51. Xoperation pops two values from the stack and pushes back either a 1 or
  52. Xa 0, or a formula @samp{a = b} if the values' equality cannot be determined.
  53. X
  54. XNote that @kbd{C-u 3 a =} or @samp{a = b = c} does not return true if
  55. X@samp{a}, @samp{b}, and @samp{c} are equal.  (It is interpreted as
  56. X@samp{(a = b) = c}, and returns true if @samp{c} is 1 when @samp{a} and
  57. X@samp{b} are equal, or 0 when they are unequal.)  Only negative prefix
  58. Xarguments are likely to be useful with @kbd{a =} (@pxref{Prefix Arguments}).
  59. XThe same is true of the other relational operators described below.
  60. X
  61. X@kindex a #
  62. X@pindex calc-not-equal-to
  63. X@tindex neq
  64. XThe @kbd{a #} (@code{calc-not-equal-to}) command, or @samp{neq(a,b)} or
  65. X@samp{a != b} function, is true if @samp{a} and @samp{b} are not equal.
  66. X
  67. X@kindex a <
  68. X@kindex a >
  69. X@kindex a [
  70. X@kindex a ]
  71. X@pindex calc-less-than
  72. X@pindex calc-greater-than
  73. X@pindex calc-less-equal
  74. X@pindex calc-greater-equal
  75. X@tindex lt
  76. X@tindex gt
  77. X@tindex leq
  78. X@tindex geq
  79. XThe @kbd{a <} (@code{calc-less-than}) [@samp{lt(a,b)} or @samp{a < b}]
  80. Xoperation is true if @samp{a} is less than @samp{b}.  Similar functions
  81. Xare @kbd{a >} (@code{calc-greater-than}) [@samp{gt(a,b)} or @samp{a > b}],
  82. X@kbd{a [} (@code{calc-less-equal}) [@samp{leq(a,b)} or @samp{a <= b}], and
  83. X@kbd{a ]} (@code{calc-greater-equal}) [@samp{geq(a,b)} or @samp{a >= b}].
  84. X
  85. X@kindex a &
  86. X@pindex calc-logical-and
  87. X@tindex land
  88. XThe @kbd{a &} (@code{calc-logical-and}) [@samp{land(a,b)} or @samp{a && b}]
  89. Xfunction is true if both of its arguments are true, i.e., are
  90. Xnon-zero numbers.  In this case, the result will be either @samp{a} or
  91. X@samp{b}, chosen arbitrarily.  If either argument is zero, the result is
  92. Xzero.  Otherwise, the formula is left in symbolic form.
  93. X
  94. X@kindex a |
  95. X@pindex calc-logical-or
  96. X@tindex lor
  97. XThe @kbd{a |} (@code{calc-logical-or}) [@samp{lor(a,b)} or @samp{a || b}]
  98. Xfunction is true if either of its arguments is true (nonzero).  The
  99. Xresult is whichever argument was nonzero, choosing arbitrarily if both
  100. Xare nonzero.  If both @samp{a} and @samp{b} are zero, the result is
  101. Xzero.
  102. X
  103. X@kindex a !
  104. X@pindex calc-logical-not
  105. X@tindex lnot
  106. XThe @kbd{a !} (@code{calc-logical-not}) [@samp{lnot(a)} or @samp{! a}]
  107. Xfunction is true if @samp{a} is false (zero), or false if @samp{a} is
  108. Xtrue (nonzero).  It is left in symbolic form if @samp{a} is not a
  109. Xnumber.
  110. X
  111. X@tindex if
  112. XThe @samp{if(a,b,c)} or @samp{a ? b : c} function is equal to either
  113. X@samp{b} or @samp{c} if @samp{a} is a nonzero number or zero, respectively.
  114. XIf @samp{a} is not a number, the test is left in symbolic form and
  115. Xneither @samp{b} nor @samp{c} is evaluated in any way.  This is the
  116. Xonly Calc function whose arguments are not automatically evaluated
  117. Xwhen the function itself is evaluated.
  118. X
  119. X@kindex a @{
  120. X@pindex calc-in-set
  121. X@tindex in
  122. XThe @kbd{a @{} (@code{calc-in-set}) [@samp{in(a,b)}] function is true if
  123. Xthe number @samp{a} is in the set of numbers represented by @samp{b}.
  124. XIf @samp{b} is an interval form, @samp{a} must be one of the values
  125. Xencompassed by the interval.  If @samp{b} is a vector, @samp{a} must be
  126. Xequal to one of the elements of the vector.  (If any vector elements are
  127. Xintervals, @samp{a} must be in any of the intervals.)  If @samp{b} is a
  128. Xplain number, @samp{a} must be numerically equal to @samp{b}.
  129. X
  130. X@tindex typeof
  131. XThe @samp{typeof(a)} function produces an integer or variable which
  132. Xcharacterizes @samp{a}.  If @samp{a} is a number, vector, or variable,
  133. Xthe result will be one of the following numbers:
  134. X
  135. X@example
  136. X 1   Integer
  137. X 2   Fraction
  138. X 3   Floating-point number
  139. X 4   HMS form
  140. X 5   Rectangular complex number
  141. X 6   Polar complex number
  142. X 7   Error form
  143. X 8   Interval form
  144. X 9   Modulo form
  145. X100  Variable
  146. X101  Vector (but not a matrix)
  147. X102  Matrix
  148. X@end example
  149. X
  150. XOtherwise, @samp{a} is a formula, and the result is a variable which
  151. Xrepresents name of the top-level function call.
  152. X
  153. X@tindex integer
  154. X@tindex real
  155. X@tindex constant
  156. XThe @samp{integer(a)} function returns true if @samp{a} is an integer.
  157. XThis is shorthand for @samp{typeof(a) = 1}.  The @samp{real(a)} function
  158. Xis true if @samp{a} is a real number, either integer, fraction, or
  159. Xfloat.  The @samp{constant(a)} function returns true if @samp{a} is
  160. Xany of the objects for which @code{typeof} would produce an integer
  161. Xcode result except for variables, and provided that the components of
  162. Xan object like a vector or error form are themselves constant.
  163. X
  164. X@tindex refers
  165. XThe @samp{refers(a,b)} function is true if the variable (or sub-expression)
  166. X@samp{b} appears in @samp{a}, or false otherwise.  Unlike the other
  167. Xtests described here, this function returns a definite ``no'' answer
  168. Xeven if its arguments are still in symbolic form.  The only case where
  169. X@code{refers} will be left unevaluated is if @samp{a} is a plain
  170. Xvariable (different from @samp{b}).
  171. X
  172. X@node Calculus, , Logical Operations, Algebra
  173. X@section Calculus
  174. X
  175. XThe following calculus commands do not automatically simplify their
  176. Xinputs or outputs using @code{calc-simplify}.  You may find it helps
  177. Xto do this by hand by typing @kbd{a s} or @kbd{a e}.  It may also help
  178. Xto use @kbd{a x} and/or @kbd{a c} to arrange a result in the most
  179. Xreadable way.
  180. X
  181. X@kindex a d
  182. X@kindex H a d
  183. X@pindex calc-derivative
  184. X@tindex deriv
  185. X@tindex tderiv
  186. XThe @kbd{a d} (@code{calc-derivative}) [@code{deriv}] command computes
  187. Xthe derivative of the expression on the top of the stack with respect to
  188. Xsome variable, which it will prompt you to enter.  Normally, variables
  189. Xin the formula other than the specified differentiation variable are
  190. Xconsidered constant, i.e., @samp{deriv(y,x)} is reduced to zero.  With
  191. Xthe Hyperbolic flag, the @code{tderiv} (total derivative) operation is used
  192. Xinstead, in which derivatives of variables are not reduced to zero.
  193. X
  194. XIf you use the @code{deriv} function directly in an algebraic formula,
  195. Xyou can also write @samp{deriv(f,x,x0)} which represents the derivative
  196. Xof @code{f} with respect to @code{x}, evaluated at the point @samp{x=x0}.
  197. X
  198. XIf the formula being differentiated contains functions which Calc does
  199. Xnot know, the derivatives of those functions are produced by adding
  200. Xprimes (apostrophe characters).  For example, @samp{deriv(f(2x), x)}
  201. Xproduces @samp{2 f'(2 x)}, where the function @code{f'} represents the
  202. Xderivative of @code{f}.  If you enter a definition for @code{f'}, for
  203. Xexample using the @kbd{Z F} (@code{calc-user-define-formula}) command,
  204. Xthe Calculator will use it.  @xref{Algebraic Definitions}.
  205. X
  206. XFor multi-argument functions @samp{f(x,y,z)}, the derivative with respect
  207. Xto the first argument is written @samp{f'(x,y,z)}; derivatives with
  208. Xrespect to the other arguments are @samp{f'2(x,y,z)} and @samp{f'3(x,y,z)}.
  209. XVarious higher-order derivatives can be formed in the obvious way, e.g.,
  210. X@samp{f'@var{}'(x)} (the second derivative of @code{f}) or
  211. X@samp{f'@var{}'2'3(x,y,z)} (@code{f} differentiated with respect to each
  212. Xargument once).@refill
  213. X
  214. X@kindex a i
  215. X@pindex calc-integral
  216. X@tindex integ
  217. XThe @kbd{a i} (@code{calc-integral}) [@code{integ}] command computes the
  218. Xindefinite integral of the expression on the top of the stack with
  219. Xrespect to a variable.  The integrator is not guaranteed to work for
  220. Xall integrable functions, but it will work for any polynomial and for
  221. Xmost simple functions.
  222. X
  223. XIf you use the @code{integ} function directly in an algebraic formula,
  224. Xyou can also write @samp{integ(f,x,v)} which expresses the resulting
  225. Xindefinite integral in terms of variable @code{v} instead of @code{x},
  226. Xor @samp{integ(f,x,a,b)}, which represents a definite integral from
  227. X@code{a} to @code{b}.
  228. X
  229. XThe Calculator remembers all the integrals it has done.  If conditions
  230. Xchange in a way that would invalidate the old integrals, they will be
  231. Xthrown out.  If you suspect this is not happening when it should,
  232. Xuse the @code{calc-flush-caches} command; @pxref{Caches}.
  233. X
  234. X@kindex a I
  235. X@pindex calc-integral-limit
  236. XThe @kbd{a I} (@code{calc-integral-limit}) command displays or, with a
  237. Xnumeric prefix argument, sets the current ``integration nesting limit.''
  238. XThis is the depth to which Calc will pursue integration by substitution
  239. Xor integration by parts before abandoning an approach as fruitless.
  240. XThe default value is 3.  If the integrator is taking too long, you
  241. Xmight consider lowering this limit.  However, the lower this limit is,
  242. Xthe greater the chance that Calc will be unable to integrate a function
  243. Xit could otherwise handle.  Raising this limit allows the Calculator to
  244. Xsolve more integrals, though the time it takes may grow exponentially.
  245. XYou can monitor the integrator's actions by creating an Emacs buffer called
  246. X@code{*Trace*}.  If such a buffer exists, the @kbd{a i} command will write
  247. Xa log of its actions there.
  248. X
  249. X@kindex a t
  250. X@pindex calc-taylor
  251. X@tindex taylor
  252. XThe @kbd{a t} (@code{calc-taylor}) [@code{taylor}] command computes a
  253. Xpower series expansion or Taylor series of a function.  You specify the
  254. Xvariable and the desired number of terms.  You may give an expression of
  255. Xthe form @samp{@var{var} = @var{a}} or @samp{@var{var} - @var{a}} to
  256. Xproduce a Taylor expansion about the point @var{a}.  You may specify the
  257. Xnumber of terms with a numeric prefix argument; otherwise the command
  258. Xwill prompt you for the number of terms.  Note that many series
  259. Xexpansions have coefficients of zero for some terms, so you may appear
  260. Xto get fewer terms than you asked for.@refill
  261. X
  262. XIf the @kbd{a i} command is unable to find a symbolic integral for a
  263. Xfunction, you can get an approximation by integrating the function's
  264. XTaylor series.
  265. X
  266. X@kindex a S
  267. X@pindex calc-solve-for
  268. X@tindex solve
  269. XThe @kbd{a S} (@code{calc-solve-for}) [@code{solve}] command rearranges
  270. Xan equation to solve for a specific variable.  An equation is an
  271. Xexpression of the form @samp{L = R}.  If the input is not an equation,
  272. Xit is treated like an equation of the form @samp{X = 0}.  For example,
  273. Xthe command @kbd{a S x} will rearrange @samp{y = 3x + 6} to the form,
  274. X@samp{x = y/3 - 2}.
  275. X
  276. X@kindex H a S
  277. X@tindex fsolve
  278. XSome equations have more than one solution.  The Hyperbolic flag
  279. X(@code{H a S}) [@code{fsolve}] tells the solver to report the fully
  280. Xgeneral family of solutions.  It will invent variables @code{n1},
  281. X@code{n2}, @dots{}, which represent independent arbitrary integers, and
  282. X@code{s1}, @code{s2}, @dots{}, which represent independent arbitrary
  283. Xsigns (either @samp{+1} or @samp{-1}).  If you don't use the Hyperbolic
  284. Xflag, Calc will use zero in place of all arbitrary integers, and plus
  285. Xone in place of all arbitrary signs.  Note that variables like @code{n1}
  286. Xand @code{s1} are not given any special interpretation in Calc except by
  287. X@code{calc-solve-for} itself.  As usual, you can use the @code{calc-let}
  288. Xcommand to obtain solutions for various actual values of these
  289. Xvariables.
  290. X
  291. X@kindex I a S
  292. X@kindex H I a S
  293. X@tindex finv
  294. X@tindex ffinv
  295. XWith the Inverse flag, @kbd{I a S} [@code{finv}] treats the expression
  296. Xon top of the stack as a function of the specified variable and solves
  297. Xto find the inverse function, written in terms of the same variable.
  298. XFor example, @kbd{I a S x} inverts @samp{2x + 6} to @samp{x/2 - 3}.
  299. XYou can use both Inverse and Hyperbolic [@code{ffinv}] to obtain a
  300. Xfully general inverse, as described above.
  301. X
  302. X@node Units, Store and Recall, Algebra, Top
  303. X@chapter Operating on Units
  304. X
  305. XOne special interpretation of formulas is as numbers with units.
  306. XFor example, the formula @samp{5 m / s^2} can be read ``five meters
  307. Xper second squared.''  The commands in this chapter help you
  308. Xmanipulate units expressions in this form.  Units-related commands
  309. Xbegin with the @kbd{u} prefix key.
  310. X
  311. X@menu
  312. X* Basic Operations on Units::
  313. X* The Units Table::
  314. X* Predefined Units::
  315. X* User-Defined Units::
  316. X@end menu
  317. X
  318. X@node Basic Operations on Units, The Units Table, Units, Units
  319. X@section Basic Operations on Units
  320. X
  321. XA @dfn{units expression} is a formula which is basically a number
  322. Xmultiplied and/or divided by one or more @dfn{unit names}, which may
  323. Xoptionally be raised to powers.  Actually, the value part need not be
  324. Xa number; any product or quotient involving unit names is a unit
  325. Xexpression.  Many of the units commands will also accept any formula,
  326. Xwhere the command applies to all units expressions which appear in the
  327. Xformula.
  328. X
  329. XA unit name is a variable whose name appears in the @dfn{unit table},
  330. Xor a variable whose name is a prefix character like @samp{k} (for ``kilo'')
  331. Xor @samp{u} (for ``micro'') followed by a name from the unit table.
  332. XA substantial table of built-in units is provided with Calc;
  333. X@pxref{Predefined Units}.  You can also define your own unit names;
  334. X@pxref{User-Defined Units}.@refill
  335. X
  336. XNote that if the value part of a units expression is exactly @samp{1},
  337. Xit will be removed by the Calculator's automatic algebra routines:  The
  338. Xformula @samp{1 mm} is ``simplified'' to @samp{mm}.  This is only a
  339. Xdisplay anomaly, however; @samp{mm} will work just fine as a
  340. Xrepresentation of one millimeter.@refill
  341. X
  342. XYou may find that Algebraic Mode (@pxref{Algebraic Entry}) makes working
  343. Xwith units expressions easier.  Otherwise, you will have to remember
  344. Xto hit the apostrophe key every time you wish to enter units.
  345. X
  346. X@kindex u s
  347. X@pindex calc-simplify-units
  348. XThe @kbd{u s} (@code{calc-simplify-units}) command simplifies a units
  349. Xexpression.  It uses @kbd{a s} (@code{calc-simplify}) to simplify the
  350. Xexpression first as a regular algebraic formula; it then looks for
  351. Xfeatures that can be further simplified by converting one object's units
  352. Xto be compatible with another's.  For example, @samp{5 m + 23 mm} will
  353. Xsimplify to @samp{5.023 m}.  When different but compatible units are
  354. Xadded, the righthand term's units are converted to match those of the
  355. Xlefthand term.  @xref{Simplification Modes}, for a way to have this done
  356. Xautomatically at all times.
  357. X
  358. XUnits simplification also handles quotients of two units with the same
  359. Xdimensionality, as in @samp{2 in s/L cm} to @samp{5.08 s/L}; fractional
  360. Xpowers of unit expressions, as in @samp{sqrt(9 mm^2)} to @samp{3 mm} and
  361. X@samp{sqrt(9 acre)} to a quantity in meters; and @code{floor},
  362. X@code{ceil}, @code{round}, @code{trunc}, @code{float}, @code{frac},
  363. X@code{abs}, and @code{clean} applied to units expressions, in which case
  364. Xthe operation in question is applied only to the numeric part of the
  365. Xexpression.  Finally, trigonometric functions of quantities with units
  366. Xof angle are evaluated, regardless of the current angular mode.@refill
  367. X
  368. X@kindex u c
  369. X@pindex calc-convert-units
  370. XThe @kbd{u c} (@code{calc-convert-units}) command converts a units
  371. Xexpression to new, compatible units.  For example, given the units
  372. Xexpression @samp{55 mph}, typing @kbd{u c m/s @key{RET}} produces
  373. X@samp{24.5872 m/s}.  If the units you request are inconsistent with
  374. Xthe original units, the number will be converted into your units
  375. Xtimes whatever ``remainder'' units are left over.  For example,
  376. Xconverting @samp{55 mph} into acres produces @samp{6.08e-3 acre / m s}.
  377. X(Recall that multiplication binds more strongly than division in Calc
  378. Xformulas, so the units here are acres per meter-second.)  Remainder
  379. Xunits are expressed in terms of ``fundamental'' units like @samp{m} and
  380. X@samp{s}, regardless of the input units.
  381. X
  382. XYou can use explicit unit conversion instead of the @kbd{u s} command
  383. Xto gain more control over the units of the result of an expression.
  384. XFor example, given @samp{5 m + 23 mm}, you can type @kbd{u c m} or
  385. X@kbd{u c mm} to express the result in either meters or millimeters.
  386. X(For that matter, you could type @kbd{u c fath} to express the result
  387. Xin fathoms, if you preferred!)
  388. X
  389. XIn place of a specific set of units, you can also enter one of the
  390. Xunits system names @code{si}, @code{mks} (equivalent), or @code{cgs}.
  391. XFor example, @kbd{u c si @key{RET}} converts the expression into
  392. XInternational System of Units (SI) base units.  Also, @code{base}
  393. Xconverts to Calc's base units, which are the same as @code{si} units
  394. Xexcept that @code{base} uses @samp{g} as the fundamental unit of mass
  395. Xwhereas @code{si} uses @samp{kg}.
  396. X
  397. XIf the value on the stack does not contain any units, @kbd{u c} will
  398. Xprompt first for the old units which this value should be considered
  399. Xto have, then for the new units.
  400. X
  401. X@kindex u b
  402. X@pindex calc-base-units
  403. XThe @kbd{u b} (@code{calc-base-units}) command is shorthand for
  404. X@kbd{u c base}; it converts the units expression on the top of the
  405. Xstack into @code{base} units.  If @kbd{u s} does not simplify a
  406. Xunits expression as far as you would like, try @kbd{u b}.
  407. X
  408. XThe @kbd{u c} and @kbd{u b} commands treat temperature units (like
  409. X@samp{degC} and @samp{K}) as relative temperatures.  For example,
  410. X@kbd{u c} converts @samp{10 degC} to @samp{18 degF}: A change of 10
  411. Xdegrees Celsius corresponds to a change of 18 degrees Fahrenheit.
  412. X
  413. X@kindex u t
  414. X@pindex calc-convert-temperature
  415. X@cindex Temperature conversion
  416. XThe @kbd{u t} (@code{calc-convert-temperature}) command converts
  417. Xabsolute temperatures.  The value on the stack must be a simple units
  418. Xexpression with units of temperature only.  This command would convert
  419. X@samp{10 degC} to @samp{50 degF}, the equivalent temperature on the
  420. XFahrenheit scale.@refill
  421. X
  422. X@kindex u r
  423. X@pindex calc-remove-units
  424. X@kindex u x
  425. X@pindex calc-extract-units
  426. XThe @kbd{u r} (@code{calc-remove-units}) command removes units from the
  427. Xformula at the top of the stack.  The @kbd{u x}
  428. X(@code{calc-extract-units}) command extracts only the units portion of a
  429. Xformula.  These commands essentially replace every term of the formula
  430. Xthat does or doesn't (respectively) look like a unit name by the
  431. Xconstant 1, then resimplify the formula.@refill
  432. X
  433. X@node The Units Table, Predefined Units, Basic Operations on Units, Units
  434. X@section The Units Table
  435. X
  436. X@kindex u v
  437. X@pindex calc-enter-units-table
  438. XThe @kbd{u v} (@code{calc-enter-units-table}) command displays the units table
  439. Xin another buffer called @code{*Units Table*}.  Each entry in this table
  440. Xgives the unit name as it would appear in an expression, the definition
  441. Xof the unit in terms of simpler units, and a full name or description of
  442. Xthe unit.  Fundamental units are defined as themselves; these are the
  443. Xunits produced by the @kbd{u b} command.  The fundamental units are
  444. Xmeters, seconds, grams, kelvins, amperes, candelas, moles, radians,
  445. Xand steradians.
  446. X
  447. XThe Units Table buffer also displays the Unit Prefix Table.  Note that
  448. Xtwo prefixes, ``kilo'' and ``hecto,'' accept either upper- or lower-case
  449. Xprefix letters.  @samp{Meg} is also accepted as a synonym for the @samp{M}
  450. Xprefix.  Whenever a unit name can be interpreted as either a built-in name
  451. Xor a prefix followed by another built-in name, the former interpretation
  452. Xwins.  For example, @samp{2 pt} means two pints, not two pico-tons.
  453. X
  454. XThe Units Table buffer, once created, is not rebuilt unless you define
  455. Xnew units.  To force the buffer to be rebuilt, give any numeric prefix
  456. Xargument to @kbd{u v}.
  457. X
  458. X@kindex u V
  459. X@pindex calc-view-units-table
  460. XThe @kbd{u V} (@code{calc-enter-units-table}) command is like @kbd{u v} except
  461. Xthat the cursor is not moved into the Units Table buffer.  To return
  462. Xfrom the Units Table buffer after a @kbd{u v}, type @kbd{M-x calc}
  463. Xagain or use the regular Emacs @kbd{C-x o} (@code{other-window})
  464. Xcommand.  You can also kill the buffer with @kbd{C-x k} if you wish;
  465. Xthe actual units table is safely stored inside the Calculator.
  466. X
  467. X@kindex u g
  468. X@pindex calc-get-unit-definition
  469. XThe @kbd{u g} (@code{calc-get-unit-definition}) command retrieves a unit's
  470. Xdefining expression and pushes it onto the Calculator stack.  For example,
  471. X@kbd{u g in} will produce the expression @samp{2.54 cm}.  This is the
  472. Xsame definition for the unit that would appear in the Units Table buffer.
  473. XNote that this command works only for actual unit names; @kbd{u g km}
  474. Xwill report that no such unit exists, for example, because @code{km} is
  475. Xreally the unit @code{m} with a @code{k} (``kilo'') prefix.  To see a
  476. Xdefinition of a unit in terms of base units, it is easier to push the
  477. Xunit name on the stack and then reduce it to base units with @kbd{u b}.
  478. X
  479. X@kindex u e
  480. X@pindex calc-explain-units
  481. XThe @kbd{u e} (@code{calc-explain-units}) command displays an English
  482. Xdescription of the units of the expression on the stack.  For example,
  483. Xfor the expression @samp{62 km^2 g / s^2 mol K}, the description is
  484. X``Square-Kilometer Gram per (Second-squared Mole Degree-Kelvin).''  This
  485. Xcommand uses the English descriptions that appear in the righthand
  486. Xcolumn of the Units Table.
  487. X
  488. X@node Predefined Units, User-Defined Units, The Units Table, Units
  489. X@section Predefined Units
  490. X
  491. XSince the exact definitions of many kinds of units have evolved over the
  492. Xyears, and since certain countries sometimes have local differences in
  493. Xtheir definitions, it is a good idea to examine Calc's definition of a
  494. Xunit before depending on its exact value.  For example, there are three
  495. Xdifferent units for gallons, corresponding to the US, Canadian, and
  496. XBritish definitions.  Also, note that @code{oz} is a standard ounce of
  497. Xmass, @code{ozt} is a Troy ounce, and @code{ozfl} is a fluid ounce.
  498. X
  499. XThe temperature units corresponding to degrees Kelvin and Centigrade
  500. X(Celsius) are the same in this table, since most units commands treat
  501. Xtemperatures as being relative.  The @code{calc-convert-temperature}
  502. Xcommand has special rules for handling the different absolute magnitudes
  503. Xof the various temperature scales.
  504. X
  505. XSome ``units'' are really physical constants; for example, @code{c}
  506. Xrepresents the speed of light, and @code{h} represents Planck's
  507. Xconstant.  You can use these just like other units: converting
  508. X@samp{.5 c} to @samp{m/s} expresses one-half the speed of light in
  509. Xmeters per second.  You can also use this merely as a handy reference;
  510. Xthe @kbd{u g} command gets the definition of one of these constants
  511. Xin its normal terms, and @kbd{u b} expresses the definition in base
  512. Xunits.
  513. X
  514. XTwo units, @code{pi} and @code{fsc} (the fine structure constant) are
  515. Xdimensionless.  The units simplification commands simply treat these
  516. Xnames as equivalent to their corresponding values.  However you can, for
  517. Xexample, use @kbd{u c} to convert a pure number into multiples of the
  518. Xfine structure constant, or @kbd{u b} to convert this back into a pure
  519. Xnumber.  (When @kbd{u c} prompts for the ``old units,'' just enter a
  520. Xblank line to signify that the value really is unitless.)
  521. X
  522. X@c Describe angular units, luminosity vs. steradians problem.
  523. X
  524. X@node User-Defined Units, , Predefined Units, Units
  525. X@section User-Defined Units
  526. X
  527. X@kindex u d
  528. X@pindex calc-define-unit
  529. X@cindex User-defined units
  530. XThe @kbd{u d} (@code{calc-define-unit}) command records the units
  531. Xexpression on the top of the stack as the definition for a new,
  532. Xuser-defined unit.  For example, putting @samp{16.5 ft} on the stack and
  533. Xtyping @kbd{u d rod} defines the new unit @samp{rod} to be equivalent to
  534. X16.5 feet.  The unit conversion and simplification commands will now
  535. Xtreat @code{rod} just like any other unit of length.  You will also be
  536. Xprompted for an optional English description of the unit, which will
  537. Xappear in the Units Table.
  538. X
  539. X@kindex u u
  540. X@pindex calc-undefine-unit
  541. XThe @kbd{u u} (@code{calc-undefine-unit}) command removes a user-defined
  542. Xunit.  It is not possible to remove one of the predefined units,
  543. Xhowever.
  544. X
  545. XIf you define a unit with an existing unit name, your new definition
  546. Xwill replace the original definition of that unit.  If the unit was a
  547. Xpredefined unit, the old definition will not be replaced, only
  548. X``shadowed.''  The built-in definition will reappear if you later use
  549. X@kbd{u u} to remove the shadowing definition.
  550. X
  551. XTo create a new fundamental unit, use either 1 or the unit name itself
  552. Xas the defining expression.  Otherwise the expression can involve any
  553. Xother units that you like.  The next unit operation like @kbd{u c} or @kbd{u v}
  554. Xwill rebuild the internal unit table incorporating your modifications.
  555. XNote that erroneous definitions (such as two units defined in terms of
  556. Xeach other) will not be detected until the unit table is next rebuilt;
  557. X@kbd{u v} is a convenient way to cause this to happen.
  558. X
  559. XTemperature units are treated specially inside the Calculator; it is not
  560. Xpossible to create user-defined temperature units.
  561. X
  562. X@kindex u p
  563. X@pindex calc-permanent-units
  564. X@cindex @file{.emacs} file, user-defined units
  565. XThe @kbd{u p} (@code{calc-permanent-units}) command stores the user-defined
  566. Xunits in your @file{.emacs} file, so that the units will still be
  567. Xavailable in subsequent Emacs sessions.  If there was already a set of
  568. Xuser-defined units in your @file{.emacs} file, it is replaced by the
  569. Xnew set.
  570. X
  571. X@node Store and Recall, Kill and Yank, Units, Top
  572. X@chapter Storing and Recalling Variables
  573. X
  574. XCalculator variables are really just Lisp variables that contain numbers
  575. Xor formulas in a form that Calc can understand.  The commands in this
  576. Xsection allow you to manipulate variables conveniently.
  577. X
  578. X@menu
  579. X* Storing Variables::
  580. X* Recalling Variables::
  581. X* Let Command::
  582. X@end menu
  583. X
  584. X@node Storing Variables, Recalling Variables, Store and Recall, Store and Recall
  585. X@section Storing Variables
  586. X
  587. X@kindex s
  588. X@pindex calc-store
  589. X@cindex Storing variables
  590. X@cindex Quick variables
  591. XThe @kbd{s} (@code{calc-store}) command stores the value at the top of
  592. Xthe stack into a specified variable.  It prompts you to enter the
  593. Xname of the variable.  If you press a single digit, the value is stored
  594. Ximmediately in one of the ``quick'' variables @code{var-0} through
  595. X@code{var-9}.  Or you can enter any variable name.  The prefix @samp{var-}
  596. Xis supplied for you; when a name appears in a formula (as in @samp{a+b})
  597. Xthe prefix @samp{var-} is also supplied there, so normally you can simply
  598. Xforget about @samp{var-} everywhere.  Its only purpose is to enable you to
  599. Xuse Calc variables without fear of accidentally clobbering some variable in
  600. Xanother program.  If you really want to store in an arbitrary Lisp variable,
  601. Xjust backspace over the @samp{var-}.
  602. X
  603. XThe value stored generally replaces any old contents of the variable,
  604. Xand also remains on the stack.  (The @key{DEL} key can then be used to
  605. Xremove it from the stack if you wish.)  If you press one of the operator
  606. Xkeys @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/}, @kbd{^}, or @kbd{|} before
  607. Xentering the variable name, the value on the top of the stack is added
  608. Xto, subtracted from, multiplied or divided into, used as a power for, or
  609. Xconcatenated to the value already in the specified variable.  (The new
  610. Xvalue of the variable is reported in the @samp{*Calc Trail*} buffer for
  611. Xyour information.)@refill
  612. X
  613. X@pindex calc-unstore
  614. X@cindex Void variables
  615. X@cindex Un-storing variables
  616. XUntil you store a value in them, variables are ``void,'' that is, they
  617. Xcontain no value at all.  If they appear in an algebraic formula they
  618. Xwill be left alone even if you press @kbd{=} (@code{calc-evaluate}).
  619. XTo return a variable to the void state, use @code{calc-unstore}.  An
  620. Xeasy keyboard shortcut for this command is @kbd{s} (@code{calc-store})
  621. Xwith any numeric prefix argument:  @kbd{C-u s foo @key{RET}}
  622. XThis form does not use the value on
  623. Xthe top of the stack; it simply removes any value that may already be
  624. Xdefined for the specified variable.@refill
  625. X
  626. X@kindex Z V
  627. X@pindex calc-permanent-variable
  628. X@cindex Storing variables
  629. X@cindex Permanent variables
  630. X@cindex @file{.emacs} file, veriables
  631. XThe shift-@kbd{Z V} (@code{calc-permanent-variable}) command saves a
  632. Xvariable's value permanently in your @file{.emacs} file, so that its
  633. Xvalue will still be available in future Emacs sessions.  You can
  634. Xre-execute @kbd{Z V} later on to update the saved value, but the
  635. Xonly way to remove a saved variable is to edit your @file{.emacs} file
  636. Xby hand.  (The shift-@kbd{Z} prefix key is mainly for defining user
  637. Xcommands; @pxref{Programming}.)
  638. X
  639. XThe only variables with predefined values are the ``special constants''
  640. X@code{var-pi}, @code{var-e}, and @code{var-i}.  You are free to unstore these
  641. Xvariables or to store a new value over them if you like, although some
  642. Xof the algebraic-manipulation functions may assume these variables
  643. Xrepresent their standard values.
  644. X
  645. X@node Recalling Variables, Let Command, Storing Variables, Store and Recall
  646. X@section Recalling Variables
  647. X
  648. X@kindex r
  649. X@pindex calc-recall
  650. X@cindex Recalling variables
  651. XThe most straightforward way to extract the stored value from a variable
  652. Xis to use the @kbd{r} (@code{calc-recall}) command.  This command prompts
  653. Xfor a variable name (similarly to @code{calc-store}), looks up the value
  654. Xof the specified variable, and pushes that value onto the stack.  It is
  655. Xan error to try to recall a void variable.
  656. X
  657. XIt is also possible to recall the value from a variable by evaluating a
  658. Xformula containing that variable.  For example, @kbd{' a @key{RET} =} is
  659. Xthe same as @kbd{r a @key{RET}} except that if the variable is void, the
  660. Xformer will simply leave the formula @samp{a} on the stack whereas the
  661. Xlatter will produce an error message.
  662. X
  663. X@node Let Command, , Recalling Variables, Store and Recall
  664. X@section The Let Command
  665. X
  666. X@kindex l
  667. X@pindex calc-let
  668. X@cindex Variables, temporary assignment
  669. X@cindex Temporary assignment to variables
  670. XIf you have an expression like @samp{a+b^2} on the stack and you wish to
  671. Xcompute its value where @samp{b=3}, you can simply store 3 in @samp{b} and
  672. Xthen press @kbd{=} to reevaluate the formula.  This has the side-effect
  673. Xof storing the value 3 in @samp{b} for future operations.
  674. X
  675. XThe lowercase-@kbd{l} (@code{calc-let}) command evaluates a formula under
  676. Xa @emph{temporary} assignment of a variable.  It stores the value on the
  677. Xtop of the stack into the specified variable, then evaluates the
  678. Xsecond-to-top stack entry, then restores the orginal value (or lack
  679. Xof one) in the variable.  Thus after @kbd{' a+b^2 @key{RET} 3 l b @key{RET}},
  680. Xthe stack will contain the formula @samp{a + 9}.  The subsequent command
  681. X@kbd{5 l a @key{RET}} will replace this formula with the number 14.  The
  682. Xvariables @samp{a} and @samp{b} are not permanently affected in any way
  683. Xby these commands.
  684. X
  685. X@node Kill and Yank, Programming, Store and Recall, Top
  686. X@chapter Kill and Yank Functions
  687. X
  688. XThe commands in this chapter move information between the Calculator and
  689. Xother Emacs editing buffers.
  690. X
  691. X@menu
  692. X* Killing From Stack::
  693. X* Yanking Into Stack::
  694. X* Grabbing From Buffers::
  695. X* Yanking Into Buffers::
  696. X* Editing Stack Entries::
  697. X@end menu
  698. X
  699. X@node Killing From Stack, Yanking Into Stack, Kill and Yank, Kill and Yank
  700. X@section Killing from the Stack
  701. X
  702. X@kindex C-k
  703. X@pindex calc-kill
  704. X@kindex M-k
  705. X@pindex calc-copy-as-kill
  706. X@kindex C-w
  707. X@pindex calc-kill-region
  708. X@kindex M-w
  709. X@pindex calc-copy-region-as-kill
  710. X@cindex Kill ring
  711. X@dfn{Kill} commands are Emacs commands that insert text into the
  712. X``kill ring,'' from which it can later be ``yanked'' by a @kbd{C-y}
  713. Xcommand.  Three common kill commands in normal Emacs are @kbd{C-k}, which
  714. Xkills one line, @kbd{C-w}, which kills the region between mark and point,
  715. Xand @kbd{M-w}, which puts the region into the kill ring without actually
  716. Xdeleting it.  All of these commands work in the Calculator, too.  Also,
  717. X@kbd{M-k} has been provided to complete the set; it puts the current line
  718. Xinto the kill ring without deleting anything.
  719. X
  720. XThe kill commands are unusual in that they pay attention to the location
  721. Xof the cursor in the Calculator buffer.  If the cursor is on or below the
  722. Xbottom line, the kill commands operate on the top of the stack.  Otherwise,
  723. Xthey operate on whatever stack element the cursor is on.  Calc's kill
  724. Xcommands always operate on whole stack entries.  (They act the same as their
  725. Xstandard Emacs cousins except they ``round up'' the specified region to
  726. Xencompass full lines.)  The text is copied into the kill ring exactly as
  727. Xit appears on the screen, including line numbers if they are enabled.
  728. X
  729. XA numeric prefix argument to @kbd{C-k} or @kbd{M-k} affects the number
  730. Xof lines killed.  A positive argument kills the current line and @samp{n-1}
  731. Xlines below it.  A negative argument kills the @samp{-n} lines above the
  732. Xcurrent line.  Again this mirrors the behavior of the standard Emacs
  733. X@kbd{C-k} command.  Although a whole line is always deleted, @kbd{C-k}
  734. Xwith no argument copies only the number itself into the kill ring, whereas
  735. X@kbd{C-k} with a prefix argument of 1 copies the number with its trailing
  736. Xnewline.
  737. X
  738. X@node Yanking Into Stack, Grabbing From Buffers, Killing From Stack, Kill and Yank
  739. X@section Yanking into the Stack
  740. X
  741. X@kindex C-y
  742. X@pindex calc-yank
  743. XThe @kbd{C-y} command yanks the most recently killed text back into the
  744. XCalculator.  It pushes this value onto the top of the stack regardless of
  745. Xthe cursor position.  In general it re-parses the killed text as a number
  746. Xor formula (or list of these seperated by commas or newlines).  However if
  747. Xthe thing being yanked is something that was just killed from the Calculator
  748. Xitself, its full internal structure is yanked.  For example, if you have
  749. Xset the floating-point display mode to show only four significant digits,
  750. Xthen killing and re-yanking 3.14159 (which displays as 3.142) will yank the
  751. Xfull 3.14159, even though yanking it into any other buffer would yank the
  752. Xnumber in its displayed form, 3.142.  (Since the default display modes
  753. Xshow all objects to their full precision, this feature normally makes no
  754. Xdifference.)
  755. X
  756. X@node Grabbing From Buffers, Yanking Into Buffers, Yanking Into Stack, Kill and Yank
  757. X@section Grabbing from Other Buffers
  758. X
  759. X@kindex M-# (calc-grab-region)
  760. X@pindex calc-grab-region
  761. XThe @code{calc-grab-region} command takes the text between point and mark
  762. Xin the current buffer and attempts to parse it as a matrix.  If point and
  763. Xmark are both in the leftmost column, the lines in between are parsed in
  764. Xtheir entirety.  Otherwise, point and mark define the corners of a rectangle
  765. Xwhose contents are parsed.  Note that, unlike all other @samp{calc-}
  766. Xcommands, @code{calc-grab-region} is intended to be used in a normal
  767. Xediting buffer, @emph{not} from inside the Calculator buffer!  If the
  768. Xcommand works successfully, it does an automatic @kbd{M-x calc} to enter
  769. Xthe Calculator buffer.
  770. X
  771. XThe @kbd{M-#} key with a negative prefix argument (@kbd{M-- M-#}) is
  772. Xequivalent to @code{calc-grab-region}.
  773. X
  774. XEach line of the grabbed area must contain one or more numbers,
  775. Xseparated by spaces and/or commas.  Leading and trailing square
  776. Xbrackets, curly braces and commas on the line are ignored.  Leading
  777. Xline numbers (as found in the Calc stack) are also ignored.  Thus, this
  778. Xcommand can generally read anything from a column of numbers in a
  779. Xspreadsheet to a matrix in one of Calc's own display formats.  The
  780. Xgrabbed values may actually be any Calc formulas, except that spaces
  781. Xnot enclosed in parentheses will be interpreted as separating two
  782. Xseparate formulas.@refill
  783. X
  784. XIf a different language has been specified (@pxref{Language Modes}),
  785. Xthe grabbed text will be interpreted according to the that language.
  786. X
  787. XEach line becomes a row of the matrix.  The result will actually be a
  788. Xvector of vectors, which Calc will treat as a matrix only if every row
  789. Xcontains the same number of values.  @xref{Matrix Functions}, to see how
  790. Xto pull the matrix apart into its constituent rows and columns.  (If it
  791. Xis a 1x1 matrix, just hit @kbd{v u} (@code{calc-unpack}) twice.)
  792. X
  793. XFor example, suppose we have a column of numbers in a file which we
  794. Xwish to sum.  Go to one corner of the column and press @kbd{C-@@} to
  795. Xset the mark; go to the other corner and type @kbd{M-x calc-grab-region}.
  796. XThis will grab the column of numbers into the calculator in the form
  797. Xof column matrix.  Press @kbd{v t} to transpose this into a row matrix.
  798. XPress @kbd{v u} to unpack this into a simple vector.  (Recall, a one-row
  799. Xmatrix is a vector of a single vector.)  Then, type @kbd{V R +} to
  800. Xcompute the sum.
  801. X
  802. X@node Yanking Into Buffers, Editing Stack Entries, Grabbing From Buffers, Kill and Yank
  803. X@section Yanking into Other Buffers
  804. X
  805. X@kindex y
  806. X@pindex calc-copy-to-buffer
  807. XThe plain @kbd{y} (@code{calc-copy-to-buffer}) command inserts the number
  808. Xat the top of the stack into the most recently used normal editing buffer.
  809. X(More specifically, this is the most recently used buffer which is displayed
  810. Xin a window and whose name does not begin with @samp{*}.  If there is no
  811. Xsuch buffer, this is the most recently used buffer except for Calculator
  812. Xand Calc Trail buffers.)  The number is inserted exactly as it appears and
  813. Xwithout a newline.  (If line-numbering is enabled, the line number is
  814. Xnormally not included.)  The number is @emph{not} removed from the stack.
  815. X
  816. XWith a prefix argument, @kbd{y} inserts several numbers, one per line.
  817. XA positive argument inserts the specified number of values from the top
  818. Xof the stack.  A negative argument inserts the @samp{n}th value from the
  819. Xtop of the stack.  An argument of zero inserts the entire stack.  Note
  820. Xthat @kbd{y} with an argument of 1 is different from @kbd{y} with no
  821. Xargument; the former always copies exactly what appears in the stack
  822. Xdisplay, whereas the latter strips off the line number and trailing
  823. Xnewline.
  824. X
  825. XWith a lone @kbd{C-u} as a prefix argument, @kbd{y} @emph{replaces} the
  826. Xregion in the other buffer with the yanked text, then quits the Calculator,
  827. Xleaving you in that buffer.  A typical use would be to run
  828. X@code{calc-grab-region} to read a matrix of data into the Calculator,
  829. Xoperate on the data to produce a new matrix, then type @kbd{C-u y} to
  830. Xreplace the original matrix with the new one.  One would normally disable
  831. Xline-numbering (with @kbd{d l}) before doing this, and possibly also alter
  832. Xthe matrix display style (@pxref{Vector and Matrix Formats}) or change the
  833. Xcurrent display language (@pxref{Language Modes}).  Also, note
  834. Xthat this command replaces a linear region of text, not a rectangle.@refill
  835. X
  836. X@node Editing Stack Entries, , Yanking Into Buffers, Kill and Yank
  837. X@section Editing Stack Entries
  838. X
  839. X@kindex `
  840. X@pindex calc-edit
  841. X@kindex C-c C-c (Edit Mode)
  842. X@pindex calc-edit-finish
  843. X@cindex Editing the stack with Emacs
  844. XThe backquote, @kbd{`} (@code{calc-edit}) command creates a temporary
  845. Xbuffer (@samp{*Calc Edit*}) for editing the top-of-stack value using
  846. Xregular Emacs commands.  With a numeric prefix argument, it edits the
  847. Xspecified number of stack entries at once.  (An argument of zero edits
  848. Xthe entire stack.)  When you press @key{RET}, @key{LFD}, or @kbd{C-c C-c}
  849. X(all bound to @code{calc-edit-finish}), the Calculator parses the
  850. Xlines of text in this buffer as numbers or formulas, replaces the
  851. Xoriginal stack elements in the original buffer with these new values,
  852. Xthen kills the @samp{*Calc Edit*} buffer.  The original Calculator buffer
  853. Xcontinues to exist during editing, but for best results you should be
  854. Xcareful not to change it until you have finished the edit.  You can
  855. Xalso cancel the edit simply by killing the @samp{*Calc Edit*} buffer
  856. Xwith @kbd{C-x k} (@code{kill-buffer}).
  857. X
  858. X@node Programming, Installation, Kill and Yank, Top
  859. X@chapter Programming the Calculator
  860. X
  861. XThere are several ways to ``program'' the Emacs Calculator, depending
  862. Xon the nature of the problem you need to solve.
  863. X
  864. X@enumerate
  865. X@item
  866. X@dfn{Keyboard macros} allow you to record a sequence of keystrokes
  867. Xand play them back at a later time.  This is just the standard Emacs
  868. Xkeyboard macro mechanism, dressed up with a few more features such
  869. Xas loops and conditionals.
  870. X@item
  871. X@dfn{Algebraic definitions} allow you to use any formula to define a
  872. Xnew function.  This function can then be used in algebraic formulas or
  873. Xas an interactive command.
  874. X@item
  875. X@dfn{Rewrite rules} are discussed in the section on algebra commands.
  876. XYou can use them for programming by creating a command (say, using
  877. Xkeyboard macros) that applies the rules that define your function's
  878. Xbehavior.  @xref{Rewrite Rules}.
  879. X@item
  880. X@dfn{Lisp} is the programming language that Calc (and most of Emacs)
  881. Xis written in.  If the above techniques aren't powerful enough, you
  882. Xcan write Lisp functions to do anything that built-in Calc commands
  883. Xcan do.  Lisp code is also somewhat faster than keyboard macros.
  884. X@end enumerate
  885. X
  886. XProgramming features are available through the @kbd{z} and @kbd{Z}
  887. Xprefix keys.  New commands that you define are two-key sequences
  888. Xbeginning with @kbd{z}.  Commands for managing these definitions
  889. Xuse the shift-@kbd{Z} prefix.  (The @kbd{Z V} (@code{calc-permanent-variable})
  890. Xcommand is described elsewhere; @pxref{Storing Variables}.)
  891. X
  892. X@menu
  893. X* Creating User Keys::
  894. X* Keyboard Macros::
  895. X* Algebraic Definitions::
  896. X* Lisp Definitions::
  897. X@end menu
  898. X
  899. X@node Creating User Keys, Keyboard Macros, Programming, Programming
  900. X@section Creating User Keys
  901. X
  902. X@kindex Z D
  903. X@pindex calc-user-define
  904. XAny Calculator command may be bound to a key using the @kbd{Z D}
  905. X(@code{calc-user-define}) command.  Actually, it is bound to a two-key
  906. Xsequence beginning with the @kbd{z} prefix.  (Note that the commands
  907. Xfor programming begin with shift-@kbd{Z}, and your own commands begin
  908. Xwith unshifted @kbd{z}.)
  909. X
  910. XThe @kbd{Z D} command first prompts for the key to define.  For example,
  911. Xpress @kbd{Z D a} to define the new key sequence @kbd{z a}.  You are then
  912. Xprompted for the name of the Calculator command that this key should
  913. Xrun.  For example, the @code{calc-hypot} (hypotenuse) command is not normally
  914. Xavailable on a key.  Typing @kbd{Z D h hypot @key{RET}} programs the
  915. X@kbd{z h} key sequence to run @code{calc-hypot}.  This definition will remain
  916. Xin effect for the rest of this Emacs session, or until you redefine
  917. X@kbd{z h} to be something else.
  918. X
  919. XYou can actually bind any Emacs command to a @kbd{z} key sequence by
  920. Xbackspacing over the @samp{calc-} when you are prompted for the command name.
  921. X
  922. XAs with any other prefix key, you can type @kbd{z ?} to see a list of
  923. Xall the two-key sequences you have defined that start with @kbd{z}.
  924. XInitially, no @kbd{z} sequences (except @kbd{z ?} itself) are defined.
  925. X
  926. XUser keys are typically letters, but may in fact use any key.
  927. X(@key{META}-keys are not permitted, nor are a terminal's special
  928. Xfunction keys which generate multi-character sequences when pressed.)
  929. XYou can define different commands on the shifted and unshifted versions
  930. Xof a letter if you wish.
  931. X
  932. X@kindex Z U
  933. X@pindex calc-user-undefine
  934. XThe @kbd{Z U} (@code{calc-user-undefine}) command unbinds a user key.
  935. XFor example, the key sequence @kbd{Z U h} will undefine the @code{hypot}
  936. Xkey we defined above.
  937. X
  938. X@kindex Z P
  939. X@pindex calc-user-define-permanent
  940. X@cindex Storing user definitions
  941. X@cindex Permanent user definitions
  942. X@cindex @file{.emacs} file, user-defined commands
  943. XThe @kbd{Z P} (@code{calc-user-define-permanent}) command makes a key
  944. Xbinding permanent so that it will remain in effect even in future Emacs
  945. Xsessions.  (It does this by adding a suitable bit of Lisp code into
  946. Xyour @file{.emacs} file.)  For example, @kbd{Z P h} would register
  947. Xour @code{hypot} command permanently.  If you later wish to unregister
  948. Xthis command you must edit your @file{.emacs} file by hand.
  949. X
  950. X@kindex Z E
  951. X@pindex calc-user-define-edit
  952. X@cindex Editing user definitions
  953. XThe @kbd{Z E} (@code{calc-user-define-edit}) command edits the definition
  954. Xof a user key.  This works for keys that have been defined by either
  955. Xkeyboard macros or formulas; further details are contained in the relevant
  956. Xfollowing sections.
  957. X
  958. X@node Keyboard Macros, Algebraic Definitions, Creating User Keys, Programming
  959. X@section Programming with Keyboard Macros
  960. X
  961. X@kindex K
  962. X@cindex Programming with keyboard macros
  963. X@cindex Keyboard macros
  964. XThe easiest way to ``program'' the Emacs Calculator is to use standard
  965. Xkeyboard macros.  Press @kbd{C-x (} to begin recording a macro.  From
  966. Xthis point on, keystrokes you type will be saved away as well as
  967. Xperforming their usual functions.  Press @kbd{C-x )} to end recording.
  968. XPress shift-@kbd{K} (or the standard Emacs key sequence @kbd{C-x e}) to
  969. Xexecute your keyboard macro by replaying the recorded keystrokes.
  970. X@xref{Keyboard Macros, , , emacs, the Emacs manual}, for further
  971. Xinformation.@refill
  972. X
  973. XWhen you use @kbd{K} to invoke a keyboard macro, the entire macro is
  974. Xtreated as a single command by the undo and trail features.  The stack
  975. Xdisplay buffer is not updated during macro execution, but is instead
  976. Xfixed up once the macro completes.  Thus, commands defined with keyboard
  977. Xmacros are convenient and efficient.  The @kbd{C-x e} command, on the
  978. Xother hand, invokes the keyboard macro with no special treatment: Each
  979. Xcommand in the macro will record its own undo information and trail
  980. Xentry, and update stack buffer accordingly.  If your macro uses features
  981. Xoutside of Calc's control to operate on the contents of the Calc stack
  982. Xbuffer, or if it includes Undo, Redo, or Last X commands, you must use
  983. X@kbd{C-x e} to make sure the buffer and undo list are up-to-date at all
  984. Xtimes.
  985. X
  986. XCalc extends the standard Emacs keyboard macros in several ways.
  987. XKeyboard macros can be used to create user-defined commands.  Keyboard
  988. Xmacros can include conditional and iteration structures, somewhat
  989. Xanalogous to those provided by a traditional programmable calculator.
  990. X
  991. X@menu
  992. X* Naming Keyboard Macros::
  993. X* Conditionals in Macros::
  994. X* Loops in Macros::
  995. X* Local Values in Macros::
  996. X* Queries in Macros::
  997. X@end menu
  998. X
  999. X@node Naming Keyboard Macros, Conditionals in Macros, Keyboard Macros, Keyboard Macros
  1000. X@subsection Naming Keyboard Macros
  1001. X
  1002. X@kindex Z K
  1003. X@pindex calc-user-define-kbd-macro
  1004. XOnce you have defined a keyboard macro, you can bind it to a @kbd{z}
  1005. Xkey sequence with the @kbd{Z K} (@code{calc-user-define-kbd-macro}) command.
  1006. XThis command prompts first for a key, then for a command name.  For
  1007. Xexample, if you type @kbd{C-x ( n @key{TAB} n @key{TAB} C-x )} you will
  1008. Xdefine a keyboard macro which negates the top two numbers on the stack
  1009. X(@key{TAB} swaps the top two stack elements).  Now you can type
  1010. X@kbd{Z K n @key{RET}} to define this keyboard macro onto the @kbd{z n} key
  1011. Xsequence.  The default command name (if you answer the second prompt with
  1012. Xjust the @key{RET} key as in this example) will be something like
  1013. X@samp{calc-User-n}.  The keyboard macro will now be available as both
  1014. X@kbd{z n} and @kbd{M-x calc-User-n}.  You can backspace and enter a more
  1015. Xdescriptive command name if you wish.@refill
  1016. X
  1017. XMacros defined by @kbd{Z K} act like single commands; they are executed
  1018. Xin the same way as by the @kbd{K} key.  If you wish to define the macro
  1019. Xas a standard no-frills Emacs macro (to be executed as if by @kbd{C-x e}),
  1020. Xgive a negative prefix argument to @kbd{Z K}.
  1021. X
  1022. XOnce you have bound your keyboard macro to a key, you can use
  1023. X@kbd{Z P} to register it permanently with Emacs.  @xref{Creating User Keys}.
  1024. X
  1025. X@cindex Keyboard macros, editing
  1026. XThe @kbd{Z E} (@code{calc-user-define-edit}) command on a key that has
  1027. Xbeen defined by a keyboard macro tries to use the @code{edit-kbd-macro}
  1028. Xcommand to edit the macro.  This command may be found in the
  1029. X@file{macedit} package, a copy of which comes with Calc.  If
  1030. X@code{edit-kbd-macro} is not available, or if you give a negative
  1031. Xnumeric prefix argument, @kbd{Z E} uses a variant of @code{calc-edit}
  1032. Xwith the contents of the buffer being the keyboard macro in ``raw''
  1033. Xform.  In either case, type @kbd{C-c C-c} to finish editing and update
  1034. Xthe definition stored on the key, or, to cancel the edit, just kill the
  1035. Xmacro-editing buffer with @kbd{C-x k}.@refill
  1036. X
  1037. X@node Conditionals in Macros, Loops in Macros, Naming Keyboard Macros, Keyboard Macros
  1038. X@subsection Conditionals in Keyboard Macros
  1039. X
  1040. X@kindex Z [
  1041. X@kindex Z :
  1042. X@kindex Z |
  1043. X@kindex Z ]
  1044. X@pindex calc-kbd-if
  1045. X@pindex calc-kbd-else
  1046. X@pindex calc-kbd-else-if
  1047. X@pindex calc-kbd-end-if
  1048. X@cindex Conditional structures
  1049. XThe @kbd{Z [} (@code{calc-kbd-if}) and @kbd{Z ]} (@code{calc-kbd-end-if})
  1050. Xcommands allow you to put simple tests in a keyboard macro.  When Calc
  1051. Xsees the @kbd{Z [}, it pops an object from the stack and, if the object is
  1052. Xa non-zero number, continues executing keystrokes.  But if the object is
  1053. Xzero, or if it is not a number, Calc skips ahead to the matching @kbd{Z ]}
  1054. Xkeystroke.  @xref{Logical Operations}, for a set of commands for performing
  1055. Xtests which conveniently produce 1 for true and 0 for false.
  1056. X
  1057. XFor example, @kbd{@key{RET} 0 a < Z [ n Z ]} implements an absolute-value
  1058. Xfunction as a keyboard macro.  To program this macro, type @kbd{C-x (},
  1059. Xtype the above sequence of keystrokes, then type @kbd{C-x )}.  Note that
  1060. Xthe keystrokes will be executed while you are making the definition as
  1061. Xwell as when you later re-execute the macro by typing @kbd{K}.  Thus you
  1062. Xshould make sure a suitable number is on the stack before defining the
  1063. Xmacro to make sure you don't get a stack-underflow error during the
  1064. Xdefinition process.
  1065. X
  1066. XThe above macro duplicates the number on the top of the stack, pushes
  1067. Xzero and compares using @kbd{a <} (@code{calc-less-than}), then, if
  1068. Xthe number was less than zero, executes @kbd{n} (@code{calc-change-sign}).
  1069. XOtherwise, the change-sign command is skipped.
  1070. X
  1071. XConditionals can be nested arbitrarily.  However, there should be exactly
  1072. Xone @kbd{Z ]} for each @kbd{Z [} in a keyboard macro.
  1073. X
  1074. XThe @kbd{Z :} (@code{calc-kbd-else}) command allows you to choose between
  1075. Xtwo keystroke sequences.  The general format is @kbd{@var{cond} Z [
  1076. X@var{then-part} Z : @var{else-part} Z ]}.  If @var{cond} is true
  1077. X(i.e., if the top of stack contains a non-zero number after @var{cond}
  1078. Xhas been executed), the @var{then-part} will be executed and the
  1079. X@var{else-part} will be skipped.  Otherwise, the @var{then-part} will
  1080. Xbe skipped and the @var{else-part} will be executed.
  1081. X
  1082. XThe @kbd{Z |} (@code{calc-kbd-else-if}) command allows you to choose
  1083. Xbetween any number of alternatives.  For example,
  1084. X@kbd{@var{cond1} Z [ @var{part1} Z : @var{cond2} Z | @var{part2} Z :
  1085. X@var{part3} Z ]} will execute @var{part1} if @var{cond1} is true,
  1086. Xotherwise it will execute @var{part2} if @var{cond2} is true, otherwise
  1087. Xit will execute @var{part3}.
  1088. X
  1089. XMore precisely, @kbd{Z [} pops a number and conditionally skips to the
  1090. Xnext matching @kbd{Z :} or @kbd{Z ]} key.  @kbd{Z ]} has no effect when
  1091. Xactually executed.  @kbd{Z :} skips to the next matching @kbd{Z ]}.
  1092. X@kbd{Z |} pops a number and conditionally skips to the next matching
  1093. X@kbd{Z :} or @kbd{Z ]}; thus, @kbd{Z [} and @kbd{Z |} are functionally
  1094. Xequivalent except that @kbd{Z [} participates in nesting but @kbd{Z |}
  1095. Xdoes not.
  1096. X
  1097. X@kindex Z C-g
  1098. XIf Calc gets stuck while skipping characters during the definition of a
  1099. Xmacro, type @kbd{Z C-g} to cancel the definition.  (Typing plain @kbd{C-g}
  1100. Xactually adds a @kbd{C-g} keystroke to the macro.)
  1101. X
  1102. X@node Loops in Macros, Local Values in Macros, Conditionals in Macros, Keyboard Macros
  1103. X@subsection Loops in Keyboard Macros
  1104. X
  1105. X@kindex Z <
  1106. X@kindex Z >
  1107. X@pindex calc-kbd-repeat
  1108. X@pindex calc-kbd-end-repeat
  1109. X@cindex Looping structures
  1110. X@cindex Iterative structures
  1111. XThe @kbd{Z <} (@code{calc-kbd-repeat}) and @kbd{Z >}
  1112. X(@code{calc-kbd-end-repeat}) commands pop a number from the stack,
  1113. Xwhich must be an integer, then repeat the keystrokes between the brackets
  1114. Xthe specified number of times.  If the integer is zero or negative, the
  1115. Xbody is skipped altogether.  For example, @kbd{1 @key{TAB} Z < 2 * Z >}
  1116. Xcomputes two to a nonnegative integer power.  First, we push 1 on the
  1117. Xstack and then swap the integer argument back to the top.  The @kbd{Z <}
  1118. Xpops that argument leaving the 1 back on top of the stack.  Then, we
  1119. Xrepeat a multiply-by-two step however many times.@refill
  1120. X
  1121. XOnce again, the keyboard macro is executed as it is being entered.
  1122. XIn this case it is especially important to set up reasonable initial
  1123. Xconditions before making the definition:  Suppose the integer 1000 just
  1124. Xhappened to be sitting on the stack before we typed the above definition!
  1125. XAnother approach is to enter a harmless dummy definition for the macro,
  1126. Xthen go back and edit in the real one with a @kbd{Z E} command.
  1127. X
  1128. X@kindex Z /
  1129. X@pindex calc-break
  1130. XThe @kbd{Z /} (@code{calc-kbd-break}) command allows you to break out
  1131. Xof a keyboard macro loop prematurely.  It pops an object from the stack;
  1132. Xif that object is true (a non-zero number), control jumps out of the
  1133. Xinnermost enclosing @kbd{Z <} @dots{} @kbd{Z >} loop and continues
  1134. Xafter the @kbd{Z >}.  If the object is false, the @kbd{Z /} has no
  1135. Xeffect.  Thus the effect is similar to the C language's @code{break}
  1136. Xstatement with an enclosing @code{if}.@refill
  1137. X
  1138. X@kindex Z (
  1139. X@kindex Z )
  1140. X@pindex calc-kbd-for
  1141. X@pindex calc-kbd-end-for
  1142. XThe @kbd{Z (} (@code{calc-kbd-for}) and @kbd{Z )} (@code{calc-kbd-end-for})
  1143. Xcommands are similar to @kbd{Z <} and @kbd{Z >}, except that they make the
  1144. Xvalue of the counter available inside the loop.  The general layout is
  1145. X@kbd{@var{init} @var{final} Z ( @var{body} @var{step} Z )}.  The @kbd{Z (}
  1146. Xcommand pops initial and final values from the stack.  It then creates
  1147. Xa temporary internal counter and initializes it with the value @var{init}.
  1148. XThe @kbd{Z (} command then repeatedly pushes the counter value onto the
  1149. Xstack and executes @var{body} and @var{step}, adding @var{step} to the
  1150. Xcounter each time until the loop finishes.@refill
  1151. X
  1152. X@cindex Summations (by keyboard macros)
  1153. XBy default, the loop finishes when the counter becomes greater than (or
  1154. Xless than) @var{final}, assuming @var{initial} is less than (greater
  1155. Xthan) @var{final}.  If @var{initial} is equal to @var{final}, the body
  1156. Xexecutes exactly once.  The body of the loop always executes at least
  1157. Xonce.  For example, @kbd{0 1 10 Z ( 2 ^ + 1 Z )} computes the sum of the
  1158. Xsquares of the integers from 1 to 10, in steps of 1.
  1159. X
  1160. XIf you give a numeric prefix argument of 1 to @kbd{Z (}, the loop is
  1161. Xforced to use upward-counting conventions.  In this case, if @var{initial}
  1162. Xis greater than @var{final} the body will not be executed at all.
  1163. XNote that @var{step} may still be negative in this loop; the prefix
  1164. Xargument merely constrains the loop-finished test.  Likewise, a prefix
  1165. Xargument of -1 forces downward-counting conventions.
  1166. X
  1167. X@kindex Z @{
  1168. X@kindex Z @}
  1169. X@pindex calc-kbd-loop
  1170. X@pindex calc-kbd-end-loop
  1171. XThe @kbd{Z @{} (@code{calc-kbd-loop}) and @kbd{Z @}}
  1172. X(@code{calc-kbd-end-loop}) commands are similar to @kbd{Z <} and
  1173. X@kbd{Z >}, except that they do not pop a count from the stack---they
  1174. Xeffectively create an infinite loop.  Every @kbd{Z @{} @dots{} @kbd{Z @}}
  1175. Xloop ought to include at least one @kbd{Z /} to make sure the loop
  1176. Xdoesn't run forever.  (If any error message occurs which causes Emacs
  1177. Xto beep, the keyboard macro will also be halted; this is a standard
  1178. Xfeature of Emacs.  You can also generally press @kbd{C-g} to halt a
  1179. Xrunning keyboard macro, although not all versions of Unix support
  1180. Xthis feature.)
  1181. X
  1182. XCalc's conditional and looping constructs work by scanning the
  1183. Xkeyboard macro for occurrences of character sequences like @samp{Z:}
  1184. Xand @samp{Z>}.  One side-effect of this is that if you use these
  1185. Xconstructs you must be careful that these character pairs do not
  1186. Xoccur by accident in other parts of the macros.  Since Calc doesn't
  1187. Xuse shift-Z for any purpose except as a prefix character, this is
  1188. Xnot likely to be a problem.  Another side-effect is that it will
  1189. Xnot work to define your own custom key bindings for these commands.
  1190. XOnly the standard shift-Z bindings will work correctly.
  1191. X
  1192. XThe conditional and looping constructs are not actually tied to
  1193. Xkeyboard macros, but they are most often used in that context.
  1194. XFor example, the keystrokes @kbd{10 Z < 23 @key{RET} Z >} push
  1195. Xten copies of 23 onto the stack.  This can be typed ``live'' just
  1196. Xas easily as in a macro definition.
  1197. X
  1198. X@node Local Values in Macros, Queries in Macros, Loops in Macros, Keyboard Macros
  1199. X@subsection Local Values in Macros
  1200. X
  1201. X@cindex Local variables
  1202. X@cindex Restoring saved modes
  1203. XKeyboard macros sometimes want to operate under known conditions
  1204. Xwithout affecting surrounding conditions.  For example, a keyboard
  1205. Xmacro may wish to turn on Fraction Mode, or set a particular
  1206. Xprecision, independent of the user's normal setting for those
  1207. Xmodes.
  1208. X
  1209. X@kindex Z `
  1210. X@kindex Z '
  1211. X@pindex calc-kbd-push
  1212. X@pindex calc-kbd-pop
  1213. XMacros also sometimes need to use local variables.  Assignments to
  1214. Xlocal variables inside the macro should not affect any variables
  1215. Xoutside the macro.  The @kbd{Z `} (@code{calc-kbd-push}) and @kbd{Z '}
  1216. X(@code{calc-kbd-pop}) commands give you both of these capabilities.
  1217. X
  1218. XWhen you type @kbd{Z `} (with a backquote or accent grave character),
  1219. Xthe values of various mode settings are saved away.  The ten ``quick''
  1220. Xvariables @code{var-0} through @code{var-9} are also saved.  When
  1221. Xyou type @kbd{Z '} (with an apostrophe), these values are restored.
  1222. SHAR_EOF
  1223. echo "End of part 17"
  1224. echo "File calc.texinfo is continued in part 18"
  1225. echo "18" > s2_seq_.tmp
  1226. exit 0
  1227.