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

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i091:  gnucalc - GNU Emacs Calculator, v2.00, Part43/56
  4. Message-ID: <1991Nov1.183734.20953@sparky.imd.sterling.com>
  5. X-Md4-Signature: 39f99093092765d9c0ec1c2c7592f913
  6. Date: Fri, 1 Nov 1991 18:37:34 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 91
  11. Archive-name: gnucalc/part43
  12. Environment: Emacs
  13. Supersedes: gmcalc: Volume 13, Issue 27-45
  14.  
  15. ---- Cut Here and unpack ----
  16. #!/bin/sh
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file calc.texinfo continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 43; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test ! -f _shar_wnt_.tmp; then
  33.     echo 'x - still skipping calc.texinfo'
  34. else
  35. echo 'x - continuing file calc.texinfo'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
  37. certainty that the number is prime.  After a few steps, the chance that
  38. a number was mistakenly described as prime will be less than one percent.
  39. (Indeed, this is the worst case; in practice even a single iteration is
  40. quite reliable.)  After the @kbd{k p} command, the number will be reported
  41. as definitely prime or non-prime if possible, or otherwise ``probably''
  42. prime with a certain probability of error.
  43. X
  44. @tindex prime
  45. The normal @kbd{k p} command performs one iteration of the primality
  46. test.  Pressing @kbd{k p} repeatedly for the same integer will perform
  47. additional iterations.  Also, @kbd{k p} with a numeric prefix performs
  48. the specified number of iterations.  There is also an algebraic function
  49. @samp{prime(n)} or @samp{prime(n,iters)} which returns 1 if @cite{n}
  50. is (probably) prime and 0 if not.
  51. X
  52. @kindex k f
  53. @pindex calc-prime-factors
  54. @tindex prfac
  55. The @kbd{k f} (@code{calc-prime-factors}) [@code{prfac}] command
  56. attempts to decompose an integer into its prime factors.  For numbers up
  57. to 25 million, the answer is exact although it may take some time.  The
  58. result is a vector of the prime factors in increasing order.  For larger
  59. inputs, prime factors above 5000 may not be found, in which case the
  60. last number in the vector will be an unfactored integer greater than 25
  61. million.  For negative integers, the first element of the list will be
  62. @i{-1}.  For inputs @i{-1}, @i{0}, and @i{1}, the result is a list of
  63. the same number.
  64. X
  65. @kindex k n
  66. @pindex calc-next-prime
  67. @tindex nextprime
  68. The @kbd{k n} (@code{calc-next-prime}) [@code{nextprime}] command finds
  69. the next prime above a given number.  Essentially, it searches by calling
  70. @code{calc-prime-test} on successive integers until it finds one that
  71. passes the test.  This is quite fast for integers less than eight million,
  72. but once the probabilistic test comes into play the search may be rather
  73. slow.  Ordinarily this command stops for any prime that passes one iteration
  74. of the primality test.  With a numeric prefix argument, a number must pass
  75. the specified number of iterations before the search stops.  (This only
  76. matters when searching above eight million.)  You can always use additional
  77. @kbd{k p} commands to increase your certainty that the number is indeed
  78. prime.
  79. X
  80. @kindex I k n
  81. @pindex calc-prev-prime
  82. @tindex prevprime
  83. The @kbd{I k n} (@code{calc-prev-prime}) [@code{prevprime}] command
  84. analogously finds the next prime less than a given number.
  85. X
  86. @kindex k t
  87. @pindex calc-totient
  88. @tindex totient
  89. The @kbd{k t} (@code{calc-totient}) [@code{totient}] command computes the
  90. Euler ``totient'' function@c{ $\phi(n)$}
  91. @asis{}, the number of integers less than @cite{n} which
  92. are relatively prime to @cite{n}.
  93. X
  94. @kindex k m
  95. @pindex calc-moebius
  96. @tindex moebius
  97. The @kbd{k m} (@code{calc-moebius}) [@code{moebius}] command computes the
  98. @c{M\"obius $\mu$}
  99. @asis{Moebius ``mu''} function.  If the input number is a product of @cite{k}
  100. distinct factors, this is @cite{(-1)^k}.  If the input number has any
  101. duplicate factors (i.e., can be divided by the same prime more than once),
  102. the result is zero.
  103. X
  104. @node Probability Distribution Functions, , Combinatorial Functions, Scientific Functions
  105. @section Probability Distribution Functions
  106. X
  107. @noindent
  108. The functions in this section compute various probability distributions.
  109. For continuous distributions, this is the integral of the probability
  110. density function from @cite{x} to infinity.  (These are the ``upper
  111. tail'' distribution functions; there are also corresponding ``lower
  112. tail'' functions which integrate from minus infinity to @cite{x}.)
  113. For discrete distributions, the upper tail function gives the sum
  114. from @cite{x} to infinity; the lower tail function gives the sum
  115. from minus infinity up to, but not including, @cite{x}.
  116. X
  117. To integrate from @cite{x} to @cite{y}, just use the distribution
  118. function twice and subtract.  For example, the probability that a
  119. Gaussian random variable with mean 2 and standard deviation 1 will
  120. lie in the range from 2.5 to 2.8 is @samp{utpn(2.5,2,1) - utpn(2.8,2,1)}
  121. (``the probability that it is greater than 2.5, but not greater than 2.8''),
  122. or equivalently @samp{ltpn(2.8,2,1) - ltpn(2.5,2,1)}.
  123. X
  124. @kindex k B
  125. @kindex I k B
  126. @pindex calc-utpb
  127. @tindex utpb
  128. @tindex ltpb
  129. The @kbd{k B} (@code{calc-utpb}) [@code{utpb}] function uses the
  130. binomial distribution.  Push the parameters @var{n}, @var{p}, and
  131. then @var{x} onto the stack; the result (@samp{utpb(x,n,p)}) is the
  132. probability that an event will occur @var{x} or more times out
  133. of @var{n} trials, if its probability of occurring in any given
  134. trial is @var{p}.  The @kbd{I k B} [@code{ltpb}] function is
  135. the probability that the event will occur fewer than @var{x} times.
  136. X
  137. The other probability distribution functions similarly take the
  138. form @kbd{k @var{X}} (@code{calc-utp@var{x}}) [@code{utp@var{x}}]
  139. and @kbd{I k @var{X}} [@code{ltp@var{x}}], for various letters
  140. @var{x}.  The arguments to the algebraic functions are the value of
  141. the random variable first, then whatever other parameters define the
  142. distribution.  Note these are among the few Calc functions where the
  143. order of the arguments in algebraic form differs from the order of
  144. arguments as found on the stack.  (The random variable comes last on
  145. the stack, so that you can type, e.g., @kbd{2 @key{RET} 1 @key{RET} 2.5
  146. k N M-@key{RET} @key{DEL} 2.8 k N -}, using @kbd{M-@key{RET} @key{DEL}} to
  147. recover the original arguments but substitute a new value for @cite{x}.)
  148. X
  149. @kindex k C
  150. @kindex I k C
  151. @pindex calc-utpc
  152. @tindex utpc
  153. @tindex ltpc
  154. The @samp{utpc(x,v)} function uses the chi-square distribution with
  155. @cite{v} degrees of freedom.  It is the probability that a model is
  156. correct if its chi-square statistic is @cite{x}.
  157. X
  158. @kindex k F
  159. @kindex I k F
  160. @pindex calc-utpf
  161. @tindex utpf
  162. @tindex ltpf
  163. The @samp{utpf(F,v1,v2)} function uses the F distribution, used in
  164. various statistical tests.  The parameters @cite{v1} and @cite{v2}
  165. are the degrees of freedom in the numerator and denominator,
  166. respectively, used in computing the statistic @cite{F}.
  167. X
  168. @kindex k N
  169. @kindex I k N
  170. @pindex calc-utpn
  171. @tindex utpn
  172. @tindex ltpn
  173. The @samp{utpn(x,m,s)} function uses a normal (Gaussian) distribution
  174. with mean @cite{m} and standard deviation @cite{s}.  It is the
  175. probability that such a normal-distributed random variable would
  176. exceed @cite{x}.
  177. X
  178. @kindex k P
  179. @kindex I k P
  180. @pindex calc-utpp
  181. @tindex utpp
  182. @tindex ltpp
  183. The @samp{utpp(n,x)} function uses a Poisson distribution with
  184. mean @cite{x}.  It is the probability that @cite{n} or more such
  185. Poisson random events will occur.
  186. X
  187. @kindex k T
  188. @kindex I k T
  189. @pindex calc-ltpt
  190. @tindex utpt
  191. @tindex ltpt
  192. The @samp{utpt(t,v)} function uses the Student's ``t'' distribution
  193. with @cite{v} degrees of freedom.  It is the probability that a
  194. t-distributed random variable will be greater than @cite{t}.
  195. (Note:  This computes the distribution function @cite{A(t|v)}
  196. where @cite{A(0|v) = 1} and @c{$A(\infty|v) \to 0$}
  197. @cite{A(inf|v) -> 0}.  The
  198. @code{UTPT} operation on the HP-48 uses a different definition
  199. which returns half of Calc's value:  @samp{UTPT(t,v) = .5*utpt(t,v)}.)
  200. X
  201. While Calc does not provide inverses of the probability distribution
  202. functions, the @kbd{a R} command can be used to solve for the inverse.
  203. Since the distribution functions are monotonic, @kbd{a R} is guaranteed
  204. to be able to find a solution given any initial guess.
  205. @xref{Numerical Solutions}.
  206. X
  207. @node Matrix Functions, Algebra, Scientific Functions, Top
  208. @chapter Vector/Matrix Functions
  209. X
  210. @noindent
  211. Many of the commands described here begin with the @kbd{v} prefix.
  212. (For convenience, the shift-@kbd{V} prefix is equivalent to @kbd{v}.)
  213. The commands usually apply to both plain vectors and matrices; some
  214. apply only to matrices or only to square matrices.  If the argument
  215. has the wrong dimensions the operation is left in symbolic form.
  216. X
  217. Vectors are entered and displayed using @samp{[a,b,c]} notation.
  218. Matrices are vectors of which all elements are vectors of equal length.
  219. (Though none of the standard Calc commands use this concept, a
  220. three-dimensional matrix or rank-3 tensor could be defined as a
  221. vector of matrices, and so on.)
  222. X
  223. @menu
  224. * Packing and Unpacking::
  225. * Building Vectors::
  226. * Extracting Elements::
  227. * Manipulating Vectors::
  228. * Vector and Matrix Arithmetic::
  229. * Set Operations::
  230. * Statistical Operations::
  231. * Reducing and Mapping::
  232. * Vector and Matrix Formats::
  233. @end menu
  234. X
  235. @node Packing and Unpacking, Building Vectors, Matrix Functions, Matrix Functions
  236. @section Packing and Unpacking
  237. X
  238. @noindent
  239. Calc's ``pack'' and ``unpack'' commands collect stack entries to build
  240. composite objects such as vectors and complex numbers.  They are
  241. described in this chapter because they are most often used to build
  242. vectors.
  243. X
  244. @kindex v p
  245. @pindex calc-pack
  246. The @kbd{v p} (@code{calc-pack}) [@code{pack}] command collects several
  247. elements from the stack into a matrix, complex number, HMS form, error
  248. form, etc.  It uses a numeric prefix argument to specify the kind of
  249. object to be built; this argument is referred to as the ``packing mode.''
  250. If the packing mode is a nonnegative integer, a vector of that
  251. length is created.  For example, @kbd{C-u 5 v p} will pop the top
  252. five stack elements and push back a single vector of those five
  253. elements.  (@kbd{C-u 0 v p} simply creates an empty vector.)
  254. X
  255. The same effect can be had by pressing @kbd{[} to push an incomplete
  256. vector on the stack, using @code{calc-roll-down} to sneak the
  257. incomplete object up past a certain number of elements, and
  258. then pressing @kbd{]} to complete the vector.
  259. X
  260. Negative packing modes create other kinds of composite objects:
  261. X
  262. @table @cite
  263. @item -1
  264. Two values are collected to build a complex number.  For example,
  265. @kbd{5 @key{RET} 7 C-u -1 v p} creates the complex number
  266. @cite{(5, 7)}.  The result is always a rectangular complex
  267. number.  The two input values must both be real numbers,
  268. i.e., integers, fractions, or floats.  If they are not, Calc
  269. will instead build a formula like @samp{a + (0, 1) b}.  (The
  270. other packing modes also create a symbolic answer if the
  271. components are not suitable.)
  272. X
  273. @item -2
  274. Two values are collected to build a polar complex number.
  275. The first is the magnitude; the second is the phase expressed
  276. in either degrees or radians according to the current angular
  277. mode.
  278. X
  279. @item -3
  280. Three values are collected into an HMS form.  The first
  281. two values (hours and minutes) must be integers or
  282. integer-valued floats.  The third value may be any real
  283. number.
  284. X
  285. @item -4
  286. Two values are collected into an error form.  The inputs
  287. may be real numbers or formulas.
  288. X
  289. @item -5
  290. Two values are collected into a modulo form.  The inputs
  291. must be real numbers.
  292. X
  293. @item -6
  294. Two values are collected into the interval @samp{[a .. b]}.
  295. The inputs may be real numbers, HMS or date forms, or formulas.
  296. X
  297. @item -7
  298. Two values are collected into the interval @samp{[a .. b)}.
  299. X
  300. @item -8
  301. Two values are collected into the interval @samp{(a .. b]}.
  302. X
  303. @item -9
  304. Two values are collected into the interval @samp{(a .. b)}.
  305. X
  306. @item -10
  307. Two integer values are collected into a fraction.
  308. X
  309. @item -11
  310. Two values are collected into a floating-point number.
  311. The first is the mantissa; the second, which must be an
  312. integer, is the exponent.  The result is the mantissa
  313. times ten to the power of the exponent.
  314. X
  315. @item -12
  316. This is treated the same as @i{-11} by the @kbd{v p} command.
  317. When unpacking, @i{-12} specifies that a floating-point mantissa
  318. is desired.
  319. X
  320. @item -13
  321. A real number is converted into a date form.
  322. X
  323. @item -14
  324. Three numbers (year, month, day) are packed into a pure date form.
  325. X
  326. @item -15
  327. Six numbers are packed into a date/time form.
  328. @end table
  329. X
  330. With any of the two-input negative packing modes, either or both
  331. of the inputs may be vectors.  If both are vectors of the same
  332. length, the result is another vector made by packing corresponding
  333. elements of the input vectors.  If one input is a vector and the
  334. other is a plain number, the number is packed along with each vector
  335. element to produce a new vector.  For example, @kbd{C-u -4 v p}
  336. could be used to convert a vector of numbers and a vector of errors
  337. into a single vector of error forms; @kbd{C-u -5 v p} could convert
  338. a vector of numbers and a single number @var{M} into a vector of
  339. numbers modulo @var{M}.
  340. X
  341. If you don't give a prefix argument to @kbd{v p}, it takes
  342. the packing mode from the top of the stack.  The elements to
  343. be packed then begin at stack level 2.  Thus
  344. @kbd{1 @key{RET} 2 @key{RET} 4 n v p} is another way to
  345. enter the error form @samp{1 +/- 2}.
  346. X
  347. If the packing mode taken from the stack is a vector, the result is a
  348. matrix with the dimensions specified by the elements of the vector,
  349. which must each be integers.  For example, if the packing mode is
  350. @samp{[2, 3]}, then six numbers will be taken from the stack and
  351. returned in the form @samp{[[a, b, c], [d, e, f]]}.
  352. X
  353. If any elements of the vector are negative, other kinds of
  354. packing are done at that level as described above.  For
  355. example, @samp{[2, 3, -4]} takes 12 objects and creates a
  356. @c{$2\times3$}
  357. @asis{2x3} matrix of error forms: @samp{[[a +/- b, c +/- d ... ]]}.
  358. Also, @samp{[-4, -10]} will convert four integers into an
  359. error form consisting of two fractions:  @samp{a:b +/- c:d}.
  360. X
  361. @tindex pack
  362. There is an equivalent algebraic function,
  363. @samp{pack(@var{mode}, @var{items})} where @var{mode} is a
  364. packing mode (an integer or a vector of integers) and @var{items}
  365. is a vector of objects to be packed (re-packed, really) according
  366. to that mode.  For example, @samp{pack([3, -4], [a,b,c,d,e,f])}
  367. yields @samp{[a +/- b, c +/- d, e +/- f]}.  The function is
  368. left in symbolic form if the packing mode is illegal, or if the
  369. number of data items does not match the number of items required
  370. by the mode.
  371. X
  372. @kindex v u
  373. @pindex calc-unpack
  374. The @kbd{v u} (@code{calc-unpack}) command takes the vector, complex
  375. number, HMS form, or other composite object on the top of the stack and
  376. ``unpacks'' it, pushing each of its elements onto the stack as separate
  377. objects.  Thus, it is the ``inverse'' of @kbd{v p}.  If the value
  378. at the top of the stack is a formula, @kbd{v u} unpacks it by pushing
  379. each of the arguments of the top-level operator onto the stack.
  380. X
  381. You can optionally give a numeric prefix argument to @kbd{v u}
  382. to specify an explicit (un)packing mode.  If the packing mode is
  383. negative and the input is actually a vector or matrix, the result
  384. will be two or three similar vectors or matrices of the elements.
  385. For example, given the vector @samp{[a +/- b, c^2, d +/- 7]},
  386. the result of @kbd{C-u -4 v u} will be the two vectors
  387. @samp{[a, c^2, d]} and @samp{[b, 0, 7]}.
  388. X
  389. Note that the prefix argument can have an effect even when the input is
  390. not a vector.  For example, if the input is the number @i{-5}, then
  391. @kbd{c-u -1 v u} yields @i{-5} and 0 (the components of @i{-5}
  392. when viewed as a rectangular complex number); @kbd{C-u -2 v u} yields 5
  393. and 180 (assuming degrees mode); and @kbd{C-u -10 v u} yields @i{-5}
  394. and 1 (the numerator and denominator of @i{-5}, viewed as a rational
  395. number).  Plain @kbd{v u} with this input would complain that the input
  396. is not a composite object.
  397. X
  398. Unpacking mode @i{-11} converts a float into an integer mantissa and
  399. an integer exponent, where the mantissa is not divisible by 10
  400. (except that 0.0 is represented by a mantissa and exponent of 0).
  401. Unpacking mode @i{-12} converts a float into a floating-point mantissa
  402. and integer exponent, where the mantissa (for non-zero numbers)
  403. is guaranteed to lie in the range [1 .. 10).  In both cases,
  404. the mantissa is shifted left or right (and the exponent adjusted
  405. to compensate) in order to satisfy these constraints.
  406. X
  407. Positive unpacking modes are treated differently than for @kbd{v p}.
  408. A mode of 1 is much like plain @kbd{v u} with no prefix argument,
  409. except that in addition to the components of the input object,
  410. a suitable packing mode to re-pack the object is also pushed.
  411. Thus, @kbd{C-u 1 v u} followed by @kbd{v p} will re-build the
  412. original object.
  413. X
  414. A mode of 2 unpacks two levels of the object; the resulting
  415. re-packing mode will be a vector of length 2.  This might be used
  416. to unpack a matrix, say, or a vector of error forms.  Higher
  417. unpacking modes unpack the input even more deeply.
  418. X
  419. @tindex unpack
  420. There are two algebraic functions analogous to @kbd{v u}.
  421. The @samp{unpack(@var{mode}, @var{item})} function unpacks the
  422. @var{item} using the given @var{mode}, returning the result as
  423. a vector of components.  Here the @var{mode} must be an
  424. integer, not a vector.  For example, @samp{unpack(-4, a +/- b)}
  425. returns @samp{[a, b]}, as does @samp{unpack(1, a +/- b)}.
  426. X
  427. @tindex unpackt
  428. The @code{unpackt} function is like @code{unpack} but instead
  429. of returning a simple vector of items, it returns a vector of
  430. two things:  The mode, and the vector of items.  For example,
  431. @samp{unpackt(1, 2:3 +/- 1:4)} returns @samp{[-4, [2:3, 1:4]]},
  432. and @samp{unpackt(2, 2:3 +/- 1:4)} returns @samp{[[-4, -10], [2, 3, 1, 4]]}.
  433. The identity for re-building the original object is
  434. @samp{apply(pack, unpackt(@var{n}, @var{x})) = @var{x}}.  (The
  435. @code{apply} function builds a function call given the function
  436. name and a vector of arguments.)
  437. X
  438. @cindex Numerator of a fraction, extracting
  439. Subscript notation is a useful way to extract a particular part
  440. of an object.  For example, to get the numerator of a rational
  441. number, you can use @samp{unpack(-10, @var{x})_1}.
  442. X
  443. @node Building Vectors, Extracting Elements, Packing and Unpacking, Matrix Functions
  444. @section Building Vectors
  445. X
  446. @noindent
  447. Vectors and matrices can be added,
  448. subtracted, multiplied, and divided; @pxref{Basic Arithmetic}.@refill
  449. X
  450. @kindex |
  451. @pindex calc-concat
  452. @tindex |
  453. The @kbd{|} (@code{calc-concat}) command ``concatenates'' two vectors
  454. together.  For example, after @kbd{[ 1 , 2 ] [ 3 , 4 ] |}, the stack
  455. will contain the single vector @samp{[1, 2, 3, 4]}.  If the arguments
  456. are matrices, the rows of the first matrix are concatenated with the
  457. rows of the second.  (In other words, two matrices are just two vectors
  458. of row-vectors as far as @kbd{|} is concerned.)
  459. X
  460. If either argument to @kbd{|} is a scalar (a non-vector), it is treated
  461. like a one-element vector for purposes of concatenation:  @kbd{1 [ 2 , 3 ] |}
  462. produces the vector @samp{[1, 2, 3]}.  Likewise, if one argument is a
  463. matrix and the other is a plain vector, the vector is treated as a
  464. one-row matrix.
  465. X
  466. @kindex H |
  467. @tindex append
  468. The @kbd{H |} (@code{calc-append}) [@code{append}] command concatenates
  469. two vectors without any special cases.  Both inputs must be vectors.
  470. Whether or not they are matrices is not taken into account.  If either
  471. argument is a scalar, the @code{append} function is left in symbolic form.
  472. See also @code{cons} and @code{rcons} below.
  473. X
  474. @kindex I |
  475. @kindex H I |
  476. The @kbd{I |} and @kbd{H I |} commands are similar, but they use their
  477. two stack arguments in the opposite order.  Thus @kbd{I |} is equivalent
  478. to @kbd{TAB |}, but possibly more convenient and also a bit faster.
  479. X
  480. @kindex v d
  481. @pindex calc-diag
  482. @tindex diag
  483. The @kbd{v d} (@code{calc-diag}) [@code{diag}] function builds a diagonal
  484. square matrix.  The optional numeric prefix gives the number of rows
  485. and columns in the matrix.  If the value at the top of the stack is a
  486. vector, the elements of the vector are used as the diagonal elements; the
  487. prefix, if specified, must match the size of the vector.  If the value on
  488. the stack is a scalar, it is used for each element on the diagonal, and
  489. the prefix argument is required.
  490. X
  491. To build a constant square matrix, e.g., a @c{$3\times3$}
  492. @asis{3x3} matrix filled with ones,
  493. use @kbd{0 M-3 v d 1 +}, i.e., build a zero matrix first and then add a
  494. constant value to that matrix.  (Another alternative would be to use
  495. @code{calc-build-vector} and @code{calc-arrange-vector}; see below.)
  496. X
  497. @kindex v i
  498. @pindex calc-ident
  499. @tindex idn
  500. The @kbd{v i} (@code{calc-ident}) [@code{idn}] function builds an identity
  501. matrix of the specified size.  It is a convenient form of @code{calc-diag}
  502. where the diagonal element is always one.  If no prefix argument is given,
  503. this command prompts for one.
  504. X
  505. In algebraic notation, @samp{idn(a,n)} acts much like @samp{diag(a,n)},
  506. except that @cite{a} is required to be a scalar (non-vector) quantity.
  507. If @cite{n} is omitted, @samp{idn(a)} represents @cite{a} times an
  508. identity matrix of unknown size.  Calc can operate algebraically on
  509. such generic identity matrices, and if one is combined with a matrix
  510. whose size is known, it is converted automatically to an identity
  511. matrix of a suitable matching size.  The @kbd{v i} command with an
  512. argument of zero creates a generic identity matrix, @samp{idn(1)}.
  513. Note that in dimensioned matrix mode (@pxref{Matrix Mode}), generic
  514. identity matrices are immediately expanded to the current default
  515. dimensions.
  516. X
  517. @kindex v x
  518. @pindex calc-index
  519. @tindex index
  520. The @kbd{v x} (@code{calc-index}) [@code{index}] function builds a vector
  521. of consecutive integers from 1 to @var{n}, where @var{n} is the numeric
  522. prefix argument.  If you do not provide a prefix argument, you will be
  523. prompted to enter a suitable number.  If @var{n} is negative, the result
  524. is a vector of negative integers from @var{n} to @i{-1}.
  525. X
  526. With a prefix argument of just @kbd{C-u}, the @kbd{v x} command takes
  527. three values from the stack: @var{n}, @var{start}, and @var{incr} (with
  528. @var{incr} at top-of-stack).  Counting starts at @var{start} and increases
  529. by @var{incr} for successive vector elements.  If @var{start} or @var{n}
  530. is in floating-point format, the resulting vector elements will also be
  531. floats.  Note that @var{start} and @var{incr} may in fact be any kind
  532. of numbers or formulas.
  533. X
  534. When @var{start} and @var{incr} are specified, a negative @var{n} has a
  535. different interpretation:  It causes a geometric instead of arithmetic
  536. sequence to be generated.  For example, @samp{index(-3, a, b)} produces
  537. @samp{[a, a b, a b^2]}.  If you omit @var{incr} in the algebraic form,
  538. @samp{index(@var{n}, @var{start})}, the default value for @var{incr}
  539. is one for positive @var{n} or two for negative @var{n}.
  540. X
  541. @kindex v b
  542. @pindex calc-build-vector
  543. @tindex cvec
  544. The @kbd{v b} (@code{calc-build-vector}) [@code{cvec}] function builds a
  545. vector of @var{n} copies of the value on the top of the stack, where @var{n}
  546. is the numeric prefix argument.  In algebraic formulas, @samp{cvec(x,n,m)}
  547. can also be used to build an @var{n}-by-@var{m} matrix of copies of @var{x}.
  548. (Interactively, just use @kbd{v b} twice: once to build a row, then again
  549. to build a matrix of copies of that row.)
  550. X
  551. @kindex v h
  552. @kindex I v h
  553. @pindex calc-head
  554. @pindex calc-tail
  555. @tindex head
  556. @tindex tail
  557. The @kbd{v h} (@code{calc-head}) [@code{head}] function returns the first
  558. element of a vector.  The @kbd{I v h} (@code{calc-tail}) [@code{tail}]
  559. function returns the vector with its first element removed.  In both
  560. cases, the argument must be a non-empty vector.
  561. X
  562. @kindex v k
  563. @pindex calc-cons
  564. @tindex cons
  565. The @kbd{v k} (@code{calc-cons}) [@code{cons}] function takes a value @var{h}
  566. and a vector @var{t} from the stack, and produces the vector whose head is
  567. @var{h} and whose tail is @var{t}.  This is similar to @kbd{|}, except
  568. if @var{h} is itself a vector, @kbd{|} will concatenate the two vectors
  569. whereas @code{cons} will insert @var{h} at the front of the vector @var{t}.
  570. X
  571. @kindex H v h
  572. @kindex H I v h
  573. @kindex H v k
  574. @tindex rhead
  575. @tindex rtail
  576. @tindex rcons
  577. Each of these three functions also accepts the Hyperbolic flag [@code{rhead},
  578. @code{rtail}, @code{rcons}] in which case @var{t} instead represents
  579. the @emph{last} single element of the vector, with @var{h}
  580. representing the remainder of the vector.  Thus the vector
  581. @samp{[a, b, c, d] = cons(a, [b, c, d]) = rcons([a, b, c], d)}.
  582. Also, @samp{head([a, b, c, d]) = a}, @samp{tail([a, b, c, d]) = [b, c, d]},
  583. @samp{rhead([a, b, c, d]) = [a, b, c]}, and @samp{rtail([a, b, c, d]) = d}.
  584. X
  585. @node Extracting Elements, Manipulating Vectors, Building Vectors, Matrix Functions
  586. @section Extracting Vector Elements
  587. X
  588. @noindent
  589. @kindex v r
  590. @pindex calc-mrow
  591. @tindex mrow
  592. The @kbd{v r} (@code{calc-mrow}) [@code{mrow}] command extracts one row of
  593. the matrix on the top of the stack, or one element of the plain vector on
  594. the top of the stack.  The row or element is specified by the numeric
  595. prefix argument; the default is to prompt for the row or element number.
  596. The matrix or vector is replaced by the specified row or element in the
  597. form of a vector or scalar, respectively.
  598. X
  599. @cindex Permutations, applying
  600. With a prefix argument of @kbd{C-u} only, @kbd{v r} takes the index of
  601. the element or row from the top of the stack, and the vector or matrix
  602. from the second-to-top position.  If the index is itself a vector of
  603. integers, the result is a vector of the corresponding elements of the
  604. input vector, or a matrix of the corresponding rows of the input matrix.
  605. This command can be used to obtain any permutation of a vector.
  606. X
  607. With @kbd{C-u}, if the index is an interval form with integer components,
  608. it is interpreted as a range of indices and the corresponding subvector or
  609. submatrix is returned.
  610. X
  611. @cindex Subscript notation
  612. @kindex a _
  613. @pindex calc-subscript
  614. @tindex subscr
  615. @tindex _
  616. Subscript notation in algebraic formulas (@samp{a_b}) stands for the
  617. Calc function @code{subscr}, which is synonymous with @code{mrow}.
  618. Thus, @samp{[x, y, z]_k} produces @cite{x}, @cite{y}, or @cite{z} if
  619. @cite{k} is one, two, or three, respectively.  A double subscript
  620. (@samp{M_i_j}, equivalent to @samp{subscr(subscr(M, i), j)}) will
  621. access the element at row @cite{i}, column @cite{j} of a matrix.
  622. The @kbd{a _} [@code{calc-subscript}] command creates a subscript
  623. formula @samp{a_b} out of two stack entries.  (It is on the @kbd{a}
  624. ``algebra'' prefix because subscripted variables are often used
  625. purely as an algebraic notation.)
  626. X
  627. @tindex mrrow
  628. Given a negative prefix argument, @kbd{v r} instead deletes one row or
  629. element from the matrix or vector on the top of the stack.  Thus
  630. @kbd{C-u 2 v r} replaces a matrix with its second row, but @kbd{C-u -2 v r}
  631. replaces the matrix with the same matrix with its second row removed.
  632. In algebraic form this function is called @code{mrrow}.
  633. X
  634. @tindex getdiag
  635. Given a prefix argument of zero, @kbd{v r} extracts the diagonal elements
  636. of a square matrix in the form of a vector.  In algebraic form this
  637. function is called @code{getdiag}.
  638. X
  639. @kindex v c
  640. @pindex calc-mcol
  641. @tindex mcol
  642. @tindex mrcol
  643. The @kbd{v c} (@code{calc-mcol}) [@code{mcol} or @code{mrcol}] command is
  644. the analogous operation on columns of a matrix.  Given a plain vector
  645. it extracts (or removes) one element, just like @kbd{v r}.  If the
  646. index in @kbd{C-u v c} is an interval or vector and the argument is a
  647. matrix, the result is a submatrix with only the specified columns
  648. retained (and possibly permuted in the case of a vector index).@refill
  649. X
  650. To extract a matrix element at a given row and column, use @kbd{v r} to
  651. extract the row as a vector, then @kbd{v c} to extract the column element
  652. from that vector.  In algebraic formulas, it is often more convenient to
  653. use subscript notation:  @samp{m_i_j} gives row @cite{i}, column @cite{j}
  654. of matrix @cite{m}.
  655. X
  656. @kindex v s
  657. @pindex calc-subvector
  658. @tindex subvec
  659. The @kbd{v s} (@code{calc-subvector}) [@code{subvec}] command extracts
  660. a subvector of a vector.  The arguments are the vector, the starting
  661. index, and the ending index, with the ending index in the top-of-stack
  662. position.  The starting index indicates the first element of the vector
  663. to take.  The ending index indicates the first element @emph{past} the
  664. range to be taken.  Thus, @samp{subvec([a, b, c, d, e], 2, 4)} produces
  665. the subvector @samp{[b, c]}.  You could get the same result using
  666. @samp{mrow([a, b, c, d, e], [2 .. 4))}.
  667. X
  668. If either the start or the end index is zero or negative, it is
  669. interpreted as relative to the end of the vector.  Thus
  670. @samp{subvec([a, b, c, d, e], 2, -2)} also produces @samp{[b, c]}.  In
  671. the algebraic form, the end index can be omitted in which case it
  672. is taken as zero, i.e., elements from the starting element to the
  673. end of the vector are used.  The infinity symbol, @code{inf}, also
  674. has this effect when used as the ending index.
  675. X
  676. @kindex I v s
  677. @tindex rsubvec
  678. With the Inverse flag, @kbd{I v s} [@code{rsubvec}] removes a subvector
  679. from a vector.  The arguments are interpreted the same as for the
  680. normal @kbd{v s} command.  Thus, @samp{rsubvec([a, b, c, d, e], 2, 4)}
  681. produces @samp{[a, d, e]}.  It is always true that @code{subvec} and
  682. @code{rsubvec} return complementary parts of the input vector.
  683. X
  684. @xref{Selecting Subformulas}, for an alternative way to operate on
  685. vectors one element at a time.
  686. X
  687. @node Manipulating Vectors, Vector and Matrix Arithmetic, Extracting Elements, Matrix Functions
  688. @section Manipulating Vectors
  689. X
  690. @noindent
  691. @kindex v l
  692. @pindex calc-vlength
  693. @tindex vlen
  694. The @kbd{v l} (@code{calc-vlength}) [@code{vlen}] command computes the
  695. length of a vector.  The length of a non-vector is considered to be zero.
  696. Note that matrices are just vectors of vectors for the purposes of this
  697. command.@refill
  698. X
  699. @kindex H v l
  700. @tindex mdims
  701. With the Hyperbolic flag, @kbd{H v l} [@code{mdims}] computes a vector
  702. of the dimensions of a vector, matrix, or higher-order object.  For
  703. example, @samp{mdims([[a,b,c],[d,e,f]])} returns @samp{[2, 3]} since
  704. its argument is a @c{$2\times3$}
  705. @asis{2x3} matrix.
  706. X
  707. @kindex v f
  708. @pindex calc-vector-find
  709. @tindex find
  710. The @kbd{v f} (@code{calc-vector-find}) [@code{find}] command searches
  711. along a vector for the first element equal to a given target.  The target
  712. is on the top of the stack; the vector is in the second-to-top position.
  713. If a match is found, the result is the index of the matching element.
  714. Otherwise, the result is zero.  The numeric prefix argument, if given,
  715. allows you to select any starting index for the search.
  716. X
  717. @kindex v a
  718. @pindex calc-arrange-vector
  719. @tindex arrange
  720. @cindex Arranging a matrix
  721. @cindex Reshaping a matrix
  722. @cindex Flattening a matrix
  723. The @kbd{v a} (@code{calc-arrange-vector}) [@code{arrange}] command
  724. rearranges a vector to have a certain number of columns and rows.  The
  725. numeric prefix argument specifies the number of columns; if you do not
  726. provide an argument, you will be prompted for the number of columns.
  727. The vector or matrix on the top of the stack is @dfn{flattened} into a
  728. plain vector.  If the number of columns is nonzero, this vector is
  729. then formed into a matrix by taking successive groups of @var{n} elements.
  730. If the number of columns does not evenly divide the number of elements
  731. in the vector, the last row will be short and the result will not be
  732. suitable for use as a matrix.  For example, with the matrix
  733. @samp{[[1, 2], [3, 4]]} on the stack, @kbd{v a 4} produces
  734. @samp{[[1, 2, 3, 4]]} (a @c{$1\times4$}
  735. @asis{1x4} matrix), @kbd{v a 1} produces
  736. @samp{[[1], [2], [3], [4]]} (a @c{$4\times1$}
  737. @asis{4x1} matrix), @kbd{v a 2} produces
  738. @samp{[[1, 2], [3, 4]]} (the original @c{$2\times2$}
  739. @asis{2x2} matrix), @kbd{v a 3} produces
  740. @samp{[[1, 2, 3], [4]]} (not a matrix), and @kbd{v a 0} produces
  741. the flattened list @samp{[1, 2, 3, 4]}.
  742. X
  743. @cindex Sorting data
  744. @kindex V S
  745. @kindex I V S
  746. @pindex calc-sort
  747. @tindex sort
  748. @tindex rsort
  749. The @kbd{V S} (@code{calc-sort}) [@code{sort}] command sorts the elements of
  750. a vector into increasing order.  Real numbers, real infinities, and
  751. constant interval forms come first in this ordering; next come other
  752. kinds of numbers, then variables (in alphabetical order), then finally
  753. come formulas and other kinds of objects; these are sorted according
  754. to a kind of lexicographic ordering with the useful property that one
  755. one vector is less or greater than another if the first corresponding
  756. unequal elements are less or greater, respectively.  Since quoted strings
  757. are stored by Calc internally as vectors of ASCII character codes
  758. (@pxref{Strings}), this means strings are also sorted into alphabetical
  759. order by this command.
  760. X
  761. The @kbd{I V S} [@code{rsort}] command sorts a vector into decreasing order.
  762. X
  763. @cindex Permutation, inverse of
  764. @cindex Inverse of permutation
  765. @cindex Index tables
  766. @cindex Rank tables
  767. @kindex V G
  768. @kindex I V G
  769. @pindex calc-grade
  770. @tindex grade
  771. @tindex rgrade
  772. The @kbd{V G} (@code{calc-grade}) [@code{grade}, @code{rgrade}] command
  773. produces an index table or permutation vector which, if applied to the
  774. input vector (as the index of @kbd{C-u v r}, say), would sort the vector.
  775. A permutation vector is just a vector of integers from 1 to @var{n}, where
  776. each integer occurs exactly once.  One application of this is to sort a
  777. matrix of data rows using one column as the sort key; extract that column,
  778. grade it with @kbd{V G}, then use the result to reorder the original matrix
  779. with @kbd{C-u v r}.  Another interesting property of the @code{V G} command
  780. is that, if the input is itself a permutation vector, the result will
  781. be the inverse of the permutation.  The inverse of an index table is
  782. a rank table, whose @var{k}th element says where the @var{k}th original
  783. vector element will rest when the vector is sorted.  To get a rank
  784. table, just use @kbd{V G V G}.
  785. X
  786. With the Inverse flag, @kbd{I V G} produces an index table that would
  787. sort the input into decreasing order.  Note that @kbd{V S} and @kbd{V G}
  788. use a ``stable'' sorting algorithm, i.e., any two elements which are equal
  789. will not be moved out of their original order.  Generally there is no way
  790. to tell with @kbd{V S}, since two elements which are equal look the same,
  791. but with @kbd{V G} this can be an important issue.  In the matrix-of-rows
  792. example, suppose you have names and telephone numbers as two columns and
  793. you wish to sort by phone number primarily, and by name when the numbers
  794. are equal.  You can sort the data matrix by names first, and then again
  795. by phone numbers.  Because the sort is stable, any two rows with equal
  796. phone numbers will remain sorted by name even after the second sort.
  797. X
  798. @cindex Histograms
  799. @kindex V H
  800. @pindex calc-histogram
  801. @tindex histogram
  802. The @kbd{V H} (@code{calc-histogram}) [@code{histogram}] command builds a
  803. histogram of a vector of numbers.  Vector elements are assumed to be
  804. integers or real numbers in the range [0..@var{n}) for some ``number of
  805. bins'' @var{n}, which is the numeric prefix argument given to the
  806. command.  The result is a vector of @var{n} counts of how many times
  807. each value appeared in the original vector.  Non-integers in the input
  808. are rounded down to integers.  Any vector elements outside the specified
  809. range are ignored.  (You can tell if elements have been ignored by noting
  810. that the counts in the result vector don't add up to the length of the
  811. input vector.)
  812. X
  813. @kindex H V H
  814. With the Hyperbolic flag, @kbd{H V H} pulls two vectors from the stack.
  815. The second-to-top vector is the list of numbers as before.  The top
  816. vector is an equal-sized list of ``weights'' to attach to the elements
  817. of the data vector.  For example, if the first data element is 4.2 and
  818. the first weight is 10, then 10 will be added to bin 4 of the result
  819. vector.  Without the hyperbolic flag, every element has a weight of one.
  820. X
  821. @kindex v t
  822. @pindex calc-transpose
  823. @tindex trn
  824. The @kbd{v t} (@code{calc-transpose}) [@code{trn}] command computes
  825. the transpose of the matrix at the top of the stack.  If the argument
  826. is a plain vector, it is treated as a row vector and transposed into
  827. a one-column matrix.
  828. X
  829. @kindex v v
  830. @pindex calc-reverse-vector
  831. @tindex rev
  832. The @kbd{v v} (@code{calc-reverse-vector}) [@code{vec}] command reverses
  833. a vector end-for-end.  Given a matrix, it reverses the order of the rows.
  834. (To reverse the columns instead, just use @kbd{v t v v v t}.  The same
  835. principle can be used to apply other vector commands to the columns of
  836. a matrix.)
  837. X
  838. @kindex v m
  839. @pindex calc-mask-vector
  840. @tindex vmask
  841. The @kbd{v m} (@code{calc-mask-vector}) [@code{vmask}] command uses
  842. one vector as a mask to extract elements of another vector.  The mask
  843. is in the second-to-top position; the target vector is on the top of
  844. the stack.  These vectors must have the same length.  The result is
  845. the same as the target vector, but with all elements which correspond
  846. to zeros in the mask vector deleted.  Thus, for example,
  847. @samp{vmask([1, 0, 1, 0, 1], [a, b, c, d, e])} produces @samp{[a, c, e]}.
  848. X
  849. @kindex v e
  850. @pindex calc-expand-vector
  851. @tindex vexp
  852. The @kbd{v e} (@code{calc-expand-vector}) [@code{vexp}] command
  853. expands a vector according to another mask vector.  The result is a
  854. vector the same length as the mask, but with nonzero elements replaced
  855. by successive elements from the target vector.  The length of the target
  856. vector is normally the number of nonzero elements in the mask.  If the
  857. target vector is longer, its last few elements are lost.  If the target
  858. vector is shorter, the last few nonzero mask elements are left
  859. unreplaced in the result.  Thus @samp{vexp([2, 0, 3, 0, 7], [a, b])}
  860. produces @samp{[a, 0, b, 0, 7]}.
  861. X
  862. @kindex H v e
  863. With the Hyperbolic flag, @kbd{H v e} takes a filler value from the
  864. top of the stack; the mask and target vectors come from the third and
  865. second elements of the stack.  This filler is used where the mask is
  866. zero:  @samp{vexp([2, 0, 3, 0, 7], [a, b], z)} produces
  867. @samp{[a, z, c, z, 7]}.  If the filler value is itself a vector,
  868. then successive values are taken from it, so that the effect is to
  869. interleave two vectors according to the mask:
  870. @samp{vexp([2, 0, 3, 7, 0, 0], [a, b], [x, y])} produces
  871. @samp{[a, x, b, 7, y, 0]}.
  872. X
  873. Another variation on the masking idea is to combine @samp{[a, b, c, d, e]}
  874. with the mask @samp{[1, 0, 1, 0, 1]} to produce @samp{[a, 0, c, 0, e]}.
  875. You can accomplish this with @kbd{V M a &}, mapping the logical ``and''
  876. operation across the two vectors.  @xref{Logical Operations}.  Note that
  877. the @code{? :} operation also discussed there allows other types of
  878. masking using vectors.
  879. X
  880. @node Vector and Matrix Arithmetic, Set Operations, Manipulating Vectors, Matrix Functions
  881. @section Vector and Matrix Arithmetic
  882. X
  883. @noindent
  884. Basic arithmetic operations like addition and multiplication are defined
  885. for vectors and matrices as well as for numbers.  Division of matrices, in
  886. the sense of multiplying by the inverse, is supported.  (Division by a
  887. matrix actually uses LU-decomposition for greater accuracy and speed.)
  888. @xref{Basic Arithmetic}.
  889. X
  890. The following functions are applied element-wise if their arguments are
  891. vectors or matrices: @code{change-sign}, @code{conj}, @code{arg},
  892. @code{re}, @code{im}, @code{polar}, @code{rect}, @code{clean},
  893. @code{float}, @code{frac}.  @xref{Function Index}.@refill
  894. X
  895. @kindex V J
  896. @pindex calc-conj-transpose
  897. @tindex ctrn
  898. The @kbd{V J} (@code{calc-conj-transpose}) [@code{ctrn}] command computes
  899. the conjugate transpose of its argument, i.e., @samp{conj(trn(x))}.
  900. X
  901. @kindex A (vectors)
  902. @pindex calc-abs (vectors)
  903. @tindex abs (vectors)
  904. The @kbd{A} (@code{calc-abs}) [@code{abs}] command computes the
  905. Frobenius norm of a vector or matrix argument.  This is the square
  906. root of the sum of the squares of the absolute values of the
  907. elements of the vector or matrix.  If the vector is interpreted as
  908. a point in two- or three-dimensional space, this is the distance
  909. from that point to the origin.@refill
  910. X
  911. @kindex v n
  912. @pindex calc-rnorm
  913. @tindex rnorm
  914. The @kbd{v n} (@code{calc-rnorm}) [@code{rnorm}] command computes
  915. the row norm, or infinity-norm, of a vector or matrix.  For a plain
  916. vector, this is the maximum of the absolute values of the elements.
  917. For a matrix, this is the maximum of the row-absolute-value-sums,
  918. i.e., of the sums of the absolute values of the elements along the
  919. various rows.
  920. X
  921. @kindex V N
  922. @pindex calc-cnorm
  923. @tindex cnorm
  924. The @kbd{V N} (@code{calc-cnorm}) [@code{cnorm}] command computes
  925. the column norm, or one-norm, of a vector or matrix.  For a plain
  926. vector, this is the sum of the absolute values of the elements.
  927. For a matrix, this is the maximum of the column-absolute-value-sums.
  928. General @cite{k}-norms for @cite{k} other than one or infinity are
  929. not provided.
  930. X
  931. @kindex V C
  932. @pindex calc-cross
  933. @tindex cross
  934. The @kbd{V C} (@code{calc-cross}) [@code{cross}] command computes the
  935. right-handed cross product of two vectors, each of which must have
  936. exactly three elements.
  937. X
  938. @kindex & (matrices)
  939. @pindex calc-inv (matrices)
  940. @tindex inv (matrices)
  941. The @kbd{&} (@code{calc-inv}) [@code{inv}] command computes the
  942. inverse of a square matrix.  If the matrix is singular, the inverse
  943. operation is left in symbolic form.  Matrix inverses are recorded so
  944. that once an inverse (or determinant) of a particular matrix has been
  945. computed, the inverse and determinant of the matrix can be recomputed
  946. quickly in the future.
  947. X
  948. If the argument to @kbd{&} is a plain number @cite{x}, this
  949. command simply computes @cite{1/x}.  This is okay, because the
  950. @samp{/} operator also does a matrix inversion when dividing one
  951. by a matrix.
  952. X
  953. @kindex V D
  954. @pindex calc-mdet
  955. @tindex det
  956. The @kbd{V D} (@code{calc-mdet}) [@code{det}] command computes the
  957. determinant of a square matrix.
  958. X
  959. @kindex V L
  960. @pindex calc-mlud
  961. @tindex lud
  962. The @kbd{V L} (@code{calc-mlud}) [@code{lud}] command computes the
  963. LU decomposition of a matrix.  The result is a list of three matrices
  964. which, when multiplied together left-to-right, form the original matrix.
  965. The first is a permutation matrix that arises from pivoting in the
  966. algorithm, the second is lower-triangular with ones on the diagonal,
  967. and the third is upper-triangular.
  968. X
  969. @kindex V T
  970. @pindex calc-mtrace
  971. @tindex tr
  972. The @kbd{V T} (@code{calc-mtrace}) [@code{tr}] command computes the
  973. trace of a square matrix.  This is defined as the sum of the diagonal
  974. elements of the matrix.
  975. X
  976. @node Set Operations, Statistical Operations, Vector and Matrix Arithmetic, Matrix Functions
  977. @section Set Operations using Vectors
  978. X
  979. @noindent
  980. @cindex Sets, as vectors
  981. Calc includes several commands which interpret vectors as @dfn{sets} of
  982. objects.  A set is a collection of objects; any given object can appear
  983. only once in the set.  Calc stores sets as vectors of objects in
  984. sorted order.  Objects in a Calc set can be any of the usual things,
  985. such as numbers, variables, or formulas.  Two set elements are considered
  986. equal if they are identical, except that numerically equal numbers like
  987. the integer 4 and the float 4.0 are considered equal even though they
  988. are not ``identical.''  Variables are treated like plain symbols without
  989. attached values by the set operations; subtracting the set @samp{[b]}
  990. from @samp{[a, b]} always yields the set @samp{[a]} even though if
  991. the variables @samp{a} and @samp{b} both equalled 17, you might
  992. expect the answer @samp{[]}.
  993. X
  994. If a set contains interval forms, then it is assumed to be a set of
  995. real numbers.  In this case, all set operations require the elements
  996. of the set to be only things that are allowed in intervals:  Real
  997. numbers, plus and minus infinity, HMS forms, and date forms.  If
  998. there are variables or other non-real objects present in a real set,
  999. all set operations on it will be left in unevaluated form.
  1000. X
  1001. If the input to a set operation is a plain number or interval form
  1002. @var{a}, it is treated like the one-element vector @samp{[@var{a}]}.
  1003. The result is always a vector, except that if the set consists of a
  1004. single interval, the interval itself is returned instead.
  1005. X
  1006. @xref{Logical Operations}, for the @code{in} function which tests if
  1007. a certain value is a member of a given set.  To test if the set @cite{a}
  1008. is a subset of the set @cite{b}, use @samp{vdiff(a, b) = []}.
  1009. X
  1010. @kindex V +
  1011. @pindex calc-remove-duplicates
  1012. @tindex rdup
  1013. The @kbd{V +} (@code{calc-remove-duplicates}) [@code{rdup}] command
  1014. converts an arbitrary vector into set notation.  It works by sorting
  1015. the vector as if by @kbd{V S}, then removing duplicates.  (For example,
  1016. @kbd{[a, 5, 4, a, 4.0]} is sorted to @samp{[4, 4.0, 5, a, a]} and then
  1017. reduced to @samp{[4, 5, a]}).  Overlapping intervals are merged as
  1018. necessary.  You rarely need to use @kbd{V +} explicitly, since all the
  1019. other set-based commands apply @kbd{V +} to their inputs before using
  1020. them.
  1021. X
  1022. @kindex V V
  1023. @pindex calc-set-union
  1024. @tindex vunion
  1025. The @kbd{V V} (@code{calc-set-union}) [@code{vunion}] command computes
  1026. the union of two sets.  An object is in the union of two sets if and
  1027. only if it is in either (or both) of the input sets.  (You could
  1028. accomplish the same thing by concatenating the sets with @kbd{|},
  1029. then using @kbd{V +}.)
  1030. X
  1031. @kindex V ^
  1032. @pindex calc-set-intersect
  1033. @tindex vint
  1034. The @kbd{V ^} (@code{calc-set-intersect}) [@code{vint}] command computes
  1035. the intersection of two sets.  An object is in the intersection if
  1036. and only if it is in both of the input sets.  Thus if the input
  1037. sets are disjoint, i.e., if they share no common elements, the result
  1038. will be the empty vector @samp{[]}.  Note that the characters @kbd{V}
  1039. and @kbd{^} were chosen to be close to the conventional mathematical
  1040. notation for set union@c{ ($A \cup B$)}
  1041. @asis{} and intersection@c{ ($A \cap B$)}
  1042. @asis{}.
  1043. X
  1044. @kindex V -
  1045. @pindex calc-set-difference
  1046. @tindex vdiff
  1047. The @kbd{V -} (@code{calc-set-difference}) [@code{vdiff}] command computes
  1048. the difference between two sets.  An object is in the difference
  1049. @cite{A - B} if and only if it is in @cite{A} but not in @cite{B}.
  1050. Thus subtracting @samp{[y,z]} from a set will remove the elements
  1051. @samp{y} and @samp{z} if they are present.  You can also think of this
  1052. as a general @dfn{set complement} operator; if @cite{A} is the set of
  1053. all possible values, then @cite{A - B} is the ``complement'' of @cite{B}.
  1054. Obviously this only makes sense if the set of all possible values in
  1055. your problem is small enough to list in a Calc vector (or simple
  1056. enough to express in a few intervals).
  1057. X
  1058. @kindex V X
  1059. @pindex calc-set-xor
  1060. @tindex vxor
  1061. The @kbd{V X} (@code{calc-set-xor}) [@code{vxor}] command computes
  1062. the ``exclusive-or,'' or ``symmetric difference'' of two sets.
  1063. An object is in the symmetric difference of two sets if and only
  1064. if it is in one, but @emph{not} both, of the sets.  Objects that
  1065. occur in both sets ``cancel out.''
  1066. X
  1067. @kindex V ~
  1068. @pindex calc-set-complement
  1069. @tindex vcompl
  1070. The @kbd{V ~} (@code{calc-set-complement}) [@code{vcompl}] command
  1071. computes the complement of a set with respect to the real numbers.
  1072. Thus @samp{vcompl(x)} is equivalent to @samp{vdiff([-inf .. inf], x)}.
  1073. For example, @samp{vcompl([2, (3 .. 4]])} evaluates to
  1074. @samp{[[-inf .. 2), (2 .. 3], (4 .. inf]]}.
  1075. X
  1076. @kindex V F
  1077. @pindex calc-set-floor
  1078. @tindex vfloor
  1079. The @kbd{V F} (@code{calc-set-floor}) [@code{vfloor}] command
  1080. reinterprets a set as a set of integers.  Any non-integer values,
  1081. and intervals that do not enclose any integers, are removed.  Open
  1082. intervals are converted to equivalent closed intervals.  Successive
  1083. integers are converted into intervals of integers.  For example, the
  1084. complement of the set @samp{[2, 6, 7, 8]} is messy, but if you wanted
  1085. the complement with respect to the set of integers you could type
  1086. @kbd{V ~ V F} to get @samp{[[-inf .. 1], [3 .. 5], [9 .. inf]]}.
  1087. X
  1088. @kindex V E
  1089. @pindex calc-set-enumerate
  1090. @tindex venum
  1091. The @kbd{V E} (@code{calc-set-enumerate}) [@code{venum}] command
  1092. converts a set of integers into an explicit vector.  Intervals in
  1093. the set are expanded out to lists of all integers encompassed by
  1094. the intervals.  This only works for finite sets (i.e., sets which
  1095. do not involve @samp{-inf} or @samp{inf}).
  1096. X
  1097. @kindex V :
  1098. @pindex calc-set-span
  1099. @tindex vspan
  1100. The @kbd{V :} (@code{calc-set-span}) [@code{vspan}] command converts any
  1101. set of reals into an interval form that encompasses all its elements.
  1102. The lower limit will be the smallest element in the set; the upper
  1103. limit will be the largest element.  For an empty set, @samp{vspan([])}
  1104. returns the empty interval @samp{[0 .. 0)}.
  1105. X
  1106. @kindex V #
  1107. @pindex calc-set-cardinality
  1108. @tindex vcard
  1109. The @kbd{V #} (@code{calc-set-cardinality}) [@code{vcard}] command counts
  1110. the number of integers in a set.  The result is the length of the vector
  1111. that would be produced by @kbd{V E}, although the computation is much
  1112. more efficient than actually producing that vector.
  1113. X
  1114. @cindex Sets, as binary numbers
  1115. Another representation for sets that may be more appropriate in some
  1116. cases is binary numbers.  If you are dealing with sets of integers
  1117. in the range 0 to 49, you can use a 50-bit binary number where a
  1118. particular bit is 1 if the corresponding element is in the set.
  1119. @xref{Binary Functions}, for a list of commands that operate on
  1120. binary numbers.  Note that many of the above set operations have
  1121. direct equivalents in binary arithmetic:  @kbd{b o} (@code{calc-or}),
  1122. @kbd{b a} (@code{calc-and}), @kbd{b d} (@code{calc-diff}),
  1123. @kbd{b x} (@code{calc-xor}), and @kbd{b n} (@code{calc-not}),
  1124. respectively.  You can use whatever representation for sets is most
  1125. convenient to you.
  1126. X
  1127. @kindex b p
  1128. @kindex b u
  1129. @pindex calc-pack-bits
  1130. @pindex calc-unpack-bits
  1131. @tindex vpack
  1132. @tindex vunpack
  1133. The @kbd{b u} (@code{calc-unpack-bits}) [@code{vunpack}] command
  1134. converts an integer that represents a set in binary into a set
  1135. in vector/interval notation.  For example, @samp{vunpack(67)}
  1136. returns @samp{[[0 .. 1], 6]}.  If the input is negative, the set
  1137. it represents is semi-infinite: @samp{vunpack(-4) = [2 .. inf)}.
  1138. Use @kbd{V E} afterwards to expand intervals to individual
  1139. values if you wish.  Note that this command uses the @kbd{b}
  1140. (binary) prefix key.
  1141. X
  1142. The @kbd{b p} (@code{calc-pack-bits}) [@code{vpack}] command
  1143. converts the other way, from a vector or interval representing
  1144. a set of nonnegative integers into a binary integer describing
  1145. the same set.  The set may include positive infinity, but must
  1146. not include any negative numbers.  The input is interpreted as a
  1147. set of integers in the sense of @kbd{V F} (@code{vfloor}).  Beware
  1148. that a simple input like @samp{[100]} can result in a huge integer
  1149. representation (@c{$2^{100}$}
  1150. @cite{2^100}, a 31-digit integer, in this case).
  1151. X
  1152. @node Statistical Operations, Reducing and Mapping, Set Operations, Matrix Functions
  1153. @section Statistical Operations on Vectors
  1154. X
  1155. @noindent
  1156. @cindex Statistical functions
  1157. The commands in this section take vectors as arguments and compute
  1158. various statistical measures on the data stored in the vectors.  The
  1159. references used in the definitions of these functions are Bevington's
  1160. @emph{Data Reduction and Error Analysis for the Physical Sciences},
  1161. and @emph{Numerical Recipes} by Press, Flannery, Teukolsky and
  1162. Vetterling.
  1163. X
  1164. The statistical commands use the @kbd{u} prefix key followed by
  1165. a shifted letter or other character.
  1166. X
  1167. @xref{Manipulating Vectors}, for a description of @kbd{V H}
  1168. (@code{calc-histogram}).
  1169. X
  1170. @xref{Curve Fitting}, for the @kbd{a F} command for doing
  1171. least-squares fits to statistical data.
  1172. X
  1173. @xref{Probability Distribution Functions}, for several common
  1174. probability distribution functions.
  1175. X
  1176. @menu
  1177. * Single-Variable Statistics::
  1178. * Paired-Sample Statistics::
  1179. @end menu
  1180. X
  1181. @node Single-Variable Statistics, Paired-Sample Statistics, Statistical Operations, Statistical Operations
  1182. @subsection Single-Variable Statistics
  1183. X
  1184. @noindent
  1185. These functions do various statistical computations on single
  1186. vectors.  Given a numeric prefix argument, they actually pop
  1187. @var{n} objects from the stack and combine them into a data
  1188. vector.  Each object may be either a number or a vector; if a
  1189. vector, any sub-vectors inside it are ``flattened'' as if by
  1190. @kbd{v a 0}; @pxref{Manipulating Vectors}.  By default one object
  1191. is popped, which (in order to be useful) is usually a vector.
  1192. X
  1193. If an argument is a variable name, and the value stored in that
  1194. variable is a vector, then the stored vector is used.  This method
  1195. has the advantage that if your data vector is large, you can avoid
  1196. the slow process of manipulating it directly on the stack.
  1197. X
  1198. These functions are left in symbolic form if any of their arguments
  1199. are not numbers or vectors, e.g., if an argument is a formula, or
  1200. a non-vector variable.  However, formulas embedded within vector
  1201. arguments are accepted; the result is a symbolic representation
  1202. of the computation, based on the assumption that the formula does
  1203. not itself represent a vector.  All varieties of numbers such as
  1204. error forms and interval forms are acceptable.
  1205. X
  1206. Some of the functions in this section also accept a single error form
  1207. or interval as an argument.  They then describe a property of the
  1208. normal or uniform (respectively) statistical distribution described
  1209. by the argument.  The arguments are interpreted in the same way as
  1210. the @var{M} argument of the random number function @kbd{k r}.  In
  1211. particular, an interval with integer limits is considered an integer
  1212. distribution, so that @samp{[2 .. 6)} is the same as @samp{[2 .. 5]}.
  1213. An interval with at least one floating-point limit is a continuous
  1214. distribution:  @samp{[2.0 .. 6.0)} is @emph{not} the same as
  1215. @samp{[2.0 .. 5.0]}!
  1216. X
  1217. @kindex u #
  1218. @pindex calc-vector-count
  1219. @tindex vcount
  1220. The @kbd{u #} (@code{calc-vector-count}) [@code{vcount}] command
  1221. computes the number of data values represented by the inputs.
  1222. For example, @samp{vcount(1, [2, 3], [[4, 5], [], x, y])} returns 7.
  1223. If the argument is a single vector with no sub-vectors, this
  1224. simply computes the length of the vector.
  1225. X
  1226. @kindex u +
  1227. @kindex u *
  1228. @pindex calc-vector-sum
  1229. @pindex calc-vector-prod
  1230. @tindex vsum
  1231. @tindex vprod
  1232. @cindex Summations (statistical)
  1233. The @kbd{u +} (@code{calc-vector-sum}) [@code{vsum}] command
  1234. computes the sum of the data values.  The @kbd{u *}
  1235. (@code{calc-vector-prod}) [@code{vprod}] command computes the
  1236. product of the data values.  If the input is a single flat vector,
  1237. these are the same as @kbd{V R +} and @kbd{V R *}
  1238. (@pxref{Reducing and Mapping}).@refill
  1239. X
  1240. @kindex u X
  1241. @kindex u N
  1242. @pindex calc-vector-max
  1243. @pindex calc-vector-min
  1244. @tindex vmax
  1245. @tindex vmin
  1246. The @kbd{u X} (@code{calc-vector-max}) [@code{vmax}] command
  1247. computes the maximum of the data values, and the @kbd{u N}
  1248. (@code{calc-vector-min}) [@code{vmin}] command computes the minimum.
  1249. If the argument is an interval, this finds the minimum or maximum
  1250. value in the interval.  (Note that @samp{vmax([2..6)) = 5} as
  1251. described above.)  If the argument is an error form, this returns
  1252. plus or minus infinity.
  1253. X
  1254. @kindex u M
  1255. @pindex calc-vector-mean
  1256. @tindex vmean
  1257. @cindex Mean of data values
  1258. The @kbd{u M} (@code{calc-vector-mean}) [@code{vmean}] command
  1259. computes the average (arithmetic mean) of the data values.
  1260. If the inputs are error forms @c{$x$ @code{+/-} $\sigma$}
  1261. @samp{x +/- s}, this is the weighted
  1262. mean of the @cite{x} values with weights @c{$1 / \sigma^2$}
  1263. @cite{1 / s^2}.
  1264. @tex
  1265. \turnoffactive
  1266. $$ \mu = { \displaystyle \sum { x_i \over \sigma_i^2 } \over
  1267. X           \displaystyle \sum { 1 \over \sigma_i^2 } } $$
  1268. @end tex
  1269. If the inputs are not error forms, this is simply the sum of the
  1270. values divided by the count of the values.@refill
  1271. X
  1272. Note that a plain number can be considered an error form with
  1273. error @c{$\sigma = 0$}
  1274. @cite{s = 0}.  If the input to @kbd{u M} is a mixture of
  1275. plain numbers and error forms, the result is the mean of the
  1276. plain numbers, ignoring all values with non-zero errors.  (By the
  1277. above definitions it's clear that a plain number effectively
  1278. has an infinite weight, next to which an error form with a finite
  1279. weight is completely negligible.)
  1280. X
  1281. This function also works for distributions (error forms or
  1282. intervals).  The mean of an error form `@i{a} @t{+/-} @i{b}' is simply
  1283. @cite{a}.  The mean of an interval is the mean of the minimum
  1284. and maximum values of the interval.
  1285. X
  1286. @kindex I u M
  1287. @pindex calc-vector-mean-error
  1288. @tindex vmeane
  1289. The @kbd{I u M} (@code{calc-vector-mean-error}) [@code{vmeane}]
  1290. command computes the mean of the data points expressed as an
  1291. SHAR_EOF
  1292. true || echo 'restore of calc.texinfo failed'
  1293. fi
  1294. echo 'End of  part 43'
  1295. echo 'File calc.texinfo is continued in part 44'
  1296. echo 44 > _shar_seq_.tmp
  1297. exit 0
  1298. exit 0 # Just in case...
  1299. -- 
  1300. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1301. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1302. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1303. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1304.