home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
calc.info-6
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
50KB
|
922 lines
This is Info file calc.info, produced by Makeinfo-1.55 from the input
file calc.texinfo.
This file documents Calc, the GNU Emacs calculator.
Copyright (C) 1990, 1991 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the section entitled "GNU General Public License" is included
exactly as in the original, and provided that the entire resulting
derived work is distributed under the terms of a permission notice
identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the section entitled "GNU General Public License"
may be included in a translation approved by the author instead of in
the original English.
File: calc.info, Node: Programming Tutorial, Next: Answers to Exercises, Prev: Algebra Tutorial, Up: Tutorial
Programming Tutorial
====================
The Calculator is written entirely in Emacs Lisp, a highly extensible
language. If you know Lisp, you can program the Calculator to do
anything you like. Rewrite rules also work as a powerful programming
system. But Lisp and rewrite rules take a while to master, and often
all you want to do is define a new function or repeat a command a few
times. Calc has features that allow you to do these things easily.
(Note that the programming commands relating to user-defined keys
are not yet supported under Lucid Emacs 19.)
One very limited form of programming is defining your own functions.
Calc's `Z F' command allows you to define a function name and key
sequence to correspond to any formula. Programming commands use the
shift-`Z' prefix; the user commands they create use the lower case `z'
prefix.
1: 1 + x + x^2 / 2 + x^3 / 6 1: 1 + x + x^2 / 2 + x^3 / 6
. .
' 1 + x + x^2/2! + x^3/3! RET Z F e myexp RET RET RET y
This polynomial is a Taylor series approximation to `exp(x)'. The
`Z F' command asks a number of questions. The above answers say that
the key sequence for our function should be `z e'; the `M-x' equivalent
should be `calc-myexp'; the name of the function in algebraic formulas
should also be `myexp'; the default argument list `(x)' is acceptable;
and finally `y' answers the question "leave it in symbolic form for
non-constant arguments?"
1: 1.3495 2: 1.3495 3: 1.3495
. 1: 1.34986 2: 1.34986
. 1: myexp(a + 1)
.
.3 z e .3 E ' a+1 RET z e
First we call our new `exp' approximation with 0.3 as an argument, and
compare it with the true `exp' function. Then we note that, as
requested, if we try to give `z e' an argument that isn't a plain
number, it leaves the `myexp' function call in symbolic form. If we
had answered `n' to the final question, `myexp(a + 1)' would have
evaluated by plugging in `a + 1' for `x' in the defining formula.
(*) *Exercise 1.* The "sine integral" function `Si(x)' is defined
as the integral of `sin(t)/t' for `t = 0' to `x' in radians. (It was
invented because this integral has no solution in terms of basic
functions; if you give it to Calc's `a i' command, it will ponder it
for a long time and then give up.) We can use the numerical
integration command, however, which in algebraic notation is written
like `ninteg(f(t), t, 0, x)' with any integrand `f(t)'. Define a `z s'
command and `Si' function that implement this. You will need to edit
the default argument list a bit. As a test, `Si(1)' should return
0.946083. (Hint: `ninteg' will run a lot faster if you reduce the
precision to, say, six digits beforehand.) *Note 1: Programming Answer
1. (*)
The simplest way to do real "programming" of Emacs is to define a
"keyboard macro". A keyboard macro is simply a sequence of keystrokes
which Emacs has stored away and can play back on demand. For example,
if you find yourself typing `H a S x RET' often, you may wish to
program a keyboard macro to type this for you.
1: y = sqrt(x) 1: x = y^2
. .
' y=sqrt(x) RET C-x ( H a S x RET C-x )
1: y = cos(x) 1: x = s1 arccos(y) + 2 pi n1
. .
' y=cos(x) RET X
When you type `C-x (', Emacs begins recording. But it is also still
ready to execute your keystrokes, so you're really "training" Emacs by
walking it through the procedure once. When you type `C-x )', the
macro is recorded. You can now type `X' to re-execute the same
keystrokes.
You can give a name to your macro by typing `Z K'.
1: . 1: y = x^4 1: x = s2 sqrt(s1 sqrt(y))
. .
Z K x RET ' y=x^4 RET z x
Notice that we use shift-`Z' to define the command, and lower-case `z'
to call it up.
Keyboard macros can call other macros.
1: abs(x) 1: x = s1 y 1: 2 / x 1: x = 2 / y
. . . .
' abs(x) RET C-x ( ' y RET a = z x C-x ) ' 2/x RET X
(*) *Exercise 2.* Define a keyboard macro to negate the item in
level 3 of the stack, without disturbing the rest of the stack. *Note
2: Programming Answer 2. (*)
(*) *Exercise 3.* Define keyboard macros to compute the following
functions:
1. Compute `sin(x) / x', where `x' is the number on the top of the
stack.
2. Compute the base-`b' logarithm, just like the `B' key except the
arguments are taken in the opposite order.
3. Produce a vector of integers from 1 to the integer on the top of
the stack.
*Note 3: Programming Answer 3. (*)
(*) *Exercise 4.* Define a keyboard macro to compute the average
(mean) value of a list of numbers. *Note 4: Programming Answer 4. (*)
In many programs, some of the steps must execute several times.
Calc has "looping" commands that allow this. Loops are useful inside
keyboard macros, but actually work at any time.
1: x^6 2: x^6 1: 360 x^2
. 1: 4 .
.
' x^6 RET 4 Z < a d x RET Z >
Here we have computed the fourth derivative of `x^6' by enclosing a
derivative command in a "repeat loop" structure. This structure pops a
repeat count from the stack, then executes the body of the loop that
many times.
If you make a mistake while entering the body of the loop, type
`Z C-g' to cancel the loop command.
Here's another example:
3: 1 2: 10946
2: 1 1: 17711
1: 20 .
.
1 RET RET 20 Z < TAB C-j + Z >
The numbers in levels 2 and 1 should be the 21st and 22nd Fibonacci
numbers, respectively. (To see what's going on, try a few repetitions
of the loop body by hand; `C-j', also on the Line-Feed or LFD key if
you have one, makes a copy of the number in level 2.)
A fascinating property of the Fibonacci numbers is that the `n'th
Fibonacci number can be found directly by computing `phi^n / sqrt(5)'
and then rounding to the nearest integer, where `phi', the "golden
ratio," is `(1 + sqrt(5)) / 2'. (For convenience, this constant is
available from the `phi' variable, or the `I H P' command.)
1: 1.61803 1: 24476.0000409 1: 10945.9999817 1: 10946
. . . .
I H P 21 ^ 5 Q / R
(*) *Exercise 5.* The "continued fraction" representation of `phi'
is `1 + 1/(1 + 1/(1 + 1/( ... )))'. We can compute an approximate
value by carrying this however far and then replacing the innermost
`1/( ... )' by 1. Approximate `phi' using a