home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
calc.info-4
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
40KB
|
739 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: List Tutorial, Prev: Matrix Tutorial, Up: Vector/Matrix Tutorial
Vectors as Lists
----------------
Although Calc has a number of features for manipulating vectors and
matrices as mathematical objects, you can also treat vectors as simple
lists of values. For example, we saw that the `k f' command returns a
vector which is a list of the prime factors of a number.
You can pack and unpack stack entries into vectors:
3: 10 1: [10, 20, 30] 3: 10
2: 20 . 2: 20
1: 30 1: 30
. .
M-3 v p v u
You can also build vectors out of consecutive integers, or out of
many copies of a given value:
1: [1, 2, 3, 4] 2: [1, 2, 3, 4] 2: [1, 2, 3, 4]
. 1: 17 1: [17, 17, 17, 17]
. .
v x 4 RET 17 v b 4 RET
You can apply an operator to every element of a vector using the
"map" command.
1: [17, 34, 51, 68] 1: [289, 1156, 2601, 4624] 1: [17, 34, 51, 68]
. . .
V M * 2 V M ^ V M Q
In the first step, we multiply the vector of integers by the vector of
17's elementwise. In the second step, we raise each element to the
power two. (The general rule is that both operands must be vectors of
the same length, or else one must be a vector and the other a plain
number.) In the final step, we take the square root of each element.
(*) *Exercise 1.* Compute a vector of powers of two from `2^-4' to
`2^4'. *Note 1: List Answer 1. (*)
You can also "reduce" a binary operator across a vector. For
example, reducing `*' computes the product of all the elements in the
vector:
1: 123123 1: [3, 7, 11, 13, 41] 1: 123123
. . .
123123 k f V R *
In this example, we decompose 123123 into its prime factors, then
multiply those factors together again to yield the original number.
We could compute a dot product "by hand" using mapping and reduction:
2: [1, 2, 3] 1: [7, 12, 0] 1: 19
1: [7, 6, 0] . .
.
r 1 r 2 V M * V R +
Recalling two vectors from the previous section, we compute the sum of
pairwise products of the elements to get the same answer for the dot
product as before.
A slight variant of vector reduction is the "accumulate" operation,
`V U'. This produces a vector of the intermediate results from a
corresponding reduction. Here we compute a table of factorials:
1: [1, 2, 3, 4, 5, 6] 1: [1, 2, 6, 24, 120, 720]
. .
v x 6 RET V U *
Calc allows vectors to grow as large as you like, although it gets
rather slow if vectors have more than about a hundred elements.
Actually, most of the time is spent formatting these large vectors for
display, not calculating on them. Try the following experiment (if
your computer is very fast you may need to substitute a larger vector
size).
1: [1, 2, 3, 4, ... 1: [2, 3, 4, 5, ...
. .
v x 500 RET 1 V M +
Now press `v .' (the letter `v', then a period) and try the
experiment again. In `v .' mode, long vectors are displayed
"abbreviated" like this:
1: [1, 2, 3, ..., 500] 1: [2, 3, 4, ..., 501]
. .
v x 500 RET 1 V M +
(where now the `...' is actually part of the Calc display). You will
find both operations are now much faster. But notice that even in
`v .' mode, the full vectors are still shown in the Trail. Type `t .'
to cause the trail to abbreviate as well, and try the experiment one
more time. Operations on long vectors are now quite fast! (But of
course if you use `t .' you will lose the ability to get old vectors
back using the `t y' command.)
An easy way to view a full vector when `v .' mode is active is to
press ``' (back-quote) to edit the vector; editing always works with
the full, unabbreviated value.
As a larger example, let's try to fit a straight line to some data,
using the method of least squares. (Calc has a built-in command for
least-squares curve fitting, but we'll do it by hand here just to
practice working with vectors.) Suppose we have the following list of
values in a file we have loaded into Emacs:
x y
--- ---
1.34 0.234
1.41 0.298
1.49 0.402
1.56 0.412
1.64 0.466
1.73 0.473
1.82 0.601
1.91 0.519
2.01 0.603
2.11 0.637
2.22 0.645
2.33 0.705
2.45 0.917
2.58 1.009
2.71 0.971
2.85 1.062
3.00 1.148
3.15 1.157
3.32 1.354
If you are reading this tutorial in printed form, you will find it
easiest to press `M-# i' to enter the on-line Info version of the
manual and find this table there. (Press `g', then type `List
Tutorial', to jump straight to this section.)
Position the cursor at the upper-left corner of this table, just to
the left of the `1.34'. Press `C-@' to set the mark. (On your system
this may be `C-2', `C-SPC', or `NUL'.) Now position the cursor to the
lower-right, just after the `1.354'. You have now defined this region
as an Emacs "rectangle." Still in the Info buffer, type `M-# r'. This
command (`calc-grab-rectangle') will pop you back into the Calculator,
with the contents of the rectangle you specified in the form of a
matrix.
1: [ [ 1.34, 0.234 ]
[ 1.41, 0.298 ]
...
(You may wish to use `v .' mode to abbreviate the display of this large
matrix.)
We want to treat this as a pair of lists. The first step is to
transpose this matrix into a pair of rows. Remember, a matrix is just
a vector of vectors. So we can unpack the matrix into a pair of row
vectors on the stack.
1: [ [ 1.34, 1.41, 1.49, ... ] 2: [1.34, 1.41, 1.49, ... ]
[ 0.234, 0.298, 0.402, ... ] ] 1: [0.234, 0.298, 0.402, ... ]
. .
v t v u
Let's store these in quick variables 1 and 2, respectively.
1: [1.34, 1.41, 1.49, ... ] .
.
t 2 t 1
(Recall that `t 2' is a variant of `s 2' that removes the stored value
from the stack.)
In a least squares fit, the slope `m' is given by the formula
m = (N sum(x y) - sum(x) sum(y)) / (N sum(x^2) - sum(x)^2)
where `sum(x)' represents the sum of all the values of `x'. While
there is an actual `sum' function in Calc, it's easier to sum a vector
using a simple reduction. First, let's compute the four different sums
that this formula uses.
1: 41.63 1: 98.0003