home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-10-31 | 55.5 KB | 1,536 lines |
- Newsgroups: comp.sources.misc
- From: daveg@synaptics.com (David Gillespie)
- Subject: v24i080: gnucalc - GNU Emacs Calculator, v2.00, Part32/56
- Message-ID: <1991Oct31.214441.2310@sparky.imd.sterling.com>
- X-Md4-Signature: 72b72746ae9b5f12caa6a0ec114001b6
- Date: Thu, 31 Oct 1991 21:44:41 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: daveg@synaptics.com (David Gillespie)
- Posting-number: Volume 24, Issue 80
- Archive-name: gnucalc/part32
- Environment: Emacs
- Supersedes: gmcalc: Volume 13, Issue 27-45
-
- ---- Cut Here and unpack ----
- #!/bin/sh
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file calc.texinfo continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 32; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test ! -f _shar_wnt_.tmp; then
- echo 'x - still skipping calc.texinfo'
- else
- echo 'x - continuing file calc.texinfo'
- sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
- @samp{+} and @samp{-}. For example, the expression
- X
- @example
- 2 + 3*4*5 / 6*7^8 - 9
- @end example
- X
- @noindent
- is equivalent to
- X
- @example
- 2 + ((3*4*5) / (6*(7^8)) - 9
- @end example
- X
- @noindent
- or, in large mathematical notation,
- X
- @ifinfo
- @group
- @example
- X 3 * 4 * 5
- 2 + --------- - 9
- X 8
- X 6 * 7
- @end example
- @end group
- @end ifinfo
- @tex
- \turnoffactive
- $$ 2 + { 3 \times 4 \times 5 \over 6 \times 7^8 } - 9 $$
- @end tex
- X
- @noindent
- The result of this expression will be the number @i{-6.99999826533}.
- X
- Calc's order of evaluation is the same as for most computer languages,
- except that @samp{*} binds more strongly than @samp{/}, as the above
- example shows. As in normal mathematical notation, the @samp{*} symbol
- can often be omitted: @samp{2 a} is the same as @samp{2*a}.
- X
- Operators at the same level are evaluated from left to right, except
- that @samp{^} is evaluated from right to left. Thus, @samp{2-3-4} is
- equivalent to @samp{(2-3)-4} or @i{-5}, whereas @samp{2^3^4} is equivalent
- to @samp{2^(3^4)} (a very large integer; try it!).
- X
- If you tire of typing the apostrophe all the time, there is an
- ``algebraic mode'' you can select in which Calc automatically senses
- when you are about to type an algebraic expression. To enter this
- mode, press the two letters @w{@kbd{m a}}. (An @samp{Alg} indicator
- should appear in the Calc window's mode line.)
- X
- Press @kbd{m a}, then @kbd{2+3+4} with no apostrophe, then @key{RET}.
- X
- In algebraic mode, when you press any key that would normally begin
- entering a number (such as a digit, a decimal point, or the @kbd{_}
- key), or if you press @kbd{(} or @kbd{[}, Calc automatically begins
- an algebraic entry.
- X
- Functions which do not have operator symbols like @samp{+} and @samp{*}
- must be entered in formulas using function-call notation. For example,
- the function name corresponding to the square-root key @kbd{Q} is
- @code{sqrt}. To compute a square root in a formula, you would use
- the notation @samp{sqrt(@var{x})}.
- X
- Press the apostrophe, then type @kbd{sqrt(5*2) - 3}. The result should
- be @cite{0.16227766017}.
- X
- Note that if the formula begins with a function name, you need to use
- the apostrophe even if you are in algebraic mode. If you type @kbd{arcsin}
- out of the blue, the @kbd{a r} will be taken as an Algebraic Rewrite
- command, and the @kbd{csin} will be taken as the name of the rewrite
- rule to use!
- X
- Some people prefer to enter complex numbers and vectors in algebraic
- form because they find RPN entry with incomplete objects to be too
- distracting, even though they otherwise use Calc as an RPN calculator.
- X
- Still in algebraic mode, type:
- X
- @group
- @smallexample
- 1: (2, 3) 2: (2, 3) 1: (8, -1) 2: (8, -1) 1: (9, -1)
- X . 1: (1, -2) . 1: 1 .
- X . .
- X
- X (2,3) RET (1,-2) RET * 1 RET +
- @end smallexample
- @end group
- X
- Algebraic mode allows us to enter complex numbers without pressing
- an apostrophe first, but it also means we need to press @key{RET}
- after every entry, even for a simple number like @cite{1}.
- X
- (You can type @kbd{C-u m a} to enable a special ``incomplete algebraic
- mode'' in which the @kbd{(} and @kbd{[} keys use algebraic entry even
- though regular numeric keys still use RPN numeric entry. There is also
- a ``total algebraic mode'', started by typing @kbd{m t}, in which all
- normal keys begin algebraic entry. You must then use the @key{META} key
- to type Calc commands: @kbd{M-m t} to get back out of total algebraic
- mode, @kbd{M-q} to quit, etc.)
- X
- If you're still in algebraic mode, press @kbd{m a} again to turn it off.
- X
- Actual non-RPN calculators use a mixture of algebraic and RPN styles.
- In general, operators of two numbers (like @kbd{+} and @kbd{*})
- use algebraic form, but operators of one number (like @kbd{n} and @kbd{Q})
- use RPN form. Also, a non-RPN calculator allows you to see the
- intermediate results of a calculation as you go along. You can
- accomplish this in Calc by performing your calculation as a series
- of algebraic entries, using the @kbd{$} sign to tie them together.
- In an algebraic formula, @kbd{$} represents the number on the top
- of the stack. Here, we perform the calculation @c{$\sqrt{2\times4+1}$}
- @cite{sqrt(2*4+1)},
- which on a traditional calculator would be done by pressing
- @kbd{2 * 4 + 1 =} and then the square-root key.
- X
- @group
- @smallexample
- 1: 8 1: 9 1: 3
- X . . .
- X
- X ' 2*4 RET $+1 RET Q
- @end smallexample
- @end group
- X
- @noindent
- Notice that we didn't need to press an apostrophe for the @kbd{$+1},
- because the dollar sign always begins an algebraic entry.
- X
- (@bullet{}) @strong{Exercise 1.} How could you get the same effect as
- pressing @kbd{Q} but using an algebraic entry instead? How about
- if the @kbd{Q} key on your keyboard were broken?
- @xref{Algebraic Answer 1, 1}. (@bullet{})
- X
- Algebraic formulas can include @dfn{variables}. To store in a
- variable, press @kbd{s s}, then type the variable name, then press
- @key{RET}. (There are actually two flavors of store command:
- @kbd{s s} stores a number in a variable but also leaves the number
- on the stack, while @w{@kbd{s t}} removes a number from the stack and
- stores it in the variable.) A variable name should consist of one
- or more letters or digits, beginning with a letter.
- X
- @group
- @smallexample
- 1: 17 . 1: a + a^2 1: 306
- X . . .
- X
- X 17 s t a RET ' a+a^2 RET =
- @end smallexample
- @end group
- X
- @noindent
- The @kbd{=} key @dfn{evaluates} a formula by replacing all its
- variables by the values that were stored in them.
- X
- For RPN calculations, you can recall a variable's value on the
- stack either by entering its name as a formula and pressing @kbd{=},
- or by using the @kbd{s r} command.
- X
- @group
- @smallexample
- 1: 17 2: 17 3: 17 2: 17 1: 306
- X . 1: 17 2: 17 1: 289 .
- X . 1: 2 .
- X .
- X
- X s r a RET ' a RET = 2 ^ +
- @end smallexample
- @end group
- X
- If you press a single digit for a variable name (as in @kbd{s t 3}, you
- get one of ten @dfn{quick variables} @code{q0} through @code{q9}.
- They are ``quick'' simply because you don't have to type the letter
- @code{q} or the @key{RET} after their names. In fact, you can type
- simply @kbd{s 3} as a shorthand for @kbd{s s 3}, and likewise for
- @kbd{t 3} and @w{@kbd{r 3}}.
- X
- Any variables in an algebraic formula for which you have not stored
- values are left alone, even when you evaluate the formula.
- X
- @group
- @smallexample
- 1: 2 a + 2 b 1: 34 + 2 b
- X . .
- X
- X ' 2a+2b RET =
- @end smallexample
- @end group
- X
- Calls to function names which are undefined in Calc are also left
- alone, as are calls for which the value is undefined.
- X
- @group
- @smallexample
- 1: 2 + log10(0) + log10(x) + log10(5, 6) + foo(3)
- X .
- X
- X ' log10(100) + log10(0) + log10(x) + log10(5,6) + foo(3) RET
- @end smallexample
- @end group
- X
- @noindent
- In this example, the first call to @code{log10} works, but the other
- calls are not evaluated. In the second call, the logarithm is
- undefined for that value of the argument; in the third, the argument
- is symbolic, and in the fourth, there are too many arguments. In the
- fifth case, there is no function called @code{foo}. You will see a
- ``Wrong number of arguments'' message referring to @samp{log10(5,6)}.
- Press the @kbd{w} (``why'') key to see any other messages that may
- have arisen from the last calculation. In this case you will get
- ``logarithm of zero,'' then ``number expected: @code{x}''. Calc
- automatically displays the first message only if the message is
- sufficiently important; for example, Calc considers ``wrong number
- of arguments'' and ``logarithm of zero'' to be important enough to
- report automatically, while a message like ``number expected: @code{x}''
- will only show up if you explicitly press the @kbd{w} key.
- X
- (@bullet{}) @strong{Exercise 2.} Joe entered the formula @samp{2 x y},
- stored 5 in @code{x}, pressed @kbd{=}, and got the expected result,
- @samp{10 y}. He then tried the same for the formula @samp{2 x (1+y)},
- expecting @samp{10 (1+y)}, but it didn't work. Why not?
- @xref{Algebraic Answer 2, 2}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 3.} What result would you expect
- @kbd{1 @key{RET} 0 /} to give? What if you then type @kbd{0 *}?
- @xref{Algebraic Answer 3, 3}. (@bullet{})
- X
- One interesting way to work with variables is to use the
- @dfn{evaluates-to} (@samp{=>}) operator. It works like this:
- Enter a formula algebraically in the usual way, but follow
- the formula with an @samp{=>} symbol. (There is also an @kbd{s =}
- command which builds an @samp{=>} formula using the stack.) On
- the stack, you will see two copies of the formula with an @samp{=>}
- between them. The lefthand formula is exactly like you typed it;
- the righthand formula has been evaluated as if by typing @kbd{=}.
- X
- @group
- @smallexample
- 2: 2 + 3 => 5 2: 2 + 3 => 5
- 1: 2 a + 2 b => 34 + 2 b 1: 2 a + 2 b => 20 + 2 b
- X . .
- X
- ' 2+3 => RET ' 2a+2b RET s = 10 s t a RET
- @end smallexample
- @end group
- X
- @noindent
- Notice that the instant we stored a new value in @code{a}, all
- @samp{=>} operators already on the stack that referred to @cite{a}
- were updated to use the new value. With @samp{=>}, you can push a
- set of formulas on the stack, then change the variables experimentally
- to see the effects on the formulas' values.
- X
- You can also ``unstore'' a variable when you are through with it:
- X
- @group
- @smallexample
- 2: 2 + 5 => 5
- 1: 2 a + 2 b => 2 a + 2 b
- X .
- X
- X s u a RET
- @end smallexample
- @end group
- X
- We will encounter formulas involving variables and functions again
- when we discuss the algebra and calculus features of the Calculator.
- X
- @node Undo Tutorial, Modes Tutorial, Algebraic Tutorial, Basic Tutorial
- @subsection Undo and Redo
- X
- @noindent
- If you make a mistake, you can usually correct it by pressing shift-@kbd{U},
- the ``undo'' command. First, clear the stack (@kbd{M-0 DEL}) and exit
- and restart Calc (@kbd{M-# M-# M-# M-#}) to make sure things start off
- with a clean slate. Now:
- X
- @group
- @smallexample
- 1: 2 2: 2 1: 8 2: 2 1: 6
- X . 1: 3 . 1: 3 .
- X . .
- X
- X 2 RET 3 ^ U *
- @end smallexample
- @end group
- X
- You can undo any number of times. Calc keeps a complete record of
- all you have done since you last opened the Calc window. After the
- above example, you could type:
- X
- @group
- @smallexample
- 1: 6 2: 2 1: 2 . .
- X . 1: 3 .
- X .
- X (error)
- X U U U U
- @end smallexample
- @end group
- X
- You can also type @kbd{D} to ``redo'' a command that you have undone
- mistakenly.
- X
- @group
- @smallexample
- X . 1: 2 2: 2 1: 6 1: 6
- X . 1: 3 . .
- X .
- X (error)
- X D D D D
- @end smallexample
- @end group
- X
- @noindent
- It was not possible to redo past the @cite{6}, since that was placed there
- by something other than an undo command.
- X
- @cindex Time travel
- You can think of undo and redo as a sort of ``time machine.'' Press
- @kbd{U} to go backward in time, @kbd{D} to go forward. If you go
- backward and do something (like @kbd{*}) then, as any science fiction
- reader knows, you have changed your future and you cannot go forward
- again. Thus, the inability to redo past the @cite{6} even though there
- was an earlier undo command.
- X
- You can always recall an earlier result using the Trail. We've ignored
- the trail so far, but it has been faithfully recording everything we
- did since we loaded the Calculator. If the Trail is not displayed,
- press @kbd{t d} now to turn it on.
- X
- Let's try grabbing an earlier result. The @cite{8} we computed was
- undone by a @kbd{U} command, and was lost even to Redo when we pressed
- @kbd{*}, but it's still there in the trail. There should be a little
- @samp{>} arrow (the @dfn{trail pointer}) resting on the last trail
- entry. If there isn't, press @kbd{t ]} to reset the trail pointer.
- Now, press @w{@kbd{t p}} to move the arrow onto the line containing
- @cite{8}, and press @w{@kbd{t y}} to ``yank'' that number back onto the
- stack.
- X
- If you press @kbd{t ]} again, you will see that even our Yank command
- went into the trail.
- X
- Let's go further back in time. Earlier in the tutorial we computed
- a huge integer using the formula @samp{2^3^4}. We don't remember
- what it was, but the first digits were ``241''. Press @kbd{t r}
- (which stands for trail-search-reverse), then type @kbd{241}.
- The trail cursor will jump back to the next previous occurrence of
- the string ``241'' in the trail. This is just a regular Emacs
- incremental search; you can now press @kbd{C-s} or @kbd{C-r} to
- continue the search forwards or backwards as you like.
- X
- To finish the search, press @key{RET}. This halts the incremental
- search and leaves the trail pointer at the thing we found. Now we
- can type @kbd{t y} to yank that number onto the stack. If we hadn't
- remembered the ``241'', we could simply have searched for @kbd{2^3^4},
- then pressed @kbd{@key{RET} t n} to halt and then move to the next item.
- X
- You may have noticed that all the trail-related commands begin with
- the letter @kbd{t}. (The store-and-recall commands, on the other hand,
- all began with @kbd{s}.) Calc has so many commands that there aren't
- enough keys for all of them, so various commands are grouped into
- two-letter sequences where the first letter is called the @dfn{prefix}
- key. If you type a prefix key by accident, you can press @kbd{C-g}
- to cancel it. (In fact, you can press @kbd{C-g} to cancel almost
- anything in Emacs.) To get help on a prefix key, press that key
- followed by @kbd{?}. Some prefixes have several lines of help,
- so you need to press @kbd{?} repeatedly to see them all.
- X
- Try pressing @kbd{t ?} now. You will see a line of the form,
- X
- @smallexample
- trail/time: Display; Fwd, Back; Next, Prev, Here, [, ]; Yank: [MORE] t-
- @end smallexample
- X
- @noindent
- The word ``trail'' indicates that the @kbd{t} prefix key contains
- trail-related commands. Each entry on the line shows one command,
- with a single capital letter showing which letter you press to get
- that command. We have used @kbd{t n}, @kbd{t p}, @kbd{t ]}, and
- @kbd{t y} so far. The @samp{[MORE]} means you can press @kbd{?}
- again to see more @kbd{t}-prefix comands. Notice that the commands
- are roughly divided (by semicolons) into related groups.
- X
- When you are in the help display for a prefix key, the prefix is
- still active. If you press another key, like @kbd{y} for example,
- it will be interpreted as a @kbd{t y} command. If all you wanted
- was to look at the help messages, press @kbd{C-g} afterwards to cancel
- the prefix.
- X
- One more way to correct an error is by editing the stack entries.
- The actual Stack buffer is marked read-only and must not be edited
- directly, but you can press @kbd{`} (the backquote or accent grave)
- to edit a stack entry.
- X
- Try entering @samp{3.141439} now. If this is supposed to represent
- @c{$\pi$}
- @cite{pi}, it's got several errors. Press @kbd{`} to edit this number.
- Now use the normal Emacs cursor motion and editing keys to change
- the second 4 to a 5, and to transpose the 3 and the 9. When you
- press @key{RET}, the number on the stack will be replaced by your
- new number. This works for formulas, vectors, and all other types
- of values you can put on the stack. The @kbd{`} key also works
- during entry of a number or algebraic formula.
- X
- @node Modes Tutorial, , Undo Tutorial, Basic Tutorial
- @subsection Mode-Setting Commands
- X
- @noindent
- Calc has many types of @dfn{modes} that affect the way it interprets
- your commands or the way it displays data. We have already seen one
- mode, namely algebraic mode. There are many others, too; we'll
- try some of the most common ones here.
- X
- Perhaps the most fundamental mode in Calc is the current @dfn{precision}.
- Notice the @samp{12} on the Calc window's mode line:
- X
- @smallexample
- --%%-Calc: 12 Deg (Calculator)----All------
- @end smallexample
- X
- @noindent
- Most of the symbols there are Emacs things you don't need to worry
- about, but the @samp{12} and the @samp{Deg} are mode indicators.
- The @samp{12} means that calculations should always be carried to
- 12 significant figures. That is why, when we type @kbd{1 @key{RET} 7 /},
- we get @cite{0.142857142857} with exactly 12 digits, not counting
- leading and trailing zeros.
- X
- You can set the precision to anything you like by pressing @kbd{p},
- then entering a suitable number. Try pressing @kbd{p 30 @key{RET}},
- then doing @kbd{1 @key{RET} 7 /} again:
- X
- @group
- @smallexample
- 1: 0.142857142857
- 2: 0.142857142857142857142857142857
- X .
- @end smallexample
- @end group
- X
- Although the precision can be set arbitrarily high, Calc always
- has to have @emph{some} value for the current precision. After
- all, the true value @cite{1/7} is an infinitely repeating decimal;
- Calc has to stop somewhere.
- X
- Of course, calculations are slower the more digits you request.
- Press @w{@kbd{p 12}} now to set the precision back down to the default.
- X
- Calculations always use the current precision. For example, even
- though we have a 30-digit value for @cite{1/7} on the stack, if
- we use it in a calculation in 12-digit mode it will be rounded
- down to 12 digits before it is used. Try it; press @key{RET} to
- duplicate the number, then @w{@kbd{1 +}}. Notice that the @key{RET}
- key didn't round the number, because it doesn't do any calculation.
- But the instant we pressed @kbd{+}, the number was rounded down.
- X
- @group
- @smallexample
- 1: 0.142857142857
- 2: 0.142857142857142857142857142857
- 3: 1.14285714286
- X .
- @end smallexample
- @end group
- X
- @noindent
- In fact, since we added a digit on the left, we had to lose one
- digit on the right from even the 12-digit value of @cite{1/7}.
- X
- How did we get more than 12 digits when we computed @samp{2^3^4}? The
- answer is that Calc makes a distinction between @dfn{integers} and
- @dfn{floating-point} numbers, or @dfn{floats}. An integer is a number
- that does not contain a decimal point. There is no such thing as an
- ``infinitely repeating fraction integer,'' so Calc doesn't have to limit
- itself. If you asked for @samp{2^10000} (don't try this!), you would
- have to wait a long time but you would eventually get an exact answer.
- If you ask for @samp{2.^10000}, you will quickly get an answer which is
- correct only to 12 places. The decimal point tells Calc that it should
- use floating-point arithmetic to get the answer, not exact integer
- arithmetic.
- X
- You can use the @kbd{F} (@code{calc-floor}) command to convert a
- floating-point value to an integer, and @kbd{c f} (@code{calc-float})
- to convert an integer to floating-point form.
- X
- Let's try entering that last calculation:
- X
- @group
- @smallexample
- 1: 2. 2: 2. 1: 1.99506311689e3010
- X . 1: 10000 .
- X .
- X
- X 2.0 RET 10000 RET ^
- @end smallexample
- @end group
- X
- @noindent
- Notice the letter @samp{e} in there. It represents ``times ten to the
- power of,'' and is used by Calc automatically whenever writing the
- number out fully would introduce more extra zeros than you probably
- want to see. You can enter numbers in this notation, too.
- X
- @group
- @smallexample
- 1: 2. 2: 2. 1: 1.99506311678e3010
- X . 1: 10000. .
- X .
- X
- X 2.0 RET 1e4 RET ^
- @end smallexample
- @end group
- X
- @cindex Round-off errors
- @noindent
- Hey, the answer is different! Look closely at the middle columns
- of the two examples. In the first, the stack contained the
- exact integer @cite{10000}, but in the second it contained
- a floating-point value with a decimal point. When you raise a
- number to an integer power, Calc uses repeated squaring and
- multiplication to get the answer. When you use a floating-point
- power, Calc uses logarithms and exponentials. As you can see,
- a slight error crept in during one of these methods. Which
- one should we trust? Let's raise the precision a bit and find
- out:
- X
- @group
- @smallexample
- X . 1: 2. 2: 2. 1: 1.995063116880828e3010
- X . 1: 10000. .
- X .
- X
- X p 16 RET 2. RET 1e4 ^ p 12 RET
- @end smallexample
- @end group
- X
- @noindent
- Presumably, it doesn't matter whether we do this higher-precision
- calculation using an integer or floating-point power, since we
- have added enough ``guard digits'' to trust the first 12 digits
- no matter what. And the verdict is@dots{} Integer powers were more
- accurate; in fact, the result was only off by one unit in the
- last place.
- X
- @cindex Guard digits
- Calc does many of its internal calculations to a slightly higher
- precision, but it doesn't always bump the precision up enough.
- In each case, Calc added about two digits of precision during
- its calculation and then rounded back down to 12 digits
- afterward. In one case, it was enough; in the the other, it
- wasn't. If you really need @var{x} digits of precision, it
- never hurts to do the calculation with a few extra guard digits.
- X
- What if we want guard digits but don't want to look at them?
- We can set the @dfn{float format}. Calc supports four major
- formats for floating-point numbers, called @dfn{normal},
- @dfn{fixed-point}, @dfn{scientific notation}, and @dfn{engineering
- notation}. You get them by pressing @w{@kbd{d n}}, @kbd{d f},
- @kbd{d s}, and @kbd{d e}, respectively. In each case, you can
- supply a numeric prefix argument which says how many digits
- should be displayed. As an example, let's put a few numbers
- onto the stack and try some different display modes. First,
- use @kbd{M-0 DEL} to clear the stack, then enter the four
- numbers shown here:
- X
- @group
- @smallexample
- 4: 12345 4: 12345 4: 12345 4: 12345 4: 12345
- 3: 12345. 3: 12300. 3: 1.2345e4 3: 1.23e4 3: 12345.000
- 2: 123.45 2: 123. 2: 1.2345e2 2: 1.23e2 2: 123.450
- 1: 12.345 1: 12.3 1: 1.2345e1 1: 1.23e1 1: 12.345
- X . . . . .
- X
- X d n M-3 d n d s M-3 d s M-3 d f
- @end smallexample
- @end group
- X
- @noindent
- Notice that when we typed @kbd{M-3 d n}, the numbers were rounded down
- to three significant digits, but then when we typed @kbd{d s} all
- five significant figures reappeared. The float format does not
- affect how numbers are stored, it only affects how they are
- displayed. Only the current precision governs the actual rounding
- of numbers in the Calculator's memory.
- X
- Engineering notation, not shown here, is like scientific notation
- except the exponent (the power-of-ten part) is always adjusted to be
- a multiple of three (as in ``kilo,'' ``micro,'' etc.). As a result
- there will be one, two, or three digits before the decimal point.
- X
- Whenever you change a display-related mode, Calc redraws everything
- in the stack. This may be slow if there are many things on the stack,
- so Calc allows you to type shift-@kbd{H} before any mode command to
- prevent it from updating the stack. Anything Calc displays after the
- mode-changing command will appear in the new format.
- X
- @group
- @smallexample
- 4: 12345 4: 12345 4: 12345 4: 12345 4: 12345
- 3: 12345.000 3: 12345.000 3: 12345.000 3: 1.2345e4 3: 12345.
- 2: 123.450 2: 123.450 2: 1.2345e1 2: 1.2345e1 2: 123.45
- 1: 12.345 1: 1.2345e1 1: 1.2345e2 1: 1.2345e2 1: 12.345
- X . . . . .
- X
- X H d s DEL U TAB d SPC d n
- @end smallexample
- @end group
- X
- @noindent
- Here the @kbd{H d s} command changes to scientific notation but without
- updating the screen. Deleting the top stack entry and undoing it back
- causes it to show up in the new format; swapping the top two stack
- entries reformats both entries. The @kbd{d SPC} command refreshes the
- whole stack. The @kbd{d n} command changes back to the normal float
- format; since it doesn't have an @kbd{H} prefix, it also updates all
- the stack entries to be in @kbd{d n} format.
- X
- Notice that the integer @cite{12345} was not affected by any
- of the float formats. Integers are integers, and are always
- displayed exactly.
- X
- @cindex Large numbers, readability
- Large integers have their own problems. Let's look back at
- the result of @kbd{2^3^4}.
- X
- @example
- 2417851639229258349412352
- @end example
- X
- @noindent
- Quick---how many digits does this have? Try typing @kbd{d g}:
- X
- @example
- 2,417,851,639,229,258,349,412,352
- @end example
- X
- @noindent
- Now how many digits does this have? It's much easier to tell!
- We can actually group digits into clumps of any size. Some
- people prefer @kbd{M-5 d g}:
- X
- @example
- 24178,51639,22925,83494,12352
- @end example
- X
- Let's see what happens to floating-point numbers when they are grouped.
- First, type @kbd{p 25 @key{RET}} to make sure we have enough precision
- to get ourselves into trouble. Now, type @kbd{1e13 /}:
- X
- @example
- 24,17851,63922.9258349412352
- @end example
- X
- @noindent
- The integer part is grouped but the fractional part isn't. Now try
- @kbd{M-- M-5 d g} (that's meta-minus-sign, meta-five):
- X
- @example
- 24,17851,63922.92583,49412,352
- @end example
- X
- If you find it hard to tell the decimal point from the commas, try
- changing the grouping character to a space with @kbd{d , @key{SPC}}:
- X
- @example
- 24 17851 63922.92583 49412 352
- @end example
- X
- Type @kbd{d , ,} to restore the normal grouping character, then
- @kbd{d g} again to turn grouping off. Also, press @kbd{p 12} to
- restore the default precision.
- X
- Press @kbd{U} enough times to get the original big integer back.
- (Notice that @kbd{U} does not undo each mode-setting command; if
- you want to undo a mode-setting command, you have to do it yourself.)
- Now, type @kbd{d r 16 @key{RET}}:
- X
- @example
- 16#200000000000000000000
- @end example
- X
- @noindent
- The number is now displayed in @dfn{hexadecimal}, or ``base-16'' form.
- Suddenly it looks pretty simple; this should be no surprise, since we
- got this number by computing a power of two, and 16 is a power of 2.
- In fact, we can use @w{@kbd{d r 2 @key{RET}}} to see it in actual binary
- form:
- X
- @example
- 2#1000000000000000000000000000000000000000000000000000000 @dots{}
- @end example
- X
- @noindent
- We don't have enough space here to show all the zeros! They won't
- fit on a typical screen, either, so you will have to use horizontal
- scrolling to see them all. Press @kbd{<} and @kbd{>} to scroll the
- stack window left and right by half its width. Another way to view
- something large is to press @kbd{`} (back-quote) to edit the top of
- stack in a separate window. (Press @kbd{M-# M-#} when you are done.)
- X
- You can enter non-decimal numbers using the @kbd{#} symbol, too.
- Let's see what the hexadecimal number @samp{5FE} looks like in
- binary. Type @kbd{16#5FE} (the letters can be typed in upper or
- lower case; they will always appear in upper case). It will also
- help to turn grouping on with @kbd{d g}:
- X
- @example
- 2#101,1111,1110
- @end example
- X
- Notice that @kbd{d g} groups by fours by default if the display radix
- is binary or hexadecimal, but by threes if it is decimal, octal, or any
- other radix.
- X
- Now let's see that number in decimal; type @kbd{d r 10}:
- X
- @example
- 1,534
- @end example
- X
- Numbers are not @emph{stored} with any particular radix attached. They're
- just numbers; they can be entered in any radix, and are always displayed
- in whatever radix you've chosen with @kbd{d r}. The current radix applies
- to integers, fractions, and floats.
- X
- (@bullet{}) @strong{Exercise 1.} Your friend Joe tried to enter one-third
- as @samp{3#0.1} in @kbd{d r 3} mode with a precision of 12. He got
- @samp{3#0.0222222...} (with 25 2's) in the display. When he multiplied
- that by three, he got @samp{3#0.222222...} instead of the expected
- @samp{3#1}. Next, Joe entered @samp{3#0.2} and, to his great relief,
- saw @samp{3#0.2} on the screen. But when he typed @kbd{2 /}, he got
- @samp{3#0.10000001} (some zeros omitted). What's going on here?
- @xref{Modes Answer 1, 1}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 2.} Scientific notation works in non-decimal
- modes in the natural way (the exponent is a power of the radix instead of
- a power of ten, although the exponent itself is always written in decimal).
- Thus @samp{8#1.23e3 = 8#1230.0}. Suppose we have the hexadecimal number
- @samp{f.e8f} times 16 to the 15th power: We write @samp{16#f.e8fe15}.
- What is wrong with this picture? What could we write instead that would
- work better? @xref{Modes Answer 2, 2}. (@bullet{})
- X
- The @kbd{m} prefix key has another set of modes, relating to the way
- Calc interprets your inputs and does computations. Whereas @kbd{d}-prefix
- modes generally affect the way thing look, @kbd{m}-prefix modes affect
- the way they are actually computed.
- X
- The most popular @kbd{m}-prefix mode is the @dfn{angular mode}. Notice
- the @samp{Deg} indicator in the mode line. This means that if you use
- a command that interprets a number as an angle, it will assume the
- angle is measured in degrees. For example,
- X
- @group
- @smallexample
- 1: 45 1: 0.707106781187 1: 0.500000000001 1: 0.5
- X . . . .
- X
- X 45 S 2 ^ c 1
- @end smallexample
- @end group
- X
- @noindent
- The shift-@kbd{S} command computes the sine of an angle. The sine
- of 45 degrees is @c{$\sqrt{2}/2$}
- @cite{sqrt(2)/2}; squaring this yields @cite{2/4 = 0.5}.
- However, there has been a slight roundoff error because the
- representation of @c{$\sqrt{2}/2$}
- @cite{sqrt(2)/2} wasn't exact. The @kbd{c 1}
- command is a handy way to clean up numbers in this case; it
- temporarily reduces the precision by one digit while it
- re-rounds the number on the top of the stack.
- X
- (@bullet{}) @strong{Exercise 3.} Your friend Joe computed the sine
- of 45 degrees as shown above, then, hoping to avoid an inexact
- result, he increased the precision to 16 digits before squaring.
- What happened? @xref{Modes Answer 3, 3}. (@bullet{})
- X
- To do this calculation in radians, we would type @kbd{m r} first.
- (The indicator changes to @samp{Rad}.) 45 degrees corresponds to
- @c{$\pi\over4$}
- @cite{pi/4} radians. To get @c{$\pi$}
- @cite{pi}, press the @kbd{P} key. (Once
- again, this is a shifted capital @kbd{P}. Remember, unshifted
- @kbd{p} sets the precision.)
- X
- @group
- @smallexample
- 1: 3.14159265359 1: 0.785398163398 1: 0.707106781187
- X . . .
- X
- X P 4 / m r S
- @end smallexample
- @end group
- X
- Likewise, inverse trigonometric functions generate results in
- either radians or degrees, depending on the current angular mode.
- X
- @group
- @smallexample
- 1: 0.707106781187 1: 0.785398163398 1: 45.
- X . . .
- X
- X .5 Q m r I S m d U I S
- @end smallexample
- @end group
- X
- @noindent
- Here we compute the Inverse Sine of @c{$\sqrt{0.5}$}
- @cite{sqrt(0.5)}, first in
- radians, then in degrees.
- X
- Use @kbd{c d} and @kbd{c r} to convert a number from radians to degrees
- and vice-versa.
- X
- @group
- @smallexample
- 1: 45 1: 0.785398163397 1: 45.
- X . . .
- X
- X 45 c r c d
- @end smallexample
- @end group
- X
- Another interesting mode is @dfn{fraction mode}. Normally,
- dividing two integers produces a floating-point result if the
- quotient can't be expressed as an exact integer. Fraction mode
- causes integer division to produce a fraction, i.e., a rational
- number, instead.
- X
- @group
- @smallexample
- 2: 12 1: 1.33333333333 1: 4:3
- 1: 9 . .
- X .
- X
- X 12 RET 9 / m f U / m f
- @end smallexample
- @end group
- X
- @noindent
- In the first case, we get an approximate floating-point result.
- In the second case, we get an exact fractional result (four-thirds).
- X
- You can enter a fraction at any time using @kbd{:} notation.
- (Calc uses @kbd{:} instead of @kbd{/} as the fraction separator
- because @kbd{/} is already used to divide the top two stack
- elements.) Calculations involving fractions will always
- produce exact fractional results; fraction mode only says
- what to do when dividing two integers.
- X
- @cindex Fractions vs. floats
- @cindex Floats vs. fractions
- (@bullet{}) @strong{Exercise 4.} If fractional arithmetic is exact,
- why would you ever use floating-point numbers instead?
- @xref{Modes Answer 4, 4}. (@bullet{})
- X
- Typing @kbd{m f} doesn't change any existing values in the stack.
- In the above example, we had to Undo the division and do it over
- again when we changed to fraction mode. But if you use the
- evaluates-to operator you can get commands like @kbd{m f} to
- recompute for you.
- X
- @group
- @smallexample
- 1: 12 / 9 => 1.33333333333 1: 12 / 9 => 1.333 1: 12 / 9 => 4:3
- X . . .
- X
- X ' 12/9 => RET p 4 RET m f
- @end smallexample
- @end group
- X
- @noindent
- In this example, the righthand side of the @samp{=>} operator
- on the stack is recomputed when we change the precision, then
- again when we change to fraction mode. All @samp{=>} expressions
- on the stack are recomputed every time you change any mode that
- might affect their values.
- X
- @node Arithmetic Tutorial, Vector/Matrix Tutorial, Basic Tutorial, Tutorial
- @section Arithmetic Tutorial
- X
- @noindent
- In this section, we explore the arithmetic and scientific functions
- available in the Calculator.
- X
- The standard arithmetic commands are @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/},
- and @kbd{^}. Each normally takes two numbers from the top of the stack
- and pushes back a result. The @kbd{n} and @kbd{&} keys perform
- change-sign and reciprocal operations, respectively.
- X
- @group
- @smallexample
- 1: 5 1: 0.2 1: 5. 1: -5. 1: 5.
- X . . . . .
- X
- X 5 & & n n
- @end smallexample
- @end group
- X
- @cindex Binary operators
- You can apply a ``binary operator'' like @kbd{+} across any number of
- stack entries by giving it a numeric prefix. You can also apply it
- pairwise to several stack elements along with the top one if you use
- a negative prefix.
- X
- @group
- @smallexample
- 3: 2 1: 9 3: 2 4: 2 3: 12
- 2: 3 . 2: 3 3: 3 2: 13
- 1: 4 1: 4 2: 4 1: 14
- X . . 1: 10 .
- X .
- X
- 2 RET 3 RET 4 M-3 + U 10 M-- M-3 +
- @end smallexample
- @end group
- X
- @cindex Unary operators
- You can apply a ``unary operator'' like @kbd{&} to the top @var{n}
- stack entries with a numeric prefix, too.
- X
- @group
- @smallexample
- 3: 2 3: 0.5 3: 0.5
- 2: 3 2: 0.333333333333 2: 3.
- 1: 4 1: 0.25 1: 4.
- X . . .
- X
- 2 RET 3 RET 4 M-3 & M-2 &
- @end smallexample
- @end group
- X
- Notice that the results here are left in floating-point form.
- We can convert them back to integers by pressing @kbd{F}, the
- ``floor'' function. This function rounds down to the next lower
- integer. There is also @kbd{R}, which rounds to the nearest
- integer.
- X
- @group
- @smallexample
- 7: 2. 7: 2 7: 2
- 6: 2.4 6: 2 6: 2
- 5: 2.5 5: 2 5: 3
- 4: 2.6 4: 2 4: 3
- 3: -2. 3: -2 3: -2
- 2: -2.4 2: -3 2: -2
- 1: -2.6 1: -3 1: -3
- X . . .
- X
- X M-7 F U M-7 R
- @end smallexample
- @end group
- X
- Since dividing-and-flooring (i.e., ``integer quotient'') is such a
- common operation, Calc provides a special command for that purpose, the
- backslash @kbd{\}. Another common arithmetic operator is @kbd{%}, which
- computes the remainder that would arise from a @kbd{\} operation, i.e.,
- the ``modulo'' of two numbers. For example,
- X
- @group
- @smallexample
- 2: 1234 1: 12 2: 1234 1: 34
- 1: 100 . 1: 100 .
- X . .
- X
- 1234 RET 100 \ U %
- @end smallexample
- @end group
- X
- These commands actually work for any real numbers, not just integers.
- X
- @group
- @smallexample
- 2: 3.1415 1: 3 2: 3.1415 1: 0.1415
- 1: 1 . 1: 1 .
- X . .
- X
- 3.1415 RET 1 \ U %
- @end smallexample
- @end group
- X
- (@bullet{}) @strong{Exercise 1.} The @kbd{\} command would appear to be a
- frill, since you could always do the same thing with @kbd{/ F}. Think
- of a situation where this is not true---@kbd{/ F} would be inadequate.
- Now think of a way you could get around the problem if Calc didn't
- provide a @kbd{\} command. @xref{Arithmetic Answer 1, 1}. (@bullet{})
- X
- We've already seen the @kbd{Q} (square root) and @kbd{S} (sine)
- commands. Other commands along those lines are @kbd{C} (cosine),
- @kbd{T} (tangent), @kbd{E} (@cite{e^x}) and @kbd{L} (natural
- logarithm). These can be modified by the @kbd{I} (inverse) and
- @kbd{H} (hyperbolic) prefix keys.
- X
- Let's compute the sine and cosine of an angle, and verify the
- identity @c{$\sin^2x + \cos^2x = 1$}
- @cite{sin(x)^2 + cos(x)^2 = 1}. We'll
- arbitrarily pick @i{-64} degrees as a good value for @cite{x}. With
- the angular mode set to degrees (type @w{@kbd{m d}}), do:
- X
- @group
- @smallexample
- 2: -64 2: -64 2: -0.89879 2: -0.89879 1: 1.
- 1: -64 1: -0.89879 1: -64 1: 0.43837 .
- X . . . .
- X
- X 64 n RET RET S TAB C f h
- @end smallexample
- @end group
- X
- @noindent
- (For brevity, we're showing only five digits of the results here.
- You can of course do these calculations to any precision you like.)
- X
- Remember, @kbd{f h} is the @code{calc-hypot}, or square-root of sum
- of squares, command.
- X
- Another identity is @c{$\displaystyle\tan x = {\sin x \over \cos x}$}
- @cite{tan(x) = sin(x) / cos(x)}.
- @group
- @smallexample
- X
- 2: -0.89879 1: -2.0503 1: -64.
- 1: 0.43837 . .
- X .
- X
- X U / I T
- @end smallexample
- @end group
- X
- A physical interpretation of this calculation is that if you move
- @cite{0.89879} units downward and @cite{0.43837} units to the right,
- your direction of motion is @i{-64} degrees from horizontal. Suppose
- we move in the opposite direction, up and to the left:
- X
- @group
- @smallexample
- 2: -0.89879 2: 0.89879 1: -2.0503 1: -64.
- 1: 0.43837 1: -0.43837 . .
- X . .
- X
- X U U M-2 n / I T
- @end smallexample
- @end group
- X
- @noindent
- How can the angle be the same? The answer is that the @kbd{/} operation
- loses information about the signs of its inputs. Because the quotient
- is negative, we know exactly one of the inputs was negative, but we
- can't tell which one. There is an @kbd{f T} [@code{arctan2}] function which
- computes the inverse tangent of the quotient of a pair of numbers.
- Since you feed it the two original numbers, it has enough information
- to give you a full 360-degree answer.
- X
- @group
- @smallexample
- 2: 0.89879 1: 116. 3: 116. 2: 116. 1: 180.
- 1: -0.43837 . 2: -0.89879 1: -64. .
- X . 1: 0.43837 .
- X .
- X
- X U U f T M-RET M-2 n f T -
- @end smallexample
- @end group
- X
- @noindent
- The resulting angles differ by 180 degrees; in other words, they
- point in opposite directions, just as we would expect.
- X
- The @key{META}-@key{RET} we used in the third step is the
- ``last-arguments'' command. It is sort of like Undo, except that it
- restores the arguments of the last command to the stack without removing
- the command's result. It is useful in situations like this one,
- where we need to do several operations on the same inputs. We could
- have accomplished the same thing by using @kbd{M-2 @key{RET}} to duplicate
- the top two stack elements right after the @kbd{U U}, then a pair of
- @kbd{M-@key{TAB}} commands to cycle the 116 up around the duplicates.
- X
- A similar identity is supposed to hold for hyperbolic sines and cosines,
- except that it is the @emph{difference}
- @c{$\cosh^2x - \sinh^2x$}
- @cite{cosh(x)^2 - sinh(x)^2} that always equals one.
- Let's try to verify this identity.@refill
- X
- @group
- @smallexample
- 2: -64 2: -64 2: -64 2: 9.7192e54 2: 9.7192e54
- 1: -64 1: -3.1175e27 1: 9.7192e54 1: -64 1: 9.7192e54
- X . . . . .
- X
- X 64 n RET RET H C 2 ^ TAB H S 2 ^
- @end smallexample
- @end group
- X
- @noindent
- Something's obviously wrong, because when we subtract these numbers
- the answer will clearly be zero! But if you think about it, if these
- numbers @emph{did} differ by one, it would be in the 55th decimal
- place. The difference we seek has been lost entirely to roundoff
- error.
- X
- We could verify this hypothesis by doing the actual calculation with,
- say, 60 decimal places of precision. This will be slow, but not
- enormously so. Try it if you wish; sure enough, the answer is
- 0.99999, reasonably close to 1.
- X
- Of course, a more reasonable way to verify the identity is to use
- a more reasonable value for @cite{x}!
- X
- Some Calculator commands use the Hyperbolic prefix for other purposes.
- The logarithm and exponential functions, for example, work to the base
- @cite{e} normally but use base-10 instead if you use the Hyperbolic
- prefix.
- X
- @group
- @smallexample
- 1: 1000 1: 6.9077 1: 1000 1: 3
- X . . . .
- X
- X 1000 L U H L
- @end smallexample
- @end group
- X
- @noindent
- First, we mistakenly compute a natural logarithm. Then we undo
- and compute a common logarithm instead.
- X
- The @kbd{B} key computes a general base-@var{b} logarithm for any
- value of @var{b}.
- X
- @group
- @smallexample
- 2: 1000 1: 3 1: 1000. 2: 1000. 1: 6.9077
- 1: 10 . . 1: 2.71828 .
- X . .
- X
- X 1000 RET 10 B H E H P B
- @end smallexample
- @end group
- X
- @noindent
- Here we first use @kbd{B} to compute the base-10 logarithm, then use
- the ``hyperbolic'' exponential as a cheap hack to recover the number
- 1000, then use @kbd{B} again to compute the natural logarithm. Note
- that @kbd{P} with the hyperbolic prefix pushes the constant @cite{e}
- onto the stack.
- X
- You may have noticed that both times we took the base-10 logarithm
- of 1000, we got an exact integer result. Calc always tries to give
- an exact rational result for calculations involving rational numbers
- where possible. But when we used @kbd{H E}, the result was a
- floating-point number for no apparent reason. In fact, if we had
- computed @kbd{10 @key{RET} 3 ^} we @emph{would} have gotten an
- exact integer 1000. But the @kbd{H E} command is rigged to generate
- a floating-point result all of the time so that @kbd{1000 H E} will
- not waste time computing a thousand-digit integer when all you
- probably wanted was @samp{1e1000}.
- X
- (@bullet{}) @strong{Exercise 2.} Find a pair of integer inputs to
- the @kbd{B} command for which Calc could find an exact rational
- result but doesn't. @xref{Arithmetic Answer 2, 2}. (@bullet{})
- X
- The Calculator also has a set of functions relating to combinatorics
- and statistics. You may be familiar with the @dfn{factorial} function,
- which computes the product of all the integers up to a given number.
- X
- @group
- @smallexample
- 1: 100 1: 93326215443... 1: 100. 1: 9.3326e157
- X . . . .
- X
- X 100 ! U c f !
- @end smallexample
- @end group
- X
- @noindent
- The @kbd{c f} command converts the integer or fraction at the top
- of the stack to floating-point format. If you take the factorial
- of a floating-point number, you get a floating-point result
- accurate to the current precision. But if you give @kbd{!} an
- exact integer, you get an exact integer result (158 digits long
- in this case).
- X
- If you take the factorial of a non-integer, Calc uses a generalized
- factorial function defined in terms of Euler's Gamma function
- @c{$\Gamma(n)$}
- @cite{gamma(n)}
- (which is itself available as the @kbd{f g} command).
- X
- @group
- @smallexample
- 3: 4. 3: 24. 1: 5.5 1: 52.342777847
- 2: 4.5 2: 52.3427777847 . .
- 1: 5. 1: 120.
- X . .
- X
- X M-3 ! M-0 DEL 5.5 f g
- @end smallexample
- @end group
- X
- @noindent
- Here we verify the identity @c{$n! = \Gamma(n+1)$}
- @cite{@var{n}!@: = gamma(@var{n}+1)}.
- X
- The binomial coefficient @var{n}-choose-@var{m}@c{ or $\displaystyle {n \choose m}$}
- @asis{} is defined by
- @c{$\displaystyle {n! \over m! \, (n-m)!}$}
- @cite{n!@: / m!@: (n-m)!} for all reals @cite{n} and
- @cite{m}. The intermediate results in this formula can become quite
- large even if the final result is small; the @kbd{k c} command computes
- a binomial coefficient in a way that avoids large intermediate
- values.
- X
- The @kbd{k} prefix key defines several common functions out of
- combinatorics and number theory. Here we compute the binomial
- coefficient 30-choose-20, then determine its prime factorization.
- X
- @group
- @smallexample
- 2: 30 1: 30045015 1: [3, 3, 5, 7, 11, 13, 23, 29]
- 1: 20 . .
- X .
- X
- X 30 RET 20 k c k f
- @end smallexample
- @end group
- X
- @noindent
- You can verify these prime factors by using @kbd{v u} to ``unpack''
- this vector into 8 separate stack entries, then @kbd{M-8 *} to
- multiply them back together. The result is the original number,
- 30045015.
- X
- @cindex Hash tables
- Suppose a program you are writing needs a hash table with at least
- 10000 entries. It's best to use a prime number as the actual size
- of a hash table. Calc can compute the next prime number after 10000:
- X
- @group
- @smallexample
- 1: 10000 1: 10007 1: 9973
- X . . .
- X
- X 10000 k n I k n
- @end smallexample
- @end group
- X
- @noindent
- Just for kicks we've also computed the next prime @emph{less} than
- 10000.
- X
- @c [fix-ref Financial Functions]
- @xref{Financial Functions}, for a description of the Calculator
- commands that deal with business and financial calculations (functions
- like @code{pv}, @code{rate}, and @code{sln}).
- X
- @c [fix-ref Binary Number Functions]
- @xref{Binary Functions}, to read about the commands for operating
- on binary numbers (like @code{and}, @code{xor}, and @code{lsh}).
- X
- @node Vector/Matrix Tutorial, Types Tutorial, Arithmetic Tutorial, Tutorial
- @section Vector/Matrix Tutorial
- X
- @noindent
- A @dfn{vector} is a list of numbers or other Calc data objects.
- Calc provides a large set of commands that operate on vectors. Some
- are familiar operations from vector analysis. Others simply treat
- a vector as a list of objects.
- X
- @menu
- * Vector Analysis Tutorial::
- * Matrix Tutorial::
- * List Tutorial::
- @end menu
- X
- @node Vector Analysis Tutorial, Matrix Tutorial, Vector/Matrix Tutorial, Vector/Matrix Tutorial
- @subsection Vector Analysis
- X
- @noindent
- If you add two vectors, the result is a vector of the sums of the
- elements, taken pairwise.
- X
- @group
- @smallexample
- 1: [1, 2, 3] 2: [1, 2, 3] 1: [8, 8, 3]
- X . 1: [7, 6, 0] .
- X .
- X
- X [1,2,3] s 1 [7 6 0] s 2 +
- @end smallexample
- @end group
- X
- @noindent
- Note that we can separate the vector elements with either commas or
- spaces. This is true whether we are using incomplete vectors or
- algebraic entry. The @kbd{s 1} and @kbd{s 2} commands save these
- vectors so we can easily reuse them later.
- X
- If you multiply two vectors, the result is the sum of the products
- of the elements taken pairwise. This is called the @dfn{dot product}
- of the vectors.
- X
- @group
- @smallexample
- 2: [1, 2, 3] 1: 19
- 1: [7, 6, 0] .
- X .
- X
- X r 1 r 2 *
- @end smallexample
- @end group
- X
- @cindex Dot product
- The dot product of two vectors is equal to the product of their
- lengths times the cosine of the angle between them. (Here the vector
- is interpreted as a line from the origin @cite{(0,0,0)} to the
- specified point in three-dimensional space.) The @kbd{A}
- (absolute value) command can be used to compute the length of a
- vector.
- X
- @group
- @smallexample
- 3: 19 3: 19 1: 0.550782 1: 56.579
- 2: [1, 2, 3] 2: 3.741657 . .
- 1: [7, 6, 0] 1: 9.219544
- X . .
- X
- X M-RET M-2 A * / I C
- @end smallexample
- @end group
- X
- @noindent
- First we recall the arguments to the dot product command, then
- we compute the absolute values of the top two stack entries to
- obtain the lengths of the vectors, then we divide the dot product
- by the product of the lengths to get the cosine of the angle.
- The inverse cosine finds that the angle between the vectors
- is about 56 degrees.
- X
- @cindex Cross product
- @cindex Perpendicular vectors
- The @dfn{cross product} of two vectors is a vector whose length
- is the product of the lengths of the inputs times the sine of the
- angle between them, and whose direction is perpendicular to both
- input vectors. Unlike the dot product, the cross product is
- defined only for three-dimensional vectors. Let's double-check
- our computation of the angle using the cross product.
- X
- @group
- @smallexample
- 2: [1, 2, 3] 3: [-18, 21, -8] 1: [-0.52, 0.61, -0.23] 1: 56.579
- 1: [7, 6, 0] 2: [1, 2, 3] . .
- X . 1: [7, 6, 0]
- X .
- X
- X r 1 r 2 V C s 3 M-RET M-2 A * / A I S
- @end smallexample
- @end group
- X
- @noindent
- First we recall the original vectors and compute their cross product,
- which we also store for later reference. Now we divide the vector
- by the product of the lengths of the original vectors. The length of
- this vector should be the sine of the angle; sure enough, it is!
- X
- @c [fix-ref General Mode Commands]
- Vector-related commands generally begin with the @kbd{v} prefix key.
- Some are uppercase letters and some are lowercase. To make it easier
- to type these commands, the shift-@kbd{V} prefix key acts the same as
- the @kbd{v} key. (@xref{General Mode Commands}, for a way to make all
- prefix keys have this property.)
- X
- If we take the dot product of two perpendicular vectors we expect
- to get zero, since the cosine of 90 degrees is zero. Let's check
- that the cross product is indeed perpendicular to both inputs:
- X
- @group
- @smallexample
- 2: [1, 2, 3] 1: 0 2: [7, 6, 0] 1: 0
- 1: [-18, 21, -8] . 1: [-18, 21, -8] .
- X . .
- X
- X r 1 r 3 * DEL r 2 r 3 *
- @end smallexample
- @end group
- X
- @cindex Normalizing a vector
- @cindex Unit vectors
- (@bullet{}) @strong{Exercise 1.} Given a vector on the top of the
- stack, what keystrokes would you use to @dfn{normalize} the
- vector, i.e., to reduce its length to one without changing its
- direction? @xref{Vector Answer 1, 1}. (@bullet{})
- X
- (@bullet{}) @strong{Exercise 2.} Suppose a certain particle can be
- at any of several positions along a ruler. You have a list of
- those positions in the form of a vector, and another list of the
- probabilities for the particle to be at the corresponding positions.
- Find the average position of the particle.
- @xref{Vector Answer 2, 2}. (@bullet{})
- X
- @node Matrix Tutorial, List Tutorial, Vector Analysis Tutorial, Vector/Matrix Tutorial
- @subsection Matrices
- X
- @noindent
- A @dfn{matrix} is just a vector of vectors, all the same length.
- This means you can enter a matrix using nested brackets. You can
- also use the semicolon character to enter a matrix. We'll show
- both methods here:
- X
- @group
- @smallexample
- 1: [ [ 1, 2, 3 ] 1: [ [ 1, 2, 3 ]
- X [ 4, 5, 6 ] ] [ 4, 5, 6 ] ]
- X . .
- X
- X [[1 2 3] [4 5 6]] ' [1 2 3; 4 5 6] RET
- @end smallexample
- @end group
- X
- @noindent
- We'll be using this matrix again, so type @kbd{s 4} to save it now.
- X
- Note that semicolons work with incomplete vectors, but they work
- better in algebraic entry. That's why we use the apostrophe in
- the second example.
- X
- When two matrices are multiplied, the lefthand matrix must have
- the same number of columns as the righthand matrix has rows.
- Row @cite{i}, column @cite{j} of the result is effectively the
- dot product of row @cite{i} of the left matrix by column @cite{j}
- of the right matrix.
- X
- If we try to duplicate this matrix and multiply it by itself,
- the dimensions are wrong and the multiplication cannot take place:
- X
- @group
- @smallexample
- 1: [ [ 1, 2, 3 ] * [ [ 1, 2, 3 ]
- X [ 4, 5, 6 ] ] [ 4, 5, 6 ] ]
- X .
- X
- X RET *
- @end smallexample
- @end group
- X
- @noindent
- Though rather hard to read, this is a formula which shows the product
- of two matrices. The @samp{*} function, having invalid arguments, has
- been left in symbolic form.
- X
- We can multiply the matrices if we @dfn{transpose} one of them first.
- X
- @group
- @smallexample
- 2: [ [ 1, 2, 3 ] 1: [ [ 14, 32 ] 1: [ [ 17, 22, 27 ]
- X [ 4, 5, 6 ] ] [ 32, 77 ] ] [ 22, 29, 36 ]
- 1: [ [ 1, 4 ] . [ 27, 36, 45 ] ]
- X [ 2, 5 ] .
- X [ 3, 6 ] ]
- X .
- X
- X U v t * U TAB *
- @end smallexample
- @end group
- X
- Matrix multiplication is not commutative; indeed, switching the
- order of the operands can even change the dimensions of the result
- matrix, as happened here!
- X
- If you multiply a plain vector by a matrix, it is treated as a
- single row or column depending on which side of the matrix it is
- on. The result is a plain vector which should also be interpreted
- as a row or column as appropriate.
- X
- @group
- @smallexample
- 2: [ [ 1, 2, 3 ] 1: [14, 32]
- X [ 4, 5, 6 ] ] .
- 1: [1, 2, 3]
- X .
- X
- X r 4 r 1 *
- @end smallexample
- @end group
- X
- Multiplying in the other order wouldn't work because the number of
- rows in the matrix is different from the number of elements in the
- SHAR_EOF
- true || echo 'restore of calc.texinfo failed'
- fi
- echo 'End of part 32'
- echo 'File calc.texinfo is continued in part 33'
- echo 33 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
- --
- Kent Landfield INTERNET: kent@sparky.IMD.Sterling.COM
- Sterling Software, IMD UUCP: uunet!sparky!kent
- Phone: (402) 291-8300 FAX: (402) 291-4362
- Please send comp.sources.misc-related mail to kent@uunet.uu.net.
-