Programming Answer 10 (GNU Emacs Calc Manual)

Next: , Previous: , Up: Answers to Exercises [Contents][Index]


2.7.67 Programming Tutorial Exercise 10

Taking the derivative of a term of the form ‘x^n’ will produce a term like ‘n x^(n-1)’. Taking the derivative of a constant produces zero. From this it is easy to see that the ‘n’th derivative of a polynomial, evaluated at ‘x = 0’, will equal the coefficient on the ‘x^n’ term times ‘n!’.

(Because this definition is long, it will be repeated in concise form below. You can use C-x * m to load it from there. While you are entering a Z ` Z ' body in a macro, Calc simply collects keystrokes without executing them. In the following diagrams we’ll pretend Calc actually executed the keystrokes as you typed them, just for purposes of illustration.)

2: 5 x^4 + (x + 1)^2 3: 5 x^4 + (x + 1)^2
1: 6 2: 0
 . 1: 6
 .
 ' 5 x^4 + (x+1)^2 RET 6 C-x ( Z ` [ ] t 1 0 TAB

Variable 1 will accumulate the vector of coefficients.

2: 0 3: 0 2: 5 x^4 + ...
1: 5 x^4 + ... 2: 5 x^4 + ... 1: 1
 . 1: 1 .
 .
 Z ( TAB RET 0 s l x RET M-TAB ! / s | 1

Note that s | 1 appends the top-of-stack value to the vector in a variable; it is completely analogous to s + 1. We could have written instead, r 1 TAB | t 1.

1: 20 x^3 + 2 x + 2 1: 0 1: [1, 2, 1, 0, 5, 0, 0]
 . . .
 a d x RET 1 Z ) DEL r 1 Z ' C-x )

To convert back, a simple method is just to map the coefficients against a table of powers of ‘x’.

2: [1, 2, 1, 0, 5, 0, 0] 2: [1, 2, 1, 0, 5, 0, 0]
1: 6 1: [0, 1, 2, 3, 4, 5, 6]
 . .
 6 RET 1 + 0 RET 1 C-u v x
2: [1, 2, 1, 0, 5, 0, 0] 2: 1 + 2 x + x^2 + 5 x^4
1: [1, x, x^2, x^3, ... ] .
 .
 ' x RET TAB V M ^ *

Once again, here are the whole polynomial to/from vector programs:

C-x ( Z ` [ ] t 1 0 TAB
 Z ( TAB RET 0 s l x RET M-TAB ! / s | 1
 a d x RET
 1 Z ) r 1
 Z '
C-x )
C-x ( 1 + 0 RET 1 C-u v x ' x RET TAB V M ^ * C-x )

Next: Programming Tutorial Exercise 11, Previous: Programming Tutorial Exercise 9, Up: Answers to Exercises [Contents][Index]

AltStyle によって変換されたページ (->オリジナル) /