home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume13 / gmcalc / part16 < prev    next >
Encoding:
Text File  |  1990-06-05  |  57.8 KB  |  1,283 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@csvax.caltech.edu (David Gillespie)
  3. Subject: v13i042: Emacs Calculator 1.01, part 16/19
  4. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5.  
  6. Posting-number: Volume 13, Issue 42
  7. Submitted-by: daveg@csvax.caltech.edu (David Gillespie)
  8. Archive-name: gmcalc/part16
  9.  
  10. ---- Cut Here and unpack ----
  11. #!/bin/sh
  12. # this is part 16 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=16
  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. Xof an angle or complex number.  If the input is an HMS form, it is interpreted
  29. Xas degrees-minutes-seconds; otherwise, the input is interpreted according
  30. Xto the current angular mode.  It is best to use Radians mode when operating
  31. Xon complex numbers.@refill
  32. X
  33. X@kindex I S
  34. X@pindex calc-arcsin
  35. X@tindex arcsin
  36. XWith the Inverse flag, @code{calc-sin} computes an arcsine.  This is also
  37. Xavailable as the @code{calc-arcsin} command or @code{arcsin} algebraic
  38. Xfunction.  The returned argument is converted to degrees, radians, or HMS
  39. Xnotation depending on the current angular mode.
  40. X
  41. X@kindex H S
  42. X@pindex calc-sinh
  43. X@tindex sinh
  44. X@kindex H I S
  45. X@pindex calc-arcsinh
  46. X@tindex arcsinh
  47. XWith the Hyperbolic flag, @code{calc-sin} computes the hyperbolic
  48. Xsine, also available as @code{calc-sinh} [@code{sinh}].  With the
  49. XHyperbolic and Inverse flags, it computes the hyperbolic arcsine
  50. X(@code{calc-arcsinh}) [@code{arcsinh}].
  51. X
  52. X@kindex C
  53. X@pindex calc-cos
  54. X@tindex cos
  55. X@kindex I C
  56. X@pindex calc-arccos
  57. X@tindex arccos
  58. X@kindex H C
  59. X@pindex calc-cosh
  60. X@tindex cosh
  61. X@kindex H I C
  62. X@pindex calc-arccosh
  63. X@tindex arccosh
  64. X@kindex T
  65. X@pindex calc-tan
  66. X@tindex tan
  67. X@kindex I T
  68. X@pindex calc-arctan
  69. X@tindex arctan
  70. X@kindex H T
  71. X@pindex calc-tanh
  72. X@tindex tanh
  73. X@kindex H I T
  74. X@pindex calc-arctanh
  75. X@tindex arctanh
  76. XThe shift-@kbd{C} (@code{calc-cos}) [@code{cos}] command computes the cosine
  77. Xof an angle or complex number, and shift-@kbd{T} (@code{calc-tan}) [@code{tan}]
  78. Xcomputes the tangent, along with all the various inverse and hyperbolic
  79. Xvariants of these functions.
  80. X
  81. X@pindex calc-arctan2
  82. X@tindex arctan2
  83. XThe @code{calc-arctan2} [@code{arctan2}] command takes two numbers from
  84. Xthe stack and computes the arc tangent of their ratio.  The result is in
  85. Xthe full range from -180 (exclusive) to +180 (inclusive) degrees, or the
  86. Xanalogous range in radians.  A similar result would be obtained with
  87. X@kbd{/} followed by @kbd{I T}, but the value would only be in the range
  88. Xfrom -90 to 90 degrees since the division loses information about the
  89. Xsigns of the two components, and an error might result from an explicit
  90. Xdivision by zero which @code{arctan2} would avoid.  By (arbitrary)
  91. Xdefinition, @samp{arctan2(0,0)=0}.
  92. X
  93. X@pindex calc-sincos
  94. X@tindex sincos
  95. X@tindex arcsincos
  96. XThe @code{calc-sincos} [@code{sincos}] command computes the sine and
  97. Xcosine of a number, returning them as a vector of the form
  98. X@samp{[@var{cos}, @var{sin}]}.
  99. XWith the Inverse flag [@code{arcsincos}], this command takes a two-element
  100. Xvector as an argument and computes @code{arctan2} of the elements.
  101. X(This command does not accept the Hyperbolic flag.)@refill
  102. X
  103. X@node Branch Cuts, Random Numbers, Trigonometric and Hyperbolic Functions, Scientific Functions
  104. X@section Branch Cuts and Principal Values
  105. X
  106. X@cindex Branch cuts
  107. X@cindex Principal values
  108. XAll of the logarithmic, trigonometric, and hyperbolic functions are defined
  109. Xfor complex numbers as well as for reals.  This section describes the values
  110. Xreturned in cases where the general result is a family of possible values.
  111. XCalc follows section 12.5.3 of Steele's @cite{Common Lisp, the Language} in
  112. Xthese matters.  This section will describe each function briefly; for a
  113. Xmore detailed discussion, consult Steele's book.  Except as noted below,
  114. Xthese branch cuts also exactly match those implemented by the HP-28C
  115. Xcalculator.
  116. X
  117. XNote:  The current implementations of these functions with complex arguments
  118. Xare designed with proper behavior around the branch cuts in mind, @emph{not}
  119. Xefficiency or accuracy.  You may need to increase the floating precision
  120. Xand wait a while to get suitable answers from them.
  121. X
  122. XFor @samp{sqrt(a+bi)}:  When @samp{a<0} and @samp{b} is small but positive
  123. Xor zero, the result is close to the @samp{+i} axis.  For @samp{b} small and
  124. Xnegative, the result is close to the @samp{-i} axis.  The result always lies
  125. Xin the right half of the complex plane.
  126. X
  127. XFor @samp{ln(a+bi)}:  The real part is defined as @samp{ln(abs(a+bi))}.
  128. XThe imaginary part is defined as @samp{arg(a+bi) = arctan2(b,a)}.
  129. X
  130. XFor @samp{z1^z2}:  This is defined by @samp{exp(ln(z1)*z2)}.
  131. X
  132. XFor @samp{arcsin(z)}:  This is defined by @samp{-i*ln(i*z + sqrt(1-z^2))}.
  133. X
  134. XFor @samp{arccos(z)}:  This is defined by @samp{-i*ln(z + i*sqrt(1-z^2))},
  135. Xor equivalently by @samp{pi/2 - arcsin(z)}.
  136. X
  137. XFor @samp{arctan(z)}:  This is defined by
  138. X@samp{-i*ln((1+i*z) * sqrt(1/(1+z^2)))}.  The HP-28C differs in that the
  139. Xbranch cuts are continuous on the opposite sides.@refill
  140. X
  141. XFor @samp{arcsinh(z)}:  This is defined by @samp{ln(z + sqrt(1+z^2))}.
  142. X
  143. XFor @samp{arccosh(z)}:  This is defined by
  144. X@samp{ln(z + (z+1)*sqrt((z-1)/(z+1)))}.
  145. X
  146. XFor @samp{arctanh(z)}:  This is defined by @samp{ln((1+z) * sqrt(1/(1-z^2)))}.
  147. XAgain this differs from the HP-28C in that the values exactly on the branch
  148. Xcuts are continuous with the opposite sides.
  149. X
  150. X@node Random Numbers, Combinatorial Functions, Branch Cuts, Scientific Functions
  151. X@section Random Numbers
  152. X
  153. X@kindex k r
  154. X@pindex calc-random
  155. X@tindex random
  156. XThe @kbd{k r} (@code{calc-random}) [@code{random}] command produces
  157. Xrandom numbers of various sorts.
  158. X
  159. XGiven a positive numeric prefix argument @samp{M}, it produces a random
  160. Xinteger @samp{N} in the range @samp{0 <= N < M}.  Each of the @samp{M}
  161. Xvalues appears with equal probability.@refill
  162. X
  163. XWith no numeric prefix argument, the @kbd{k r} command takes its argument
  164. Xfrom the stack instead.  Once again, if this is a positive integer @samp{M}
  165. Xthe result is a random integer less than @samp{M}.  However, note that
  166. Xwhile numeric prefix arguments are limited to six digits or so, an @samp{M}
  167. Xtaken from the stack can be arbitrarily large.
  168. X
  169. XIf the value on the stack is a real number @samp{M}, the result is a
  170. Xrandom real number @samp{N} in the range @samp{0 <= N < M} or
  171. X@samp{M < N <= 0}, according to the sign of @samp{M}.
  172. X
  173. XIf @samp{M} is zero, the result is a Gaussian-distributed random real
  174. Xnumber; the distribution has a mean of zero and a variance of one.
  175. XThe algorithm used generates random numbers in pairs; thus, every other
  176. Xcall to this function will be especially fast.
  177. X
  178. XIf @samp{M} is an interval form, the lower and upper bounds specify the
  179. Xacceptable limits of the random numbers.  If both bounds are integers,
  180. Xthe result is a random integer in the specified range.  If either bound
  181. Xis floating-point, the result is a random real number in the specified
  182. Xrange.
  183. X
  184. X@pindex calc-rrandom
  185. XThe @code{calc-rrandom} command produces a random real number between
  186. Xzero and one.  It is equivalent to @samp{random(1.0)}.
  187. X
  188. X@kindex k a
  189. X@pindex calc-random-again
  190. XThe @kbd{k a} (@code{calc-random-again}) command produces another random
  191. Xnumber, re-using the most recent value of @samp{M}.
  192. X
  193. X@node Combinatorial Functions, , Random Numbers, Scientific Functions
  194. X@section Combinatorial Functions
  195. X
  196. XCommands relating to combinatorics and number theory begin with the
  197. X@kbd{k} key prefix.
  198. X
  199. X@kindex k g
  200. X@pindex calc-gcd
  201. X@tindex gcd
  202. XThe @kbd{k g} (@code{calc-gcd}) [@code{gcd}] command computes the
  203. XGreatest Common Divisor of two integers.  (For non-integer arguments the
  204. Xoperation is left in symbolic form.)@refill
  205. X
  206. X@kindex k l
  207. X@pindex calc-lcm
  208. X@tindex lcm
  209. XThe @kbd{k l} (@code{calc-lcm}) [@code{lcm}] command computes the
  210. XLeast Common Multiple of two integers.  The product of the LCM and GCD
  211. Xof two integers is equal to the product of the integers.@refill
  212. X
  213. X@kindex k G
  214. X@pindex calc-extended-gcd
  215. X@tindex egcd
  216. XThe @kbd{k G} (@code{calc-extended-gcd}) [@code{egcd}] command computes
  217. Xthe GCD of two integers @samp{x} and @samp{y} and returns a vector
  218. X@samp{[g, a, b]} where @samp{g = gcd(x,y) = a x + b y}.
  219. X
  220. X@kindex !
  221. X@pindex calc-factorial
  222. X@tindex fact
  223. XThe @kbd{!} (@code{calc-factorial}) [@code{fact}] command computes the
  224. Xfactorial of the number at the top of the stack.  If the number is an
  225. Xinteger, the result is an exact integer.  If the number is an
  226. Xinteger-valued float, the result is a floating-point approximation.  If
  227. Xthe number is a non-integral real number, the generalized factorial is used,
  228. Xas defined by the Euler Gamma function.  Please note that computation of
  229. Xlarge factorials can be slow; using floating-point format will help
  230. Xsince fewer digits must be maintained.@refill
  231. X
  232. X@pindex calc-gamma
  233. X@tindex gamma
  234. XThe @code{calc-gamma} [@code{gamma}] command computes the Euler Gamma
  235. Xfunction explicitly, according to the rule @samp{gamma(x+1) = fact(x)}.
  236. X
  237. X@kindex k d
  238. X@pindex calc-double-factorial
  239. X@tindex dfact
  240. XThe @kbd{k d} (@code{calc-double-factorial}) [@code{dfact}] command
  241. Xcomputes the ``double factorial'' of an integer.  For an even integer,
  242. Xthis is the product of even integers from 2 to @samp{N}.  For an odd
  243. Xinteger, this is the product of odd integers from 3 to @samp{N}.  If
  244. Xthe argument is an integer-valued float, the result is a floating-point
  245. Xapproximation.@refill
  246. X
  247. X@kindex k b
  248. X@pindex calc-choose
  249. X@tindex choose
  250. XThe @kbd{k b} (@code{calc-choose}) [@code{choose}] command computes the
  251. Xbinomial coefficient @samp{N}-choose-@samp{M}, where @samp{M} is the number
  252. Xon the top of the stack and @samp{N} is second-to-top.  If both arguments
  253. Xare integers, the result is an exact integer.  Otherwise, the result is a
  254. Xfloating-point approximation.  The binomial coefficient is defined for all
  255. Xreal numbers by @samp{N! / M! (N-M)!}.
  256. X
  257. X@kindex H k b
  258. X@pindex calc-perm
  259. X@tindex perm
  260. XThe @kbd{H k b} (@code{calc-perm}) [@code{perm}] command computes the
  261. Xnumber-of-permutations function @samp{N! / M!}.
  262. X
  263. X@kindex k p
  264. X@pindex calc-prime-test
  265. X@cindex Primes
  266. XThe @kbd{k p} (@code{calc-prime-test}) command checks if the integer on
  267. Xthe top of the stack is prime.  For integers less than eight million, the
  268. Xanswer is always exact and reasonably fast.  For larger integers, a
  269. Xprobabilistic method is used (See Knuth vol. II, section 4.5.4, algorithm P).
  270. XThe number is first checked against small prime factors (up to 13).  Then,
  271. Xany number of iterations of the algorithm are performed.  Each step either
  272. Xdiscovers that the number is non-prime, or substantially increases the
  273. Xcertainty that the number is prime.  After a few steps, the chance that
  274. Xa number was mistakenly described as prime will be less than one percent.
  275. X(Indeed, this is the worst case; in practice even a single iteration is
  276. Xquite reliable.)  After the @kbd{k p} command, the number will be reported
  277. Xas definitely prime or non-prime if possible, or otherwise ``probably''
  278. Xprime with a certain probability of error.
  279. X
  280. XThe normal @kbd{k p} command performs one iteration of the primality
  281. Xtest.  Pressing @kbd{k p} repeatedly for the same integer will perform
  282. Xadditional iterations.  Also, @kbd{k p} with a numeric prefix performs
  283. Xthe specified number of iterations.
  284. X
  285. X@kindex k f
  286. X@pindex calc-prime-factors
  287. X@tindex prfac
  288. XThe @kbd{k f} (@code{calc-prime-factors}) [@code{prfac}] command
  289. Xattempts to decompose an integer into its prime factors.  For numbers up
  290. Xto 25 million, the answer is exact although it may take some time.  The
  291. Xresult is a vector of the prime factors in increasing order.  For larger
  292. Xinputs, prime factors above 5000 may not be found, in which case the
  293. Xlast number in the vector will be an unfactored integer greater than 25
  294. Xmillion.
  295. X
  296. X@kindex k n
  297. X@pindex calc-next-prime
  298. X@tindex nextprime
  299. XThe @kbd{k n} (@code{calc-next-prime}) [@code{nextprime}] command finds
  300. Xthe next prime above a given number.  Essentially, it searches by calling
  301. X@code{calc-prime-test} on successive integers until it finds one that
  302. Xpasses the test.  This is quite fast for integers less than eight million,
  303. Xbut once the probabilistic test comes into play the search may be rather
  304. Xslow.  Ordinarily this command stops for any prime that passes one iteration
  305. Xof the primality test.  With a numeric prefix argument, a number must pass
  306. Xthe specified number of iterations before the search stops.  (This only
  307. Xmatters when searching above eight million.)  You can always use additional
  308. X@kbd{k p} commands to increase your certainty that the number is indeed
  309. Xprime.
  310. X
  311. X@kindex I k n
  312. X@pindex calc-prev-prime
  313. X@tindex prevprime
  314. XThe @kbd{I k n} (@code{calc-prev-prime}) [@code{prevprime}] command
  315. Xanalogously finds the next prime less than a given number.
  316. X
  317. X@kindex k t
  318. X@pindex calc-totient
  319. X@tindex totient
  320. XThe @kbd{k t} (@code{calc-totient}) [@code{totient}] command computes the
  321. XEuler ``totient'' function, the number of integers less than @samp{n} which
  322. Xare relatively prime to @samp{n}.
  323. X
  324. X@kindex k m
  325. X@pindex calc-moebius
  326. X@tindex moebius
  327. XThe @kbd{k m} (@code{calc-moebius}) [@code{moebius}] command computes the
  328. XMoebius ``mu'' function.  If the input number is a product of @samp{k}
  329. Xdistinct factors, this is @samp{(-1)^k}.  If the input number has any
  330. Xduplicate factors (i.e., can be divided by the same prime more than once),
  331. Xthe result is zero.
  332. X
  333. X@node Binary Functions, Matrix Functions, Scientific Functions, Top
  334. X@chapter Binary Number Functions
  335. X
  336. XThe commands in this chapter all use two-letter sequences beginning with
  337. Xthe @kbd{b} prefix.
  338. X
  339. X@cindex Binary numbers
  340. XThe ``binary'' operations actually work regardless of the currently
  341. Xdisplayed radix, although their results make the most sense in a radix
  342. Xlike 2, 8, or 16 (as obtained by the @kbd{d 2}, @kbd{d 8}, or @kbd{d 6}
  343. Xcommands, respectively).  You may also wish to enable display of leading
  344. Xzeros with @kbd{d z}.  @xref{Radix Modes}.
  345. X
  346. X@cindex Word size for binary operations
  347. XThe Calculator maintains a current @dfn{word size} @samp{w}, an
  348. Xarbitrary positive or negative integer.  For a positive word size, all
  349. Xof the binary operations described here operate modulo @samp{2^w}.  In
  350. Xparticular, negative arguments are converted to positive integers modulo
  351. X@samp{2^w} by all binary functions.@refill
  352. X
  353. XIf the word size is negative, binary operations produce 2's complement
  354. Xintegers from @samp{-(2^(-w-1))} to @samp{2^(-w-1)-1} inclusive.  Either
  355. Xmode accepts inputs in any range; the sign of @samp{w} affects only
  356. Xthe results produced.
  357. X
  358. X@kindex b c
  359. X@pindex calc-clip
  360. X@tindex clip
  361. XThe @kbd{b c} (@code{calc-clip})
  362. X[@code{clip}] command can be used to clip a number by reducing it modulo
  363. X@samp{2^w}.  The commands described in this chapter automatically clip
  364. Xtheir results to the current word size.  Note that other operations like
  365. Xaddition do not use the current word size, since integer addition
  366. Xgenerally is not ``binary.''  (However, @pxref{Simplification Modes},
  367. X@code{calc-bin-simplify-mode}.)@refill
  368. X
  369. X@kindex b w
  370. X@pindex calc-word-size
  371. XThe default word size is 32 bits.  All operations except the shifts and
  372. Xrotates allow you to specify a different word size for that one
  373. Xoperation by giving a numeric prefix argument:  @kbd{C-u 8 b c} clips the
  374. Xtop of stack to the range 0 to 255 regardless of the current word size.
  375. XTo set the word size permanently, use @kbd{b w} (@code{calc-word-size}).
  376. XBy default this command displays the current word size; with a numeric
  377. Xprefix, it sets the word size to that value.
  378. X
  379. XWhen the binary operations are written in symbolic form, they take an
  380. Xoptional second (or third) word-size parameter.  When a formula like
  381. X@samp{and(a,b)} is finally evaluated, the word size current at that time
  382. Xwill be used, but when @samp{and(a,b,-8)} is evaluated, a word size of -8
  383. Xwill always be used.  A symbolic binary function will be left in symbolic
  384. Xform unless the all of its argument(s) are integers or integer-valued floats.
  385. X
  386. XIf either or both arguments are modulo forms for which @samp{M} is a
  387. Xpower of two, that power of two is taken as the word size unless a
  388. Xnumeric prefix argument overrides it.  The current word size is never
  389. Xconsulted when modulo-power-of-two forms are involved.
  390. X
  391. X@kindex b a
  392. X@pindex calc-and
  393. X@tindex and
  394. XThe @kbd{b a} (@code{calc-and}) [@code{and}] command computes the bitwise
  395. XAND of the two numbers on the top of the stack.  In other words, for each
  396. Xof the @samp{w} binary digits of the two numbers (pairwise), the corresponding
  397. Xbit of the result is 1 if and only if both input bits are 1.
  398. X
  399. X@kindex b o
  400. X@pindex calc-or
  401. X@tindex or
  402. XThe @kbd{b o} (@code{calc-or}) [@code{or}] command computes the bitwise
  403. Xinclusive OR of two numbers.  A bit is 1 if either of the input bits, or
  404. Xboth, are 1.
  405. X
  406. X@kindex b x
  407. X@pindex calc-xor
  408. X@tindex xor
  409. XThe @kbd{b x} (@code{calc-xor}) [@code{xor}] command computes the bitwise
  410. Xexclusive OR of two numbers.  A bit is 1 if exactly one of the input bits
  411. Xis 1.
  412. X
  413. X@kindex b d
  414. X@pindex calc-diff
  415. X@tindex diff
  416. XThe @kbd{b d} (@code{calc-diff}) [@code{diff}] command computes the bitwise
  417. Xdifference of two numbers; this is defined by @samp{diff(a,b) = and(a,not(b))}.
  418. X
  419. X@kindex b n
  420. X@pindex calc-not
  421. X@tindex not
  422. XThe @kbd{b n} (@code{calc-not}) [@code{not}] command computes the bitwise
  423. XNOT of a number.  A bit is 1 if the input bit is 0 and vice-versa.
  424. X
  425. X@kindex b l
  426. X@pindex calc-lshift-binary
  427. X@tindex lsh
  428. XThe @kbd{b l} (@code{calc-lshift-binary}) [@code{lsh}] command shifts a
  429. Xnumber left by one bit, or by the number of bits specified in the numeric
  430. Xprefix argument.  A negative prefix argument performs a logical right shift,
  431. Xin which zeros are shifted in on the left.  In symbolic form, @samp{lsh(a)}
  432. Xis short for @samp{lsh(a,1)}, which in turn is short for @samp{lsh(a,n,w)}.
  433. XBits shifted ``off the end,'' according to the current word size, are lost.
  434. X
  435. X@kindex b r
  436. X@pindex calc-rshift-binary
  437. X@tindex rsh
  438. XThe @kbd{b r} (@code{calc-rshift-binary}) [@code{rsh}] command shifts a
  439. Xnumber right by one bit, or by the number of bits specified in the numeric
  440. Xprefix argument:  @samp{rsh(a,n) = lsh(a,-n)}.
  441. X
  442. X@kindex b s
  443. X@pindex calc-shift-binary
  444. X@tindex ash
  445. XThe @kbd{b s} (@code{calc-shift-binary}) [@code{ash}] command shifts a
  446. Xnumber left in the same way as @code{lsh}.  However, if shifting right,
  447. Xit performs an ``arithmetic'' shift, in which the leftmost bit (according
  448. Xto the current word size) is duplicated rather than shifting in zeros.
  449. XThis corresponds to dividing by two where the input is interpreted as a
  450. Xsigned, twos-complement number.  (The distinction between the @samp{lsh}
  451. Xand @samp{ash} operations is totally independent from whether the word
  452. Xsize is positive or negative.)
  453. X
  454. X@kindex b R
  455. X@pindex calc-rotate-binary
  456. X@tindex rot
  457. XThe @kbd{b R} (@code{calc-rotate-binary}) [@code{rot}] command rotates a
  458. Xnumber one bit to the left.  The leftmost bit (according to the current
  459. Xword size) is dropped off the left and shifted in on the right.  With a
  460. Xnumeric prefix argument, the number is rotated the specified number of
  461. Xbits to the left or right.
  462. X
  463. X@node Matrix Functions, Algebra, Binary Functions, Top
  464. X@chapter Vector and Matrix Functions
  465. X
  466. XMany of the commands described here begin with the @kbd{v} prefix.
  467. X(For convenience, the shift-@kbd{V} prefix is equivalent to @kbd{v}.)
  468. XThe commands usually apply to both plain vectors and matrices; some
  469. Xapply only to matrices or only to square matrices.  If the argument
  470. Xhas the wrong dimensions the operation is left in symbolic form.
  471. X
  472. X@menu
  473. X* Building Vectors::
  474. X* Extracting Elements::
  475. X* Vector and Matrix Arithmetic::
  476. X* Reducing and Mapping::
  477. X* Vector and Matrix Formats::
  478. X@end menu
  479. X
  480. X@node Building Vectors, Extracting Elements, Matrix Functions, Matrix Functions
  481. X@section Building Vectors
  482. X
  483. XVectors are entered and displayed using @samp{[a,b,c]} notation.
  484. XMatrices are vectors of which all elements are vectors of equal length.
  485. X(Though none of the standard Calc commands use this concept, a
  486. Xthree-dimensional matrix or rank-3 tensor could be defined as a
  487. Xvector of matrices, and so on.)  Vectors and matrices can be added,
  488. Xsubtracted, multiplied, and divided; @pxref{Basic Arithmetic}.@refill
  489. X
  490. X@kindex |
  491. X@pindex calc-concat
  492. XThe @kbd{|} (@code{calc-concat}) command ``concatenates'' two vectors
  493. Xtogether.  For example, after @kbd{[ 1 , 2 ] [ 3 , 4 ] |}, the stack
  494. Xwill contain the single vector @samp{[1, 2, 3, 4]}.  If the arguments
  495. Xare matrices, the rows of the first matrix are concatenated with the
  496. Xrows of the second.  (In other words, two matrices are just two vectors of
  497. Xrow-vectors as far as @code{calc-concat} is concerned.)
  498. X
  499. XIf either argument to @kbd{|} is a scalar (a non-vector), it is treated
  500. Xlike a one-element vector for purposes of concatenation:  @kbd{1 [ 2 , 3 ] |}
  501. Xproduces the vector @samp{[1, 2, 3]}.  Likewise, if one argument is a
  502. Xmatrix and the other is a plain vector, the vector is treated as a
  503. Xone-row matrix.
  504. X
  505. X@kindex v p (vectors)
  506. X@pindex calc-pack (vectors)
  507. XThe @kbd{v p} (@code{calc-pack}) command collects several elements from
  508. Xthe stack into a matrix, complex number, HMS form, or error form.
  509. XBy itself, it collects two
  510. Xstack elements into a complex number according to the current Polar
  511. XMode.  With a numeric prefix argument of -1 or -2, it builds a
  512. Xrectangular or polar number (respectively) regardless of the polar mode.
  513. XWith an argument of -3 it builds an HMS form from the top three elements
  514. Xon the stack.  With an argument of -4 it builds an error form from the
  515. Xtop two elements on the stack.  With an argument of -5 it builds a
  516. Xmodulo form.
  517. XWith a positive numeric prefix, it collects the specified number of
  518. Xstack elements into a vector.@refill
  519. X
  520. XThe same effect can be had by pressing @kbd{[} or @kbd{(} to push an
  521. Xincomplete vector or complex number on the stack, using @code{calc-roll-down}
  522. Xto sneak the incomplete object up past a certain number of elements, and
  523. Xthen pressing the matching @kbd{]} or @kbd{)}.
  524. X
  525. X@kindex v u (vectors)
  526. X@pindex calc-unpack (vectors)
  527. XThe @kbd{v u} (@code{calc-unpack}) command takes the vector, complex
  528. Xnumber, HMS form, or other composite object on the top of the stack and
  529. X``unpacks'' it, pushing each of its elements onto the stack as separate
  530. Xobjects.  Thus, it is the ``inverse'' of @code{calc-pack}.  If the value
  531. Xat the top of the stack is a formula, @code{calc-unpack} unpacks it by
  532. Xpushing each of the arguments of the top-level operator onto the stack.
  533. X
  534. X@kindex v d
  535. X@pindex calc-diag
  536. X@tindex diag
  537. XThe @kbd{v d} (@code{calc-diag}) [@code{diag}] function builds a diagonal
  538. Xsquare matrix.  The optional numeric prefix gives the number of rows
  539. Xand columns in the matrix.  If the value at the top of the stack is a
  540. Xvector, the elements of the vector are used as the diagonal elements; the
  541. Xprefix, if specified, must match the size of the vector.  If the value on
  542. Xthe stack is a scalar, it is used for each element on the diagonal, and
  543. Xthe prefix argument is required.
  544. X
  545. XTo build a constant square matrix, e.g., a 3x3 matrix filled with ones,
  546. Xuse @kbd{0 M-3 v d 1 +}, i.e., build a zero matrix first and then add a
  547. Xconstant value to that matrix.  (Another alternative would be to use
  548. X@code{calc-build-vector} and @code{calc-arrange-vector}; see below.)
  549. X
  550. X@kindex v i
  551. X@pindex calc-ident
  552. XThe @kbd{v i} (@code{calc-ident}) function builds an identity matrix of
  553. Xthe specified size.  It is a convenient form of @code{calc-diag} where the
  554. Xdiagonal element is always one.
  555. X
  556. X@kindex v x
  557. X@pindex calc-index
  558. X@tindex index
  559. XThe @kbd{v x} (@code{calc-index}) [@code{index}] function builds a vector
  560. Xof consecutive integers from 1 to @var{n}, where @var{n} is the numeric
  561. Xprefix argument.  If you do not provide a prefix argument, you will be
  562. Xprompted to enter a suitable number.
  563. X
  564. X@kindex v b
  565. X@pindex calc-build-vector
  566. X@tindex cvec
  567. XThe @kbd{v b} (@code{calc-build-vector}) [@code{cvec}] function builds a
  568. Xvector of @var{n} copies of the value on the top of the stack, where @var{n}
  569. Xis the numeric prefix argument.  In algebraic formulas, @samp{cvec(x,n,m)}
  570. Xcan also be used to build an @var{n}-by-@var{m} matrix of copies of @var{x}.
  571. X
  572. X@kindex v a
  573. X@pindex calc-arrange-vector
  574. X@tindex arrange
  575. X@cindex Arranging a matrix
  576. X@cindex Reshaping a matrix
  577. X@cindex Flattening a matrix
  578. XThe @kbd{v a} (@code{calc-arrange-vector}) [@code{arrange}] function
  579. Xrearranges a vector to have a certain number of columns and rows.  The
  580. Xnumeric prefix argument specifies the number of columns; if you do not
  581. Xprovide an argument, you will be prompted for the number of columns.
  582. XThe vector or matrix on the top of the stack is @dfn{flattened} into a
  583. Xplain vector.  If the number of columns is nonzero, this vector is
  584. Xthen formed into a matrix by taking successive groups of @var{n} elements.
  585. XIf the number of columns does not evenly divide the number of elements
  586. Xin the vector, the last row will be short and the result will not be
  587. Xsuitable for use as a matrix.  For example, with the matrix
  588. X@samp{[[1, 2], [3, 4]]} on the stack, @kbd{v a 4} produces
  589. X@samp{[[1, 2, 3, 4]]} (a 1x4 matrix), @kbd{v a 1} produces
  590. X@samp{[[1], [2], [3], [4]]} (a 4x1 matrix), @kbd{v a 2} produces
  591. X@samp{[[1, 2], [3, 4]]} (the original 2x2 matrix), @kbd{v a 3} produces
  592. X@samp{[[1, 2, 3], [4]]} (not a matrix), and @kbd{v a 0} produces
  593. Xthe flattened list @samp{[1, 2, 3, 4]}.
  594. X
  595. X@kindex v s
  596. X@kindex I v s
  597. X@pindex calc-sort
  598. X@tindex sort
  599. X@tindex rsort
  600. XThe @kbd{v s} (@code{calc-sort}) [@code{sort}] function sorts the elements of
  601. Xa vector into increasing order.  Non-real vector elements are sorted
  602. Xaccording to a kind of lexicographic ordering.  The @kbd{I v s} [@code{rsort}]
  603. Xfunction sorts a vector into decreasing order.
  604. X
  605. X@kindex v h
  606. X@pindex calc-histogram
  607. XThe @kbd{v h} (@code{calc-histogram}) [@code{histogram}] function builds a
  608. Xhistogram of a vector of numbers.  Vector elements are assumed to be
  609. Xintegers or real numbers in the range [0..@var{n}) for some ``number of
  610. Xbins'' @var{n}, which is the numeric prefix argument given to the
  611. Xcommand.  The result is a vector of @var{n} counts of how many times
  612. Xeach value appeared in the original vector.  Non-integers in the input
  613. Xare rounded down to integers.  Any vector elements outside the specified
  614. Xrange are ignored.  (You can tell if elements have been ignored by checking
  615. Xthat the elements in the result vector add up to the length of the input
  616. Xvector.)
  617. X
  618. XWith the Hyperbolic flag, @kbd{v h} pulls two vectors from the stack.
  619. XThe second-to-top vector is the list of numbers as before.  The top
  620. Xvector is an equal-sized list of ``weights'' to attach to the elements
  621. Xof the data vector.  For example, if the first data element is 4.2 and
  622. Xthe first weight is 10, then 10 will be added to bin 4 of the result
  623. Xvector.  Without the hyperbolic flag, every element has a weight of one.
  624. X
  625. X@node Extracting Elements, Vector and Matrix Arithmetic, Building Vectors, Matrix Functions
  626. X@section Extracting Vector and Matrix Elements
  627. X
  628. X@kindex v r
  629. X@pindex calc-mrow
  630. X@tindex mrow
  631. XThe @kbd{v r} (@code{calc-mrow}) [@code{mrow}] command extracts one row of
  632. Xthe matrix on the top of the stack, or one element of the plain vector on
  633. Xthe top of the stack.  The row or element is specified by the numeric
  634. Xprefix argument; the default is to prompt for the row or element number.
  635. XThe matrix or vector is replaced by the specified row or element in the
  636. Xform of a vector or scalar, respectively.
  637. X
  638. X@tindex mrrow
  639. XGiven a negative prefix argument, @kbd{v r} instead deletes one row or
  640. Xelement from the matrix or vector on the top of the stack.  Thus
  641. X@kbd{C-u 2 v r} replaces a matrix with its second row, but @kbd{C-u -2 v r}
  642. Xreplaces the matrix with the same matrix with its second row removed.
  643. XIn algebraic form this function is called @code{mrrow}.
  644. X
  645. X@tindex getdiag
  646. XGiven a prefix argument of zero, @kbd{v r} extracts the diagonal elements
  647. Xof a square matrix in the form of a vector.  In algebraic form this
  648. Xfunction is called @code{getdiag}.
  649. X
  650. X@kindex v c
  651. X@pindex calc-mcol
  652. X@tindex mcol
  653. X@tindex mrcol
  654. XThe @kbd{v c} (@code{calc-mcol}) [@code{mcol} or @code{mrcol}] command is
  655. Xthe analogous operation on columns of a matrix.  Given a plain vector
  656. Xit extracts (or removes) one element, just like @kbd{v r}.@refill
  657. X
  658. XTo extract a matrix element at a given row and column, use @kbd{v r} to
  659. Xextract the row as a vector, then @kbd{v c} to extract the column element
  660. Xfrom that vector.
  661. X
  662. X@kindex v l
  663. X@pindex calc-vlength
  664. X@tindex vlen
  665. XThe @kbd{v l} (@code{calc-vlength}) [@code{vlen}] command computes the
  666. Xlength of a vector.  The length of a non-vector is considered to be zero.
  667. XNote that matrices are just vectors of vectors for the purposes of this
  668. Xcommand.@refill
  669. X
  670. X@node Vector and Matrix Arithmetic, Reducing and Mapping, Extracting Elements, Matrix Functions
  671. X@section Vector and Matrix Arithmetic
  672. X
  673. XBasic arithmetic operations like addition and multiplication are defined
  674. Xfor vectors and matrices as well as for numbers.  Division of matrices, in
  675. Xthe sense of multiplying by the inverse, is supported.  (Division by a
  676. Xmatrix actually uses LU-decomposition for greater accuracy and speed.)
  677. X@xref{Basic Arithmetic}.
  678. X
  679. XThe following functions are applied element-wise if their arguments are
  680. Xvectors or matrices: @code{change-sign}, @code{conj}, @code{arg},
  681. X@code{re}, @code{im}, @code{polar}, @code{rect}, @code{clean},
  682. X@code{float}, @code{frac}.  @xref{Function Index}.@refill
  683. X
  684. X@kindex v t
  685. X@pindex calc-transpose
  686. X@tindex trn
  687. XThe @kbd{v t} (@code{calc-transpose}) [@code{trn}] command computes
  688. Xthe transpose of the matrix at the top of the stack.  If the argument
  689. Xis a plain vector, it is treated as a row vector and transposed into
  690. Xa one-column matrix.
  691. X
  692. X@kindex V J
  693. X@pindex calc-conj-transpose
  694. XThe @kbd{V J} (@code{calc-conj-transpose}) [@code{ctrn}] command computes
  695. Xthe conjugate transpose of its argument, i.e., @samp{conj(trn(x))}.
  696. X
  697. X@kindex A (vectors)
  698. X@pindex calc-abs (vectors)
  699. X@tindex abs (vectors)
  700. XThe @kbd{A} (@code{calc-abs}) [@code{abs}] command computes the
  701. XFrobenius norm of a vector or matrix argument.  This is the square
  702. Xroot of the sum of the squares of the absolute values of the
  703. Xelements of the vector or matrix.@refill
  704. X
  705. X@kindex v n
  706. X@pindex calc-rnorm
  707. X@tindex rnorm
  708. XThe @kbd{v n} (@code{calc-rnorm}) [@code{rnorm}] command computes
  709. Xthe row norm, or infinity-norm, of a vector or matrix.  For a plain
  710. Xvector, this is the maximum of the absolute values of the elements.
  711. XFor a matrix, this is the maximum of the row-absolute-value-sums,
  712. Xi.e., of the sums of the absolute values of the elements along the
  713. Xvarious rows.
  714. X
  715. X@kindex V N
  716. X@pindex calc-cnorm
  717. X@tindex cnorm
  718. XThe @kbd{V N} (@code{calc-cnorm}) [@code{cnorm}] command computes
  719. Xthe column norm, or one-norm, of a vector or matrix.  For a plain
  720. Xvector, this is the sum of the absolute values of the elements.
  721. XFor a matrix, this is the maximum of the column-absolute-value-sums.
  722. XGeneral @samp{k}-norms for @samp{k} other than one or infinity are
  723. Xnot provided.
  724. X
  725. X@kindex V C
  726. X@pindex calc-cross
  727. X@tindex cross
  728. XThe @kbd{V C} (@code{calc-cross}) [@code{cross}] command computes the
  729. Xright-handed cross product of two vectors, each of which must have
  730. Xexactly three elements.
  731. X
  732. X@kindex V I
  733. X@pindex calc-inv
  734. X@tindex inv
  735. XThe @kbd{V I} (@code{calc-inv}) [@code{inv}] command computes the
  736. Xinverse of a square matrix.  If the matrix is singular, the inverse
  737. Xoperation is left in symbolic form.  Matrix inverses are recorded so
  738. Xthat once an inverse (or determinant) of a particular matrix has been
  739. Xcomputed, the inverse and determinant of the matrix can be recomputed
  740. Xquickly in the future.
  741. X
  742. XIf the argument to @kbd{V I} is a plain number @samp{x}, this
  743. Xcommand simply computes @samp{1/x}.  The same operation is also
  744. Xavailable on the @kbd{&} key.
  745. X
  746. X@kindex V D
  747. X@pindex calc-mdet
  748. X@tindex det
  749. XThe @kbd{V D} (@code{calc-mdet}) [@code{det}] command computes the
  750. Xdeterminant of a square matrix.
  751. X
  752. X@kindex V L
  753. X@pindex calc-mlud
  754. X@tindex lud
  755. XThe @kbd{V L} (@code{calc-mlud}) [@code{lud}] command computes the
  756. XLU decomposition of a matrix.  The result is a list of three matrices
  757. Xwhich, when multiplied together left-to-right, form the original matrix.
  758. XThe first is a permutation matrix that arises from pivoting in the
  759. Xalgorithm, the second is lower-triangular with ones on the diagonal,
  760. Xand the third is upper-triangular.
  761. X
  762. X@kindex V T
  763. X@pindex calc-mtrace
  764. X@tindex tr
  765. XThe @kbd{V T} (@code{calc-mtrace}) [@code{tr}] command computes the
  766. Xtrace of a square matrix.  This is defined as the sum of the diagonal
  767. Xelements of the matrix.
  768. X
  769. X@node Reducing and Mapping, Vector and Matrix Formats, Vector and Matrix Arithmetic, Matrix Functions
  770. X@section Reducing and Mapping Vectors
  771. X
  772. XThe commands in this section allow for more general operations on the
  773. Xelements of vectors.
  774. X
  775. X@kindex V A
  776. X@pindex calc-apply
  777. X@tindex apply
  778. XThe @kbd{V A} (@code{calc-apply}) command applies a given operator to
  779. Xthe elements of a vector.  For example, applying the hypothetical
  780. Xfunction @code{f} to the vector @samp{[1, 2, 3]} would produce the
  781. Xfunction call @samp{f(1, 2, 3)}.  Applying the @code{+} function to the
  782. Xvector @samp{[a, b]} produces @samp{a + b}.  Applying @code{+} to
  783. Xthe vector @samp{[a, b, c]} is an error, since the @code{+} function
  784. Xaccepts exactly two arguments.  (While @code{calc-apply} is useful in
  785. Xsome cases, you will usually find that @code{calc-reduce} or @code{calc-map},
  786. Xdescribed below, are closer to what you want.)
  787. X
  788. XThe @kbd{V A} command prompts you to press the key corresponding to the
  789. Xdesired operator.  Press @kbd{?} for a list of all available operators.
  790. XOr, press @samp{x} at the operator prompt to select any algebraic
  791. Xfunction by name to use as the operator.  This can include functions you
  792. Xhave defined yourself using the @kbd{Z F} command.  (@xref{Algebraic
  793. XDefinitions}.)  If you give a name for which no function has been
  794. Xdefined, the result is left in symbolic form, as in @samp{f(1, 2,
  795. X3)}.@refill
  796. X
  797. XIt is also possible to apply any formula to a vector by treating that
  798. Xformula as a function.  When prompted for the operator to use, press
  799. X@kbd{'} (the apostrophe) and type your formula as an algebraic entry.
  800. XYou will then be prompted for the argument list, which defaults to a
  801. Xlist of all variables that appear in the formula, sorted into alphabetic
  802. Xorder.  For example, suppose you enter the formula @samp{x + 2y^x}.
  803. XThe default argument list would be @samp{(x y)}, which means that if
  804. Xthis function is applied to the arguments @samp{[3, 10]}, the result would
  805. Xbe @samp{3 + 2*10^3}.  If you plan to use a certain formula in this
  806. Xway often, you might consider defining it as a function with @kbd{Z F}.
  807. X
  808. XIf you press @kbd{$} at the operator prompt, the effect is similar to
  809. Xthe apostrophe except that the relevant formula is taken from top-of-stack
  810. Xinstead.  The actual vector argument of the @code{calc-apply} command is
  811. Xthen taken from the second-to-top stack position.  You will still be
  812. Xprompted for an argument list.
  813. X
  814. X@cindex Generic functions
  815. X@cindex Lambda expressions
  816. X@tindex lambda
  817. XThe algebraic notation for a ``generic function'' is @samp{lambda(a1,a2,a3,x)}
  818. Xwhere the @code{a}s are argument variables, and @code{x} is a formula
  819. Xinvolving those variables.  For example, when you use the apostrophe to
  820. Xenter the formula @samp{x + 2y^x} as an operator with argument list
  821. X@samp{(x y)}, Calc builds the generic function @samp{lambda(x, y, x + 2y^x)}
  822. Xand gives that to the @code{apply} operation instead of a function name.
  823. XYou can enter a lambda expression by hand if you wish, either directly
  824. Xwith @kbd{'}, or on the stack with @kbd{$}.  (The word @code{lambda}
  825. Xderives from Lisp notation and the theory of functions.  Note that there
  826. Xis no actual Calc function called @code{lambda}; the whole point is that
  827. Xthe @code{lambda} expression is used in its symbolic form, not evaluated
  828. Xfor an answer.)
  829. X
  830. X@kindex V R
  831. X@pindex calc-reduce
  832. X@tindex reduce
  833. XThe @kbd{V R} (@code{calc-reduce}) command applies a given binary operator
  834. Xacross all the elements of a vector.  A binary operator is a function such
  835. Xas @code{+} or @code{max} which takes two arguments.  For example, reducing
  836. X@code{+} over a vector computes the sum of the elements of the vector.
  837. XReducing @code{-} computes the first element minus each of the remaining
  838. Xelements.  Reducing @code{max} computes the maximum element, and so on.
  839. XIn general, reducing @code{f} over the vector @samp{[a, b, c, d]} produces
  840. X@samp{f(f(f(a, b), c), d)}.
  841. X
  842. X@kindex V M
  843. X@pindex calc-map
  844. X@tindex map
  845. XThe @kbd{V M} (@code{calc-map}) command applies a given operator elementwise
  846. Xto one or more vectors.  For example, mapping @code{A} [@code{abs}] produces
  847. Xa vector of the absolute values of the elements in the input vector.  Mapping
  848. X@code{+} pops two vectors from the stack, which must be of equal length, and
  849. Xproduces a vector of the pairwise sums of the elements.  If either argument
  850. Xis a non-vector, it is duplicated for each element of the other vector.  For
  851. Xexample, @kbd{[1,2,3] 2 V M ^} squares the elements of the specified vector.
  852. XWith the 2 listed first, it would have computed a vector of powers of two.
  853. XMapping a user-defined function pops as many arguments from the stack as
  854. Xthe function requires.  If you give an undefined name, you will be prompted
  855. Xfor the number of arguments to use.@refill
  856. X
  857. X@tindex add
  858. X@tindex sub
  859. X@tindex mul
  860. X@tindex div
  861. X@tindex pow
  862. X@tindex neg
  863. X@tindex mod
  864. X@tindex vconcat
  865. XIn algebraic formulas, these operations can be expressed as
  866. X@samp{apply(@var{oper}, @var{vec})},
  867. X@samp{reduce(@var{oper}, @var{vec})}, and
  868. X@samp{map(@var{oper}, @var{v1}, @var{v2}, ...)}.
  869. XFor example, @samp{map(gcd, [10,20,30], x)}.
  870. XIf @var{vec}, or all of the @var{v}'s,
  871. Xare non-vectors, the expression is left in symbolic form.  Here, @var{oper}
  872. Xis any function name like @code{max} or @code{gcd}, or a @code{lambda}
  873. Xexpression as described above.  Operators that are
  874. Xnormally written as symbols have the names @code{add}, @code{sub}, @code{mul},
  875. X@code{div}, @code{pow}, @code{neg}, @code{mod}, and @code{vconcat}.@refill
  876. X
  877. X(Experts will notice that it's not quite proper to use a variable to name
  878. Xa function, since the name @code{gcd} corresponds to the Lisp variable
  879. X@code{var-gcd} but to the function @code{calcFunc-gcd}.  Calc automatically
  880. Xmakes this translation, so you don't have to worry about it.)
  881. X
  882. X@menu
  883. X* Map/Reduce Examples::
  884. X* Mapping Matrices::
  885. X* Reducing Matrices::
  886. X@end menu
  887. X
  888. X@node Map/Reduce Examples, Mapping Matrices, Reducing and Mapping, Reducing and Mapping
  889. X@subsection Further Examples
  890. X
  891. X@cindex Statistical operations
  892. XConsider computing the mean and standard deviation of a list of values.
  893. XTo compute the mean, sum the values and divide by the
  894. Xlength of the vector.  To automate this process, type the sequence
  895. X@kbd{@key{RET} V R + @key{TAB} v l /} to duplicate the vector, compute
  896. Xits sum, bring the duplicate to the top of the stack and compute its length,
  897. Xthen divide the sum by the length.@refill
  898. X
  899. XTo compute the standard deviation, duplicate twice, compute the mean as
  900. Xabove, then type @kbd{V M - 2 V M ^ V R + @key{TAB} v l 1 - / Q}.
  901. XThe steps are to subtract the mean from the elements of the vector,
  902. Xsquare the elements, sum the elements, compute the length, then divide
  903. Xthe sum of squares by the length minus one to yield the variance; the
  904. Xstandard deviation is the square root of this.  Note that some of these
  905. Xoperations could be done more easily with other built-in commands;
  906. Xfor example, plain @kbd{-} will subtract a number from a vector elementwise,
  907. Xand @kbd{A} will compute the square root of the sum of the squares of the
  908. Xelements of a vector.  @xref{Keyboard Macros}, for a convenient way to
  909. Xprogram a key sequence like this into the Calculator for easy re-use.
  910. X
  911. XIf you have a matrix and wish to compute the means and standard deviations
  912. Xof all the rows or columns at once, just use the appropriate mapping modes
  913. Xas described in the next two sections.
  914. X
  915. X@cindex Summations (by vector mapping)
  916. XAs another example, suppose you wish to sum the function @samp{x^1.5}
  917. Xover the integers from 5 to 20.  First, build a vector of index values
  918. Xwith @kbd{v x 16 @key{RET} 4 +}.  Next, map the function @samp{x^1.5}
  919. Xover this vector with @kbd{V M ' x^1.5}.  Finally, sum the vector with
  920. X@kbd{V R +}.  This same technique can be used to compute any sum or
  921. Xproduct over a reasonably small set of index values.
  922. X
  923. X@node Mapping Matrices, Reducing Matrices, Map/Reduce Examples, Reducing and Mapping
  924. X@subsection Mapping Matrices
  925. X
  926. X@cindex Mapping modes
  927. XWhen matrices are involved in a mapping operation, they can be interpreted
  928. Xin several ways.  The current @dfn{mapping mode} controls this
  929. Xinterpretation; you set the mode by typing a two-letter combination
  930. Xstarting with @kbd{m} when Calc asks which operator to use.  Once you
  931. Xhave set a mapping mode, that mode stays in effect until you change it.
  932. XThe default mode is map-by-elements.  The mapping mode is also used by
  933. Xthe @code{calc-reduce} command, as described in the next section.
  934. XThe mapping mode has no effect if the arguments are plain vectors.
  935. X
  936. XUse @kbd{m e} to select map-by-elements mode.  In this mode, the operator
  937. Xis applied to each element of the argument matrix, or to corresponding
  938. Xelements of the argument matrices.  For example, mapping by elements
  939. Xthe sine function over a matrix replaces each matrix element by its sine.
  940. XMapping addition by elements adds corresponding matrix elements.  If
  941. Xeither argument is a scalar, the same scalar is mapped with each element
  942. Xof the other matrix.
  943. X
  944. X@tindex mapr
  945. XUse @kbd{m r} to select map-by-rows mode.  In this mode, the operator is
  946. Xapplied to each row of the argument matrix, or to corresponding rows of
  947. Xthe argument matrices.  Rows are presented to the operator as plain vectors.
  948. XFor example, mapping multiplication by rows computes a vector of dot
  949. Xproducts of the rows of the input matrices, since multiplying two plain
  950. Xvectors computes a dot product.  If either argument is scalar, or a plain
  951. Xvector of the same length as a row of the other matrix, it is mapped with
  952. Xevery row of the matrix.
  953. X
  954. X@tindex mapc
  955. XUse @kbd{m c} to select map-by-columns mode.  Essentially, this transposes
  956. Xall matrix arguments, maps by rows, then transposes the result.
  957. X
  958. X@tindex mapa
  959. XUse @kbd{m a} to select map-across mode.  In this mode, the input is a
  960. Xsingle matrix with as many columns as the operator takes arguments.
  961. XThe columns of the matrix are treated as arguments in a regular vector
  962. Xmapping operation.  For example, mapping multiplication across matrix
  963. X@samp{[[a, b], [c, d], [e, f]]} produces the vector @samp{[a*b, c*d, e*f]}.
  964. X
  965. X@tindex mapd
  966. XUse @kbd{m d} to select map-down mode.  This mode is analogous to map-across,
  967. Xexcept that it treats the rows of the matrix as arguments to the mapping
  968. Xoperation.
  969. X
  970. XAs another example, you could concatenate two matrices horizontally by
  971. Xmapping @samp{|} (vector concatenation) by rows.
  972. X
  973. X@node Reducing Matrices, , Mapping Matrices, Reducing and Mapping
  974. X@subsection Reducing Matrices
  975. X
  976. XThe current @dfn{mapping mode}, as described in the previous section,
  977. Xalso influences reduction operations when the argument is a matrix.
  978. X
  979. XUse @kbd{m e} to select reduce-by-elements mode.  In this mode, all the
  980. Xelements of the matrix are reduced into one scalar result.  For example,
  981. Xmapping addition by elements over a 3x4 matrix adds all 12 elements
  982. Xtogether.  Normal English left-to-right, top-to-bottom order is used;
  983. Xfor example, reducing subtraction by elements over the matrix
  984. X@samp{[[a, b], [c, d]]} produces @samp{(((a - b) - c) - d)}.
  985. X
  986. X@tindex reducer
  987. XUse @kbd{m r} to select reduce-by-rows mode.  In this mode, the rows of
  988. Xthe matrix are treated as vectors; the operator is applied to these
  989. Xrow vectors.  For example, mapping addition by rows produces a vector
  990. Xof the sums of the various columns of the matrix, because adding two
  991. Xvectors does a standard vector addition.  Reducing multiplication by
  992. Xrows is not very useful unless there are only two rows, in which case
  993. Xit computes the dot product of the two rows.
  994. X
  995. X@tindex reducec
  996. XUse @kbd{m c} to select reduce-by-columns mode.  In this mode, the columns
  997. Xof the matrix are treated as vectors; the operator is reduced along the
  998. Xcolumn vectors.  In other words, it transposes the argument matrix and
  999. Xthen reduces by rows.
  1000. X
  1001. X@tindex reducea
  1002. XUse @kbd{m a} to select reduce-across mode.  In this mode, the rows of the
  1003. Xmatrix are treated as vectors; each row is reduced in the usual way, and
  1004. Xthe results are collected into a vector.  For example, reducing
  1005. Xmultiplication across a matrix produces a vector of the products of the
  1006. Xvarious rows.  Reducing addition produces a vector of the sums of the
  1007. Xvarious rows, producing the same result as reducing by columns but in a
  1008. Xdifferent way.
  1009. X
  1010. X@tindex reduced
  1011. XUse @kbd{m d} to select reduce-down mode.  In this mode, the columns of the
  1012. Xmatrix are treated as vectors which are reduced individually.  This is
  1013. Xthe same as transposing the argument matrix and then reducing across.
  1014. X
  1015. X@node Vector and Matrix Formats, , Reducing and Mapping, Matrix Functions
  1016. X@section Vector and Matrix Display Formats
  1017. X
  1018. XCommands for controlling vector and matrix display use the @kbd{v} prefix
  1019. Xinstead of the usual @kbd{d} prefix.
  1020. X
  1021. X@kindex v <
  1022. X@pindex calc-matrix-left-justify
  1023. X@kindex v =
  1024. X@pindex calc-matrix-center-justify
  1025. X@kindex v >
  1026. X@pindex calc-matrix-right-justify
  1027. XThe @kbd{v <} (@code{calc-matrix-left-justify}), @kbd{v >}
  1028. X(@code{calc-matrix-right-justify}), and @kbd{v =}
  1029. X(@code{calc-matrix-center-justify}) commands control whether matrix elements
  1030. Xare justified to the left, right, or center of their columns.@refill
  1031. X
  1032. X@kindex v [
  1033. X@pindex calc-vector-brackets
  1034. X@kindex v @{
  1035. X@pindex calc-vector-braces
  1036. X@kindex v (
  1037. X@pindex calc-vector-parens
  1038. XThe @kbd{v [} (@code{calc-vector-brackets}) command turns the square
  1039. Xbrackets that surround vectors and matrices displayed in the stack on
  1040. Xand off.  The @kbd{v @{} (@code{calc-vector-braces}) and @kbd{v (}
  1041. X(@code{calc-vector-parens}) commands use curly braces or parentheses,
  1042. Xrespectively, instead of square brackets.  For example, @kbd{v @{} might
  1043. Xbe used in preparation for yanking a matrix into a buffer running
  1044. XMathematica.  (In fact, the Mathematica language mode uses this mode;
  1045. X@pxref{Mathematica Language Mode}.)  Note that, regardless of the
  1046. Xdisplay mode, either brackets and braces may be used to enter vectors,
  1047. Xand parentheses may never be used for this purpose.@refill
  1048. X
  1049. X@kindex v ,
  1050. X@pindex calc-vector-commas
  1051. XThe @kbd{v ,} (@code{calc-vector-commas}) command turns commas on and
  1052. Xoff in vector and matrix display.@refill
  1053. X
  1054. X@node Algebra, Units, Matrix Functions, Top
  1055. X@chapter Algebra
  1056. X
  1057. XThe commands for operating on algebraic formulas use the @kbd{a} key
  1058. Xprefix.  @xref{Editing Stack Entries}, to see how to manipulate formulas
  1059. Xusing regular Emacs editing commands.
  1060. X
  1061. XMany of these commands prompt for a variable name or formula.  If you
  1062. Xanswer the prompt with a blank line, the variable or formula is taken
  1063. Xfrom top-of-stack, and the normal argument for the command is taken
  1064. Xfrom second-to-top.
  1065. X
  1066. XWhen doing algebraic work, you may find several of the Calculator's
  1067. Xmodes to be helpful, including algebraic-simplification mode (@kbd{m A}),
  1068. Xalgebraic-entry mode (@kbd{m a}), fraction mode (@kbd{m f}), and
  1069. Xsymbolic mode (@kbd{m s}).  @xref{Mode Settings}, for discussions
  1070. Xof these modes.  You may also wish to select ``big'' display mode (@kbd{d B}).
  1071. X@xref{Normal Language Modes}.
  1072. X
  1073. X@menu
  1074. X* Algebraic Manipulation::
  1075. X* Rewrite Rules::
  1076. X* Logical Operations::
  1077. X* Calculus::
  1078. X@end menu
  1079. X
  1080. X@node Algebraic Manipulation, Rewrite Rules, Algebra, Algebra
  1081. X@section Algebraic Manipulation
  1082. X
  1083. X@kindex a s
  1084. X@pindex calc-simplify
  1085. XThe @kbd{a s} (@code{calc-simplify}) command applies various algebraic
  1086. Xrules to simplify a formula.  This generally entails combining like
  1087. Xterms of sums and products.  The result will be a formula which is
  1088. Xmathematically equivalent to the original one, but somewhat simpler.
  1089. X
  1090. X@kindex a e
  1091. X@pindex calc-simplify-extended
  1092. XThe @kbd{a e} (@code{calc-simplify-extended}) command is like @kbd{a s}
  1093. Xexcept that it applies some additional simplifications which are not
  1094. X``safe'' in all cases.  For example, powers are simplified according
  1095. Xto the rule @samp{(a^b)^c = a^(b c)}, even though this is not always
  1096. Xtrue (consider @samp{sqrt(a^2)} for negative @samp{a}).  The symbolic
  1097. Xintegrator uses @code{calc-simplify-extended}; one effect of this is
  1098. Xthat the integrator's results must be used with caution.  Where an
  1099. Xintegral table will often attach conditions like ``for positive @samp{a}
  1100. Xonly,'' Calc (and most other symbolic integration programs) will simply
  1101. Xproduce an unqualified result.
  1102. X
  1103. X@kindex a x
  1104. X@pindex calc-expand
  1105. XThe @kbd{a x} (@code{calc-expand}) command expands an expression by
  1106. Xapplying the distributive law.  It applies to products, quotients, and
  1107. Xpowers involving sums.  By default, it fully distributes all parts of
  1108. Xthe expression.  With the a numeric prefix argument, the distributive
  1109. Xlaw is applied only the specified number of times, then the partially
  1110. Xexpanded expression is left on the stack.
  1111. X
  1112. XCalc's automatic simplifications will sometimes reverse a partial
  1113. Xexpansion.  For example, the first step in expanding @samp{(x+1)^3} is
  1114. Xto write @samp{(x+1)*(x+1)^2}.  If @code{calc-expand} stops there and
  1115. Xtries to put this formula onto the stack, though, Calc will automatically
  1116. Xsimplify it back to @samp{(x+1)^3} form.  The solution is to turn
  1117. Xsimplification off first (@pxref{Simplification Modes}), or to run
  1118. X@kbd{a x} without a numeric prefix argument so that it expands all
  1119. Xthe way in one step.
  1120. X
  1121. X@kindex a c
  1122. X@pindex calc-collect
  1123. XThe @kbd{a c} (@code{calc-collect}) command rearranges a formula as a
  1124. Xpolynomial in a given variable.  For example, expanding @samp{(x+y)^3}
  1125. Xwith @kbd{a x} produces a sum of four terms, initially written in
  1126. Xarbitrary order.  You can use @kbd{a c x} or @kbd{a c y} to arrange
  1127. Xthe terms in order of decreasing powers of @code{x} or @code{y},
  1128. Xrespectively.  You can also give any expression about which to collect:
  1129. X@kbd{a c ln(x+1)} will collect together all terms multiplied by @samp{ln(x+1)}
  1130. Xor integer powers thereof.
  1131. X
  1132. X@kindex a b
  1133. X@pindex calc-substitute
  1134. XThe @kbd{a b} (@code{calc-substitute}) command substitutes occurrences
  1135. Xof some variable or sub-expression of an expression with a new
  1136. Xsub-expression.  For example, substituting @samp{sin(x)} to @samp{cos(y)}
  1137. Xin @samp{2 sin(x)^2 + x sin(x) + sin(2 x)} produces
  1138. X@samp{2 cos(y)^2 + x cos(y) + sin(2 x)}.
  1139. XNote that this is a purely structural substitution; the lone @code{x} and the
  1140. X@samp{sin(2 x)} stayed the same because they did not look like
  1141. X@samp{sin(x)}.  @xref{Rewrite Rules}, for a more general method for
  1142. Xdoing substitutions.@refill
  1143. X
  1144. XThe @code{calc-substitute} command normally prompts for two formulas,
  1145. Xthe old one and the new one.  If you enter a blank line for the first
  1146. Xprompt, all three arguments are taken from the stack (old, then new,
  1147. Xthen target expression).  If you type an old formula but then enter a
  1148. Xblank line for the new one, the new formula is taken from top-of-stack
  1149. Xand the target from second-to-top.  If you answer both prompts, the
  1150. Xtarget is taken from top-of-stack as usual.
  1151. X
  1152. X@node Rewrite Rules, Logical Operations, Algebraic Manipulation, Algebra
  1153. X@section Rewrite Rules
  1154. X
  1155. X@cindex Rewrite rules
  1156. X@cindex Transformations
  1157. X@cindex Pattern matching
  1158. X@kindex a r
  1159. X@pindex calc-rewrite
  1160. XThe @kbd{a r} (@code{calc-rewrite}) command makes substitutions in a
  1161. Xformula according to a specified pattern or patterns.  It is like a
  1162. Xgeneralized version of @code{calc-substitute}.  You provide Calc with
  1163. Xone or more @dfn{rewrite rules}, which take the form of vectors of
  1164. Xtwo or three elements:  @samp{[old, new]} or @samp{[old, new, cond]}.
  1165. XTo specify a set of rules, make a vector of rule vectors.  (Calc will
  1166. Xinterpret this structure as a matrix as long as the rules either all
  1167. Xinclude or all omit the @samp{cond} part.  Whether or not this is true
  1168. Xmakes no difference to @code{calc-rewrite} itself.)
  1169. X
  1170. XWhen you are prompted to enter the rewrite rules, you can either type
  1171. Xthe vector directly (as an algebraic entry), or type the name of a
  1172. XCalc variable into which you have stored a rules vector, or enter
  1173. Xa blank line in which case the rules vector will be taken from the
  1174. Xtop of the stack.  It is most convenient to store rules you use often
  1175. Xin a variable and invoke them by naming the variable.  You may also
  1176. Xwish to use @kbd{Z V} (@code{calc-permanent-variable}) to save the
  1177. Xrules permanently; @pxref{Storing Variables}.
  1178. X
  1179. XTo match a particular formula @samp{x} with a particular rule vector
  1180. X@samp{[old, new]}, Calc compares the structure of @samp{x} with the
  1181. Xstructure of @samp{old}.  Variables that appear in @samp{old} are
  1182. Xtreated as ``meta-variables''; the corresponding positions in @samp{x}
  1183. Xmay contain any sub-formulas.  For example, the pattern @samp{f(x,y)}
  1184. Xwould match the expression @samp{f(12, a+1)} with the meta-variable
  1185. X@samp{x} corresponding to 12 and with @samp{y} corresponding to
  1186. X@samp{a+1}.  However, this pattern would not match @samp{f(12)} or
  1187. X@samp{g(12, a+1)}, since there is no assignment of the meta-variables
  1188. Xthat will allow the pattern to match these expressions.  Notice that
  1189. Xif the pattern is a single variable, it will match any expression.
  1190. X
  1191. XIf a given meta-variable appears more than once in @var{old}, the
  1192. Xcorresponding sub-formulas of @samp{x} must be identical.  Thus
  1193. Xthe pattern @samp{f(x,x)} would match @samp{f(12, 12)} and
  1194. X@samp{f(a+1, a+1)} but not @samp{f(12, a+1)} or @samp{f(12, 12.0)}.
  1195. X
  1196. XThings other than variables must match exactly between the pattern
  1197. Xand the target formula.  To match a particular variable, use the
  1198. Xpseudo-function @samp{quote(v)} in the pattern.  For example, the
  1199. Xpattern @samp{x+quote(y)} matches @samp{x+y}, @samp{2+y}, or
  1200. X@samp{sin(x)+y}.
  1201. X
  1202. XPattern-matching is completely structural, not taking the algebraic
  1203. Xproperties of formulas into account.  Thus @samp{f(x+1)} matches
  1204. Xthe formula @samp{f(a+1)} but not @samp{f(a)}, @samp{f(a+2)}, or
  1205. X@samp{f(1+a)}.@refill
  1206. X
  1207. XIf the @samp{old} pattern is found to match a given formula, that
  1208. Xformula is replaced by @samp{new}, where any occurrences in @samp{new}
  1209. Xof meta-variables from the pattern are replaced with the sub-formulas
  1210. Xthat they matched.  Thus, applying the rule @samp{[f(x,y), g(y+x,x)]}
  1211. Xto @samp{f(12, a+1)} would produce @samp{g(a+13, 12)}.
  1212. X
  1213. XIf a @samp{cond} part is also present in the rule, this is an
  1214. Xadditional condition that must be satisfied before the rule is
  1215. Xaccepted.  Once @samp{old} has been successfully matched to the
  1216. Xtarget expression, @samp{cond} is evaluated (with the meta-variables
  1217. Xsubstituted for the values they matched) and simplified with
  1218. X@code{calc-simplify}.  If the result is a nonzero number, the rule
  1219. Xis accepted.  If the result is zero or if it is still a symbolic
  1220. Xformula, the rule is rejected.  @xref{Logical Operations}, for a
  1221. Xnumber of functions that return 1 or 0 according to the result of
  1222. Xsome test.  For example, the formula @samp{n > 0} simplifies to
  1223. X1 or 0 if @samp{n} is a replaced by a positive or nonpositive
  1224. Xnumber, respectively.  Thus, the rule @samp{[f(x,y), g(y+x,x), x+y > 0]}
  1225. Xwould be applied to @samp{f(0, 4)} but not to @samp{f(-3, 2)} or
  1226. X@samp{f(12, a+1)}.  (In the former case, the condition can be shown
  1227. Xnot to be satisfied; in the latter case, the condition merely cannot
  1228. Xbe shown to be satisfied, but that is enough to reject the rule.)
  1229. X
  1230. XWhen @code{calc-rewrite} is used, it takes an expression from the
  1231. Xtop of the stack and attempts to match any of the specified rules
  1232. Xto any part of the expression, starting with the whole expression
  1233. Xand then, if that fails, trying deeper and deeper sub-expressions.
  1234. XFor each part of the expression, the rules are tried in the
  1235. Xspecified order.  The first rule to match the first sub-expression
  1236. Xwins; it replaces the matched sub-expression according to the
  1237. X@samp{new} part of the rule.  By default, only one substitution
  1238. Xis made per @code{calc-rewrite} command.
  1239. X
  1240. XGiven a positive numeric prefix argument, @code{calc-rewrite} will
  1241. Xperform up to that many substitutions before stopping.  First, the
  1242. Xtop-level formula is matched and substituted repeatedly until it
  1243. Xno longer matches the pattern; then, sub-formulas are tried, and
  1244. Xso on.  If the limit still has not been reached, the top-level
  1245. Xformula is check again (in case a substitution of one of its
  1246. Xarguments causes it again to match).  This continues until no
  1247. Xfurther matches can be made, or until the limit is reached.
  1248. X
  1249. XGiven a negative numeric prefix argument, @code{calc-rewrite} will
  1250. Xmatch and substitute the top-level expression up to that many times,
  1251. Xbut will not attempt to match any sub-expressions.
  1252. X
  1253. XA prefix argument of zero is equivalent to a prefix of 25.  This is
  1254. Xmeant to be a convenient limit that is usually enough to finish the
  1255. Xjob, but not enough to be too slow in case the rule transformations
  1256. Xget into an infinite loop.
  1257. X
  1258. XAs a larger example, suppose we wish to substitute the pattern
  1259. X@samp{sin(x)^2 + cos(x)^2} with 1 in a given expression.  The rule
  1260. X@samp{[sin(x)^2 + cos(x)^2, 1]} would work, but only for the most
  1261. Xliteral matches.  This would miss the formula @samp{2 sin(a)^2 + 2
  1262. Xcos(a)^2}, for example, because our pattern does not account for both
  1263. Xterms being multiplied by an equal factor.  A more flexible solution
  1264. Xwould be to use the rule @samp{[cos(x)^2, 1 - sin(x)^2]}, followed by
  1265. Xalgebraic simplification if necessary.@refill
  1266. X
  1267. XSimilarly, the pattern @samp{[f(x, x+1), g(x)]} is not a very reliable
  1268. Xrule since it fails to match in cases like @samp{f(2, 3)}.  A better
  1269. Xrule would be @samp{[f(x,y), g(x), y=x+1]}, since the algebraic
  1270. Xsimplifier is applied to conditions but not to patterns.
  1271. X
  1272. XAs another example, we could define our own factorial function with the
  1273. Xrules @samp{[[f(0), 1], [f(n), n*f(n-1), n>0]]}.  Enter this rules
  1274. Xvector and store it in a variable: @kbd{s factrules}.  Now, given a
  1275. Xfactorial formula like @samp{f(5)} on the stack, type @samp{C-u 0 a r
  1276. Xfactrules} to apply these rules repeatedly.  After six applications,
  1277. X@code{calc-rewrite} will stop with 120 on the stack.  If we expect to
  1278. SHAR_EOF
  1279. echo "End of part 16"
  1280. echo "File calc.texinfo is continued in part 17"
  1281. echo "17" > s2_seq_.tmp
  1282. exit 0
  1283.