home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
calc.info-7
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
50KB
|
1,003 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 Answer 7, Next: List Answer 8, Prev: List Answer 6, Up: Answers to Exercises
List Tutorial Exercise 7
------------------------
Here's one solution. First, compute the triangular list from the
previous exercise and type `1 -' to subtract one from all the elements.
1: [ [0],
[0, 1],
[0, 1, 2],
...
1 -
The numbers down the lefthand edge of the list we desire are called
the "triangular numbers" (now you know why!). The `n'th triangular
number is the sum of the integers from 1 to `n', and can be computed
directly by the formula `n * (n+1) / 2'.
2: [ [0], [0, 1], ... ] 2: [ [0], [0, 1], ... ]
1: [0, 1, 2, 3, 4, 5] 1: [0, 1, 3, 6, 10, 15]
. .
v x 6 RET 1 - V M ' $ ($+1)/2 RET
Adding this list to the above list of lists produces the desired result:
1: [ [0],
[1, 2],
[3, 4, 5],
[6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20] ]
.
V M +
If we did not know the formula for triangular numbers, we could have
computed them using a `V U +' command. We could also have gotten them
the hard way by mapping a reduction across the original triangular list.
2: [ [0], [0, 1], ... ] 2: [ [0], [0, 1], ... ]
1: [ [0], [0, 1], ... ] 1: [0, 1, 3, 6, 10, 15]
. .
RET V M V R +
(This means "map a `V R +' command across the vector," and since each
element of the main vector is itself a small vector, `V R +' computes
the sum of its elements.)
File: calc.info, Node: List Answer 8, Next: List Answer 9, Prev: List Answer 7, Up: Answers to Exercises
List Tutorial Exercise 8
------------------------
The first step is to build a list of values of `x'.
1: [1, 2, 3, ..., 21] 1: [0, 1, 2, ..., 20] 1: [0, 0.25, 0.5, ..., 5]
. . .
v x 21 RET 1 - 4 / s 1
Next, we compute the Bessel function values.
1: [0., 0.124, 0.242, ..., -0.328]
.
V M ' besJ(1,$) RET
(Another way to do this would be `1 TAB V M f j'.)
A way to isolate the maximum value is to compute the maximum using
`V R X', then compare all the Bessel values with that maximum.
2: [0., 0.124, 0.242, ... ] 1: [0, 0, 0, ... ] 2: [0, 0, 0, ... ]
1: 0.5801562 . 1: 1
. .
RET V R X V M a = RET V R + DEL
It's a good idea to verify, as in the last step above, that only one
value is equal to the maximum. (After all, a plot of `sin(x)' might
have many points all equal to the maximum value, 1.)
The vector we have now has a single 1 in the position that indicates
the maximum value of `x'. Now it is a simple matter to convert this
back into the corresponding value itself.
2: [0, 0, 0, ... ] 1: [0, 0., 0., ... ] 1: 1.75
1: [0, 0.25, 0.5, ... ] . .
.
r 1 V M * V R +
If `a =' had produced more than one `1' value, this method would
have given the sum of all maximum `x' values; not very useful! In this
case we could have used `v m' (`calc-mask-vector') instead. This
command deletes all elements of a "data" vector that correspond to
zeros in a "mask" vector, leaving us with, in this example, a vector of
maximum `x' values.
The built-in `a X' command maximizes a function using more efficient
methods. Just for illustration, let's use `a X' to maximize
`besJ(1,x)' over this same interval.
2: besJ(1, x) 1: [1.84115, 0.581865]
1: [0 .. 5] .
.
' besJ(1,x), [0..5] RET a X x RET
The output from `a X' is a vector containing the value of `x' that
maximizes the function, and the function's value at that maximum. As
you can see, our simple search got quite close to the right answer.
File: calc.info, Node: List Answer 9, Next: List Answer 10, Prev: List Answer 8, Up: Answers to Exercises
List Tutorial Exercise 9
------------------------
Step one is to convert our integer into vector notation.
1: 25129925999 3: 25129925999
. 2: 10
1: [11, 10, 9, ..., 1, 0]
.
25129925999 RET 10 RET 12 RET v x 12 RET -
1: 25129925999 1: [0, 2, 25, 251, 2512, ... ]
2: [100000000000, ... ] .
.
V M ^ s 1 V M \
(Recall, the `\' command computes an integer quotient.)
1: [0, 2, 5, 1, 2, 9, 9, 2, 5, 9, 9, 9]
.
10 V M % s 2
Next we must increment this number. This involves adding one to the
last digit, plus handling carries. There is a carry to the left out of
a digit if that digit is a nine and all the digits to the right of it
are nines.
1: [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1] 1: [1, 1, 1, 0, 0, 1, ... ]
. .
9 V M a = v v
1: [1, 1, 1, 0, 0, 0, ... ] 1: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1]
. .
V U * v v 1 |
Accumulating `*' across a vector of ones and zeros will preserve only
the initial run of ones. These are the carries into all digits except
the rightmost digit. Concatenating a one on the right takes care of
aligning the carries properly, and also adding one to the rightmost
digit.
2: [0, 0, 0, 0, ... ] 1: [0, 0, 2, 5, 1, 2, 9, 9, 2, 6, 0, 0, 0]
1: [0, 0, 2, 5, ... ] .
.
0 r 2 | V M + 10 V M %
Here we have concatenated 0 to the *left* of the original number; this
takes care of shifting the carries by one with respect to the digits
that generated them.
Finally, we must convert this list back into an integer.
3: [0, 0, 2, 5, ... ] 2: [0, 0, 2, 5, ... ]
2: 1000000000000 1: [1000000000000, 100000000000, ... ]
1: [100000000000, ... ] .
.
10 RET 12 ^ r 1 |
1: [0, 0, 20000000000, 5000000000, ... ] 1: 25129926000
. .
V M * V R +
Another way to do this final step would be to reduce the formula
`10 $$ + $' across the vector of digits.
1: [0, 0, 2, 5, ... ] 1: 25129926000
. .
V R ' 10 $$ + $ RET
File: calc.info, Node: List Answer 10, Next: List Answer 11, Prev: List Answer 9, Up: Answers to Exercises
List Tutorial Exercise 10
-------------------------
For the list